/Resources/shaders/pointLightingPixel.fsh

http://github.com/tartiflop/ojgl · Fragment Shader File · 121 lines · 84 code · 30 blank · 7 comment · 8 complexity · 425989609b3660c7426c445291e7d076 MD5 · raw file

  1. #define MAX_LIGHTS 8
  2. struct Light {
  3. vec4 position;
  4. vec4 ambientColor;
  5. vec4 diffuseColor;
  6. vec4 specularColor;
  7. float attenuation;
  8. };
  9. struct Material {
  10. vec4 ambientColor;
  11. vec4 diffuseColor;
  12. vec4 specularColor;
  13. float shininess;
  14. };
  15. uniform sampler2D s_texture;
  16. uniform bool u_useTexture;
  17. uniform Light u_light[MAX_LIGHTS];
  18. uniform int u_lightEnabled[MAX_LIGHTS];
  19. uniform Material u_material;
  20. uniform bool u_lightingEnabled;
  21. uniform bool u_includeSpecular;
  22. varying vec4 v_ambient;
  23. varying vec4 v_ecPosition3;
  24. varying vec3 v_normal;
  25. varying vec3 v_eye;
  26. varying vec2 v_texCoord;
  27. const float c_zero = 0.0;
  28. const float c_one = 1.0;
  29. vec4 color;
  30. vec4 specular;
  31. void pointLight(in int lightIndex,
  32. inout vec4 ambient,
  33. inout vec4 diffuse,
  34. inout vec4 specular) {
  35. float nDotVP;
  36. float eDotRV;
  37. float pf;
  38. float attenuation;
  39. float d;
  40. vec3 VP;
  41. vec3 reflectVector;
  42. // Vector between light position and vertex
  43. VP = vec3(u_light[lightIndex].position - v_ecPosition3);
  44. // Distance between the two
  45. d = length(VP);
  46. // Normalise
  47. VP = normalize(VP);
  48. // Calculate attenuation (only quadratic)
  49. attenuation = c_one / (c_one + u_light[lightIndex].attenuation * d * d);
  50. // angle between normal and light-vertex vector
  51. nDotVP = max(c_zero, dot(VP, v_normal));
  52. if (nDotVP > c_zero) {
  53. diffuse += u_light[lightIndex].diffuseColor * nDotVP * attenuation;
  54. if (u_includeSpecular) {
  55. // reflected vector
  56. reflectVector = normalize(reflect(-VP, v_normal));
  57. // angle between eye and reflected vector
  58. eDotRV = max(c_zero, dot(v_eye, reflectVector));
  59. eDotRV = pow(eDotRV, 16.0);
  60. pf = pow(eDotRV, u_material.shininess);
  61. specular += u_light[lightIndex].specularColor * pf * attenuation;
  62. }
  63. }
  64. }
  65. void doLighting() {
  66. int i;
  67. vec4 amb = vec4(c_zero);
  68. vec4 diff = vec4(c_zero);
  69. vec4 spec = vec4(c_zero);
  70. if (u_lightingEnabled) {
  71. for (i = int(c_zero); i < MAX_LIGHTS; i++) {
  72. if (u_lightEnabled[i] == 1) {
  73. pointLight(i, amb, diff, spec);
  74. }
  75. }
  76. color = v_ambient + diff * u_material.diffuseColor;
  77. color.a = u_material.diffuseColor.a;
  78. specular = spec * u_material.specularColor;
  79. specular.a = u_material.specularColor.a;
  80. } else {
  81. color = u_material.diffuseColor;
  82. specular = spec;
  83. }
  84. }
  85. void main() {
  86. doLighting();
  87. if (u_useTexture) {
  88. color = texture2D(s_texture, v_texCoord) * color;
  89. }
  90. gl_FragColor = color + specular;
  91. }