Advertisement
Guest User

Untitled

a guest
Aug 14th, 2014
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. #version 330
  2. in vec2 texcoord;
  3.  
  4. uniform sampler2D texNorm;
  5. uniform sampler2D texDepth;
  6.  
  7. uniform mat4 invProjViewMat;
  8.  
  9. layout(location = 0) out vec4 fragColour;
  10.  
  11. vec3 get_v_pos(in vec2 coord) {
  12.     float rawDepth = texture(texDepth, coord).r * 2.f - 1.f;
  13.     float viewDepth = 2.f * 0.1f * 64.f / (64.f + 0.1f - rawDepth * (64.f - 0.1f));
  14.  
  15.     vec2 xy = vec4(invProjViewMat * vec4(texcoord * 2.f - 1.f, rawDepth, 1.f)).xy;
  16.     return vec3(xy, viewDepth);
  17. }
  18.  
  19. const float distanceThreshold = 5.f;
  20. const vec2 filterRadius = vec2(10.f / 1280.f, 10.f / 720.f);
  21.  
  22. const int sampleCount = 16;
  23. const vec2 poisson16[] = vec2[](    // These are the Poisson Disk Samples
  24.                                 vec2( -0.94201624,  -0.39906216 ),
  25.                                 vec2(  0.94558609,  -0.76890725 ),
  26.                                 vec2( -0.094184101, -0.92938870 ),
  27.                                 vec2(  0.34495938,   0.29387760 ),
  28.                                 vec2( -0.91588581,   0.45771432 ),
  29.                                 vec2( -0.81544232,  -0.87912464 ),
  30.                                 vec2( -0.38277543,   0.27676845 ),
  31.                                 vec2(  0.97484398,   0.75648379 ),
  32.                                 vec2(  0.44323325,  -0.97511554 ),
  33.                                 vec2(  0.53742981,  -0.47373420 ),
  34.                                 vec2( -0.26496911,  -0.41893023 ),
  35.                                 vec2(  0.79197514,   0.19090188 ),
  36.                                 vec2( -0.24188840,   0.99706507 ),
  37.                                 vec2( -0.81409955,   0.91437590 ),
  38.                                 vec2(  0.19984126,   0.78641367 ),
  39.                                 vec2(  0.14383161,  -0.14100790 )
  40.                                );
  41.  
  42. void main() {
  43.     vec3 pos = get_v_pos(texcoord);
  44.  
  45.     vec3 normal = normalize(texture(texNorm, texcoord).rgb * 2.f - 1.f);
  46.  
  47.     float ambientOcclusion = 0;
  48.     // perform AO
  49.     for (int i = 0; i < sampleCount; i++) {
  50.         // sample at an offset specified by the current Poisson-Disk sample and scale it by a radius (has to be in Texture-Space)
  51.         vec2 sampleTexCoord = texcoord + (poisson16[i] * (filterRadius));
  52.         vec3 samplePos = get_v_pos(sampleTexCoord);
  53.         vec3 sampleDir = normalize(samplePos - pos);
  54.  
  55.         // angle between SURFACE-NORMAL and SAMPLE-DIRECTION (vector from SURFACE-POSITION to SAMPLE-POSITION)
  56.         float NdotS = max(dot(normal, sampleDir), 0);
  57.         // distance between SURFACE-POSITION and SAMPLE-POSITION
  58.         float VPdistSP = distance(pos, samplePos);
  59.  
  60.         // a = distance function
  61.         float a = 1.0 - smoothstep(distanceThreshold, distanceThreshold * 2, VPdistSP);
  62.         // b = dot-Product
  63.         float b = NdotS;
  64.  
  65.         ambientOcclusion += (a * b);
  66.     }
  67.  
  68.     float ao = ambientOcclusion / sampleCount;
  69.     fragColour = vec4(ao, ao, ao, 1.f);
  70.     //fragColour = vec4(normal / 2.f + 0.5f, 1.f);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement