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.

134 lines
3.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "particle_simple3d.h"
  10. #include "view.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. // Defined in pm_math.c
  14. float anglemod( float a );
  15. //------------------------------------------------------------------------------
  16. // Purpose :
  17. // Input :
  18. // Output :
  19. //------------------------------------------------------------------------------
  20. CSmartPtr<CSimple3DEmitter> CSimple3DEmitter::Create( const char *pDebugName )
  21. {
  22. CSimple3DEmitter* pSimple3DEmitter = new CSimple3DEmitter( pDebugName );
  23. // Do in world space
  24. pSimple3DEmitter->m_ParticleEffect.SetEffectCameraSpace( false );
  25. return pSimple3DEmitter;
  26. }
  27. void CSimple3DEmitter::SimulateParticles( CParticleSimulateIterator *pIterator )
  28. {
  29. Particle3D *pParticle = (Particle3D*)pIterator->GetFirst();
  30. while ( pParticle )
  31. {
  32. const float timeDelta = pIterator->GetTimeDelta();
  33. //Should this particle die?
  34. pParticle->m_flLifeRemaining -= timeDelta;
  35. if ( pParticle->IsDead() )
  36. {
  37. pIterator->RemoveParticle( pParticle );
  38. }
  39. else
  40. {
  41. // Angular rotation
  42. pParticle->m_vAngles.x += pParticle->m_flAngSpeed * timeDelta;
  43. pParticle->m_vAngles.y += pParticle->m_flAngSpeed * timeDelta;
  44. pParticle->m_vAngles.z += pParticle->m_flAngSpeed * timeDelta;
  45. //Simulate the movement with collision
  46. trace_t trace;
  47. m_ParticleCollision.MoveParticle( pParticle->m_Pos, pParticle->m_vecVelocity, &pParticle->m_flAngSpeed, timeDelta, &trace );
  48. // ---------------------------------------
  49. // Decay towards flat
  50. // ---------------------------------------
  51. if (pParticle->m_flAngSpeed == 0 || trace.fraction != 1.0)
  52. {
  53. pParticle->m_vAngles.x = anglemod(pParticle->m_vAngles.x);
  54. if (pParticle->m_vAngles.x < 180)
  55. {
  56. if (fabs(pParticle->m_vAngles.x - 90) > 0.5)
  57. {
  58. pParticle->m_vAngles.x = 0.5*pParticle->m_vAngles.x + 46;
  59. }
  60. }
  61. else
  62. {
  63. if (fabs(pParticle->m_vAngles.x - 270) > 0.5)
  64. {
  65. pParticle->m_vAngles.x = 0.5*pParticle->m_vAngles.x + 135;
  66. }
  67. }
  68. pParticle->m_vAngles.y = anglemod(pParticle->m_vAngles.y);
  69. if (fabs(pParticle->m_vAngles.y) > 0.5)
  70. {
  71. pParticle->m_vAngles.y = 0.5*pParticle->m_vAngles.z;
  72. }
  73. }
  74. }
  75. pParticle = (Particle3D*)pIterator->GetNext();
  76. }
  77. }
  78. void CSimple3DEmitter::RenderParticles( CParticleRenderIterator *pIterator )
  79. {
  80. const Particle3D *pParticle = (const Particle3D *)pIterator->GetFirst();
  81. while ( pParticle )
  82. {
  83. float sortKey = CurrentViewForward().Dot( CurrentViewOrigin() - pParticle->m_Pos );
  84. // -------------------------------------------------------
  85. // Set color based on direction towards camera
  86. // -------------------------------------------------------
  87. Vector color;
  88. Vector vFaceNorm;
  89. Vector vCameraToFace = (pParticle->m_Pos - CurrentViewOrigin());
  90. AngleVectors(pParticle->m_vAngles,&vFaceNorm);
  91. float flFacing = DotProduct(vCameraToFace,vFaceNorm);
  92. if (flFacing <= 0)
  93. {
  94. color[0] = pParticle->m_uchFrontColor[0] / 255.0f;
  95. color[1] = pParticle->m_uchFrontColor[1] / 255.0f;
  96. color[2] = pParticle->m_uchFrontColor[2] / 255.0f;
  97. }
  98. else
  99. {
  100. color[0] = pParticle->m_uchBackColor[0] / 255.0f;
  101. color[1] = pParticle->m_uchBackColor[1] / 255.0f;
  102. color[2] = pParticle->m_uchBackColor[2] / 255.0f;
  103. }
  104. //Render it in world space
  105. RenderParticle_ColorSizeAngles(
  106. pIterator->GetParticleDraw(),
  107. pParticle->m_Pos,
  108. color,
  109. pParticle->GetFadeFraction(),
  110. pParticle->m_uchSize,
  111. pParticle->m_vAngles);
  112. pParticle = (const Particle3D *)pIterator->GetNext( sortKey );
  113. }
  114. }