Counter Strike : Global Offensive Source Code
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.

185 lines
5.9 KiB

  1. //===== Copyright � 1996-2005, 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. // NOTE: theta and phi are *half angles*
  40. float m_Theta; //< inner cone angle. no angular falloff
  41. //< within this cone
  42. float m_Phi; //< outer cone angle
  43. // the values below are derived from the above settings for optimizations
  44. // These aren't used by DX8. . used for software lighting.
  45. // NOTE: These dots are cos( m_Theta ), cos( m_Phi )
  46. float m_ThetaDot;
  47. float m_PhiDot;
  48. float m_OneOverThetaDotMinusPhiDot;
  49. unsigned int m_Flags;
  50. protected:
  51. float m_RangeSquared;
  52. public:
  53. void RecalculateDerivedValues(void); // calculate m_xxDot, m_Type for changed parms
  54. void RecalculateOneOverThetaDotMinusPhiDot();
  55. LightDesc_t(void)
  56. {
  57. }
  58. // constructors for various useful subtypes
  59. // a point light with infinite range
  60. LightDesc_t( const Vector &pos, const Vector &color )
  61. {
  62. InitPoint( pos, color );
  63. }
  64. LightDesc_t &operator=( const LightDesc_t &src )
  65. {
  66. memcpy( this, &src, sizeof(LightDesc_t) );
  67. return *this;
  68. }
  69. /// a simple light. cone boundaries in radians. you pass a look_at point and the
  70. /// direciton is derived from that.
  71. LightDesc_t( const Vector &pos, const Vector &color, const Vector &point_at,
  72. float inner_cone_boundary, float outer_cone_boundary )
  73. {
  74. InitSpot( pos, color, point_at, inner_cone_boundary, outer_cone_boundary );
  75. }
  76. void InitPoint( const Vector &pos, const Vector &color );
  77. void InitDirectional( const Vector &dir, const Vector &color );
  78. void InitSpot(const Vector &pos, const Vector &color, const Vector &point_at,
  79. float inner_cone_boundary, float outer_cone_boundary );
  80. /// Given 4 points and 4 normals, ADD lighting from this light into "color".
  81. void ComputeLightAtPoints( const FourVectors &pos, const FourVectors &normal,
  82. FourVectors &color, bool DoHalfLambert=false ) const;
  83. void ComputeNonincidenceLightAtPoints( const FourVectors &pos, FourVectors &color ) const;
  84. void ComputeLightAtPointsForDirectional( const FourVectors &pos,
  85. const FourVectors &normal,
  86. FourVectors &color, bool DoHalfLambert=false ) const;
  87. // warning - modifies color!!! set color first!!
  88. void SetupOldStyleAttenuation( float fQuadatricAttn, float fLinearAttn, float fConstantAttn );
  89. void SetupNewStyleAttenuation( float fFiftyPercentDistance, float fZeroPercentDistance );
  90. /// given a direction relative to the light source position, is this ray within the
  91. /// light cone (for spotlights..non spots consider all rays to be within their cone)
  92. bool IsDirectionWithinLightCone(const Vector &rdir) const
  93. {
  94. return ( ( m_Type != MATERIAL_LIGHT_SPOT ) || ( rdir.Dot(m_Direction) >= m_PhiDot ) );
  95. }
  96. float OneOverThetaDotMinusPhiDot() const
  97. {
  98. return m_OneOverThetaDotMinusPhiDot;
  99. }
  100. float DistanceAtWhichBrightnessIsLessThan( float flAmount ) const;
  101. };
  102. //-----------------------------------------------------------------------------
  103. // a point light with infinite range
  104. //-----------------------------------------------------------------------------
  105. inline void LightDesc_t::InitPoint( const Vector &pos, const Vector &color )
  106. {
  107. m_Type=MATERIAL_LIGHT_POINT;
  108. m_Color=color;
  109. m_Position=pos;
  110. m_Range=0.0; // infinite
  111. m_Attenuation0=1.0;
  112. m_Attenuation1=0;
  113. m_Attenuation2=0;
  114. RecalculateDerivedValues();
  115. }
  116. //-----------------------------------------------------------------------------
  117. // a directional light with infinite range
  118. //-----------------------------------------------------------------------------
  119. inline void LightDesc_t::InitDirectional( const Vector &dir, const Vector &color )
  120. {
  121. m_Type=MATERIAL_LIGHT_DIRECTIONAL;
  122. m_Color=color;
  123. m_Direction=dir;
  124. m_Range=0.0; // infinite
  125. m_Attenuation0=1.0;
  126. m_Attenuation1=0;
  127. m_Attenuation2=0;
  128. RecalculateDerivedValues();
  129. }
  130. //-----------------------------------------------------------------------------
  131. // a simple light. cone boundaries in radians. you pass a look_at point and the
  132. // direciton is derived from that.
  133. //-----------------------------------------------------------------------------
  134. inline void LightDesc_t::InitSpot(const Vector &pos, const Vector &color, const Vector &point_at,
  135. float inner_cone_boundary, float outer_cone_boundary)
  136. {
  137. m_Type=MATERIAL_LIGHT_SPOT;
  138. m_Color=color;
  139. m_Position=pos;
  140. m_Direction=point_at;
  141. m_Direction-=pos;
  142. VectorNormalizeFast(m_Direction);
  143. m_Falloff=5.0; // linear angle falloff
  144. m_Theta=inner_cone_boundary;
  145. m_Phi=outer_cone_boundary;
  146. m_Range=0.0; // infinite
  147. m_Attenuation0=1.0;
  148. m_Attenuation1=0;
  149. m_Attenuation2=0;
  150. RecalculateDerivedValues();
  151. }
  152. #endif