Programming with OpenGL: Advanced Rendering
Advanced Graphics Programming Techniques Using OpenGL
Organizer:
Tom McReynolds
Silicon Graphics
Copyright
c
1998 by Tom McReynolds and David Blythe.
All rights reserved
April 26, 1998
SIGGRAPH ‘98 Course
Abstract
This advanced course demonstrates sophisticated and novel computer graphics programming
techniques, implemented in C using the widely available OpenGL library.
By explaining the concepts and demonstrating the techniques required to generate images of
greater realism and utility, the course helps students achieve two goals: they gain a deeper in-
sight into OpenGL functionality and computer graphics concepts, while expanding their “tool-
box” of useful OpenGL techniques.
i
Programming with OpenGL: Advanced Rendering
Speakers
David Blythe
David Blytheis a Principal Engineer with the Advanced Graphics Software groupat Silicon Graph-
ics. David joined SGI in 1991 and has contributedto the development of RealityEngineandInfinite-
Reality graphics. He has worked extensively on implementations of the OpenGL graphics library
and OpenGL extension specifications. David is currently working on high-level toolkits which are
built on top of OpenGL as well as contributing to the continuing evolution of OpenGL.
Prior to joining SGI, David was a visualization scientist at the Ontario Centre for Large Scale Com-
putation. David received both a B.S. and M.S. degree in Computer Science from the University of
Toronto.
Email:
Brad Grantham
Brad Grantham currently contributes to the design and implementation of Silicon Graphics’ high-
levelgraphics toolkits,includingthe Fahrenheit Scene Graph, a collaborativeprojectwithMicrosoft
and Hewlett-Packard. Brad previously worked on OpenGL Optimizer, Cosmo 3D, and IRIS Per-
former.
Before joining SGI, Brad wrote UNIX kernel code and imaging codecs. He received a Computer
Science B.S. degree from Virginia Tech in 1992, and his previousclaimto fame was MacBSD, BSD
UNIX for the Macintosh.
Email:
Tom McReynolds
Tom McReynolds is a software engineer in the Core Rendering group at Silicon Graphics. He’s
implementedOpenGLextensions,doneOpenGLperformancework,andworkedonIRIS Performer,
a real-time visualization library that uses OpenGL.
Prior to SGI, he worked at Sun Microsystems, where he developed graphics hardware support soft-
ware and graphics libraries, including XGL.
Tom is also an adjunct professor at Santa Clara University, where he teaches courses in computer
graphics using the OpenGL library. He has also presented at the X Technical Conference, SIG-
GRAPH ’96 and ’97, SGI’s 1996 Developer Forum, and at SGI’s 1997 OpenGLDeveloper’s Work-
shop.
Email:
ii
Programming with OpenGL: Advanced Rendering
Scott R. Nelson
Scott R. Nelson is a senior staff engineer in the High Performance Graphics group at Sun Microsys-
tems. He works in the development of new graphics accelerator architectures and contributed to the
development of the GT, ZX, and Elite3D graphics accelerators.
Before joining Sun in 1988, Scott spent eightyears at Evans& Sutherland developinggraphics hard-
ware. He received a B.S. degree in Computer Science from the University of Utah.
Email:
Other Contributers
Celeste Fowler (Author)
Celeste Fowler is a software engineer in the Advanced Systems Division at Silicon Graphics. She
worked on the OpenGL imaging pipelinefor the InfiniteReality graphics systemand on the OpenGL
display list implementation for InfiniteReality and RealityEngine.
Before coming to SGI, Celeste attended Princeton University where she did research on radiosity
techniques and TA’d courses in computer graphics and programming systems.
Email:
Simon Hui (Author)
Simon Hui is a software engineer at 3Dfx Interactive, Inc. He currently works on OpenGLand other
graphics libraries for PC and consumer platforms.
Priortojoining3Dfx, Simon workedon IRIS Performer, a realtime graphics toolkit,in the Advanced
Systems Divisionat Silicon Graphics. He has also worked on OpenGL implementations for the Re-
alityEngineand InfiniteReality. Simon received a B.A. in Computer Science from the University of
California at Berkeley.
Email:
Paula Womack (Author)
Paula Womack is a softwareengineerinthe AdvancedSystemsDivisionat SiliconGraphics. Shehas
managed the OpenGL group at Silicon Graphics, and was also a member of the OpenGL Architec-
tural Review Board (the OpenGL ARB) which is responsible for defining and enhancing OpenGL.
Prior to joining Silicon Graphics, Paula worked on OpenGL at Kubota and Digital Equipment. She
has a B.S. in Computer Engineering from the University of California at San Diego.
Email:
iii
Programming with OpenGL: Advanced Rendering
Linda Rae Sande (Production Editor)
Linda Rae Sande is a production editor in Technical Publications at Silicon Graphics. A graduate
of Northern Arizona University (B.S. in Physics-Astronomy), she has taught college algrebra and
physical science courses and worked in marketing communications and technical training. As co-
authorof two physics laboratory textbooksand author of several productionmanuals, LindaRae has
many years of experience in book production and production coordination.
Prior to SGI, she was a production coordinator at ESL-TRW responsible for the TravInfo and Tran-
sCal transportation project documentation and deliverables.
Email:
Dany Galgani (Illustrator)
Dany Galgani has provided illustrations to Technical Publications at Silicon Graphics for over 9
years. He has illustrated hardware and software manuals, from user’s guides to programmer’s man-
uals.
Before that, he did commercial art for advertising agencies and book publishers,including illustrat-
ing books in Ortho’s “Do-It-Yourself” series.
Dany received his degree in the Arts from the University of Paris as well as a CPA.
Email:
iv
Programming with OpenGL: Advanced Rendering
Course Syllabus
8:30 A Introduction (McReynolds)
8:35 B Visual Simulation (McReynolds)
1. Tiling Large Textures
2. Anisotropic Texturing
3. Developing LOD Models for Geometry
4. Billboarding
5. Light Points
9:20 C Adding Realism (Blythe and McReynolds)
9:20 Object Realism (Blythe)
1. Phong Shading
2. Bump Mapping with Textures
3. Complex BDRFs Using Multiple Phong Lights
10:00 Break
10:15 Interobject Realism (McReynolds)
4. Shadows
5. Reflections and Refractions
6. Transparency
11:00 D Image Processing (Grantham)
1. OpenGL Image Processing
2. Image Warping with Textures
3. Accumulation Buffer Convolution
4. Antialiasing with Accumulation Buffer
5. Texture Synthesis and Procedural Texturing
v
Programming with OpenGL: Advanced Rendering
12:00 Lunch
1:30 E CAD (Nelson)
1. Constructive Solid Geometry
2. Meshing and Tessellation
3. Numerical Instabilities and Their Cure
4. Antialiasing Geometry
2:15 F Scientific Visualization (Blythe)
1. Volume Rendering
2. Textures as Multidimensional Functions
3. Visualizing Flow Fields (line integral convolution)
3:00 Break
3:15 G Graphics Special Effects (Grantham)
1. Stencil Dissolves
2. Color Space Operations
3. Photographic Techniques (depth of field, motion blur)
4. Compositing
4:00 H Simulating Natural Phenomena (McReynolds)
1. Smoke
2. Fire
3. Clouds
4. Water
5. Fog
5:00 I Summary, Questions and Answers (variable) All
vi
Programming with OpenGL: Advanced Rendering
Contents
1 Introduction 1
1.1 OpenGLVersion ................................... 1
1.2 CourseNotesandSlideSetOrganization....................... 1
1.3 Acknowledgments .................................. 2
1.4 Acknowledgments for 1997 Course Notes ...................... 2
1.5 CourseNotesWebSite ................................ 3
2 About OpenGL 4
3 Modeling 5
3.1 ModelingConsiderations............................... 5
3.2 Decomposition and Tessellation ........................... 7
3.3 GeneratingModelNormals.............................. 8
3.3.1 ConsistentVertexWinding.......................... 11
3.3.2 SmoothShading ............................... 12
3.4 Triangle-stripping................................... 13
3.4.1 GreedyTri-stripping ............................. 15
3.5 CappingClippedSolidswiththeStencilBuffer ................... 15
3.6 ConstructiveSolidGeometrywiththeStencilBuffer ................ 16
4 Geometry and Transformations 25
4.1 StereoViewing .................................... 25
4.1.1 FusionDistance................................ 25
4.1.2 ComputingtheTransforms.......................... 26
4.2 DepthofField..................................... 28
4.3 TheZCoordinateandPerspectiveProjection .................... 28
4.3.1 DepthBuffering ............................... 30
4.4 Image Tiling ...................................... 32
4.5 Moving the Current Raster Position ......................... 34
4.6 PreventingClippingofWideLinesandPoints.................... 34
4.7 DistortionCorrection................................. 35
5 Texture Mapping 39
5.1 Review ........................................ 39
5.1.1 Filtering .................................... 39
5.1.2 TextureEnvironment............................. 40
5.2 MipmapGeneration.................................. 41
vii
Programming with OpenGL: Advanced Rendering
5.3 TextureMapLimits.................................. 43
5.4 Anisotropic Texture Filtering . ............................ 44
5.5 PagingTextures.................................... 47
5.5.1 TextureSubloading.............................. 48
5.5.2 PagingImagesinSystemMemory...................... 49
5.6 TransparencyMappingandTrimmingwithAlpha.................. 50
5.7 Billboards....................................... 51
5.8 RenderingText .................................... 53
5.9 TextureMosaicing .................................. 53
5.10TextureCoordinateGeneration............................ 54
5.11ColorCodingandContouring ............................ 54
5.12 Annotating Metrics .................................. 55
5.13ProjectiveTextures .................................. 55
5.13.1 HowtoProjectaTexture........................... 56
5.14EnvironmentMapping ................................ 58
5.15ImageWarpingandDewarping............................ 58
5.163DTextures...................................... 59
5.16.1 Using3DTextures .............................. 59
5.16.2 3DTexturestoRenderSolidMaterials.................... 60
5.16.3 3D Textures as Multidimensional Functions ................. 60
5.17 Line Integral Convolution (LIC) with Texture . . . ................. 61
5.17.1 Sampling ................................... 62
5.17.2 Using OpenGL to Create Line Integral Convolution (LIC) Images ..... 63
5.17.3 Line Integral Convolution Procedure ..................... 64
5.17.4 Details .................................... 64
5.17.5 MaximizingContrast............................. 65
5.17.6 GoingFarther................................. 65
5.18DetailTextures .................................... 66
5.18.1 SignedIntensityDetailTextures....................... 68
5.18.2 MakingDetailTextures............................ 69
5.19GradualCutawayViews ............................... 69
5.19.1 StepstoGeneratingaCutawayShell..................... 70
5.19.2 Refinements ................................. 72
5.19.3 RenderingaSurfaceTexturedShell ..................... 72
5.19.4 AlphaBufferApproach............................ 72
5.19.5 NoAlphaBufferApproach.......................... 73
5.20ProceduralTextureGeneration............................ 74
5.20.1 Filtered Noise Functions ........................... 74
viii
Programming with OpenGL: Advanced Rendering
5.20.2 GeneratingNoiseFunctions ......................... 74
5.20.3 High Resolution Filtering .......................... 75
5.20.4 SpectralSynthesis .............................. 76
5.20.5 OtherNoiseFunctions ............................ 77
5.20.6 Turbulence .................................. 77
5.20.7 Example:ImageWarping .......................... 78
5.20.8 Generating3DNoise............................. 78
5.20.9 Generating2DNoisetoSimulate3DNoise................. 79
5.20.10Trade-offsBetween3Dand2DTechniques................. 79
6 Blending 80
6.1 Compositing ...................................... 80
6.2 AdvancedBlending.................................. 80
6.3 Painting........................................ 81
6.4 BlendingwiththeAccumulationBuffer ....................... 81
6.5 BlendingTransitions ................................. 83
7 Antialiasing 84
7.1 LineandPointAntialiasing.............................. 84
7.2 Polygon Antialiasing ................................. 85
7.3 Multisampling ..................................... 86
7.4 AntialiasingWithTextures .............................. 86
7.5 AntialiasingwithAccumulationBuffer........................ 87
8 Lighting 90
8.1 Phong Shading .................................... 90
8.1.1 Phong Highlights with Texture ........................ 90
8.1.2 ImprovedHighlightShape.......................... 90
8.1.3 SpotlightEffectsusingProjectiveTextures ................. 91
8.1.4 Phong Shading by Adaptive Tessellation . ................. 93
8.2 LightMaps ...................................... 93
8.2.1 2DTextureLightMaps............................ 94
8.2.2 3DTextureLightMaps............................ 96
8.3 OtherLightingModels ................................ 97
8.4 GlobalIllumination.................................. 98
8.5 BumpMappingwithTextures ............................ 99
8.5.1 TangentSpace ................................ 100
8.5.2 Going for Higher Quality ........................... 104
ix
Programming with OpenGL: Advanced Rendering
8.5.3 Blending ................................... 104
8.5.4 WhyDoesThisWork?............................ 104
8.5.5 Limitations.................................. 105
8.6 Choosing Material Properties . ............................ 105
8.6.1 ModelingMaterialType ........................... 105
8.6.2 ModelingMaterialSmoothness ....................... 107
9 Scene Realism 110
9.1 MotionBlur...................................... 110
9.2 DepthofField..................................... 110
9.3 ReflectionsandRefractions.............................. 112
9.3.1 PlanarReflectors............................... 113
9.3.2 SphereMapping ............................... 118
9.4 CreatingShadows................................... 126
9.4.1 ProjectionShadows.............................. 126
9.4.2 ShadowVolumes............................... 128
9.4.3 ShadowMaps................................. 131
9.4.4 Soft Shadows by Jittering Lights ....................... 133
9.4.5 SoftShadowsUsingTextures ........................ 133
10 Transparency 135
10.1Screen-DoorTransparency .............................. 135
10.2AlphaBlending.................................... 135
10.3Sorting......................................... 136
10.4UsingtheAlphaFunction............................... 137
10.5 Using Multisampling ................................. 137
11 Natural Phenomena 139
11.1Smoke......................................... 139
11.2VaporTrails...................................... 140
11.3Fire .......................................... 140
11.4Explosions....................................... 141
11.5 Clouds . . ....................................... 141
11.6Water ......................................... 142
11.7LightPoints...................................... 144
11.8OtherAtmosphericEffects .............................. 144
11.9ParticleSystems.................................... 146
11.9.1 RepresentingParticles ............................ 146
x
Programming with OpenGL: Advanced Rendering
11.9.2 ParticleSizes................................. 147
11.9.3 LargeandSmallPoints............................ 148
11.9.4 Antialiasing.................................. 148
11.9.5 “Fat”Particles ................................ 148
11.9.6 ParticleSystemsinaScene.......................... 149
11.10Precipitation...................................... 149
12 Image Processing 152
12.1 Introduction ...................................... 152
12.1.1 ThePixelTransferPipeline.......................... 152
12.1.2 GeometricDrawingandTexturing...................... 153
12.1.3 TheFramebufferandPer-FragmentOperations............... 153
12.1.4 TheImagingSubsetinOpenGL1.2 ..................... 154
12.2 Colors and Color Spaces . . . ............................ 155
12.2.1 TheAccumulationBuffer:InterpolationandExtrapolation......... 155
12.2.2 PixelScaleandBiasOperations....................... 157
12.2.3 Look-Up Tables . . . ............................ 157
12.2.4 TheColorMatrixExtension ......................... 160
12.3 Convolutions ..................................... 163
12.3.1 Introduction .................................. 163
12.3.2 The Convolution Operation ......................... 163
12.3.3 Convolutions Using the Accumulation Buffer ................ 165
12.3.4 The Convolution Extension ......................... 167
12.3.5 Useful Convolution Filters .......................... 168
12.3.6 CorrelationandFeatureDetection...................... 171
12.4ImageWarping .................................... 172
12.4.1 ThePixelZoomOperation.......................... 172
12.4.2 WarpsUsingTextureMapping........................ 173
13 Volume Visualization with Texture 174
13.1OverviewoftheTechnique.............................. 174
13.23DTextureVolumeRendering ............................ 175
13.32DTextureVolumeRendering ............................ 176
13.4BlendingOperators.................................. 177
13.4.1 Over...................................... 177
13.4.2 Attenuate................................... 178
13.4.3 MaximumIntensityProjection........................ 178
13.4.4 Under..................................... 178
xi
Programming with OpenGL: Advanced Rendering
13.5SamplingFrequency ................................. 178
13.6ShrinkingtheVolumeImage ............................. 179
13.7VirtualizingTextureMemory............................. 180
13.8MixingVolumetricandGeometricObjects...................... 180
13.9TransferFunctions .................................. 180
13.10Volume Cutting Planes ................................ 181
13.11ShadingtheVolume.................................. 181
13.12WarpedVolumes ................................... 182
14 Using the Stencil Buffer 183
14.1DissolveswithStencil................................. 185
14.2DecalingwithStencil................................. 186
14.3FindingDepthComplexitywiththeStencilBuffer.................. 189
14.4 Compositing Images with Depth ........................... 190
15 Line Rendering Techniques 192
15.1WireframeModels .................................. 192
15.2HiddenLines ..................................... 192
15.2.1 glPolygonOffset . . . ............................ 194
15.2.2 glDepthRange ................................ 195
15.3HaloedLines ..................................... 195
15.4 Silhouette Edges ................................... 197
15.5PreventingSmoothWideLineOverlap........................ 198
15.6EndCapsOnWideLines............................... 198
16 Tuning Your OpenGL Application 199
16.1WhatIsPipelineTuning?............................... 199
16.1.1 Three-StageModeloftheGraphicsPipeline................. 199
16.1.2 Finding Bottlenecks in Your Application . ................. 200
16.2OptimizingYourApplicationCode.......................... 201
16.2.1 OptimizeCacheandMemoryUsage..................... 201
16.2.2 StoreDatainaFormatThatisEfficientforRendering ........... 202
16.2.3 Per-PlatformTuning ............................. 203
16.3TuningtheGeometrySubsystem........................... 204
16.3.1 UseExpensiveModesEfficiently ...................... 204
16.3.2 OptimizingTransformations......................... 204
16.3.3 OptimizingLightingPerformance ...................... 205
16.3.4 AdvancedGeometry-LimitedTuningTechniques.............. 207
xii