OpenGL light flicker

Started by
11 comments, last by Phynix 14 years, 10 months ago
I'm new to opengl lighting, and when I finally made a light in an FPS environment, it flickered. I mean, whenever you changed position in the game (and thus, changed the translation/rotation of all objects in the scene), the light would turn on and off repeatedly. I have absolutely no clue what's happening. Please help!
Advertisement
Im guessing
A/ its a positional light
B/ after youve moved the 'camera' then u have to reposition the light again
Well, it is a positional light, and I already change the light position when I move the camera. Part of my drawing code is below, and surfaces[0].draw just draws a triangle. The normals are accurate.
<code>

glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glDisable(GL_BLEND);

glRotatef(-camera.rotation.x,1,0,0);
glRotatef(-camera.rotation.y,0,1,0);
glRotatef(-camera.rotation.z,0,0,1);
glTranslatef(-camera.location.x,-camera.location.y-6,-camera.location.z);
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
glNormal3f(surfaces[0].normal.x,surfaces[0].normal.y,surfaces[0].normal.z);
surfaces[0].draw();

</code>

In the beginning of the program, I say this:
<code>
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
</code>

And the values for these lighting variables are:

<code>
GLfloat LightAmbient[]= { 0.9f, 0.9f, 0.9f, 1.0f };
GLfloat LightDiffuse[]= { 0.0f, 1.0f, 0.0f, 1.0f };
GLfloat LightPosition[]= { -100.0f, 0.0f, 100.0f, 1.0f };
</code>
Note that glTranslate and its mates do also affect the position of the light you set afterwards. You do not have to manually adjust LightPosition. If you translate the light manually by changing your LightPosition according to the player movement then you get the translation (and rotation) added twice.
------------------------------------I always enjoy being rated up by you ...
But then if I don't set the location of the light after the translate and rotates, then it always seems as if the light is right under the player.
Oh I understood that this is the effect you wanted to achieve. If the light should be stationary you should not have to set its position each frame. I am not entirely sure but I would say that setting the light position once should be enough.

Anyway, if it is not then simply push the MODELVIEW matrix, load an identity matrix, set your light position and pop the MODELVIEW. This avoids any need to modify your light position for stationary lights to compensate for players movements. Or just set the light position right after loading the identity. For the sake of showing how to do it the following snippet chooses the more complex solution in case you activate your light in a different place than where you set the viewer transformation. Hence the duplicated or unnessesary complicated code lines:

glMatrixMode(GL_MODELVIEW);glLoadIdentity();glDisable(GL_BLEND);glRotatef(-camera.rotation.x,1,0,0);glRotatef(-camera.rotation.y,0,1,0);glRotatef(-camera.rotation.z,0,0,1);glTranslatef(-camera.location.x,-camera.location.y-6,-camera.location.z);glPushMatrix(); // <- saves viewer transformationglMatrixMode(GL_MODELVIEW );glLoadIdentity(); // <- sets "no transformation" to take light position as isglLightfv(GL_LIGHT1, GL_POSITION,LightPosition);glNormal3f(surfaces[0].normal.x,surfaces[0].normal.y,surfaces[0].normal.z);glPopMatrix(); // <- restores viewer transformation for renderingsurfaces[0].draw();


Using this code there is no need to change LightPosition. Just initialize it to the light's world position and only change it if your light source's world position changes which is usually not the case.
------------------------------------I always enjoy being rated up by you ...
I'm not looking to make a light that is stationary in perspective to the user (e.g. a headlight, as opengl.org calls it). I'm trying to make a light that stays relative to the environment. Sort of like your house light, or a lamp in, let's say, Halo or Doom. You should be able to walk around the light. But the thing is that when I achieve this effect, the light repeatedly turns on and off when the camera is rotated/translated.
OK, I implemented the code, and now another thing happens: The light doesn't seem to be in the same world space. Now, it always is right under the camera.
OK, I got it, after a while of experimenting. The problem was that the light was right on the surface. As in, the surface and light were overlapping. But now I have another question. Why is it that when I position the light somewhere other than the corner of the triangle, the light doesn't seem as bright?
OpenGL does lighting by vertex and interpolates those values across the triangle or quad. Therefor when it is just outside the triangle corner it is adding the most light to that vertex. If you want to do per pixel light, ie the light circle of a flash light, you will have to write a shader.

Quote:Original post by Phynix
OK, I got it, after a while of experimenting. The problem was that the light was right on the surface. As in, the surface and light were overlapping. But now I have another question. Why is it that when I position the light somewhere other than the corner of the triangle, the light doesn't seem as bright?


This topic is closed to new replies.

Advertisement