Jump to content
  • Advertisement
Sign in to follow this  
thecheeselover

OpenGL [Answered] 3x4 Matrix instead of 4x4

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!