Now for the topic that strikes fear into the heart of all new game programmers: matrices. Matrices are a mathematical device (part of linear algebra) that makes it easier to work with large sets of related numbers.
In its simplest form, a matrix is a table of numbers. Let's say that I wanted to represent a coordinate in space. I could write its value down as follows:
A single row or single column of a matrix is called a vector. Vectors are important because they can be used to both position things and move things.
The typical matrix used in games contains four values: x, y, z, and w. These x, y, and z components typically refer to a position in the 3D coordinate system, while the w is a switch:
Here's an example:
1, 5, 10, 1
), represents a point at x = 1
, y = 5
, and z = 10
in a 3D coordinate system.1, 5, 10, 0
), is a point that moves 1 unit in the x direction, 5 units in the y direction, and 10 units in the z directionThe real power of vectors comes when they are combined. The most common way to combine vectors is to multiply them. Look at the following example:
The matrix on the left is known as a translation matrix because when you multiply it by a positional vector, the result will be a new position (moving things in a 3D space is known as translation). In this case, the point at (2, 1, 0) has been translated to a new position at (3, 6, 6).
If you are paying attention, you must be wondering how we got the third matrix! It turns out that multiplying two matrices is actually more complex that it seems. In order to multiply the two matrices shown earlier, the following operations had to occur:
Each cell in each row of the first matrix is multiplied by each cell in each column of the second matrix.
This might seem like a lot of trouble just to move a point, but when it comes to quickly moving 3D objects around in a game, matrix math is much faster than other techniques.
Don't worry! This is about all we are going to say about matrices and vectors. You should know that OpenGL uses matrices to calculate transformations, including:
If you ever work with both OpenGL and DirectX, you will need to be aware that there is a difference in the way they handle matrices. OpenGL uses a row major order, while DirectX users a column major order. In a row major matrix, all of the cells in the first column are adjacent, followed by all of the cells in the next row, and so forth. In a column major matrix, all of the cells in the first column are adjacent, followed by all of the cells in the next column, and so forth. This makes a huge difference in how you manipulate and calculate the matrices!
I will mention one more special matrix:
1 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
The preceding matrix is known as an identity matrix. If you multiply any matrix by an identity matrix, the result is the original matrix (just like multiplying any number by 1 results in the original number). Whenever we want to initialize a matrix, we set it to an identity matrix.
There are special matrices in OpenGL, and you will be introduced to some of them in the next code.
18.218.8.147