PDA

View Full Version : Problem with fragment shader



bnzification
05-16-2011, 12:13 PM
Hi,

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

Here (http://pastebin.com/vpziHKvq) 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

BionicBytes
05-16-2011, 12:32 PM
Does the cube draw using the fixed function pipeline instead?

bnzification
05-16-2011, 01:32 PM
Well, the cube is drawn, but only if if the color attribute is "put out of service", like so:



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

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...

bnzification
05-17-2011, 08:05 AM
Nobody?

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

DarkGKnight
05-17-2011, 08:42 AM
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 (http://www.opengl.org/wiki/Vertex_Array_Object).

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.

Ilian Dinev
05-17-2011, 01:24 PM
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 :) .

bnzification
05-17-2011, 02:26 PM
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. :(


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 (http://pastebin.com/Qktqr9ys)


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