[Answered] 3x4 Matrix instead of 4x4

Started by
4 comments, last by 21st Century Moose 11 years, 6 months ago
Hi there,

I have a quick question: Why does OpenGL use 4x4 matrices instead of 3x4 matrices? I never saw the last row being used.

Thank you

Hide yo cheese! Hide yo wife!

Advertisement
The math assumes 4x4 i think. Using 3x4 would require some ugly special code to fill in the missing row (or is it column? i hate this stuff)

It would also be ugly to mix 3x4 and 4x4 as i believe you would need a 4x4 for the perspective stuff...

o3o

If you're using M*v convention (like is typical with OpenGL), the last row stores parameters related to projection transforms. For affine transforms, the last row is always (0, 0, 0, 1). It's fully possible to use only float3x4, but one needs to be careful about the data layout. For genericity, float4x4 is often used, since being the most generic form it allows using the same matrix type for both projection transforms and other transforms.

In MathGeoLib, I have the class float3x4, which I use in my game when I explicitly want to specify an affine transform without projection, or as storage to save a few bits, or when I want to save a few cycles off the computations. Those are rather minor though, and therefore just using the same type float4x4 for all math often trumps the rest.
To further expand, the OpenGL perpective projection matrix is calculated as so:
gl_projectionmatrix_eq16.png


You need that last row for your projection to work. The 1 in that row copies the z coordinate to use it in the homogeneous divide (there is a point where the view frustum is converted to a 2x2x2 box (each axis going from -1 to +1), and coordinates in this system are called "homogeneous coordinates"). It's a -1 though because of the right hand rule and the way OpenGL's axes work. It needs to be a 4x4 matrix, you see.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

To further expand, the OpenGL perpective projection matrix is calculated as so:
gl_projectionmatrix_eq16.png


You need that last row for your projection to work. The 1 in that row copies the z coordinate to use it in the homogeneous divide (there is a point where the view frustum is converted to a 2x2x2 box (each axis going from -1 to +1), and coordinates in this system are called "homogeneous coordinates"). It's a -1 though because of the right hand rule and the way OpenGL's axes work. It needs to be a 4x4 matrix, you see.

Just curious, apart from the perspective projection, is there any other case where you need the bottom row?

Just curious, apart from the perspective projection, is there any other case where you need the bottom row?


Any kind of projection needs the bottom row - this includes perspective and ortho, but these are just two special cases of projection, and it's entirely possible to define your own custom projections.

Another case where you need the bottom row is if you need to transpose a matrix; transposing a 3x4 will give you 4x3 and suddenly your matrix * vector multiplications may no longer be valid. Transposing a matrix isn't a rare or exotic or special-case operation either - it's used for lighting.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement