O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

OpenGL 4 Shading Language Cookbook - Third Edition

Book Description

Over 70 recipes that cover advanced techniques for 3D programming such as lighting, shading, textures, particle systems, and image processing with OpenGL 4.6

Key Features

  • Explore techniques for implementing shadows using shadow maps and shadow volumes
  • Learn to use GLSL features such as compute, geometry, and tessellation shaders
  • Use GLSL to create a wide variety of modern, realistic visual effects

Book Description

OpenGL 4 Shading Language Cookbook, Third Edition provides easy-to-follow recipes that first walk you through the theory and background behind each technique, and then proceed to showcase and explain the GLSL and OpenGL code needed to implement them.

The book begins by familiarizing you with beginner-level topics such as compiling and linking shader programs, saving and loading shader binaries (including SPIR-V), and using an OpenGL function loader library. We then proceed to cover basic lighting and shading effects. After that, you'll learn to use textures, produce shadows, and use geometry and tessellation shaders. Topics such as particle systems, screen-space ambient occlusion, deferred rendering, depth-based tessellation, and physically based rendering will help you tackle advanced topics.

OpenGL 4 Shading Language Cookbook, Third Edition also covers advanced topics such as shadow techniques (including the two of the most common techniques: shadow maps and shadow volumes). You will learn how to use noise in shaders and how to use compute shaders.

The book provides examples of modern shading techniques that can be used as a starting point for programmers to expand upon to produce modern, interactive, 3D computer-graphics applications.

What you will learn

  • Compile, debug, and communicate with shader programs
  • Use compute shaders for physics, animation, and general computing
  • Learn about features such as shader storage buffer objects and image load/store
  • Utilize noise in shaders and learn how to use shaders in animations
  • Use textures for various effects including cube maps for reflection or refraction
  • Understand physically based reflection models and the SPIR-V Shader binary
  • Learn how to create shadows using shadow maps or shadow volumes
  • Create particle systems that simulate smoke, fire, and other effects

Who this book is for

If you are a graphics programmer looking to learn the GLSL shading language, this book is for you. A basic understanding of 3D graphics and programming experience with C++ are required.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. OpenGL 4 Shading Language Cookbook Third Edition
  3. Packt Upsell
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Getting Started with GLSL
    1. Introduction
      1. GLSL
      2. Profiles – core versus compatibility
    2. Using a loading library to access the latest OpenGL functionality
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Using GLM for mathematics
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Using the GLM types as input to OpenGL
      5. See also
    4. Determining the GLSL and OpenGL version
      1. How to do it...
      2. How it works...
      3. There's more...
    5. Compiling a shader
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Deleting a shader object
      5. See also
    6. Linking a shader program
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Shader input/output variables
        2. Deleting a shader program
      5. See also
    7. Saving and loading a shader binary
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Loading a SPIR-V shader program
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  7. Working with GLSL Programs
    1. Introduction
    2. Sending data to a shader using vertex attributes and vertex buffer objects
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Separate attribute format
        2. Fragment shader output
        3. Specifying attribute indexes without using layout qualifiers
        4. Using element arrays
        5. Interleaved arrays
      5. See also
    3. Getting a list of active vertex input attributes and locations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Sending data to a shader using uniform variables
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Getting a list of active uniform variables
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Using uniform blocks and uniform buffer objects
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Using an instance name with a uniform block
        2. Using layout qualifiers with uniform blocks
      5. See also
    7. Using program pipelines
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Getting debug messages
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    9. Building a C++ shader program class
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  8. The Basics of GLSL Shaders
    1. Introduction
      1. Vertex and fragment shaders
      2. Learning the basics first
    2. Diffuse and per-vertex shading with a single point light source
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Implementing the Phong reflection model
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Using a nonlocal viewer
        2. Per-vertex versus per-fragment
        3. Directional lights
        4. Light attenuation with distance
      5. See also
    4. Using functions in shaders
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. The const qualifier
        2. Function overloading
        3. Passing arrays or structures to a function
      5. See also
    5. Implementing two-sided shading
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Using two-sided rendering for debugging
      5. See also
    6. Implementing flat shading
      1. How to do it...
      2. How it works...
      3. See also
    7. Using subroutines to select shader functionality
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Discarding fragments to create a perforated look
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  9. Lighting and Shading
    1. Introduction
    2. Shading with multiple positional lights
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Shading with a directional light source
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Using per-fragment shading for improved realism
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. The Blinn-Phong reflection model
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Simulating a spotlight
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Creating a cartoon shading effect
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Simulating fog
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Computing distance from the eye
      5. See also
    9. A physically-based reflection model
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  10. Using Textures
    1. Introduction
    2. Applying a 2D texture
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Specifying the sampler binding within GLSL
      5. See also
    3. Applying multiple textures
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Using alpha maps to discard pixels
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Using normal maps
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Parallax mapping
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Steep parallax mapping with self shadowing
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Simulating reflection with cube maps
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Simulating refraction with cube maps
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. The Fresnel equations
        2. Chromatic aberration
        3. Refracting through both sides of the object
      5. See also
    10. Applying a projected texture
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    11. Rendering to a texture
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    12. Using sampler objects
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    13. Diffuse image-based lighting
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  11. Image Processing and Screen Space Techniques
    1. Introduction
    2. Applying an edge detection filter
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Optimization techniques
      5. See also
    3. Applying a Gaussian blur filter
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Implementing HDR lighting with tone mapping
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Creating a bloom effect
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Using gamma correction to improve image quality
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    7. Using multisample anti-aliasing
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Using deferred shading
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Screen space ambient occlusion
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Configuring the depth test
      1. How to do it...
      2. How it works...
      3. See also
    11. Implementing order-independent transparency
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  12. Using Geometry and Tessellation Shaders
    1. Introduction
      1. The shader pipeline extended
      2. The geometry shader
      3. The tessellation shaders
    2. Point sprites with the geometry shader
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Drawing a wireframe on top of a shaded mesh
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also...
    4. Drawing silhouette lines using the geometry shader
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Tessellating a curve
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Tessellating a 2D quad
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Tessellating a 3D surface
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Tessellating based on depth
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  13. Shadows
    1. Introduction
    2. Rendering shadows with shadow maps
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Aliasing
        2. Rendering back faces only for the shadow map
      5. See also
    3. Anti-aliasing shadow edges with PCF
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Creating soft shadow edges with random sampling
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Creating shadows using shadow volumes and the geometry shader
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  14. Using Noise in Shaders
    1. Introduction
    2. Creating a noise texture using GLM
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Creating a seamless noise texture
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Creating a cloud-like effect
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Creating a wood-grain effect
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Creating a disintegration effect
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Creating a paint-spatter effect
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Creating a rusted metal effect
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Creating a night-vision effect
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  15. Particle Systems and Animation
    1. Introduction
    2. Animating a surface with vertex displacement
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Creating a particle fountain
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Creating a particle system using transform feedback
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Using layout qualifiers
        2. Querying transform feedback results
      5. See also
    5. Creating a particle system using instanced meshes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Simulating fire with particles
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Simulating smoke with particles
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  16. Using Compute Shaders
    1. Introduction
      1. Compute space and work groups
      2. Executing the compute shader
    2. Implementing a particle simulation with the compute shader
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Creating a fractal texture using the compute shader
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Using the compute shader for cloth simulation
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Implementing an edge detection filter with the compute shader
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  17. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think