Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct OpenGL matrix format?

My question simply is: what is the correct format for the Projection and ModelView matrix?

I've been told that the following example matrices are transposed and are not built like OpenGL matrices should be.

ModelView Matrix
{(1, 0, 0, 0)
(0, 0.7071068, 0.7071068, 0)
(0, -0.7071068, 0.7071068, 0)
(0, -141.4214, -141.4214, 1)}

Projection Matrix
{(1.931371, 0, 0, 0)
(0, 2.414213, 0, 0)
(0, 0, -1.0002, -1)
(0, 0, -2.0002, 0)}
like image 493
Cobra_Fast Avatar asked Dec 05 '10 20:12

Cobra_Fast


1 Answers

Edit: this answer is in serious need of an update. Namely, there is no consideration about shaders.

As @gman points out in the comments, whether to use row-major or column-major depends on how you do your math. You may choose one or the other (or even both at different times if you don't think that's confusing) just as long as they match your coordinate systems and order of operations.

I'm leaving this answer as community wiki in case someone has the time and will to update it.


OpenGL specifies matrices as a one-dimensional array listed in column-major order, ie with elements ordered like this:

m0 m4 m8  m12
m1 m5 m9  m13
m2 m6 m10 m14
m3 m7 m11 m15

So if you initialize an array this way, in C or pretty much any other language, the resulting matrix will look like it needs transposing, because C code reads left-to-right first and then top-to-bottom (in other words, like if it were in row-major order):

int mat[16] = {
    0, 1, 2, 3,
    4, 5, 6, 7,
    8, 9, 10, 11,
    12, 13, 14, 15,
}

By the way, OpenGL has both glLoadTransposeMatrix and glMultTransposeMatrix, which you can use instead of glLoadMatrix and glMultMatrix, so this shouldn't be a problem.

like image 53
3 revs Avatar answered Oct 05 '22 19:10

3 revs