Jump to content
  • Advertisement
Sign in to follow this  
Dr_Asik

Lights moving with camera

This topic is 3210 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I want to render a simple scene with lights in it. These lights are supposed to be static. However, when I move the camera around, they appear to move with the camera, as if their position was defined relatively to the camera. For instance if I have a specular highlight on a sphere, when I rotate the camera, the highlight always faces the camera, instead of rotating with the sphere, which implies that the light source is moving with the camera. I have defined the lights position in an initialization function as such:
GLfloat position_red[] = { -10.0, -10.0, -10.0, 0.0 };
glLightfv(GL_LIGHT1, GL_POSITION, position_red);
And each frame I place the camera like so (spherical coordinates):
    gluLookAt(
        (float)radius * sin(theta) * cos(phi), 
        (float)radius * sin(theta) * sin(phi),
        (float)radius * cos(theta),
        0, 0, 0,
        0, 0, 1);
And basically when the user moves the mouse around I adjust the values of theta and phi. I don't get what am I doing wrong. How can make the light sources stay static in world coordinates?

Share this post


Link to post
Share on other sites
Advertisement
You need to set the light position every frame after gluLookat, otherwise it is transformed by the new model matrix.

Treat it just like any other static object in your scene, you render it after you setup the camera.

Share this post


Link to post
Share on other sites
I have a new problem. My scene is a cube missing one face so we can see inside. There's a light source inside the cube, and we can see that the faces are lit inside the cube, and dark outside the cube, as we want to.

http://img504.imageshack.us/img504/2163/50698191.png

However, I also have a light source on the outside of the cube. In theory, it should light up some of the exterior faces of the cube, but it does weird things. Some faces are lit up inside the cube, some faces just don't lit up at all. The same lighting seems applied on both sides of the same face. It's behaving in a completely different way.

On this screenshot, the light source is in the upper-right corner, the specular highlight on the sphere tells approximately in what direction it is. It is outside of the cube completely.

http://img441.imageshack.us/img441/2426/44598075.png

I would expect the faces facing the light to be lit up, but they are dark, and some of the faces on the opposite side of the cube are lit. Also if I rotate the cube I can see the same lighting is applied on both sides of each face.

Here is some of my code.

I use a material for the red cube defined as so:

    GLfloat red_ambient[] = { 0.2f, 0.1f, 0.1f, 1.0f };
GLfloat red_diffuse[] = { 1.0f, 0.0f, 0.0f, 1.0f };
GLfloat red_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glNewList(RED_MAT, GL_COMPILE);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, red_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, red_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, red_specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64.0);
glEndList();


And the bogus light is defined as such : global variable

GLfloat positionLight1[] = { -10.0f, 0.0f, 10.0f, 0.0f };


Initialization:

    GLfloat light_ambient[] = { 0.5f, 0.5f, 0.5f };
GLfloat light_diffuse[] = { 0.5f, 0.5f, 0.5f };
GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);


And that's basically it. Here are the general GL calls in the init function:

    glShadeModel(GL_SMOOTH);

glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

Share this post


Link to post
Share on other sites
Quote:
Original post by Dr_Asik

However, I also have a light source on the outside of the cube. In theory, it should light up some of the exterior faces of the cube, but it does weird things. Some faces are lit up inside the cube, some faces just don't lit up at all. The same lighting seems applied on both sides of the same face. It's behaving in a completely different way.



If your lights are not lighting up a face as you would expect, check the normals for those triangles and ensure that they are in the correct direction. Are you just using one quad for your box wall, or two (one inside facing quad, and one outside facing quad)?

The triangles need proper normals to have correct lighting. You either need to enable two sided lighting (I think), or generate two triangles with normals facing opposite direction, and enable backface culling if you want to have lights on both sides of a triangle.

Quote:
Original post by Dr_Asik

However, I also have a light source on the outside of the cube. In theory, it should light up some of the exterior faces of the cube, but it does weird things. Some faces are lit up inside the cube, some faces just don't lit up at all. The same lighting seems applied on both sides of the same face. It's behaving in a completely different way.


Opengl will not cast shadows for you. If you put 2 triangles one behind the other and shine a light facing the first one, both will appear equally lit. This is why the insides of the box appear lit. If you want to have proper shadowing of objects it is a complex subject.

I think you should seek out some opengl lighting tutorials and read up on them, any basic one should be able to address these kinds of questions.

Share this post


Link to post
Share on other sites
It is hard to tell what is going on in the second image. As suggested a triangle only has 1 normal, so lighting on one side is not going to work from the opposite side.

Try hollowing out your cube so that it is like a "real" world wall that has thickness.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!