PDA

View Full Version : camera as object and gluLookAt



akashiraffee
02-27-2009, 01:06 PM
I've found a few different methods for "making the camera" move and I don't like any of them. What I would prefer to do is keep track of the camera as an object like any other:


void camera (float pos[6]) {
glPushMatrix();
glTranslatef(pos[0],pos[1],pos[2]);
glRotatef(pos[3], 1.0f, 0.0f, 0.0f);
glRotatef(pos[4], 0.0f, 1.0f, 0.0f);
glRotatef(pos[5], 0.0f, 0.0f, 1.0f);
gluLookAt([hmmm]);
glPopMatrix();
}

Then aim at a point in front of me using gluLookAt(), as indicated.

How can I extract some numbers from the current matrix (after the transformation and rotations are done) to use with gluLookAt in the manner I desire?

I suppose in essence my question is: why can't I find a function that will tell me the 16 values of the current matrix?

dletozeun
02-27-2009, 03:54 PM
I am not sure to understand your problem but I think you misunderstand something about gluLookAt. This function move to the eye position and set a view matrix (typically the modelview one) to look at the given target position.
More information here:
http://www.opengl.org/sdk/docs/man/xhtml/gluLookAt.xml

So when ou want to set up a camera using gluLookAt, you do not have to move to the camera position, etc... That is why I do not understand the purpose of the glTranslate and glRotate calls you made in the above code.
You simply call gluLookAt after setting modelview matrix to identity and before performing transformations of all objects in the scene.
Typically in your draw function you have something like this:



draw
{
// Camera setup
glLoadIdentity();
glLookAt(...);

// draw the scene
//

glPushMatrix();
// apply object transformations
// draw object 1
glPopMatrix();

glPushMatrix();
// apply object transformations
// draw object 2
glPopMatrix();

glPushMatrix();
// apply object transformations
// draw object 3
glPopMatrix();
...
}

akashiraffee
02-27-2009, 04:00 PM
I found glGetFloat -- which in both the redbook and the superbible, this function is only referred to in the context of fetching a single enum value the way you might use "[i" in vim. The superbible version is a literal plagarism of the redbook paragraph too! And then both of them go on to spend countless pages detailing the intricacies of matrix transformation while managing to avoid ANY reference to the fact that you might want the literal values! Tsk tsk tsk...computer book publishing could really use some more intelligent writers.

Anyway, if anyone has any comments or thoughts on this first person perspective method, do post -- I'm sure there's a more elegant way.

akashiraffee
02-27-2009, 04:10 PM
Because I have a scene, and I would like the camera to zoom in, rotate, and then keep the z-axis relative to the rotate so that direction of further zooming changes. First person perspective is what I believe they call it in the shoot 'em up world.

Like I said, I found a few methods for doing this (there is one in the superbible, altho most of the actual code is in functions on the disc and not in the book) but it seemed ridiculous to me, and the other methods seem like hacks awaiting a real API function too. So here's one more.

I did get it to work, b/t/w, but it requires acquiring two points via a transform function like in the OP, keeping the positions (from glGetFloat) and using one as the camera position and the other as the "center" position for gluLookAt.

dletozeun
02-28-2009, 09:04 AM
gluLookAt is helpful when you want to move the eye at a specific position and looking at a specific target.
For 1st person camera systems, a robust way to handle this is to store camera roll, pitch and yaw axis and use them to perform all camera movements (panning, tracking, zooming, etc). To do this you will have to rotate around one of these axis. The best way is to use quaternions (if you are interested, you will find googling, many example code that compute rotations with quaternions).

akashiraffee
02-28-2009, 11:22 AM
Thanks for the tip dletozeun! I was actually lying when I said that trick with one point following another worked -- it seemed like it might (I am kind of a trigometric weakling), but perhaps I don't need to invent my own method after all...

I wanted more freedom with the perspective to examine what I'm doing as I learn. I'm surprised how complicated the first person perspective is...for now I'm just orbiting and tilting the scene and zooming in an out while using gluLookAt() to pick a target (or just focus on the origin).

Anyway, the most useful purpose of glGetFloat would seem to me to be to extract matrix data, and I was SO shocked and #$%'d off with the authors of both the superbible and the red book for not referring to this possibility at all (in fact!!), at least in the first quarter of the book(s), by which point I think it really is important.

But that is not the fault of anyone here! Thanks again.

dletozeun
02-28-2009, 03:25 PM
hmm, glGet is useful for many things among getting matrix data. Keep in mind that, opengl matrices, matrix stacks are deprecated now with opengl 3.0 but of course still supported. Anyway, when you write your own camera class it is better to have also a class that handles 4x4 matrices and all associated matrix operations to store camera view transformations.

Actually, getting opengl matrix data is something that is rarely done. Usually you manage transformations with your own matrices and give them at the very end to opengl so that modelview transformations may be computed.