# Direct3D and OpenGL Matrix representation

As I read in OpenGL matrices are column major. It means that if I create a 16 element array first four elements are the first column in matrix. Is it the same for Direct3D or any transformation's required.

Heishe is right about memory layout and matrices being the same, but his answer is somewhat misleading. Such things as right-handed or left-handed matrices don't exist. Be it a right- or left- handed coordinate system, math and the matrix stays the same, matrix operations give the same result for both of these systems. How we will interpret the data later, thats the difference.

In OpenGL, matrix operations are pre-concatenated, which means concatenated transformations are applied from right to left. Direct3D, however, applies concatenated transformations from left to right.

So what you need to do is to match the order of operation in OpenGL to the order of Direct3D. When writing OpenGL code, you apply the transform that you want to occur first last; when writing DirectX you're doing the opposite. Let's say you just transformed vector v in OpenGL by matrices A and B: v' = ABv In Direct3D the same transformation would be v' = vBA.

• Thanks for the clear explanation. Could you please tell whether the storage I.e. memory layout of a matrix class matters in the system (OpenGL or DirectX) used? Jan 30, 2013 at 7:46

DirectX matrices are right-handed and OpenGL requires left handed matrices, but while OpenGLs matrices are column-major, DirectX's matrices are row-major, so they're layout in memory is the same.

• OpenGL matrices are definitely NOT left-handed (and cooridnate systems neither). Aug 15, 2011 at 17:38

Neither D3D nor OpenGL specify or enforce a matrix handedness. True, the matrix stack functions in OpenGL are RH, but there's absolutely nothing to stop you from writing your own matrix library (or using a 3rd party one) that uses LH coords and loading the result using glLoadMatrixf. Likewise D3D has a full set of both RH and LH matrix functions. This is an old old old misunderstanding and needs to be put to bed.

Regarding row-major vs column major, please see the following: http://www.opengl.org/resources/faq/technical/transformations.htm:

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.

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.