LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: sober on August 01, 2005, 09:43:24

Title: Problem moving my Camera
Post by: sober on August 01, 2005, 09:43:24
Some informations before I get to my Problem:
I am using a 4x4 Matrix on every Object I handle in my World. With m.rotate, m.translate based on the m.getAxis methods I move them very easy. Everytime I render an object, I glLoadMatrix the matrix of the Object and drag the planes for it.
Everything works great this way.
Now I want to move my Camera (first step as beeing in firstPersonView).
As the first step I tried it with glTranslate and glRotate, that failed as I overwrite that with every glLoadMatrix, I do.
I tan tried out gluLookAt. Nothing happend to the Objects, I only saw, that I can move the lights with this command.
I finaly tried to build an Cameraobject that handles the movement and rotatin on a matrix. I incremented the count of matrices up to three but always got weird results.

what I did: I used 1 Matrix for the rotation (rot), one for the Movement (mov) and one for Both (all).
all was used to get correct axisdata for the movement (mov.translate( all.getZAxis()).
so everything I did I executed on all, translates are done on mov and rotates on rot.
Now, for every object I draw, I take the crossproduct of mov and rot and glMulMatrix it with the matrix of the object.
This way, rotating the camera worked great. I thought I found it and was happy. well until I started to move. Moving forwards without rotating works great ;) As soon as I rotate something funny happend to the viewperspective. Even a rotate of 1 or 2 degrees let me see objects that were standing in line and so were hidden behind other objects.


  #   <-- object

  #   <-- object

  #   <-- object


  |   <-- cam


rotating alittle bit:


 #   <-- object

    #   <-- object

       #   <-- object


       \   <-- cam

Thats not really how it works in RL ;)

So I started to google some more and gave up now after alot of hours finding howtos that use glulookat or glTranslates and recalculate every translate for the Vectors they draw, or some weird documents that use Letters to name their matrices, for what I haven't found any information what they are doing.

I hope you understand, what my problem is. English is not my motherlanguage, so it might very bad.

Greetings and Thanks in advance,

Jens
Title: Similar problem!
Post by: CaseyB on August 01, 2005, 12:55:54
I have been having a similar problem and I too tried gluLookAt with no change!  If someone could post some code that shows how to move a camera I am sure that both of us would be extremely thankful!
Title: Problem moving my Camera
Post by: tomb on August 01, 2005, 13:41:30
Don't overwrite the current matrix. Use multMatrix instead of loadMatrix.
Title: Problem moving my Camera
Post by: sober on August 01, 2005, 14:47:42
Hmm, I think you understood me wrong.

I Matrix.mul() the CamMatrix with the ObjectMatrix for every object.
If I would let the Matrix untouched and glMultMatrix the objectMatrix everytime, they would add up again and again.

But what happens is, that the Objects rotate around the 0,0 point and not around the camera.

I think I just miss the basic idea, how to move and rotate the world based on cameramatrices to let it look as if the camera is moving/rotating.
Title: Problem moving my Camera
Post by: Orangy Tang on August 01, 2005, 18:07:41
Quote from: "sober"
But what happens is, that the Objects rotate around the 0,0 point and not around the camera.
This usually implies that the order you're multiplying your matrices in is wrong. Generally for OpenGL cameras it is easiest to:

- Generate camera matrix (however)
- Set modelview matrix, load identity
- Load camera matrix

for every object:
- push modelview matrix
- multiply modelview by object matrix
- render object
- pop modelview matrix

If that's still giving you funny results, post code.
Title: Problem moving my Camera
Post by: napier on August 01, 2005, 19:23:54
Have you looked at the tutorials at Nehe?  (http://nehe.gamedev.net)

They have a tutorial on camera motion (number 10 I think).  The basic idea is to move the scene in the opposite direction of the camera motion, so if the camera moves forward 5 units, you actually moves the scene backward 5 units.  Here's a snip:

// Camera position
static float[] cameraPos = {0f,0f,15f};
float cameraRotation = 0f;

// A constant used in navigation
final float piover180 = 0.0174532925f;


public void render() {
// select model view for subsequent transforms
       GL11.glMatrixMode(GL11.GL_MODELVIEW);
       GL11.glLoadIdentity();

       // adjust camera position according to arrow key events
       setCameraPosition();

       // shift and rotate entire scene (opposite to "camera" position)
       // first rotate around y axis
       GL11.glRotatef((360.0f-cameraRotation), 0, 1f, 0);
       
       // then move forward on x,z axis
       GL11.glTranslatef( -cameraPos[0], -cameraPos[1], -cameraPos[2]);  

       // draw the scene
...
}

public void setCameraPosition()
{
// Turn left
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
   cameraRotation += 1.0f;
}
// Turn right
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
   cameraRotation -= 1.0f;
}
// move forward in current direction
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
   cameraPos[0] -= (float) Math.sin(cameraRotation * piover180) * .3f;
   cameraPos[2] -= (float) Math.cos(cameraRotation * piover180) * .3f;
}
// move backward in current direction
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
   cameraPos[0] += (float) Math.sin(cameraRotation * piover180) * .3f;
   cameraPos[2] += (float) Math.cos(cameraRotation * piover180) * .3f;
}
// move camera down
if (Keyboard.isKeyDown(Keyboard.KEY_PRIOR)) {
   cameraPos[1] +=  .3f;
}
// move camera up
if (Keyboard.isKeyDown(Keyboard.KEY_NEXT)) {
   cameraPos[1] -=  .3f;
}
}


I have a working example (lwjgl .95) at http://potatoland.com/code/gl (the "Navigation" demo).
Title: Problem moving my Camera
Post by: sober on August 01, 2005, 20:02:37
hmm, I am using Matrices to handle alot of Objects.
The way the tutorial works, I would need to glTranslate and glRotate, than store the Matrix into a Floatbuffer and Multiply this everytime I draw an object.
I have red somewhere, that reading and storing the Matrix out of the Graphiccard is to slow to do it every frame.
Title: Problem moving my Camera
Post by: tomb on August 01, 2005, 22:33:29
No, you don't have to read back the matrix. You can apply your model matrix to the matrix stack where it says "77 draw the scene".

Read Orangy Tangs post on what you need to do. It's pretty straigth forward.