View Full Version : Super Simple -- Moving around in OGL
10-03-2000, 04:15 AM
Say I make a auxWireCube at (0,0,0). Now I want to move the CAMERA around, as a first-person view kinda thing. That is, I want to use my arrow keys to approach the box and move to the side and back, etc. Can all this be done with simple glTranslatef() functions? It always seems like I'm moving the objects (I've tried two cubes), instead of my camera.
auxWireTeapot(1.0); //or cube
Does this translate the object or the camera? Also, how could I add another object at another position? Wouldn't I have to use another glTranslate() call/load identity--this always screws the program up(if I put a loadIdentity() then tranlate my second object, I am unable to 'walk around' in the compiled program. Thanks!
There is no camera in OpenGL, only a viewpoint located at the origin, and this position is fixed, you can never ever change this. However, instead of translating the viewpoint, you can translate the entire world in the oposite direction. You know, moving the "camera" forward is the same as moving the world backwards. And whatever you do in terms of translation, you are always moving/rotating the world, but the viewpoint is still fixed at the origin.
Now when this is clear (I hope), I can say that what you probably need to do in your code is to place a '-1*' (multiplication by minus one)infront of each variable in your call to glTranslatef(), to make the world translate in the oposite direction.
10-06-2000, 06:43 AM
If there is no camera then what exactly is GLuLookAt() for? It was my understanding that this was a way to manipulate the viewport.
[This message has been edited by GRanDaddY (edited 10-06-2000).]
I am sure this is not the best way to do this but it worked so far...
My mouse move func looks likes this:
// add in the difference between the last position
// and the current position
yRot -= x - lastX;
xRot += y - lastY;
// set to current position
lastX = x;
lastY = y;
Then my idle movement thing looks like this:
if ( keys[ 'w' ] )
// move forward
pos.x -= (GLfloat)sin( yRot * piover180 ) * kMovementSpeed;
pos.z -= (GLfloat)cos( yRot * piover180 ) * kMovementSpeed;
if ( flight )
pos.y -= (GLfloat)sin( xRot * piover180 ) * kMovementSpeed;
else if ( keys [ 's' ] )
// move backward
pos.x += (GLfloat)sin( yRot * piover180 ) * kMovementSpeed;
pos.z += (GLfloat)cos( yRot * piover180 ) * kMovementSpeed;
if ( flight )
pos.y += (GLfloat)sin( xRot * piover180 ) * kMovementSpeed;
if ( keys[ 'a' ] )
// move left
pos.x -= (GLfloat)sin( ( yRot + 90 ) * piover180 ) * kMovementSpeed;
pos.z -= (GLfloat)cos( ( yRot + 90 ) * piover180 ) * kMovementSpeed;
else if ( keys[ 'd' ] )
// move right
pos.x += (GLfloat)sin( ( yRot + 90 ) * piover180 ) * kMovementSpeed;
pos.z += (GLfloat)cos( ( yRot + 90 ) * piover180 ) * kMovementSpeed;
if ( keys [ GLUT_KEY_UP ] )
pos.y += kMovementSpeed;
else if ( keys [ GLUT_KEY_DOWN ] )
pos.y -= kMovementSpeed;
And finally my draw routine looks like this:
GLfloat xtrans = -pos.x;
GLfloat ytrans = -pos.y;
GLfloat ztrans = -pos.z;
// Reset The View
glRotatef( xRot, 1.0f, 0, 0 );
glRotatef( sceneroty, 0, 1.0f, 0 );
glTranslatef( xtrans, ytrans, ztrans );
I won't know if this is readable until I submit so if it isn't pretty...sorry.
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.