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.

76 lines
1.9 KiB

  1. #version 330
  2. // Input vertex attributes (from vertex shader)
  3. in vec3 fragPosition;
  4. //in vec2 fragTexCoord;
  5. in vec4 fragColor;
  6. in vec3 fragNormal;
  7. // Input uniform values
  8. //uniform sampler2D texture0;
  9. uniform vec4 colDiffuse;
  10. // Output fragment color
  11. out vec4 finalColor;
  12. // NOTE: Add here your custom variables
  13. #define MAX_LIGHTS 4
  14. #define LIGHT_DIRECTIONAL 0
  15. #define LIGHT_POINT 1
  16. struct Light {
  17. int enabled;
  18. int type;
  19. vec3 position;
  20. vec3 target;
  21. vec4 color;
  22. };
  23. // Input lighting values
  24. uniform Light lights[MAX_LIGHTS];
  25. uniform vec4 ambient;
  26. uniform vec3 viewPos;
  27. void main()
  28. {
  29. // Texel color fetching from texture sampler
  30. //vec4 texelColor = texture(texture0, fragTexCoord);
  31. vec3 lightDot = vec3(0.0);
  32. vec3 normal = normalize(fragNormal);
  33. vec3 viewD = normalize(viewPos - fragPosition);
  34. vec3 specular = vec3(0.0);
  35. // NOTE: Implement here your fragment shader code
  36. for (int i = 0; i < MAX_LIGHTS; i++)
  37. {
  38. if (lights[i].enabled == 1)
  39. {
  40. vec3 light = vec3(0.0);
  41. if (lights[i].type == LIGHT_DIRECTIONAL)
  42. {
  43. light = -normalize(lights[i].target - lights[i].position);
  44. }
  45. if (lights[i].type == LIGHT_POINT)
  46. {
  47. light = normalize(lights[i].position - fragPosition);
  48. }
  49. float NdotL = max(dot(normal, light), 0.0);
  50. lightDot += lights[i].color.rgb*NdotL;
  51. float specCo = 0.0;
  52. if (NdotL > 0.0) specCo = pow(max(0.0, dot(viewD, reflect(-(light), normal))), 16.0); // 16 refers to shine
  53. specular += specCo;
  54. }
  55. }
  56. finalColor = (fragColor*((colDiffuse + vec4(specular, 1.0))*vec4(lightDot, 1.0)));
  57. finalColor += fragColor*(ambient/10.0)*colDiffuse;
  58. // Gamma correction
  59. finalColor = pow(finalColor, vec4(1.0/2.2));
  60. }