phong_model.glsl 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /***
  2. phong lighting model
  3. Parameters:
  4. position: input vertex position in camera coordinates
  5. normal: input vertex normal in camera coordinates
  6. camDir: input camera directions
  7. matAmbient: input material ambient color
  8. matDiffuse: input material diffuse color
  9. ambdiff: output ambient+diffuse color
  10. spec: output specular color
  11. Uniforms:
  12. AmbientLightColor[]
  13. DiffuseLightColor[]
  14. DiffuseLightPosition[]
  15. PointLightColor[]
  16. PointLightPosition[]
  17. PointLightLinearDecay[]
  18. PointLightQuadraticDecay[]
  19. MatSpecularColor
  20. MatShininess
  21. *****/
  22. void phongModel(vec4 position, vec3 normal, vec3 camDir, vec3 matAmbient, vec3 matDiffuse, out vec3 ambdiff, out vec3 spec) {
  23. vec3 ambientTotal = vec3(0.0);
  24. vec3 diffuseTotal = vec3(0.0);
  25. vec3 specularTotal = vec3(0.0);
  26. bool noLights = true;
  27. const float EPS = 0.00001;
  28. #if AMB_LIGHTS>0
  29. noLights = false;
  30. // Ambient lights
  31. for (int i = 0; i < AMB_LIGHTS; ++i) {
  32. ambientTotal += AmbientLightColor[i] * matAmbient;
  33. }
  34. #endif
  35. #if DIR_LIGHTS>0
  36. noLights = false;
  37. // Directional lights
  38. for (int i = 0; i < DIR_LIGHTS; ++i) {
  39. vec3 lightDirection = normalize(DirLightPosition(i)); // Vector from fragment to light source
  40. float dotNormal = dot(lightDirection, normal); // Dot product between light direction and fragment normal
  41. if (dotNormal > EPS) { // If the fragment is lit
  42. diffuseTotal += DirLightColor(i) * matDiffuse * dotNormal;
  43. specularTotal += DirLightColor(i) * MatSpecularColor * pow(max(dot(reflect(-lightDirection, normal), camDir), 0.0), MatShininess);
  44. }
  45. }
  46. #endif
  47. #if POINT_LIGHTS>0
  48. noLights = false;
  49. // Point lights
  50. for (int i = 0; i < POINT_LIGHTS; ++i) {
  51. vec3 lightDirection = PointLightPosition(i) - vec3(position); // Vector from fragment to light source
  52. float lightDistance = length(lightDirection); // Distance from fragment to light source
  53. lightDirection = lightDirection / lightDistance; // Normalize lightDirection
  54. float dotNormal = dot(lightDirection, normal); // Dot product between light direction and fragment normal
  55. if (dotNormal > EPS) { // If the fragment is lit
  56. float attenuation = 1.0 / (1.0 + lightDistance * (PointLightLinearDecay(i) + PointLightQuadraticDecay(i) * lightDistance));
  57. vec3 attenuatedColor = PointLightColor(i) * attenuation;
  58. diffuseTotal += attenuatedColor * matDiffuse * dotNormal;
  59. specularTotal += attenuatedColor * MatSpecularColor * pow(max(dot(reflect(-lightDirection, normal), camDir), 0.0), MatShininess);
  60. }
  61. }
  62. #endif
  63. #if SPOT_LIGHTS>0
  64. noLights = false;
  65. for (int i = 0; i < SPOT_LIGHTS; ++i) {
  66. // Calculates the direction and distance from the current vertex to this spot light.
  67. vec3 lightDirection = SpotLightPosition(i) - vec3(position); // Vector from fragment to light source
  68. float lightDistance = length(lightDirection); // Distance from fragment to light source
  69. lightDirection = lightDirection / lightDistance; // Normalize lightDirection
  70. float angleDot = dot(-lightDirection, SpotLightDirection(i));
  71. float angle = acos(angleDot);
  72. float cutoff = radians(clamp(SpotLightCutoffAngle(i), 0.0, 90.0));
  73. if (angle < cutoff) { // Check if fragment is inside spotlight beam
  74. float dotNormal = dot(lightDirection, normal); // Dot product between light direction and fragment normal
  75. if (dotNormal > EPS) { // If the fragment is lit
  76. float attenuation = 1.0 / (1.0 + lightDistance * (SpotLightLinearDecay(i) + SpotLightQuadraticDecay(i) * lightDistance));
  77. float spotFactor = pow(angleDot, SpotLightAngularDecay(i));
  78. vec3 attenuatedColor = SpotLightColor(i) * attenuation * spotFactor;
  79. diffuseTotal += attenuatedColor * matDiffuse * dotNormal;
  80. specularTotal += attenuatedColor * MatSpecularColor * pow(max(dot(reflect(-lightDirection, normal), camDir), 0.0), MatShininess);
  81. }
  82. }
  83. }
  84. #endif
  85. if (noLights) {
  86. diffuseTotal = matDiffuse;
  87. }
  88. // Sets output colors
  89. ambdiff = ambientTotal + MatEmissiveColor + diffuseTotal;
  90. spec = specularTotal;
  91. }