OpenGL 4 Shading Language Cookbook
Build high-quality, real-time 3D graphics with OpenGL 4.6, GLSL 4.6 and C++17, 3rd Edition
David Wolff
- 472 pagine
- English
- ePUB (disponibile sull'app)
- Disponibile su iOS e Android
OpenGL 4 Shading Language Cookbook
Build high-quality, real-time 3D graphics with OpenGL 4.6, GLSL 4.6 and C++17, 3rd Edition
David Wolff
Informazioni sul libro
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.
Domande frequenti
Informazioni
Image Processing and Screen Space Techniques
- 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
- Screen space ambient occlusion
- Configuring the depth test
- Implementing order-independent transparency
Introduction
Applying an edge detection filter
Getting ready
- Width: This is used to set the width of the screen window in pixels
- Height: This is used to set the height of the screen window in pixels
- EdgeThreshold: This is the minimum value of g squared required to be considered on an edge
- RenderTex: This is the texture associated with the FBO
How to do it...
- The vertex shader just converts the position and normal to camera coordinates and passes them along to the fragment shader.
- The fragment shader applies the reflection model in the first pass, and applies the edge detection filter in the second pass:
in vec3 Position; in vec3 Normal;
uniform int Pass; // Pass number // The texture containing the results of the first pass layout( binding=0 ) uniform sampler2D RenderTex; uniform float EdgeThreshold; // The squared threshold // Light/material uniforms... layout( location = 0 ) out vec4 FragColor; const vec3 lum = vec3(0.2126, 0.7152, 0.0722); vec3 blinnPhong( vec3 pos, vec3 norm ) {
// ... } // Approximates the brightness of a RGB value. float luminance( vec3 color ) { return dot(lum, color); }
vec4 pass1() { return vec4(blinnPhong( Position, normalize(Normal) ),1.0); }
vec4 pass2() { ivec2 pix = ivec2(gl_FragCoord.xy); float s00 = luminance( texelFetchOffset(RenderTex, pix, 0, ivec2(-1,1)).rgb); float s10 = luminance( texelFetchOffset(RenderTex, pix, 0, ivec2(-1,0)).rgb); float s20 = luminance( texelFetchOffset(RenderTex, pix, 0, ivec2(-1,-1)).rgb); float s01 = luminance( texelFetchOffset(RenderTex, pix, 0, ivec2(0,1)).rgb); float s21 = luminance( texelFetchOffset(RenderTex, pix, 0, ivec2(0,-1)).rgb); float s02 = luminance( texelFetchOffset(RenderTex, pix, 0, ivec2(1,1)).rgb); float s12 = luminance( texelFetchOffset(RenderTex, pix, 0, ivec2(1,0)).rgb); float s22 = luminance( texelFetchOffset(RenderTex, pix, 0, ivec2(1,-1)).rgb); float sx = s00 + 2 * s10 + s20 - (s02 + 2 * s12 + s22); float sy = s00 + 2 * s01 + s02 - (s20 + 2 * s21 + s22); float g = sx * sx + sy * sy; if( g > EdgeThreshold ) return vec4(1.0); else return vec4(0.0,0.0,0.0,1.0); } void main() { if( Pass == 1 ) FragColor = pass1();
if( Pass == 2 ) FragColor = pass2(); }
- Select FBO, and clear the color/depth buffers
- Set the ...