How to do it...

  1. In the first pass, render the scene to the geometry buffers (see Using deferred shading for details).
  2. In the second pass, we'll use this fragment shader code to compute the AO factor. To do so, we first compute a matrix for converting the kernel points into camera space. When doing so, we use a vector from RandTex to rotate the kernel. This process is similar to computing the tangent space matrix in normal mapping. For more on this, see Using normal maps:
// Create the random tangent space matrixvec3 randDir = normalize( texture(RandTex, TexCoord.xy * randScale).xyz );vec3 n = normalize( texture(NormalTex, TexCoord).xyz );vec3 biTang = cross( n, randDir );// If n and randDir are parallel, n is in x-y planeif( length(biTang) ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.