| 1 |
////////////////////////////////////////////////////////////////////////////////////// |
| 2 |
// |
| 3 |
// |
| 4 |
// EyeDome Lighting - oriented light version |
| 5 |
// |
| 6 |
// C.B. - 04/23/2008 |
| 7 |
// D.G-M. - 10/21/2010 |
| 8 |
// D.G-M. - 02/17/2014 |
| 9 |
// |
| 10 |
// IN: Depth buffer of the scene |
| 11 |
// r (red component) = recorded z |
| 12 |
// OUT: EDL shaded image |
| 13 |
// |
| 14 |
/////////////////////////////////////////////////////////////////////////////////////// |
| 15 |
//#extension GL_ARB_draw_buffers : enable |
| 16 |
//#version 110 |
| 17 |
|
| 18 |
/**************************************************/ |
| 19 |
uniform sampler2D s1_color; |
| 20 |
uniform sampler2D s2_depth; |
| 21 |
|
| 22 |
uniform float Pix_scale; // (relative) pixel scale in image |
| 23 |
uniform vec2 Neigh_pos_2D[8]; // array of neighbors (2D positions) |
| 24 |
uniform float Exp_scale; // exponential scale factor (for computed AO) |
| 25 |
|
| 26 |
uniform float Zm; // minimal depth in image |
| 27 |
uniform float ZM; // maximal depth in image |
| 28 |
|
| 29 |
uniform float Sx; // screen width (pix) |
| 30 |
uniform float Sy; // screen height (pix) |
| 31 |
|
| 32 |
uniform float Dist_to_neighbor_pix; // distance to neighbors (in pixel) |
| 33 |
uniform int PerspectiveMode; // whether perspective mode is enabled (1) or not (0) - for z-Buffer compensation |
| 34 |
|
| 35 |
uniform vec3 Light_dir; |
| 36 |
/**************************************************/ |
| 37 |
|
| 38 |
float fixDepth(float depth) |
| 39 |
{ |
| 40 |
if (PerspectiveMode == 1) |
| 41 |
{ |
| 42 |
//'1/z' depth-buffer transformation correction |
| 43 |
depth = (2.0 * ZM * Zm) / ((ZM + Zm) - (2.0 * depth - 1.0) * (ZM - Zm)); |
| 44 |
//eventually we want the depth to fall between 0 and 1 |
| 45 |
depth = (depth - Zm) / (ZM - Zm); |
| 46 |
} |
| 47 |
|
| 48 |
return clamp(1.0 - depth, 0.0, 1.0); |
| 49 |
} |
| 50 |
|
| 51 |
float computeObscurance(float depth, float scale) |
| 52 |
{ |
| 53 |
// light-plane point |
| 54 |
vec4 P = vec4( Light_dir.xyz , -dot(Light_dir.xyz,vec3(0.0, 0.0, depth)) ); |
| 55 |
|
| 56 |
float sum = 0.0; |
| 57 |
|
| 58 |
// contribution of each neighbor |
| 59 |
for(int c = 0; c < 8; c++) |
| 60 |
{ |
| 61 |
vec2 N_rel_pos = scale * Dist_to_neighbor_pix / vec2(Sx, Sy) * Neigh_pos_2D[c]; //neighbor relative position |
| 62 |
vec2 N_abs_pos = gl_TexCoord[0].st + N_rel_pos; //neighbor absolute position |
| 63 |
|
| 64 |
// version with background shading |
| 65 |
float Zn = fixDepth(texture2D(s2_depth, N_abs_pos).r); //depth of the real neighbor |
| 66 |
float Znp = dot(vec4(N_rel_pos, Zn, 1.0), P); //depth of the in-plane neighbor |
| 67 |
|
| 68 |
// obscurance (pseudo angle version) |
| 69 |
sum += max(0.0, Znp) / scale; |
| 70 |
} |
| 71 |
|
| 72 |
return sum; |
| 73 |
} |
| 74 |
|
| 75 |
void main (void) |
| 76 |
{ |
| 77 |
// ambient occlusion |
| 78 |
vec3 rgb = texture2D(s1_color, gl_TexCoord[0].st).rgb; |
| 79 |
float depth = fixDepth(texture2D(s2_depth, gl_TexCoord[0].st).r); |
| 80 |
|
| 81 |
if (depth > 0.001) |
| 82 |
{ |
| 83 |
float f = computeObscurance(depth, Pix_scale); |
| 84 |
f = exp(-Exp_scale*f); |
| 85 |
|
| 86 |
gl_FragData[0] = vec4(f*rgb, 1.0); |
| 87 |
} |
| 88 |
else |
| 89 |
{ |
| 90 |
gl_FragData[0] = vec4(rgb, 1.0); |
| 91 |
} |
| 92 |
} |