Advertisement
Guest User

Untitled

a guest
May 21st, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. #version 330 core
  2. #extension GL_ARB_gpu_shader_fp64 : enable
  3.  
  4. in VS_OUT {
  5. vec2 TexCoord;
  6. vec3 FragPos;
  7. mat3 TBN;
  8. vec4 FragPosLightSpace;
  9. vec4 FragSpotLightSpaceMatrix;
  10. } fs_in;
  11.  
  12. out vec4 fragColor;
  13.  
  14. struct Material {
  15. sampler2D diffuseMap;
  16. sampler2D specularMap;
  17. sampler2D normalMap;
  18. sampler2D shadowMap;
  19. sampler2D spotLightShadowMap;
  20. float shininess;
  21. };
  22.  
  23. struct DirLight {
  24. vec3 direction;
  25.  
  26. vec3 ambient;
  27. vec3 diffuse;
  28. vec3 specular;
  29. vec3 color;
  30. };
  31.  
  32. struct SpotLight {
  33. vec3 color;
  34. vec3 position;
  35. vec3 direction;
  36. float cutOff;
  37. float outerCutOff;
  38.  
  39. float constant;
  40. float linear;
  41. float quadratic;
  42.  
  43. vec3 ambient;
  44. vec3 diffuse;
  45. vec3 specular;
  46. };
  47.  
  48. uniform vec3 viewPos;
  49. uniform DirLight dirLight;
  50. uniform SpotLight spotLight;
  51. uniform Material material;
  52.  
  53. double ShadowCalculation(vec4 fragPosLightSpace, vec3 normal, vec3 lightDir, sampler2D shadowMap)
  54. {
  55. vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
  56. projCoords = projCoords * 0.5 + 0.5;
  57.  
  58. if(projCoords.z > 1.0)
  59. return 0.0;
  60.  
  61. double closestDepth = texture(shadowMap, projCoords.xy).r;
  62. double currentDepth = projCoords.z;
  63. double bias = max(0.005 * (1.0 - dot(normal, lightDir)), 0.001);
  64.  
  65. double shadow = 0.0;
  66. vec2 texelSize = 1.0 / textureSize(shadowMap, 0);
  67. for(int x = -1; x <= 1; ++x)
  68. {
  69. for(int y = -1; y <= 1; ++y)
  70. {
  71. double pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
  72. shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0;
  73. }
  74. }
  75. shadow /= 9.0;
  76.  
  77. return shadow;
  78. }
  79.  
  80. dvec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
  81. {
  82. vec3 color = texture(material.diffuseMap, fs_in.TexCoord).rgb;
  83. vec3 lightDir = normalize(-light.direction);
  84. vec3 halfwayDir = normalize(lightDir + viewDir); //Blinn
  85. float diff = max(dot(normal, lightDir), 0.0);
  86. vec3 reflectDir = reflect(-lightDir, normal);
  87. float spec = pow(max(dot(normal, halfwayDir), 0.0), material.shininess); //Blinn
  88. dvec3 ambient = light.ambient * color;
  89. dvec3 diffuse = light.diffuse * diff * color;
  90. dvec3 specular = light.specular * spec * vec3(texture(material.specularMap, fs_in.TexCoord));
  91. double shadow = ShadowCalculation(fs_in.FragPosLightSpace, normal, lightDir, material.shadowMap);
  92. return (ambient + (1.0 - shadow) * (diffuse + specular));
  93. }
  94.  
  95.  
  96. dvec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
  97. {
  98. //vec3 tangentLightPos = fs_in.TBN * light.position;
  99. vec3 lightDir = normalize(light.position - fragPos);
  100. vec3 halfwayDir = normalize(lightDir + viewDir); //Blinn
  101. float diff = max(dot(normal, lightDir), 0.0);
  102. vec3 reflectDir = reflect(-lightDir, normal);
  103. float spec = pow(max(dot(normal, halfwayDir), 0.0), material.shininess); //Blinn
  104.  
  105. float distance = length(light.position - fragPos);
  106. float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
  107.  
  108. float theta = dot(lightDir, normalize(-light.direction));
  109. float epsilon = light.cutOff - light.outerCutOff;
  110. float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
  111.  
  112. dvec3 ambient = light.ambient * vec3(texture(material.diffuseMap, fs_in.TexCoord));
  113. dvec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuseMap, fs_in.TexCoord));
  114. dvec3 specular = light.specular * spec * vec3(texture(material.specularMap, fs_in.TexCoord));
  115. ambient *= attenuation * intensity;
  116. diffuse *= attenuation * intensity;
  117. specular *= attenuation * intensity;
  118. double shadow = ShadowCalculation(fs_in.FragSpotLightSpaceMatrix, normal, lightDir, material.spotLightShadowMap);
  119. return (ambient + (1.0 - shadow) * (diffuse + specular));
  120. }
  121.  
  122.  
  123. void main()
  124. {
  125. vec3 normal = vec3(texture(material.normalMap, fs_in.TexCoord).rgb);
  126. normal = normal * 2.0 - 1.0;
  127. normal = normalize(normal);
  128.  
  129. //check if drawing a mirrored fragment
  130. vec3 tangent = fs_in.TBN[0];
  131. vec3 bitangent = fs_in.TBN[1];
  132. vec3 calcN = cross(tangent, bitangent);
  133. float normalsAligned = dot(calcN, fs_in.TBN[2]);
  134. if (normalsAligned < 0) {
  135. tangent =-tangent;
  136. mat3 TBN_CORRECT = fs_in.TBN;
  137. TBN_CORRECT[0] = tangent;
  138. normal = normalize(TBN_CORRECT * normal);
  139. }
  140. else {
  141. normal = normalize(fs_in.TBN * normal);
  142. }
  143. vec3 viewDir = normalize(viewPos - fs_in.FragPos);
  144.  
  145. dvec3 result = CalcDirLight(dirLight, normal, viewDir) * dirLight.color;
  146. result += CalcSpotLight(spotLight, normal, fs_in.FragPos, viewDir) * spotLight.color;
  147. fragColor = vec4(result, 1.0);
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement