OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL...

15
OpenGL® Programming Guide Sixth Edition The Official Guide to Learning OpenGL ® , Version 2.1 OpenGL Architecture Review Board Dave Shreiner Mason Woo Jackie Neider Tom Davis :Addison-Wesley Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City

Transcript of OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL...

Page 1: OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL Code 5 OpenGL Command Syntax 7 OpenGL as a State Machine 9 OpenGL Rendering Pipeline

OpenGL®Programming GuideSixth Edition

The Official Guide toLearning OpenGL®, Version 2.1

OpenGL Architecture Review BoardDave Shreiner Mason WooJackie Neider Tom Davis

:Addison-WesleyUpper Saddle River, NJ • Boston • Indianapolis • San FranciscoNew York • Toronto • Montreal • London • Munich • Paris • MadridCapetown • Sydney • Tokyo • Singapore • Mexico City

Page 2: OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL Code 5 OpenGL Command Syntax 7 OpenGL as a State Machine 9 OpenGL Rendering Pipeline

Contents

Figures xxi

Tables xxv

Examples xxix

About This Guide xxxv

What This Guide Contains xxxv

What's New in This Edition xxxviii

What You Should Know Before Reading This Guide xxxviii

How to Obtain the Sample Code xxxix

Nate Robins' OpenGL Tutors xl

Errata xl

Style Conventions xl

Acknowledgments xliii

1. Introduction to OpenGL 1

What Is OpenGL? 2

A Smidgen of OpenGL Code 5

OpenGL Command Syntax 7

OpenGL as a State Machine 9

OpenGL Rendering Pipeline 10

Display Lists 11Evaluators 11Per-Vertex Operations 12

Page 3: OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL Code 5 OpenGL Command Syntax 7 OpenGL as a State Machine 9 OpenGL Rendering Pipeline

Primitive Assembly 12

Pixel Operations 12

Texture Assembly 13

Rasterization 13

Fragment Operations 13

OpenGL-Related Libraries 14

Include Files 15

GLUT, the OpenGL Utility Toolkit 16

Animation 20

The Refresh That Pauses 22

Motion = Redraw + Swap 23

2. State Management and Drawing Geometric Objects 27

A Drawing Survival Kit 29

Clearing the Window 30

Specifying a Color 32

Forcing Completion of Drawing 34

Coordinate System Survival Kit 36

Describing Points, Lines, and Polygons 37

What Are Points, Lines, and Polygons? 37

Specifying Vertices 41

OpenGL Geometric Drawing Primitives 42Basic State Management 48

Displaying Points, Lines, and Polygons 50Point Details 50Line Details 51Polygon Details 55

Normal Vectors 63Vertex Arrays 65

Step 1: Enabling Arrays 67Step 2: Specifying Data for the Arrays 68Step 3: Dereferencing and Rendering 71Interleaved Arrays 78

Buffer Objects 82Creating Buffer Objects 82

viii Contents

Page 4: OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL Code 5 OpenGL Command Syntax 7 OpenGL as a State Machine 9 OpenGL Rendering Pipeline

Making a Buffer Object Active 83

Allocating and Initializing Buffer Objects with Data 84

Updating Data Values in Buffer Objects 86

Cleaning Up Buffer Objects 88

Using Buffer Objects with Vertex-Array Data 88

Attribute Groups 91

Some Hints for Building Polygonal Models of Surfaces 94

An Example: Building an Icosahedron 96

3. Viewing 103

Overview: The Camera Analogy 106

A Simple Example: Drawing a Cube 109

General-Purpose Transformation Commands 114

Viewing and Modeling Transformations 117

Thinking about Transformations 117

Modeling Transformations 120

Viewing Transformations 126

Projection Transformations 133

Perspective Projection 133

Orthographic Projection 136

Viewing Volume Clipping 138

Viewport Transformation 138

Defining the Viewport 139

The Transformed Depth Coordinate 141

Troubleshooting Transformations 142

Manipulating the Matrix Stacks 145

The Modelview Matrix Stack 148The Projection Matrix Stack 148

Additional Clipping Planes 149

Examples of Composing Several Transformations 152Building a Solar System 153

Building an Articulated Robot Arm 156

Reversing or Mimicking Transformations 160

Contents ix

Page 5: OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL Code 5 OpenGL Command Syntax 7 OpenGL as a State Machine 9 OpenGL Rendering Pipeline

4. Color 165

Color Perception 166

Computer Color 168

RGBA versus Color-Index Mode 170

RGBA Display Mode 171

Color-Index Display Mode 173

Choosing between RGBA and Color-Index Mode 175

Changing between Display Modes 176

Specifying a Color and a Shading Model 176

Specifying a Color in RGBA Mode 177

Specifying a Color in Color-Index Mode 178

Specifying a Shading Model 179

5. Lighting 183

A Hidden-Surface Removal Survival Kit 185

Real-World and OpenGL Lighting 187

Ambient, Diffuse, Specular, and Emissive Light 188

Material Colors 189

RGB Values for Lights and Materials 189

A Simple Example: Rendering a Lit Sphere 190

Creating Light Sources 194

Color 196

Position and Attenuation 197

Spotlights 199

Multiple Lights 200

Controlling a Light's Position and Direction 201Selecting a Lighting Model 207

Global Ambient Light 208

Local or Infinite Viewpoint 209Two-Sided Lighting 209Secondary Specular Color 210Enabling Lighting 211

Defining Material Properties 211Diffuse and Ambient Reflection 213Specular Reflection 214

Contents

Page 6: OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL Code 5 OpenGL Command Syntax 7 OpenGL as a State Machine 9 OpenGL Rendering Pipeline

Emission 214

Changing Material Properties 215

Color Material Mode 217

The Mathematics of Lighting 220

Material Emission 221

Scaled Global Ambient Light 222

Contributions from Light Sources 222

Putting It All Together 224

Secondary Specular Color 225

Lighting in Color-Index Mode 226

The Mathematics of Color-Index Mode Lighting 227

6. Blending, Antialiasing, Fog, and Polygon Offset 229

Blending 231

The Source and Destination Factors 232

Enabling Blending 235

Combining Pixels Using Blending Equations 235

Sample Uses of Blending 238

A Blending Example 240

Three-Dimensional Blending with the Depth Buffer 243

Antialiasing 247

Antialiasing Points or Lines 249

Antialiasing Geometric Primitives with Multisampling 255

Antialiasing Polygons 259

Fog 261

Using Fog 261

Fog Equations 264

Point Parameters 271

Polygon Offset 274

7. Display Lists 277

Why Use Display Lists? 278

An Example of Using a Display List 279

Display List Design Philosophy 282

Contents xi

Page 7: OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL Code 5 OpenGL Command Syntax 7 OpenGL as a State Machine 9 OpenGL Rendering Pipeline

Creating and Executing a Display List 285

Naming and Creating a Display List 286

What's Stored in a Display List? 287

Executing a Display List 289

Hierarchical Display Lists 290

Managing Display List Indices 291

Executing Multiple Display Lists 292

Managing State Variables with Display Lists 297

Encapsulating Mode Changes 299

8. Drawing Pixels, Bitmaps, Fonts, and Images 301

Bitmaps and Fonts 303

The Current Raster Position 305

Drawing the Bitmap 306

Choosing a Color for the Bitmap 308

Fonts and Display Lists 309

Defining and Using a Complete Font 310

Images 312

Reading, Writing, and Copying Pixel Data 313

Imaging Pipeline 321

Pixel Packing and Unpacking 324

Controlling Pixel-Storage Modes 325

Pixel-Transfer Operations 330

Pixel Mapping 333

Magnifying, Reducing, or Flipping an Image 334

Reading and Drawing Pixel Rectangles 337

The Pixel Rectangle Drawing Process 338

Using Buffer Objects with Pixel Rectangle Data 341

Using Buffer Objects to Transfer Pixel Data 342

Using Buffer Objects to Retrieve Pixel Data 344Tips for Improving Pixel Drawing Rates 345Imaging Subset 346

Color Tables 348Convolutions 353Color Matrix 361Histogram 363Minmax 366

xii Contents

Page 8: OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL Code 5 OpenGL Command Syntax 7 OpenGL as a State Machine 9 OpenGL Rendering Pipeline

9. Texture Mapping 369

An Overview and an Example 375

Steps in Texture Mapping 375

A Sample Program 377

Specifying the Texture 380Texture Proxy 385

Replacing All or Part of a Texture Image 387

One-Dimensional Textures 390

Three-Dimensional Textures 392

Compressed Texture Images 397

Using a Texture's Borders 400

Mipmaps: Multiple Levels of Detail 400

Filtering 411Texture Objects 414

Naming a Texture Object 415

Creating and Using Texture Objects 415

Cleaning Up Texture Objects 418

A Working Set of Resident Textures 419

Texture Functions 421

Assigning Texture Coordinates 425

Computing Appropriate Texture Coordinates 427

Repeating and Clamping Textures 428

Automatic Texture-Coordinate Generation 434

Creating Contours 435

Sphere Map 439

Cube Map Textures 441

Multitexturing 443

Texture Combiner Functions 449

The Interpolation Combiner Function 453

Applying Secondary Color after Texturing 455

Secondary Color When Lighting Is Disabled 455

Secondary Specular Color When Lighting Is Enabled 455Point Sprites 456

The Texture Matrix Stack 457

Depth Textures 459Creating a Shadow Map 460

Generating Texture Coordinates and Rendering 461

Contents xiii

Page 9: OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL Code 5 OpenGL Command Syntax 7 OpenGL as a State Machine 9 OpenGL Rendering Pipeline

10. The Framebuffer 465

Buffers and Their Uses 468

Color Buffers 469

Clearing Buffers 470

Selecting Color Buffers for Writing and Reading 471

Masking Buffers 473

Testing and Operating an Fragments 475

Scissor Test 476

Alpha Test 476

Stencil Test 478

Depth Test 483

Occlusion Query 484

Blending, Dithering, and Logical Operations 487

The Accumulation Buffer 490

Scene Antialiasing 491

Motion Blur 497

Depth of Field 497Soft Shadows 502Jittering 502

11. Tessellators and Quadrics 505

Polygon Tessellation 506Creating a Tessellation Object 508Tessellation Callback Routines 508Tessellation Properties 513Polygon Definition 518Deleting a Tessellation Object 521Tessellation Performance Tips 521Describing GLU Errors 522Backward Compatibility 522

Quadrics: Rendering Spheres, Cylinders, and Disks 523Managing Quadrics Objects 524Controlling Quadrics Attributes 525Quadrics Primitives 527

xiv Contents

Page 10: OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL Code 5 OpenGL Command Syntax 7 OpenGL as a State Machine 9 OpenGL Rendering Pipeline

12. Evaluators and NURBS 533

Prerequisites 535

Evaluators 536One-Dimensional Evaluators 536Two-Dimensional Evaluators 542Using Evaluators for Textures 548

The GLU NURBS Interface 550A Simple NURBS Example 550

Managing a NURBS Object 555

Creating a NURBS Curve or Surface 559Trimming a NURBS Surface 565

13. Selection and Feedback 569

Selection 570

The Basic Steps 571Creating the Name Stack 572

The Hit Record 574

A Selection Example 575

Picking 578

Hints for Writing a Program That Uses Selection 589Feedback 591

The Feedback Array 593

Using Markers in Feedback Mode 594

A Feedback Example 594

14. Now That You Know 599

Error Handling 601

Which Version Am I Using? 603Utility Library Version 604

Window System Extension Versions 605

Extensions to the Standard 605

Extensions to the Standard for Microsoft Windows (WGL) 607

Cheesy Translucency 608

An Easy Fade Effect 608

Contents Xv

Page 11: OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL Code 5 OpenGL Command Syntax 7 OpenGL as a State Machine 9 OpenGL Rendering Pipeline

Object Selection Using the Back Buffer 610

Cheap Image Transformation 611

Displaying Layers 612

Antialiased Characters 613

Drawing Round Points 616

Interpolating Images 616

Making Decals 616

Drawing Filled, Concave Polygons Using the, Stencil Buffer 618

Finding Interference Regions 619

Shadows 621

Hidden-Line Removal 622

Hidden-Line Removal with Polygon Offset 622

Hidden-Line Removal with the Stencil Buffer 623

Texture Mapping Applications 624

Drawing Depth-Buffered Images 625

Dirichlet Domains 625

Life in the Stencil Buffer 627

Alternative Uses for glDrawPixels() and glCopyPixels() 628

15. The OpenGL Shading Language 631

The OpenGL Graphics Pipeline and Programmable Shading 632

Vertex Processing 633

Fragment Processing 635

Using GLSL Shaders 636

A Sample Shader 636

OpenGL / GLSL Interface 637

The OpenGL Shading Language 644Creating Shaders with GLSL 645

The Starting Point 645Deciaring Variables 645Aggregate Types 647Computational Invariance 656Statements 656Functions 660Using OpenGL State Values in GLSL Programs 661

xvi Contents

Page 12: OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL Code 5 OpenGL Command Syntax 7 OpenGL as a State Machine 9 OpenGL Rendering Pipeline

Accessing Texture Maps in Shaders 661Shader Preprocessor 664

Preprocessor Directives 664Macro Definition 665Preprocessor Conditionals 666Compiler Control 666Extension Processing 667Vertex Shader Specifics 668

Fragment Shaders 675

A. Order of Operations 679

Overview 680Geometrie Operations 681

Per-Vertex Operations 681Primitive Assembly 682

Pixel Operations 682

Texture Memory 683Fragment Operations 683Odds and Ends 684

B. State Variables 685

The Query Commands 686

OpenGL State Variables 688

Current Values and Associated Data 690

Vertex Array 691Transformation 697

Coloring 699Lighting 700Rasterization 702

Multisampling 705

Texturing 706Pixel Operations 712

Framebuffer Control 715

Pixels 716Evaluators 722

Contents Xvii

Page 13: OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL Code 5 OpenGL Command Syntax 7 OpenGL as a State Machine 9 OpenGL Rendering Pipeline

Shader Object State 723

Program Object State 724

Vertex Shader State 726

Hints 726

Implementation-Dependent Values 727

Implementation-Dependent Pixel Depths 733

Miscellaneous 733

C. OpenGL and Window Systems 735

Accessing New OpenGL Functions 736

GLX: OpenGL Extension for the X Window System 737

Initialization 738

Controlling Rendering 739

GLX Prototypes 741

AGL: OpenGL Extensions for the Apple Macintosh 744

Initialization 744

Rendering and Contexts 745

Managing an OpenGL Rendering Context 745

On-Screen Rendering 745

Off-Screen Rendering 746

Full-Screen Rendering 746Swapping Buffers 746

Updating the Rendering Buffers 746

Using an Apple Macintosh Font 746

Error Handling 747

AGL Prototypes 747

PGL: OpenGL Extension for IBM OS/2 Warp 749Initialization 749

Controlling Rendering 750PGL Prototypes 751

WGL: OpenGL Extension for Microsoft Windows

95/98/NT/ME/2000/XP 753Initialization 753Controlling Rendering 754WGL Prototypes 755

xviii Contents

Page 14: OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL Code 5 OpenGL Command Syntax 7 OpenGL as a State Machine 9 OpenGL Rendering Pipeline

D. Basics of GLUT: The OpenGL Utility Toolkit 759

Initializing and Creating a Window 760

Handling Window and Input Events 761Loading the Color Map 763Initializing and Drawing Three-Dimensional Objects 763

Managing a Background Process 765Running the Program 765

E. Calculating Normal Vectors 767

Finding Normals for Analytic Surfaces 769

Finding Normals from Polygonal Data 771

F. Homogeneous Coordinates and Transformation Matrices 773

Homogeneous Coordinates 774

Transforming Vertices 774

Transforming Normals 775

Transformation Matrices 775

Translation 776

Scaling 776

Rotation 776

Perspective Projection 777

Orthographic Projection 778

G. Programming Tips 779

OpenGL Correctness Tips 780

OpenGL Performance Tips 782

GLX Tips 784

H. OpenGL Invariance 785

I. Bunt-1n OpenGL Shading Language Variables and Functions 789

Variables 790

Vertex Shader Input Attributes Variables 790

Vertex Shader Special Output Variables 790

Vertex Shader Output Varying Variables 791

Contents xix

Page 15: OpenGL® - CERN · 2014-08-15 · Introduction to OpenGL 1 What Is OpenGL? 2 A Smidgen of OpenGL Code 5 OpenGL Command Syntax 7 OpenGL as a State Machine 9 OpenGL Rendering Pipeline

Built-In Implementation Constants 792

Built-In Uniform State Variables 793

Built-In Functions 802

Angle Conversion and Trigonometric Functions 802

Transcendental Functions 803

Basic Numerical Functions 804

Vector-Operation Functions 805

Matrix Functions 806

Vector-Component Relational Functions 807

Texture Lookup Functions 808

Fragment Processing Functions 813

Noise Functions 813

Glossary 815

Index 837

XX Contents