Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the relationship between gl_Color and gl_FrontColor in both vertex and fragment shaders

I have pass-through vertex and fragment shaders.

vertex shader

void main(void)
{
    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

fragment shader

void main(void)
{
    gl_FragColor = gl_Color;
}

Those produce empty rendering (black not background color like glClearBuffer does).

If I modify the vertex shader to set the gl_FrontColor to gl_Color it does render untouched OpenGl buffer ... with is the expected behavior of pass-through shaders.

void main(void)
{
    gl_FrontColor = gl_Color; //Added line
    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

I am confused, how settings the gl_FrontColor in the vertex shader can change the value of the gl_Color in the fragment one ? What I am missing ?

like image 417
Thomas Vincent Avatar asked Jun 21 '11 18:06

Thomas Vincent


People also ask

What happens between vertex shader and fragment shader?

There are several kinds of shaders, but two are commonly used to create graphics on the web: Vertex Shaders and Fragment (Pixel) Shaders. Vertex Shaders transform shape positions into 3D drawing coordinates. Fragment Shaders compute the renderings of a shape's colors and other attributes.

What is a fragment in a fragment shader?

A Fragment Shader is the Shader stage that will process a Fragment generated by the Rasterization into a set of colors and a single depth value. The fragment shader is the OpenGL pipeline stage after a primitive is rasterized. For each sample of the pixels covered by a primitive, a "fragment" is generated.

What does a vertex shader do?

Vertex shaders typically perform transformations to post-projection space, for consumption by the Vertex Post-Processing stage. They can also be used to do per-vertex lighting, or to perform setup work for later shader stages.

Is fragment shader a pixel shader?

A fragment shader is the same as pixel shader.


2 Answers

gl_Color means different things in different places.

In the vertex shader, gl_Color represents the primary per-vertex color attribute passed by the user. This is set using glColor* calls or array data fetched by glColorPointer.

In the fragment shader, gl_Color represents the interpolated color for the facing of the triangle being rendered. Remember that triangles have a front-face and a back-face. If you enable face culling, then all faces of one kind or the other (or both) are not rendered. However, if you turn off face culling, then both sides are rendered.

It is often useful to have different per-vertex output values based on the particular facing of the triangle. The primary color has a front color and a back color, representing the color for front-facing triangles and back-facing triangles. The vertex shader outputs for these are gl_FrontColor and gl_BackColor.

If you are doing two-sided rendering, you will need to set both of these values in order for the fragment shader's gl_Color input to mean anything. If you are only doing front-face rendering, then you only need to set gl_FrontColor.

like image 160
Nicol Bolas Avatar answered Nov 15 '22 04:11

Nicol Bolas


gl_Color is the Color you passed in your source to the vertices, so it is an attribute. gl_FrontColor (and also BackColor) are varyings that set the gl_Color in the fragmentshader depending on which side of the primitive you see. So if you see front gl_Color will be equal to gl_FrontColor...

like image 43
Nobody moving away from SE Avatar answered Nov 15 '22 04:11

Nobody moving away from SE