Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mathematics for Game Programming and Computer Graphics

You're reading from  Mathematics for Game Programming and Computer Graphics

Product type Book
Published in Nov 2022
Publisher Packt
ISBN-13 9781801077330
Pages 444 pages
Edition 1st Edition
Languages
Author (1):
Penny de Byl Penny de Byl
Profile icon Penny de Byl

Table of Contents (26) Chapters

Preface 1. Part 1 – Essential Tools
2. Chapter 1: Hello Graphics Window: You’re On Your Way 3. Chapter 2: Let’s Start Drawing 4. Chapter 3: Line Plotting Pixel by Pixel 5. Chapter 4: Graphics and Game Engine Components 6. Chapter 5: Let’s Light It Up! 7. Chapter 6: Updating and Drawing the Graphics Environment 8. Chapter 7: Interactions with the Keyboard and Mouse for Dynamic Graphics Programs 9. Part 2 – Essential Trigonometry
10. Chapter 8: Reviewing Our Knowledge of Triangles 11. Chapter 9: Practicing Vector Essentials 12. Chapter 10: Getting Acquainted with Lines, Rays, and Normals 13. Chapter 11: Manipulating the Light and Texture of Triangles 14. Part 3 – Essential Transformations
15. Chapter 12: Mastering Affine Transformations 16. Chapter 13: Understanding the Importance of Matrices 17. Chapter 14: Working with Coordinate Spaces 18. Chapter 15: Navigating the View Space 19. Chapter 16: Rotating with Quaternions 20. Part 4 – Essential Rendering Techniques
21. Chapter 17: Vertex and Fragment Shading 22. Chapter 18: Customizing the Render Pipeline 23. Chapter 19: Rendering Visual Realism Like a Pro 24. Index 25. Other Books You May Enjoy

Understanding the Importance of Matrices

Matrices are an advanced mathematical concept that you will find everywhere throughout computer games and graphics. Their power comes from their ability to store affine transformations and apply them through multiplication. An understanding of their inherent structure and mathematical operations will provide you with a deep appreciation of the methods underpinning all graphics and game engines.

In this chapter, we will cover the following topics:

  • Defining matrices
  • Performing Operations on matrices
  • Creating matrix representations of affine transformations
  • Combining transformation matrices for complex maneuvers

We will begin by defining matrices and working through all the mathematical operations that can be performed on them. Then, we will examine how the affine transformations presented in Chapter 12, Mastering Affine Transformations, can be achieved through matrix operations. This will involve clarifying the benefits...

Technical requirements

In this chapter, we will be using Python, PyCharm, and Pygame, as used in previous chapters.

Before you begin coding, create a new folder in the PyCharm project for the contents of this chapter called Chapter_13.

The solution files for this chapter can be found on GitHub at https://github.com/PacktPublishing/Mathematics-for-Game-Programming-and-Computer-Graphics/tree/main/Chapter13.

Defining matrices

A matrix is an array of numbers. It is defined by the number of rows and columns that define its size. For example, this is a matrix with three rows and two columns:

Each value in the matrix is associated with its location. The value of 3 in the preceding matrix is located in row 0, column 0. More formally, we write the following:

The values specified in the square brackets are in the order [row, column], like so:

Here, the value in row 2, column 1 is 1, and the value in row 1, column 0 is 4.

In pure theoretical mathematics, the row and column values start at 1. We are starting our count at 0 because, in programming, when storing arrays and matrices, the index values start at 0.

Now, let’s take a look at the mathematical operations that can be achieved with two matrices.

Performing operations on matrices

All matrix manipulation occurs via a set of mathematical operations based on addition, subtraction, and multiplication. Because matrix operations use these familiar fundamentals of arithmetic, they are a relatively easy concept to grasp. In this section, you will explore each of these, beginning with addition, subtraction, and multiplication. However, when it comes to division, as you will soon experience, a whole new set of concepts are required. We will cover these toward the end of this section.

Go easy on yourself if you haven’t worked with matrices before and become overwhelmed with the content. They take a lot of practice to become comfortable with and you may not appreciate many of them until you get to apply them in your own graphics projects.

Let’s start with the very familiar and simple addition and subtraction operations.

Adding and subtracting matrices

To add or subtract two matrices, they must be the same size...

Creating matrix representations of affine transformations

In Chapter 12, Mastering Affine Transformations, we examined numerous techniques for repositioning and resizing vertices and meshes. The mathematics involved, except for rotations, was mostly straightforward. For these formulae, we applied straightforward arithmetic and some trigonometry to build up equations. Would it surprise you to know that you can represent these transformations as matrix operations? In this section, I will reveal how this can be achieved.

Moving from linear equations to matrix operations

Let’s remind ourselves of the formulae used for the most popular of the affine transformations – translation, scaling, and rotation. The point, Q, can be translated by adding a translation value, T, to each of its coordinates, resulting in a new point, P:

P(x, y, z) = T(x, y, z) + Q(x, y, z)

We can turn this into a matrix addition operation like so:

If you are thinking that I’ve...

Combining transformation matrices for complex maneuvers

As with the OpenGL order of transformations, which we discussed in Chapter 12, Mastering Affine Transformations, when combining these homogeneous representation matrices to produce compound movements involving translation, scaling, and rotation, the matrices are presented in reverse order. For example, to transform a point by (3, 4, 5), rotate it around the X-axis by 45 degrees, and then scale it by 0.3 in all directions; the matrix multiplication is as follows:

Note how the translation matrix of the first operation is placed on the right and the scaling matrix on the left. To multiply this out, we begin by multiplying the last two matrices (the translation and rotation) to get the following:

Then, we complete the multiplication with the remaining two matrices, which results in the following:

Although learning to calculate these operations by hand is a great skill to have and will help embed your understanding...

Summary

A lot of mathematical concepts were covered in this chapter that focused on matrices. Besides understanding vectors, a solid knowledge of matrices (especially 4 x 4) is an essential skill to have as a graphics programmer since they underpin the majority of the mathematics found in graphics and game engines. Once you appreciate the beauty of their simplicity and power, you’ll become more and more comfortable with their use.

In this chapter, we have only scratched the surface of using matrices in graphics. After learning how the addition operation that’s used in translations can be transformed into a 4 x 4 matrix, and integrated with scaling and rotation to perform compound transformations in 3D, we took a brief look at the ModelView Matrix in OpenGL using the project code created thus far. However, the way we currently perform the transformations is restricted to the same order as how glTranslate(), glRotate(), and glScale() are used in the existing code.

...

Answers

For a great online matrix calculator that will also reveal the working out for you, visit https://matrix.reshish.com/multiplication.php:

Exercise A:

Exercise B:

Exercise C:

Exercise D:

Exercise E:

Exercise F:

Exercise G:

Exercise H:

Exercise I:

lock icon The rest of the chapter is locked
You have been reading a chapter from
Mathematics for Game Programming and Computer Graphics
Published in: Nov 2022 Publisher: Packt ISBN-13: 9781801077330
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}