Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL glEnable(GL_COLOR_MATERIAL) followed by glDisable(GL_COLOR_MATERIAL)

I was drawing, simultaneously, a vertex colored cube next to a textured cube, with glDrawArrays(), and found that with lighting enabled the textured cube gets "slightly brighter".

After some debugging I found that the very first moment that glEnable(GL_COLOR_MATERIAL) is called, even if glDisable(GL_COLOR_MATERIAL) is immediately called thereafter, causes a "slightly brighter" effect on textured cube!

Here is the short-description:

glEnable(GL_COLOR_MATERIAL);
glDisable(GL_COLOR_MATERIAL);
(...)
glDrawArrays(GL_QUADS, 0, n);

If glEnable(GL_COLOR_MATERIAL) is not called at all, the cube is drawn with a yellow color. And if glEnable(GL_COLOR_MATERIAL) is called, even if followed by glDisable(GL_COLOR_MATERIAL), the cube is drawn slightly brighter yellow, and I can not switch back to the "initial darker yellow cube color".

Can you please tell me if this is the expected behavior?

like image 844
luiez Avatar asked Nov 13 '22 11:11

luiez


1 Answers

this is not exactly the expected behavior, but it should be easy to fix. The GL_COLOR_MATERIAL enables overwriting material properties with vertex colors. The bug is that the colors are not only being rewritten on vertex color specification, but also on color material enable (my guess is that it might have been done intentinaly to prevent bugs).

All you need to do to make your cube slightly darker again is to restore the default color material properties (that would probably be diffuse and ambient colors), using glMaterialfv(). You can find the default material colors here.

like image 55
the swine Avatar answered Jan 05 '23 18:01

the swine