You are previewing OpenGL 4 Shading Language Cookbook Second Edition.
O'Reilly logo
OpenGL 4 Shading Language Cookbook Second Edition

Book Description

Acquiring the skills of OpenGL Shading Language is so much easier with this cookbook. You’ll be creating graphics rather than learning theory, gaining a high level of capability in modern 3D programming along the way.

  • Discover simple and advanced techniques for leveraging modern OpenGL and GLSL

  • Learn how to use the newest features of GLSL including compute shaders, geometry, and tessellation shaders

  • Get to grips with a wide range of techniques for implementing shadows using shadow maps, shadow volumes, and more

  • Clear, easy-to-follow examples with detailed explanations and full, cross-platform source code available from GitHub

  • In Detail

    OpenGL Shading Language (GLSL) is a programming language used for customizing parts of the OpenGL graphics pipeline that were formerly fixed-function, and are executed directly on the GPU. It provides programmers with unprecedented flexibility for implementing effects and optimizations utilizing the power of modern GPUs. With Version 4, the language has been further refined to provide programmers with greater power and flexibility, with new stages such as tessellation and compute.

    OpenGL 4 Shading Language Cookbook provides easy-to-follow examples that first walk you through the theory and background behind each technique, and then go on to provide and explain the GLSL and OpenGL code needed to implement it. Beginner level through to advanced techniques are presented including topics such as texturing, screen-space techniques, lighting, shading, tessellation shaders, geometry shaders, compute shaders, and shadows.

    OpenGL Shading Language 4 Cookbook is a practical guide that takes you from the fundamentals of programming with modern GLSL and OpenGL, through to advanced techniques. The recipes build upon each other and take you quickly from novice to advanced level code.

    You’ll see essential lighting and shading techniques; examples that demonstrate how to make use of textures for a wide variety of effects and as part of other techniques; examples of screen-space techniques including HDR rendering, bloom, and blur; shadowing techniques; tessellation, geometry, and compute shaders; how to use noise effectively; and animation with particle systems.

    OpenGL Shading Language 4 Cookbook 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.

    Table of Contents

    1. OpenGL 4 Shading Language Cookbook Second Edition
      1. Table of Contents
      2. OpenGL 4 Shading Language Cookbook Second Edition
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      8. 1. Getting Started with GLSL
        1. Introduction
          1. The OpenGL Shading Language
          2. Profiles – Core vs. Compatibility
        2. Using a function loader to access the latest OpenGL functionality
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Generating a C++ loader
            2. No-load styles
            3. Using Extensions
          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...
          4. See also
        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. Deleting a Shader program
          5. See also
        7. 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
        8. 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
        9. 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
        10. 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
        11. 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
        12. Getting debug messages
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        13. Building a C++ shader program class
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      9. 2. The Basics of GLSL Shaders
        1. Introduction
          1. Vertex and fragment shaders
          2. Replicating the old fixed functionality
        2. Implementing diffuse, 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 per-vertex ambient, diffuse, and specular (ADS) shading
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using a non-local 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
      10. 3. Lighting, Shading, and Optimization
        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. Using the halfway vector for improved performance
          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. Configuring the depth test
          1. How to do it...
          2. How it works...
          3. See also
      11. 4. 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. Simulating reflection with cube maps
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. 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
        8. Applying a projected texture
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        9. Rendering to a texture
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        10. Using sampler objects
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      12. 5. 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...
        7. Using multisample anti-aliasing
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Using deferred shading
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        9. Implementing order-independent transparency
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      13. 6. 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...
        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...
        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
      14. 7. 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
      15. 8. Using Noise in Shaders
        1. Introduction
          1. See Also…
        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 night-vision effect
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      16. 9. 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
            3. Recycling particles
          5. See also
        5. Creating a particle system using instanced particles
          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
      17. 10. 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. 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
        4. 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
        5. Creating a fractal texture using the compute shader
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      18. Index