Hello Guest

(unwanted) Light that follow object

  • 2 Replies
  • 5719 Views
(unwanted) Light that follow object
« on: October 19, 2005, 02:30:10 »
Hi, I'm new to OpenGL, I have made a 3D object, and 3D camera using LWJGL, and they move as expected, The camera follows the 3d object.
Trouble is when I added lighting, they keep following the object when I rotate it, and I want the lights to stay where they are, How to do this?
Here's a piece of code:

//OPENGL SETUP
 GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
      GL11.glLoadIdentity(); // Reset The Projection Matrix

GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
      GL11.glLoadIdentity();

GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, lightpos);
      GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION, lightpos2);

/////////////////////////////////////

//THE RENDER FUNCTION

private void render(){
.
.setup camera and boy position, rotation, etc
.
.
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);      
      //GL11.glLoadIdentity(); //this is model view
      GL11.glPushMatrix();

      GL11.glPushMatrix();
      GL11.glLoadIdentity();

      GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, lightpos);
      GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION, lightpos2);
      GL11.glPopMatrix();
      
      GLU.gluLookAt(cam.Center[0], cam.Center[2], -cam.Center[1], cam.Target[0], cam.Target[2], -cam.Target[1], 0.0f, 1.0f, 0.0f);

      GL11.glPushMatrix();      

      GL11.glTranslatef(boy.Position[0], boy.Position[2], -boy.Position[1]);
      GL11.glRotatef(boy.angle, 0, 1, 0);

      GL11.glEnable(GL11.GL_LIGHTING);

      //draw
      GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture[54]);
      GL11.glInterleavedArrays(GL11.GL_T2F_N3F_V3F, 0, world.boy.MeshBuf);         
      GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, world.boy.FaceCount * 3);

      GL11.glDisable(GL11.GL_LIGHTING);
//Enable, Disable lighting are required to illuminate 'boy' only
      GL11.glPopMatrix();
      GL11.glPopMatrix();
}

*

Offline hvor

  • *
  • 37
    • http://hvor.madpage.com
(unwanted) Light that follow object
« Reply #1 on: October 19, 2005, 07:48:08 »
It's hard to say only by slight look at the code, but maybe :

1) I think that in the beginning of render process you should have:
Code: [Select]

        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();

2) Why do you call PushMatrix two times at the beginning?
3) try to render all objects and at the END of render() just do
Code: [Select]
           GL11.glLight(lightHandle, GL11.GL_POSITION,lightPosition);
Hope it helps :)

hmmmmm...
« Reply #2 on: October 20, 2005, 17:03:21 »
Just breifly looking at the code I think:
Code: [Select]
GLU.gluLookAt(cam.Center[0], cam.Center[2], -cam.Center[1], cam.Target[0], cam.Target[2], -cam.Target[1], 0.0f, 1.0f, 0.0f);
should be done at the beginning of the render function right before the first pushMatrix() call. And you shouldn't need the pushMatrix(), loadIdentity(), and popMatrix() around setting the light position.
The reason is that you want the lights to stay in the same world location, so they need to rotate with the camera.  You are always telling the lights to be put at their positions, ignoring the camera position making it look like the lights are following the camera. (Bad explanation, I know  :lol: maybe someone else can explain it better :wink:  )
Quote
2) Why do you call PushMatrix two times at the beginning?

I agree.  I don't think you need the outer pushMatrix() and popMatrix().
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D