tags:

views:

13

answers:

1

I know the camera is at 0,0,0 and I need to rotate the world around it, but I'm getting confused as to what order to do translations and rotations.

If have a theoretical x,y,z coordinate system where the camera is at cx,cy,cz and it's oriented to cox,coy,coz and I have a cube which sits at bx,by,bz oriented to box,boy,boz then what series of glTranslatef and glRotatef are required to rotate the box correctly and at the correct location away from the camera?

Here are the basic operations, but I have no idea what order to put them in and what other operations are required to make it show up as expected.

gl.glLoadIdentity();

// rotation and translation for cube
gl.glRotatef(box, 1,0,0);
gl.glRotatef(boy, 0,1,0);
gl.glRotatef(boz, 0,0,1);
gl.glTranslatef(bx,by,bz);

// rotation and translation for camera
gl.glRotatef(cox, 1,0,0);
gl.glRotatef(coy, 0,1,0);
gl.glRotatef(coz, 0,0,1);
gl.glTranslatef(cx,cy,cz);

// draw the cube
cube.draw(gl);
+1  A: 

Do it the other way around: camera transform first, then your object(s):

gl.glLoadIdentity();

// rotation and translation for camera
gl.glRotatef(-cox, 1,0,0);
gl.glRotatef(-coy, 0,1,0);
gl.glRotatef(-coz, 0,0,1);
gl.glTranslatef(-cx,-cy,-cz);

// rotation and translation for cube
gl.glRotatef(box, 1,0,0);
gl.glRotatef(boy, 0,1,0);
gl.glRotatef(boz, 0,0,1);
gl.glTranslatef(bx,by,bz);

// draw the cube
cube.draw(gl);
genpfault