ebook img

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

445 Pages·2022·34.146 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

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

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 Co n t r i b u t o r s 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 requirements 4 Let’s do it… 15 Getting Started with Python, Your Turn… 19 PyCharm, and Pygame 5 Summary 19 Creating a Basic Graphics Window 8 Answers 20 Working with Window and Cartesian Coordinates 14 2 Let’s Start Drawing 21 Technical requirements 22 Your turn… 32 Color 22 Polygons 33 Let’s do it… 23 Let’s do it… 34 Your turn… 25 Your turn… 35 Lines 25 Raster Images 35 Let’s do it… 25 Let’s do it… 36 Your turn… 26 Summary 38 Let’s do it… 27 Answers 38 Text 29 Let’s do it… 31 vi Table of Contents 3 Line Plotting Pixel by Pixel 41 Technical requirements 41 Let’s do it… 47 The Naïve Way: Drawing a line Drawing circles the Bresenham way 52 with brute force 42 Let’s do it… 53 Let’s do it… 43 Anti-aliasing 56 Enter Bresenham: The Summary 59 improved approach 45 4 Graphics and Game Engine Components 61 Technical requirements 61 Understanding 3D Coordinate Exploring the OpenGL Graphics Systems in OpenGL 73 Pipeline 62 Perspective Projection 74 Let’s do it… 63 Your turn… 76 Projecting into the NDC 76 Drawing Models with Meshes 65 Let’s do it… 79 Let’s do it… 67 Your turn… 82 Your turn… 69 Summary 82 Viewing the Scene with Cameras 69 Answers 83 Projecting Pixels onto the Screen 73 5 Let’s Light It Up! 85 Technical requirements 85 Let’s do it… 93 Adding Lighting Effects 85 Summary 97 Let’s do it… 87 Answers 98 Your turn… 90 Placing Textures on Meshes 90 Let’s do it… 90 Table of Contents vii 6 Updating and Drawing the Graphics Environment 99 Technical requirements 100 Measuring time 108 Introducing the main game loop 100 Let’s do it… 109 Updating and drawing objects 102 Summary 110 Let’s do it… 104 Answers 110 Your turn… 108 7 Interactions with the Keyboard and Mouse for Dynamic Graphics Programs 113 Technical requirements 113 Your turn… 130 Working with mouse interactions 114 Let’s do it… 131 Let’s do it… 115 Adding keyboard commands 132 Your turn… 119 Let’s do it… 132 Combining 2D and 3D environments 120 Your turn… 133 Let’s do it… 120 Let’s do it… 134 Converting the mouse to projection Summary 135 coordinates 126 Let’s do it… 128 Answers 135 Part 2 – Essential Trigonometry 8 Reviewing Our Knowledge of Triangles 141 Technical requirements 142 Calculating angles with sine, cosine, Comparing similar triangles 142 and tangent 148 Working with right-angled triangles 145 Investigating triangles by drawing 3D objects 151 Your turn… 147 Summary 156 Answers 156 viii Table of Contents 9 Practicing Vector Essentials 159 Technical requirements 160 Exploring the relationship between Understanding the difference between angles and vectors 172 points and vectors 160 The dot product 174 Defining vector key operations 165 The cross product 174 Let’s do it… 169 Summary 176 Working out a vector’s magnitude 171 Answers 177 Your turn… 172 10 Getting Acquainted with Lines, Rays, and Normals 179 Technical requirements 180 Your turn… 189 Defining lines, segments, and rays 180 Let’s do it… 189 Your turn… 193 Using the parametric form of lines 182 Your turn… 183 Summary 193 Let’s do it… 184 Answers 194 Calculating and displaying normals 187 11 Manipulating the Light and Texture of Triangles 197 Technical requirements 198 Let’s do it… 206 Displaying Mesh Triangle Normals 198 Let’s do it… 208 Let’s do it… 199 Exploring How Normals Affect Lighting 214 Defining polygon sides with normals 202 Let’s do it… 215 Working with visible sides of a polygon 203 Summary 220 Culling Polygons According to the Normals 205 Table of Contents ix Part 3 – Essential Transformations 12 Mastering Affine Transformations 223 Technical requirements 224 Rotating points around a pivot 231 Translating points in 3D 225 Let’s do it… 233 Your turn… 226 Exploring transformation orders 236 Scaling points with x, y, and z 227 Shearing and reflections 239 Let’s do it… 228 Summary 241 Your turn… 231 Answers 241 13 Understanding the Importance of Matrices 243 Technical requirements 244 Moving from linear equations to matrix operations 252 Defining matrices 244 Compounding affine transformations 254 Performing operations on matrices 245 Combining transformation matrices Adding and subtracting matrices 245 for complex maneuvers 256 Multiplying by a single value 246 Dividing matrices 248 Let’s do it… 257 Creating matrix representations of Summary 263 affine transformations 252 Answers 264 14 Working with Coordinate Spaces 267 Technical requirements 268 Let’s do it… 276 Understanding OpenGL’s Matrix Stack 268 Working with the Projection Matrix 280 Working with the Model Matrix 270 Let’s do it… 281 Let’s do it… 271 Your turn… 283 Your turn… 275 Summary 284 Working with the View Matrix 276 Answers 284

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.