opengl - gbv
TRANSCRIPT
OpenGL
SUPERBIBLE Third Edition
Richard S. Wright, Jr., and Benjamin Upchak
SAMS 800 East 96th Street, Indianapolis, Indiana 46240
Table of Contents
Introduction 1
Part I Classic OpenGL
1 Introduction to 3D Graphics and OpenGL 11
What's This All About? 11 A Brief History of Computer Graphics 11
Enter the CRT 12 Going 3D 12
A Survey of 3D Effects 15 Perspective 16 Color and Shading 16 Light and Shadows 17 Texture Mapping 18 Fog 19 Blending and Transparency 19 Antialiasing 20
Common Uses for 3D Graphics 21 Real-Time 3D 21 Non-Real-Time 3D 23
Basic 3D Programming Principles 24 Immediate Mode and Retained Mode (Scene Graphs) 25 Coordinate Systems 25 Projections: Getting 3D to 2D 30
Summary 32
2 Using OpenGL 33
What Is OpenGL? 33 Evolution of a Standard 34 The API Wars 35
How Does OpenGL Work? 41 Generic Implementations 41 Hardware Implementations 42 The Pipeline 43
Bible
OpenGL: An API, Not a Language 44 Libraries and Headers 44
API Specifics 45 Data Types 45 Function-Naming Conventions 46
Platform Independence 48 Using GLUT 48 Setting Up Your Programming Environment 49 Your First Program 49 Drawing Shapes with OpenGL 55
Animation with OpenGL and GLUT 62 Double-Buffering 66
The OpenGL State Machine 68 Saving and Restoring States 69
OpenGL Errors 69 When Bad Things Happen to Good Code 70
Identifying the Version 71 Getting a Clue with glHint 71 Using Extensions 72
Checking for an Extension 72 Who's Extension Is This? 73 Getting to OpenGL Beyond 1.1 on Windows 74
Summary 74 Reference 75
Drawing in Space: Geometric Primitives and Buffers 89
Drawing Points in 3D 90 Setting Up a 3D Canvas 90 A 3D Point: The Vertex 92 Draw Something! 93
Drawing Points 93 Setting the Point Size 97 Drawing Lines in 3D 100
Line Strips and Loops 102 Approximating Curves with Straight Lines 102 Setting the Line Width 104 Line Stippling 106
Drawing Triangles in 3D 109 Triangles: Your First Polygon 109
. Winding 110
ч
Contents vii
Triangle Strips I l l Triangle Fans 112
Building Solid Objects 112 Setting Polygon Colors 116 Hidden Surface Removal 116 Culling: Hiding Surfaces for Performance 118 Polygon Modes 121
Other Primitives 122 Four-Sided Polygons: Quads 122 General Polygons 123 Filling Polygons, or Stippling Revisited 123 Polygon Construction Rules 128 Subdivision and Edges 129
Other Buffer Tricks 132 Using Buffer Targets 132 Manipulating the Depth Buffer 134 Cutting It Out with Scissors 135 Using the Stencil Buffer 136
Summary 141 Reference 142
4 Geometric Transformations: The Pipeline 159
Is This the Dreaded Math Chapter? 159 Understanding Transformations 160
Eye Coordinates 161 Viewing Transformations 162 Modeling Transformations 162 The Modelview Duality 163 Projection Transformations 165 Viewport Transformations 166
The Matrix: Mathematical Currency for 3D Graphics 166 What Is a Matrix? 166 The Transformation Pipeline 167 The Modelview Matrix 168 The Identity Matrix 172 The Matrix Stacks 174 A Nuclear Example 175
Using Projections 178 Orthographic Projections 179 Perspective Projections 180 A Far-Out Example 183
VIII OpenGL Super Bible
Advanced Matrix Manipulation 186 Loading a Matrix 188 Performing Your Own Transformations 189 Adding Transformations Together 192
Moving Around in OpenGL Using Cameras and Actors 193 An Actor Frame 194 Euler Angles: "Use the Frame, Luke!" 195 Camera Management 196
Bringing It All Together 196 Summary 203 Reference 204
5 Color, Materials, and Lighting: The Basics 211
What Is Color? 212 Light as a Wave 212 Light as a Particle 212 Your Personal Photon Detector 214 The Computer as a Photon Generator 214
PC Color Hardware 215 PC Display Modes 216
Screen Resolution 217 Color Depth 217
Using Color in OpenGL 218 The Color Cube 218 Setting the Drawing Color 220 Shading 221 Setting the Shading Model 223
Color in the Real World 224 Ambient Light 225 Diffuse Light 225 Specular Light 226 Putting It All Together 226
Materials in the Real World 227 Material Properties 227 Adding Light to Materials 227 Calculating Ambient Light Effects 228 Diffuse and Specular Effects 228
Adding Light to a Scene 229 Enabling the Lighting 229 Setting Up the Lighting Model 229 Setting Material Properties 230
Contents ix
Using a Light Source 234 Which Way Is Up? 234 Surface Normals 235 Specifying a Normal 235 Unit Normals 237 Finding a Normal 238 Setting Up a Source 240 Setting the Material Properties 241 Specifying the Polygons 242
Lighting Effects 244 Specular Highlights 244 Specular Light 244 Specular Reflectance 245 Specular Exponent 246 Normal Averaging 248
Putting It All Together 250 Creating a Spotlight 251 Drawing a Spotlight 253
Shadows 258 What Is a Shadow? 258 Squish Code 259 A Shadow Example 261 Sphere World Revisited 264
Summary 265 Reference 266
6 More on Colors and Materials 275
Blending 275 Combining Colors 276 Changing the Blending Equation 279 Antialiasing 280
Fog 286 Accumulation Buffer 289 Other Color Operations 292
Color Masking 292 Color Logical Operations 292 Alpha Testing 293 Dithering 294
Summary 294 Reference 295
x OpenGL Super Bible
7 Imaging with OpenGL 301
Bitmaps 301 A Bitmapped Sample 303
Pixel Packing 309 Pixmaps 310
Packed Pixel Formats 311 A More Colorful Example 312 Moving Pixels Around 316 Saving Pixels 317
More Fun with Pixels 320 Pixel Zoom 327 Pixel Transfer 329 Pixel Mapping 333
The Imaging "Subset" 335 Imaging Pipeline 340
Summary 353 Reference 353
8 Texture Mapping: The Basics 375
Loading Textures 376 Using the Color Buffer 379 Updating Textures 379
Mapping Textures to Geometry 380 Texture Matrix 382
A Simple 2D Example 383 Texture Environment 390 Texture Parameters 392
Basic Filtering 392 Texture Wrap 394 Cartoons with Texture 395 Mipmapping 400
Texture Objects 405 Managing Multiple Textures 406
Summary 416 Reference 416
9 Texture Mapping: Beyond the Basics 435
Secondary Color 435 Anisotropic Filtering 438
Contents xi
Texture Compression 440 Compressing Textures 441 Loading Compressed Textures 442
Texture Coordinate Generation 443 Object Linear Mapping 450 Eye Linear Mapping 452 Sphere Mapping 452 Cube Mapping 453
Multitexture 457 Multiple Texture Coordinates 459 A Multitextured Example 459
Texture Combiners 465 Summary 467 Reference 467
10 Curves and Surfaces 477
Built-in Surfaces 478 Setting Quadric States 478 Drawing Quadrics 480 Modeling with Quadrics 483
Bezier Curves and Surfaces 487 Parametric Representation 488 Evaluators 491
NURBS 501 From Bezier to B-Splines 501 Knots 502 Creating a NURBS Surface 503 NURBS Properties 504 Defining the Surface 504 Trimming 505 NURBS Curves 508
Tessellation 508 The Tessellator 509 Tessellator Callbacks 510 Specifying Vertex Data 512 Putting It All Together 512
Summary 518 Reference 519
Super Bible
11 It's All About the Pipeline: Faster Geometry Throughput 547
Model Assembly 101 548 Pieces and Parts 548
Display Lists 563 Batch Processing 563 Preprocessed Batches 564 Display List Caveats 566 Converting to Display Lists 566
Measuring Performance 567 A Better Example 569
Vertex Arrays 574 Loading the Geometry 578 Enabling Arrays 579 Where's the Data? 580 Draw! 581 Indexed Vertex Arrays 582
Summary 596 Reference 596
12 Interactive Graphics 611
Selection 612 Naming Your Primitives 612 Working with Selection Mode 614 The Selection Buffer 615 Picking 617 Hierarchical Picking 620 Feedback 625 The Feedback Buffer 625 Feedback Data 625 Passthrough Markers 627
A Feedback Example 627 Label the Objects for Feedback 627 Step 1: Select the Object 630 Step 2: Get Feedback on the Object 632
Summary 634 Reference 634
Contents xiii
Part II OpenGL Everywhere
13 Wigg le : OpenGL on Windows 643
OpenGL Implementations on Windows 644 Generic OpenGL 644 Installable Client Driver 644 Mini-Client Driver 645 Mini-Driver 645 Extended OpenGL 646
Basic Windows Rendering 647 GDI Device Contexts 648 Pixel Formats 650 The OpenGL Rendering Context 657
Putting It All Together 658 Creating the Window 658 Using the OpenGL Rendering Context 663 Other Windows Messages 667
Windows Palettes 669 Color Matching 669 Palette Arbitration 670 Creating a Palette for OpenGL 672 Palette Creation and Disposal 677
OpenGL and Windows Fonts 679 3D Fonts and Text 679 2D Fonts and Text 682
Full-Screen Rendering 685 Creating a Frameless Window 685 Creating a Full-Screen Window 686
Multithreaded Rendering 689 OpenGL and WGL Extensions 690
Simple Extensions 691 Using New Entrypoints 692 WGL Extensions 693
Summary 725 Reference 725
14 OpenGL on MacOS X 743
The Basics 743 Frameworks 744 Using the GLUT API 744
xiv OpenGL Super Bible
Using the AGL and Carbon APIs 744 Pixel Formats 745 Managing Contexts 747 Doing Double-Buffered Rendering 747 Your First AGL Program 747 Using Bitmap Fonts 760
Using the Cocoa API 775 The NSOpenGL Class 775 Your First Cocoa Program 778
Summary 788 Reference 788
15 GLX: OpenGL on Linux 793
The Basics 793 Using the OpenGL and Xl l Libraries 793 Using the GLUT Library 796 OpenGL on Linux 796 OpenGL Emulation: Mesa 797
The OpenGL Extension for the X Window System 797 X Window System Basics 798 Choosing a Visual 798 Managing OpenGL Contexts 800 Creating an OpenGL Window 800 Double-Buffered Windows 801 Putting It All Together 801
Creating Bitmap Fonts for OpenGL 812 Offscreen Rendering 825
Using GLX Pixmaps 825 Using Pbuffers 832
Using the Motif Library 839 GLwDrawingArea and GLwMDrawingArea: The OpenGL
Widgets 840 Callbacks 841 Functions 842 Putting It All Together 843
Summary 855 Reference 855
Contents xv
Part III OpenGL: The Next Generation
16 Buffer Objects: It's Your Video Memory; You Manage It! 865
First, You Need Vertex Arrays 867 Generating the Spherical Particle Clouds 867 Enabling the Vertex Arrays 869 More Spheres, Please! 869
Migration to Buffer Objects 872 Buffer Object Management 874 Rendering with Buffer Objects 874
Loading Data into Buffer Objects 874 Copying Data into the Buffer Object 875 Mapping the Buffer Object Directly 876
A Few Loose Ends 881 Summary 882 Reference 882
17 Occlusion Queries: Why Do More Work Than You Need To? 891
The World Before Occlusion Queries 892 Bounding Boxes 895 Querying the Query Object 900 Summary 903 Reference 903
18 Depth Textures and Shadows 907
Be That Light 908 Fit the Scene to the Window 909 No Bells or Whistles, Please 909
A New Kind of Texture 913 Draw the Shadows First?! 913 And Then There Was Light 914
Projecting Your Shadow Map: The "Why" 915 Projecting Your Shadow Map: The "How" 916 The Shadow Comparison 918
Two Out of Three Ain't Bad 925 A Few Words About Polygon Offset 926 Summary 927 Reference 928
xvi OpenGL Super Bible
19 Programmable Pipeline: This Isn't Your Father's OpenGL 931
Out with the Old 932 Fixed Vertex Processing 932 Fixed Fragment Processing 935
In with the New 936 Programmable Vertex Shaders 936 Fixed Functionality Glue 938 Programmable Fragment Shaders 939
Introduction to Shader Extensions 940 Low-Level Extensions 940 High-Level Extensions 943
Summary 944
20 Low-Level Shading: Coding to the Metal 945
Managing Low-Level Shaders 946 Creating and Binding Shaders 946 Loading Shaders 946 Deleting Shaders 948 Setting Up the Extensions 948
Instruction Sets 950 Common Instructions 950 Vertex-Specific Instructions 952 Fragment-Specific Instructions 952
Variable Types 954 Temporaries 954 Parameters 954 Attributes 957 Outputs 959 Aliases 960 Addresses 961
Input and Output Modifiers 961 Input Negate 961 Input Swizzle 961 Output Writemask 962 Output Clamp 962
Resource Consumption and Queries 963 Parser Limits 963 Native Limits 964 Other Queries 966
Contents xvi
Shader Options 966 Position-Invariant Vertex Option 966 Fog Application Fragment Options 966 Precision Hint Fragment Option 967
Summary 967 Reference 967
21 High-Level Shading: The Real Slim Shader 981
Managing High-Level Shaders 982 Shader Objects 982 Program Objects 984 Setting Up the Extensions 986
Variables 987 Basic Types 988 Structures 988 Arrays 989 Qualifiers 990 Built-in Variables 990
Expressions 991 Operators 992 Array Access 992 Constructors 992 Component Selectors 994
Control Flow 995 Loops 995 If/else 995 Discard 996 Functions 996
Summary 999 Reference 999
22 Vertex Shading: Do-It-Yourself Transform, Lighting, and Texgen 1015
Getting Your Feet Wet 1015 Diffuse Lighting 1018 Specular Lighting 1021 Improved Specular Lighting 1024 Per-Vertex Fog 1032 Per-Vertex Point Size 1037 Customized Vertex Transformation 1040 Vertex Blending 1043 Summary 1049
23 Fragment Shading: Empower Your Pixel Processing 1051
Color Conversion 1052 Grayscale 1052 Sepia Tone 1053 Inversion 1055 Heat Signature 1056 Per-Fragment Fog 1058
Image Processing 1061 Blur 1062 Sharpen 1065 Dilation and Erosion 1067 Edge Detection 1070
Lighting 1072 Diffuse Lighting 1073 Multiple Specular Lights 1077
Procedural Texture Mapping 1083 Checkerboard Texture 1084 Beach Ball Texture 1091 Toy Ball Texture 1096
Summary 1103
A Further Reading 1105
Other Good OpenGL Books 1105 3D Graphics Books 1105 WebSites 1106
В Glossary 1107
С OpenGL ES 1113
Reduction of Data Types 1113 Totally Gone 1114 Greatly Reduced Functionality 1115
Texture Mapping 1115 Raster Operations 1115 Lighting 1115
Conclusion 1116
Index 1117