GL_POSITION retrieved with glGetLightfv wrong
Alec Jacobson
October 21, 2012
I recently got snagged on a bug in my code. I was setting the position of a light at every frame in OpenGL with:
const float lp[4] = {0,0,1,1};
glLightfv(GL_LIGHT0,GL_POSITION,lp);
This produced the correct result the first frame but then subsequent frames came out wrong. To debug this I added:
float cp[4];
glGetLightfv(GL_LIGHT0,GL_POSITION,lp);
// print lp
// print cp
Which would print:
lp: [0 0 1 1]
cp: [0 0 1 1]
lp: [0 0 1 1]
cp: [0 0 -29 1]
Finally I discovered it was because I was setting the light position before setting up my model view matrix. I suppose the first time through it had an identity matrix there as default, but then it was corrupted by the time of the second draw.
Note: At first, to make matters worse, I was using lp[3]
instead of lp[4]
.