Книга: DirectX 8 Programming Tutorial

What are matrices and how do they work?

What are matrices and how do they work?

Well, matrices are quite an advanced maths topic so I'll try my best to explain the basics. A matrix is a grid of numbers that we can use in 3D graphics to modify the position of vertices (points in 3D space). Some of the main uses for matrices in 3D graphics are to rotate, scale and translate (move) vertices. So, what does a matrix look like? In DirectX, a matrix is a 4x4 grid of numbers; Fig 5.1 below shows an example matrix that will scale a vertex by five units:


Fig 5.1

OK, so how can a matrix change the position values of a vertex? To modify the x, y and z values of a vertex you need to multiply them by the matrix. Fig 5.2 below shows an example of multiplying a vertex by a matrix:


Fig 5.2

This is a pretty simple calculation, all you need to do is multiply each of the x, y and z values by the columns one by one. Each column will give you one of the new values for the vertex. You may notice that in the "current vertex" in our example, we have added a 1 after the z value. This is simply to balance the vertex against the matrix. You need to have the same number of values, as there are columns for the transformation to work correctly.

So, all you need to rotate, scale or translate (move) a vertex is the correct matrix. Luckily for you, DirectX has a number of functions for generating these common matrices. What if you want to scale AND rotate some vertices? Well, first you need to have two matrices, one for the rotation and one for the scale. Then you need to multiply the two matrices (for scale and rotate) together to form one new matrix that scales and rotates. This new matrix is then applied to the vertices. You must be careful to have the matrices in the right order. MatrixA X MatrixB will give you a different answer than MatrixB X MatrixA. Fig 5.3 shows how to multiply two matrices together:


Fig 5.3

To multiply two matrices together you need to multiply each row in the first matrix with each column in the second. In the example above, we have multiplied the first row (in the first matrix) by each column (in the second matrix). These four calculations will give us the top row of our answer matrix. To calculate the other three rows, simply multiply the second, third and forth rows in the first matrix by each column in the second. As I said above, DirectX has a built in function for multiplying two matrices, so don't worry too much about this!

Оглавление книги


Генерация: 1.553. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз