CyanPrime
CyanPrime

Reputation: 5193

OpenGL matrix not rotating properly

Alright, so I got this code for gluLookAt:

lookAt = new Vector3f(-player.pos.x, -player.pos.y, -player.pos.z);
       lookAt.x += (float)Math.cos(Math.toRadians(player.yaw)) * Math.cos(Math.toRadians(player.pitch));
       lookAt.y += (float)Math.sin(Math.toRadians(player.pitch));
       lookAt.z += (float) Math.sin(Math.toRadians(player.yaw)) * Math.cos(Math.toRadians(player.pitch));

       GLU.gluLookAt(-player.pos.x, -player.pos.y, -player.pos.z, 
               lookAt.x, lookAt.y, lookAt.z, 
               0, 1, 0);

And when I try to draw a rotated cube it does not rotate properly.

GL11.glPushMatrix();
GL11.glLoadIdentity();
GL11.glTranslatef(-cube.pos.x, -cube.pos.y, -cube.pos.z);
GL11.glRotatef(cube.yaw, 0, 1, 0);
GL11.glTranslatef(cube.pos.x, cube.pos.y, cube.pos.z);
/*draw the cube normally*/
GL11.glPopMatrix();

So my question is am I handling the alterations to the matrix done by glulookat properly? or am I doing somethign wrong? The result I am looking for is to return the cube to 0,0,0 and rotate it, then put it back where it was.

Upvotes: 0

Views: 300

Answers (2)

Stephan van den Heuvel
Stephan van den Heuvel

Reputation: 2138

problem is here:

GL11.glTranslatef(-cube.pos.x, -cube.pos.y, -cube.pos.z);
GL11.glRotatef(cube.yaw, 0, 1, 0);
GL11.glTranslatef(cube.pos.x, cube.pos.y, cube.pos.z);

The cube is already 'at' 0,0,0. This is because the Medel matrix is identity (as you called glLoadIdentity()).

so you should do:

 GL11.glRotatef(cube.yaw, 0, 1, 0);
 GL11.glTranslatef(cube.pos.x, cube.pos.y, cube.pos.z);

which should have the desired effect. If not, try it with a fixed camera to see if the code you added before glulookat() is causing the lookat target to be too far away from 0,0,0 (where your cube is)

Upvotes: 2

Tim
Tim

Reputation: 35943

Calling glLoadIdentity before drawing your cube will wipe out whatever gluLookAt has setup in your view matrix, I don't think it should be there.

Upvotes: 1

Related Questions