1
#extension GL_ARB_texture_rectangle : enable
3
ivec2 screen_position();
4
float screen_get_depth(ivec2 position);
5
vec4 screen_get_color(ivec2 position);
7
uniform int screen_width;
8
uniform int screen_height;
10
// shader from http://www.pasteall.org/12282, http://www.youtube.com/watch?v=R_L-_oGTbqw
14
float width = float(screen_width);
15
float height = float(screen_height);
17
uniform float near; //Z-near
18
uniform float far; //Z-far
20
int samples = 6;//8; //samples on the first ring
21
int rings = 3;//6; //ring count
23
vec2 rand(in vec2 coord) //generating random noise
25
float noiseX = (fract(sin(dot(coord ,vec2(12.9898,78.233))) * 43758.5453));
26
float noiseY = (fract(sin(dot(coord ,vec2(12.9898,78.233)*2.0)) * 43758.5453));
27
return vec2(noiseX,noiseY)*0.004;
30
float readDepth(const in ivec2 coord)
32
return (2.0 * near) / (far + near - screen_get_depth(coord) * (far-near));
35
float compareDepths( in float depth1, in float depth2 )
37
const float aoCap = 0.8;//1.0;
38
const float aoMultiplier = 100.0;
39
const float depthTolerance = 0.0001;
40
const float aorange = 60.0;// units in space the AO effect extends to (this gets divided by the camera far range
41
float diff = sqrt(clamp(1.0-(depth1-depth2) / (aorange/(far-near)),0.0,1.0));
42
float ao = min(aoCap,max(0.0,depth1-depth2-depthTolerance) * aoMultiplier) * diff;
48
ivec2 current_pos = screen_position();
49
float depth = readDepth(current_pos);
52
float aspect = width/height;
53
vec2 noise = rand(vec2(current_pos));
55
float w = (1.0 / width)/clamp(depth,0.05,1.0)+(noise.x*(1.0-noise.x));
56
float h = (1.0 / height)/clamp(depth,0.05,1.0)+(noise.y*(1.0-noise.y));
58
w *= width/2.0; h *= height/2.0; // JA added this line !!
67
for (int i = 0 ; i < rings; i += 1)
70
for (int j = 0 ; j < samples*i; j += 1)
72
float step = PI*2.0 / float(samples*i);
73
pw = (cos(float(j)*step)*float(i));
74
ph = (sin(float(j)*step)*float(i));
75
d = readDepth(current_pos + ivec2(int(pw * w), int(ph * h)));
76
ao += compareDepths(depth,d)*fade;
84
vec3 color = screen_get_color(current_pos).rgb;
85
//vec3 luminance = texture2D(bgl_LuminanceTexture,vec2(current_pos)).rgb;
86
//luminance = clamp(max(0.0,luminance-0.2)+max(0.0,luminance-0.2)+max(0.0,luminance-0.2),0.0,1.0);
87
//gl_FragColor = vec4(color*mix(vec3(ao),vec3(1.0),luminance),1.0);
90
gl_FragColor = vec4(color*vec3(ao),1.0);
91
//gl_FragColor = vec4(vec3(ao), 1.0); // ssao only