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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.19.147