# Quaternion and position vector to view matrix

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

## 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 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.

1. 1
2. 2
Rutin
25
3. 3
4. 4
5. 5

• 9
• 13
• 19
• 14
• 9
• ### Forum Statistics

• Total Topics
632940
• Total Posts
3009328
• ### Who's Online (See full list)

There are no registered users currently online

×