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.

229 lines
5.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef PARTICLESPHERERENDERER_H
  8. #define PARTICLESPHERERENDERER_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "particlemgr.h"
  13. #include "particle_util.h"
  14. class CParticleSphereRenderer
  15. {
  16. public:
  17. CParticleSphereRenderer();
  18. // Initialize and tell it the material you'll be using.
  19. void Init( CParticleMgr *pParticleMgr, IMaterial *pMaterial );
  20. // Pass this call through from your particle system too.
  21. void StartRender( VMatrix &effectMatrix );
  22. // Call this to render a spherical particle.
  23. void RenderParticle(
  24. ParticleDraw *pDraw,
  25. const Vector &vOriginalPos,
  26. const Vector &vTransformedPos,
  27. float flAlpha, // value 0 - 255.4
  28. float flParticleSize,
  29. float flAngle = 0.0f );
  30. void RenderParticle_AddColor(
  31. ParticleDraw *pDraw,
  32. const Vector &vOriginalPos,
  33. const Vector &vTransformedPos,
  34. float flAlpha, // value 0 - 255.4
  35. float flParticleSize,
  36. const Vector &vToAdd0to1 // Add this to the color (value 0-1)
  37. );
  38. // Lighting is (base color) + (ambient / dist^2) + bump(directional / dist^2)
  39. const Vector& GetBaseColor() const; // Specified as 0-1
  40. void SetBaseColor( const Vector &vColor );
  41. const CParticleLightInfo& GetAmbientLight() const;
  42. void SetAmbientLight( const CParticleLightInfo &info );
  43. const CParticleLightInfo& GetDirectionalLight() const;
  44. void SetDirectionalLight( const CParticleLightInfo &info );
  45. private:
  46. void AddLightColor(
  47. Vector const *pPos,
  48. Vector const *pLightPos,
  49. Vector const *pLightColor,
  50. float flLightIntensity,
  51. Vector *pOutColor );
  52. inline void ClampColor( Vector &vColor );
  53. private:
  54. int m_iLastTickStartRenderCalled; // Used for debugging.
  55. CParticleMgr *m_pParticleMgr;
  56. Vector m_vBaseColor;
  57. CParticleLightInfo m_AmbientLight;
  58. CParticleLightInfo m_DirectionalLight;
  59. bool m_bUsingPixelShaders;
  60. };
  61. // ------------------------------------------------------------------------ //
  62. // Inlines.
  63. // ------------------------------------------------------------------------ //
  64. inline void CParticleSphereRenderer::AddLightColor(
  65. Vector const *pPos,
  66. Vector const *pLightPos,
  67. Vector const *pLightColor,
  68. float flLightIntensity,
  69. Vector *pOutColor )
  70. {
  71. if( flLightIntensity )
  72. {
  73. float fDist = pPos->DistToSqr( *pLightPos );
  74. float fAmt;
  75. if( fDist > 0.0001f )
  76. fAmt = flLightIntensity / fDist;
  77. else
  78. fAmt = 1000.f;
  79. *pOutColor += *pLightColor * fAmt;
  80. }
  81. }
  82. inline void CParticleSphereRenderer::ClampColor( Vector &vColor )
  83. {
  84. float flMax = MAX( vColor.x, MAX( vColor.y, vColor.z ) );
  85. if( flMax > 1 )
  86. {
  87. vColor *= 255.0f / flMax;
  88. }
  89. else
  90. {
  91. vColor *= 255.0;
  92. }
  93. }
  94. inline const Vector& CParticleSphereRenderer::GetBaseColor() const
  95. {
  96. return m_vBaseColor;
  97. }
  98. inline void CParticleSphereRenderer::SetBaseColor( const Vector &vColor )
  99. {
  100. m_vBaseColor = vColor;
  101. }
  102. inline const CParticleLightInfo& CParticleSphereRenderer::GetAmbientLight() const
  103. {
  104. return m_AmbientLight;
  105. }
  106. inline void CParticleSphereRenderer::SetAmbientLight( const CParticleLightInfo &info )
  107. {
  108. m_AmbientLight = info;
  109. }
  110. inline const CParticleLightInfo& CParticleSphereRenderer::GetDirectionalLight() const
  111. {
  112. return m_DirectionalLight;
  113. }
  114. inline void CParticleSphereRenderer::SetDirectionalLight( const CParticleLightInfo &info )
  115. {
  116. m_DirectionalLight = info;
  117. }
  118. inline void CParticleSphereRenderer::RenderParticle(
  119. ParticleDraw *pDraw,
  120. const Vector &vOriginalPos,
  121. const Vector &vTransformedPos,
  122. float flAlpha,
  123. float flParticleSize,
  124. float flAngle )
  125. {
  126. // Make sure they called StartRender on us so we were able to set the directional light parameters.
  127. #ifdef _DEBUG
  128. if ( pDraw->GetMeshBuilder() )
  129. {
  130. Assert( m_iLastTickStartRenderCalled == gpGlobals->tickcount );
  131. }
  132. #endif
  133. Vector vColor = m_vBaseColor;
  134. AddLightColor( &vOriginalPos, &m_AmbientLight.m_vPos, &m_AmbientLight.m_vColor, m_AmbientLight.m_flIntensity, &vColor );
  135. // If the DX8 shader isn't going to handle the directional light color, then add its contribution here.
  136. if( !m_bUsingPixelShaders )
  137. {
  138. AddLightColor( &vOriginalPos, &m_DirectionalLight.m_vPos, &m_DirectionalLight.m_vColor, m_DirectionalLight.m_flIntensity, &vColor );
  139. }
  140. ClampColor( vColor );
  141. RenderParticle_Color255SizeNormalAngle(
  142. pDraw,
  143. vTransformedPos,
  144. vColor, // ambient color
  145. flAlpha, // alpha
  146. flParticleSize,
  147. vec3_origin,
  148. flAngle );
  149. }
  150. inline void CParticleSphereRenderer::RenderParticle_AddColor(
  151. ParticleDraw *pDraw,
  152. const Vector &vOriginalPos,
  153. const Vector &vTransformedPos,
  154. float flAlpha,
  155. float flParticleSize,
  156. const Vector &vToAdd0to1
  157. )
  158. {
  159. // Make sure they called StartRender on us so we were able to set the directional light parameters.
  160. #ifdef _DEBUG
  161. if ( pDraw->GetMeshBuilder() )
  162. {
  163. Assert( m_iLastTickStartRenderCalled == gpGlobals->tickcount );
  164. }
  165. #endif
  166. Vector vColor = m_vBaseColor + vToAdd0to1;
  167. AddLightColor( &vOriginalPos, &m_AmbientLight.m_vPos, &m_AmbientLight.m_vColor, m_AmbientLight.m_flIntensity, &vColor );
  168. // If the DX8 shader isn't going to handle the directional light color, then add its contribution here.
  169. if( !m_bUsingPixelShaders )
  170. {
  171. AddLightColor( &vOriginalPos, &m_DirectionalLight.m_vPos, &m_DirectionalLight.m_vColor, m_DirectionalLight.m_flIntensity, &vColor );
  172. }
  173. ClampColor( vColor );
  174. RenderParticle_Color255Size(
  175. pDraw,
  176. vTransformedPos,
  177. vColor, // ambient color
  178. flAlpha, // alpha
  179. flParticleSize );
  180. }
  181. #endif // PARTICLESPHERERENDERER_H