PhongSpotLight.glsl 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //! Function computes contribution of spotlight source
  2. //! into global variables Diffuse and Specular (Phong shading).
  3. //! @param theId light source index
  4. //! @param theNormal surface normal
  5. //! @param theView view direction
  6. //! @param thePoint 3D position (world space)
  7. //! @param theIsFront front/back face flag
  8. void occSpotLight (in int theId,
  9. in vec3 theNormal,
  10. in vec3 theView,
  11. in vec3 thePoint,
  12. in bool theIsFront)
  13. {
  14. vec3 aLight = occLight_Position (theId) - thePoint;
  15. float aDist = length (aLight);
  16. float aRange = occLight_Range (theId);
  17. float anAtten = occPointLightAttenuation (aDist, aRange, occLight_LinearAttenuation (theId), occLight_ConstAttenuation (theId));
  18. if (anAtten <= 0.0) return;
  19. aLight /= aDist;
  20. vec3 aSpotDir = occLight_SpotDirection (theId);
  21. // light cone
  22. float aCosA = dot (aSpotDir, -aLight);
  23. if (aCosA >= 1.0 || aCosA < cos (occLight_SpotCutOff (theId)))
  24. {
  25. return;
  26. }
  27. float anExponent = occLight_SpotExponent (theId);
  28. if (anExponent > 0.0)
  29. {
  30. anAtten *= pow (aCosA, anExponent * 128.0);
  31. }
  32. vec3 aHalf = normalize (aLight + theView);
  33. vec3 aFaceSideNormal = theIsFront ? theNormal : -theNormal;
  34. float aNdotL = max (0.0, dot (aFaceSideNormal, aLight));
  35. float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf ));
  36. float aSpecl = 0.0;
  37. if (aNdotL > 0.0)
  38. {
  39. aSpecl = pow (aNdotH, occMaterial_Shininess (theIsFront));
  40. }
  41. Diffuse += occLight_Diffuse (theId) * aNdotL * anAtten;
  42. Specular += occLight_Specular(theId) * aSpecl * anAtten;
  43. }