PDA

View Full Version : Order of initializing matrices

05-17-2012, 07:06 PM
The meaning of column-major in opengl has confused me.

A 4x4 matrix looks like this with the numbers representing the sequential order of each number,

1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16

Correct? So 1 - 4 might represent the x, y, z, and w of some vector...

Would I initialize a float array or a mat4 like this:

mat4( 1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16);

or like this?

mat4( 1, 5, 9, 13,
2, 6, 10, 14,
3, 7, 11, 15,
4, 8, 12, 16);

Thanks for any help.

BionicBytes
05-18-2012, 01:40 AM
It's simply a convention. You can use which ever convention suits you and your code base - but be consitent!
When you upload a matrix as a uniform, there are options to transpose or you could work with the Column-Major OpenGL convention in the first place.
Personally, I stick with the OpenGL column major approach which then makes interaction with any OpenGL matrix seamless.
To answer your last question, it depends on the approach you want to take. If you want column major, then
mat4( 1, 5, 9, 13,
2, 6, 10, 14,
3, 7, 11, 15,
4, 8, 12, 16);
is the way to do it and elements 13,14,15 represent the Translate (X,Y,Z) portion of the Matrix (eg when using glTranslatef command ).

Dark Photon
05-18-2012, 06:04 AM
It's simply a convention. You can use which ever convention suits you and your code base - but be consitent!
When you upload a matrix as a uniform, there are options to transpose or you could work with the Column-Major OpenGL convention in the first place.
Personally, I stick with the OpenGL column major approach which then makes interaction with any OpenGL matrix seamless.

OpenGL requires column-major operator-on-the-left order:

P*V*M * vobj = vclip

In your C++ code (which uses row-major ordered arrays), it's simplest to use row-major operator-on-the-right order for your matrix storage and math:

vobj * M*V*P = vclip

These two changes cancel each other out and you end up storing exactly the same values in exactly the same matrix locations, and there's no need to do matrix transposes when passing matrix values to OpenGL.

BionicBytes
05-18-2012, 08:40 AM
Yep, totally agree with Dark Photon.
However, if like me you end up writing your own matrix and vector classes you need to debug them, and that's where matching the OGL convention is so much easier. Once you then endup with something which works it's then hard to justify changing the convention.