A crude and small graphics-engine I've made during computer graphics courses in late 2018.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fragShader.frag 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #version 330 core
  2. #define NR_POINT_LIGHTS 1
  3. #define NR_SPOT_LIGHTS 2
  4. in vec3 FragPos;
  5. in vec3 Normal;
  6. in vec2 TexCoords;
  7. out vec4 FragColor;
  8. struct Material {
  9. vec3 ambient;
  10. sampler2D diffuse;
  11. sampler2D specular;
  12. sampler2D emission;
  13. float shininess;
  14. };
  15. struct DirLight {
  16. vec3 direction;
  17. vec3 ambient;
  18. vec3 diffuse;
  19. vec3 specular;
  20. };
  21. struct PointLight {
  22. vec3 position;
  23. float constant;
  24. float linear;
  25. float quadratic;
  26. vec3 ambient;
  27. vec3 diffuse;
  28. vec3 specular;
  29. };
  30. struct SpotLight {
  31. vec3 position;
  32. vec3 direction;
  33. float cutOff;
  34. float outerCutOff;
  35. float constant;
  36. float linear;
  37. float quadratic;
  38. vec3 ambient;
  39. vec3 diffuse;
  40. vec3 specular;
  41. };
  42. uniform float time;
  43. uniform vec3 viewPos;
  44. uniform Material material;
  45. uniform DirLight dirLight;
  46. uniform PointLight pointLights[NR_POINT_LIGHTS];
  47. uniform SpotLight spotLights[NR_SPOT_LIGHTS];
  48. vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
  49. {
  50. vec3 lightDir = normalize(-light.direction);
  51. // diffuse shading
  52. float diff = max(dot(normal, lightDir), 0.0);
  53. // specular shading
  54. vec3 reflectDir = reflect(-lightDir, normal);
  55. float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
  56. // combine results
  57. vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
  58. vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
  59. vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
  60. return (ambient + diffuse + specular);
  61. }
  62. vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
  63. {
  64. vec3 lightDir = normalize(light.position - fragPos);
  65. // diffuse shading
  66. float diff = max(dot(normal, lightDir), 0.0);
  67. // specular shading
  68. vec3 reflectDir = reflect(-lightDir, normal);
  69. float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
  70. // attenuation
  71. float distance = length(light.position - fragPos);
  72. float attenuation = 1.0 / (light.constant + light.linear * distance +
  73. light.quadratic * (distance * distance));
  74. // combine results
  75. vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
  76. vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
  77. vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
  78. ambient *= attenuation;
  79. diffuse *= attenuation;
  80. specular *= attenuation;
  81. return (ambient + diffuse + specular);
  82. }
  83. vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
  84. {
  85. vec3 lightDir = normalize(light.position - fragPos);
  86. // ambient
  87. vec3 ambient = light.ambient * texture(material.diffuse, TexCoords).rgb;
  88. // diffuse
  89. vec3 norm = normalize(normal);
  90. float diff = max(dot(norm, lightDir), 0.0);
  91. vec3 diffuse = light.diffuse * diff * texture(material.diffuse, TexCoords).rgb;
  92. // specular
  93. vec3 reflectDir = reflect(-lightDir, norm);
  94. float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
  95. vec3 specular = light.specular * spec * texture(material.specular, TexCoords).rgb;
  96. // spotlight (soft edges)
  97. float theta = dot(lightDir, normalize(-light.direction));
  98. float epsilon = light.cutOff - light.outerCutOff;
  99. float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
  100. ambient *= intensity;
  101. diffuse *= intensity;
  102. specular *= intensity;
  103. // attenuation
  104. float distance = length(light.position - fragPos);
  105. float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
  106. ambient *= attenuation;
  107. diffuse *= attenuation;
  108. specular *= attenuation;
  109. return (ambient + diffuse + specular);
  110. }
  111. void main()
  112. {
  113. // properties
  114. vec3 norm = normalize(Normal);
  115. vec3 viewDir = normalize(viewPos - FragPos);
  116. // phase 1: Directional lighting
  117. vec3 result = CalcDirLight(dirLight, norm, viewDir);
  118. // phase 2: Point lights
  119. for(int i = 0; i < NR_POINT_LIGHTS; i++)
  120. result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);
  121. // phase 3: Spot light
  122. for(int i = 0; i < NR_SPOT_LIGHTS; i++)
  123. result += CalcSpotLight(spotLights[i], norm, FragPos, viewDir);
  124. FragColor = vec4(result, 1.0);
  125. }