column vs. rows

Started by
1 comment, last by Hodgman 12 years, 9 months ago
[font="Times"]
I've been wondering which matrix format are you using.

Many applications use row-major matrices but GLSL vertex shader uses column-major matrices (so do I).
When I was writing export plugin for blender I discovered that blender uses row-major matrices (it gives me different results like those in my application but its was not problem at all)
It's not big problem so I can use both because post-multiplying with column-major matrices produces the same result as pre-multiplying with row-major matrices.

Just simplified example:
I want to get local position of object in coordinate system 'A' (A is 4x4 matrix, position of object is represented by vector...). This is simple and easy to imagine way to determine multiplication order
-------------------------------------------------
If I use row vector to represent position:
localPosition (x,y,z,w)
to get global position I can use:
localPosition * A = globalPosition

so
localPosition = globalPosition*A[sup]-1[/sup]

-------------------------------------------------
If I use column vector to represent position:
localPosition (x; y; z; w)
to get global position I can use:
A * localPosition = globalPosition

[/font]
[font="Times"]using transpose: [/font]
[font="Times"]localPosition[sup]T[/sup] * A[sup]T[/sup] = globalPosition[sup]T[/sup][/font]
[font="Times"][sup]localPosition[sup]T[/sup] = globalPosition[sup]T[/sup][/sup][/font][font="Times"][sup]* (A[/sup][/font][font="Times"][sup][sup]-1[/sup][/sup][/font][font="Times"][sup])[/sup][/font][font="Times"][sup][sup]T[/sup][/sup][/font]
[font="Times"][sup][/sup]
so
localPosition = A[sup]-1[/sup] * globalPosition[font="arial, verdana, tahoma, sans-serif"] [/font][/font][font="Times"](direct analogy to pre/post multiplying)
[/font][font="Times"]-------------------------------------------------

This is same with matrices multiplication when converting from global to local space(A and B are 4x4 matrices). It is bit harder to imagine but same as first example.[/font]
[font="Times"][font="arial, verdana, tahoma, sans-serif"]
[font="Times"]row-major:[/font]
[font="Times"]to get global coordinate system [/font]
[font="Times"][font="arial, verdana, tahoma, sans-serif"][font="Times"][font="arial, verdana, tahoma, sans-serif"][font="Times"]localB * [/font][/font]globalA[/font][font="Times"] [/font][font="Times"]=globalB[/font][/font][/font]
[font="Times"][font="arial, verdana, tahoma, sans-serif"] [/font]
[/font][font="Times"]so[/font][/font][/font]
[font="Times"][font="arial, verdana, tahoma, sans-serif"][font="Times"]localB[/font][font="Times"]=globalB*globalA[sup]-1[/sup][/font][/font][/font]
[font="Times"]------------------------------------------------------------------------[/font]
[font="Times"]column-major:[/font]
[font="Times"]to get global coordinate system[/font]
[font="Times"][font="arial, verdana, tahoma, sans-serif"][font="Times"]globalA*[/font][font="Times"] localB[/font][font="Times"]=globalB[/font][/font]
[/font]
[font="Times"][font="arial, verdana, tahoma, sans-serif"][font="Times"]fo example using invert: [/font][/font][/font]
[font="Times"]globalA*[/font][font="Times"] localB[/font][font="Times"]*globalB[sup]-1[/sup][/font][font="Times"]=globalB[/font][font="Times"]*globalB[sup]-1[/sup][/font]
[font="Times"][font="arial, verdana, tahoma, sans-serif"][font="Times"]globalA=(localB*globalB[sup]-1[/sup])[sup]-1[/sup][/font]
[font="Times"][font="arial, verdana, tahoma, sans-serif"][font="Times"]globalA[/font][font="Times"][sup]-1[/sup][/font][font="Times"]=localB*globalB[sup]-1[/sup][/font][/font][/font][/font][/font]
[font="Times"][font="arial, verdana, tahoma, sans-serif"][font="Times"][font="arial, verdana, tahoma, sans-serif"][font="Times"][sup]
[/sup]
[/font][/font]
[/font][/font][/font][font="Times"][font="arial, verdana, tahoma, sans-serif"][font="Times"][sup][font="arial, verdana, tahoma, sans-serif"][font="Times"]so[/font][/font][/sup][/font][/font][/font]
[font="Times"][font="arial, verdana, tahoma, sans-serif"][font="Times"][sup][font="arial, verdana, tahoma, sans-serif"][font="Times"]localB=[/font][/font][/sup][/font][/font][/font][font="Times"][font="arial, verdana, tahoma, sans-serif"][font="Times"][font="arial, verdana, tahoma, sans-serif"][font="Times"][sup][font="arial, verdana, tahoma, sans-serif"][font="Times"]globalA[/font][font="Times"][sup]-1[/sup][/font][/font][/sup][/font][/font][/font][/font][/font][font="Times"][font="arial, verdana, tahoma, sans-serif"][font="Times"][sup][font="arial, verdana, tahoma, sans-serif"][font="Times"]*globalB[/font][/font][/sup][/font][/font][/font]
[font="Times"][font="arial, verdana, tahoma, sans-serif"][font="Times"][sup][font="arial, verdana, tahoma, sans-serif"][font="Times"]------------------------------------------------------------------------[/font][/font][/sup][/font][/font][/font]
[font="Times"][font="arial, verdana, tahoma, sans-serif"] [/font][/font]
[font="Times"]
What am I trying to point out is which vector format (matrix format) is correct, if there is any standard/convention...

Thans for your opinions[/font]
Advertisement
There is no correct format, both forms are used extensively.

DirectX like row major.
OpenGL likes column major.

Use whatever 'major' prevents you from having to do the most trasposes.
(and be consistent! pick one)
DirectX does not "like row major" any more -- you can use either, and in fact, column major is now the default.

In HLSL you can use the [font="Courier New"]row_major[/font] and [font="Courier New"]column_major[/font] keywords to specify the internal storage format for a matrix variable. If you don't specify one, then the default is column major (just like GLSL).

When writing matrix/vector math using SIMD instructions (on the CPU, or the GPU via GLSL/HLSL), the math will be exactly the same, but the amount of instructions required to perform the math will be different depending on whether you're using row-major or column-major. Depending on the kinds of operations you're performing, one storage format will be more optimal than the other.

This topic is closed to new replies.

Advertisement