# Thread: Matrix Shenanigans

1. ## 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.]

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

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

2. ## Re: Matrix Shenanigans

In what way are the results different?

3. ## 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. ## 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. ## 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. ## Re: Matrix Shenanigans

Just a guess (without looking stuff up)
Try:

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

7. ## Re: Matrix Shenanigans

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

8. ## 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. ## 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. ## 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
•