问题
I am trying to add shaders with GLFW/GLEW.
I am getting an error that the shaders are loaded but they don't have valid object code.
This is the code I have for loading the shaders:
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");
}
}
This is the code for initializing the shaders:
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);
This is vertexShader.vert and fragmentShader.frag:
void main(){
gl_Position = ftransform();
}
void main(){
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
And this is the error I get when I print the log, I get this really weird symbol at the end of the scripts which is every time I compile again another symbol:
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.
回答1:
Your Load function is most likely the problem: the signature should read
int SHADER::Load(const char *filename, char *&shaderSource) // Note the &
for a quick fix. You're allocating the memory in that function and reassigning to the pointer - you only load the source to that address - but that never leaves the function.
The compiler most likely gets an empty string due to memory being zero initialized (are you running this in debug?) and doesn't fail because that's sometimes okay. But the linker notices that there's not really any code to link!
回答2:
When it fails to link, retrieve the link error log, which may have additional clues.
See glGetProgramInfoLog.
来源:https://stackoverflow.com/questions/11122815/opengl-shaders-dont-link-with-shader-program