OpenGL 4 Shading Language Cookbook, Second Edition

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.

OpenGL 4 Shading Language Cookbook, Second Edition

Cookbook

David WolffDecember 2013

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.

Who This Book Is For

This book is for OpenGL programmers looking to use the modern features of GLSL 4 to create real-time, three-dimensional graphics. Familiarity with OpenGL programming, along with the typical 3D coordinate systems, projections, and transformations is assumed. It can also be useful for experienced GLSL programmers who are looking to implement the techniques that are presented here.

Table of Contents

Chapter 1: Getting Started with GLSL

Introduction

Using a function loader to access the latest OpenGL functionality

Using GLM for mathematics

Determining the GLSL and OpenGL version

Compiling a shader

Linking a shader program

Sending data to a shader using vertex attributes and vertex buffer objects

Getting a list of active vertex input attributes and locations

Sending data to a shader using uniform variables

Getting a list of active uniform variables

Using uniform blocks and uniform buffer objects

Getting debug messages

Building a C++ shader program class

Chapter 2: The Basics of GLSL Shaders

Introduction

Implementing diffuse, per-vertex shading with a single point light source

Implementing per-vertex ambient, diffuse, and specular (ADS) shading

Using functions in shaders

Implementing two-sided shading

Implementing flat shading

Using subroutines to select shader functionality

Discarding fragments to create a perforated look

Chapter 3: Lighting, Shading, and Optimization

Introduction

Shading with multiple positional lights

Shading with a directional light source

Using per-fragment shading for improved realism

Using the halfway vector for improved performance

Simulating a spotlight

Creating a cartoon shading effect

Simulating fog

Configuring the depth test

Chapter 4: Using Textures

Introduction

Applying a 2D texture

Applying multiple textures

Using alpha maps to discard pixels

Using normal maps

Simulating reflection with cube maps

Simulating refraction with cube maps

Applying a projected texture

Rendering to a texture

Using sampler objects

Chapter 5: Image Processing and Screen Space Techniques

Introduction

Applying an edge detection filter

Applying a Gaussian blur filter

Implementing HDR lighting with tone mapping

Creating a bloom effect

Using gamma correction to improve image quality

Using multisample anti-aliasing

Using deferred shading

Implementing order-independent transparency

Chapter 6: Using Geometry and Tessellation Shaders

Introduction

Point sprites with the geometry shader

Drawing a wireframe on top of a shaded mesh

Drawing silhouette lines using the geometry shader

Tessellating a curve

Tessellating a 2D quad

Tessellating a 3D surface

Tessellating based on depth

Chapter 7: Shadows

Introduction

Rendering shadows with shadow maps

Anti-aliasing shadow edges with PCF

Creating soft shadow edges with random sampling

Creating shadows using shadow volumes and the geometry shader

Chapter 8: Using Noise in Shaders

Introduction

Creating a noise texture using GLM

Creating a seamless noise texture

Creating a cloud-like effect

Creating a wood-grain effect

Creating a disintegration effect

Creating a paint-spatter effect

Creating a night-vision effect

Chapter 9: Particle Systems and Animation

Introduction

Animating a surface with vertex displacement

Creating a particle fountain

Creating a particle system using transform feedback

Creating a particle system using instanced particles

Simulating fire with particles

Simulating smoke with particles

Chapter 10: Using Compute Shaders

Introduction

Implementing a particle simulation with the compute shader

Using the compute shader for cloth simulation

Implementing an edge detection filter with the compute shader

Creating a fractal texture using the compute shader

What You Will Learn

Compile, debug, and communicate with shader programs

Use new features of GLSL 4 such as subroutines, sampler objects, and uniform blocks

Implement core lighting and shading techniques such as diffuse and specular shading, per-fragment shading, and spotlights

Use textures for a variety of effects including cube maps for reflection or refraction

Make use of compute shaders for physics, animation, and general computing

Learn how to use new OpenGL features such as shader storage buffer objects, and image load/store

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.

Authors

David Wolff

David Wolff is an associate professor in the Computer Science and Computer Engineering Department at Pacific Lutheran University (PLU). He received a PhD in Physics and an MS in Computer Science from Oregon State University. He has been teaching computer graphics to undergraduates at PLU for over 10 years, using OpenGL.

Table of Contents

Chapter 1: Getting Started with GLSL

Introduction

Using a function loader to access the latest OpenGL functionality

Using GLM for mathematics

Determining the GLSL and OpenGL version

Compiling a shader

Linking a shader program

Sending data to a shader using vertex attributes and vertex buffer objects

Getting a list of active vertex input attributes and locations

Sending data to a shader using uniform variables

Getting a list of active uniform variables

Using uniform blocks and uniform buffer objects

Getting debug messages

Building a C++ shader program class

Chapter 2: The Basics of GLSL Shaders

Introduction

Implementing diffuse, per-vertex shading with a single point light source

Alerts & Offers

Series & Level

We understand your time is important. Uniquely amongst the major publishers, we seek to develop and publish the broadest range of learning and information products on each technology. Every Packt product delivers a specific learning pathway, broadly defined by the Series type. This structured approach enables you to select the pathway which best suits your knowledge level, learning style and task objectives.

Learning

As a new user, these step-by-step tutorial guides will give you all the practical skills necessary to become competent and efficient.

Beginner's Guide

Friendly, informal tutorials that provide a practical introduction using examples, activities, and challenges.

Essentials

Fast paced, concentrated introductions showing the quickest way to put the tool to work in the real world.

Cookbook

A collection of practical self-contained recipes that all users of the technology will find useful for building more powerful and reliable systems.

Blueprints

Guides you through the most common types of project you'll encounter, giving you end-to-end guidance on how to build your specific solution quickly and reliably.

Mastering

Take your skills to the next level with advanced tutorials that will give you confidence to master the tool's most powerful features.

Starting

Accessible to readers adopting the topic, these titles get you into the tool or technology so that you can become an effective user.

Progressing

Building on core skills you already have, these titles share solutions and expertise so you become a highly productive power user.