Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a fixed light source that preserves its world coordinates
    primarykey
    data
    text
    <p>I want to put a light in the scene that is fixed at a certain spot. Here's what the <a href="http://www.opengl.org/resources/faq/technical/lights.htm" rel="nofollow">OpenGL site</a> has to say about this:</p> <blockquote> <p><strong>How can I make my light stay fixed relative to my scene? How can I put a light in the corner and make it stay there while I change my view?</strong></p> <p>As your view changes, your ModelView matrix also changes. This means you'll need to respecify the light position, usually at the start of every frame. A typical application will display a frame with the following pseudocode:</p> <ul> <li><em>Set the view transform.</em></li> <li>*Set the light position //glLightfv(GL_LIGHT_POSITION,…)*</li> <li><em>Send down the scene or model geometry.</em></li> <li><em>Swap buffers.</em></li> </ul> <p>If your light source is part of a light fixture, you also may need to specify a modeling transform, so the light position is in the same location as the surrounding fixture geometry.</p> </blockquote> <p>So I added this to the <code>paintGL()</code> function.</p> <pre><code>glMatrixMode(GL_MODELVIEW); glPushMatrix(); gluLookAt(0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); GLfloat l0pos[] = {0.0, 0.0, 0.0, 1.0}; glLightfv(GL_LIGHT0, GL_POSITION, l0pos); glPopMatrix(); </code></pre> <p>It seems okay when I'm standing still, but when I move the camera's eye position or the camera's center position, the light flickers. What am I doing wrong?</p> <hr> <p><strong>Update2</strong>: I ended up using this code, but I'm still having issues. The light no longer moves, but its position isn't accurate. The matrix on top of the stack is the matrix generated by a <code>gluLookAt</code> command that represents the camera position.</p> <pre><code>glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslatef(xCoordinate, yCoordinate, zCoordinate); GLfloat lpos[] = {0.0, 0.0, 0.0, w}; glLightfv(GL_LIGHT0, GL_POSITION, lpos); glPopMatrix(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload