LWJGL
May 17, 2012, 02:54:29 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL 2.8.3 released!
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: (unwanted) Light that follow object  (Read 2535 times)
New2OpenGl
Newbie
*
Posts: 3


« on: October 18, 2005, 19: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();
}
Logged
hvor
Newbie
*
Posts: 37



WWW
« Reply #1 on: October 19, 2005, 00: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:

        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:
           GL11.glLight(lightHandle, GL11.GL_POSITION,lightPosition);

Hope it helps Smiley
Logged

Fool Running
Nerdus Imperius
*****
Posts: 647


« Reply #2 on: October 20, 2005, 10:03:21 »

Just breifly looking at the code I think:
Code:
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().
Logged

Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option Grin
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!