Row v Column Majors

Started by
1 comment, last by Hodgman 9 years, 3 months ago

I'm a bit confused. I keep reading the following: OpenGL uses a right-handed coordinate system, column-major matrices, column vectors, and post-multiplication.

Yet the in memory layout of an OpenGL matrix is:


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

Which is a row major layout!

I'm trying to roll my own math library, looking at glm for guidance, matrices are indexed as


mat[row][col]

which would seem to only further strengthen that OpenGL is indeed using row matrices.

It sounds like the matrix notation used in the OpenGL docs (and blue book) does not describe the in memory layout of the matrices.

Which begs the question, does this mean i should be pre-multiplying matrices, or do i need to account for the in-memory layout when doing matrix multiplications and keep post-multiply?

again, turning to glm for help, if i want to translate a model to 1, 2, 3 and then scale it up by 2 i need to do post multiplication:


glm::mat4 mat = glm::translate(glm::vec3(1.0f, 2.0f, 3.0f)) * glm::scale(glm::vec3(2.0f, 2.0f, 2.0f));
Results in:

2, 0, 0, 0,

0, 2, 0, 0,

0, 0, 2, 0,

1, 2, 3, 1

But, if the matrix has a row layout, shouldn't that multiplication be in reverse?

So confused.....

Advertisement
OpenGL uses column-major notation. This has nothing to do with how it is laid out in memory.


Which begs the question, does this mean i should be pre-multiplying matrices, or do i need to account for the in-memory layout when doing matrix multiplications and keep post-multiply?

OpenGL uses column-major notation—multiply right-to-left as long as you are using their multiplication routines.

If you are making your own math routines, it is your choice. It has nothing to do with how they are in memory. You can write your matrix multiplication routine to be post-multiply or pre-multiply without changing the memory layout.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Yet the in memory layout of an OpenGL matrix is:

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
Which is a row major layout!

No, not necessarily!

Given a matrix:
A,B,C,D
E,F,G,H
I,J,K,L
M,N,O,P

If the *memory layout* is row-major, it will be stored as:
ABCDEFGHIJKLMNOP
If the memory layout is column -major, it will be
AEIMBFJNCGKODHLP

Now leaving computers and going to pure math:
If the mathematical conventions are row major, then the X-axis basis vector will be stored in ABC.
If the mathematical conventions are column-major, then the X-axis basis vector will be stored in AEI.

So, the data you've observed is just using the same memory storage convention and mathematical convention.
It is either using row-major memory layouts and row-major maths, or its using column-major memory layouts and column-major maths.

From the data you've presented, you can't tell which it is.
With the further information that "GL is column major" then we can then deduct that it must use both column-major memory layouts and column-major maths notations.
That means that your 2d visualization is wrong. The 1d dataset you posted should actually be visualised as:
x.x y.x z.x p.x
x.y y.y z.y p.y
x.z y.z z.z p.z
  0   0   0   1

This topic is closed to new replies.

Advertisement