Advertisement
Guest User

Untitled

a guest
May 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #version 330 core
  2.  
  3. in VS_OUT {
  4. vec2 TexCoord;
  5. vec3 WorldPos;
  6. mat3 TBN;
  7. vec4 FragPosLightSpace;
  8. } fs_in;
  9.  
  10. out vec4 fragColor;
  11.  
  12. struct Material {
  13. sampler2D diffuseMap;
  14. sampler2D specularMap;
  15. sampler2D normalMap;
  16. sampler2D shadowMap;
  17. float shininess;
  18. };
  19.  
  20. struct DirLight {
  21. //vec3 position;
  22. vec3 direction;
  23.  
  24. vec3 ambient;
  25. vec3 diffuse;
  26. vec3 specular;
  27. vec3 color;
  28. };
  29.  
  30. uniform vec3 viewPos;
  31. uniform DirLight dirLight;
  32. uniform Material material;
  33.  
  34. float ShadowCalculation(vec4 fragPosLightSpace, vec3 normal, vec3 lightDir)
  35. {
  36. vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
  37. projCoords = projCoords * 0.5 + 0.5;
  38.  
  39. if(projCoords.z > 1.0)
  40. return 0.0;
  41.  
  42. float closestDepth = texture(material.shadowMap, projCoords.xy).r;
  43. float currentDepth = projCoords.z;
  44. float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005);
  45. //float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
  46.  
  47.  
  48. float shadow = 0.0;
  49. vec2 texelSize = 1.0 / textureSize(material.shadowMap, 0);
  50. for(int x = -1; x <= 1; ++x)
  51. {
  52. for(int y = -1; y <= 1; ++y)
  53. {
  54. float pcfDepth = texture(material.shadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
  55. shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0;
  56. }
  57. }
  58. shadow /= 9.0;
  59.  
  60. return shadow;
  61. }
  62.  
  63. vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
  64. {
  65. vec3 color = texture(material.diffuseMap, fs_in.TexCoord).rgb;
  66. vec3 lightDir = normalize(-light.direction);
  67. vec3 halfwayDir = normalize(lightDir + viewDir); //Blinn
  68. float diff = max(dot(normal, lightDir), 0.0);
  69. vec3 reflectDir = reflect(-lightDir, normal);
  70. float spec = pow(max(dot(normal, halfwayDir), 0.0), material.shininess); //Blinn
  71. vec3 ambient = light.ambient * color;
  72. vec3 diffuse = light.diffuse * diff * color;
  73. vec3 specular = light.specular * spec * vec3(texture(material.specularMap, fs_in.TexCoord));
  74. float shadow = ShadowCalculation(fs_in.FragPosLightSpace, normal, lightDir);
  75. return (ambient + (1.0 - shadow) * (diffuse + specular));
  76. //return (ambient + diffuse + specular);
  77. }
  78. void main()
  79. {
  80. vec3 normal = normalize(vec3(texture(material.normalMap, fs_in.TexCoord).rgb));
  81. normal = normal * 2.0 - 1.0;
  82. normal = normalize(normal);
  83.  
  84. //check if drawing a mirrored fragment
  85. vec3 tangent = fs_in.TBN[0];
  86. vec3 bitangent = fs_in.TBN[1];
  87. vec3 calcN = cross(tangent, bitangent);
  88. float normalsAligned = dot(calcN, fs_in.TBN[2]);
  89. if (normalsAligned < 0) {
  90. tangent =-tangent;
  91. mat3 TBN_CORRECT = fs_in.TBN;
  92. TBN_CORRECT[0] = tangent;
  93. normal = normalize(TBN_CORRECT * normal);
  94. }
  95. else {
  96. normal = normalize(fs_in.TBN * normal);
  97. }
  98. vec3 viewDir = normalize(viewPos - fs_in.WorldPos);
  99.  
  100. vec3 result = CalcDirLight(dirLight, normal, viewDir) * dirLight.color;
  101. fragColor = vec4(result, 1.0);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement