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.

271 lines
8.5 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "movieobjects/dmelight.h"
  7. #include "tier0/dbg.h"
  8. #include "datamodel/dmelementfactoryhelper.h"
  9. #include "mathlib/vector.h"
  10. #include "movieobjects/dmetransform.h"
  11. #include "materialsystem/imaterialsystem.h"
  12. #include "movieobjects_interfaces.h"
  13. #include "tier2/tier2.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. //-----------------------------------------------------------------------------
  17. // Expose this class to the scene database
  18. //-----------------------------------------------------------------------------
  19. IMPLEMENT_ELEMENT_FACTORY( DmeLight, CDmeLight );
  20. //-----------------------------------------------------------------------------
  21. // Purpose:
  22. //-----------------------------------------------------------------------------
  23. void CDmeLight::OnConstruction()
  24. {
  25. m_Color.InitAndSet( this, "color", Color( 255, 255, 255, 255 ) );
  26. m_flIntensity.InitAndSet( this, "intensity", 1.0f );
  27. }
  28. void CDmeLight::OnDestruction()
  29. {
  30. }
  31. //-----------------------------------------------------------------------------
  32. // Sets the color and intensity
  33. // NOTE: Color is specified 0-255 floating point.
  34. //-----------------------------------------------------------------------------
  35. void CDmeLight::SetColor( const Color &color )
  36. {
  37. m_Color.Set( color );
  38. }
  39. void CDmeLight::SetIntensity( float flIntensity )
  40. {
  41. m_flIntensity = flIntensity;
  42. }
  43. //-----------------------------------------------------------------------------
  44. // Sets up render state in the material system for rendering
  45. //-----------------------------------------------------------------------------
  46. void CDmeLight::SetupRenderStateInternal( LightDesc_t &desc, float flAtten0, float flAtten1, float flAtten2 )
  47. {
  48. desc.m_Color[0] = m_Color.Get().r();
  49. desc.m_Color[1] = m_Color.Get().g();
  50. desc.m_Color[2] = m_Color.Get().b();
  51. desc.m_Color *= m_flIntensity / 255.0f;
  52. desc.m_Attenuation0 = flAtten0;
  53. desc.m_Attenuation1 = flAtten1;
  54. desc.m_Attenuation2 = flAtten2;
  55. desc.m_Flags = 0;
  56. if ( desc.m_Attenuation0 != 0.0f )
  57. {
  58. desc.m_Flags |= LIGHTTYPE_OPTIMIZATIONFLAGS_HAS_ATTENUATION0;
  59. }
  60. if ( desc.m_Attenuation1 != 0.0f )
  61. {
  62. desc.m_Flags |= LIGHTTYPE_OPTIMIZATIONFLAGS_HAS_ATTENUATION1;
  63. }
  64. if ( desc.m_Attenuation2 != 0.0f )
  65. {
  66. desc.m_Flags |= LIGHTTYPE_OPTIMIZATIONFLAGS_HAS_ATTENUATION2;
  67. }
  68. }
  69. //-----------------------------------------------------------------------------
  70. //
  71. // A directional light
  72. //
  73. //-----------------------------------------------------------------------------
  74. IMPLEMENT_ELEMENT_FACTORY( DmeDirectionalLight, CDmeDirectionalLight );
  75. //-----------------------------------------------------------------------------
  76. // Purpose:
  77. //-----------------------------------------------------------------------------
  78. void CDmeDirectionalLight::OnConstruction()
  79. {
  80. }
  81. void CDmeDirectionalLight::OnDestruction()
  82. {
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Gets a light desc for the light
  86. //-----------------------------------------------------------------------------
  87. bool CDmeDirectionalLight::GetLightDesc( LightDesc_t *pDesc )
  88. {
  89. memset( pDesc, 0, sizeof(LightDesc_t) );
  90. pDesc->m_Type = MATERIAL_LIGHT_DIRECTIONAL;
  91. SetupRenderStateInternal( *pDesc, 1.0f, 0.0f, 0.0f );
  92. matrix3x4_t m;
  93. GetAbsTransform( m );
  94. MatrixGetColumn( m, 0, pDesc->m_Direction ); // from mathlib_base.cpp: MatrixVectors(): Matrix is right-handed x=forward, y=left, z=up. We a left-handed convention for vectors in the game code (forward, right, up)
  95. pDesc->m_Theta = 0.0f;
  96. pDesc->m_Phi = 0.0f;
  97. pDesc->m_Falloff = 1.0f;
  98. pDesc->RecalculateDerivedValues();
  99. return true;
  100. }
  101. //-----------------------------------------------------------------------------
  102. //
  103. // A point light
  104. //
  105. //-----------------------------------------------------------------------------
  106. IMPLEMENT_ELEMENT_FACTORY( DmePointLight, CDmePointLight );
  107. //-----------------------------------------------------------------------------
  108. // Purpose:
  109. //-----------------------------------------------------------------------------
  110. void CDmePointLight::OnConstruction()
  111. {
  112. m_flAttenuation0.InitAndSet( this, "constantAttenuation", 1.0f );
  113. m_flAttenuation1.InitAndSet( this, "linearAttenuation", 0.0f );
  114. m_flAttenuation2.InitAndSet( this, "quadraticAttenuation", 0.0f );
  115. m_flMaxDistance.InitAndSet( this, "maxDistance", 600.0f ); // 50 feet
  116. }
  117. void CDmePointLight::OnDestruction()
  118. {
  119. }
  120. //-----------------------------------------------------------------------------
  121. // Sets the attenuation factors
  122. //-----------------------------------------------------------------------------
  123. void CDmePointLight::SetAttenuation( float flConstant, float flLinear, float flQuadratic )
  124. {
  125. m_flAttenuation0 = flConstant;
  126. m_flAttenuation1 = flLinear;
  127. m_flAttenuation2 = flQuadratic;
  128. }
  129. //-----------------------------------------------------------------------------
  130. // Sets the maximum range
  131. //-----------------------------------------------------------------------------
  132. void CDmePointLight::SetMaxDistance( float flMaxDistance )
  133. {
  134. m_flMaxDistance = flMaxDistance;
  135. }
  136. //-----------------------------------------------------------------------------
  137. // Sets up render state in the material system for rendering
  138. //-----------------------------------------------------------------------------
  139. bool CDmePointLight::GetLightDesc( LightDesc_t *pDesc )
  140. {
  141. memset( pDesc, 0, sizeof(LightDesc_t) );
  142. pDesc->m_Type = MATERIAL_LIGHT_POINT;
  143. SetupRenderStateInternal( *pDesc, m_flAttenuation0, m_flAttenuation1, m_flAttenuation2 );
  144. matrix3x4_t m;
  145. GetAbsTransform( m );
  146. MatrixPosition( m, pDesc->m_Position );
  147. pDesc->m_Direction.Init( 0, 0, 1 );
  148. pDesc->m_Range = m_flMaxDistance;
  149. pDesc->m_Theta = 0.0f;
  150. pDesc->m_Phi = 0.0f;
  151. pDesc->m_Falloff = 1.0f;
  152. pDesc->RecalculateDerivedValues();
  153. return true;
  154. }
  155. //-----------------------------------------------------------------------------
  156. //
  157. // A spot light
  158. //
  159. //-----------------------------------------------------------------------------
  160. IMPLEMENT_ELEMENT_FACTORY( DmeSpotLight, CDmeSpotLight );
  161. //-----------------------------------------------------------------------------
  162. // Purpose:
  163. //-----------------------------------------------------------------------------
  164. void CDmeSpotLight::OnConstruction()
  165. {
  166. m_flSpotInnerAngle.InitAndSet( this, "spotInnerAngle", 60.0f );
  167. m_flSpotOuterAngle.InitAndSet( this, "spotOuterAngle", 90.0f );
  168. m_flSpotAngularFalloff.InitAndSet( this, "spotAngularFalloff", 1.0f );
  169. }
  170. void CDmeSpotLight::OnDestruction()
  171. {
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Sets the spotlight angle factors
  175. // Angles are specified in degrees, as full angles (as opposed to half-angles)
  176. //-----------------------------------------------------------------------------
  177. void CDmeSpotLight::SetAngles( float flInnerAngle, float flOuterAngle, float flAngularFalloff )
  178. {
  179. m_flSpotInnerAngle = flInnerAngle;
  180. m_flSpotOuterAngle = flOuterAngle;
  181. m_flSpotAngularFalloff = flAngularFalloff;
  182. }
  183. //-----------------------------------------------------------------------------
  184. // Sets up render state in the material system for rendering
  185. //-----------------------------------------------------------------------------
  186. bool CDmeSpotLight::GetLightDesc( LightDesc_t *pDesc )
  187. {
  188. memset( pDesc, 0, sizeof(LightDesc_t) );
  189. pDesc->m_Type = MATERIAL_LIGHT_SPOT;
  190. SetupRenderStateInternal( *pDesc, m_flAttenuation0, m_flAttenuation1, m_flAttenuation2 );
  191. matrix3x4_t m;
  192. GetAbsTransform( m );
  193. MatrixPosition( m, pDesc->m_Position );
  194. MatrixGetColumn( m, 0, pDesc->m_Direction ); // from mathlib_base.cpp: MatrixVectors(): Matrix is right-handed x=forward, y=left, z=up. We a left-handed convention for vectors in the game code (forward, right, up)
  195. pDesc->m_Range = m_flMaxDistance;
  196. // Convert to radians
  197. pDesc->m_Theta = 0.5f * m_flSpotInnerAngle * M_PI / 180.0f;
  198. pDesc->m_Phi = 0.5f * m_flSpotOuterAngle * M_PI / 180.0f;
  199. pDesc->m_Falloff = m_flSpotAngularFalloff;
  200. pDesc->RecalculateDerivedValues();
  201. return true;
  202. }
  203. //-----------------------------------------------------------------------------
  204. //
  205. // An ambient light
  206. //
  207. //-----------------------------------------------------------------------------
  208. IMPLEMENT_ELEMENT_FACTORY( DmeAmbientLight, CDmeAmbientLight );
  209. //-----------------------------------------------------------------------------
  210. // Purpose:
  211. //-----------------------------------------------------------------------------
  212. void CDmeAmbientLight::OnConstruction()
  213. {
  214. }
  215. void CDmeAmbientLight::OnDestruction()
  216. {
  217. }