Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.51 KB | None | 0 0
  1. #version 330 core
  2. out vec4 FragColor;
  3.  
  4. struct Material {
  5.     sampler2D texture_diffuse;
  6.     sampler2D texture_specular;
  7.     float shininess;
  8. };
  9.  
  10. struct DirLight {
  11.     vec3 direction;
  12.    
  13.     vec3 ambient;
  14.     vec3 diffuse;
  15.     vec3 specular;
  16. };
  17.  
  18. struct PointLight {
  19.     vec3 position;
  20.    
  21.     vec3 ambient;
  22.     vec3 diffuse;
  23.     vec3 specular;
  24.    
  25.     float constant;
  26.     float linear;
  27.     float quadratic;
  28. };
  29.  
  30. struct Spotlight {
  31.     vec3 position;
  32.     vec3 direction;
  33.     float cutOff;
  34.     float outerCutOff;
  35.    
  36.     vec3 ambient;
  37.     vec3 diffuse;
  38.     vec3 specular;
  39.    
  40.     float constant;
  41.     float linear;
  42.     float quadratic;
  43. };
  44.  
  45. in vec3 FragPos;
  46. in vec3 Normal;
  47. in vec2 TexCoords;
  48.  
  49. uniform vec3 viewPos;
  50. uniform Material material;
  51. uniform DirLight dirLight;
  52. #define NR_POINT_LIGHTS 4
  53. uniform PointLight pointLights[NR_POINT_LIGHTS];
  54. uniform Spotlight spotlight;
  55.  
  56. vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);
  57. vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
  58. vec3 CalcSpotlight(Spotlight light, vec3 normal, vec3 fragPos, vec3 viewDir);
  59.  
  60. void main() {
  61.     vec3 result = vec3(0.0f, 0.0f, 0.0f);
  62.    
  63.     vec3 normal = normalize(Normal);
  64.     vec3 viewDir = normalize(viewPos - FragPos);
  65.    
  66.     result += CalcDirLight(dirLight, normal, viewDir);
  67.     for (int i = 0; i < NR_POINT_LIGHTS; ++i) {
  68.         result += CalcPointLight(pointLights[i], normal, FragPos, viewDir);
  69.     }
  70.     result += CalcSpotlight(spotlight, normal, FragPos, viewDir);
  71.    
  72.     //FragColor = vec4(result, 1.0);
  73.     FragColor = vec4(CalcPointLight(pointLights[0], normal, FragPos, viewDir), 1.0);
  74. }
  75.  
  76. vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir) {
  77.     // ambient
  78.     vec3 ambient = light.ambient * texture(material.texture_diffuse, TexCoords).rgb;
  79.    
  80.     // diffuse
  81.     vec3 lightDir = -normalize(light.direction);
  82.     float diff = max(dot(lightDir, normal), 0.0);
  83.     vec3 diffuse = light.diffuse * diff * texture(material.texture_diffuse, TexCoords).rgb;
  84.    
  85.     // specular
  86.     vec3 lightReflect = reflect(-lightDir, normal);
  87.     float spec = pow(max(dot(lightReflect, viewDir), 0.0), material.shininess);
  88.     vec3 specular = light.specular * spec * texture(material.texture_specular, TexCoords).rgb;
  89.    
  90.     return ambient + diffuse + specular;
  91. }
  92.  
  93. vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir) {
  94.     // attenuation
  95.     float distance = length(light.position - FragPos);
  96.     float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
  97.    
  98.     // ambient
  99.     vec3 ambient = light.ambient * texture(material.texture_diffuse, TexCoords).rgb;
  100.    
  101.     // diffuse
  102.     vec3 lightDir = normalize(light.position - fragPos);
  103.     float diff = max(dot(lightDir, normal), 0.0);
  104.     vec3 diffuse = light.diffuse * diff * texture(material.texture_diffuse, TexCoords).rgb;
  105.    
  106.     // specular
  107.     vec3 lightReflect = reflect(-lightDir, normal);
  108.     float spec = pow(max(dot(viewDir, lightReflect), 0.0), material.shininess);
  109.     vec3 specular = light.specular * spec * texture(material.texture_specular, TexCoords).rgb;
  110.    
  111.     return (ambient + diffuse + specular) * attenuation;
  112. }
  113.  
  114. vec3 CalcSpotlight(Spotlight light, vec3 normal, vec3 fragPos, vec3 viewDir) {
  115.     // attenuation
  116.     float distance = length(light.position - FragPos);
  117.     float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
  118.    
  119.     // pure ambient
  120.     vec3 ambient = light.ambient * vec3(texture(material.texture_diffuse, TexCoords));
  121.    
  122.     // diffuse
  123.     vec3 lightDir = normalize(light.position - FragPos);
  124.     float diff = max(dot(normal, lightDir), 0.0);
  125.     vec3 diffuse = light.diffuse * diff * texture(material.texture_diffuse, TexCoords).rgb;
  126.    
  127.     // pure specular
  128.     vec3 reflectDir = -reflect(lightDir, normal);
  129.     float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
  130.     vec3 specular = light.specular * spec * texture(material.texture_specular, TexCoords).rgb;
  131.    
  132.     // intensity
  133.     float theta = dot(lightDir, normalize(-light.direction));
  134.     float epsilon = (light.cutOff - light.outerCutOff);
  135.     float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
  136.    
  137.     // final color
  138.     ambient = ambient * attenuation;
  139.     diffuse = diffuse * attenuation * intensity;
  140.     specular = specular * attenuation * intensity;
  141.    
  142.     return ambient + diffuse + specular;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement