Sign in to follow this  
gprime11

glLookAt camera rotation help

Recommended Posts

Ok im having a little trouble figuring this out. And yes this is (was) homework that is already handed in and i lost marks for not getting the camera motion correct. So maybe you guys can help. So what i have to do is use the glLookAt function to rotate my object up and down (x axis) left and right (y axis). We also have to zoom in and out which i got working fine. So this is what i have: P be a specific point centrally located on the submersible, this point is defined with center.x, center.y, center.z (struct) in glLookAt. The values are set in init().
glColor3f(0.0f, 0.0f, 1.0f); // set the color to blue
	gluLookAt(eye.x, eye.y, eye.z, center.x, center.y, center.z, up.x, up.y, up.z);
	glGetFloatv(GL_MODELVIEW_MATRIX, mm);
I also get the MODELVIEW Matrix and store it in an array to use for matrix multiplication and stuff. I am having trouble doing this: "Use the up and down arrow keys to rotate the center of projection up or down about point P." I know how to set the up and down keys and register it with glutSpecialFunc() but my matrix arithmetic is all messed up. This is my code:
// rotate sub up and down
		case GLUT_KEY_UP:
			// The transformed matrix of rotate left/right is constructed as:
			// M = T(centerX, centerY, centerZ) * R(angle, mm[0], mm[4], mm[8]) * T(-centerX, -centerY, -centerZ)
			// Initialize the transformed matrix..
			transformM.loadIdentity();

			// Translate to P(center.x, center.y, center.z).
			// M = T(centerX, centerY, centerZ)
			tempM.loadTranslateMatrix(center.x, center.y, center.z);
			
			transformM.multMatrix(tempM);
			
			// Rotate about X axis of eye coordinates.
			// M = T(centerX, centerY, centerZ) * R(angle, mm[0], mm[4], mm[8])
			tempM.loadRotateMatrix(-2/RAD, mm[0], mm[4], mm[8]);
			transformM.multMatrix(tempM);

			// Translate back.
			// M = T(centerX, centerY, centerZ) * R(angle, mm[0], mm[4], mm[8]) * T(-centerX, -centerY, -centerZ)
			tempM.loadTranslateMatrix(-center.x, -center.y, -center.z);
			transformM.multMatrix(tempM);
			//eye.x++;
			// Transform the EYE position.
			eye = transformM.transformPoint(eye.x, eye.y, eye.z);
			break;
		case GLUT_KEY_DOWN:
			// The transformed matrix of rotate left/right is constructed as:
			// M = T(centerX, centerY, centerZ) * R(angle, mm[0], mm[4], mm[8]) * T(-centerX, -centerY, -centerZ)
			// Initialize the transformed matrix..
			transformM.loadIdentity();

			// Translate to P(center.x, center.y, center.z).
			// M = T(centerX, centerY, centerZ)
			tempM.loadTranslateMatrix(center.x, center.y, center.z);

			transformM.multMatrix(tempM);
			
			// Rotate about X axis of eye coordinates.
			// M = T(centerX, centerY, centerZ) * R(angle, mm[0], mm[4], mm[8])
			tempM.loadRotateMatrix(2/RAD, mm[0], mm[4], mm[8]);
			transformM.multMatrix(tempM);

			// Translate back.
			// M = T(centerX, centerY, centerZ) * R(angle, mm[0], mm[4], mm[8]) * T(-centerX, -centerY, -centerZ)
			tempM.loadTranslateMatrix(-center.x, -center.y, -center.z);
			transformM.multMatrix(tempM);
			//eye.x++;
			// Transform the EYE position.
			eye = transformM.transformPoint(eye.x, eye.y, eye.z);
			break;
It rotates around the x axis ok but when it gets to a certain point it just gets all screwed up. Can someone tell me if im doing this right? Im sure the commented code is good enough for explanation.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this