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.

166 lines
4.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #include "cbase.h"
  10. #include "clientsideeffects.h"
  11. #include "fx_staticline.h"
  12. #include "materialsystem/imaterial.h"
  13. #include "materialsystem/imesh.h"
  14. #include "view.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. /*
  18. ==================================================
  19. CFXStaticLine
  20. ==================================================
  21. */
  22. CFXStaticLine::CFXStaticLine( const char *name, const Vector& start, const Vector& end, float scale, float life, const char *shader, unsigned int flags )
  23. : CClientSideEffect( name )
  24. {
  25. assert( materials );
  26. if ( materials == NULL )
  27. return;
  28. // Create a material...
  29. m_pMaterial = materials->FindMaterial( shader, TEXTURE_GROUP_CLIENT_EFFECTS );
  30. m_pMaterial->IncrementReferenceCount();
  31. //Fill in the rest of the fields
  32. m_vecStart = start;
  33. m_vecEnd = end;
  34. m_uiFlags = flags;
  35. m_fLife = life;
  36. m_fScale = scale * 0.5f;
  37. }
  38. CFXStaticLine::~CFXStaticLine( void )
  39. {
  40. Destroy();
  41. }
  42. //==================================================
  43. // Purpose: Draw the primitive
  44. // Input: frametime - the time, this frame
  45. //==================================================
  46. void CFXStaticLine::Draw( double frametime )
  47. {
  48. Vector lineDir, viewDir, cross;
  49. Vector tmp;
  50. // Update the effect
  51. Update( frametime );
  52. // Get the proper orientation for the line
  53. VectorSubtract( m_vecEnd, m_vecStart, lineDir );
  54. VectorSubtract( m_vecEnd, CurrentViewOrigin(), viewDir );
  55. cross = lineDir.Cross( viewDir );
  56. VectorNormalize( cross );
  57. CMatRenderContextPtr pRenderContext( materials );
  58. //Bind the material
  59. IMesh* pMesh = pRenderContext->GetDynamicMesh( true, NULL, NULL, m_pMaterial );
  60. CMeshBuilder meshBuilder;
  61. meshBuilder.Begin( pMesh, MATERIAL_QUADS, 1 );
  62. bool flipVertical = (m_uiFlags & FXSTATICLINE_FLIP_VERTICAL) != 0;
  63. bool flipHorizontal = (m_uiFlags & FXSTATICLINE_FLIP_HORIZONTAL ) != 0;
  64. //Setup our points
  65. VectorMA( m_vecStart, -m_fScale, cross, tmp );
  66. meshBuilder.Position3fv( tmp.Base() );
  67. meshBuilder.Normal3fv( cross.Base() );
  68. if (flipHorizontal)
  69. meshBuilder.TexCoord2f( 0, 0.0f, 1.0f );
  70. else if (flipVertical)
  71. meshBuilder.TexCoord2f( 0, 0.0f, 0.0f );
  72. else
  73. meshBuilder.TexCoord2f( 0, 1.0f, 1.0f );
  74. meshBuilder.Color4ub( 255, 255, 255, 255 );
  75. meshBuilder.AdvanceVertex();
  76. VectorMA( m_vecStart, m_fScale, cross, tmp );
  77. meshBuilder.Position3fv( tmp.Base() );
  78. meshBuilder.Normal3fv( cross.Base() );
  79. if (flipHorizontal)
  80. meshBuilder.TexCoord2f( 0, 1.0f, 1.0f );
  81. else if (flipVertical)
  82. meshBuilder.TexCoord2f( 0, 1.0f, 0.0f );
  83. else
  84. meshBuilder.TexCoord2f( 0, 0.0f, 1.0f );
  85. meshBuilder.Color4ub( 255, 255, 255, 255 );
  86. meshBuilder.AdvanceVertex();
  87. VectorMA( m_vecEnd, m_fScale, cross, tmp );
  88. meshBuilder.Position3fv( tmp.Base() );
  89. meshBuilder.Normal3fv( cross.Base() );
  90. if (flipHorizontal)
  91. meshBuilder.TexCoord2f( 0, 1.0f, 0.0f );
  92. else if (flipVertical)
  93. meshBuilder.TexCoord2f( 0, 1.0f, 1.0f );
  94. else
  95. meshBuilder.TexCoord2f( 0, 0.0f, 0.0f );
  96. meshBuilder.Color4ub( 255, 255, 255, 255 );
  97. meshBuilder.AdvanceVertex();
  98. VectorMA( m_vecEnd, -m_fScale, cross, tmp );
  99. meshBuilder.Position3fv( tmp.Base() );
  100. meshBuilder.Normal3fv( cross.Base() );
  101. if (flipHorizontal)
  102. meshBuilder.TexCoord2f( 0, 0.0f, 0.0f );
  103. else if (flipVertical)
  104. meshBuilder.TexCoord2f( 0, 0.0f, 1.0f );
  105. else
  106. meshBuilder.TexCoord2f( 0, 1.0f, 0.0f );
  107. meshBuilder.Color4ub( 255, 255, 255, 255 );
  108. meshBuilder.AdvanceVertex();
  109. meshBuilder.End();
  110. pMesh->Draw();
  111. }
  112. //==================================================
  113. // Purpose: Returns whether or not the effect is still active
  114. // Output: true if active
  115. //==================================================
  116. bool CFXStaticLine::IsActive( void )
  117. {
  118. return ( m_fLife > 0.0 ) ? true : false;
  119. }
  120. //==================================================
  121. // Purpose: Destroy the effect and its local resources
  122. //==================================================
  123. void CFXStaticLine::Destroy( void )
  124. {
  125. //Release the material
  126. if ( m_pMaterial != NULL )
  127. {
  128. m_pMaterial->DecrementReferenceCount();
  129. m_pMaterial = NULL;
  130. }
  131. }
  132. //==================================================
  133. // Purpose: Perform any necessary updates
  134. // Input: frametime - the time, this frame
  135. //==================================================
  136. void CFXStaticLine::Update( double frametime )
  137. {
  138. m_fLife -= frametime;
  139. }