opengl superbible, second edition - gbv

18
OpenGL SuperBible, Second Edition Richard S. Wright Jr. Michael Sweet Waite Group Press A Division of Macmillan USA, Inc. 201 West 103rd St., Indianapolis, Indiana, 46290 USA

Upload: others

Post on 20-Oct-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

OpenGL SuperBible,

Second Edition

Richard S. Wright Jr.

Michael Sweet

Waite Group Press A Division of Macmillan USA, Inc.

201 West 103rd St., Indianapolis, Indiana, 46290 USA

CONTENTS AT A GLANCE

INTRODUCTION 1

CHAPTER 1 3D Graphics Fundamentals 9

CHAPTER 2 What Is OpenGL? 29

CHAPTER 3 Using OpenGL 37

CHAPTER 4 Drawing in Space: Lines, Points, and Polygons 79

CHAPTER 5 Moving Around in Space: Coordinate Transformations 135

CHAPTER 6 Color, Lighting, and Materials 173

CHAPTER 7 Raster Graphics in OpenGL 241

CHAPTER 8 Texture Mapping 273

CHAPTER 9 3D Modeling and Object Composition 309

CHAPTER 10 Visual Effects: Blending and Fog 359

CHAPTER 11 Buffers: Not Just for Animation 371

CHAPTER 12 Beyond Lines and Triangles 397

CHAPTER 13 Curves and Surfaces: What the #%@!& Are NURBS? 429

CHAPTER 14 Interactive Graphics 471

CHAPTER 15 Imaging with OpenGL 503

CHAPTER 16 Common OpenGL Extensions 515

PART 3;i?PENGL FOR WINDOWS: OPENGL + WUJ3T= WIGGLE

CHAPTER 17 The OpenGL Pixel Format and Rendering Context 531

CHAPTER 18 Non-Windowed Rendering 595

CHAPTER 19 Real-Time Programming with OpenGL 605

iii

APPENDIX A Summary of OpenGL Updates 647

APPENDIX В Further Reading 649

APPENDIX С The OpenGL State Machine 651

APPENDIX D Glossary 661

INDEX 665

iv ъ

TABLE OF CONTENTS

INTRODUCTION 1

CHAPTER 1 3D GRAPHICS FUNDAMENTALS 9 What's This All About? 9 A Brief History of Computer Graphics 9

Enter the CRT 10 Going 3D 10

2D + Perspective = 3D 11 3D Artifacts 13

A Survey of 3D Effects 13 Perspective 13 Color and Shading 14 Light and Shadows 14 Texture Mapping 15 Fog 15 Blending and Transparency 15 Anti-Aliasing 16

Common Uses for 3D Graphics 17 Real-Time 3D 17 Non-Real-Time 3D 20

Basic 3D Programming Principles 21 Immediate Mode and Retained Mode (Scene Graphs) 21 Coordinate Systems 21

2D Cartesian Coordinates 22 Coordinate Clipping 23 Viewports: Mapping Drawing Coordinates to Window Coordinates 23 The Vertex-A Position in Space 25 3D Cartesian Coordinates 25

Projections: Getting 3D to 2D 25 Orthographic Projections 26 Perspective Projections 27

Summary 28

CHAPTER 2 WHAT IS OPENGL? 29 Evolution of a Standard 30

The OpenGL ARB 30

V

OPENGL SUPERBIBLE, SECOND EDITION

Licensing and Conformance 31 The Future of OpenGL 31

How Does OpenGL Work? 32 Generic Implementations 32 Hardware Implementations 33 The Pipeline 34 The State Machine 35

Summary 35

CHAPTER 3 USING OPENGL 37 What you'll learn in this chapter: 37 OpenGL: An API, Not a Language 37

Libraries and Headers 38 Hardware Acceleration 39 Other Implementations 39

API Specifics 40 Data Types 40 Function Naming Conventions 42

Platform Independence 43 Using GLUT 43 Setting Up Your Programming Environment 44

Your First Program 45 The Headers 47 The Body 47

Display Mode: Single Buffered 47 Creating the OpenGL Window 48 Displaying Callback 48 Set Up the Context and Go! 48

OpenGL Graphics Calls 48 Actually Clear 50 Flushing That Queue 50

Drawing Shapes with OpenGL 51 Drawing a Rectangle 53 Scaling to the Window 54

Setting the Viewport and Clipping Volume 54 Defining the Viewport 55 Defining the Clipping Volume 56 Keeping a Square Square 58

Animation with OpenGL and GLUT 58 Double-Buffering 62

— vi

CONTENTS

Finally Some 3D! 63 Summary 64 Reference Section 65

CHAPTER 4 DRAWING IN SPACE: LINES, POINTS, AND POLYGONS 79 What you'll learn in this chapter: 79 Drawing Points in 3D 80 Setting Up a 3D Canvas 80 A 3D Point: the Vertex 82 Draw Something! 83

Drawing Points 83 Our First Example 84

Setting the Point Size 87 Drawing Lines in 3D 90

Line Strips and Loops 91 Approximating Curves with Straight Lines 92 Setting the Line Width 93 Line Stippling 95

Drawing Triangles in 3D 98 Triangles: Your First Polygon 98 Winding 99 Triangle Strips 100 Triangle Fans 101

Building Solid Objects 102 Setting Polygon Colors 105 Hidden Surface Removal 106 Culling: Hiding Surfaces for Performance 107 Polygon Modes 110

Other Primitives I l l Four-Sided Polygons: Quads I l l

Quad Strips I l l General Polygons 112 Filling Polygons, or Stippling Revisited 112 Polygon Construction Rules 116 Subdivision and Edges 118

Summary 120 Reference Section 120

OPENGL SUPERBIBLE, SECOND EDITION

CHAPTER 5 MOVING AROUND IN SPACE: COORDINATE TRANSFORMATIONS 135

What you'll learn in this chapter: 135 Is This the Dreaded Math Chapter? 136 Understanding Transformations 136

Eye Coordinates 137 Viewing Transformations 138 Modeling Transformations 139 The Modelview Duality 140 Projection Transformations 141 Viewport Transformations 142

Matrix Munching 142 What Is a Matrix? 143 The Transformation Pipeline 143 The Modelview Matrix 144

Translation 145 Rotation 146 Scaling 146

The Identity Matrix 147 The Matrix Stacks 150 A Nuclear Example 151

Using Projections 154 Orthographic Projections 154 Perspective Projections 155 A Far-Out Example 159

Advanced Matrix Manipulation 161 Loading a Matrix 162 Performing Your Own Transformations 162 Other Transformations 163

Summary 163 Reference Section 163

CHAPTER 6 COLOR, LIGHTING, AND MATERIALS 173 What you'll learn in this chapter: 173 What Is Color? 174

Light as a Wave 174 Light as a Particle 174 Your Personal Photon Detector 176 The Computer as a Photon Generator 176

V I I I

CONTENTS

PC Color Hardware 177 PC Display Modes 178

Screen Resolution 179 Color Depth 179

4-Bit Color 179 8-Bit Color 179 24-Bit Color 180 Other Color Depths 180

Using Color in OpenGL 180 The Color Cube 180 Setting the Drawing Color 182 Shading 183 Setting the Shading Model 185

Color in the Real World 186 Ambient Light 186 Diffuse Light 187 Specular Light 187 Put It All Together 187

Materials in the Real World 188 Material Properties 189 Adding Light to Materials 189 Calculating Ambient Light Effects 190 Diffuse and Specular Effects 190

Adding Light to a Scene 191 Enable the Lighting 191 Set Up the Lighting Model 191 Set Material Properties 192

Using a Light Source 195 Which Way Is Up? 195 Surface Normals 196 Specifying a Normal 197 Unit Normals 199 Finding a Normal 200 Setting Up a Source 202 Setting the Material Properties 204 Specifying the Polygons 204

Lighting Effects 206 Specular Highlights 206 Specular Light 206

ix

OPENGL SUPERBIBLE, SECOND EDITION

Specular Reflectance 207 Specular Exponent 208 Normal Averaging 209

Putting It All Together 211 Creating a Spotlight 212 Drawing a Spotlight 214

Shadows 219 What Is a Shadow? 219 Squish Code 220 A Shadow Example 221

Summary 225 Reference Section 225

PART 2; MEAT AND POTATOES—THE STAPLES OF OPENGL RENDERING

CHAPTER 7 RASTER GRAPHICS IN OPENGL 241 What you'll learn in this chapter: 241 Drawing Bitmaps 242

Raster Graphics Clipping 244 Bitmap Fonts 245

Building a Simple Font Library 245 Destroying Bitmap Fonts 247 Drawing Using Bitmap Fonts 247 Displaying Formatted Text 248 A Simple Text Drawing Program 249

Bitmaps with Color—Pixmaps 250 Drawing Pixmaps 250

A Windows .BMP File Viewer 252 About Windows Bitmap Files 252 Reading .BMP Files 253 Displaying the Bitmap 254 Scaling the Image 255

Drawing Images with Transparency 256 Drawing Indexed Images 258 Panning an Image 259 Reading Images 259 Copying Images 262 Writing .BMP Files 262 Summary 263 Reference Section 264

X

CONTENTS

CHAPTER 8 TEXTURE MAPPING 273 What you'll learn in this chapter: 273 The Basics of Texture Mapping 274 Defining Texture Images 275

Defining ID Textures 275 Defining 2D Textures 276 Texture Modes 276 Texture Filters 277 Texture Coordinates 278 Texture Wrapping 279

Putting It All Together—A ID Texture Example 279 A 2D Texturing Example 281 Using More Than One Texture Image 281

Managing Texture Objects 281 Keeping Texture Objects in Memory 282 A Simple Texture Object Loader 282

A Terrain Viewing Program 286 Making the Terrain More Interesting 288

Automatically Generating Texture Coordinates 289 Other Generation Modes 290

Mipmapped Textures 290 Defining Mipmapped Textures 291 Generating Mipmaps Automatically 292 Controlling the Level of Detail 293 Adding Mipmaps to the Terrain Viewer 293

Lighting with Textures 294 Rendering Specular Highlights 296

Replacing Texture Image Data 297 3D Texturing 298

A Simple 3D Texture Program 298 Summary 299 Reference Section 300

CHAPTER 9 3D MODELING AND OBJECT COMPOSITION 309 What you'll learn in this chapter: 309 Defining the Task 310

Choosing a Projection 310 Choosing the Lighting and Material Properties 311 Displaying the Results 312

XI

OPENGL SUPERBIBLE, SECOND EDITION

Constructing a Model, One Piece at a Time 313 The Head 314 The Shaft 318 The Thread 321 Putting the Model Together 324

Preprocessing Your Models 325 Display Lists 326

Display List Caveats 327 A Display List Sample 327

Vertex Arrays 329 Creating an Array 330 Accessing an Array 334 OpenGL Support for Vertex Arrays 335 Using Array Elements 336 Specifying a Block of Primitives 337 Indexed Vertex Arrays 337

Vertex Arrays and Display Lists 340 Summary 341 Reference Section 341

CHAPTER 10 VISUAL EFFECTS: BLENDING AND FOG 359 What you'll learn in this chapter: 359 Blending 359

Using Blending for Transparency 361 Using Blending with Anti-Aliasing 362 Using Blending for a Paint Program 363 Revisiting the Terrain Viewing Program 364

Fog 365 Drawing Depth-Cued Teapots 366 Other Types of Fog 367 Fog Distance 367

Back to the Terrain Viewing Program 367 Summary 368 Reference Section 369

CHAPTER 11 BUFFERS: NOT JUST FOR ANIMATION 371 What you'll learn in this chapter: 371 What Are Buffers? 372

Buffers in the WIN32 Environment 372 Pixel Formats 372

XII

CONTENTS

Device Contexts 374 Rendering Contexts 375

Configuring Buffers Using WIN32 Functions 375 Getting a Device Context 375 Setting the Pixel Format 375 Creating a Color Palette as Needed 376 Creating a Rendering Context 377

Configuring Buffers Using GLUT Functions 378 Picking Only the Buffers You Need 378 The Color Buffer 379

Double Buffering 379 Stereo Buffering 379 Swapping Buffers 380

The Depth Buffer 380 Enabling Depth Comparisons 381 Depth Comparisons 381 Depth Values 383 Applications of the Depth Buffer 383 Another Application of the Depth Buffer 384 Cutting Away Parts of a Scene 385

The Stencil Buffer 386 Using the Stencil Buffer 386 Stencil Buffer Functions 386 Drawing into the Stencil Buffer 387

The Accumulation Buffer 389 Using the Accumulation Buffer for Motion Blur 390 Using the Accumulation Buffer for Anti-Aliasing 391 Costs of Full-Scene Anti-Aliasing 393

Summary 393 Reference Section 393

CHAPTER 12 BEYOND LINES AND TRIANGLES 397 What you'll learn in this chapter: 397 Quadrics 398

Changing the Way Quadrics Are Drawn 398 Drawing Cylinders 399 Texturing and Cylinders 400 Drawing Cones 400 Drawing Disks 400

XIII

OPENGL SUPERBIBLE, SECOND EDITION

Disks and Textures 401 Drawing Partial Disks 401 Drawing Spheres 402 Spheres and Textures 402

Drawing a Pencil 402 Complex Polygons 404

GLU 1.1 and GLU 1.2 405 Creating a Tessellator Object 405 Callback Functions 406 Drawing Concave Polygons 408 Drawing Complex Polygons 409

Combining Quadrics and Polygon Tessellators 411 Building an F-16 Model 411 Drawing the F-16 Model 413

Tessellating Height Fields (Grids) 415 Drawing Height Fields with Triangle Strips 415 Improving on the Triangle Strip Method 415 Other Tessellation Algorithms 416

Improving the Terrain Viewing Program 416 Summary 417 Reference Section 418

CHAPTER 13 CURVES AND SURFACES: WHAT THE #%©!& ARE NURBS? 429 What you'll learn in this chapter: 429 Curves and Surfaces 430

Parametric Representation 430 Control Points 431 Continuity 432

Evaluators 432 A 2D Curve 433 Evaluating a Curve 437 A 3D Surface 437 Lighting and Normal Vectors 440

NURBS 441 From Bezier to B-Splines 441 Knots 442 Creating a NURBS Surface 443 NURBS Properties 444

XIV

CONTENTS

Define the Surface 444 Trimming 445 Nurbs Curves 448

Summary 448 Reference Section 448

CHAPTER 14 INTERACTIVE GRAPHICS 471 What you'll learn in this chapter: 471 Selection 472

Naming Your Primitives 472 Working with Selection Mode 474 The Selection Buffer 475 Picking 477 Hierarchical Picking 479

Feedback 483 The Feedback Buffer 483 Feedback Data 484 Passthrough Markers 485

A Feedback Example 485 Label the Objects for Feedback 486 Step 1: Select the Object 488 Step 2: Get Feedback on the Object 490

Summary 492 Reference Section 492

CHAPTER 15 IMAGING WITH OPENGL 503 What you'll learn in this chapter 503 Basics of the OpenGL Imaging Extension 503

Checking for the OpenGL Imaging Extension 504 The Color Matrix 504

Color Matrix Tricks 504 Brightness 504 Contrast 505 Color Filters 505 Saturation 505 Color Correction 506

Convolution (Filtering) 508 Sharpening Convolution Filters 508 Blurring Convolution Filters 508

XV

OPENGL SUPERBIBLE, SECOND EDITION

Histogram Equalization 509 Getting Histogram Data 509 Getting Minimum and Maximum Data 509

Summary 510 Reference Section 510

CHAPTER 16 COMMON OPENGL EXTENSIONS 515 What you'll learn in this chapter: 515 What Extensions Are Available? 515 When Should I Use an Extension? 516 Checking for Extension Availability 516

Accessing Extension Functions 517 Extension Constants 517

The Swap Hint Extension 518 Making a Flight Simulator 518

The Vertex Culling Extension 520 Using the Vertex Culling Extension 521 Vertex Culling in the Terrain Examples 521

The Multi-Texture Extension 523 Checking for the Multi-Texture Extension 523 Using the Multi-Texture Extension 523

Summary 525 Reference Section 525

PART 3: OPENGL FOR WINDOWS: OPENGL + WIN32 = WIGGLE

CHAPTER 17 THE OPENGL PIXEL FORMAT AND RENDERING CONTEXT 531 What you'll learn in this chapter: 531 OpenGL Implementations on Windows 532

Generic OpenGL 532 Installable-Client Driver 533 Mini-Client Driver 533 Mini-Driver 534

Basic Windows Rendering 534 GDI Device Contexts 535 Pixel Formats 537

Describing a Pixel Format 537 Enumerating Pixel Formats 541 Selecting and Setting a Pixel Format 543

The OpenGL Rendering Context 544

XVI

CONTENTS

Putting It All Together 545 Creating the Window 545

First, You Need a Device Context 547 Enter WIN32 548 Three Steps Forward, Two Steps Back 548 All Is Not Lost 549

Using the OpenGL Rendering Context 550 Initializing the Rendering Context 553 Shutting Down the Rendering Context 553

Other Windows Messages 554 The WM_PAINT Message 554

Windows Palettes 555 Color Matching 556 Palette Arbitration 556 Creating a Palette for OpenGL 558

Do You Need a Palette? 559 The Palette's Structure 559 The 3-3-2 Palette 560 Building the Palette 561

Palette Creation and Disposal 563 Some Restrictions Apply 564

OpenGL and Windows Fonts 565 3D Fonts and Text 565

Rendering 3D Text 567 2D Fonts and Text 568

Full-Screen Rendering 568 Creating a Frameless Window 569 Creating a Full-Screen Window 570

Multithreaded Rendering 572 Summary 573 Reference Section 574

CHAPTER 18 NON-WINDOWED RENDERING 595 The Basics of Offscreen Rendering 595

Limitations of Offscreen Rendering 596 Creating an Offscreen Buffer for Rendering 596 Destroying an Offscreen Buffer 597 Sharing Display Lists 598

Saving to a .BMP File 598 Rendering to the Offscreen Buffer 599

xvii

OPENGL SUPERBIBLE, SECOND EDITION

Printing Images 601 Printing from the Terrain Viewing Program 601

Metafiles 602 Creating an Enhanced Metafile Device Context 603 Metafiles and OpenGL Viewports 603 Generating a Metafile from the Terrain Viewing Program 603

Summary 604

CHAPTER 19 REAL-TIME PROGRAMMING WITH OPENGL 605 What you'll learn in this chapter: 605 How Fast Is Fast? 606

Performance Metrics 606 Measuring Performance 607

Windows Animation Techniques 608 Using Timers for Animation 608

A Simple Benchmark 610 Short-Circuiting the WM_PAINT Mechanism 613 Event Loop Insertion 615 Using a Rendering Thread 618

Creating an OpenGL Rendering Thread 619 Communicating with the Rendering Thread 621 Terminating the Rendering Thread 624

Putting the "Real" in Real-Time 625 Time-Based Key Framing 625 Practical Limitations 627 Real-Time Response 628

Reading the Keyboard 629 Reading a Joystick 629 Moving the Camera 630

Scene Graph Basics 635 Cluttering Up the Landscape 635

What's Wrong With This Picture? 638 Model Culling 638

Just Plane Math 639 Shortcuts Rule! 640

Other Real-Time Techniques 642 Direct X 642 General OpenGL Performance Tips 642

Summary 643

XVIII

CONTENTS

APPENDIX A SUMMARY OF OPENGL UPDATES 647 Version 1.1 (December 1995) 647 Version 1.2 (March 1998) 648

APPENDIX В FURTHER READING 649 Windows Programming Books 649 OpenGL Books 649 3D Graphics Books 649 The Web 650

APPENDIX С THE OPENGL STATE MACHINE 651 Basic OpenGL State Functions 651 Saving and Restoring States 652

Drawing States 654 Depth Buffer States 655 Stencil Buffer States 655 Lighting States 655 Texturing States 656 Pixel States 657

Reference Section 657

APPENDIX D GLOSSARY 661

INDEX 665