PDA

View Full Version : Camera placement and movement



ltrain_riders
12-06-2002, 11:05 AM
What would be the easiest way to create a camera at a certain location and be able to move it around with the keyboard? To get an idea of what I'm trying to do you can imagine a canyon and I want a camera to be the viewer's eyes when your at the base bottom. Pressing forward and back keys will move you forward and left/right will turn rotate the camera.

Stack Overflow
12-06-2002, 11:15 AM
Probably:

static int xcam, ycam, zcam = 0.0;

glRotated ( ( GLdouble ) xcam, 1.0, 0.0, 0.0 ); // For Rotating Camera on x-Axis
glRotated ( ( GLdouble ) ycam, 0.0, 1.0, 0.0 ); // For Rotating Camera on y-Axis
glRotated ( ( GLdouble ) zcam, 0.0, 0.0, 1.0 ); // For Rotating Camera on z-Axis

then on keyboard function:

Left-Key:
ycam = ( ycam - 1 ) % 360;

Right-Key:
ycam = ( ycam + 1 ) % 360;

Up-Key:
xcam = ( xcam - 1 ) % 360;

Down-Key:
xcam = ( xcam + 1 ) % 360;

I hope that helps.

ltrain_riders
12-06-2002, 11:45 AM
whats problematic to me is, I'm using gluLookAt( ) because I can't get my camera to view without it. This is my current setup:

void display ( void )
{
float x1, y1, z1, x2, y2, z2, x3, y3, z3,
dx1, dy1, dz1, dx2, dy2, dz2;
/* origin of camera */
x1 = 5.0f; // 5.0; -5.0; -5.0;
y1 = 5.0f; // 3.5; 3.5; 3.5;
z1 = 4.0f; // -4.0; -4.0; 4.0;

/* point looking at */
x2 = 0.0f;
y2 = 0.2f;
z2 = 0.0f;

/* camera rotation */
x3 = 0.0f;
y3 = 1.0f;
z3 = 0.0f;

dx1 = -1.5;
dy1 = 0.5;
dz1 = 3.8;

dx2 = dx1;
dy2 = dy1;
dz2 = dz1 - 0.1;

if( overview == ON ) {
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
glFrustum( -0.33, 0.33, -0.33, 0.33, 0.5, 20.0 );
//gluPerspective( 45.0f, (800/600), 0.1f, 100.0f );
gluLookAt( x1, y1, z1, x2, y2, z2, x3, y3, z3);
glRotatef( yAngle, 0.0, 1.0, 0.0 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity( );
}
else {
glMatrixMode ( GL_PROJECTION );
glLoadIdentity( );
//glOrtho( -0.33, 0.33, -0.33, 0.33, 0.1, 10.0 );
gluPerspective( 45.0f, (800/600), 0.1f, 10.0f );
gluLookAt( dx1, dy1, dz1, dx2, dy2, dz2, x3, y3, z3 );
glRotatef( newY, 0.0, 1.0, 0.0 );
glTranslatef( 0.0, 0.0, newZ);

glMatrixMode ( GL_MODELVIEW );
glLoadIdentity( );
}
.......
}

void SpecialKeys(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_LEFT:
if( overview == ON ) {
yAngle -= 1.0;
glutPostRedisplay( );
}
else {
newY -= 1;
glutPostRedisplay( );
}
break;
case GLUT_KEY_RIGHT:
if( overview == ON ) {
yAngle += 1.0;
glutPostRedisplay();
}
else {
newY += 1;
glutPostRedisplay( );
}
break;
case GLUT_KEY_UP:
if( overview == OFF ) {
newZ += 0.05;
glutPostRedisplay( );
}
break;
case GLUT_KEY_DOWN:
if( overview == OFF ) {
newZ -= 0.05;
glutPostRedisplay( );
}
break;
default:
break;
}
}

Stack Overflow
12-06-2002, 11:53 AM
You can goto Steve's website at: http://www.gldomain.com/programs/FirstPersonShooter.htm

It comes with the source code.