3Д БУМ

3Д принтеры и всё что с ними связано

Building Transformation Matrices

One of the most important uses for matrices in game programming is for transformations. Transformation matrices are used to position geometry and other objects onscreen. There are five main transformation matrices that you will use when working with 3D models. They are rotation around the X-axis, rotation around the Y-axis, rotation around the Z-axis, transla­tion, and scaling. A rotation matrix, whether around the X, Y or Z axes, will rotate all of the affected geometry around that specific axis. A transla­tion matrix is used to move geometry from one place to another, and a scale matrix is used to change the size of the geometry.

The best thing about these matrices is the capability to multiply them together to perform several transformations with a single matrix. Keep in mind that the order of multiplication is important here; multiplying transformation matrices in one order will not necessarily give you the same result as multiplying the same matrices in a different order. Let’s look at an example.

Say you have three rotation matrices. The first is a rotation of 90 degrees around the X-axis, the second -90 degrees around the X-axis, and the third a rotation of 90 degrees around the Y-axis. If you multiply them in

the order of the first times the second, times the third, you will end with a rotation of 90 degrees around the Y-axis because the first two will cancel each other out. However, if you multiply in the order of the first times the third, times the second, your end result will be a positive 90 degree rotation around the Z-axis. Quite a difference.

So, how do you create these transformation matrices? Each rotation matrix will rotate a certain number of degrees around the X, Y, or Z axis. Figure 1.13 shows the X, Y, and Z rotation matrices.

1 0 0

0

cos(0)

0 — sin(0)

0

cos(0)

-sin(0)

0

0 cos(0) — sin(0)

0

0

1 0

0

sin(0)

О

о

0

0 sin(0) cos(0)

0

sin(0)

0 cos(0)

0

0

0

1

0 0 0

1

0

0 0

1

0

0

0

RotX

RotY

RotZ

Figure 1.13 The three rotation matrices. The left matrix

is

for rotation around the X

axis, the center for rotation around the Y axis, the right for rotation around the Z axis. Each matrix will transform an object в degrees around its respective axis. Note that they are placed in 4×4 matrices so they can be multiplied with the other transformation matrices, but they could be stored in a 3×3 matrix as well.

The translation matrix will move an object a set number of units in each direction. The distances in each direction are given by three values stored in the bottom row of the matrix. The scaling matrix is also very simple. Like the translation matrix, there are also three values, one for each axis. Instead of being in the bottom row of the matrix, they are located along the diagonal. The scaling matrix looks a lot like the identity matrix, only with values other than one in the diagonal. Figure 1.14 shows both the translation and the scaling matrices.

Для любых предложений по сайту: [email protected]