Problem with fragment shader

Hi,

I’m currently trying to get a coloured rectangle on the screen, but I just can’t get it to work.

Here I pasted some (almost) compilable code (you’d need to include GLFW, OpenGL and GLEW to run it), which illustrates my problem: The screen is just not drawn.
The code is of course only an example - normally I’d do more error checking, etc.-

The problem seems to lie within the fragment shader, but I really cannot spot the error. The attributes are setup correct (as far as I can see), the shaders compile, the program links, but the screen is empty.
Maybe one of you can spot the error?

Thanks in advance!
Greetings,
bnz

Does the cube draw using the fixed function pipeline instead?

Well, the cube is drawn, but only if if the color attribute is “put out of service”, like so:


    const char* vertexShaderSource =
        "#version 330
"
        "in vec4 inPosition;
"
        "//in vec4 inColor;
"
        "//out vec4 vertexColor;
"
        "void main()
"
        "{
"
        "    //vertexColor = inColor;
"
        "    gl_Position = inPosition;
"
        "}";
    const char* fragmentShaderSource =
        "#version 330
"
        "//in vec4 vertexColor;
"
        "out vec4 fragColor;
"
        "void main()
"
        "{
"
        "    //fragColor = vertexColor;
"
        "    fragColor = vec4(1.0, 1.0, 1.0, 1.0);
"
        "}";

Now the quad is drawn white, but strangely enough changing the fragColor value in the fragment shader doesn’t have any effect - which is why I guess that there is something wrong with it.
However, the shader-program still compiles and links…

Nobody?

Maybe someone could try the code and see if he can get it to work?

Try interleaving all of your color and positional information into one ARRAY_BUFFER, then attach/bind it to the VAO. That will work. You can also try enabling the attribute array before binding the ARRAY_BUFFER (just a guess). Read more about how VAO works on the wiki.

EDIT: After setting a VBO to a particular vertex attribute, you should call glBindBuffer(GL_ARRAY_BUFFER,0) to prevent what appears to be happening in your demo.

Apart from the glBindBuffer(GL_ARRAY_BUFFER,0) tip,
in Gl3.2+ this: glBindVertexArray(0); is kinda illegal, you always should have some valid VAO bound.
Other than that, I don’t see any probs with your code.
Oh, and do check what the values of colorLoc and positionLoc are.
Also, if there’s no error returned by glGetError(), then the thing is drawn. Probably in black; so always set clear-color to something like grey :slight_smile: .

Thanks for the answers.

EDIT: After setting a VBO to a particular vertex attribute, you should call glBindBuffer(GL_ARRAY_BUFFER,0) to prevent what appears to be happening in your demo.

I did that, but it didn’t change anything. :frowning:

in Gl3.2+ this: glBindVertexArray(0); is kinda illegal, you always should have some valid VAO bound. […]
Also, if there’s no error returned by glGetError(), then the thing is drawn. Probably in black; so always set clear-color to something like grey smile .

Okay, I did this - still nothing.

Here’s a revised version of the above code, just to make sure that my changes are correct:
Link

Try interleaving all of your color and positional information into one ARRAY_BUFFER, then attach/bind it to the VAO. That will work.

Okay, I’ll try that tomorrow.

Greetings,
bnz