fifth edition - gbv

12
OpenGL SUPERBIBLE Fifth Edition Comprehensive Tutorial and Reference Nicholas Haemel Richard S. Wright, Jr. Graham Sellers Benjamin Lipchak AAddison-Wesley Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Cape Town • Sydney • Tokyo • Singapore • Mexico City

Upload: others

Post on 17-Oct-2021

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fifth Edition - GBV

OpenGL SUPERBIBLE Fifth Edition

Comprehensive Tutorial and Reference

Nicholas Haemel Richard S. Wright, Jr.

Graham Sellers Benjamin Lipchak

AAddison-Wesley Upper Saddle River, NJ • Boston • Indianapolis • San Francisco

New York • Toronto • Montreal • London • Munich • Paris • Madrid Cape Town • Sydney • Tokyo • Singapore • Mexico City

Page 2: Fifth Edition - GBV

Table of Contents

Preface to the Fifth Edition xxi

Preface to the Fourth Edition xxiii

Preface to the Third Edition xxvi

Introduction 1

What's New in This Edition 1 How This Book Is Organized 2

Part I: Basic Concepts 2 Part II: Intermediate to Advanced Ideas 3 Part III: Platform-Specific Notes 4

Conventions Used in This Book 4 About the Companion Web Site 5

PART I Basic Concepts 7

1 Introduction to 3D Graphics and OpenGL 9

A Brief History of Computer Graphics 9 Going Electric 10 Going 3D 11

3D Graphics Techniques and Terminology 14 Transformations and Projections 14 Rasterization 14 Shading 16 Texture Mapping 16 Blending 17 Connecting the Dots 18

Common Uses for 3D Graphics 18 Real-Time 3D 18 Non-Real-Time 3D 22 Shaders 22

Page 3: Fifth Edition - GBV

x OpenGL SuperBible

Basic 3D Programming Principles 23 Not a Toolbox 23 Coordinate Systems 24 Projections: Getting 3D to 2D 28

Summary 30

2 Getting Started 33

What Is OpenGL? 33 Evolution of a Standard 34 Licensing and Conformance 38 The Future of OpenGL 38 Deprecated Functionality 40

Using OpenGL 42 Our Supporting Cast 42 OpenGL API Specifics 44 OpenGL Errors 46 Identifying the Version 47 Getting a Clue with glHint 47 The OpenGL State Machine 48

Setting Up Windows Projects 49 Including Paths 50 Creating the Project 52 Adding Our Files 53

Setting Up Mac OS X Projects 55 Custom Build Settings 55 Creating a New Project 56 Frameworks, Headers, and Libraries 59

Your First Triangle 61 What to "Include" 64 Starting GLUT 65 Coordinate System Basics 67 Setting Things Up 70 Getting Down to Business 73

Putting a Little Life into It! 73 Special Keys 74 Refreshing the Display 75 Simple Automated Animation 76

Summary 77

Page 4: Fifth Edition - GBV

Contents xi

3 Basic Rendering 79

The Basic Graphics Pipeline 80 Client-Server 81 Shaders 82

Setting Up Your Coordinate System 84 Orthographic Projections 85 Perspective Projections 85

Using the Stock Shaders 86 Attributes 87 Uniforms 87

Connecting The Dots 89 Points and Lines 90 Drawing Triangles in 3D 94 Individual Triangles 95 A Simple Batch Container 100 Unwanted Geometry 101 Polygon Offset 108 Cutting It Out with Scissors I l l

Blending 113 Combining Colors 114 Changing the Blending Equation 116 Antialiasing 117 Multisampling 121

Summary 123

4 Basic Transformations: A Vector/Matrix Primer 125

Is This the Dreaded Math Chapter? 126 A Crash Course in 3D Graphics Math 126

Vectors, or Which Way Is Which? 127 The Matrix 130

Understanding Transformations 132 Eye Coordinates 132 Viewing Transformations 133 Modeling Transformations 134 The Modelview Duality 135 Projection Transformations 136 Viewport Transformations 137

Page 5: Fifth Edition - GBV

xii OpenGL SuperBible

The Modelview Matrix 138 Matrix Construction 138 Applying a Modelview Matrix 144

More Objects 145 Using the Triangle Batch Class 146 Making a Sphere 146 Making a Torus 147 Making a Cylinder or a Cone 148 Making a Disk 150

Projection Matrix 150 Orthographic Projections 151 Perspective Projection 153 The ModelviewProjection Matrix 155

The Transformation Pipeline 159 Using a Matrix Stack 159 Managing Your Pipeline 161 Spicing It Up! 165

Moving Around Using Cameras and Actors 167 An Actor Frame 168 Euler Angles: "Use the Frame, Luke!" 169 Camera Management 170 Adding More Actors 173 What About Lights? 175

Summary 177

5 Basic Texturing 179

Raw Image Data 180 Pixel Packing 181 Pixmaps 183 Packed Pixel Formats 185 Saving Pixels 186 Reading Pixels 188

Loading Textures 192 Using the Color Buffer 193 Updating Textures 194 Texture Objects 195

Texture Application 196 Texture Coordinates 196 Texture Parameters 199 Putting It All Together 202

Page 6: Fifth Edition - GBV

Contents xiii

Mipmapping 207 Mipmap Filtering 209 Generating Mip Levels 210 Mipmaps in Action 210

Anisotropie Filtering 221 Texture Compression 223

Compressing Textures 224 Loading Compressed Textures 225 A Final Example 226

Summary 227

6 Thinking Outside the Box, Nonstock Shaders 229

GLSL101 230 Variables and Data Types 231 Storage Qualifiers 234 ARealShader 236 Compiling, Binding, and Linking 240 Using the Shader 247 Provoking Vertex 248

Shader Uniforms 249 Finding Your Uniforms 249 Setting Scalars and Vector Uniforms 250 Setting Uniform Arrays 251 Setting Uniform Matrices 252 The Flat Shader 252

Built-in Functions 255 Trigonometry Functions 256 Exponential Functions 256 Geometric Functions 257 Matrix Functions 257 Vector Relational Functions 258 Common Functions 259

Simulating Light 262 Simple Diffuse Lighting 262 The Point Light Diffuse Shader 264 The ADS Light Model 269 Phong Shading 273

Page 7: Fifth Edition - GBV

xiv OpenGL SuperBible

Accessing Textures 277 Just the Texels Please 277 Light the Texels 279 Discarding Fragments 281 Cell Shading—Texels as Light 284

Summary 287

7 More Advanced Texture Topics 289

Rectangle Textures 289 Loading a Rectangle Texture 290 Using a Rectangle Texture 291

Cube Maps 294 Loading Cube Maps 295 Making a Skybox 297 Making a Reflection 299

Multitexture 301 Multiple Texture Coordinates 301 A Multitextured Example 302

Point Sprites 304 Texturing Points 305 Point Sizes 306 Putting This All Together 307 Point Parameters 310 Shaped Points 311 Rotating Points 312

Texture Arrays 314 Loading a 2D Texture Array 314 Indexing the Texture Array 316 Accessing Texture Arrays 317

Texture Proxies 318 Summary 320

PART II Intermediate to Advanced Ideas 321

8 Buffer Objects: Storage Is Now in Your Hands 323

Buffers 324 Creating Your Very Own Buffers 324 Filling Buffers 325 Pixel Buffer Objects 326 Texture Buffer Objects 334

Page 8: Fifth Edition - GBV

Contents xv

Framebuffer Objects, Going Beyond the Window 336 HowtoUseFBOs 336 Renderbuffer Objects 337 Draw Buffers 339 Framebuffer Completeness 342 Copying Data in Framebuffers 345 Putting It All Together—Using FBOs 346

Rendering to Textures 351 Summary 358

9 Advanced Buffers: Beyond the Basics 359

Getting at Your Data 359 Mapping Buffers 360 Copying Buffers 361

Controlling the Destiny of Your Pixel Shaders; Mapping Fragment Outputs 361

New Formats for a New Hardware Generation 364 Floats—True Precision at Last! 364 Multisampling 380 Integers 385 sRGB 386 Texture Compression 387

Summary 389

10 Fragment Operations: The End of the Pipeline 391

Scissoring—Cutting Your Geometry Down To Size 392 Multisampling 392

Sample Coverage 393 Sample Mask 394 Putting It All Together 394

Stencil Operations 399 Depth Testing 402

Depth Clamp 402 Blending Everything Together 402

Blend Equation 402 Blend Function 403 Putting It All Together 404

Dithering 406 Logic Ops 407

Page 9: Fifth Edition - GBV

xvi OpenGL SuperBible

Masking Output 408 Color 408 Depth 408 Stencil 408 Usage 409

Summary 409

11 Advanced Shader Usage 411

Advanced Vertex Shaders 412 Physical Simulation in the Vertex Shader 412

Geometry Shaders 419 The Pass-Through Geometry Shader 420 Using Geometry Shaders in an Application 422 Discarding Geometry in the Geometry Shader 426 Modifying Geometry in the Geometry Shader 429 Generating Geometry in the Geometry Shader 430 Changing the Primitive Type in the Geometry Shader 434 New Primitive Types Introduced by the Geometry Shader 438

Advanced Fragment Shaders 441 Post-Processing in the Fragment Shader—Color Correction 442 Post-Processing in the Fragment Shader—Convolution 444 Generating Image Data in the Fragment Shader 448 Discarding Work in the Fragment Shader 451 Controlling Depth Per Fragment 453

More Advanced Shader Functions 454 Interpolation and Storage Qualifiers 454 Other Advanced Built-in Functions 458

Uniform Buffer Objects 460 Building Uniform Blocks 461

Summary 470

12 Advanced Geometry Management 471

Gathering Information about the OpenGL Pipeline—Queries 472 Preparing a Query 472 Issuing a Query 473 Retrieving Query Results 474 Using the Results of a Query 475 Getting OpenGL to Make Decisions for You 478 Measuring Time Taken to Execute Commands 480

Page 10: Fifth Edition - GBV

Contents xvii

Storing Data in GPU Memory 483 Using Buffers to Store Vertex Data 484 Storing Vertex Indices in Buffers 489

Using Vertex Array Objects to Organize Your Buffers 490 Drawing a lot of Geometry Efficiently 492

Combining Drawing Functions 492 Combining Geometry Using Primitive Restart 494 Instanced Rendering 496 Getting Your Data Automatically 503

Storing Transformed Vertices—Transform Feedback 508 Transform Feedback 509 Turning Off Rasterization 514 Counting Vertices Using Primitive Queries 515 Using the Results of a Primitive Query 516 Example Uses for Transform Feedback 517

Clipping and Determining What Should Be Drawn 528 Clip Distances—Defining Your Own Custom Clip Space 528

Synchronizing When OpenGL Begins to Draw 532 Summary 537

PART III Platform-Specific Notes 539

13 OpenGL on Windows 541

OpenGL Implementations on Windows 542 Microsoft's OpenGL 542 Modern Graphics Drivers 542 Extended OpenGL 544 WGL Extensions 545

Basic Windows Rendering 547 GDI Device Contexts 547 Pixel Formats 548 The OpenGL Rendering Context 556

Putting It All Together 559 Creating the Window 560

Full-Screen Rendering 565 Double Buffering 567

Eliminating Visual Tearing 567 Summary 568

Page 11: Fifth Edition - GBV

xviii OpenGL SuperBible

14 OpenGL on OS X 569

The Four Faces of OpenGL on the Mac 569 OpenGL with Cocoa 570

Creating a Cocoa Program 571 Wiring It All Together 578 Double or Single Buffered? 580 SphereWorld 580

Full-Screen Rendering 585 Going Full-Screen with Cocoa 586

CGL 593 Sync Frame Rate 593 Increasing Fill Performance 594 Multithreaded OpenGL 595

Summary 595

15 OpenGL on Linux 597

The Basics 597 Brief History 598 What Is X? 598

Getting Started 598 Checking for OpenGL 599 Setting Up Mesa 599 Setting Up Hardware Drivers 600 Setting Up GLUT and GLEW 600 Building OpenGL Apps 601

GLX—Interfacing with X Windows 602 Displays and X Windows 603 Config Management and Visuals 603 Windows and Render Surfaces 607 Extending OpenGL and GLX 608 Context Management 609 Synchronization 612 GLX Queries 613 Putting It All Together 614

Summary 617

Page 12: Fifth Edition - GBV

16 OpenGL ES on Mobile Devices 619

OpenGL on a Diet 619 What's the ES For? 620 A Brief History 620

Which Version Is Right for You? 622 ES 2.0 622

The ES Environment 627 Application Design Considerations 627 Dealing with a Limited Environment 628 Fixed-Point Math 629

EGL: A New Windowing Environment 630 EGL Displays 631 Creating a Window 632 Context Management 636 Presenting Buffers and Rendering Synchronization 636 More EGL Stuff 637

Negotiating Embedded Environments 638 Popular Operating Systems 638 Vendor-Specific Extensions 639 For the Home Gamer 639

Apple Handheld Platforms 639 Setting Up An iPhone Project 640 Moving to the iPhone 644

Summary 652

Appendix A Further Reading 653

Other Good OpenGL Books 653 3D Graphics Books 653 WebSites 654

Appendix В Glossary 655

Appendix С OpenGL Man Pages for (Core) OpenGL 3.3 661

Index 939