Lighting in OpenGL (C++)
Hello everyone,
I'm new to both OpenGL and Gamedev. I was playing around with NeHe's tutorials and I figured I could make the light source from lesson 7 move left and right, up and down, etc...
But, the only way I could do this was to move the glLightfv function call from initGL to DrawGLScene. However, when I do this, only the "front" face of the box is illuminated (or, what was originally the front) and the rest are dark as if there is no light hitting them. When I try to rotate the box, the original face stays illuminated (even though it can be on the opposite side). The light still moves correctly, but I know I'm missing something.
Can someone provide any help?
Thanks!
Lighting positions are transformed by the current matrix - try moving the glLightfv call before the glRotatef call (or after if it's already before)
Thank you, that fixed the problem -- my only question is why? If I used rotate before the lighting function, what was happening that made it break? I'm not really sure how the matrices play into this (I am familiar with matrix algebra, just not how it applies to this).
Thanks again for your help!
Thanks again for your help!
It's as chowe said (lighting positions are transformed by the current matrix).
When you specify the lights position with glLightfv, it interprets the array as the object coordinates of the light. So these coordinates undergo the same transformation (the modelview transformation) that geometric objects would. In this case, if you rotate before you specify the light, the lights get rotated.
Depending on the actual circumstances the light could end up right on one face of the box and remain there, which would explain what you saw. If you want to post DrawGLScene I'll try to explain more.
When you specify the lights position with glLightfv, it interprets the array as the object coordinates of the light. So these coordinates undergo the same transformation (the modelview transformation) that geometric objects would. In this case, if you rotate before you specify the light, the lights get rotated.
Depending on the actual circumstances the light could end up right on one face of the box and remain there, which would explain what you saw. If you want to post DrawGLScene I'll try to explain more.
Awesome, I get it! The light moves with the most recent transformation -- I confirmed this by specifying a triangle right where the light is so I could see the "light" move, and sure enough, it moved along with the transformation (rotation). Moving the function call before the rotate call works, and so does loading the identity matrix with glLoadIdentity().
Thank you all so much for your help!
Thank you all so much for your help!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement