3

I'm trying to step into using shaders with OpenGL, but it seems I can't compile the shader. More frustratingly, it also appears the error log is empty. I've searched through this site extensively and checked many different tutorials but there doesn't seem to be an explanation for why it fails. I even bought a book dealing with shaders but it doesn't account for vanishing log files.

My feeling is that there must be an error with how I am linking the shader source.

//Create shader GLuint vertObject; vertObject = glCreateShader(GL_VERTEX_SHADER); //Stream ifstream vertfile; //Try for open - vertex try { //Open vertfile.open(vertex.c_str()); if(!vertfile) { // file couldn't be opened cout << "Open " << vertex << " failed."; } else { getline(vertfile, verttext, '\0'); //Test file read worked cout << verttext; } //Close vertfile.close(); } catch(std::ifstream::failure e) { cout << "Failed to open" << vertfile; } //Link source GLint const shader_length = verttext.size(); GLchar const *shader_source = verttext.c_str(); glShaderSource(vertObject, 1, &shader_source, &shader_length); //Compile glCompileShader(vertObject); //Check for compilation result GLint isCompiled = 0; glGetShaderiv(vertObject, GL_COMPILE_STATUS, &isCompiled); if (!isCompiled) { //Did not compile, why? std::cout << "The shader could not be compiled\n" << std::endl; char errorlog[500] = {'\0'}; glGetShaderInfoLog(vertObject, 500, 0, errorlog); string failed(errorlog); printf("Log size is %d", failed.size()); } printf("Compiled state = %d", isCompiled); 

The shader code is as trivial as can be.

void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; } 

I can't get either my fragment or my vertex shader to compile. If I can get the error log to show, then at least I will be able to start error checking my own work. For now, though, I can't even get the errors.

0

1 Answer 1

4

It seems that the reason glCompile is failing without a log in this case is because I attempted to perform this action before the OpenGL context had been initialised (glutCreateWindow etc).

If anybody else gets this problem in future, try getting your version just before you try to create any GLSL objects.

 printf("OpenGL version is (%s)\n", glGetString(GL_VERSION)); 

If you get "OpenGL version is (null)", then you don't have a valid context to create your shaders with. Find where you create your context, and make sure your shader creation comes afterwards.

Sign up to request clarification or add additional context in comments.

1 Comment

What an obvious thing that I didn't check, was scratching my head on this for a while. Thanks :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.