# Modelview matrix format

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

## Recommended Posts

I'm trying to create a modelview matrix to match a quaternion-camera-class that I have made. I want to set the modelview matrix like this: float mymatrix[16]; ... Set mymatrix to match camera ... glLoadMatrixf( mymatrix ); And I am wondering if there is anyone who know what format the matrix should have? In other words, what part of the matrix coresponds to the front-vector, up-vector, right-vector, scaling, those sort of things?

##### Share on other sites
There are two parts to the problem: building the matrix, and inverting the matrix.

OpenGL matrices are column major and are constructed so as to work with column vectors, which (for affine transforms) means:

- The x basis vector is stored in elements 0, 1 and 2
- The y basis vector is stored in elements 4, 5 and 6
- The z basis vector is stored in elements 8, 9 and 10
- The translation is stored in elements 12, 13 and 14

Presumably you have a quaternion-to-matrix function that will take care of computing the basis vectors for you. Make sure though that the conventions of your quat conversion function match OpenGL's; otherwise you'll get the reverse of the desired rotation.

Once you've build the matrix, it needs to be inverted. This can be done using a 4x4 general inversion function (which should be fairly easy to find online), or using a special-case function that takes advantage of the nature of the camera transform to save some operations.

##### Share on other sites
Well here is how I build my VIEW matrix from the left, right and up vectors:

float viewmatrix[16]=
{
right.x, up.x, -forward.x, 0,
right.y, up.y, -forward.y, 0,
right.z, up.z, -forward.z, 0,
-((right.x,position.x) + (right.y,position.y) + (right.z,position.z)),
-((up.x, position.x) + (up.y , position.y) + up.z , position.z)),
-((forward.x, position.x) + (forward.y, position.y) + (forward.z, position.z)),
1.0f
};

where position is the current position of the camera. Before you can use this matrix you will need to multiply this with the projection matrix so as to get a viewprojection matrix. I get my projection matrix from a frustum class I have which maintains a projection matrix (since glu isn't available at my end).

##### Share on other sites
Thanks for answering and thanks for the code. I didn't know you had to multiply the view matrix with the projection matrix. Can I just change the matrix like this then?

float projection_matrix[16], viewprojection_matrix[16];
glGetFloatv( GL_PROJECTION_MATRIX, projection_matrix );
viewprojection_matrix = DotProduct( view_matrix, projection_matrix );

##### Share on other sites
Quote:
 Original post by ScreenSaverThanks for answering and thanks for the code. I didn't know you had to multiply the view matrix with the projection matrix. Can I just change the matrix like this then?float projection_matrix[16], viewprojection_matrix[16];glGetFloatv( GL_PROJECTION_MATRIX, projection_matrix );viewprojection_matrix = DotProduct( view_matrix, projection_matrix );glLoadMatrixf( viewprojection_matrix );
You can multiply your own matrices with the top matrix in the stack via glMultMatrix(). In general it shouldn't be necessary to do it manually as in your example.

Aside from that, the matrices in the product are in the wrong order, and unless DotProduct() is a misnamed matrix multiplication function, the result will be wrong anyway.

##### Share on other sites
OK. glMultMatrix() works. But none of the methods works completely. It works perfectly as long as I only rotate around the forward vector OR the up vector. But when I rotate around the right vector it gets all wierd. I have rebuilt my camera class two times over the last 30 minutes and I still have the same problem. I'm not sure but when I run the program it looks like everything "jumps" when it goes over some threshhold.

##### Share on other sites
Fixed the jumping at least (one - sign to much). Now the problem is that the view matrix doesn't rotate around up, even if the quaternions does.

[EDIT]
Correction: It is rotating, the wrong way.

[Edited by - ScreenSaver on July 6, 2006 4:19:54 PM]

1. 1
Rutin
31
2. 2
3. 3
4. 4
5. 5

• 13
• 48
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632965
• Total Posts
3009545
• ### Who's Online (See full list)

There are no registered users currently online

×