phong_model.glsl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #if AMB_LIGHTS>0
  28. noLights = false;
  29. // Ambient lights
  30. for (int i = 0; i < AMB_LIGHTS; ++i) {
  31. ambientTotal += AmbientLightColor[i] * matAmbient;
  32. }
  33. #endif
  34. #if DIR_LIGHTS>0
  35. noLights = false;
  36. // Directional lights
  37. for (int i = 0; i < DIR_LIGHTS; ++i) {
  38. vec3 lightDirection = normalize(DirLightPosition(i)); // Vector from fragment to light source
  39. float dotNormal = max(dot(lightDirection, normal), 0.0); // Dot product between light direction and fragment normal
  40. if (dotNormal > 0.0) { // If the fragment is lit
  41. diffuseTotal += DirLightColor(i) * matDiffuse * dotNormal;
  42. specularTotal += DirLightColor(i) * MatSpecularColor * pow(max(dot(reflect(-lightDirection, normal), camDir), 0.0), MatShininess);
  43. }
  44. }
  45. #endif
  46. #if POINT_LIGHTS>0
  47. noLights = false;
  48. // Point lights
  49. for (int i = 0; i < POINT_LIGHTS; ++i) {
  50. vec3 lightDirection = PointLightPosition(i) - vec3(position); // Vector from fragment to light source
  51. float lightDistance = length(lightDirection); // Distance from fragment to light source
  52. lightDirection = lightDirection / lightDistance; // Normalize lightDirection
  53. float dotNormal = max(dot(lightDirection, normal), 0.0); // Dot product between light direction and fragment normal
  54. if (dotNormal > 0.0) { // If the fragment is lit
  55. float attenuation = 1.0 / (1.0 + PointLightLinearDecay(i) * lightDistance + PointLightQuadraticDecay(i) * lightDistance * lightDistance);
  56. vec3 attenuatedColor = PointLightColor(i) * attenuation;
  57. diffuseTotal += attenuatedColor * matDiffuse * dotNormal;
  58. specularTotal += attenuatedColor * MatSpecularColor * pow(max(dot(reflect(-lightDirection, normal), camDir), 0.0), MatShininess);
  59. }
  60. }
  61. #endif
  62. #if SPOT_LIGHTS>0
  63. noLights = false;
  64. for (int i = 0; i < SPOT_LIGHTS; ++i) {
  65. // Calculates the direction and distance from the current vertex to this spot light.
  66. vec3 lightDirection = SpotLightPosition(i) - vec3(position); // Vector from fragment to light source
  67. float lightDistance = length(lightDirection); // Distance from fragment to light source
  68. lightDirection = lightDirection / lightDistance; // Normalize lightDirection
  69. float angleDot = dot(-lightDirection, SpotLightDirection(i));
  70. float angle = acos(angleDot);
  71. float cutoff = radians(clamp(SpotLightCutoffAngle(i), 0.0, 90.0));
  72. if (angle < cutoff) { // Check if fragment is inside spotlight beam
  73. float dotNormal = max(dot(lightDirection, normal), 0.0); // Dot product between light direction and fragment normal
  74. if (dotNormal > 0.0) { // If the fragment is lit
  75. float attenuation = 1.0 / (1.0 + SpotLightLinearDecay(i) * lightDistance + SpotLightQuadraticDecay(i) * lightDistance * lightDistance);
  76. float spotFactor = pow(angleDot, SpotLightAngularDecay(i));
  77. vec3 attenuatedColor = SpotLightColor(i) * attenuation * spotFactor;
  78. diffuseTotal += attenuatedColor * matDiffuse * dotNormal;
  79. specularTotal += attenuatedColor * MatSpecularColor * pow(max(dot(reflect(-lightDirection, normal), camDir), 0.0), MatShininess);
  80. }
  81. }
  82. }
  83. #endif
  84. if (noLights) {
  85. diffuseTotal = matDiffuse;
  86. }
  87. // Sets output colors
  88. ambdiff = ambientTotal + MatEmissiveColor + diffuseTotal;
  89. spec = specularTotal;
  90. }