Question about the matrix in Opengl
Hi all! I am new comer in opengl. I have trouble with the glGetFloatv(GL_MODELVIEW_MATRIX, lastmatrix); I did a simple test program like this:
glLoadIdentity();
glRotatef(45,0,0,1);
glGetFloatv(GL_MODELVIEW_MATRIX, lastmatrix);
//
the value of the lastmatrix is { 0.70710677, 0.70710677 , 0 ,0
-0.70710677 , 0.70710677 , 0 ,0
0.0000000000, 0.00000000, 1 ,0
0.0000000000, 0.00000000, 0 ,1}
But I thought it should be { 0.70710677, -0.70710677 , 0 ,0
0.70710677 , 0.70710677 , 0 , 0
0.0000000000, 0.00000000, 1 ,0
0.0000000000, 0.00000000, 0 ,1}
What is wrong with this? I get the transpose of the matrix I want. Then I use another program to test, it is all right;
//
glLoadIdentity();
GLfloat lastmatrix[16] = {2,5,0,1,
0,1,2, 2,
5,4,4,4,
9,5,2,0};
glMultMatrixf(lastmatrix);
glGetFloatv(GL_MODELVIEW_MATRIX, lastmatrix);
then I get the right answer, the value of the lastmatrix is
{2,5,0,1,
0,1,2, 2,
5,4,4,4,
9,5,2,0};
So can anybody tell me what is wrong with this? Thanks a lot!
guess u are a directx guy, in OpenGL matrices are all in column major order.. that is when u put something like
float mat[16] = {2,3,4,5,
6,7,8,9,
......
};
it actually means that
2,3,4,5 is the first COLUMN of the matrix, i.e.
|2,6
|3,7
|4,8
|5,9
float mat[16] = {2,3,4,5,
6,7,8,9,
......
};
it actually means that
2,3,4,5 is the first COLUMN of the matrix, i.e.
|2,6
|3,7
|4,8
|5,9
pengsenl,
OpenGL uses column-major format for matrix. So 4 elements in a column are filled first then move on the next column.
OpenGL uses column-major format for matrix. So 4 elements in a column are filled first then move on the next column.
Thanks a lot! But i still want to know if I multiply the current matrix M by another matrix like this
glLoadIdentity();
GLfloat A[16] = {2,5,0,1,
0,1,2, 2,
5,4,4,4,
9,5,2,0};
glMultMatrixf(A);
Then how opengl do it? the result is M*A' or M*A?
If I use the glGetFloatv(GL_MODELVIEW_MATRIX, m_lastMatrix); It return the matrix in column order or in row order?
glLoadIdentity();
GLfloat A[16] = {2,5,0,1,
0,1,2, 2,
5,4,4,4,
9,5,2,0};
glMultMatrixf(A);
Then how opengl do it? the result is M*A' or M*A?
If I use the glGetFloatv(GL_MODELVIEW_MATRIX, m_lastMatrix); It return the matrix in column order or in row order?
It's M*A of course. Just silly to transpose the matrix before multiplication. Same with glGetFloat; it returns the matrix, not it's transpose.
But then again, you're probably still confused about what the linear layout of elements really are, compared to the transposed layout.
A matrix M with elements like this:
is defined as a one-dimensional array in C as
From this layout and the corresponding mapping, you can determing how multiplication behaves in OpenGL.
But then again, you're probably still confused about what the linear layout of elements really are, compared to the transposed layout.
A matrix M with elements like this:
| a, b, c, d |M = | e, f, g, h | | i, j, k, l | | m, n, o, p |
is defined as a one-dimensional array in C as
float M[16] = {a, e, i, m, b, f, j, n, c, g, k, l, d, h, l, p};
From this layout and the corresponding mapping, you can determing how multiplication behaves in OpenGL.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement