Problem with shader tutorial

Hello. I was following a tutorial on openGL shaders, and I ran into a problem. Im trying to pass the color information into the shader while still using the fragment shader to apply lighting onto that color. However as soon as I take in the color using…

gl_FrontColor = glColor;

The object takes on a solid color and ignores the fragment shader, even if the fragment shader does nothing with the color itself. The shading from the fragment shader works great until I try to take the color in. How can I get it to both take in color while still using the fragment shader code?

// simple toon vertex shader
// www.lighthouse3d.com
varying vec3 normal, lightDir;
varying float red, green, blue, alpha;

void main()
{
lightDir = normalize(vec3(gl_LightSource[0].position));
normal = normalize(gl_NormalMatrix * gl_Normal);

gl_FrontColor = glColor;
gl_Position = ftransform();

}

// simple toon fragment shader
// www.lighthouse3d.com
varying vec3 normal, lightDir;

void main()
{
float intensity;
vec3 n;
vec4 color;
n = normalize(normal);
intensity = max(dot(lightDir,n),0.0);
color = vec4(intensity/2, intensity/2, intensity, 1.0f);
gl_FragColor = color;
}

My recollection from back when using GLSL’s legacy vertex attribs and varyings for compatibility (GLSL 1.2 or earlier) is that in the vertex shader you want this:

gl_FrontColor = gl_Color;

and in the fragShader you want this:

gl_FragColor = gl_Color;

Thing is, the front/back facing decision is made by the GPU between the vertex shader and frag shader. So either gl_FrontColor or gl_BackColor will be fed into the frag shader as gl_Color. You only need to populate gl_FrontColor in the vtx shader if you are telling the GPU to cull back faces (i.e. enable CULL_FACE and set glCullFace to GL_BACK (the default).

Note that gl_Color in the vtx shader indices the incoming vertex attribute you set with glVertex* on the C++ side, while gl_Color in the fragment shader indicates the selected vertex color varying output from the vertex shader (gl_FrontColor or gl_FragColor).

Just checked my old GLSL Programming Guide, rev 1 and that looks like it.

Nowadays, you’d just declare your own vertex attribs and varyings and not depend on the old built-in names, which you can only use in GLSL 1.2 an earlier IIRC.

Thanks for the reply- Im not sure that it addresses my issue however. I already have the gl_FrontColor part as you can see above, and that does bring the color in as far as the vertex shader, but the intensity calculations in the fragment shader stop working. Even if I try to set to a new static color in the fragment shader all it does is give me a flat color image of the original color. Not sure how putting in the second line could help given that I dont want the return color from the fragment shader to be equal to the orignal color alone.

From the rest of what youre saying- it sounds like this approach is mostly out of date. Are you saying that I should create my own color variable and pass it in myself instead?

Yes, the GLSL built-in variables you are using are deprecated in more recent versions of GLSL.

I don’t really understand the explanation of your problem, when you say “take the color in.” It would help if you presented the fragment shader code for both the “before” and “after,” so to speak.

But I think I have an idea of what you’re getting at. The way you combine the object’s inherent color, and the diffuse light component, is multiply the base color by the diffuse light’s intensity. Typically, you would also add some ambient light component to the diffuse light component before multiplying the base color, as well.

Well- I just figured out that it works if I use gl_Color and not glColor. I copied that code from the tutorial, but I guess the name could have changed.

Sigh… well perhaps Ill just move to another tutorial I found if this one is too old. Is the material at Tutorial2: VAOs, VBOs, Vertex and Fragment Shaders (C / SDL) - OpenGL Wiki out of date? The structure to the shader is quite different there, though similar to what I saw with the shaders in xna c#.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.