Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Matrix stuff

  1. #1
    Junior Member Regular Contributor
    Join Date
    May 2004
    Location
    Renkum
    Posts
    193

    Matrix stuff

    Hello,
    Because i'm now into OpenGL shading language programming, I thought it would be a good idea to figure out what actually matrices are.
    I now know how simple translation and scaling works using a identity matrix.
    But my GLSL book also talks about an inverse matrix, transpose matrix and inversetranspose matrix.
    I've figured(thanks to wikipedia :-)) that the transpose of a matrix is that rows are turned into columns(or the other way round).
    And i've also found out that the inverse of a matrix is mymatr^(-1) (equals to 1/myattr ?), although I not yet understand how they calculate the inverse of a matrix.
    But what I really want to know is, what are the use of those matrices?
    Why(and when) do I need them?
    I hope y'all can help me a little with this math stuff.
    Thanks in advance,
    Hylke

  2. #2
    Intern Newbie
    Join Date
    Jan 2005
    Posts
    35

    Re: Matrix stuff

    Hi,

    first, the inverse matrix of a matrix A is denoted A^-1 however, it does not mean 1/A. A^-1 is the matrix such that A*A^-1 = I, where I is the identity matrix. As far as calculating inverse matrices, consult a linear algebra book to learn how to do this.

    The use of an inverse matrix is to 'undo' a transformation. For example in a vertex shader you multply a vertex's world postion by the modelview matrix to transform into clip space, however, at some point you may need a vertex position back in world space, so you would multiply the vertex by the inverse modelview matrix to go back to world space (this was a contrived example so it might seem irrational).

    Under some conditions the transpose of a matrix can be the matrix's inverse.

  3. #3
    Junior Member Regular Contributor
    Join Date
    May 2004
    Location
    Renkum
    Posts
    193

    Re: Matrix stuff

    Hello brtnrdr,
    Thank you for your reply. Now that I know this the formula also makes sense.
    But what I do not fully understand now, is why I would want to use the transpose matrix.
    I hope you can also help me with that.
    Thanks in advance,
    Hylke

  4. #4
    Senior Member OpenGL Pro
    Join Date
    May 2000
    Location
    Naarn, Austria
    Posts
    1,142

    Re: Matrix stuff

    The inverse transpose is needed to transform the normal. It's just simple mathematics:

    In lighting calculations, you use this term:
    n . p (that's a dot product)

    n is the normal, and p is some position (usually light relative to the vertex)

    If you see the column vectors n and p as 4x1 matrices, the dot product can be written as matrix product:

    nT * p (nT is n transposed, a 1x4 matrix)

    Thats a 1x4 matrix * a 4x1 matrix, the result is a 1x1 matrix (also known as number ).

    Now we have the problem that we have the position in view space, that is:

    M * p (that's still a 4x1 matrix)

    M is the modelview matrix, a 4x4 matrix. We are looking for a matrix to multiply with the normal, so the dot product stays the same:

    n . p == (N*n) . (M*p)

    In matrix form:

    (N*n)T * (M*p)

    The matrix we are looking for is the inverse transposed modelview matrix (M^-1)T, because:

    ((M^-1)T * n) . (M * p) ==
    ((M^-1)T * n)T * (M*p) ==
    (nT * (M^-1)TT) * (M*p) == // because (A*B)T == BT * AT
    nT * (M^-1 * M) * p == // because ATT == A
    nT * p ==
    n . p

  5. #5
    Junior Member Regular Contributor
    Join Date
    May 2004
    Location
    Renkum
    Posts
    193

    Re: Matrix stuff

    Ok, I think I understand it, sort of. But I still have a question:
    Originally posted by Overmind:
    The matrix we are looking for is the inverse transposed modelview matrix (M^-1)T, because:

    ((M^-1)T * n) . (M * p) ==
    ((M^-1)T * n)T * (M*p) ==
    (nT * (M^-1)TT) * (M*p) == // because (A*B)T == BT * AT
    nT * (M^-1 * M) * p == // because ATT == A
    nT * p ==
    n . p
    I think I understand why to use the inverse transpose matrix:
    Transpose: So that you can use a matrix style multiplication.
    Inverse: Because normal is not in viewspace.
    But I don't understand what those calculating things have todo with it.

    Why don't I learn such things in school?

  6. #6
    Senior Member OpenGL Pro
    Join Date
    May 2000
    Location
    Naarn, Austria
    Posts
    1,142

    Re: Matrix stuff

    No, I think you didn't understand correctly

    The first line of the equations and the last line are equal. That's the whole point of it.

    You use the inverse transpose modelview matrix because these two lines have to be equal. If you'd use another matrix, they would not be equal (easily shown by repeating my calculations with some other matrix).

    If you transform the whole scene (light position + object), the lighting should not change. And the dot product in the first line of my equations is a part of the lighting calculations.

  7. #7
    Junior Member Regular Contributor
    Join Date
    May 2004
    Location
    Renkum
    Posts
    193

    Re: Matrix stuff

    But if those things are equal, then what's the point of using the matrices?
    For example: 3x*(1/3) is simply x, isn't that the same thing you're big ass calculation boils down to?

  8. #8
    Junior Member Regular Contributor
    Join Date
    Jul 2005
    Location
    Berlin, Germany
    Posts
    188

    Re: Matrix stuff

    The equations that Overmind showed you are not what's happening inside OpenGL. OpenGL simply multiplies each vertex position with the modelview matrix and each vertex normal with the inverse transpose of the modelview matrix.

    What Overmind showed you, was the /proof/, that, when transforming a surface by matrix M, you have to transform the surface normals by (M^-1)T to keep the relation between normals and surface intact.
    355/113 -- Not the famous irrational number PI, but an incredible simulation!

  9. #9
    Senior Member OpenGL Pro
    Join Date
    May 2000
    Location
    Naarn, Austria
    Posts
    1,142

    Re: Matrix stuff

    Yes, what I showed is not something OpenGL will calculate, it's just to show why we need to use the inverse transpose matrix. I wouldn't go as far as calling it a proof...


    3x*(1/3) is simply x, isn't that the same thing you're big ass calculation boils down to?
    Yes. Assume you don't know x, you only know 3x, but you need to calculate x*y. So you just calculate 3x*(1/3)*y.

    With the matrices it's the same. You only have the light position in view coordinates, so the light-vertex vector will be in view coordinates, too... That's (M * p) in my calculations, you don't know p.

  10. #10
    Junior Member Regular Contributor
    Join Date
    May 2004
    Location
    Renkum
    Posts
    193

    Re: Matrix stuff

    Ok, I think I get it now.
    But I have one final question about the formula for creating an inverse (4x4)matrix(in C++).
    According to wikipedia it's:
    A^(-1) = 1/det(a)*adj(a)
    det = Determinant function
    adj = Adjugate function
    So my first step was finding out how the determinant function worked.
    The formula for that was:

    So I now have the following code for the determinant function:
    Code :
    	for(unsigned int i = 0;i < 4;i++)
    	{
    		for(unsigned int j = 0;j < 4;j++)
    		{
    			float matrixsum = 0.0;
    			for(unsigned int k = 0;k < 4;k++)
    			{
    				for(unsigned int l = 0;l < 4;l++)
    				{
    					if(k == i &amp;#0124;&amp;#0124; l == j)
    						continue;
    					matrixsum += mat.data[i][j];
    				}
    			}
    			returnvalue.data[i][j] = mat.data[i][j]*pow(-1, i+j)*matrixsum;
    		}
    	}
    And i've also found the formula for a adjugate matrix:

    So the total formula would look like this:
    Code :
                     (-1)^(i+j)*Mi,j
    INVERSE MATRIXi,j = ------------------
                     Ai,j*(-1)^(i+j)*Mi,j
    which equals to:
    Code :
                               1
    INVERSE MATRIXi,j = ------------------
                             Ai,j
    But that's not correct, because if you translate a mtrix, the x component of the translation shouldn't be 1/Aij but it should be -Aij instead.
    So I'm hoping somebody can help me with this stuff.
    Thanks in advance,
    Hylke

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •