math - Confusion between C++ and OpenGL matrix order (row-major vs column-major) -


i'm getting thoroughly confused on matrix definitions. have matrix class, holds float[16] assumed row-major, based on following observations:

float matrixa[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; float matrixb[4][4] = { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 }, { 12, 13, 14, 15 } }; 

matrixa , matrixb both have same linear layout in memory (i.e. numbers in order). according http://en.wikipedia.org/wiki/row-major_order indicates row-major layout.

matrixa[0] == matrixb[0][0]; matrixa[3] == matrixb[0][3]; matrixa[4] == matrixb[1][0]; matrixa[7] == matrixb[1][3]; 

therefore, matrixb[0] = row 0, matrixb[1] = row 1, etc. again, indicates row-major layout.

my problem / confusion comes when create translation matrix looks like:

1, 0, 0, transx 0, 1, 0, transy 0, 0, 1, transz 0, 0, 0, 1 

which laid out in memory as, { 1, 0, 0, transx, 0, 1, 0, transy, 0, 0, 1, transz, 0, 0, 0, 1 }.

then when call gluniformmatrix4fv, need set transpose flag gl_false, indicating it's column-major, else transforms such translate / scale etc don't applied correctly:

if transpose gl_false, each matrix assumed supplied in column major order. if transpose gl_true, each matrix assumed supplied in row major order.

why matrix, appears row-major, need passed opengl column-major?

matrix notation used in opengl documentation not describe in-memory layout opengl matrices

if think it'll easier if drop/forget entire "row/column-major" thing. that's because in addition row/column major, programmer can decide how want lay out matrix in memory (whether adjacent elements form rows or columns), in addition notation, adds confusion.

opengl matrices have same memory layout directx matrices.

x.x x.y x.z 0 y.x y.y y.z 0 z.x z.y z.z 0 p.x p.y p.z 1 

or

{ x.x x.y x.z 0 y.x y.y y.z 0 z.x z.y z.z 0 p.x p.y p.z 1 } 
  • x, y, z 3-component vectors describing matrix coordinate system (local coordinate system within relative global coordinate system).

  • p 3-component vector describing origin of matrix coordinate system.

which means translation matrix should laid out in memory this:

{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, transx, transy, transz, 1 }. 

leave @ that, , rest should easy.

---citation old opengl faq--


9.005 opengl matrices column-major or row-major?

for programming purposes, opengl matrices 16-value arrays base vectors laid out contiguously in memory. translation components occupy 13th, 14th, , 15th elements of 16-element matrix, indices numbered 1 16 described in section 2.11.2 of opengl 2.1 specification.

column-major versus row-major purely notational convention. note post-multiplying column-major matrices produces same result pre-multiplying row-major matrices. opengl specification , opengl reference manual both use column-major notation. can use notation, long it's stated.

sadly, use of column-major format in spec , blue book has resulted in endless confusion in opengl programming community. column-major notation suggests matrices not laid out in memory programmer expect.



Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -