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 11

Thread: Matrix Shenanigans

  1. #1
    Guest

    Matrix Shenanigans

    Ok, so the vertex v' that is passed into the clip matrix is given by...

    v'=PMv

    ...where v is the current vertex, P is the projection matrix and M is the modelview matrix. Correct?

    Now assume that we perform some transformations on the modelview matrix, as we often do, then the transformation becomes:

    v'=P(MAB)v

    ...where A and B are the transformation matrices we multiply by. Now if the modelview matrix originates in identity form, then the transformation is:

    v'=P(IAB)v=P(AB)v

    ...but what if we want to move the transformations A and B into the projection matrix? If we say that they are being applied to the projection matrix, we can say this:

    v'=PMv=P(MAB)v=P(IAB)v=P(AB)v=P(AB)Iv=(PAB)Iv

    [For the last 2 expressions, assume we simply keep the modelview matrix in identity form (hence the new I that appears), now the transformations apply directly to the projection matrix.]

    So instead of:

    Code :
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glDoOtherMatrixStuffARB();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glMultMatrixd(MatrixA);
    glMultMatrixd(MatrixB);
    We do:

    Code :
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glDoOtherMatrixStuffARB();
    glMultMatrixd(MatrixA);
    glMultMatrixd(MatrixB);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    The math seems to work out, but the results are very different! Why is this?

  2. #2
    Advanced Member Frequent Contributor plasmonster's Avatar
    Join Date
    Mar 2004
    Posts
    739

    Re: Matrix Shenanigans

    In what way are the results different?

  3. #3
    Junior Member Regular Contributor
    Join Date
    Apr 2002
    Location
    St. Louis, MO USA
    Posts
    177

    Re: Matrix Shenanigans

    Matrix multiplication is not commutative, meaning that the order the multiplications are applied changes the result.

    look at the difference between PAB and P(AB).

    In the latter, A is multiplied by B, then P is multiplied by that result. In the former, P is multiplied by A and the reult is multiplied by B.

  4. #4
    Guest

    Re: Matrix Shenanigans

    Matrix multiplication is not commutative but I do believe it is associative, which is what you are talking about.

    Commutative: AB = BA
    Associative: ABC = (AB)C = A(BC)

    This is why you can translate and then rotate, or translate and rotate in the same transformation and end up with the same result.

  5. #5
    Junior Member Regular Contributor
    Join Date
    Apr 2002
    Location
    St. Louis, MO USA
    Posts
    177

    Re: Matrix Shenanigans

    I looked that up, and you are correct. In that case, I am not sure why it doesnt work. I will be interested to find out if someone else can explain it.

  6. #6
    Senior Member OpenGL Pro sqrt[-1]'s Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    1,000

    Re: Matrix Shenanigans

    Just a guess (without looking stuff up)
    Try:

    Code :
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMultMatrixd(MatrixA);
    glMultMatrixd(MatrixB);
    glDoOtherMatrixStuffARB();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

  7. #7
    Junior Member Regular Contributor
    Join Date
    Apr 2002
    Location
    St. Louis, MO USA
    Posts
    177

    Re: Matrix Shenanigans

    Doesn't anyone know the answer? I would like to know.

  8. #8
    Member Regular Contributor
    Join Date
    Aug 2003
    Posts
    370

    Re: Matrix Shenanigans

    I remember in some previous posts that doing stuff in the projection matrix that are normally intended for the modelview matrix messes up some lighting and fog calculations. Don't know if this is correct though.

  9. #9
    Junior Member Newbie
    Join Date
    Dec 2003
    Posts
    3

    Re: Matrix Shenanigans

    See "8.030 Where should my camera go, the ModelView or projection matrix?" of http://www.opengl.org/resources/faq/technical/index.html.
    Lighting calculation of P(AB) is different from that of (PAB)I. I think this is a reason that the scene result is different.

  10. #10
    Guest

    Re: Matrix Shenanigans

    The lighting was not different. What was different was that objects were appearing completely different sizes.

    I replaced the modelview matrix with a matrix that gave me billboard effects, but I thought that would better go on the projection matrix, and I just wanted to see if it would work for kicks.

    So I replaced the modelview matrix by the identity matrix and simply multiplied the projection matrix by the billboard matrix, that WOULD have been the modelview matrix.

    The rendered objects were fine if I did it with the modelview matrix, but when I did the projection matrix trick they came out much smaller and would shrink excessively with depth.

    I've moved on from that problem though. If my billboard effects require the modelview matrix, who cares, I do my own lighting anyway.

Posting Permissions

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