Advertisement
PhoenixMee

Untitled

Apr 11th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. // FRAGMENT SHADER
  2. #version 330
  3.  
  4. // Matrices
  5. uniform mat4 matrixView;
  6. vec3 finalNormal;
  7.  
  8. // Materials
  9. uniform vec3 materialAmbient;
  10. uniform vec3 materialDiffuse;
  11. uniform vec3 materialSpecular;
  12. uniform float shininess;
  13. //uniform vec3 fogColor;
  14. uniform float reflectionPower;
  15. uniform sampler2DShadow shadowMap;
  16. uniform float B1;
  17. uniform float B2;
  18.  
  19.  
  20. // Textures
  21. uniform sampler2D texture0;
  22. uniform sampler2D textureNormal;
  23. uniform samplerCube textureCubeMap;
  24. uniform float NormalMap;
  25.  
  26.  
  27. // Input from Vert
  28. in vec4 color;
  29. in vec4 position;
  30. in vec3 normal;
  31. //in float fogStrength;
  32. in vec2 texCoord0;
  33. in vec3 texCoordCubeMap;
  34. in mat3 matrixTangent;
  35. in vec4 shadowCoord;
  36.  
  37.  
  38. // Output
  39. out vec4 outColor;
  40.  
  41.  
  42. // point light declaration
  43. struct POINT
  44. {
  45. int on;
  46. vec3 position;
  47. vec3 diffuse;
  48. vec3 specular;
  49. };
  50. uniform POINT lightPoint, lightPoint2;
  51.  
  52. // spot light declaration
  53. struct SPOT
  54. {
  55. int on;
  56. vec3 position;
  57. vec3 diffuse;
  58. vec3 specular;
  59. vec3 direction;
  60. float cutoff;
  61. float attenuation;
  62. float strength;
  63. mat4 matrix;
  64. };
  65. uniform SPOT lightSpot;
  66.  
  67.  
  68.  
  69.  
  70. // point light function
  71. vec4 PointLight(POINT light)
  72. {
  73.  
  74. // diffuse light
  75. vec4 color = vec4(0, 0, 0, 1);
  76. vec3 L = normalize((matrixView * vec4(light.position, 1)) - position).xyz;
  77. float NdotL = dot(L, finalNormal);
  78. if (NdotL > 0)
  79. {
  80. color += vec4 (light.diffuse * materialDiffuse, 1) * NdotL;
  81. }
  82.  
  83. // specular specular
  84. vec3 V = normalize(-position.xyz);
  85. vec3 R = reflect(-L, finalNormal);
  86. float RdotV = dot(R, V);
  87. if (NdotL > 0 && RdotV > 0)
  88. {
  89. color += vec4(light.specular * materialSpecular * pow(RdotV, shininess), 1);
  90. }
  91.  
  92. return color;
  93. }
  94.  
  95. // spot light function
  96. vec4 SpotLight(SPOT light)
  97. {
  98. vec4 color = vec4(0, 0, 0, 1);
  99.  
  100. vec3 L = normalize(light.matrix * vec4(light.position, 1) - position).xyz;
  101.  
  102. // diffuse light
  103. float NdotL = dot(L, finalNormal);
  104. if (NdotL > 0)
  105. {
  106. color += vec4 (materialDiffuse * light.diffuse, 1) * NdotL;
  107. }
  108.  
  109. // specular light
  110. vec3 V = normalize(-position.xyz);
  111. vec3 R = reflect(-L, finalNormal);
  112. float RdotV = dot(R, V);
  113. if (NdotL > 0 && RdotV > 0)
  114. {
  115. color += vec4(materialSpecular * light.specular * pow(RdotV, shininess), 1);
  116. }
  117.  
  118.  
  119. // spot light
  120. vec3 D = normalize(mat3(light.matrix) * light.direction);
  121. float S = dot(-L, D);
  122. float A = acos(S);
  123. float cutoff = radians(clamp(light.cutoff, 0, 360));
  124.  
  125. if (A < cutoff) S = pow(S, light.attenuation);
  126. else S = 0;
  127.  
  128. // attenuation
  129. float dist = length(light.matrix * vec4(light.position, 1) - position);
  130. float att = 1 / (dist * dist) / light.strength;
  131.  
  132. return color * S * att;
  133. }
  134.  
  135.  
  136.  
  137.  
  138. void main(void)
  139. {
  140. outColor = color;
  141.  
  142. if (NormalMap == 1)
  143. {
  144. finalNormal = 2.0 * texture(textureNormal, texCoord0).xyz - vec3(1.0, 1.0, 1.0);
  145. finalNormal = normalize(matrixTangent * finalNormal);
  146. }
  147. else finalNormal = normal;
  148.  
  149. if (lightPoint.on == 1) outColor += PointLight(lightPoint);
  150. if (lightPoint2.on == 1) outColor += PointLight(lightPoint2);
  151.  
  152. if (lightSpot.on == 1) outColor += SpotLight(lightSpot);
  153.  
  154. // calculating shadows (for a source)
  155. float shadow = 1.0;
  156. if (B1 == 1)
  157. {
  158. if(shadowCoord.w > 0) // if shadowCoord.w < 0 fragment is out of the Light POV
  159. shadow = 0.5 + 0.5 * textureProj(shadowMap, shadowCoord);
  160. }
  161.  
  162. if (B2 == 1)
  163. {
  164. if(shadowCoord.w > 0) // if shadowCoord.w < 0 fragment is out of the Light POV
  165. shadow = 0.5 + 0.5 * textureProj(shadowMap, shadowCoord);
  166. }
  167.  
  168. outColor *= shadow;
  169. outColor = mix(outColor * texture(texture0, texCoord0.st), texture(textureCubeMap, texCoordCubeMap), reflectionPower);
  170. outColor *= texture(texture0, texCoord0);
  171. //outColor = mix(vec4(fogColor, 1), outColor, fogStrength);
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement