Advertisement

Camera Confusion

Started by October 01, 2000 07:20 PM
1 comment, last by duhroach 24 years, 1 month ago
Howdy. I''ve been trying to figure out how to create a camera in OpenGL. Everything I''ve been reading has either been too simple (not taking into account things like universal rotation), or too complex. (or written in damn GLUT so it''s hard to figure out what''s going on) Does anyone have a rather good resource on makeing a camrea that can rotate, move, and strafe? ~Main === Colt "MainRoach" McAnlis www.sinewave.net
==Colt "MainRoach" McAnlisGraphics Engineer - http://mainroach.blogspot.com
I wrote this code using GLUT, but it should still help you out a bit. The only difference between this should be the if(key == xxxx) lines. These lines will rotate you around x and y and translate you along x, y, and z.

PI_180 is a constant I have to convert between degrees and radians. It's just PI/180.0. It shouldn't take much to drop this into your program or add rotation around the z axis.

	if(key == GLUT_KEY_UP)	{		fCamX += (float)sin(fYRot*PI_180) * MOVE_SPEED * 2;			fCamZ += (float)cos(fYRot*PI_180) * MOVE_SPEED * 2;				fCamY += (float)sin(fXRot*PI_180) * MOVE_SPEED * 2;	}	if(key == GLUT_KEY_DOWN)	{		fCamX -= (float)sin(fYRot*PI_180) * MOVE_SPEED * 2;		fCamZ -= (float)cos(fYRot*PI_180) * MOVE_SPEED * 2;		fCamY -= (float)sin(fXRot*PI_180) * MOVE_SPEED * 2;	}	if(key == GLUT_KEY_RIGHT)	{		fYRot -= MOVE_SPEED / 2;		if(fYRot < -360.0)			fYRot += 360.0;	}	if(key == GLUT_KEY_LEFT)	{		fYRot += MOVE_SPEED / 2;		if(fYRot > 360.0)			fYRot -= 360.0;	}	if(key == GLUT_KEY_PAGE_UP)	{		fXRot -= MOVE_SPEED / 2;		if(fXRot < -360.0)			fXRot += 360.0;	}	if(key == GLUT_KEY_PAGE_DOWN)	{		fXRot += MOVE_SPEED / 2;				if(fXRot > 360.0)			fXRot -= 360.0;	}


Then, all you have to do is this
	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();	// Do camera translation/rotation	glRotatef(fXRot, 1.0f, 0.0f, 0.0f);	glRotatef(360 - fYRot, 0.0f, 1.0f, 0.0f);	glTranslatef(fCamX, fCamY, fCamZ);        // Draw everything else below


at the beginning of your display function to implement your camera.

Ben

Edited by - pr0teus on October 1, 2000 11:22:41 PM
Advertisement
If you are still having problems, email me, and I will send you code using the framework on this site.

That goes for anyone!

This topic is closed to new replies.

Advertisement