Reader small image

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

Product typeBook
Published inNov 2022
PublisherPackt
ISBN-139781801077330
Edition1st Edition
Tools
Right arrow
Author (1)
Penny de Byl
Penny de Byl
author image
Penny de Byl

Penny de Byl is a full stack developer with an honors in graphics and Ph.D. in artificial intelligence for games. She has a passion for teaching, teaching games development and computer graphics for over 25 years in universities in Australia and Europe. Her best-selling textbooks, including Holistic Game Development with Unity, are used in over 100 institutions. She has won numerous awards for teaching, including an Australian Government Excellence in Teaching Award and the Unity Mobile Game Curriculum Competition. Her approach to teaching computer science and related fields is project-based giving you hands-on workshops you can immediately get your teeth into. The full range of her teaching interests can be found at H3D Learn.
Read more about Penny de Byl

Right arrow

Working with Coordinate Spaces

Understanding the different coordinate spaces used in graphics rendering is a critical and transferable skill that you as a programmer require. These are a universal concept across all graphics and game engines, and being able to apply them to manipulate a virtual scene is a skill you’ll never regret acquiring.

These key matrices form the OpenGL matrix stack that defines all mathematical operations. Mathematical operations are required to take the vertices of a model from their own local coordinate system into a pixel on the computer screen. They define not only where individual objects are in a scene and how they are scaled and rotated, but also allow for the creation of a virtual camera. This camera can be moved and orientated to influence the location and orientation from which a scene is viewed.

The modelview, view, and projection matrices contain indispensable mathematical functions for any graphics engine. You’ve been exploring...

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 Chapter_14 folder in the PyCharm project for the contents of this chapter.

The solution files containing the code can be found on GitHub at https://github.com/PacktPublishing/Mathematics-for-Game-Programming-and-Computer-Graphics/tree/main/Chapter14.

Understanding OpenGL’s Matrix Stack

In graphics, the current transformation applied to a point or object is determined by the current model-view-projection (MVP) matrix. This is a culmination of the model matrix, the view matrix, and the projection matrix. We first discussed these matrices as coordinate spaces in Chapter 4, Graphics and Game Engine Components. Each one has a specific use in the graphics pipeline, as is shown in Figure 14.1 (this diagram has been reinserted here from Chapter 4, Graphics and Game Engine Components, for your convenience):

Figure 14.1: The graphics pipeline

The coordinates or points that define a graphics object are stored in a model’s local coordinate system. They define the geometry of the object independently of where it is situated in world space. As we saw in Chapter 4, Graphics and Game Engine Components, a cube can be defined by six points, one for each of the vertices, thus:

cube.vertices = [(0.5, -0.5...

Working with the Model Matrix

The model matrix is the accumulation of the multiplications of the transformation matrices that are to be applied to a point or vector. As we discovered in Chapter 13, Understanding the Importance of Matrices, the order in which the transformations are multiplied is important to the final outcome. Also, at the end of the same chapter, you discovered that in OpenGL, you can obtain the contents of the modelview matrix with the following code:

glGetDoublev(GL_MODELVIEW_MATRIX)

Rather than using OpenGL’s own methods for moving, resizing, and orienting an object, you can set the matrix manually, and then perform matrix multiplication to apply the transformation to a model as long as you keep in mind the format of the modelview matrix.

These transformations were performed in Chapter 13, Understanding the Importance of Matrices:

glTranslatef(0, 0, -3)
glRotated(45, 1, 0, 0)
glScalef(0.5, 2, 1)

The result of performing these transformations...

Working with the View Matrix

The view matrix takes the world space coordinates produced by the modelview matrix and transforms them into the camera or eye space. The eye space assumes the origin of this space to be at the position of the camera or viewer’s eye. As we saw in Chapter 4, Graphics and Game Engine Components, the eye space can either be a frustum (a pyramid with the top cut off for perspective views) or orthogonal (a rectangular prism for parallel views). The view matrix can take translation and rotations like the modelview matrix, but instead of transforming individual objects, it transforms everything in the world. That’s because it’s basically the equivalent of moving a camera around in the world, and then determining what the world will look like through that camera.

Unlike the modelview matrix, which can be loaded after an OpenGL call to glGetDoublev( GL_MODELVIEW_MATRIX), the view matrix sets a special matrix mode. The view matrix is multiplied...

Working with the Projection Matrix

The objective of the projection matrix is to take objects from view space into projection space. Basically, this takes coordinates inside the camera’s viewing volume (specified by the shape and size of the frustum or rectangular prism) and puts them in normalized device coordinates (NDCs). The calculations for a perspective view’s projection matrix were explained in Chapter 4, Graphics and Game Engine Components. The OpenGL documentation also specifies how it constructs the projection matrix with a call to gluPerspective() at registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml where the mathematics is revealed. The projection matrix is defined as follows:

Here, the values of a, b, c, and d are calculated from the parameters passed to gluPerspective(), which are the vertical field of view (fovy), the aspect ratio of the window, the near plane, and the far plane. These values are determined thus:

f = cotangent...

Summary

In this chapter, we have explored the principal modelview, view, and projection matrices that comprise the OpenGL matrix stack. As discussed, these matrices are key to understanding how model coordinates are transformed into screen positions for the rendering of objects. No matter which graphics engine you work with, these matrices are always present. As you move into vertex/fragment shading, it is key that you understand the role each of these matrices plays, and the order they are applied to model coordinates.

In the next chapter, we will work further to improve the maneuverability of the camera that we created herein by adding advanced movement and rotational abilities. This will allow you to fly the camera through the scene and orient it for viewing from any angle.

Answers

Exercise A.

def get_position(self):
    position = pygame.Vector3(self.MVM[0, 3], 
                              self.MVM[1, 3], 
                              self.MVM[2, 3])
    return position
def get_scale(self):
    sx = pygame.Vector3(self.MVM[0, 0], self.MVM[1, 0], 
                        self.MVM[2, 0])
    sy = pygame.Vector3(self.MVM[0, 1], self.MVM[1, 1], 
                        self.MVM[2, 1])
    sz = pygame.Vector3(self.MVM[0, 2], self.MVM[1, 2], 
                        self.MVM[2, 2])
    return pygame.Vector3(sx.magnitude(), sy.magnitude(), 
                          sz.magnitude())
def get_rotation(self):
    scale = self.get_scale()
    rotation = np.identity(4)
    rotation[0, 0] = self.MVM[0, 0] / scale.x
    rotation[0, 1] = self.MVM[0, 1] / scale.x
    rotation[0, 2] = self.MVM[0, 2] / scale.x
    rotation[1, 0] = self.MVM[1, 0] / scale.y
    rotation[1, 1] = self.MVM[1, 1] / scale.y
    rotation[1, 2] = self.MVM[1, 2] / scale.y
    rotation[2, 0...
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 2022Publisher: PacktISBN-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.
undefined
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

Author (1)

author image
Penny de Byl

Penny de Byl is a full stack developer with an honors in graphics and Ph.D. in artificial intelligence for games. She has a passion for teaching, teaching games development and computer graphics for over 25 years in universities in Australia and Europe. Her best-selling textbooks, including Holistic Game Development with Unity, are used in over 100 institutions. She has won numerous awards for teaching, including an Australian Government Excellence in Teaching Award and the Unity Mobile Game Curriculum Competition. Her approach to teaching computer science and related fields is project-based giving you hands-on workshops you can immediately get your teeth into. The full range of her teaching interests can be found at H3D Learn.
Read more about Penny de Byl