• Home
  • Textbooks
  • Mathematics for Game Programming and Computer Graphics: Explore the essential mathematics for creating, rendering, and manipulating 3D virtual environments
  • Understanding the Importance of Matrices

Mathematics for Game Programming and Computer Graphics: Explore the essential mathematics for creating, rendering, and manipulating 3D virtual environments

Penny de Byl

Chapter 13

Understanding the Importance of Matrices - all with Video Answers

Educators


Section 1

Combining transformation matrices for complex maneuvers

Problem 1

Create a new Python folder called Chapter_13 and copy the contents from Chapter_12 into it.

Check back soon!

Problem 2

Make a copy of ExploreNormals.py and call it TransformationMatrices.py.

Check back soon!

Problem 3

Modify the code in TransformationMatrices.py to draw just a single textured cube positioned at the origin:
import math
from Object import *
from pygame.locals import *
from OpenGL.GLU import *
from Cube import *
In the first part of the code, notice the reduction in the number of required libraries.
In the second part of the code, ensure you have only one cube being drawn, as follows:
.
done $=\mathrm{False}$
white $=$ pygame. $\operatorname{Color}(255,255,255)$
objects_3d = []
objects_2d = []
cube = Object(“Cube”)
cube.add_component(Transform((0, 0, 0)))
cube.add_component(Cube(GL_POLYGON,
“images/wall.tif”))
objects_3d.append(cube)
clock = pygame.time.Clock()
fps = 30
..

Check back soon!

Problem 4

In object.py, re-instantiate the scaling, rotation, and translation lines if you commented them out previously:
def update (self, events = None):
glPushMatrix ()
for $c$ in self.components:
if isinstance (c, Transform):
pos = c.get_position ()
scale $=$ c.get_scale ()
rot_angle = c.get_rotation_angle ( )
rot_axis = c.get_rotation_axis()
glTranslatef(pos.x, pos.y, pos.z)
glRotated(rot_angle, rot_axis.x,
rot_axis.y, rot_axis.z)
glscalef(scale.x, scale.y, scale.z)
elif isinstance (c, Mesh3D) :
glColor (1, 1, 1)

Check back soon!

Problem 5

Back in TransformationMatrix.py, add the following transformations to the cube:
.
cube $=$ object ( "Cube")
cube.add_component (Transform $((0,0,0)))$
cube.add_component (Cube (GL_POLYGON,
"images/wall.tif"))
trans: Transform = cube.get_component (Transform)
trans.set_position $((0,0,-3))$
trans.set_rotation_axis (pygame.Vector3 $(1,0,0)$ )
trans.update_rotation_angle (45)
trans.set_scale (pygame.Vector3 $(0.5,2,1)$ )
objects_3d.append (cube)
clock $=$ pygame.time. $\operatorname{clock}()$
. .

Check back soon!

Problem 6

Take a look at Object. py and look at the order of execution of the translation, rotation, and scaling:
glTranslatef(pos.x, pos.y, pos.z)
glRotated(rot_angle, rot_axis.x,
rot_axis.y, rot_axis.z)
glscalef(scale.x, scale.y, scale.z)

Check back soon!
View

Problem 7

You can calculate the result of the multiplication given in step 6 manually or use the handy online Matrix Multiplication Calculator tool available at https://matrix.reshish. com/multCalculation.php.

Nicole Hoffman
Nicole Hoffman
Numerade Educator
View

Problem 8

To validate this calculation, we can also ask OpenGL what it has stored in our program. The internal OpenGL matrix that holds all the transformation multiplications is called the ModelView Matrix. We can obtain its value by adding the following code to object.py after the transformations have been applied:
rot_axis $=$ c.get_rotation_axis ()
glTranslatef(pos.x, pos.y, pos.z)
glRotated(rot_angle, rot_axis.x, rot_axis.y,
rot_axis.z)
glscalef(scale.x, scale.y, scale.z)
$m v=$ glGetDoublev(GL_MODELVIEW_MATRIX)
print ("MV: ")
print (mv)
elif isinstance (c, Mesh3D):

Victor Salazar
Victor Salazar
Numerade Educator