Sign in to follow this  

Quaternion and position vector to view matrix

This topic is 3732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a quaternion representing a rotation, and a position vector. How do I create a view matrix (a la gluLookAt) from it (I'd like to create a 1st person camera)? I have a function getRotationMatrix from the quaternion, so as view matrix, I used: V = T * R where T is a translation matrix created out of the position vector, and R is the rotation matrix returned from myQuaternion.getRotationMatrix. However, when I use this, I end up upside down in the game world, among other things. I think I may be doing something wrong with the maths, or with the column major/matrix non-commutativity conventions of OGL but I can't figure out what...

Share this post


Link to post
Share on other sites
A bit of math, please? Well, I can serve ;)

Converting from a local co-ordinate system to the world co-ordinate system (using column vectors, as is convention in OpenGL) is normally done rotation (R) first then translation (T):
Mg := T * R
(the index g means "global" here).

If pushing the both matrices T and R one-by-one to OpenGL, you have to use the order
glTranslate(...);
glRotate(...);
to yield in the OpenGL matrix equivalent of M! This is important due to the non-commutativity.

Now, a model can be transformed by applying the above formula to all of its geometry (e.g. its vertex positions) like so:
Vg := Mg * V

Looking at the camera's definition, it is most often given as another local co-ordinate frame. Hence, anything given in its system can be converted to world as shown above. Using index C for the camera, one gets
MCg := TC * RC

But you want something different: You want to transform geometry given in the world be relative to the camera system, hence you need the inverse of the thing above:
MCg-1 = RC-1 * TC-1
(Note please that the order has changed!) That matrix above is what is called the VIEW matrix in OpenGL (okay, it may consider more stuff than the rotation/translation of the camera, but that is what you've asked for).

Now, with some locally defined geometry, the entire transformation would be
MCg-1 * MMg
where the index M should denote a "mesh" (geometry) and C still denotes the camera. Such a matrix is called the MODELVIEW matrix in OpenGL.

As you can see, the VIEW part is build from the inverse matrix of the camera stuff, and the MODEL part is build of the "normal" matrix of the geometry.


Hope I've hit your problem.

Share this post


Link to post
Share on other sites

This topic is 3732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this