This topic is 2211 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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...

##### Share on other sites
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.

##### Share on other sites
To further expand, the OpenGL perpective projection matrix is calculated as so:

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.

##### Share on other sites

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

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?

##### Share on other sites

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.

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 96
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009641
• ### Who's Online (See full list)

There are no registered users currently online

×