~ubuntu-branches/ubuntu/utopic/castle-game-engine/utopic

« back to all changes in this revision

Viewing changes to src/x3d/opengl/glsl/ssao.glsl

  • Committer: Package Import Robot
  • Author(s): Abou Al Montacir
  • Date: 2013-04-27 18:06:40 UTC
  • Revision ID: package-import@ubuntu.com-20130427180640-eink4nmwzuivez1c
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#extension GL_ARB_texture_rectangle : enable
 
2
 
 
3
ivec2 screen_position();
 
4
float screen_get_depth(ivec2 position);
 
5
vec4 screen_get_color(ivec2 position);
 
6
 
 
7
uniform int screen_width;
 
8
uniform int screen_height;
 
9
 
 
10
// shader from http://www.pasteall.org/12282, http://www.youtube.com/watch?v=R_L-_oGTbqw
 
11
 
 
12
#define PI    3.14159265
 
13
 
 
14
float width = float(screen_width);
 
15
float height = float(screen_height);
 
16
 
 
17
uniform float near; //Z-near
 
18
uniform float far; //Z-far
 
19
 
 
20
int samples = 6;//8; //samples on the first ring
 
21
int rings = 3;//6; //ring count
 
22
 
 
23
vec2 rand(in vec2 coord) //generating random noise
 
24
{
 
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;
 
28
}
 
29
 
 
30
float readDepth(const in ivec2 coord)
 
31
{
 
32
  return (2.0 * near) / (far + near - screen_get_depth(coord) * (far-near));
 
33
}
 
34
 
 
35
float compareDepths( in float depth1, in float depth2 )
 
36
{
 
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;
 
43
  return ao;
 
44
}
 
45
 
 
46
void main(void)
 
47
{
 
48
  ivec2 current_pos = screen_position();
 
49
  float depth = readDepth(current_pos);
 
50
  float d;
 
51
 
 
52
  float aspect = width/height;
 
53
  vec2 noise = rand(vec2(current_pos));
 
54
 
 
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));
 
57
 
 
58
  w *= width/2.0; h *= height/2.0;  // JA added this line !!
 
59
 
 
60
  float pw;
 
61
  float ph;
 
62
 
 
63
  float ao = 0.0;
 
64
  float s = 0.0;
 
65
  float fade = 1.0;
 
66
 
 
67
  for (int i = 0 ; i < rings; i += 1)
 
68
  {
 
69
    fade *= 0.5;
 
70
    for (int j = 0 ; j < samples*i; j += 1)
 
71
    {
 
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;
 
77
      s += 1.0*fade;
 
78
    }
 
79
  }
 
80
 
 
81
  ao /= s;
 
82
  ao = 1.0-ao;
 
83
 
 
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);
 
88
 
 
89
  ao = ao * 0.8 + 0.2;
 
90
  gl_FragColor = vec4(color*vec3(ao),1.0);
 
91
  //gl_FragColor = vec4(vec3(ao), 1.0);  // ssao only
 
92
}