details about glColorMaterial

hello,
i’m using the followinf commands to specify the action of the next glColors operations :

float theBackColor[4]; theBackColor[0] = 0.f; theBackColor[1] = 0.f; theBackColor[2] = 0.f; theBackColor[3] = 1.f; glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, theBackColor );
glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, theBackColor); glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, theBackColor); glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, theBackColor);

glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE);

It works on some video cards, but on some others, a previously used color is also used.

I found that specifying glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE); solves the problem.
And also, if I call glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE); before calling glEnable(GL_COLOR_MATERIAL);, the result is correct.

Is is something known that one must call glColorMaterial BEFORE enabling GL_COLOR_MATERIAL ?
And why is-it card dependant ?

thanks much
Adrien

Yes, glColorMaterial has to be called before calling glEnable(GL_COLOR_MATERIAL). If you don’t, the behaviour is not specified. Some drivers/cards just happen to not care about the order, so you sometimes get away with it.

thanks much, but how did you know that ? I didn’t read it anywhere…

It’s mentioned in the manual page for glColorMaterial under NOTES, second paragraph.

glColorMaterial

From the OpenGL 1.5 spec, page 62:

For instance,
calling
ColorMaterial(FRONT, AMBIENT)
while COLOR MATERIAL is enabled sets the front material acm to the value of the
current color.
Also there’s a data flow diagram on the next page that seems a very clear description of what is happening. Also there’s no mention about an “active” copy of the state like in the VBO spec.

This sounds to me that glColorMaterial should work after GL_COLOR_MATERIAL is enabled, it’s very well defined what should happen. Could be a driver bug… Or am I misinterpreting the spec here?

Originally posted by memfr0b:
Yes, glColorMaterial has to be called before calling glEnable(GL_COLOR_MATERIAL). If you don’t, the behaviour is not specified. Some drivers/cards just happen to not care about the order, so you sometimes get away with it.
Wrong. Of couse it is specified and the default is (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE).

Pitfall #14 explains why you better set it first and enable it afterwards:
http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/
I’d recommend to do this for any enable.