Camera in OpenGL

How could I set camera which looks to my scenes origin( x,y,z=0 ). I have four boxes in my scene. I need to be able to view them from different positions. How could I do it?
(VC++, no glut!)

Try gluLookAt(). That should do the trick.
Just remember that when you call gluLookAt your matrix mode should be GL_MODELVIEW. Consult the Red Book.


Could someone tell me more about gluLookAt function. I’m not sure how to use it. Do I have to call it everytime I draw something to screen? And what are the last three constants int the function ( gluLookAt( ex, ey, ez, ox, oy, oz, ?, ?, ? ) ) ???

U have to call this glu function each time u want to change the direction your camera is looking.
You don’t need to call it at each frame…

the function is:

gluLookAt(ex, ey, ez, ox, oy, oz, Upx, Upy, Upz);

The last vector “Up” is you’r Up vector (nice name). By default (if you’r up axis is Y), UP = (0.f, 1.f, 0.f). But u can set this up axis to the value you want.

Hope that helped.

How does “pointing your camera” in a different direction change anything? I don’t understand because I thought things were alaways rendered such that the camera was always AT the origin and you had to move the objects around that to create the illusion of movement or to “point” at something. ???

Uhm, pointing the camera in a different direction is just a routine to move the world in the opposite direction. You are very right when you say that the point of view is always located at the origin. Every attempt to move it will always end up moving the world instead.

This is what gluLookAt does, it moves the world around the origin, to give the impression of moving the point of view.

Everything is about moving the world around the origin.

Oh, I see! So do you set up all your geometry first and then set your camera position with glLookAt() or does it matter the order in which you do it? So basically what you’re saying is that instead of doing it the way NeHe does it in his tutorials (find the user’s position etc. and then multiply everything by -1) glLookAt just does all this for you? So if I wanted, could merely layout the geometry of my level the same every time without any glTranslates or glRotates and just use glLookAt?

ADDENDUM: I mean not do any glTranslates or glRotates for the purpose of orienting the world around the user to create the appearance of movement ofcourse. I ofcourse don’t mean to not have to rotate or translate for the purpose of PLACING the geometry in the proper locations to build the scene.

Correct Punchey, but you would have to use gluLookAt before drawing anything though. Not after drawing everything. I myself generally avoid using gluLookAt, or any glu functions for that matter. Lots of Voodoo owners who still have the OpenGL MCD do not have glu functions available (nor even some gl functionality). Of course you can always decide to only support OpenGL ICD’s and use glu functions whenever you like.

Yeah, Voodoo has sucked for quite some time. So what you’re saying is that I’d want to do my gluLookAt before doing my glBegin(whatever)/glEnd() bloc? Thanks!

Everything you send to OpenGL (geometry-wise) is processed and drawn as soon as you pass them to the pipeline. This means that if you haven’t setup your modelview matrix properly, OpenGL uses the matrix that was last used instead of the one you really want. If this matrix is wrong, the result will be wrong too.