HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
"As the 'Red Book' is known to be the gold standard for OpenGL, the 'Orange Book' is considered to be the gold standard for the OpenGL Shading Language. With Randi's extensive knowledge of OpenGL and GLSL, you can be assured you will be learning from a graphics industry veteran. Within the pages of the second edition you can find topics from beginning shader development to advanced topics such as the spherical harmonic lighting model and more."
—David Tommeraasen, CEO/Programmer, Plasma Software
"This will be the definitive guide for OpenGL shaders; no other book goes into this detail. Rost has done an excellent job at setting the stage for shader development, what the purpose is, how to do it, and how it all fits together. The book includes great examples and details, and good additional coverage of 2.0 changes!"
—Jeffery Galinovsky, Director of Emerging Market Platform Development, Intel Corporation
"The coverage in this new edition of the book is pitched just right to help many new shader-writers get started, but with enough deep information for the 'old hands.'"
—Marc Olano, Assistant Professor, University of Maryland
"This is a really great book on GLSL—well written and organized, very accessible, and with good real-world examples and sample code. The topics flow naturally and easily, explanatory code fragments are inserted in very logical places to illustrate concepts, and all in all, this book makes an excellent tutorial as well as a reference."
—John Carey, Chief Technology Officer, C.O.R.E. Feature Animation
OpenGL® Shading Language, Second Edition, extensively updated for OpenGL 2.0, is the experienced application programmer's guide to writing shaders. Part reference, part tutorial, this book thoroughly explains the shift from fixed-functionality graphics hardware to the new era of programmable graphics hardware and the additions to the OpenGL API that support this programmability. With OpenGL and shaders written in the OpenGL Shading Language, applications can perform better, achieving stunning graphics effects by using the capabilities of both the visual processing unit and the central processing unit.
In this book, you will find a detailed introduction to the OpenGL Shading Language (GLSL) and the new OpenGL function calls that support it. The text begins by describing the syntax and semantics of this high-level programming language. Once this foundation has been established, the book explores the creation and manipulation of shaders using new OpenGL function calls.
OpenGL® Shading Language, Second Edition, includes updated descriptions for the language and all the GLSL entry points added to OpenGL 2.0; new chapters that discuss lighting, shadows, and surface characteristics; and an under-the-hood look at the implementation of RealWorldz, the most ambitious GLSL application to date. The second edition also features 18 extensive new examples of shaders and their underlying algorithms, including
The color plate section illustrates the power and sophistication of the OpenGL Shading Language. The API Function Reference at the end of the book is an excellent guide to the API entry points that support the OpenGL Shading Language. Also included is a convenient Quick Reference Card to GLSL.
A Simple Shading Example in OpenGL Shading Language
Download the Sample
Chapter related to this title.
1.1 OpenGL History 1
1.2 OpenGL Evolution 3
1.3 Execution Mode l4
1.4 The Frame Buffer 5
1.5 State 8
1.6 Processing Pipeline 8
1.7 Drawing Geometry 9
1.7.1 Geometry Specification 9
1.7.2 Per-Vertex Operations 12
1.7.3 Primitive Assembly 14
1.7.4 Primitive Processing 14
1.7.5 Rasterization 15
1.7.6 Fragment Processing 16
1.7.7 Per-Fragment Operations 16
1.7.8 Frame Buffer Operations 17
1.8 Drawing Images 18
1.8.1 Pixel Unpacking 19
1.8.2 Pixel Transfer 19
1.8.3 Rasterization and Back-End Processing 20
1.8.4 Read Control 20
1.9 Coordinate Transforms 21
1.10 Texturing 26
1.11 Summary 31
1.12 Further Information 32
2.1 Introduction to the OpenGL Shading Language 35
2.2 Why Write Shaders? 37
2.3 OpenGL Programmable Processors 38
2.3.1 Vertex Processor 40
2.3.2 Fragment Processor 43
2.4 Language Overview 47
2.4.1 Language Design Considerations 47
2.4.2 C Basis 50
2.4.3 Additions to C 50
2.4.4 Additions from C++ 52
2.4.5 C Features Not Supported 53
2.4.6 Other Differences 53
2.5 System Overview 54
2.5.1 Driver Model 54
2.5.2 OpenGL Shading Language Compiler/Linker 56
2.5.3 OpenGL Shading Language API 57
2.6 Key Benefits 59
2.7 Summary 61
2.8 Further Information 63
3.1 Example Shader Pair 65
3.2 Data Types 67
3.2.1 Scalars 67
3.2.2 Vectors 69
3.2.3 Matrices 70
3.2.4 Samplers 71
3.2.5 Structures 72
3.2.6 Arrays 73
3.2.7 Void 74
3.2.8 Declarations and Scope 74
3.2.9 Type Matching and Promotion 75
3.3 Initializers and Constructors 75
3.4 Type Conversions 77
3.5 Qualifiers and Interface to a Shader 78
3.5.1 Attribute Qualifiers 79
3.5.2 Uniform Qualifiers 79
3.5.3 Varying Qualifiers 79
3.5.4 Constant Qualifiers 80
3.5.5 Absent Qualifier 81
3.6 Flow Control 82
3.6.1 Functions 82
3.6.2 Calling Conventions 83
3.6.3 Built-in Functions 84
3.7 Operations 85
3.7.1 Indexing 86
3.7.2 Swizzling 87
3.7.3 Component-wise Operation 87
3.8 Preprocessor 90
3.9 Preprocessor Expressions 93
3.10 Error Handling 94
3.11 Summary 95
3.12 Further Information 95
4.1 The Vertex Processor 98
4.1.1 Vertex Attributes 99
4.1.2 Uniform Variables 100
4.1.3 Special Output Variables 101
4.1.4 Built-in Varying Variables 102
4.1.5 User-Defined Varying Variables 103
4.2 The Fragment Processor 104
4.2.1 Varying Variables 104
4.2.2 Uniform Variables 105
4.2.3 Special Input Variables 106
4.2.4 Special Output Variables 107
4.3 Built-in Uniform Variables 108
4.4 Built-in Constants 113
4.5 Interaction with OpenGL Fixed Functionality 114
4.5.1 Two-Sided Color Mode 114
4.5.2 Point Size Mode 115
4.5.3 Clipping 116
4.5.4 Raster Position 117
4.5.5 Position Invariance 117
4.5.6 Texturing 118
4.6 Summary 120
4.7 Further Information 120
5.1 Angle and Trigonometry Functions 124
5.2 Exponential Functions 126
5.3 Common Functions 126
5.4 Geometric Functions 136
5.5 Matrix Functions 138
5.6 Vector Relational Functions 139
5.7 Texture Access Functions 141
5.8 Fragment Processing Functions 144
5.9 Noise Functions 145
5.10 Summary 147
5.11 Further Information 147
6.1 Brick Shader Overview 150
6.2 Vertex Shader 151
6.3 Fragment Shader 157
6.4 Observations 164
6.5 Summary 165
6.6 Further Information 165
7.1 Obtaining Version Information 168
7.2 Creating Shader Objects 170
7.3 Compiling Shader Objects 172
7.4 Linking and Using Shaders 173
7.5 Cleaning Up 177
7.6 Query Functions 178
7.7 Specifying Vertex Attributes 184
7.8 Specifying Uniform Variables 195
7.9 Samplers 203
7.10 Multiple Render Targets 204
7.11 Development Aids 206
7.12 Implementation-Dependent API Values 207
7.13 Application Code for Brick Shaders 208
7.14 Summary 212
7.15 Further Information 213
8.1 General Principles 215
8.1.1 Understand the Problem 216
8.1.2 Add Complexity Progressively 216
8.1.3 Test and Iterate 217
8.1.4 Strive for Simplicity 217
8.1.5 Exploit Modularity 217
8.2 Performance Considerations 218
8.2.1 Consider Computational Frequency 218
8.2.2 Analyze Your Algorithm 219
8.2.3 Use the Built-in Functions 219
8.2.4 Use Vectors 220
8.2.5 Use Textures to Encode Complex Functions 220
8.2.6 Review the Information Logs 220
8.3 Shader Debugging 220
8.3.1 Use the Vertex Shader Output 221
8.3.2 Use the Fragment Shader Output 221
8.3.3 Use Simple Geometry 222
8.4 Shader Development Tools 222
8.4.1 RenderMonkey 222
8.4.2 OpenGL Shading Language Compiler Front End 225
8.5 Scene Graphs 227
8.6 Summary 229
8.7 Further Information 230
9.1 Transformation 234
9.2 Light Sources 236
9.2.1 Directional Lights 236
9.2.2 Point Lights 237
9.2.3 Spotlights 239
9.3 Material Properties and Lighting 240
9.4 Two-Sided Lighting 243
9.5 No Lighting 244
9.6 Fog 244
9.7 Texture Coordinate Generation 246
9.8 User Clipping 249
9.9 Texture Application 249
9.10 Summary 251
9.11 Further Information 251
10.1 Access to Texture Maps from a Shader 254
10.2 Simple Texturing Example 256
10.2.1 Application Setup 257
10.2.2 Vertex Shader 258
10.2.3 Fragment Shader 259
10.3 Multitexturing Example 260
10.3.1 Application Setup 262
10.3.2 Vertex Shader 262
10.3.3 Fragment Shader 263
10.4 Cube Mapping Example 265
10.4.1 Application Setup 266
10.4.2 Vertex Shader 266
10.4.3 Fragment Shader 267
10.5 Another Environment Mapping Example 68
10.5.1 Vertex Shader 269
10.5.2 Fragment Shader 269
10.6 Glyph Bombing 272
10.6.1 Application Setup 272
10.6.2 Vertex Shader 276
10.6.3 Fragment Shader 277
10.7 Summary 281
10.8 Further Information 282
11.1 Regular Patterns 287
11.1.1 Stripes Vertex Shader 289
11.1.2 Stripes Fragment Shader 290
11.2 Toy Ball 292
11.2.1 Application Setup 293
11.2.2 Vertex Shader 294
11.2.3 Fragment Shader 294
11.3 Lattice 299
11.4 Bump Mapping 300
11.4.1 Application Setup 303
11.4.2 Vertex Shader 306
11.4.3 Fragment Shader 306
11.4.4 Normal Maps 308
11.5 Summary 309
11.6 Further Information 310
12.1 Hemisphere Lighting 311
12.2 Image-Based Lighting 315
12.3 Lighting with Spherical Harmonics 318
12.4 The *erLight Shader 322
12.4.1 *erLight Controls 323
12.4.2 Vertex Shader 325
12.4.3 Fragment Shader 326
12.5 Summary 329
12.6 Further Information 330
13.1 Ambient Occlusion 334
13.2 Shadow Maps 338
13.2.1 Application Setup 340
13.2.2 Vertex Shader 341
13.2.3 Fragment Shader 343
13.3 Deferred Shading for Volume Shadows 346
13.3.1 Shaders for First Pass 349
13.3.2 Shaders for Second Pass 350
13.4 Summary 354
13.5 Further Information 355
14.1 Refraction 358
14.2 Diffraction 363
14.3 BRDF Models 368
14.4 Polynomial Texture Mapping with BRDF Data 375
14.4.1 Application Setup 378
14.4.2 Vertex Shader 379
14.4.3 Fragment Shader 382
14.5 Summary 384
14.6 Further Information 384
15.1 Noise Defined 388
15.1.1 2D Noise 394
15.1.2 Higher Dimensions of Noise 395
15.1.3 Using Noise in OpenGL Shaders 395
15.2 Noise Textures 396
15.3 Trade-offs 399
15.4 A Simple Noise Shader 400
15.4.1 Application Setup 400
15.4.2 Vertex Shader 400
15.4.3 Fragment Shader 401
15.5 Turbulence 402
15.5.1 Sun Surface Shader 403
15.5.2 Marble 404
15.6 Granite 404
15.7 Wood 405
15.7.1 Application Setup 405
15.7.2 Fragment Shader 406
15.8 Summary 409
15.9 Further Information 409
16.1 On/Off 412
16.2 Threshold 413
16.3 Translation 413
16.4 Morphing 414
16.4.1 Sphere Morph Vertex Shader 415
16.5 Other Blending Effects 417
16.6 Vertex Noise 417
16.7 Particle Systems 418
16.7.1 Application Setup 420
16.7.2 Confetti Cannon Vertex Shader 423
16.7.3 Further Enhancements 425
16.8 Wobble 426
16.9 Summary 430
16.10 Further Information 430
17.1 Sources of Aliasing 433
17.2 Avoiding Aliasing 435
17.3 Increasing Resolution 436
17.4 Antialiased Stripe Example 437
17.4.1 Generating Stripes 437
17.4.2 Analytic Prefiltering 439
17.4.3 Adaptive Analytic Prefiltering 440
17.4.4 Analytic Integration 443
17.4.5 Antialiased Brick Fragment Shader 446
17.5 Frequency Clamping 447
17.5.1 Antialiased Checkerboard Fragment Shader 448
17.6 Summary 450
17.7 Further Information 450
18.1 Hatching Example 454
18.1.1 Application Setup 455
18.1.2 Vertex Shader 455
18.1.3 Generating Hatching Strokes 456
18.1.4 Obtaining Uniform Line Density 456
18.1.5 Simulating Lighting 458
18.1.6 Adding Character 459
18.1.7 Hatching Fragment Shader 461
18.2 Technical Illustration Example 462
18.2.1 Application Setup 466
18.2.2 Vertex Shader 466
18.2.3 Fragment Shader 466
18.3 Mandelbrot Example 467
18.3.1 About the Mandelbrot Set 468
18.3.2 Vertex Shader 471
18.3.3 Fragment Shader 472
18.3.4 Julia Sets 474
18.4 Summary 475
18.5 Further Information 476
19.1 Geometric Image Transforms 480
19.2 Mathematical Mappings 481
19.3 Lookup Table Operations 481
19.4 Color Space Conversions 482
19.5 Image Interpolation and Extrapolation 483
19.5.1 Brightness 484
19.5.2 Contrast 485
19.5.3 Saturation 485
19.5.4 Sharpness 486
19.6 Blend Modes 486
19.6.1 Normal 488
19.6.2 Average 488
19.6.3 Dissolve 488
19.6.4 Behind 488
19.6.5 Clear 489
19.6.6 Darken 489
19.6.7 Lighten 489
19.6.8 Multiply489
19.6.9 Screen 490
19.6.10 Color Burn 490
19.6.11 Color Dodge 490
19.6.12 Overlay 490
19.6.13 Soft Light 491
19.6.14 Hard Light 491
19.6.15 Add 491
19.6.16 Subtract 492
19.6.17 Difference 492
19.6.18 Inverse Difference 492
19.6.19 Exclusion 492
19.6.20 Opacity 492
19.6.21 Convolution 493
19.6.22 Smoothing 495
19.6.23 Edge Detection 498
19.6.24 Sharpening 499
19.7 Summary 501
19.8 Further Information 501
20.1 Features 505
20.2 RealWorldz Internals 506
20.2.1 Terrain-Rendering Structure 506
20.2.2 Shading 507
20.2.3 Fractal Terrains 508
20.2.4 Fractal Terrains in RealWorldz 510
20.2.5 Noise Texture Creation 510
20.2.6 Tile Set Noise 511
20.2.7 Surface Normals 513
20.2.8 Overhanging Terrain 513
20.3 Implementation 517
20.3.1 Noise Values and Derivatives 517
20.3.2 Tile Sets 518
20.3.3 The Function Tree 519
20.3.4 Terrain Color 521
20.3.5 AltGrad Map for Snow 522
20.3.6 AltGrad Map for AlienRockArt 522
20.3.7 AltGrad Map for DragonRidges 523
20.3.8 Lighting 524
20.3.9 Performance Considerations 524
20.4 Atmospheric Effects 525
20.4.1 Aerial Perspective 525
20.4.2 Sky Shading 529
20.5 Ocean 532
20.5.1 Reflections 532
20.5.2 Reflected Sky 536
20.5.3 Rendering the Ocean 536
20.6 Clouds 537
20.7 Summary 540
20.8 Further Information 540
21.1 Chronology of Shading Languages 543
21.2 RenderMan 544
21.3 OpenGL Shader (ISL) 547
21.4 HLSL 549
21.5 Cg 552
21.6 Summary 554
21.7 Further Information 555
Implementation-Dependent API Values for GLSL 574
Other Queriable Values for GLSL 575
glAttachShader 576
glBindAttribLocation 578
glCompileShader 581
glCreateProgram 583
glCreateShader 585
glDeleteProgram 587
glDeleteShader 589
glDetachShader 591
glDrawBuffers 593
glEnableVertexAttribArray 596
glGetActiveAttrib 598
glGetActiveUniform 601
glGetAttachedShaders 605
glGetAttribLocation 607
glGetProgram 609
glGetProgramInfoLog 612
glGetShader 614
glGetShaderInfoLog 616
glGetShaderSource 618
glGetUniform 620
glGetUniformLocation 622
glGetVertexAttrib 624
glGetVertexAttribPointer 627
glIsProgram 629
glIsShader 631
glLinkProgram 632
glShaderSource 636
glUniform 638
glUseProgram 645
glValidateProgram 649
glVertexAttrib 651
glVertexAttribPointer 657
OpenGL 1.5 to OpenGL 2.0 GLSL Migration Guide 660
Until recently, the realm of programming graphics hardware belonged to just a few people, mainly researchers and graphics hardware driver developers. Research into programmable graphics hardware has been taking place for many years, but the point of this research has not been to produce viable hardware and software for application developers and end users. The graphics hardware driver developers have focused on the immediate task of providing support for the important graphics APIs of the time: PHIGS, PEX, Iris GL, OpenGL, Direct3D, and so on. Until recently, none of these APIs exposed the programmability of the underlying hardware, so application developers have been forced into using the fixed functionality provided by traditional graphics APIs.
Hardware companies have not exposed the programmable underpinnings of their products because of the high cost of educating and supporting customers to use low-level, device-specific interfaces, and because these interfaces typically change quite radically with each new generation of graphics hardware. Application developers who use such a device-specific interface to a piece of graphics hardware face the daunting task of updating their software for each new generation of hardware that comes along. And forget about supporting the application on hardware from multiple vendors!
As we moved into the 21st century, some of these fundamental tenets about graphics hardware were challenged. Application developers pushed the envelope as never before and demanded a variety of new features in hardware in order to create more and more sophisticated onscreen effects. As a result, new graphics hardware designs became more programmable than ever before. Standard graphics APIs were challenged to keep up with the pace of hardware innovation. For OpenGL, the result was a spate of extensions to the core API as hardware vendors struggled to support a range of interesting new features that their customers were demanding.
The creation of a standard, cross-platform, high-level shading language for commercially available graphics hardware was a watershed event for the graphics industry. A paradigm shift occurred, one that took us from the world of rigid, fixed functionality graphics hardware and graphics APIs to a brave new world where the visual processing unit, or VPU (i.e., graphics hardware), is as important as the central processing unit, or CPU. The VPU is optimized for processing dynamic media such as 3D graphics and video. Highly parallel processing of floating-point data is the primary task for VPUs, and the flexibility of the VPU means that it can also be used to process data other than a stream of traditional graphics commands. Applications can take advantage of the capabilities of both the CPU and the VPU, using the strengths of each to optimally perform the task at hand.
This book describes how graphics hardware programmability is exposed through a high-level language in the leading cross-platform 3D graphics API: OpenGL. This language, the OpenGL Shading Language, lets applications take total control over the most important stages of the graphics processing pipeline. No longer restricted to the graphics rendering algorithms and formulas chosen by hardware designers and frozen in silicon, software developers are beginning to use this programmability to create stunning effects in real time.
Readers do not need previous knowledge of OpenGL to absorb the material in this book, but such knowledge is very helpful. A brief review of OpenGL is included, but this book does not attempt to be a tutorial or reference book for OpenGL. Anyone attempting to develop an OpenGL application that uses shaders should be armed with OpenGL programming documentation in addition to this book.
Computer graphics has a mathematical basis, so some knowledge of algebra, trigonometry, and calculus will help readers understand and appreciate some of the details presented. With the advent of programmable graphics hardware, key parts of the graphics processing pipeline are once again under the control of software developers. To develop shaders successfully in this environment, developers must understand the mathematical basis of computer graphics.
The chapters are arranged to suit the needs of the reader who is least familiar with OpenGL and shading languages. Certain chapters can be skipped by readers who are more familiar with both topics. This book has somewhat compartmentalized chapters in order to allow such usage.
All the images produced for this book were done either on the first graphics accelerator to provide support for the OpenGL Shading Language, the 3Dlabs Wildcat VP, or its successor, the 3Dlabs Wildcat Realizm. I have taken as much care as possible to present shaders that are done "the right way" for the OpenGL Shading Language rather than those with idiosyncrasies from their development on the very early implementations of the OpenGL Shading Language. Electronic versions of most of these shaders are available through a link at this book's Web site at http://3dshaders.com.
Typographical Conventions
This book contains a number of typographical conventions to enhance readability
and understanding.
Download the Foreword
file related to this title.
Download the Index
file related to this title.