Advertisement
Lumi_V

shader.frag

Dec 17th, 2023
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 460 core
  2.  
  3. in vec4 col;
  4. in vec2 texCoord;
  5. in vec3 Normal;
  6. in vec3 fragPos;
  7.  
  8. out vec4 color;
  9.  
  10. // Should be same as Utilities.h header file
  11. const int MAX_POINT_LIGHTS = 3;
  12. const int MAX_SPOT_LIGHTS = 3;
  13.  
  14. struct Light {
  15.     vec3 colour;
  16.     float ambientIntensity;
  17.     float diffuseIntensity;
  18. };
  19.  
  20. struct DirectionalLight {
  21.     Light base;
  22.     vec3 direction;
  23. };
  24.  
  25. struct PointLight {
  26.     Light base;
  27.     vec3 position;
  28.     float constant;
  29.     float linear;
  30.     float exponent;
  31. };
  32.  
  33. struct SpotLight {
  34.     PointLight base;
  35.     vec3 direction;
  36.     float edge;
  37. };
  38.  
  39. struct Material {
  40.     float specularIntensity;
  41.     float shininess;
  42. };
  43.  
  44. uniform int pointLightCount;
  45. uniform int spotLightCount;
  46.  
  47. // Lights
  48. uniform DirectionalLight directionalLight;
  49. uniform PointLight pointLight[MAX_POINT_LIGHTS];
  50. uniform SpotLight spotLight[MAX_SPOT_LIGHTS];
  51.  
  52. // Textures
  53. uniform sampler2D theTexture;
  54.  
  55. // Materials
  56. uniform Material material;
  57.  
  58. // Eye Position
  59. uniform vec3 eyePosition;
  60.  
  61. vec4 calcLightByDirection(Light light, vec3 direction) {
  62.     // Ambient Light
  63.     vec4 ambientColour = vec4(light.colour, 1.0f) * light.ambientIntensity;
  64.  
  65.     // Diffuse Light
  66.     // Getting the cosine of angle between two vectors
  67.     float diffuseFactor = max(dot(normalize(Normal), normalize(direction)), 0.0f);
  68.     vec4 diffuseColour = vec4(light.colour * light.diffuseIntensity * diffuseFactor, 1.0f);
  69.  
  70.     // Specular Light
  71.     vec4 specularColour = vec4(0, 0, 0, 0);
  72.  
  73.     // If no diffuse, then no specular
  74.     if(diffuseFactor > 0.0f) {
  75.         vec3 fragToEye = normalize(eyePosition - fragPos);
  76.         vec3 reflectedVertex = normalize(reflect(direction, normalize(Normal)));
  77.  
  78.         float specularFactor = dot(fragToEye, reflectedVertex);
  79.  
  80.         if(specularFactor > 0.0f) {
  81.             specularFactor = pow(specularFactor, material.shininess);
  82.             specularColour = vec4(light.colour * material.specularIntensity * specularFactor, 1.0f);
  83.         }
  84.     }
  85.  
  86.     return (ambientColour + diffuseColour + specularColour);
  87. }
  88.  
  89. vec4 calcDirectionalLight() {
  90.     return calcLightByDirection(directionalLight.base, directionalLight.direction);
  91. }
  92.  
  93. vec4 calcPointLightsBase(PointLight pLight) {
  94.     // Calculating Direction of our Point Lights
  95.     // Getting direction from light to fragment
  96.     vec3 direction = fragPos - pLight.position;
  97.     float distance = length(direction);
  98.  
  99.     direction = normalize(direction);
  100.  
  101.     vec4 colour = calcLightByDirection(pLight.base, direction);
  102.  
  103.     float attenuation = pLight.exponent * distance * distance +
  104.                         pLight.linear * distance +
  105.                         pLight.constant;
  106.  
  107.     return (colour / attenuation);
  108. }
  109.  
  110. vec4 calcSpotLightsBase(SpotLight sLight) {
  111.     // Getting Direction
  112.     vec3 rayDirection = normalize(fragPos - sLight.base.position);
  113.     float slFactor = dot(rayDirection, sLight.direction);
  114.  
  115.     // If we are withing range
  116.     if(slFactor > sLight.edge) {
  117.         vec4 colour = calcPointLightsBase(sLight.base);
  118.  
  119.         return colour * (1.0f - (1.0 - slFactor) * (1.0f/(1.0f - sLight.edge)));
  120.     }
  121.  
  122.     else {
  123.     return vec4(0, 0, 0, 0);
  124.     }
  125. }
  126.  
  127. vec4 calcPointLights() {
  128.     vec4 totalColour = vec4(0, 0, 0, 0);
  129.  
  130.     for(int i = 0; i < pointLightCount; i++) {
  131.         totalColour += calcPointLightsBase(pointLight[i]);
  132.     }
  133.  
  134.     return totalColour;
  135. }
  136.  
  137. vec4 calcSpotLights() {
  138.     vec4 totalColour = vec4(0, 0, 0, 0);
  139.  
  140.     for(int i = 0; i < spotLightCount; i++) {
  141.         totalColour += calcSpotLightsBase(spotLight[i]);
  142.     }
  143.  
  144.     return totalColour;
  145. }
  146.  
  147. void main() {
  148.     vec4 finalColour = calcDirectionalLight();
  149.     finalColour += calcPointLights();
  150.     finalColour += calcSpotLights();
  151.  
  152.     color = texture(theTexture, texCoord) * finalColour;
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement