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

Updating and Drawing the Graphics Environment

All objects in a graphics environment undergo a cyclical process of being updated and then drawn. This occurs in the main game loop and is synchronized by a frame rate clock. Building this ability into our graphics engine and application at this point is critical for further functionality, such as physics and other interactivity, to be added down the line. Adding a strong foundation to facilitate this early on is crucial in order to succeed in graphics processing and rendering down the line.

Herein, we will examine the purpose of the main game loop and add a clock to regulate the frame rate in our project. Coordinated with this loop are updates to objects so that they can become whatever we need them to be, from audio sources to game characters. Therefore, we will also be concentrating our efforts on developing an object’s abilities.

To this end, in this chapter, we will be examining the following:

  • Introducing the...

Technical requirements

In this chapter, we will be using Python, PyCharm, and Pygame as in the previous chapters. Before you begin coding, create a new folder called Chapter 6 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/Chapter06 in the Chapter06 folder.

Introducing the main game loop

To keep any windowed application alive, the program must instigate an endless loop. We created one in Chapter 1, Hello Graphics Window: You’re On Your Way, to keep the graphics window open in the first exercise through the implementation of a while loop. Since then, we’ve continued to use this design pattern in every application. This loop in a game is called the main game loop. Each loop produces one frame of graphics. In Pygame, this frame is pushed to the screen using the pygame.display.update() or pygame.display.flip() methods.

Thus far, our graphics applications have been very simple with very little happening within the loop – however, in a full-fledged game engine, the loop is quite complex and contains multiple significant events, as illustrated in Figure 6.1:

Figure 6.1: The main game loop and associated processes

After the application begins running, various items are initialized and loaded,...

Updating and drawing objects

Objects in a game or graphics application are entities within the system that possess numerous properties and methods that allow them to exist and interact within their environment.

As shown in Figure 6.2, a typical object possesses many subparts or components:

Figure 6.2: A game object and a few of its components

You can think of components as functionality added to the object, for without the components, the object is nothing except a placeholder. The object may have all or only a subset of the components, as illustrated in Figure 6.2. For example, you might have an object in the environment that only plays a sound. For this, it would only need an audio component, whereas an object representing a game character might have a mesh that defines what it looks like, a render component that tells the game engine how to draw it on the screen, and a transform component that stores its location and orientation in the world.

In...

Measuring time

Time is used in graphics to control animations – these are any moving objects or changing images. It’s also useful for determining the frame rate at which your application is running. There are numerous useful functions for this in the Pygame API, and you can read the details here: https://www.pygame.org/docs/ref/time.html. The frame rate is expressed in frames per second (FPS), and this is an important measurement as it specifies how fast the main loop takes to execute. It’s important to keep the loop running at the same rate for the entire application to ensure smooth performance and animations. If the loop changes speed, then the experience becomes jittery.

Currently, in our code, the frame rate will be as fast as your computer can run it. However, it might not be consistent, as your operating system processes other things in the background. To see just how fast your computer is running the main loop, try removing the following from the code...

Summary

In this chapter, the fundamental elements of a graphics rendering engine were explored. It was a whirlwind introduction to numerous concepts that will be thrashed out in later chapters but was essential to gaining an overview of how to move our coding structure forward.

We began by looking at the complexities of the main game loop and the many processes an object moves through to be updated and drawn. Using this knowledge, we then added component functionality to the objects in our project to ready them for the expansion of the engine we are developing. In addition, we also explored how time is an essential controlling component of the main loop and added this practicality to the code base. There was more programming in this chapter than mathematics, but again, a lot of it was laying down a good base on which to build our mathematics functionality.

In the next chapter, we will add functionality to our application that will cater to user input through the integration...

Answers

Exercise A:

glEnable(GL_DEPTH_TEST)
objects = []
cube = Object(“Cube”)
cube.add_component(Transform((0, 0, -1)))
cube.add_component(Cube(GL_POLYGON, “images/wall.tif”))
cube2 = Object(“Cube2”)
cube2.add_component(Transform((0, 1, 0)))
cube2.add_component(Cube(GL_POLYGON, “images/brick.tif”))
objects.append(cube)
objects.append(cube2)
glEnable(GL_DEPTH_TEST)
..
..
while not done:
   ..
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
   glRotatef(5, 1, 0, 1)
   for o in objects:
       o.update()
   pygame.display.flip()
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}