edward4324

shader

Jul 18th, 2022 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 330 core
  2.  
  3. struct Material {
  4.     sampler2D diffuse;
  5.     sampler2D specular;
  6.     sampler2D emission;
  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.     float constant;
  22.     float linear;
  23.     float quadratic;  
  24.  
  25.     vec3 ambient;
  26.     vec3 diffuse;
  27.     vec3 specular;
  28. };  
  29. #define NR_POINT_LIGHTS 4  
  30.  
  31. out vec4 FragColor;
  32.  
  33. in vec3 Normal;
  34. in vec3 FragPos;
  35.  
  36. in vec2 TexCoords;
  37.  
  38. uniform vec3 objectColor;
  39. uniform vec3 viewPos;
  40.  
  41. uniform Material material;
  42. uniform DirLight dirLight;
  43. uniform PointLight pointLights[NR_POINT_LIGHTS];
  44. uniform int number_of_point_lights;
  45.  
  46. vec3 directional_light_calculation(DirLight light, vec3 normal, vec3 viewDir);
  47. vec3 point_light_calculation(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
  48.  
  49. void main()
  50. {
  51.    vec3 output = vec3(0.0f);
  52.  
  53.    vec3 norm = normalize(Normal);
  54.    vec3 viewDir = normalize(viewPos - FragPos);
  55.  
  56.    output += directional_light_calculation(dirLight, norm, viewDir);
  57.  
  58.    for (int i = 0; i < NR_POINT_LIGHTS; i++)
  59.    {
  60.        output += point_light_calculation(pointLights[i], norm, FragPos, viewDir);
  61.    }
  62.  
  63.    FragColor = vec4(texture(material.emission, TexCoords) + output, 1.0f);
  64.  
  65. }
  66.  
  67. vec3 directional_light_calculation(DirLight light, vec3 normal, vec3 viewDir){
  68.  
  69.      vec3 lightDir = normalize(-light.direction);
  70.  
  71.     float diff = max(dot(normal, lightDir), 0.0);
  72.  
  73.     vec3 reflectDir = reflect(-lightDir, normal);
  74.     float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
  75.  
  76.     vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
  77.     vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
  78.     vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
  79.     return ambient + diffuse + specular;
  80.  
  81. }
  82.  
  83. vec3 point_light_calculation(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir){
  84.  
  85.     vec3 lightDir = normalize(light.position - fragPos);
  86.  
  87.     float diff = max(dot(normal, lightDir), 0.0);
  88.  
  89.     vec3 reflectDir = reflect(-lightDir, normal);
  90.     float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
  91.  
  92.     float distance = length(light.position - fragPos);
  93.     float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));    
  94.  
  95.     vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
  96.     vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
  97.     vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
  98.     ambient *= attenuation;
  99.     diffuse *= attenuation;
  100.     specular *= attenuation;
  101.     return ambient + diffuse + specular;
  102.  
  103. }
Add Comment
Please, Sign In to add comment