Table Of Contentptg11539634
OpenGL
®
SuperBible
Sixth Edition
ptg11539634
OpenGL Series
from Addison-Wesley
ptg11539634
Visit informit.com/opengl for a complete list of available products.
The OpenGL graphics system is a software interface to graphics hardware.
(“GL” stands for “Graphics Library”.) It allows you to create interactive programs
that produce color images of moving, three-dimensional objects. With OpenGL,
you can control computer-graphics technology to produce realistic pictures, or
ones that depart from reality in imaginative ways.
The OpenGL Series from Addison-Wesley Professional comprises tutorial and
reference books that help programmers gain a practical understanding of OpenGL
standards, along with the insight needed to unlock OpenGL’s full potential.
Make sure to connect with us!
informit.com/socialconnect
OpenGL
®
SuperBible
Sixth Edition
Comprehensive Tutorial
ptg11539634
and Reference
Graham Sellers
Richard S. Wright, Jr.
Nicholas Haemel
UpperSaddleRiver,NJ•Boston•Indianapolis•SanFrancisco
NewYork•Toronto•Montreal•London•Munich•Paris•Madrid
Capetown•Sydney•Tokyo•Singapore•MexicoCity
Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheir Editor-in-Chief
productsareclaimedastrademarks.Wherethosedesignationsappearinthis MarkL.Taub
book,andthepublisherwasawareofatrademarkclaim,thedesignationshave
beenprintedwithinitialcapitallettersorinallcapitals.
ExecutiveEditor
LauraLewin
Theauthorsandpublisherhavetakencareinthepreparationofthisbook,but
makenoexpressedorimpliedwarrantyofanykindandassumeno
Development
responsibilityforerrorsoromissions.Noliabilityisassumedforincidentalor
Editor
consequentialdamagesinconnectionwithorarisingoutoftheuseofthe
SheriCain
informationorprogramscontainedherein.
ManagingEditor
Thepublisheroffersexcellentdiscountsonthisbookwhenorderedinquantity
JohnFuller
forbulkpurchasesorspecialsales,whichmayincludeelectronicversions
and/orcustomcoversandcontentparticulartoyourbusiness,traininggoals,
marketingfocus,andbrandinginterests.Formoreinformation,pleasecontact: Full-Service
Production
U.S.CorporateandGovernmentSales Manager
(800)382-3419 JulieB.Nahil
corpsales@pearsontechgroup.com
CopyEditor
ForsalesoutsidetheUnitedStates,pleasecontact: TeresaD.Wilson ptg11539634
InternationalSales
international@pearsoned.com Indexer
LarrySweazey
VisitusontheWeb:informit.com/aw
Proofreader
LibraryofCongressCataloging-in-PublicationData AndreaFox
Wright,RichardS.,Jr.,1965-author.
Technical
OpenGLsuperBible:comprehensivetutorialandreference.—Sixthedition/
Reviewers
GrahamSellers,RichardS.Wright,Jr.,NicholasHaemel.
PiersDaniell
pages cm
DanielKoch
Includesbibliographicalreferencesandindex.
DanielRakos
ISBN-13:978-0-321-90294-8(pbk.:alk.paper)
ISBN-10:0-321-90294-7(pbk.:alk.paper)
1.Computergraphics.2.OpenGL. I.Sellers,Graham,author.II.Haemel, EditorialAssistant
Nicholas,author.III.Title. OliviaBasegio
T385.W7282013
006.6’8—dc23
Compositor
2013016852
LaurelTech
Copyright©2014PearsonEducation,Inc.
Allrightsreserved.PrintedintheUnitedStatesofAmerica.Thispublicationis
protectedbycopyright,andpermissionmustbeobtainedfromthepublisher
priortoanyprohibitedreproduction,storageinaretrievalsystem,or
transmissioninanyformorbyanymeans,electronic,mechanical,
photocopying,recording,orlikewise.Toobtainpermissiontousematerial
fromthiswork,pleasesubmitawrittenrequesttoPearsonEducation,Inc.,
PermissionsDepartment,OneLakeStreet,UpperSaddleRiver,NewJersey
07458,oryoumayfaxyourrequestto(201)236–3290.
ISBN-13:978-0-321-90294-8
ISBN-10: 0-321-90294-7
TextprintedintheUnitedStatesonrecycledpaperatRRDonnelleyin
Crawfordsville,Indiana.
Firstprinting,July2013
For my family and my friends.
For those from whom I have learned.
For people who love to learn.
—Graham Sellers
For my wife LeeAnne,
for not killing me in my sleep
(when I deserved it).
To the memory of Richard S. Wright, Sr.
Thanks, Dad, for just letting me be a nerd.
—Richard S. Wright, Jr.
ptg11539634
For my wife, Anna,
who has put up with all my engineering nonsense all these
years and provided undying love and support.
And to my parents for providing me with encouragement and
more LEGOs than I could get both arms around.
—Nicholas Haemel
This page intentionally left blank
ptg11539634
Contents
Figures xv
ptg11539634
Tables xxiii
Listings xxv
Foreword xxxiii
Preface xxxv
AboutThisBook . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv
TheArchitectureoftheBook . . . . . . . . . . . . . . . . . . . xxxvi
What’sNewinThisEdition . . . . . . . . . . . . . . . . . . . . xxxviii
HowtoBuildtheSamples . . . . . . . . . . . . . . . . . . . . . xxxix
Errata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xl
Acknowledgments xli
About the Authors xlv
I Foundations 1
1 Introduction 3
OpenGLandtheGraphicsPipeline . . . . . . . . . . . . . . . . . . 4
TheOriginsandEvolutionofOpenGL . . . . . . . . . . . . . . . . 6
CoreProfileOpenGL . . . . . . . . . . . . . . . . . . . . . . . . 8
Primitives,Pipelines,andPixels . . . . . . . . . . . . . . . . . . . . 10
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
vii
2 Our First OpenGL Program 13
CreatingaSimpleApplication . . . . . . . . . . . . . . . . . . . . . 14
UsingShaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
DrawingOurFirstTriangle . . . . . . . . . . . . . . . . . . . . . . . 24
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3 Following the Pipeline 27
PassingDatatotheVertexShader . . . . . . . . . . . . . . . . . . . 28
VertexAttributes . . . . . . . . . . . . . . . . . . . . . . . . . . 28
PassingDatafromStagetoStage . . . . . . . . . . . . . . . . . . . . 29
InterfaceBlocks . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Tessellation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
TessellationControlShaders . . . . . . . . . . . . . . . . . . . 33
TheTessellationEngine . . . . . . . . . . . . . . . . . . . . . . 34
TessellationEvaluationShaders . . . . . . . . . . . . . . . . . . 34
GeometryShaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
PrimitiveAssembly,Clipping,andRasterization . . . . . . . . . . . 38
ptg11539634
Clipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
ViewportTransformation . . . . . . . . . . . . . . . . . . . . . 39
Culling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Rasterization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
FragmentShaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
FramebufferOperations . . . . . . . . . . . . . . . . . . . . . . . . . 45
PixelOperations . . . . . . . . . . . . . . . . . . . . . . . . . . 45
ComputeShaders. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
4 Math for 3D Graphics 49
IsThistheDreadedMathChapter? . . . . . . . . . . . . . . . . . . 50
ACrashCoursein3DGraphicsMath . . . . . . . . . . . . . . . . . 51
Vectors,orWhichWayIsWhich? . . . . . . . . . . . . . . . . 51
CommonVectorOperators . . . . . . . . . . . . . . . . . . . . 54
Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
MatrixConstructionandOperators . . . . . . . . . . . . . . . 60
UnderstandingTransformations . . . . . . . . . . . . . . . . . . . . 63
CoordinateSpacesinOpenGL . . . . . . . . . . . . . . . . . . 63
CoordinateTransformations . . . . . . . . . . . . . . . . . . . 66
ConcatenatingTransformations . . . . . . . . . . . . . . . . . 73
Quaternions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
TheModel-ViewTransform . . . . . . . . . . . . . . . . . . . . 76
ProjectionTransformations . . . . . . . . . . . . . . . . . . . . 79
viii Contents
Interpolation,Lines,Curves,andSplines . . . . . . . . . . . . . . . 82
Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Splines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
5 Data 91
Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
AllocatingMemoryusingBuffers . . . . . . . . . . . . . . . . . 92
FillingandCopyingDatainBuffers . . . . . . . . . . . . . . . 95
FeedingVertexShadersfromBuffers . . . . . . . . . . . . . . . 97
Uniforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
DefaultBlockUniforms . . . . . . . . . . . . . . . . . . . . . . 103
UniformBlocks . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
UsingUniformstoTransformGeometry . . . . . . . . . . . . 121
ShaderStorageBlocks . . . . . . . . . . . . . . . . . . . . . . . . . . 126
SynchronizingAccesstoMemory . . . . . . . . . . . . . . . . 129
AtomicCounters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
SynchronizingAccesstoAtomicCounters . . . . . . . . . . . 137 ptg11539634
Textures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
CreatingandInitializingTextures . . . . . . . . . . . . . . . . 138
TextureTargetsandTypes . . . . . . . . . . . . . . . . . . . . . 139
ReadingfromTexturesinShaders . . . . . . . . . . . . . . . . 141
LoadingTexturesfromFiles . . . . . . . . . . . . . . . . . . . . 144
ControllingHowTextureDataIsRead. . . . . . . . . . . . . . 148
ArrayTextures. . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
WritingtoTexturesinShaders . . . . . . . . . . . . . . . . . . 165
SynchronizingAccesstoImages . . . . . . . . . . . . . . . . . 176
TextureCompression . . . . . . . . . . . . . . . . . . . . . . . 177
TextureViews . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
6 Shaders and Programs 187
LanguageOverview . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
DataTypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Built-InFunctions . . . . . . . . . . . . . . . . . . . . . . . . . 194
Compiling,Linking,andExaminingPrograms . . . . . . . . . . . . 201
GettingInformationfromtheCompiler . . . . . . . . . . . . 201
GettingInformationfromtheLinker . . . . . . . . . . . . . . 204
SeparatePrograms . . . . . . . . . . . . . . . . . . . . . . . . . 206
ShaderSubroutines . . . . . . . . . . . . . . . . . . . . . . . . . 213
ProgramBinaries . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Contents ix