Mathematics for Game Programming and Computer Graphics - Penny de Byl - E-Book

Mathematics for Game Programming and Computer Graphics E-Book

Penny de Byl

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

Mathematics is an essential skill when it comes to graphics and game development, particularly if you want to understand the generation of real-time computer graphics and the manipulation of objects and environments in a detailed way. Python, together with Pygame and PyOpenGL, provides you with the opportunity to explore these features under the hood, revealing how computers generate and manipulate 3D environments.
Mathematics for Game Programming and Computer Graphics is an exhaustive guide to getting “back to the basics” of mathematics, using a series of problem-based, practical exercises to explore ideas around drawing graphic lines and shapes, applying vectors and vertices, constructing and rendering meshes, and working with vertex shaders. By leveraging Python, Pygame, and PyOpenGL, you’ll be able to create your own mathematics-based engine and API that will be used throughout to build applications.
By the end of this graphics focussed book, you’ll have gained a thorough understanding of how essential mathematics is for creating, rendering, and manipulating 3D virtual environments and know the secrets behind today’s top graphics and game engines.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 469

Veröffentlichungsjahr: 2022

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Mathematics for Game Programming and Computer Graphics

Explore the essential mathematics for creating, rendering, and manipulating 3D virtual environments

Penny de Byl

BIRMINGHAM—MUMBAI

Mathematics for Game Programming and Computer Graphics

Copyright © 2022 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Rohit Rajkumar

Publishing Product Manager: Nitin Nainani

Senior Editor: Hayden Edwards

Senior Content Development Editor: Rashi Dubey

Technical Editor: Saurabh Kadave

Copy Editor: Safis Editing

Project Coordinator: Sonam Pandey

Proofreader: Safis Editing

Indexer: Hemangini Bari

Production Designer: Roshan Kawale

Marketing Coordinator: Teny Thomas

First published: November 2022

Production reference: 1041122

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-80107-733-0

www.packt.com

Life-long learning has always been my goal. But I couldn’t have done it alone. I’d like to thank Adrian who has been my friend, technical reviewer, and code tester throughout. He possesses the attention to detail I lack. And, I might add, if the code doesn’t work – it’s his fault. Next, I’d like to thank my wonderful daughter, Tabytha, who is in her first year of studying theoretical mathematics and computer science at university. She’s been a great sounding board in helping me understand mathematical notations that I still struggle with. A mother could not be prouder than I am of her.

– Penny de Byl

Contributors

About the author

Penny de Byl is a full-stack developer with an honors degree in graphics and a PhD in artificial intelligence for games. She has a passion for teaching, and has been teaching games development and computer graphics for over 25 years at 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 h3dlearn.com.

I’d like to thank the wonderful publishing crew and editors at Packt Publishing, who’ve been attentive, patient, highly professional, and available throughout the writing process.

About the reviewers

Amit Verma is a game developer and WebGL graphics engineer from Dehradun, Uttarakhand, who has developed web versions of the Pixlr photo editor. He has rich experience in the gaming industry, with development experience of more than 60 games and 30 WebGL applications, using Unity3D and WebGL. His passion and curiosity toward gaming technologies and their challenges have helped his clients to launch games on various platforms, such as web, mobile, and consoles. He holds a BTech degree in computer science and engineering from Uttar Pradesh Technical University, where he studied computer science and graphics programming. Currently, he is working at Excellarate as a lead WebGL graphics engineer for the development of metaverse content using WebGL.

I would like to thank my best friend, my wife, and the love of my life, Simran Gogna, for supporting me in my hobbies and professional career. Her selfless love and wise advice gave me the strength to reach my goals. I also wish to thank my mentors, Sumeet Arora and Shiwani Arora, for providing inspiration and direction in my early career days.

When Soumya Mondal is not saving the world, he cosplays as a student at the Technical University of Munich and as a visual AI researcher at a start-up. He has shipped a few games, raising awareness of social issues, and has a strong belief in the power of games as a medium. In his free time, he travels and runs.

Table of Contents

Preface xiii

Part 1 – Essential Tools

1

Hello Graphics Window: You’re On Your Way 3

Technical requirements4

Getting Started with Python, PyCharm, and Pygame5

Creating a Basic Graphics Window8

Working with Window and Cartesian Coordinates14

Let’s do it…15

Your Turn…19

Summary19

Answers20

2

Let’s Start Drawing 21

Technical requirements22

Color22

Let’s do it…23

Your turn…25

Lines25

Let’s do it…25

Your turn…26

Let’s do it…27

Text29

Let’s do it…31

Your turn…32

Polygons33

Let’s do it…34

Your turn…35

Raster Images35

Let’s do it…36

Summary38

Answers38

3

Line Plotting Pixel by Pixel 41

Technical requirements41

The Naïve Way: Drawing a line with brute force42

Let’s do it…43

Enter Bresenham: The improved approach45

Let’s do it…47

Drawing circles the Bresenham way52

Let’s do it…53

Anti-aliasing56

Summary59

4

Graphics and Game Engine Components 61

Technical requirements61

Exploring the OpenGL Graphics Pipeline62

Let’s do it…63

Drawing Models with Meshes65

Let’s do it…67

Your turn…69

Viewing the Scene with Cameras69

Projecting Pixels onto the Screen73

Understanding 3D Coordinate Systems in OpenGL73

Perspective Projection74

Your turn…76

Projecting into the NDC76

Let’s do it…79

Your turn…82

Summary82

Answers83

5

Let’s Light It Up! 85

Technical requirements85

Adding Lighting Effects85

Let’s do it…87

Your turn…90

Placing Textures on Meshes90

Let’s do it…90

Let’s do it…93

Summary97

Answers98

6

Updating and Drawing the Graphics Environment 99

Technical requirements100

Introducing the main game loop100

Updating and drawing objects102

Let’s do it…104

Your turn…108

Measuring time108

Let’s do it…109

Summary110

Answers110

7

Interactions with the Keyboard and Mouse for Dynamic Graphics Programs 113

Technical requirements113

Working with mouse interactions114

Let’s do it…115

Your turn…119

Combining 2D and 3D environments120

Let’s do it…120

Converting the mouse to projection coordinates126

Let’s do it…128

Your turn…130

Let’s do it…131

Adding keyboard commands132

Let’s do it…132

Your turn…133

Let’s do it…134

Summary135

Answers135

Part 2 – Essential Trigonometry

8

Reviewing Our Knowledge of Triangles 141

Technical requirements142

Comparing similar triangles142

Working with right-angled triangles145

Your turn…147

Calculating angles with sine, cosine, and tangent148

Investigating triangles by drawing 3D objects151

Summary156

Answers156

9

Practicing Vector Essentials 159

Technical requirements160

Understanding the difference between points and vectors160

Defining vector key operations 165

Let’s do it…169

Working out a vector’s magnitude 171

Your turn…172

Exploring the relationship between angles and vectors172

The dot product174

The cross product174

Summary176

Answers177

10

Getting Acquainted with Lines, Rays, and Normals 179

Technical requirements180

Defining lines, segments, and rays180

Using the parametric form of lines182

Your turn…183

Let’s do it…184

Calculating and displaying normals187

Your turn…189

Let’s do it…189

Your turn…193

Summary 193

Answers194

11

Manipulating the Light and Texture of Triangles 197

Technical requirements198

Displaying Mesh Triangle Normals198

Let’s do it…199

Defining polygon sides with normals202

Working with visible sides of a polygon203

Culling Polygons According to the Normals205

Let’s do it…206

Let’s do it…208

Exploring How Normals Affect Lighting214

Let’s do it…215

Summary220

Part 3 – Essential Transformations

12

Mastering Affine Transformations 223

Technical requirements224

Translating points in 3D225

Your turn…226

Scaling points with x, y, and z227

Let’s do it…228

Your turn…231

Rotating points around a pivot231

Let’s do it…233

Exploring transformation orders236

Shearing and reflections239

Summary241

Answers241

13

Understanding the Importance of Matrices 243

Technical requirements244

Defining matrices244

Performing operations on matrices245

Adding and subtracting matrices245

Multiplying by a single value246

Dividing matrices248

Creating matrix representations of affine transformations252

Moving from linear equations to matrix operations252

Compounding affine transformations254

Combining transformation matrices for complex maneuvers256

Let’s do it…257

Summary263

Answers264

14

Working with Coordinate Spaces 267

Technical requirements268

Understanding OpenGL’s Matrix Stack268

Working with the Model Matrix270

Let’s do it…271

Your turn…275

Working with the View Matrix276

Let’s do it…276

Working with the Projection Matrix280

Let’s do it…281

Your turn…283

Summary284

Answers284

15

Navigating the View Space 289

Technical requirements289

Flying maneuvers290

Let’s do it…291

Your turn...294

Understanding and fixing compound rotation quirks299

Improving camera orientations301

Let’s do it…302

Summary307

Answers308

16

Rotating with Quaternions 311

Technical requirements312

Introducing quaternions312

Rotating around an arbitrary axis312

Exploring quaternion spaces315

Let’s do it…318

Working with unit quaternions322

Your turn…323

Understanding the purpose of normalization323

Let’s do it...326

Your turn...330

Summary330

Answers331

Part 4 – Essential Rendering Techniques

17

Vertex and Fragment Shading 335

Technical requirements336

Understanding shaders336

Transferring processing from the CPU to the GPU339

Let’s do it…339

Processing pixel by pixel348

Let’s do it…350

Summary357

18

Customizing the Render Pipeline 359

Technical requirements359

Coloring and texturing mesh faces360

Let’s do it…361

Let’s do it…364

Your turn...372

Turning on the lights373

Ambient lighting374

Diffuse lighting375

Phong377

Summary381

Answers382

19

Rendering Visual Realism Like a Pro 383

Technical requirements383

Following where light bounces384

Applying the Inverse Square Law386

Calculating Bidirectional Reflectance387

Distribution functions388

The Fresnel effect389

Geometric attentuation factor390

Putting it all together392

Let’s do it…392

Summary406

Index 409

Why subscribe?417

Other Books You May Enjoy 418

Packt is searching for authors like you420

Download a Free PDF copy of this book422

Preface

Part 1 – Essential Tools

This part will step you through the process of setting up and testing your development environment to follow along with the exercises throughout the book. Follow along step-by-step as we set up the development environment to be used throughout the book and test it by producing a graphics window of your choice in Windows or macOS. Following this, we will explore the mathematics required to draw common 2D shapes on the screen by coding the necessary formula into our project. As the part proceeds, more drawing functionality will be added until you have a 3D drawing environment with mouse and keyboard interaction that is capable of drawing, texturing, and lighting a 3D model.

In this part, we cover the following chapters:

Chapter 1, Hello Graphics Window: You’re On Your WayChapter 2, Let’s Start DrawingChapter 3, Line Plotting Pixel by PixelChapter 4, Graphics and Game Engine ComponentsChapter 5, Let’s Light It Up!Chapter 6, Updating and Drawing the Graphics EnvironmentChapter 7, Interactions with the Keyboard and Mouse for Dynamic Graphics Programs