Entering the matrix

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:

Entering the matrix

Vectors

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:

  • The value 1 means that this vector is a position
  • The value 0 means that this vector is a velocity

Here's an example:

  • The vector, (1, 5, 10, 1), represents a point at x = 1, y = 5, and z = 10 in a 3D coordinate system.
  • The vector, (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 direction

Tip

Notice that vectors can be represented as a series of numbers inside of a parenthesis. This is much easier than having to draw a table every time you need to write down a vector!

Combining vectors

The 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:

Combining vectors

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).

Tip

Remember: the last 1 in (1, 5, 6, 1) and (2, 1, 0, 1) is the w value that simply tells us we are working with a position. Notice that the w value remained 1 in the final result as well!

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:

  • (1 * 2) + (0 * 1) + (0 * 0) + (1 * 1) = 3
  • (0 * 2) + (1 * 1) + (0 * 0) + (5 * 1) = 6
  • (0 * 2) + (0 * 1) + (1 * 0) + (6 * 1) = 6
  • (0 * 2) + (0 * 1) + (0 * 0) + (1 * 1) = 1

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:

  • Moving
  • Scaling
  • Rotating

Tip

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!

Identity matrix

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.8.147