**0**

# Bottom Row of 4x4 Matrix

###
#1
Members - Reputation: **116**

Posted 15 December 2012 - 09:40 PM

In an opengl matrix, you have:

R R R T

R R R T

R R R T

A A A B

where R is rotation and T is translation.

what is A and B?

how would I apply A and B to a Vec3? ( I know how to translate and rotate )

how would I invert A and B?

###
#2
Members - Reputation: **1424**

Posted 16 December 2012 - 03:03 AM

http://www.songho.ca/opengl/gl_transform.html

(the other pages are very useful too!!! so look around)

I'm not really sure if A is used at all, I did this a long time ago, but B is supposed to be the perspective divide element.

Or at least B and the projection matrix modify the 4th element of the position, so that in clip space if you divide by it, you get the ndc coordinates.

like when you do the transformations:

-Model (world) space (apply T here):

model_space_pos = model_mat * vertex

-View (camera) space (apply R here):

view_space_pos = view_mat * model_space_pos

-Clip space (apply projection matrix here):

clip_space_pos = projection_mat * view_space_pos

-normalized device coordinates (do the perspective divide, apply B here):

ndc_pos = clip_space_pos.xyz / clip_space_pos.w

-viewport coordinates (scale & bias to get texture coordinates, scale by window coordinates)

viewport_pos = (ndc_pos.xy * 0.5 + 0.5) * vec2( screen_width, screen_height )

and to the other questions:

you would apply the 4x4 matrix to a 4 component vector (ie vec4)

and you would invert A and B by inverting the 4x4 matrix.

Blog:

http://extremeistan.wordpress.com/

Stuff I wrote:

https://github.com/Yours3lf/libmymath

https://github.com/Yours3lf/linux_gl_fps

https://github.com/Yours3lf/instanced_font_rendering

https://github.com/scrawl/smaa-opengl

https://github.com/Yours3lf/gl_browser_gui

Follow me on twitter:

###
#3
Moderators - Reputation: **9290**

Posted 16 December 2012 - 04:14 AM

The entire bottom row is used for the perspective division, not just the B-element.I'm not really sure if A is used at all, I did this a long time ago, but B is supposed to be the perspective divide element.

Or at least B and the projection matrix modify the 4th element of the position, so that in clip space if you divide by it, you get the ndc coordinates.

For example, for an orthographic projection, the bottom row is [0, 0, 0, x] where x is some value depending on the parameters of the projection matrix. That means that the fourth component of the resulting vector after multiplication is the fourth component of the in multiplying vector, to some scale factor. For glOrtho, x=1 at all times though, which means that as long as the Z-coordinate of the input is 1.0, which is often the case, there fourth component of the resulting vector is also 1.0, and there is effectively no perspective division, hence no perspective since it is an orthographic projection.

On the other hand, for a perspective matrix, the bottom row is [0, 0, x, 0]. That means that the fourth component of the resulting vector is the third component of the multiplying vector, to some scale factor x. The third component is the depth, and hence the perspective division is now dependent on the depth; you now get a perspective effect.

Likewise, the first two elements of the bottom row can also be non-zero to get a perspective effect along the X and/or the Y-axis instead.

It doesn't make much sense to talk about how to apply these elements to a 3-element vector. You simply cannot multiply a 3-element vector by a 4-by-4 matrix in the first place. What you do when adding multiplying the 3-element vector by the rotation part and then adding the translation part as a separate step is really just assuming that the missing fourth component of the 3-element vector is unity. In order to handle the fourth row of the matrix correctly, you have to do the same assumption again, carry out the multiplication, and see how the bottom row affects the other three elements, as well as performing the final perspective division to ensure that the assumption that the fourth component really is unity even after the multiplication.how would I apply A and B to a Vec3? ( I know how to translate and rotate )

In the end, you really have to carry out a full 4-vector times 4x4-matrix multiplication, although you can assume that one element is unity and eliminate its multiplication with the corresponding elements of the matrix, and just add them.

**Edited by Brother Bob, 16 December 2012 - 04:14 AM.**

###
#4
Members - Reputation: **116**

Posted 16 December 2012 - 05:09 PM

one problem I'm having is trying to create a frustum from a unit cube by multiplying each of the cube's vertices by the inverse of a projection matrix (as I'm told that is how it's done) so I can do basic view-frustum culling with world objects. currently, the result I'm getting is not a frustum.

###
#5
Members - Reputation: **116**

Posted 16 December 2012 - 05:26 PM

doing this wihtout inverting for the first try:

I've got a vector (1,2,3) and I want to multiply it by my perspective matrix:

0.75 0 0 0

0 1 0 0

0 0 -1.0001 -0.20001

0 0 -1 0

after applying rotation and translation, I have this:

0.75,2,-3.20031

and then to apply the projection division... this is where I'm not totally sure how to apply it.. but I think I would end up with a Vec4:

0.75,2,-3.20031,-1

###
#6
Moderators - Reputation: **9290**

Posted 16 December 2012 - 05:42 PM

Here's your matrix and vector and the product of the two.

>> M M = 0.7500 0 0 0 0 1.0000 0 0 0 0 -1.0001 -0.2000 0 0 -1.0000 0 >> v v = 1 2 3 1 >> p = M*v p = 0.7500 2.0000 -3.2003 -3.0000See how the fourth element of the vector

*p*is -3.0? That's because your 3-dimensional vector is actually a 4-dimensional vector with an implicit element at the end with a value of 1.0.

The perspective division is just dividing all elements of the vector by the fourth element:

>> p./p(4) ans = -0.2500 -0.6667 1.0668 1.0000So your resulting projected 3-dimensional vector is (-0.2500, -0.6667, 1.0668).

**Edited by Brother Bob, 16 December 2012 - 05:46 PM.**

###
#11
Members - Reputation: **1561**

Posted 17 December 2012 - 05:15 AM

rot: Create a rotation matrix.

scale(x,y,z): Create a scaling matrix

transl(x,y,z): Create a translation matrix

Shearz: Create a shear matrix in z.

Examples:

octave:3> scale(1,2) ans = 1 0 0 0 0 2 0 0 0 0 1 0 0 0 0 1 octave:4> v=[1;2;3;1] v = 1 2 3 1 octave:6> scale(1,2)*v ans = 1 4 3 1 octave:7> rot(pi/2) ans = 1.00000 0.00000 0.00000 0.00000 0.00000 0.00000 -1.00000 0.00000 -0.00000 1.00000 0.00000 0.00000 0.00000 0.00000 0.00000 1.00000 octave:8> rot(pi/2)*v ans = 1 -3 2 1 octave:9> inverse(rot(pi/2)) ans = 1.00000 0.00000 -0.00000 -0.00000 0.00000 0.00000 1.00000 -0.00000 0.00000 -1.00000 0.00000 -0.00000 0.00000 0.00000 0.00000 1.00000 octave:11> T=transl(1); octave:19> S=scale(2); octave:20> S*T*v ans = 4 2 3 1 octave:21> T*S*v ans = 3 2 3 1

#### Attached Files

###
#12
Members - Reputation: **116**

Posted 17 December 2012 - 03:08 PM

I presume the way to invert the perspective section of a matrix is to just negate the values, just the same as negating the translation parts. is this correct?

###
#13
Moderators - Reputation: **9290**

Posted 17 December 2012 - 04:01 PM

You cannot invert the individual sections of a matrix just like that. It just happens to work for translation under very specific circumstances. Likewise, you can invert other sections under other specific conditions.thank you, larspensjo. I was delighted to find octave available for my linux distro. but how about projection/perspective matrices?

I presume the way to invert the perspective section of a matrix is to just negate the values, just the same as negating the translation parts. is this correct?

If you have a matrix consisting of the perspective division component

**only**, then you can invert it by negating the bottom row just like you do with translation. But if you have other components of the projection matrix, and a usual projection matrix does have both translation and non-uniform scaling, then you cannot do that anymore.

But take a look at how OpenGL's matrices are commonly designed here in appendix F. It lists their inverse also.

###
#14
Members - Reputation: **116**

Posted 18 December 2012 - 09:51 AM

But take a look at how OpenGL's matrices are commonly designed here in appendix F. It lists their inverse also.

I tried implementing my own fucntions to generate a matrix based on those equations, as well as an inverse:

Multiplying a matrix together with it's own inverse should result in an identity matrix. I'm getting mostly identity, except col 3 row 4 is always -5.9, and col 4 row 3 is always -1.2. I've tripple-checked that my matrices match the ones in the image.

I think the problem is in my matrix multiplication. I know how to multiply the 3x3 rotation section of the matrix and then add the translation, but what do I do with the perspective section?

Edit: I've been googling and even looked into a few CG programming books. nobody seems to say how to handle the bottom row of a GL 4x4 when multiplying two matrices together...

**Edited by caibbor, 18 December 2012 - 12:19 PM.**

###
#16
Members - Reputation: **116**

Posted 18 December 2012 - 06:07 PM

(all in column-major order)

The projection matrix I created is:

0.074978 0.000000 0.000000 0.000000 0.000000 0.099970 0.000000 0.000000 0.000000 0.000000 -1.000100 -1.000000 0.000000 0.000000 -0.200010 0.000000

It's inverse I calculated is:

13.337285 0.000000 0.000000 0.000000 0.000000 10.002963 0.000000 0.000000 0.000000 0.000000 0.000000 -4.999750 0.000000 0.000000 -1.000000 20001000.000000

an multiplying those matrices together should be identy, but instead I get:

1.000000 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000 -20000994.000000 1.000000

All code related to this is shown below. I've been up and down and all the methods, as far as I can tell, look correct.

[source lang="cpp"]// Mat4f class is a 4x4 matrix of floats.// Mat4f::data is a float[16], and is column-major// returns float& intentionally, method is privateinline float& Mat4f::M( const int col, const int row ) { assert( col < dim && row < dim && col >= 0 && row >= 0); return data[row+col*4];}void Mat4f::Multiply( const Mat4f& mat, Mat4f& out ) const { assert ( &mat != &out ); int r,c; for ( c=0; c<4; c++ ) { for ( r=0; r<4; r++ ) { out.M(c,r) = M(r,0) * mat.M(0,c) + M(r,1) * mat.M(1,c) + M(r,2) * mat.M(2,c) + M(r,3) * mat.M(3,c); } }}// calculation from: http://www.glprogramming.com/red/images/Image23.gifvoid my_glFrustum( float l, float r, float b, float t, float n, float f, Mat4f& out ) { float n2 = n * 2.0f; out.data[0]=n2/(r-l); out.data[1]=0.0f; out.data[2]=0.0f; out.data[3]=0.0f; out.data[4]=0.0f; out.data[5]=n2/(t-b); out.data[6]=0.0f; out.data[7]=0.0f; out.data[8]=(r+l)/(r-l); out.data[9]=(t+b)/(t-b); out.data[10]=-(f+n)/(f-n); out.data[11]=-1.0f; out.data[12]=0.0f; out.data[13]=0.0f; out.data[14]=-(n2*f)/(f-n); out.data[15]=0.0f;}// calculation from: http://www.glprogramming.com/red/images/Image23.gifvoid my_glFrustum_inv( float l, float r, float b, float t, float n, float f, Mat4f& out ) { float n2 = n * 2.0f; out.data[0]=(r-l)/n2; out.data[1]=0.0f; out.data[2]=0.0f; out.data[3]=0.0f; out.data[4]=0.0f; out.data[5]=(t-b)/n2; out.data[6]=0.0f; out.data[7]=0.0f; out.data[8]=0.0f; out.data[9]=0.0f; out.data[10]=0.0f; out.data[11]=-(f-n)/(n2*f); out.data[12]=(r+l)/n2; out.data[13]=(t+b)/n2; out.data[14]=-1.0f; out.data[15]=(f+n)/n2*f;}void my_gluPerspective( float fov, float x,float y,float near,float far, Mat4f& out, bool invert = false );void my_gluPerspective( float fov, float x,float y,float near,float far, Mat4f& out, bool invert ) { // inspired by [url="http://www.cse.unsw.edu.au/~cs3421/labs/nate/frustsrc.html"]http://www.cse.unsw....e/frustsrc.html[/url] float aspect = x / y; float t = 1.0f/tanf(fov * 3.141f / 360.0f); if ( invert ) { my_glFrustum_inv(-t * aspect, t * aspect, -t, t, near, far, out); } else { my_glFrustum(-t * aspect, t * aspect, -t, t, near, far, out); }}inline void my_gluPerspective_inv( float fov, float x,float y,float near,float far, Mat4f& out ) { my_gluPerspective( fov, x, y, near, far, out, true );}int main ( void ) { Mat4f should_be_identity; Mat4f projection; Mat4f projection_inverse; my_gluPerspective(90,800,600,0.1f,2000,projection); my_gluPerspective_inv(90,800,600,0.1f,2000,projection_inverse); projection.Multiply(projection_inverse, should_be_identity); for ( int i=0; i<16; i++ ) printf("%f ", should_be_identity.data[i] ); /* expected output: 1.000000 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000 0.000000 1.000000 actual output: 1.000000 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000 -20000994.000000 1.000000 */ return 0;}[/source]

**Edited by caibbor, 18 December 2012 - 06:09 PM.**