PDA

View Full Version : Implementing GL_COLOR_MATERIAL in vertex shader.



JCipriani2
05-13-2008, 10:40 PM
I'm trying to replicate fixed functionality directional lighting in a vertex shader. I am also trying to make it behave as if GL_COLOR_MATERIAL was enabled with GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE. I was having trouble making the output match the fixed functionality exactly and I suspect that the problem was my use of gl_FrontLightModelProduct.sceneColor in the output color calculation.

The Orange Book states that gl_FrontLightModelProduct.sceneColor is equivalent to:


gl_FrontMaterial.emission + gl_FrontMaterial.ambient * gl_LightModel.ambient

So I have two questions:

1) I've observed that with GL_COLOR_MATERIAL glEnabled, gl_FrontMaterial.ambient does *not* track the vertex color. Instead I have to implement this in my shader by using gl_Color instead of gl_FrontMaterial.ambient (same goes for .diffuse). GL_COLOR_MATERIAL does not look like it has any effect on the materials available to the vertex shader. I guess this seems reasonable... is this correct or am I missing something?

2) Does this mean I can't use gl_FrontLightModelProduct.sceneColor, and instead must always calculate it with "gl_FrontMaterial.emission + gl_Color * gl_LightModel.ambient" (or in my case, material emission is [0,0,0,0] so I can just drop that component from the equation)?

Using gl_Color instead of the material's ambient and diffuse colors, and also not using gl_FLMP.sceneColor, produces precisely the same output as the fixed functionality -- so it seems right. I just want to double check to make sure I'm not missing something. The Orange Book didn't mention either of these things, I kind of had to wing it so confidence is low.

Thanks,
Jason

Relic
05-14-2008, 01:31 AM
Correct and yes.
LightModelProducts are uniforms and cannot track per vertex changes.
For identical behavior mind that the alpha component in the fixed function pipeline always comes from the diffuse part of the primary color's lighting calculation (OpenGL 2.1 spec chapter 2.14 page 63).

cmdtower
05-21-2008, 10:40 PM
I kind of have the same issue. I am simply trying to get my material (textures) to respond to glColorxx commands in immediate mode.

I have used this once when initializing:

glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);

glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); // color 1.0f is fully opaque, 0.0f is transparent
glEnable(GL_BLEND);

It use to work.

CMD