Matrix multiplication question

Started by
2 comments, last by Nanoha 8 years, 2 months ago
Hi guys,

From what I understand the correct multiplication order to get a WVP matrix is;

Projection * View * World

To multiply this against the identity matrix do you multiply that last? Like;

Projection * View * World * Identity

Or does it not matter?

Thanks in advance (a matrix math newbie) smile.png
Advertisement

From what I understand the correct multiplication order to get a WVP matrix is;

Depends on which mathematical conventions you're using.

If you're using column-major mathematical conventions, your matrices look like below and you use: Projection * View * World
$$\begin{bmatrix} Xx & Yx & Zx & Tx\\ Xy & Yy & Zy & Ty\\ Xz & Yz & Zz & Tz\\ 0 & 0 & 0 & 1 \end{bmatrix}$$

If you're using row-major mathematical conventions, your matrices look like below and you use: World * View * Projection
$$\begin{bmatrix} Xx & Xy & Xz & 0\\ Yx & Yy & Yz & 0\\ Zx & Zy & Zz & 0\\ Tx & Ty & Tz & 1 \end{bmatrix}$$

n.b. this is completely unrelated to whether you're using row-major array storage or column-major array storage.
e.g. Bullet uses column-major mathematical conventions (the top kind of matrix), but stores the values in row-major arrays. That's just an implementation detail, which should have no impact on your math.

note#2 - you'll find lots of crap on the internet saying "D3D uses row major, GL uses column major!!" but it's not true any more (ever since fixed-function graphics was replaced by shaders).

Your mathematical convention is dictated only by how you write your shader code (and how your matrix library has been written to populate translation/rotation/projection matrices...) -- e.g. do you write W*V*P or P*V*W in your shaders, and does your library fill in the values to look like the top matrix or the one below it...

Your array storage convention is controlled by keywords in GLSL/HLSL -- e.g. you can write "column_major matrix4x4 fooBar;" to choose column-major array indexing in HLSL.

To multiply this against the identity matrix do you multiply that last?

Multiplying against identity is the same as multiplying a regular number by 1 -- it does nothing, ever.
Identity * World * Identity * View * Identity * Projection * Identity == World * View * Projection

Hodgman, thanks for your reply.

I am now trying to understand what part the identity matrix plays then.

I often read that you need to set the identity matrix to prevent transforms, rotations, etc taking effect on the next model you are rendering.

Is this purely because of optimisations? So that you can re-use the same matrix assuming you want two objects to have the same transforms applied?

Hodgman, thanks for your reply.

I am now trying to understand what part the identity matrix plays then.

I often read that you need to set the identity matrix to prevent transforms, rotations, etc taking effect on the next model you are rendering.

Is this purely because of optimisations? So that you can re-use the same matrix assuming you want two objects to have the same transforms applied?

Identity usually used like a default value. Imagine you had a 'scale' value, one model might have scale of 2, the next model might not specify the value and so you need to use the default value. What is an appropriate default? In that case 1. If you used a default of 0 then it would flatten the model to nothing. The same holds true with matrix transforms, if you use a zero matrix then it will flatten the model to nothing (all vertices will end up being zero) and so you need to choose a sensible default that does nothing (0 has side effects), in this case that default is the identity matrix. It has other uses from an algebraic point of view: matrix*matrix_inverse=identity but as far as how you will be using them they are just the best choice of default values. '1' is the identity for real numbers.

There is never any need to multiply by an identity matrix for the sake of multiplying by an identity matrix, though if one of your transforms is an identity matrix then multiplying by it won't hurt. That's perhaps the most useful part about it, multiplying by a zero matrix WILL hurt.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

This topic is closed to new replies.

Advertisement