| 1 |
////////////////////////////////////////////////////////////////////////////////////// |
| 2 |
// |
| 3 |
// |
| 4 |
// Screen Space Ambient Occlusion |
| 5 |
// |
| 6 |
// C.B. - 03/05/2008 |
| 7 |
// D.G-M. - 10/22/2010 |
| 8 |
// Adapted from notes by Crytek and Inigo Quilez |
| 9 |
// |
| 10 |
// IN: Depth buffer of the scene |
| 11 |
// r = recorded z, in [0:1] |
| 12 |
// OUT: AO shaded image |
| 13 |
// |
| 14 |
//////////////////////////////////////////////////////////////////////////////////////// |
| 15 |
|
| 16 |
//#extension GL_ARB_draw_buffers : enable |
| 17 |
|
| 18 |
////////////////////////////////////////////////////////////////////////////////////// |
| 19 |
// |
| 20 |
uniform sampler2D s2_Z; |
| 21 |
uniform sampler2D s2_R; |
| 22 |
uniform sampler2D s2_C; |
| 23 |
|
| 24 |
uniform float R; // Radius of neighborhood sphere |
| 25 |
uniform float F; // Amplification of shading |
| 26 |
uniform float Kz; // distance attenuation factor |
| 27 |
uniform int B_REF; // if 1 use random reflect of neighbours |
| 28 |
uniform vec3 P[256]; // The Neighbours in unit sphere |
| 29 |
const int N = 32; // Number of neighbouri |
| 30 |
// |
| 31 |
////////////////////////////////////////////////////////////////////////////////////// |
| 32 |
|
| 33 |
////////////////////////////////////////////////////////////////////////////////////// |
| 34 |
// |
| 35 |
// SSAO |
| 36 |
// |
| 37 |
// First implementation: use projected coordinates |
| 38 |
// |
| 39 |
float computeSSAO() |
| 40 |
{ |
| 41 |
float z = texture2D(s2_Z,gl_TexCoord[0].st).x; |
| 42 |
|
| 43 |
if(z>0.999) |
| 44 |
return 1.0; |
| 45 |
|
| 46 |
float SSAO = 0.; |
| 47 |
vec3 P0 = vec3(gl_TexCoord[0].st,z); |
| 48 |
vec3 PS,REF; |
| 49 |
float dz,zs; |
| 50 |
for(int c=0;c<N;c++) |
| 51 |
{ |
| 52 |
// TO DO: implement weighting function |
| 53 |
if(B_REF == 0) |
| 54 |
{ |
| 55 |
PS = P0 + R * P[c]; |
| 56 |
} |
| 57 |
else |
| 58 |
{ |
| 59 |
// reflect vector |
| 60 |
REF = texture2D(s2_R,gl_TexCoord[0].st).xyz; |
| 61 |
PS = P0 + R * reflect(P[c], 2.*(REF-vec3(0.5)) ); |
| 62 |
} |
| 63 |
|
| 64 |
zs = texture2D(s2_Z,PS.xy).r; |
| 65 |
dz = max(0.0, min(PS.z,1.0)-zs); // min is to avoid going behind background |
| 66 |
SSAO += dz * F * 1./(1.+Kz*dz*dz) / float(N); |
| 67 |
|
| 68 |
} |
| 69 |
return max(1.0-SSAO,0.0); |
| 70 |
} |
| 71 |
// |
| 72 |
// |
| 73 |
// |
| 74 |
////////////////////////////////////////////////////////////////////////////////////// |
| 75 |
|
| 76 |
|
| 77 |
void main (void) |
| 78 |
{ |
| 79 |
vec4 C = texture2D(s2_C,gl_TexCoord[0].st).rgba; |
| 80 |
|
| 81 |
float SSAO = computeSSAO(); |
| 82 |
|
| 83 |
gl_FragData[0] = SSAO*C; |
| 84 |
} |