Advertisement
Guest User

Untitled

a guest
May 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.35 KB | None | 0 0
  1.  
  2.  
  3. // ====== rsm.vs =============================
  4.  
  5. #version 330 core
  6.  
  7. uniform mat4 model;
  8. uniform mat4 lightspace;
  9.  
  10. layout(location=0) in vec3 invertex;
  11. layout(location=1) in vec2 intexcoord;
  12. layout(location=2) in vec3 innormal;
  13.  
  14. out vec3 wsposition;
  15. out vec3 wsnormal;
  16. out vec2 texcoord;
  17.  
  18. void main(){
  19.     vec4 invertex4 = vec4(invertex, 1.0);
  20.     wsposition = (model * invertex4).xyz;
  21.     wsnormal = normalize(transpose(inverse(mat3(model))) * innormal);
  22.     texcoord = intexcoord;
  23.     gl_Position = lightspace * model * invertex4;
  24. }
  25.  
  26.  
  27. // ======= rsm.fs ===============================
  28.  
  29. #version 330 core
  30.  
  31. uniform sampler2D tex_color;
  32. uniform vec4 basecolor;
  33.  
  34. in vec3 wsposition;
  35. in vec3 wsnormal;
  36. in vec2 texcoord;
  37.  
  38. layout(location = 0) out vec4 out_position;
  39. layout(location = 1) out vec4 out_normal;
  40. layout(location = 2) out vec4 out_flux;
  41.  
  42. void main(void) {
  43.     out_position = vec4(wsposition, 1.0);
  44.     out_normal = vec4(normalize(wsnormal) * 0.5 + 0.5, 1.0);
  45.     out_flux = basecolor * texture(tex_color, texcoord);
  46. }
  47.  
  48.  
  49. // ======= rsm_render.vs ========================
  50.  
  51. #version 330 core
  52.  
  53. uniform vec3 viewposition;
  54.  
  55. uniform mat4 lightspace;
  56. uniform mat4 model;
  57. uniform mat4 MVP;
  58.  
  59. layout(location=0) in vec3 invertex;
  60. layout(location=1) in vec2 intexcoord;
  61. layout(location=2) in vec3 innormal;
  62.  
  63. out vec2 texcoord;
  64. out vec3 viewvec;
  65. out vec3 normalvec;
  66. out vec3 wsposition;
  67. out vec4 lsposition;
  68.  
  69. void main(){
  70.     vec4 invertex4 = vec4(invertex, 1.0);
  71.     wsposition = (model * invertex4).xyz;
  72.     lsposition = lightspace * vec4(wsposition, 1.0);
  73.     viewvec = wsposition.xyz-viewposition;
  74.     normalvec = normalize(transpose(inverse(mat3(model))) * innormal);
  75.     texcoord = intexcoord;
  76.     gl_Position = MVP * invertex4;
  77. }
  78.  
  79.  
  80. // ======= rsm_render.fs ========================
  81.  
  82. #version 330 core
  83.  
  84. uniform sampler2D tex_color;
  85.  
  86. uniform sampler2D tex_depth;
  87. uniform sampler2D tex_wspos;
  88. uniform sampler2D tex_normal;
  89. uniform sampler2D tex_flux;
  90. uniform sampler1D tex_rand;
  91.  
  92. uniform int samples; //64
  93. uniform float sample_radius; //0.5
  94.  
  95. uniform vec4 basecolor;
  96.  
  97. uniform vec3 lightdirection;
  98.  
  99. in vec2 texcoord;
  100. in vec3 viewvec;
  101. in vec3 normalvec;
  102. in vec3 wsposition;
  103. in vec4 lsposition;
  104.  
  105. out vec4 glfragcolor;
  106.  
  107. #define M_PI 3.1415926
  108.  
  109.  
  110. float ShadowCalculation(vec4 fragPosLightSpace){
  111.     vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
  112.     projCoords = projCoords * 0.5 + 0.5;
  113.     float closestDepth = texture(tex_depth, projCoords.xy).r;
  114.     float currentDepth = projCoords.z;
  115.     vec3 normal = normalize(normalvec);
  116.     float bias = max(0.05 * (1.0 - dot(normalvec, lightdirection)), 0.005);
  117.     float shadow = 0.0;
  118.     vec2 texelSize = 1.0 / textureSize(tex_depth, 0);
  119.     for(int x = -1; x <= 1; ++x){
  120.         for(int y = -1; y <= 1; ++y){
  121.             float pcfDepth = texture(tex_depth, projCoords.xy + vec2(x, y) * texelSize).r;
  122.             shadow += (currentDepth - bias > pcfDepth)  ? 1.0 : 0.0;        
  123.         }    
  124.     }
  125.     shadow /= 9.0;
  126.       if(projCoords.z > 1.0)
  127.         shadow = 0.0;
  128.     return shadow;
  129. }
  130.  
  131.  
  132. void main(){
  133.    
  134.     vec4 albedo = texture(tex_color, texcoord) * basecolor;
  135.    
  136.     vec3 indirect = vec3(0.0, 0.0, 0.0);
  137.  
  138.     vec2 ls_uv = (lsposition.xy / lsposition.w) * 0.5 + 0.5;
  139.  
  140.    
  141.     for (int i = 0; i < samples; i++) {
  142.         float s_dist = sample_radius * texture(tex_rand, 0.5 * (i * 2) / samples).x;
  143.         float s_angle = 2.0 * M_PI * texture(tex_rand, 0.5 * (i * 2 + 1) / samples).x;
  144.         vec2 offset = s_dist * vec2(cos(s_angle), sin(s_angle));
  145.         vec2 uv = ls_uv + offset;
  146.        
  147.         vec3 pos = texture(tex_wspos, uv).xyz;
  148.         vec3 nrm = normalize(texture(tex_normal, uv).xyz);
  149.         vec3 flux = texture(tex_flux, uv).rgb;
  150.        
  151.         float dot1 = max(0.0, dot(pos - wsposition, normalize(normalvec)));
  152.         float dot2 = max(0.0, -dot(wsposition - pos, nrm));
  153.         float dist = length(pos - wsposition);
  154.          
  155.         indirect += diff * (dot1 * dot2) / pow(dist, 4);
  156.        
  157.     //indirect += flux * ( (max(0.0, dot(nrm, pos - wsposition))) ) / pow( length(wsposition - pos), 4);
  158.          
  159.     }
  160.     indirect = 4.0 * M_PI * indirect / samples;
  161.    
  162.     float shadow = ShadowCalculation(lsposition);
  163.  
  164.     float diff = max(dot(lightdirection, normalvec), 0.0);
  165.  
  166.     vec3 lighting = (1.0 - shadow) * (albedo.rgb * diff + albedo.rgb * indirect);
  167.    
  168.     glfragcolor = vec4(lighting, albedo.a);
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement