Table Of ContentAdvanced Graphics
Programming
Using OpenGL
TOM McREYNOLDS
DAVID BLYTHE
MORGAN KAUFMANN
ELSEVIER
Advanced Graphics Programming Using OpenGL
The Morgan Kaufmann Series in Computer Graphics and Geometric Modeling
SeriesEditor:BrianA.BarskyUniversityofCalifornia,Berkeley
Thisseriespublishesthefinestworksfortheaccomplishedandaspiringgraphicsprofessional.Theseriesincludesintermediate
andadvancedtextbooks,graphicsprogrammingbooks,surveysofimportantnewareasandmethods,andreferenceworks.
AdvancedGraphicsProgrammingUsingOpenGL TheComputerAnimator’sTechnicalHandbook
TomMcReynoldsandDavidBlythe LynnPocockandJudsonRosebush
DigitalGeometryGeometricMethodsforDigital AdvancedRenderMan:
PictureAnalysis CreatingCGIforMotionPictures
RienhardKletteandAzrielRosenfeld AnthonyA.ApodacaandLarryGritz
DigitalVideoandHDTVAlgorithmsandInterfaces CurvesandSurfacesinGeometricModeling:
CharlesPoynton TheoryandAlgorithms
JeanGallier
Real-TimeShaderProgramming
RonFosner AndrewGlassner’sNotebook:
RecreationalComputerGraphics
CompleteMayaProgramming:
AndrewS.Glassner
AnExtensiveGuidetoMELandtheC++API
DavidGould WarpingandMorphingofGraphicalObjects
JonasGomes,LuciaDarsa,BrunoCosta,andLuizVelho
MELScriptingforMayaAnimators
MarkR.WilkinsandChrisKazmier JimBlinn’sCorner:DirtyPixels
JimBlinn
DigitalVideoandHDTVAlgorithmsandInterfaces
CharlesPoynton RenderingwithRadiance:
TheArtandScienceofLightingVisualization
Texturing&Modeling:
GregWardLarsonandRobShakespeare
AProceduralApproach,ThirdEdition
DavidS.Ebert,F.KentonMusgrave,DarwynPeachey, IntroductiontoImplicitSurfaces
KenPerlin,andStevenWorley EditedbyJulesBloomenthal
GeometricToolsforComputerGraphics JimBlinn’sCorner:
PhilipSchneiderandDavidEberly ATripDowntheGraphicsPipeline
JimBlinn
UnderstandingVirtualReality:
Interface,Application,andDesign InteractiveCurvesandSurfaces:
WilliamShermanandAlanCraig AMultimediaTutorialonCAGD
AlynRockwoodandPeterChambers
JimBlinn’sCorner:Notation,Notation,Notation
JimBlinn WaveletsforComputerGraphics:
TheoryandApplications
LevelofDetailfor3DGraphics:
EricJ.Stollnitz,TonyD.DeRose,andDavidH.Salesin
DavidLuebke,MartinReddy,JonathanD.Cohen,
AmitabhVarshney,BenjaminWatson,and PrinciplesofDigitalImageSynthesis
RobertHuebner AndrewS.Glassner
PyramidAlgorithms:ADynamicProgramming Radiosity&GlobalIllumination
ApproachtoCurvesandSurfacesforGeometric FrançoisX.SillionandClaudePuech
Modeling
Knotty:AB-SplineVisualizationProgram
RonGoldman
JonathanYen
Non-PhotorealisticComputerGraphics:
UserInterfaceManagementSystems:
Modeling,Rendering,andAnimation
ModelsandAlgorithms
ThomasStrothotteandStefanSchlechtweg
DanR.Olsen,Jr.
CurvesandSurfacesforCAGD:APracticalGuide,
MakingThemMove:Mechanics,Control,andAnimation
FifthEdition
ofArticulatedFigures
GeraldFarin
EditedbyNormanI.Badler,BrianA.Barsky,and
SubdivisionMethodsforGeometricDesign: DavidZeltzer
AConstructiveApproach
GeometricandSolidModeling:AnIntroduction
JoeWarrenandHenrikWeimer
ChristophM.Hoffmann
ComputerAnimation:AlgorithmsandTechniques
AnIntroductiontoSplinesforUseinComputerGraphics
RickParent
andGeometricModeling
RichardH.Bartels,JohnC.Beatty,andBrianA.Barsky
Advanced Graphics
Programming
Using OpenGL
TOM McREYNOLDS
DAVID BLYTHE
AMSTERDAM•BOSTON•HEIDELBERG•LONDON
NEWYORK•OXFORD•PARIS•SANDIEGO
SANFRANCISCO•SINGAPORE•SYDNEY•TOKYO
MORGANKAUFMANNPUBLISHERSISANIMPRINTOFELSEVIER
PublishingDirector: DianeCerra
PublishingServicesManager: SimonCrump
ProjectManager: BrandyLilly
EditorialCoordinator: MonaBuehler
CoverDesign: Dutton&ShermanDesign
TextDesign: JulioEsperas
Composition: CephaImagingPvt.Ltd.
Illustrations: DartmouthPublishing,Inc.
Copyeditor: DarilBentley;GraphicWorld
Proofreader: GraphicWorld
Indexer: GraphicWorld
Interiorprinter: ChinaTranslation&PrintingServices,Ltd.
Coverprinter: ChinaTranalation&PrintingServices,Ltd.
MorganKaufmannPublishersisanimprintofElsevier.
500SansomeStreet,Suite400,SanFrancisco,CA94111
Thisbookisprintedonacid-freepaper.
©2005byElsevierInc.Allrightsreserved.
Designationsusedbycompaniestodistinguishtheirproductsareoftenclaimedastrademarksor
registeredtrademarks.InallinstancesinwhichMorganKaufmannPublishersisawareofaclaim,
theproductnamesappearininitialcapitalorallcapitalletters.Readers,however,shouldcontact
theappropriatecompaniesformorecompleteinformationregardingtrademarksandregistration.
Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmittedinanyform
orbyanymeans—electronic,mechanical,photocopying,scanning,orotherwise—withoutpriorwritten
permissionofthepublisher.
PermissionsmaybesoughtdirectlyfromElsevier’sScience&TechnologyRightsDepartmentinOxford,
UK:phone:(+44)1865843830,fax:(+44)1865853333,e-mail:permissions@elsevier.com.uk.You
mayalsocompleteyourrequeston-lineviatheElsevierhomepage(http://elsevier.com)byselecting
“CustomerSupport”andthen“ObtainingPermissions.”
LibraryofCongressCataloging-in-PublicationData
ApplicationSubmitted
ISBN:1-55860-659-9
ForinformationonallMorganKaufmannpublications,
visitourWebsiteatwww.mkp.comorwww.books.elsevier.com
PrintedinChina
10 9 8 7 6 5 4 3 2 1
TomyfriendsandcolleaguesfromSiliconGraphics;itwasafabuloustimeandplaceto
learnabout3Dgraphics.–DB
ToEdeForneyandHarryMcGinnis;youweretherewhenitcounted.–TM
Contents
Preface
xxiii
Acknowledgments
xxvii
Biographies
xxviii
PART I
Concepts
1
CHAPTER 1
Geometry Representation and Modeling
3
1.1 Polygonal Representation 3
1.2 Decomposition and Tessellation 4
1.3 Shading Normals 8
1.3.1 SmoothShading 9
1.3.2 VertexWindingOrder 11
1.4 Triangle Stripping 12
1.4.1 GreedyTri-stripping 13
1.5 Vertices and Vertex Arrays 14
1.5.1 VertexBufferObjects 15
1.5.2 TriangleLists 16
1.6 Modeling vs. Rendering Revisited 17
vi
Contents vii
CHAPTER 2
3D Transformations
19
2.1 Data Representation 19
2.2 Overview of the Transformation Pipeline 20
2.2.1 ObjectSpaceandtheModelviewTransform 20
2.2.2 EyeSpaceandProjectionTransform 21
2.2.3 ClipSpaceandPerspectiveDivide 22
2.2.4 NDCSpaceandtheViewportTransform 22
2.2.5 WindowSpace 23
2.3 Normal Transformation 23
2.4 Texture Coordinate Generation and Transformation 25
2.4.1 TextureMatrix 25
2.4.2 TextureCoordinateGeneration 25
2.5 Modeling Transforms 27
2.6 Visualizing Transform Sequences 28
2.7 Projection Transform 30
2.8 The Z Coordinate and Perspective Projection 30
2.8.1 ZCoordinatesandFog 32
2.9 Vertex Programs 32
2.10 Summary 34
CHAPTER 3
Color, Shading, and Lighting
35
3.1 Representing Color 35
3.1.1 Resolution and Dynamic Range 36
3.1.2 Gamma 37
3.1.3 Alpha 39
3.1.4 Color Index 39
3.2 Shading 40
3.3 Lighting 43
3.3.1 Intensities,Colors,andMaterials 46
3.3.2 LightSourceProperties 47
viii Contents
3.3.3 MaterialProperties 49
3.3.4 VertexandFragmentLighting 50
3.4 Fixed-Point and Floating-Point Arithmetic 53
3.4.1 BiasedArithmetic 54
3.5 Summary 56
CHAPTER 4
Digital Images and Image Manipulation
57
4.1 Image Representation 57
4.2 Digital Filtering 60
4.3 Convolution 62
4.4 Images in OpenGL 63
4.5 Positioning Images 65
4.6 Pixel Store Operations 65
4.7 Pixel Transfer Operations 67
4.7.1 ScaleandBias 67
4.7.2 PixelMappingOperations 67
4.8 ARB Imaging Subset 68
4.8.1 Convolution 68
4.8.2 ColorMatrixTransform 68
4.8.3 Histogram 69
4.8.4 MinMax 70
4.8.5 ColorTables 70
4.8.6 BlendEquationandConstantColorBlending 71
4.9 Off-Screen Processing 72
4.10 Summary 72
CHAPTER 5
Texture Mapping
73
5.1 Loading Texture Images 73
5.1.1 TextureBorders 74
5.1.2 InternalTextureFormats 75
Contents ix
5.1.3 CompressedTextures 76
5.1.4 ProxyTextures 77
5.2 Texture Coordinates 77
5.2.1 TextureCoordinateGenerationandTransformation 79
5.3 Loading Texture Images from the Frame Buffer 79
5.4 Environment Mapping 80
5.4.1 GeneratingEnvironmentMapTextureCoordinates 81
5.4.2 TextureMapsUsedinEnvironmentMapping 82
5.4.3 CubeMapping 83
5.4.4 SphereMapping 85
5.5 3D Texture 88
5.5.1 Using3DTexturestoRenderSolidMaterials 89
5.6 Filtering 90
5.7 Additional Control of Texture Level of Detail 91
5.8 Texture Objects 93
5.9 Multitexture 95
5.9.1 MultitextureModel 96
5.9.2 MultitextureTextureEnvironments 97
5.10 Texture Environment 98
5.10.1 AdvancedTextureEnvironmentFunctionality 99
5.10.2 FragmentPrograms 100
5.11 Summary 102
CHAPTER 6
Rasterization and Fragment Processing
103
6.1 Rasterization 104
6.1.1 RasterizationConsistency 105
6.1.2 Z-Fighting 105
6.1.3 BitmapsandPixelRectangles 107
6.1.4 Texture,Color,andDepthInterpolation 108
6.1.5 wBuffering 109
6.2 Fragment Operations 110
6.2.1 MultisampleOperations 111
6.2.2 AlphaTest 111
6.2.3 StencilTest 111