OpenGL, Static Arrays and glMaterialfv
I stumbled upon weird behavior of OpenGL (OGL 1.4, Catalyst 8.612, GCC if that matters): I was setting material properties with glMaterialfv and for some reason this did not change the parameters if done multiple times in succession. I.e. when I drew two versions of the same object on the screen side by side with different material parameters, they both looked the same.
The reason seems to be I was using a static array to store the parameters like this:
void set_params(const Color& diffuse)
{
static float d[4] = {diffuse.r, diffuse.g, diffuse.b, diffuse.a};
glMaterialfv(GL_FRONT, GL_DIFFUSE, d);
}
The only explanation I can think of is OpenGL doesn’t copy or use the values before glMaterialfv returns but instead reads them later, at which time there could be different values in the array (because it’s declared static) set for some other object being drawn. But that doesn’t explain why it can read the array (which AFAIK will be located on stack) later because the address to the then-valid location on the stack most likely won’t point to the parameters. Maybe the driver assumes anything that’s not on the stack can be used later and stuff located on stack will be copied. Who knows?
In any case, not declaring the array as static fixed the problem.