PDA

View Full Version : Why does not gl_Color work?



ei05tbe
08-07-2010, 12:14 PM
I am trying to create a simple shader. Can you tell me why gl_FragData[2] = vec4(1,0,0,0) works as expected, but gl_FragData[2] = gl_Color don't. When using gl_Color nothing(except objects rendered whit another shader program ) shows up on the screen.


1 varying vec3 position, normal;
2
3 void main(void) {
4 position = vec3(gl_ModelViewMatrix * gl_Vertex);
5 normal = gl_NormalMatrix * gl_Normal;
6 gl_Position = ftransform();
7 }




1 #extension GL_ARB_draw_buffers : enable
2
3 varying vec3 position, normal;
4
5 void main(void) {
6 gl_FragData[0] = vec4(position.xyz, 0.0);
7 gl_FragData[1] = vec4(normalize(normal.xyz), 0.0);
8 gl_FragData[2] = gl_Color;//vec4(1,0,0,0); //wont the current color;
9 }

Alfonse Reinheart
08-07-2010, 03:19 PM
Because your vertex shader did not write to the color.

ei05tbe
08-08-2010, 02:52 AM
Thanks
How do I write the glColor I am currently rendering with to the fragment shader?


gl_FrontColor = gl_Color;

This will give gl_FrontColor the value of gl_Color not the other way around?

void main() {
// Set the front color to the color passed through with glColor*f
gl_FrontColor = gl_Color;
// Set the position of the current vertex
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}



Edit: Well gl_FrontColor = gl_Color; works but how?

kyle_
08-08-2010, 03:29 AM
gl_Color is atrtibute name in vertex shader.
gl_FrontColor and gl_BackColor are varying names in vertex shader.
gl_Color is varying name in fragment shader (its computed based on frontface status of your triangle and gl_FrontColor & gl_BackColor).

Use your own varying and avoid the confusion.