Matrices and Matrix Operations
Matrices are scary-looking structures, but once you understand them, they are incredibly powerful. They are used extensively in 3D graphics programming and are unavoidable. So let’s dive into matrices and take advantage of their usefulness.
The main type of matrix that you will deal with in 3D graphics programming is a 4×4 matrix. One use of this matrix is known as a transform matrix. The transform matrix is used to transform points and vectors from one shape and space to another. There are a few moving parts associated with this process, which are detailed throughout this section.
The foundation of working with matrices rests on the dot product, covered earlier in this chapter. The dot product is concerned with figuring out how much weight is given to like coordinate spaces. Think about an elevator. The elevator goes up and down, but it doesn’t matter how fast or high the elevator goes because it doesn’t move from side to side. The speed and height of the elevator will never affect its movement back and forth or side to side because the speed and height of the elevator are constrained to the y-axis and never bleed over into the x- or z-axes. Matrices are set up with null values in places where the coordinate is not impacted by changes happening in other coordinate spaces. With that in mind, let’s talk about how this applies to 3D graphics programming.
Identity Matrix
The “Hello, World” for matrices is the identity matrix. The identity matrix is set up so that if you perform the dot product on a coordinate with the identity matrix, you get the same coordinate. To understand how the matrix affects a vector, it’s important to see an example of one that has no side effects. That way you can see how the nulls affect the matrix and where you need to add values in order to change only what you want to change. This means that the identity matrix has a diagonal line of 1s from the upper-left coordinate to the lower-right coordinate. Everything else is a 0.
[ 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 ]
This is your default starting point because it doesn’t change or affect anything. Why not give it a try with our (3, 4, 0) coordinate?
[ 3 4 0 1 ]
If you perform a matrix operation on this point using the identity matrix, you get the following:
C1 = (3 * 1) + (4 * 0) + (0 * 0) + (1 * 0) = 3 C2 = (3 * 0) + (4 * 1) + (0 * 0) + (1 * 0) = 4 C3 = (3 * 0) + (4 * 0) + (0 * 1) + (1 * 0) = 0 C4 = (3 * 0) + (4 * 0) + (0 * 0) + (1 * 1) = 1
You will modify the identity matrix to perform certain tasks, but it’s important to know what you need to do to get the same result out as you put in so you can isolate the side effects that occur on each coordinate.