## Using a camera object in OpenGL

stuepfnick
Unregistered

Post: #1
Hello my friendas!

I have an OpenGL engine, made in C++ and Cocoa.

I use a vector for object position, and a quaternion for object orientation.

When changing one of them I calculate in a matrix.

Everything works fine, except: the Camera.
That is because OpenGL hasn't got a camera. So I generate a normal object, with vector and Quaternion.

But when I multiply the matrix, it's wrong, that's clear, because I have to move the world inverse of the camera before drawing.

So I first have to orient the world matrix in some inverse way (how??)

and then I translate: -x, -y, -z and then I draw.

The translate thing works, but not the orientation.
PLEASE tell me the right thing I should do. should i inverse the matrix before multiplying, or the quaternion, before converting to the matrix, or anything else?

PLEASE, tell me, I already spent weeks on this problem! I can't hear: get a book of linear algebra.
I have everything working, and I don't have the time to study linear algebra, just for one solution, that is already done very often (e.g. in Queasa, Rb3D, QD3D) but I can't get the right source out of Quesa, because I don't understand it.

Please really help and give me an information! Once I thought the solution could be, to scale the whole matrix with -1, but not tested yet!

I thank you very much for a solution and would be VERY, VERY grateful, and help that person alot.

In my old RB engine, I always made a work-around, because in that time, I also searched a solution for weeks, without success. You can imagine, how much I love searching for it. ;-)

demo is here:

http://homepage.mac.com/stuepfnick/coding/

thanks and greetings,
Stefan
ghettotek
Unregistered

Post: #2
here's how i do it:

for each frame..

1) put the matrix in MODEL VIEW mode

[sourcecode]
glMatrixMode(GL_MODELVIEW);
[/sourcecode]

2) you can either create a custom "camera" class to handle the simulation of a camera, or you can do it from whatever scope you choose (ie, drawFrame()). either way, step 2 is to load a new identity, then rotate the matrix in the opposite direction of whatever direction the camera is pointing in.

[sourcecode]
glRotatef(-cameraOrientation.x,1.0f,0.0f,0.0f);
glRotatef(-cameraOrientation.y,0.0f,1.0f,0.0f);
glRotatef(-cameraOrientation.z,0.0f,0.0f,1.0f);
[/sourcecode]

3) translate (move) the current matrix opposite of where the camera is positioned.

[sourcecode]
glTranslatef(-cameraPosition.x,-cameraPosition.y,-cameraPosition.z)
[/sourcecode]

*4) draw all objects. its important that you save and restore the current matrix when drawing objects.

[sourcecode]
glPushMatrix();
glTranslatef(objectPosition);
glRotatef(objectOrientation);

..draw polys, etc...
glPopMatrix();
[/sourcecode]

btw, im not sure if its faster or slower to do it in that order. all i know is that i went through the same problem youre having and this is what i did to solve it.
stuepfnick
Unregistered

Post: #3
Hello!

Yes, that sounds very logical.

The problem is, I have quaternions for the orientation. And for each object I multiply a matrix, calculated from there.

But I think I will just try to let the camera rotate around an object, and try again, with inverse quaternion or inverse 3*3 rotation matrix.

Thanks and greetings,
Steve
rommel
Unregistered

Post: #4
The easiest way of computing the view matrix in your case looks to be inverting the camera's orientation quaternion -- to do this, first normalize your quaternion, then multiply the x, y and z components of the quaternion by -1 (but leave the w value untouched). Then, transform the entire scene by this quaternion: convert it to matrix form, and multiply the modelview matrix by it.