Question

There is some major (no pun intended) confusion for me about row major and column major matrices.

https://gamedev.stackexchange.com/questions/18901/can-someone-explain-the-reasons-for-the-implications-of-colum-vs-row-major-in

In the above article it is stated that( for a column matrix) the translation portion is in indices 12, 13, and 14. In the 3D math primer for graphics and gamedevelopment book it is stated that they are in indices 3, 7 and 11.

So, i'm trying to build a matrix class. My array is layed out as follows:

[ xx, yx, zx, 0, xy, yy, zy, 0, xz, yz, zz, 0, xT, yT, zT, 1]

My question is, is this the righ way to lay out a column major matrix? or should it be layed out like one of the following?

[ xx, xy, xz, xT, yx, yy, yz, yT, zx, zy, zz, zT, 0, 0, 0, 1]

[ xx, yx, zx, xT, xy, yy, zy, yT, xz, yz, zz, zT, 0, 0, 0, 1]

[ xx, xy, xz, 0, yx, yy, yz, 0, zx, zy, zz, 0, xT, yT, zT, 1]

Edit to make the question more clear:

I pass my matrix to the next function (as an Array)

 gl.uniformMatrix4fv( shaderProgram.pMatrixUniform, false, pMatrix );

The question is: in what of the 4 orders ( explained above) does this method expect the matrix to be? http://msdn.microsoft.com/en-us/library/ie/dn302458%28v=vs.85%29.aspx

the documentation doesn't say anything about order, only about transposing the whole matrix. But how do i know if i have to transpose it, if i don't know what it would normaly expect.

Was it helpful?

Solution

It doesn't matter? There are 2 things often conflated in 3D math libraries. Column vs Row storage and separately how those elements are used.

Most libraries that I've used store the data as you've written it.

[ xx, yx, zx, 0, xy, yy, zy, 0, xz, yz, zz, 0, xT, yT, zT, 1]

or another way to picture it

[ xAxis, 0,
  yAxis, 0,
  zAxis, 0,
  translation, 1]

They then provide matrix functions. If you changed the storage format you could easily change the functions to so the behavior didn't change. It's only if you allow direct access to the elements of the matrix that storage format matters and then you can still choose whatever format you want, you just have to tell you users what that format is.

As an example with the format as it is above a typical GLSL shader might have code like this

gl_Position = u_worldViewProjection * a_position;

If you switched the order to

[ xx, yx, zx, xT, xy, yy, zy, yT, xz, yz, zz, zT, 0, 0, 0, 1]

You'd just change the GLSL to

gl_Position = a_position * u_worldViewProjection;

On the other hand, since (at least from my experience), most libraries store their matrixes the same as the top example it seems like a good idea to do the same?

OTHER TIPS

The transformation matrix is such that

| R  v | | p | = | v + R*p |
| 0  1 | | 1 |   | 1       |

which is interpreted as translate first by v, then rotate by R. So if you take the rows of the transformation matrix you have

| R11 R12 R13 v1 |
| R21 R22 R23 v2 | -> [R11,R12,R13,v1,R21,R22,R23,v2,...]
| R31 R32 R33 v3 |
|  0   0   0  1  |

All you have to do to confirm this is do a translation by (1,2,3) and then use glGetFloat(GL_MODELVIEW_MATRIX, matrix) to probe the data.

reference: http://relativity.net.au/gaming/java/ModelViewMatrix.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top