Moving the camera

Started by
14 comments, last by Rickwi22 21 years, 11 months ago
Use an object heirarchy. This lets you handle sub objects more easily. Each object has similiar position systems with vectors. So in the hierarchy, the camera sits at the root and each object is transformed by it''s predecessor.

void RenderObj(Obj* obj) {  glPushMatrix();    glRotatef(obj->angle.x,1.0f,0.0f,0.0f);    glRotatef(obj->angle.y,0.0f,1.0f,0.0f);    glRotatef(obj->angle.z,0.0f,0.0f,1.0f);    glTranslatef(obj->pos.x,obj->pos.y,obj->pos.z);    // draw all your polygons for that object    for(int i=0; inum_children; i++) {      RenderObj(&obj->child);    }  glPopMatrix();}void RenderWorld() {  glLoadIdentity();  RenderObj(&CamObject);} 


Something like that but more complex.
That's just my understanding; I could be wrong.
Advertisement
I mean does anyone know how I can move it forward in the direction it is facing, regardless of where it is facing in the world.
You are right, I just realized my code was not the same. My bad.

quote:Yes, you just take the camera's forward vector (normalized hopefully) multiplied by the the speed, and there's your velocity vector. Translate the camera position by that.


What I mean is how do I do the things like compute the forward vector, normalize it, and then tranlsate the camera by that.
[edited by - rickwi22 on May 8, 2002 5:57:12 PM]

[edited by - rickwi22 on May 8, 2002 6:47:30 PM]
Yes, you just take the camera''s forward vector (normalized hopefully) multiplied by the the speed, and there''s your velocity vector. Translate the camera position by that.

____________________________________________________________
www.elf-stone.com

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

quote:
What I mean is how do I do the things like compute the forward vector, normalize it, and then tranlsate the camera by that.

That depends on your implementation. If you don''t care about movement up and down (as in Quake et al), your forward vector can be calculated very simply:

     B    /|  1/ |  /  | vz /a  |A--\--   vxA->B is the facing vectora is the camera''s angle around the Y axis (which you should already have)vx is the the x part of the normalvz is the z part of the normal.vy is 0You can calculate vx and vz like thisvx=cos(a)vz=sin(a)(bear in mind the math.h header deals with angles in radians, not degrees) 


____________________________________________________________
www.elf-stone.com

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

How I implement a camera in OpenGL:

I use the technique of moving the world in an inverse manner to the camera, i.e. all operations performed in reverse w/ negated values.

My camera class has several vectors (actually the camera has a motion-controller object within, which has these, but that''s another story):

pos // camera location in x/y/z
orient // camera rotation about x/y/z

and a scaler:

fov // camera field of view in degrees


Then I do this:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(camera.fov, viewport.aspectRatio, nearz, farz);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

My code supports multiple viewports, but the aspect ratio can be simply:
aspectRatio = (float) screen.width / screen.height


Then:
gluLookAt(0,0,0, 0,0,-1, 0,1,0); // set base eye view
glRotatef(-orient.x),1,0,0); // orient camera
glRotatef(-orient.y),0,1,0);
glTranslatef(-pos.x,-pos.y,-pos.z); // position camera
get_matrix() // extracts camera vectors for move/strafe operations

// ready to render...


The reason I do not use glulookat to do the camera positioning, is that for my needs when doing several types of billboards (some z-rotatable) I need to maintain a distinct camera orientation vector so I can easily unrotate, using gluLookat would leave you without such a vector.
And I also have a handy eye-relative in/out, up/down & left/right control via sliders, which is difficult to incorporate into glulookat. My actual gluLookat:

gluLookAt(eyex,eyey,eyez, eyex,eyey,eyez-1, 0.,1.,0.);

---------------------------------------------------

Now I can simply set the camera''s pos and orient vectors to whatever/wherever I need, by keys or program-control (my motion-controller class has 5 modes of programmable movement/orientation: MANUAL, ORBIT, LOOKAT, LOOKFROM, and HOLD, soon to have several more).

The more you treat things like cameras and lights like regular 3d objects, the easier it is.

zin


zintel.com - 3d graphics & more or less
zintel.com - 3d graphics & more or less
quote:Original post by zin
The reason I do not use glulookat to do the camera positioning, is that for my needs when doing several types of billboards (some z-rotatable) I need to maintain a distinct camera orientation vector so I can easily unrotate, using gluLookat would leave you without such a vector.


Just multiply your modelview matrix by the inverse of the rotation matrix.

____________________________________________________________
www.elf-stone.com

[edited by - benjamin bunny on May 8, 2002 9:01:25 PM]

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

This topic is closed to new replies.

Advertisement