Sign in to follow this  
Phynix

OpenGL OpenGL light flicker

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Im guessing
A/ its a positional light
B/ after youve moved the 'camera' then u have to reposition the light again

Share this post


Link to post
Share on other sites
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>

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 transformation

glMatrixMode(GL_MODELVIEW );
glLoadIdentity(); // <- sets "no transformation" to take light position as is

glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
glNormal3f(surfaces[0].normal.x,surfaces[0].normal.y,surfaces[0].normal.z);

glPopMatrix(); // <- restores viewer transformation for rendering

surfaces[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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?


Share this post


Link to post
Share on other sites
So if I need to write a shader, will I be able to use OpenGL lighting? And that aside, will I even be able to use glBegin/glEnd for shapes? In fact, can I use OpenGL at all?

Or, in other words, how do you create a shader?

Share this post


Link to post
Share on other sites
Phynix,

I think it is valuable that you learn how OpenGL works. It will also apply to DX10 for the most part.

A shader just modifies the OpenGL rendering pipeline. Learning this pipeline and the process behind how your data becomes pixels on the screen will give you a big leap in your methodology. A shader won't change how you use OpenGL now, it will just enhance what you are doing.

Share this post


Link to post
Share on other sites
OK, thanks. I will. And is it possible to see the actual code inside opengl functions? Because whenever I open GL.h, I just see a bunch of function declarations. Where can I actually find the code? (they must be in the .dll/.lib files, but that's just binary, isn't it?)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this