Modelview matrix format
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?
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.
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.
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).
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).
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 );
glLoadMatrixf( viewprojection_matrix );
float projection_matrix[16], viewprojection_matrix[16];
glGetFloatv( GL_PROJECTION_MATRIX, projection_matrix );
viewprojection_matrix = DotProduct( view_matrix, projection_matrix );
glLoadMatrixf( viewprojection_matrix );
Quote:Original post by ScreenSaverYou 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.
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 );
glLoadMatrixf( viewprojection_matrix );
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement