Home > Store

OpenGL Shading Language, 2nd Edition

Register your product to gain access to bonus material or receive a coupon.

OpenGL Shading Language, 2nd Edition


  • Sorry, this book is no longer in print.
Not for Sale


  • Copyright 2006
  • Edition: 2nd
  • Book
  • ISBN-10: 0-321-33489-2
  • ISBN-13: 978-0-321-33489-3

"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

  • Image-based lighting
  • Lighting with spherical harmonics
  • Ambient occlusion
  • Shadow mapping
  • Volume shadows using deferred lighting
  • Ward's BRDF model

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.


Author's Site

Visit the Author's Web Site related to this title.

Sample Content

Online Sample Chapter

A Simple Shading Example in OpenGL Shading Language

Downloadable Sample Chapter

Download the Sample Chapter related to this title.

Table of Contents

Foreword xxiii

Foreword to the First Edition xxvii

Preface xxxi

About the Author xxxvii

About the Contributors xxxix

Acknowledgments xli

Chapter 1. Review of OpenGL Basics 1

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

Chapter 2. Basics35

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

Chapter 3. Language Definition 65

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

Chapter 4. The OpenGL Programmable Pipeline 97

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

Chapter 5. Built-in Functions 123

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

Chapter 6. Simple Shading Example 149

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

Chapter 7. OpenGL Shading Language AP I 167

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

Chapter 8. Shader Development 215

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

Chapter 9. Emulating OpenGL Fixed Functionality 233

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

Chapter 10. Stored Texture Shaders 253

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

Chapter 11. Procedural Texture Shaders 285

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

Chapter 12. Lighting 311

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

Chapter 13. Shadows 333

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

Chapter 14. Surface Characteristics 357

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

Chapter 15. Noise 387

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

Chapter 16. Animation 411

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

Chapter 17. Antialiasing Procedural Textures 433

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

Chapter 18. Non-Photorealistic Shaders 453

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

Chapter 19. Shaders for Imaging 479

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

Chapter 20. RealWorldz 505

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

Chapter 21. Language Comparison 543

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

Appendix A. Language Grammar 559

Appendix B. API Function Reference 573

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

Afterword 665

Glossary 669

Further Reading 689

Index 705


Untitled Document For just about as long as there has been graphics hardware, there has been programmable graphics hardware. Over the years, building flexibility into graphics hardware designs has been a necessary way of life for hardware developers. Graphics APIs continue to evolve, and because a hardware design can take two years or more from start to finish, the only way to guarantee a hardware product that can support the then current graphics APIs at its release is to build in some degree of programmability from the very beginning.

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.

Intended Audience

The primary audience for this book is application programmers who want to write shaders. This book can be used as both a tutorial and a reference book by people interested in learning to write shaders with the OpenGL Shading Language. Some will use the book in one fashion, and some in the other. The organization is amenable to both uses and is based on the assumption that most people won't read the book in sequential order from back to front (but some intrepid readers of the first edition reported that they did just that!).

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.

About This Book

This book has three main parts. Chapters 1 through 8 teach the reader about the OpenGL Shading Language and how to use it. This part of the book covers details of the language and details of the OpenGL commands that create and manipulate shaders. To supply a basis for writing shaders, Chapters 9 through 20 contain a gallery of shader examples and some explanation of the underlying algorithms. This part of the book is both the baseline for a reader's shader development and a springboard for inspiring new ideas. Finally, Chapter 21 compares other notable commercial shading languages, and Appendices A and B contain reference material for the language and the API entry points that support it.

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.

  • Chapter 1 reviews the fundamentals of the OpenGL API. Readers already familiar with OpenGL may skip to Chapter 2.
  • Chapter 2 introduces the OpenGL Shading Language and the OpenGL entry points that have been added to support it. If you want to know what the OpenGL Shading Language is all about and you have time to read only two chapters of this book, this chapter and Chapter 3 are the ones to read.
  • Chapter 3 thoroughly describes the OpenGL Shading Language. This material is organized to present the details of a programming language. This section serves as a useful reference section for readers who have developed a general understanding of the language.
  • Chapter 4 discusses how the newly defined programmable parts of the rendering pipeline interact with each other and with OpenGL's fixed functionality. This discussion includes descriptions of the built-in variables defined in the OpenGL Shading Language.
  • Chapter 5 describes the built-in functions that are part of the OpenGL Shading Language. This section is a useful reference section for readers with an understanding of the language.
  • Chapter 6 presents and discusses a fairly simple shader example. People who learn best by diving in and studying a real example will benefit from the discussion in this chapter.
  • Chapter 7 describes the entry points that have been added to OpenGL to support the creation and manipulation of shaders. Application programmers who want to use shaders in their application must understand this material.
  • Chapter 8 presents some general advice on shader development and describes the shader development process. It also describes tools that are currently available to aid the shader development process.
  • Chapter 9 begins a series of chapters that present and discuss shaders with a common characteristic. In this chapter, shaders that duplicate some of the fixed functionality of the OpenGL pipeline are presented.
  • Chapter 10 presents a few shaders that are based on the capability to store data in and retrieve data from texture maps.
  • Chapter 11 is devoted to shaders that are procedural in nature; that is, effects are computed algorithmically rather than being based on information stored in textures.
  • Chapter 12 presents several alternative lighting models that can be implemented with OpenGL shaders.
  • Chapter 13 discusses algorithms and shaders for producing shadows.
  • Chapter 14 delves into the details of shaders that implement more realistic surface characteristics, including refraction, diffraction, and more realistic reflection.
  • Chapter 15 describes noise and the effects that can be achieved with its proper use.
  • Chapter 16 contains examples of how shaders can create rendering effects that vary over time.
  • Chapter 17 contains a discussion of the aliasing problem and how shaders can be written to reduce the effects of aliasing.
  • Chapter 18 illustrates shaders that achieve effects other than photorealism. Such effects include technical illustration, sketching or hatching effects, and other stylized rendering.
  • Chapter 19 presents several shaders that modify images as they are being drawn with OpenGL.
  • Chapter 20 describes some of the techniques and algorithms used in a complex OpenGL application that makes extensive use of the OpenGL Shading Language.
  • Chapter 21 compares the OpenGL Shading Language with other notable commercial shading languages.
  • Appendix A contains the language grammar that more clearly specifies the OpenGL Shading Language.
  • Appendix B contains reference pages for the API entry points that are related to the OpenGL Shading Language.
  • Finally, Glossary collects terms defined in the book, Further Reading gathers all the chapter references and adds more, and Index ends the book.

About the Shader Examples

The shaders contained in this book are primarily short programs that illustrate the capabilities of the OpenGL Shading Language. None of the example shaders should be presumed to illustrate the "best" way of achieving a particular effect. (Indeed, the "best" way to implement certain effects may have yet to be discovered through the power and flexibility of programmable graphics hardware.) Performance improvements for each shader are possible for any given hardware target. For most of the shaders, image quality may be improved if greater care is taken to reduce or eliminate causes of aliasing. The source code for these shaders is written in a way that I believe represents a reasonable trade-off between source code clarity, portability, and performance. Use them to learn the OpenGL Shading Language, and improve on them for use in your own projects.

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.


I know that this book contains some errors, but I've done my best to keep them to a minimum. If you find any errors, please report them to me (randi@3dshaders.com) and I will keep a running list on this book's Web site at http://3dshaders.com.

Typographical Conventions
This book contains a number of typographical conventions to enhance readability and understanding.

  • SMALL CAPS are used for the first occurrence of defined terms.
  • Italics are used for emphasis, document titles, and coordinate values such as x, y, and z.
  • Bold serif is used for language keywords.
  • Sans serif is used for macros and symbolic constants that appear in the text.
  • Bold sans serif is used for function names.
  • Italic sans serif is used for variables, parameter names, spatial dimensions, and matrix components.
  • Fixed width is used for code examples.


Download the Foreword file related to this title.


Download the Index file related to this title.


Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020