You are previewing OpenGL 4.0 Shading Language Cookbook.
O'Reilly logo
OpenGL 4.0 Shading Language Cookbook

Book Description

With over 60 recipes, this Cookbook will teach you both the elementary and finer points of the OpenGL Shading Language, and get you familiar with the specific features of GLSL 4.0. A totally practical, hands-on guide.

  • A full set of recipes demonstrating simple and advanced techniques for producing high-quality, real-time 3D graphics using GLSL 4.0

  • How to use the OpenGL Shading Language to implement lighting and shading techniques

  • Use the new features of GLSL 4.0 including tessellation and geometry shaders

  • How to use textures in GLSL as part of a wide variety of techniques from basic texture mapping to deferred shading

  • Simple, easy-to-follow examples with GLSL source code, as well as a basic description of the theory behind each technique

  • In Detail

    The 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.0, the language has been further refined to provide programmers with greater flexibility, and additional features have been added such as an entirely new stage called the tessellation shader.

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

    The OpenGL Shading Language 4.0 Cookbook is a practical guide that takes you from the basics of programming with GLSL 4.0 and OpenGL 4.0, through basic lighting and shading techniques, to more advanced techniques and effects. It presents techniques for producing basic lighting and shading effects; 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, shadowing, tessellation and geometry shaders, noise, and animation.

    The OpenGL Shading Language 4.0 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.0 Shading Language Cookbook
      1. OpenGL 4.0 Shading Language Cookbook
      2. Credits
      3. About the Author
      4. About the Reviewers
      5. www.PacktPub.com
        1. Support files, eBooks, discount offers and more
          1. Why subscribe?
          2. Free access for Packt account holders
      6. 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 for this book
          2. Errata
          3. Piracy
          4. Questions
      7. 1. Getting Started with GLSL 4.0
        1. Introduction
          1. The OpenGL Shading Language
          2. Profiles: Core vs. Compatibility
        2. Using the GLEW Library to access the latest OpenGL functionality
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. GLEW visualinfo
            2. GLEW glewinfo
            3. Checking for extension availability at runtime
          5. See also
        3. Using the GLM library 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...
            1. How it works...
            2. There's more...
              1. Deleting a shader object
            3. See also
        6. Linking a shader program
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
              1. Deleting a shader program
            3. See also
        7. Sending data to a shader using per-vertex attributes and vertex buffer objects
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
              1. Using layout qualifiers
              2. Using element arrays
              3. Interleaved arrays
            3. See also
        8. Getting a list of active vertex input attributes and indices
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
            3. 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...
            1. How it works...
            2. There's more...
            3. See also
        11. Using uniform blocks and uniform buffer objects
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
              1. Using an instance name with a uniform block
              2. Using layout qualifiers with uniform blocks
            3. See also
        12. Building a C++ shader program class
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      8. 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 vs. 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...
            1. How it works...
            2. There's more...
            3. 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. 3. Lighting, Shading Effects, and Optimizations
        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...
            1. How it works...
            2. There's more...
            3. See also
        4. Using per-fragment shading for improved realism
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
            3. 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
      10. 4. Using Textures
        1. Introduction
        2. Applying a 2D texture
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
            3. See also
        3. Applying multiple textures
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
            3. See also
        4. Using alpha maps to discard pixels
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
            3. See also
        5. Using normal maps
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. 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. Both sides of the object?
          5. See also
        8. Image-based lighting
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        9. Applying a projected texture
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
            3. See also
        10. Rendering to a texture
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
            3. See also
      11. 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. Creating a "bloom" effect
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using low-res textures
          5. See also
        5. Using gamma correction to improve image quality
          1. How to do it...
          2. How it works...
          3. There's more...
        6. Using multisample anti-aliasing
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
        7. Using deferred shading
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      12. 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...
            1. How it works...
            2. There's more...
            3. See also
        4. Drawing silhouette lines using the geometry shader
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
            3. See also
        5. Tessellating a curve
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. 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...
            1. How it works...
            2. 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. 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...
            1. How it works...
            2. There's more...
            3. See also
        4. Creating soft shadow edges with random sampling
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
            3. See also
        5. Improving realism with prebaked ambient occlusion
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
              1. Screen-space ambient occlusion
              2. Another technique for dynamic ambient occlusion
      14. 8. Using Noise in Shaders
        1. Introduction
        2. Creating a noise texture using libnoise
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
            3. See also
        3. Creating a seamless noise texture
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Creating a cloud-like effect
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
            3. See also
        5. Creating a wood grain effect
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
            3. See also
        6. Creating a disintegration effect
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. See also
        7. Creating a paint-spatter effect
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
            3. 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
      15. 9. Animation and Particles
        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. Querying transform feedback results
            2. 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