Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


[Answered] 3x4 Matrix instead of 4x4


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 thecheeselover   Members   -  Reputation: 330

Like
0Likes
Like

Posted 30 September 2012 - 01:55 PM

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!

Sponsor:

#2 Waterlimon   Crossbones+   -  Reputation: 2596

Like
1Likes
Like

Posted 30 September 2012 - 02:00 PM

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


#3 clb   Members   -  Reputation: 1784

Like
2Likes
Like

Posted 30 September 2012 - 03:20 PM

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.
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

#4 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 30 September 2012 - 04:55 PM

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


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

#5 Ed Welch   Members   -  Reputation: 485

Like
0Likes
Like

Posted 01 October 2012 - 09:32 AM

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


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?

#6 mhagain   Crossbones+   -  Reputation: 8129

Like
1Likes
Like

Posted 01 October 2012 - 10:37 AM

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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS