   Matrices in OpenGL
  I'm just confused on how OpenGL interprets a 16 element array as a matrix. I've seen it explained by column and row. Which is it?Is it like:[0  1   2  3][4  5   6  7][8  9  10 11][12 13 14 15]or[0  4  8  12][1  5  9  13][2  6  10 14][3  7  11 15]Also when multiplying a matrix by a vector should I use a column vector or row?Thanks in advance   
  The latter. OpenGL expects matrices in column-major format. Also, you should use column vectors with it.   ;  In memory, both OpenGL and Direct3D store matrices in the later form, though, notationally they are transposes:OGL[ a e i m] = float M[16] = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p};    <span class="cpp-comment">// where m,n,o represent tx, ty, yz respectively.</span><br>   [ c g k o]<br>   [ d h l p]<br><br>D3D[ a b c d] = <span class="cpp-keyword">float</span> M[<span class="cpp-number">16</span>] = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p};<br>   [ e f g h] <span class="cpp-comment">// where m,n,o represent tx, ty, yz respectively.</span><br>   <br>   [ m n o p]<br><br></pre></div><!–ENDSCRIPT–><br><br>The confusion over this matter stems from the fact that OGL has chosen the column vector, and hence column-major, conventions in its documentation. The selection is arbitrary, but colum-major notation is more or less the accepted notation in graphics applications. That said, it is contrary to how our programmer's brain is wired to think having been weened &#111;n the row-major convention of arrays. In OGL Matrix-vector multiplication is of the form: v' = Mv.<br><br><!–QUOTE–><BLOCKQUOTE><span class="smallfont">Quote:</span><table border=0 cellpadding=4 cellspacing=0 width="95%"><tr><td class=quote><!–/QUOTE–><!–STARTQUOTE–><i>OpenGL Technical FAQ:</i><br>Are OpenGL matrices column-major or row-major?<br><br>For programming purposes, OpenGL matrices are 16-value arrays with base vectors laid out contiguously in memory. The translation components occupy the 13th, 14th, and 15th elements of the 16-element matrix.<br><br>Column-major versus row-major is purely a notational convention. Note that post-multiplying with column-major matrices produces the same result as pre-multiplying with row-major matrices. The OpenGL Specification and the OpenGL Reference Manual both use column-major notation. You can use any notation, as long as it's clearly stated.<br><br>Sadly, the use of column-major format in the spec and blue book has resulted in endless confusion in the OpenGL programming community. Column-major notation suggests that matrices are not laid out in memory as a programmer would expect.<br><!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>D3D, &#111;n the other hand, has opted to use the row vector convention, in which the matrix resembles how our programmer's brain expects things to be laid out in memory. In D3D Matrix-vector multiplication is of the form: v' = vM.;  It creates a lot of confusion. OpenGL uses column major, but it transforms vector x by T1 then T2 by x*T1*T2 rather than T2*T1*x. The T1 in the first equation is the transpose of T1 in the second equation. Column major versus row major is effectly a transpose. The transpose of a transpose is the original matrix. So if you're going to use the matrices returned by OpenGL as T2*T1*x then it is in row major order. If instead you are going to use it as x*T1*T2 like OpenGL does then it is column major order.   ;  So If I have a class Matrix and a class Vector and I had Matrix m and Vector v, m*v should be done in row major? Or if I had m1*m2?   ;Quote:Original post by Ganoosh_So If I have a class Matrix and a class Vector and I had Matrix m and Vector v, m*v should be done in row major? Or if I had m1*m2?Whether it's m*v or v*m has nothing to do (directly at least) with whether the matrices are row- or column-major. 'Majorness' has to do with how the matrix is laid out in memory, not how it functions mathematically. The difference between row and column vectors, however, is mathematical; v*m corresponds with row vectors, and m*v corresponds with column vectors.D3D uses row-major matrices and row vectors. OpenGL uses column-major matrices and column vectors. The result of this, as has probably been mentioned already, is that D3D and OpenGL matrices are actually laid out in memory the same way (although the notation we use to represent the matrix operations differs between the two APIs).   ;  M * v is representative of column vectors.v * M is representative of row vectors.You can choose either convention, though the memory layout of a matrix should be the same as whau has been outlined above, ie, where the translation x, y and z components of the matrix are in the 13th, 14th and 15th floats.Without going into too much detail, the reason that matrices are layed out this way is that it allows for SIMD optimizations for matrix-vector multiplication. This is also somewhat of a throwback to the software T&L days, when there wasn't dedicated graphics processors or helpful CPU features like dot product instuctions. If CPUs commonly had a dot-product instruction, then the alternative layout would actually be more optimizable, but IIRC, not even SSE3 has a dot product instruction (though some embedded/game systems do, such as the xbox 360 and Dreamcast[which also had a matrix-matrix multiply instruction!] as well as Video card shaders.)   