You are previewing OpenGL Programming Guide: The Official Guide to Learning OpenGL, Version 4.3, Eighth Edition.
O'Reilly logo
OpenGL Programming Guide: The Official Guide to Learning OpenGL, Version 4.3, Eighth Edition

Book Description

Includes Complete Coverage of the OpenGL® Shading Language!

Today’s OpenGL software interface enables programmers to produce extraordinarily high-quality computer-generated images and interactive applications using 2D and 3D objects, color images, and programmable shaders.

OpenGL® Programming Guide: The Official Guide to Learning OpenGL®, Version 4.3, Eighth Edition, has been almost completely rewritten and provides definitive, comprehensive information on OpenGL and the OpenGL Shading Language. This edition of the best-selling “Red Book” describes the features through OpenGL version 4.3. It also includes updated information and techniques formerly covered in OpenGL® Shading Language (the “Orange Book”).

For the first time, this guide completely integrates shader techniques, alongside classic, functioncentric techniques. Extensive new text and code are presented, demonstrating the latest in OpenGL programming techniques.

OpenGL® Programming Guide, Eighth Edition, provides clear explanations of OpenGL functionality and techniques, including processing geometric objects with vertex, tessellation, and geometry shaders using geometric transformations and viewing matrices; working with pixels and texture maps through fragment shaders; and advanced data techniques using framebuffer objects and compute shaders.

New OpenGL features covered in this edition include

  • Best practices and sample code for taking full advantage of shaders and the entire shading pipeline (including geometry and tessellation shaders)

  • Integration of general computation into the rendering pipeline via compute shaders

  • Techniques for binding multiple shader programs at once during application execution

  • Latest GLSL features for doing advanced shading techniques

  • Additional new techniques for optimizing graphics program performance

  • Table of Contents

    1. Title Page
    2. Copyright Page
    3. Praise for OpenGL® Programming Guide, Eighth Edition
    4. Dedication Page
    5. Contents
    6. Figures
    7. Tables
    8. Examples
    9. About This Guide
      1. What This Guide Contains
      2. What’s New in This Edition
      3. What You Should Know Before Reading This Guide
      4. How to Obtain the Sample Code
      5. Errata
      6. Style Conventions
    10. Chapter 1. Introduction to OpenGL
      1. What Is OpenGL?
      2. Your First Look at an OpenGL Program
      3. OpenGL Syntax
      4. OpenGL’s Rendering Pipeline
        1. Preparing to Send Data to OpenGL
        2. Sending Data to OpenGL
        3. Vertex Shading
        4. Tessellation Shading
        5. Geometry Shading
        6. Primitive Assembly
        7. Clipping
        8. Rasterization
        9. Fragment Shading
        10. Per-Fragment Operations
      5. Our First Program: A Detailed Discussion
        1. Entering main()
        2. OpenGL Initialization
        3. Our First OpenGL Rendering
    11. Chapter 2. Shader Fundamentals
      1. Shaders and OpenGL
      2. OpenGL’s Programmable Pipeline
      3. An Overview of the OpenGL Shading Language
        1. Creating Shaders with GLSL
        2. Storage Qualifiers
        3. Statements
        4. Computational Invariance
        5. Shader Preprocessor
        6. Compiler Control
        7. Global Shader-Compilation Option
      4. Interface Blocks
        1. Uniform Blocks
        2. Specifying Uniform Blocks in Shaders
        3. Accessing Uniform Blocks from Your Application
        4. Buffer Blocks
        5. In/Out Blocks
      5. Compiling Shaders
        1. Our LoadShaders() Function
      6. Shader Subroutines
        1. Advanced
        2. GLSL Subroutine Setup
        3. Selecting Shader Subroutines
      7. Separate Shader Objects
        1. Advanced
    12. Chapter 3. Drawing with OpenGL
      1. OpenGL Graphics Primitives
        1. Points
        2. Lines, Strips, and Loops
        3. Triangles, Strips, and Fans
      2. Data in OpenGL Buffers
        1. Creating and Allocating Buffers
        2. Getting Data into and out of Buffers
        3. Accessing the Content of Buffers
        4. Discarding Buffer Data
      3. Vertex Specification
        1. VertexAttribPointer in Depth
        2. Static Vertex-Attribute Specification
      4. OpenGL Drawing Commands
        1. OpenGL Drawing Exercises
        2. Restarting Primitives
      5. Instanced Rendering
        1. Instanced Vertex Attributes
        2. Using the Instance Counter in Shaders
        3. Instancing Redux
    13. Chapter 4. Color, Pixels, and Framebuffers
      1. Basic Color Theory
      2. Buffers and Their Uses
        1. Color Buffers
        2. Depth Buffer
        3. Stencil Buffer
        4. Clearing Buffers
        5. Masking Buffers
      3. Color and OpenGL
        1. Color Representation and OpenGL
        2. Vertex Colors
        3. Rasterization
      4. Multisampling
        1. Sample Shading
      5. Testing and Operating on Fragments
        1. Scissor Test
        2. Multisample Fragment Operations
        3. Stencil Test
        4. Stencil Examples
        5. Depth Test
        6. Blending
        7. Blending Factors
        8. Controlling Blending Factors
        9. The Blending Equation
        10. Dithering
        11. Logical Operations
        12. Occlusion Query
        13. Conditional Rendering
      6. Per-Primitive Antialiasing
        1. Antialiasing Lines
        2. Antialiasing Polygons
      7. Framebuffer Objects
        1. Advanced
        2. Renderbuffers
        3. Creating Renderbuffer Storage
        4. Framebuffer Attachments
        5. Framebuffer Completeness
        6. Invalidating Framebuffers
      8. Writing to Multiple Renderbuffers Simultaneously
        1. Advanced
        2. Selecting Color Buffers for Writing and Reading
        3. Dual-Source Blending
      9. Reading and Copying Pixel Data
        1. Clamping Returned Values
      10. Copying Pixel Rectangles
    14. Chapter 5. Viewing Transformations, Clipping, and Feedback
      1. Viewing
        1. Viewing Model
        2. Camera Model
        3. Orthographic Viewing Model
      2. User Transformations
        1. Matrix Multiply Refresher
        2. Homogeneous Coordinates
        3. Linear Transformations and Matrices
        4. Transforming Normals
        5. OpenGL Matrices
      3. OpenGL Transformations
        1. Viewport
        2. Multiple Viewports
        3. Advanced: z Precision
        4. Advanced: User Clipping
      4. Transform Feedback
        1. Transform Feedback Objects
        2. Transform Feedback Buffers
        3. Configuring Transform Feedback Varyings
        4. Starting and Stopping Transform Feedback
        5. Transform Feedback Example—Particle System
    15. Chapter 6. Textures
      1. Texture Mapping
      2. Basic Texture Types
      3. Creating and Initializing Textures
        1. Texture Formats
      4. Proxy Textures
      5. Specifying Texture Data
        1. Explicitly Setting Texture Data
        2. Using Pixel Unpack Buffers
        3. Copying Data from the Framebuffer
        4. Loading Images from Files
        5. Retrieving Texture Data
        6. Texture Data Layout
      6. Sampler Objects
        1. Sampler Parameters
      7. Using Textures
        1. Texture Coordinates
        2. Arranging Texture Data
        3. Using Multiple Textures
      8. Complex Texture Types
        1. 3D Textures
        2. Array Textures
        3. Cube-Map Textures
        4. Shadow Samplers
        5. Depth-Stencil Textures
        6. Buffer Textures
      9. Texture Views
      10. Compressed Textures
      11. Filtering
        1. Linear Filtering
        2. Using and Generating Mipmaps
        3. Calculating the Mipmap Level
        4. Mipmap Level-of-Detail Control
      12. Advanced Texture Lookup Functions
        1. Explicit Level of Detail
        2. Explicit Gradient Specification
        3. Texture Fetch with Offsets
        4. Projective Texturing
        5. Texture Queries in Shaders
        6. Gathering Texels
        7. Combining Special Functions
      13. Point Sprites
        1. Textured Point Sprites
        2. Controlling the Appearance of Points
      14. Rendering to Texture Maps
        1. Discarding Rendered Data
      15. Chapter Summary
        1. Texture Redux
        2. Texture Best Practices
    16. Chapter 7. Light and Shadow
      1. Lighting Introduction
      2. Classic Lighting Model
        1. Fragment Shaders for Different Light Styles
        2. Moving Calculations to the Vertex Shader
        3. Multiple Lights and Materials
        4. Lighting Coordinate Systems
        5. Limitations of the Classic Lighting Model
      3. Advanced Lighting Models
        1. Hemisphere Lighting
        2. Image-Based Lighting
        3. Lighting with Spherical Harmonics
      4. Shadow Mapping
        1. Creating a Shadow Map
    17. Chapter 8. Procedural Texturing
      1. Procedural Texturing
        1. Regular Patterns
        2. Toy Ball
        3. Lattice
        4. Procedural Shading Summary
      2. Bump Mapping
        1. Application Setup
        2. Vertex Shader
        3. Fragment Shader
        4. Normal Maps
      3. Antialiasing Procedural Textures
        1. Sources of Aliasing
        2. Avoiding Aliasing
        3. Increasing Resolution
        4. Antialiasing High Frequencies
        5. Frequency Clamping
        6. Procedural Antialiasing Summary
      4. Noise
        1. Definition of Noise
        2. Noise Textures
        3. Trade-offs
        4. A Simple Noise Shader
        5. Turbulence
        6. Marble
        7. Granite
        8. Wood
        9. Noise Summary
      5. Further Information
    18. Chapter 9. Tessellation Shaders
      1. Tessellation Shaders
      2. Tessellation Patches
      3. Tessellation Control Shaders
        1. Generating Output-Patch Vertices
        2. Tessellation Control Shader Variables
        3. Controlling Tessellation
      4. Tessellation Evaluation Shaders
        1. Specifying the Primitive Generation Domain
        2. Specifying the Face Winding for Generated Primitives
        3. Specifying the Spacing of Tessellation Coordinates
        4. Additional Tessellation Evaluation Shader layout Options
        5. Specifying a Vertex’s Position
        6. Tessellation Evaluation Shader Variables
      5. A Tessellation Example: The Teapot
        1. Processing Patch Input Vertices
        2. Evaluating Tessellation Coordinates for the Teapot
      6. Additional Tessellation Techniques
        1. View-Dependent Tessellation
        2. Shared Tessellated Edges and Cracking
        3. Displacement Mapping
    19. Chapter 10. Geometry Shaders
      1. Creating a Geometry Shader
      2. Geometry Shader Inputs and Outputs
        1. Geometry Shader Inputs
        2. Special Geometry Shader Primitives
        3. Geometry Shader Outputs
      3. Producing Primitives
        1. Culling Geometry
        2. Geometry Amplification
      4. Advanced Transform Feedback
        1. Multiple Output Streams
        2. Primitive Queries
        3. Using Transform Feedback Results
      5. Geometry Shader Instancing
      6. Multiple Viewports and Layered Rendering
        1. Viewport Index
        2. Layered Rendering
      7. Chapter Summary
        1. Geometry Shader Redux
        2. Geometry Shader Best Practices
    20. Chapter 11. Memory
      1. Using Textures for Generic Data Storage
        1. Binding Textures to Image Units
        2. Reading from and Writing to Images
      2. Shader Storage Buffer Objects
        1. Writing Structured Data
      3. Atomic Operations and Synchronization
        1. Atomic Operations on Images
        2. Atomic Operations on Buffers
        3. Sync Objects
        4. Image Qualifiers and Barriers
        5. High Performance Atomic Counters
      4. Example
        1. Order-Independent Transparency
    21. Chapter 12. Compute Shaders
      1. Overview
      2. Workgroups and Dispatch
        1. Knowing Where You Are
      3. Communication and Synchronization
        1. Communication
        2. Synchronization
      4. Examples
        1. Physical Simulation
        2. Image Processing
      5. Chapter Summary
        1. Compute Shader Redux
        2. Compute Shader Best Practices
    22. Appendix A. Basics of GLUT: The OpenGL Utility Toolkit
      1. Initializing and Creating a Window
      2. Accessing Functions
      3. Handling Window and Input Events
      4. Managing a Background Process
      5. Running the Program
    23. Appendix B. OpenGL ES and WebGL
      1. OpenGL ES
      2. WebGL
        1. Setting up WebGL within an HTML5 page
        2. Initializing Shaders in WebGL
        3. Initializing Vertex Data in WebGL
        4. Using Texture Maps in WebGL
    24. Appendix C. Built-in GLSL Variables and Functions
      1. Built-in Variables
        1. Built-in Variable Declarations
        2. Built-in Variable Descriptions
      2. Built-in Constants
      3. Built-in Functions
        1. Angle and Trigonometry Functions
        2. Exponential Functions
        3. Common Functions
        4. Floating-Point Pack and Unpack Functions
        5. Geometric Functions
        6. Matrix Functions
        7. Vector Relational Functions
        8. Integer Functions
        9. Texture Functions
        10. Atomic-Counter Functions
        11. Atomic Memory Functions
        12. Image Functions
        13. Fragment Processing Functions
        14. Noise Functions
        15. Geometry Shader Functions
        16. Shader Invocation Control Functions
        17. Shader Memory Control Functions
    25. Appendix D. State Variables
      1. The Query Commands
      2. OpenGL State Variables
        1. Current Values and Associated Data
        2. Vertex Array Object State
        3. Vertex Array Data
        4. Buffer Object State
        5. Transformation State
        6. Coloring State
        7. Rasterization State
        8. Multisampling
        9. Textures
        10. Textures
        11. Textures
        12. Textures
        13. Texture Environment
        14. Pixel Operations
        15. Framebuffer Controls
        16. Framebuffer State
        17. Framebuffer State
        18. Frambuffer State
        19. Renderbuffer State
        20. Renderbuffer State
        21. Pixel State
        22. Shader Object State
        23. Shader Program Pipeline Object State
        24. Shader Program Object State
        25. Program Interface State
        26. Program Object Resource State
        27. Vertex and Geometry Shader State
        28. Query Object State
        29. Image State
        30. Transform Feedback State
        31. Atomic Counter State
        32. Shader Storage Buffer State
        33. Sync Object State
        34. Hints
        35. Compute Dispatch State
        36. Implementation-Dependent Values
        37. Tessellation Shader Implementation-Dependent Limits
        38. Geometry Shader Implementation-Dependent Limits
        39. Fragment Shader Implementation-Dependent Limits
        40. Implementation-Dependent Compute Shader Limits
        41. Implementation-Dependent Shader Limits
        42. Implementation-Dependent Debug Output State
        43. Implementation-Dependent Values
        44. Internal Format-Dependent Values
        45. Implementation-Dependent Transform Feedback Limits
        46. Framebuffer-Dependent Values
        47. Miscellaneous
    26. Appendix E. Homogeneous Coordinates and Transformation Matrices
      1. Homogeneous Coordinates
        1. Transforming Vertices
        2. Transforming Normals
      2. Transformation Matrices
        1. Translation
        2. Scaling
        3. Rotation
        4. Perspective Projection
        5. Orthographic Projection
    27. Appendix F. OpenGL and Window Systems
      1. Accessing New OpenGL Functions
        1. GLEW: The OpenGL Extension Wrangler
      2. GLX: OpenGL Extension for the X Window System
        1. Initialization
        2. Controlling Rendering
        3. GLX Prototypes
      3. WGL: OpenGL Extensions for Microsoft Windows
        1. Initialization
        2. Controlling Rendering
        3. WGL Prototypes
      4. OpenGL in Mac OS X: The Core OpenGL (CGL) API and the NSOpenGL Classes
      5. Mac OS X’s Core OpenGL Library
        1. Initialization
        2. Controlling Rendering
        3. CGL Prototypes
      6. The NSOpenGL Classes
        1. Initialization
    28. Appendix G. Floating-Point Formats for Textures, Framebuffers, and Renderbuffers
      1. Reduced-Precision Floating-Point Values
      2. 16-bit Floating-Point Values
      3. 10- and 11-bit Unsigned Floating-Point Values
    29. Appendix H. Debugging and Profiling OpenGL
      1. Creating a Debug Context
      2. Debug Output
        1. Debug Messages
        2. Filtering Messages
        3. Application-Generated Messages
      3. Debug Groups
        1. Naming Objects
      4. Profiling
        1. Profiling Tools
        2. In-Application Profiling
    30. Appendix I. Buffer Object Layouts
      1. Using Standard Layout Qualifiers
      2. The std140 Layout Rules
      3. The std430 Layout Rules
    31. Glossary
    32. Index
    33. Ad Page