3

我正在尝试使用 GLFW/GLEW 添加着色器。

我收到一个错误,即着色器已加载,但它们没有有效的目标代码。

这是我用于加载着色器的代码:

class SHADER {
public:

void LoadShaders(const char *vertexFile, const char *fragmentFile);

char *vertexShader;
char *fragmentShader;

private:

int Load(const char *filename, char*&shaderSource);
fstream file;
};

int SHADER::Load(const char *filename, char *&shaderSource) {
file.open(filename, ios::in);
if(file.is_open()) {
    file.tellg();
    file.seekg(0,ios::end);
    unsigned long len = file.tellg();
    file.seekg(ios::beg);

    if(len == 0 ) {
        return -2;
    } else {
        shaderSource = new char[len + 1];
        file.read(shaderSource,len);
        file.close();
        printf("%s\n",shaderSource);
    }
} else {
    return -1;
}
return 0;
}

void SHADER::LoadShaders(const char *vertexFile, const char *fragmentFile) {
int resultVertex = this->Load(vertexFile, vertexShader);
int resultFragment = this->Load(fragmentFile, fragmentShader);
if(resultVertex ==0 && resultFragment ==0) {
    printf("Shaders loaded succesfully.\n");
}
if(resultVertex == -2) {
    printf("VertexShader is empty!\n");
}
if(resultFragment == -2) {
    printf("FragmentShader is empty!\n");
}
if(resultVertex == -1) {
    printf("Unable to load VertexShader!\n");
}
if(resultFragment == -1) {
    printf("Unable to load FragmentShader!\n");
}
}

这是初始化着色器的代码:

SHADER Shaders;
GLhandleARB vertexShader, fragmentShader, shaderProgram;

Shaders.LoadShaders("vertexShader.vert","fragmentShader.frag");

const char* vertTemp = Shaders.vertexShader;
vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
glShaderSourceARB(vertexShader, 1, &vertTemp, NULL);
glCompileShaderARB(vertexShader);
traceShaderInfoLog(vertexShader);

const char* fragTemp = Shaders.fragmentShader;
fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
glShaderSourceARB(fragmentShader, 1, &fragTemp, NULL);
glCompileShaderARB(fragmentShader);
traceShaderInfoLog(fragmentShader);

delete[] Shaders.vertexShader;
delete[] Shaders.fragmentShader;

shaderProgram = glCreateProgramObjectARB();
glAttachObjectARB(shaderProgram,vertexShader);
glAttachObjectARB(shaderProgram,fragmentShader);
glLinkProgramARB(shaderProgram);
traceProgramInfoLog(shaderProgram);

glUseProgramObjectARB(shaderProgram);

这是vertexShader.vert和fragmentShader.frag:

void main(){
gl_Position = ftransform();
}
void main(){
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

这是我打印日志时遇到的错误,我在脚本末尾得到这个非常奇怪的符号,每次我再次编译另一个符号时:

void main(){
    //gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    gl_Position = ftransform();
}┘
void main(){
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}♣
Shaders loaded succesfully.
Vertex shader was successfully compiled to run on hardware.

Fragment shader failed to compile with the following errors:
ERROR: 0:3: error(#132) Syntax error: '<' parse error
ERROR: error(#273) 1 compilation errors.  No code generated

Fragment shader(s) were not successfully compiled before glLinkProgram() was called.
Link failed.
4

2 回答 2

4

您的 Load 函数很可能是问题所在:签名应为

int SHADER::Load(const char *filename, char *&shaderSource) // Note the &

快速修复。您在该函数中分配内存并重新分配给指针-您仅将源加载到该地址-但这永远不会离开该函数。

由于内存初始化为零,编译器很可能会得到一个空字符串(您是否在调试中运行它?)并且不会失败,因为有时这没关系。但是链接器注意到实际上没有任何代码可以链接!

于 2012-06-20T15:53:50.113 回答
0

当链接失败时,检索链接错误日志,这可能有额外的线索。

glGetProgramInfoLog

于 2012-06-20T15:24:39.763 回答