Search code examples
opengllight

OpenGL: Light source that follows the camera?


Guys, how can this be. But in my previous projects, it wasn't. And I don't know how I attempt this effect. Tell me the truth, please.


Solution

  • Section 18.050, How can I make my light move or not move and control the light position?:

    First, you must understand how the light position is transformed by OpenGL.

    The light position is transformed by the contents of the current top of the ModelView matrix stack when you specify the light position with a call to glLightfv( GL_LIGHTn, GL_POSITION, ... ). If you later change the ModelView matrix, such as when the view changes for the next frame, the light position isn't automatically retransformed by the new contents of the ModelView matrix. If you want to update the light’s position, you must again specify the light position with a call to glLightfv( GL_LIGHTn, GL_POSITION, ... ).

    ...

    • How can I make my light position stay fixed relative to my eye position? How do I make a headlight?

    You need to specify your light in eye coordinate space. To do so, set the ModelView matrix to the identity, then specify your light position. To make a headlight (a light that appears to be positioned at or near the eye and shining along the line of sight), set the ModelView to the identity, set the light position at (or near) the origin, and set the direction to the negative Z axis.

    When a light’s position is fixed relative to the eye, you don't need to respecify the light position for every frame. Typically, you specify it once when your program initializes.