How can I get the new position of my vertex given a 4x4 transform matrix or simply 9 floats (positionX, positionY, positionZ, rotationH, rotationP, rotationR, scaleX, scaleY, scaleZ)? Usually I can assign the matrix to my final mesh (or "node", or "object", or "geom"), but right now I need to do it manually.
In order to apply a general 4x4 transformation matrix to a vertex represented as a 3 dimensional vector, you need to:
Expand the vector into the 4th dimension by adding a 1 as the w component:
(x, y, z) => (x, y, z, 1)
Multiply the transformation matrix by the 4 dimensional vector above. The result will be another 4 dimensional vector:
( 4x4 matrix ) * (x, y, z, 1) => (tx, ty, tz, w)
The general formula for multiplying a 4x4 matrix by a 4x1 vector is (if I didn't mess up):
[ m11 m12 m13 m14 ][ x ] [ m11 * x + m12 * y + m13 * z + m14 * w ] [ m21 m22 m23 m24 ][ y ] [ m21 * x + m22 * y + m23 * z + m24 * w ] [ m31 m32 m33 m34 ][ z ] = [ m31 * x + m32 * y + m33 * z + m34 * w ] [ m41 m42 m43 m44 ][ w ] [ m41 * x + m42 * y + m43 * z + m44 * w ]
Or if you consider the usual configuration of a transformation matrix:
[ m11 m12 m13 px ][ x ] [ m11 * x + m12 * y + m13 * z + px ] [ m21 m22 m23 py ][ y ] [ m21 * x + m22 * y + m23 * z + py ] [ m31 m32 m33 pz ][ z ] = [ m31 * x + m32 * y + m33 * z + pz ] [ 0 0 0 1 ][ 1 ] [ 1 ]
Convert it back into the 3rd dimension by homogenizing the vector, i.e. dividing everything by the fourth component
(tx, ty, tz, w) => (tx/w, ty/w, tz/w)
The thing is that if your transformation matrix only does a simple translation / rotation / scale, the value of
wwill be 1 and you can just drop the fourth component since that will be the same as dividing by 1.
But it's good to remember that dropping w component does not work for every case, e.g. with projection matrices you must remember to do this third step.
[ rxx rxy rxz px ] [ vx ] [ vx' ] [ ryx ryy ryz py ] [ vy ] [ vy' ] [ ryx ryy ryz pz ] [ vz ] = [ vz' ] [ 0 0 0 1 ] [ 1 ] [ 1 ]
See the page on wikipedia for more