Team Fortress 2 Source Code as on 22/4/2020
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.

173 lines
5.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. // light structure definitions.
  7. #ifndef LIGHTDESC_H
  8. #define LIGHTDESC_H
  9. #include <mathlib/ssemath.h>
  10. #include <mathlib/vector.h>
  11. //-----------------------------------------------------------------------------
  12. // Light structure
  13. //-----------------------------------------------------------------------------
  14. enum LightType_t
  15. {
  16. MATERIAL_LIGHT_DISABLE = 0,
  17. MATERIAL_LIGHT_POINT,
  18. MATERIAL_LIGHT_DIRECTIONAL,
  19. MATERIAL_LIGHT_SPOT,
  20. };
  21. enum LightType_OptimizationFlags_t
  22. {
  23. LIGHTTYPE_OPTIMIZATIONFLAGS_HAS_ATTENUATION0 = 1,
  24. LIGHTTYPE_OPTIMIZATIONFLAGS_HAS_ATTENUATION1 = 2,
  25. LIGHTTYPE_OPTIMIZATIONFLAGS_HAS_ATTENUATION2 = 4,
  26. LIGHTTYPE_OPTIMIZATIONFLAGS_DERIVED_VALUES_CALCED = 8,
  27. };
  28. struct LightDesc_t
  29. {
  30. LightType_t m_Type; //< MATERIAL_LIGHT_xxx
  31. Vector m_Color; //< color+intensity
  32. Vector m_Position; //< light source center position
  33. Vector m_Direction; //< for SPOT, direction it is pointing
  34. float m_Range; //< distance range for light.0=infinite
  35. float m_Falloff; //< angular falloff exponent for spot lights
  36. float m_Attenuation0; //< constant distance falloff term
  37. float m_Attenuation1; //< linear term of falloff
  38. float m_Attenuation2; //< quadatic term of falloff
  39. float m_Theta; //< inner cone angle. no angular falloff
  40. //< within this cone
  41. float m_Phi; //< outer cone angle
  42. // the values below are derived from the above settings for optimizations
  43. // These aren't used by DX8. . used for software lighting.
  44. float m_ThetaDot;
  45. float m_PhiDot;
  46. unsigned int m_Flags;
  47. protected:
  48. float OneOver_ThetaDot_Minus_PhiDot;
  49. float m_RangeSquared;
  50. public:
  51. void RecalculateDerivedValues(void); // calculate m_xxDot, m_Type for changed parms
  52. LightDesc_t(void)
  53. {
  54. }
  55. // constructors for various useful subtypes
  56. // a point light with infinite range
  57. LightDesc_t( const Vector &pos, const Vector &color )
  58. {
  59. InitPoint( pos, color );
  60. }
  61. /// a simple light. cone boundaries in radians. you pass a look_at point and the
  62. /// direciton is derived from that.
  63. LightDesc_t( const Vector &pos, const Vector &color, const Vector &point_at,
  64. float inner_cone_boundary, float outer_cone_boundary )
  65. {
  66. InitSpot( pos, color, point_at, inner_cone_boundary, outer_cone_boundary );
  67. }
  68. void InitPoint( const Vector &pos, const Vector &color );
  69. void InitDirectional( const Vector &dir, const Vector &color );
  70. void InitSpot(const Vector &pos, const Vector &color, const Vector &point_at,
  71. float inner_cone_boundary, float outer_cone_boundary );
  72. /// Given 4 points and 4 normals, ADD lighting from this light into "color".
  73. void ComputeLightAtPoints( const FourVectors &pos, const FourVectors &normal,
  74. FourVectors &color, bool DoHalfLambert=false ) const;
  75. void ComputeNonincidenceLightAtPoints( const FourVectors &pos, FourVectors &color ) const;
  76. void ComputeLightAtPointsForDirectional( const FourVectors &pos,
  77. const FourVectors &normal,
  78. FourVectors &color, bool DoHalfLambert=false ) const;
  79. // warning - modifies color!!! set color first!!
  80. void SetupOldStyleAttenuation( float fQuadatricAttn, float fLinearAttn, float fConstantAttn );
  81. void SetupNewStyleAttenuation( float fFiftyPercentDistance, float fZeroPercentDistance );
  82. /// given a direction relative to the light source position, is this ray within the
  83. /// light cone (for spotlights..non spots consider all rays to be within their cone)
  84. bool IsDirectionWithinLightCone(const Vector &rdir) const
  85. {
  86. return ((m_Type!=MATERIAL_LIGHT_SPOT) || (rdir.Dot(m_Direction)>=m_PhiDot));
  87. }
  88. float OneOverThetaDotMinusPhiDot() const
  89. {
  90. return OneOver_ThetaDot_Minus_PhiDot;
  91. }
  92. };
  93. //-----------------------------------------------------------------------------
  94. // a point light with infinite range
  95. //-----------------------------------------------------------------------------
  96. inline void LightDesc_t::InitPoint( const Vector &pos, const Vector &color )
  97. {
  98. m_Type=MATERIAL_LIGHT_POINT;
  99. m_Color=color;
  100. m_Position=pos;
  101. m_Range=0.0; // infinite
  102. m_Attenuation0=1.0;
  103. m_Attenuation1=0;
  104. m_Attenuation2=0;
  105. RecalculateDerivedValues();
  106. }
  107. //-----------------------------------------------------------------------------
  108. // a directional light with infinite range
  109. //-----------------------------------------------------------------------------
  110. inline void LightDesc_t::InitDirectional( const Vector &dir, const Vector &color )
  111. {
  112. m_Type=MATERIAL_LIGHT_DIRECTIONAL;
  113. m_Color=color;
  114. m_Direction=dir;
  115. m_Range=0.0; // infinite
  116. m_Attenuation0=1.0;
  117. m_Attenuation1=0;
  118. m_Attenuation2=0;
  119. RecalculateDerivedValues();
  120. }
  121. //-----------------------------------------------------------------------------
  122. // a simple light. cone boundaries in radians. you pass a look_at point and the
  123. // direciton is derived from that.
  124. //-----------------------------------------------------------------------------
  125. inline void LightDesc_t::InitSpot(const Vector &pos, const Vector &color, const Vector &point_at,
  126. float inner_cone_boundary, float outer_cone_boundary)
  127. {
  128. m_Type=MATERIAL_LIGHT_SPOT;
  129. m_Color=color;
  130. m_Position=pos;
  131. m_Direction=point_at;
  132. m_Direction-=pos;
  133. VectorNormalizeFast(m_Direction);
  134. m_Falloff=5.0; // linear angle falloff
  135. m_Theta=inner_cone_boundary;
  136. m_Phi=outer_cone_boundary;
  137. m_Range=0.0; // infinite
  138. m_Attenuation0=1.0;
  139. m_Attenuation1=0;
  140. m_Attenuation2=0;
  141. RecalculateDerivedValues();
  142. }
  143. #endif