Advertisement
Guest User

Untitled

a guest
Dec 21st, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #line 2
  2. /**
  3.  * @file        ssao.frag
  4.  *
  5.  * @author      FH Wedel, Henrik Patjens
  6.  * @date        November 2017
  7.  * @brief       Fragment-Shader zum Rendern von Objekten mit Forward-Rendering
  8.  *
  9.  * Diese Datei ist Bestandteil des Praktikums "Special Effects und Shaderprogrammierung".
  10.  *
  11.  * Copyright (C) 2017 by FH Wedel
  12.  */
  13.  
  14.  
  15. #define DIST_MIN 0.0001
  16. #define DIST_MAX 0.005
  17.  
  18.  
  19. // Version wird durch die utils.glsl definiert.
  20. out vec4 FragColor;                         /**< Farbe des Fragments */
  21.  
  22. // ----------------------------------------------------------------------------
  23. //
  24. // Uniforms
  25. //
  26. // ----------------------------------------------------------------------------
  27. layout (binding = 0) uniform sampler2D normalTex;
  28. layout (binding = 1) uniform sampler2D depthTex;
  29. //layout (RGBA8, binding = 2) uniform image2D discardTex;
  30. uniform int sampleSize;
  31. uniform mat4 ProjectionMatrix;
  32. uniform mat4 InverseProjectionMatrix;
  33. uniform int resX;
  34. uniform int resY;
  35.  
  36. // ----------------------------------------------------------------------------
  37. //
  38. // Variablen
  39. //
  40. // ----------------------------------------------------------------------------
  41.  
  42. // ----------------------------------------------------------------------------
  43. //
  44. // Funktionen
  45. //
  46. // ----------------------------------------------------------------------------
  47.  
  48. /**
  49. * Berechnet die View Space position des aktuellen Fragments
  50. */
  51. vec3 getViewPos(){
  52.     float x = (gl_FragCoord.x / resX) * 2.0 - 1.0;
  53.     float y = (gl_FragCoord.y / resY) * 2.0 - 1.0;
  54.     float z = texture(depthTex, gl_FragCoord.xy).r * 2.0 - 1.0;
  55.     vec4 projPos = vec4(x,y,z, 1.0);
  56.     vec4 viewPos = InverseProjectionMatrix * projPos;
  57.     return wdiv(viewPos);
  58. }
  59.  
  60. /**
  61.  * Einsprungpunkt fΓΌr den Fragment-Shader
  62.  */
  63. void main() {    
  64.     float occlusion = 0.0;
  65.  
  66.     vec3 origin = getViewPos();
  67.  
  68.     vec3 normal = normalize(texture(normalTex,gl_FragCoord.xy).rgb * 2.0 - 1.0);
  69.     //vec3 normal = normalize(texelFetch(normalTex, ivec2(gl_FragCoord.xy),0).xyz * 2.0 - 1.0);
  70.     vec3 u = normalize(vec3(rand(gl_FragCoord.xy),rand(gl_FragCoord.yz),rand(gl_FragCoord.xz)));
  71.     //TODO: n == u beachten!
  72.     vec3 tangent = normalize(cross(normal,u));
  73.     vec3 bitangent = normalize(cross(normal, tangent));
  74.     mat3 tbn = mat3(tangent, bitangent, normal);
  75.  
  76.     for(int i = 0; i < sampleSize; i++){
  77.         //sample position berechnen
  78.         vec3 kernel = tbn * sampleHemisphereCos(hammersley(i, sampleSize));
  79.         kernel = kernel + origin;
  80.         //projizieren
  81.         vec4 offset = vec4(kernel,1.0);
  82.         offset = ProjectionMatrix * offset;
  83.         vec3 fOffset = wdiv(offset);
  84.         fOffset.xy = fOffset.xy * 0.5 + 0.5; //scale & bias
  85.  
  86.         //sample tiefe berechnen
  87.         float depth = texture(depthTex, fOffset.xy).r * 2.0 - 1.0;
  88.  
  89.         //float dist = fOffset.z - depth;
  90.  
  91.         if(fOffset.z > depth){
  92.             occlusion += 1.0;
  93.         } /**else {
  94.             imageStore(discardTex, gl_FragCoord.xy, vec4(1,0,0,1));
  95.         }*/
  96.  
  97.     }
  98.  
  99.     occlusion = 1.0 - (occlusion/sampleSize);
  100.     FragColor = vec4(occlusion, 0.0, 0.0, 1.0);
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement