Reputation: 185
I am trying to output gl_PrimitiveID as a color in a fragment shader. However it's always equal to 0.
Here is my fragment shader:
void main(void)
{
if(gl_PrimitiveID == 0)
gl_FragColor.rgba = vec4(1, 0, 0, 1); // red for 0
else
gl_FragColor.rgba = vec4(0, 0, 1, 1); // blue otherwise
}
and vertex shader
void main(void)
{
gl_Position = ftransform();
}
The output color for triangles is always red. I am drawing the triangles with glDrawElements and the render is ok when using the default shaders.
What am i doing wrong?
Here is how I draw the triangles:
glBindBuffer(GL_ARRAY_BUFFER, vertexPosBuffer);
glVertexPointer(3, GL_FLOAT, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, vertexNormalBuffer);
glNormalPointer(GL_FLOAT, 0, NULL);
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_NORMAL_ARRAY );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
glDrawElements(GL_TRIANGLES, countVertices, GL_UNSIGNED_INT, (void*)0);
Upvotes: 3
Views: 2564
Reputation: 185
After hours of banging my head against the wall, I realized my vertex shader was not attached to the right program id.
Only the fragment shader was attached. It seems gl_PrimitiveID does not auto increment if the vertex shader is not provided.
+1 to Andon and ratchet for pointing me to the right direction.
Upvotes: 3