Correct order to multiply matrices in 2D space
What is the correct order to multiply matrices in 2D space??
When I multiply:
m = projection * translate * scale;
And finally in the vertex shader
gl_Position = m * pos;
Works perfectly.
But when I multiply:
m = projection * scale * translate;
And in the vertex shader:
gl_Position = m * pos;
Nothing is draw in the screen.
In this case scale is a matrix to invert the y axis and translate is a matrix to move a square on the screen.
Thanks advance.
Regards.
The order in which translate and scale is done is swapped when you swap the multiplication order.
So the first one first inverts Y, then moves the square, whereas the second first moves the square, and then flips the already moved square around Y.
I guess in your case, Y = 0 is at the edge of the screen, and if you first flip the square and then move it into the screen it will be visible.
If you instead move the square into the screen, and then afterwards flip it around Y, then it will be flipped with its translation so the final position will be in the opposite direction outside the screen.
It's always better to scale the model first (i.e. at the end), if you do it after a translation or a rotation you would have some unexpected results. The order, in 2D or 3D, should be projection * translate * rotate * scale always if you wan't to keep everything in proportion.
If you are using the convention that vectors are columns, you can think of what (translate * rotate * scale) does like this:
(translate * rotate * scale) * point = translate * (rotate * (scale * point))
Because matrix multiplication is associative those things are equal, but it's easier to think about the latter (it's also faster to execute). So you start with a point in model coordinates, scale it, rotate the result and finally translate it.
Notice that matrix multiplication is not commutative in general, but rotation and [uniform] scaling do commute.