Fragment shaders and gl_FrontMaterial

I am working with an nVIDIA GeForce FX 5900 Ultra and have run into the
following behavior.

I want to write a vertex/fragment shader pair which mimics OpenGL’s
lighting, but at the fragment shader level. What this will mean is per-fragment
lighting which will produce superior spotlight effects.

What I ran into is that much of the OpenGL state data appears inconsistent
to the fragment shader. I conducted the following experiment.

First, I used this simple vertex shader:

void main(void)
   {
   gl_FrontColor = gl_FrontMaterial.ambient;
   gl_BackColor = gl_FrontColor;

   gl_Position = ftransform();
   }

With this simple fragment shader:

void main(void)
   {
   gl_FragColor = gl_Color;
   }

and everything worked as I expected.

Next I used this simple vertex shader:

void main(void)
   {
   gl_Position = ftransform();
   }

With this simple fragment shader:

void main(void)
   {
   gl_FragColor = gl_FrontMaterial.ambient;
   }

and the results were inconsistent (sometimes brown, sometimes yellow, then
white) and never what I expected.

This leads me to ask, does a fragment shader have access to the OpenGL
state? If not, where was my compiler error? If so, is this a hardware
problem?

I realize that I can pass a lot of information down to the fragment shader
to circumvent the problem if necessary, but I am not sure how to pass an
array like gl_LightSource. Does anyone know?

Thanks,

  • Moe -

You should be able to access many of the GL states.
If you see inconsistent colors like that, then it’s probably a driver bug.

If you want to pass them yourself, then use uniforms. All of those GL states just need a vec4 and in some cases vec3 is enough like in the case of material.ambient.

Thanks V-man.

This is, in fact, what I have done. More experimentation seems to imply that the fragment shaders cannot use the material properties reliably, whereas the light properties seem OK.

Hopefully these are growing pains and not an attempt to promote Cg via lackluster OpenGL support.

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