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.

288 lines
7.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. //
  8. //-----------------------------------------------------------------------------
  9. // $Log: $
  10. //
  11. // $NoKeywords: $
  12. //=============================================================================//
  13. #include "cbase.h"
  14. #include "fx_discreetline.h"
  15. #include "materialsystem/imaterial.h"
  16. #include "materialsystem/imesh.h"
  17. #include "view.h"
  18. // memdbgon must be the last include file in a .cpp file!!!
  19. #include "tier0/memdbgon.h"
  20. /*
  21. ==================================================
  22. CFXLine
  23. ==================================================
  24. */
  25. CFXDiscreetLine::CFXDiscreetLine( const char *name, const Vector& start, const Vector& direction,
  26. float velocity, float length, float clipLength, float scale, float life, const char *shader )
  27. : CClientSideEffect( name )
  28. {
  29. assert( materials );
  30. if ( materials == NULL )
  31. return;
  32. // Create a material...
  33. m_pMaterial = materials->FindMaterial( shader, TEXTURE_GROUP_CLIENT_EFFECTS );
  34. m_pMaterial->IncrementReferenceCount();
  35. m_vecOrigin = start;
  36. m_vecDirection = direction;
  37. m_fVelocity = velocity;
  38. m_fClipLength = clipLength;
  39. m_fScale = scale;
  40. m_fLife = life;
  41. m_fStartTime = 0.0f;
  42. m_fLength = length;
  43. }
  44. CFXDiscreetLine::~CFXDiscreetLine( void )
  45. {
  46. Destroy();
  47. }
  48. // Does extra calculations to make them more visible over distance
  49. ConVar tracer_extra( "tracer_extra", "1" );
  50. /*
  51. ==================================================
  52. Draw
  53. ==================================================
  54. */
  55. void CFXDiscreetLine::Draw( double frametime )
  56. {
  57. Vector lineDir, viewDir, cross;
  58. Vector vecEnd, vecStart;
  59. Vector tmp;
  60. // Update the effect
  61. Update( frametime );
  62. // Calculate our distance along our path
  63. float sDistance = m_fVelocity * m_fStartTime;
  64. float eDistance = sDistance - m_fLength;
  65. //Clip to start
  66. sDistance = MAX( 0.0f, sDistance );
  67. eDistance = MAX( 0.0f, eDistance );
  68. if ( ( sDistance == 0.0f ) && ( eDistance == 0.0f ) )
  69. return;
  70. // Clip it
  71. if ( m_fClipLength != 0.0f )
  72. {
  73. sDistance = MIN( sDistance, m_fClipLength );
  74. eDistance = MIN( eDistance, m_fClipLength );
  75. }
  76. // Get our delta to calculate the tc offset
  77. float dDistance = fabs( sDistance - eDistance );
  78. float dTotal = ( m_fLength != 0.0f ) ? m_fLength : 0.01f;
  79. float fOffset = ( dDistance / dTotal );
  80. // Find our points along our path
  81. VectorMA( m_vecOrigin, sDistance, m_vecDirection, vecEnd );
  82. VectorMA( m_vecOrigin, eDistance, m_vecDirection, vecStart );
  83. //Setup our info for drawing the line
  84. VectorSubtract( vecEnd, vecStart, lineDir );
  85. VectorSubtract( vecEnd, CurrentViewOrigin(), viewDir );
  86. cross = lineDir.Cross( viewDir );
  87. VectorNormalize( cross );
  88. CMeshBuilder meshBuilder;
  89. IMesh *pMesh;
  90. CMatRenderContextPtr pRenderContext( materials );
  91. // Better, more visible tracers
  92. if ( tracer_extra.GetBool() )
  93. {
  94. float flScreenWidth = ScreenWidth();
  95. float flHalfScreenWidth = flScreenWidth * 0.5f;
  96. float zCoord = CurrentViewForward().Dot( vecStart - CurrentViewOrigin() );
  97. float flScreenSpaceWidth = m_fScale * flHalfScreenWidth / zCoord;
  98. float flAlpha;
  99. float flScale;
  100. if ( flScreenSpaceWidth < 0.5f )
  101. {
  102. flAlpha = RemapVal( flScreenSpaceWidth, 0.25f, 2.0f, 0.3f, 1.0f );
  103. flAlpha = clamp( flAlpha, 0.25f, 1.0f );
  104. flScale = 0.5f * zCoord / flHalfScreenWidth;
  105. }
  106. else
  107. {
  108. flAlpha = 1.0f;
  109. flScale = m_fScale;
  110. }
  111. //Bind the material
  112. pMesh = pRenderContext->GetDynamicMesh( true, NULL, NULL, m_pMaterial );
  113. meshBuilder.Begin( pMesh, MATERIAL_QUADS, 2 );
  114. float color = (int) 255.0f * flAlpha;
  115. //FIXME: for now no coloration
  116. VectorMA( vecStart, -flScale, cross, tmp );
  117. meshBuilder.Position3fv( tmp.Base() );
  118. meshBuilder.TexCoord2f( 0, 1.0f, 0.0f );
  119. meshBuilder.Color4ub( color, color, color, 255 );
  120. meshBuilder.Normal3fv( cross.Base() );
  121. meshBuilder.AdvanceVertex();
  122. VectorMA( vecStart, flScale, cross, tmp );
  123. meshBuilder.Position3fv( tmp.Base() );
  124. meshBuilder.TexCoord2f( 0, 0.0f, 0.0f );
  125. meshBuilder.Color4ub( color, color, color, 255 );
  126. meshBuilder.Normal3fv( cross.Base() );
  127. meshBuilder.AdvanceVertex();
  128. VectorMA( vecEnd, flScale, cross, tmp );
  129. meshBuilder.Position3fv( tmp.Base() );
  130. meshBuilder.TexCoord2f( 0, 0.0f, fOffset );
  131. meshBuilder.Color4ub( color, color, color, 255 );
  132. meshBuilder.Normal3fv( cross.Base() );
  133. meshBuilder.AdvanceVertex();
  134. VectorMA( vecEnd, -flScale, cross, tmp );
  135. meshBuilder.Position3fv( tmp.Base() );
  136. meshBuilder.TexCoord2f( 0, 1.0f, fOffset );
  137. meshBuilder.Color4ub( color, color, color, 255 );
  138. meshBuilder.Normal3fv( cross.Base() );
  139. meshBuilder.AdvanceVertex();
  140. flScale = flScale * 2.0f;
  141. color = (int) 64.0f * flAlpha;
  142. // Soft outline
  143. VectorMA( vecStart, -flScale, cross, tmp );
  144. meshBuilder.Position3fv( tmp.Base() );
  145. meshBuilder.TexCoord2f( 0, 1.0f, 0.0f );
  146. meshBuilder.Color4ub( color, color, color, 255 );
  147. meshBuilder.Normal3fv( cross.Base() );
  148. meshBuilder.AdvanceVertex();
  149. VectorMA( vecStart, flScale, cross, tmp );
  150. meshBuilder.Position3fv( tmp.Base() );
  151. meshBuilder.TexCoord2f( 0, 0.0f, 0.0f );
  152. meshBuilder.Color4ub( color, color, color, 255 );
  153. meshBuilder.Normal3fv( cross.Base() );
  154. meshBuilder.AdvanceVertex();
  155. VectorMA( vecEnd, flScale, cross, tmp );
  156. meshBuilder.Position3fv( tmp.Base() );
  157. meshBuilder.TexCoord2f( 0, 0.0f, fOffset );
  158. meshBuilder.Color4ub( color, color, color, 255 );
  159. meshBuilder.Normal3fv( cross.Base() );
  160. meshBuilder.AdvanceVertex();
  161. VectorMA( vecEnd, -flScale, cross, tmp );
  162. meshBuilder.Position3fv( tmp.Base() );
  163. meshBuilder.TexCoord2f( 0, 1.0f, fOffset );
  164. meshBuilder.Color4ub( color, color, color, 255 );
  165. meshBuilder.Normal3fv( cross.Base() );
  166. meshBuilder.AdvanceVertex();
  167. }
  168. else
  169. {
  170. //Bind the material
  171. pMesh = pRenderContext->GetDynamicMesh( true, NULL, NULL, m_pMaterial );
  172. meshBuilder.Begin( pMesh, MATERIAL_QUADS, 1 );
  173. //FIXME: for now no coloration
  174. VectorMA( vecStart, -m_fScale, cross, tmp );
  175. meshBuilder.Position3fv( tmp.Base() );
  176. meshBuilder.TexCoord2f( 0, 1.0f, 0.0f );
  177. meshBuilder.Color4ub( 255, 255, 255, 255 );
  178. meshBuilder.Normal3fv( cross.Base() );
  179. meshBuilder.AdvanceVertex();
  180. VectorMA( vecStart, m_fScale, cross, tmp );
  181. meshBuilder.Position3fv( tmp.Base() );
  182. meshBuilder.TexCoord2f( 0, 0.0f, 0.0f );
  183. meshBuilder.Color4ub( 255, 255, 255, 255 );
  184. meshBuilder.Normal3fv( cross.Base() );
  185. meshBuilder.AdvanceVertex();
  186. VectorMA( vecEnd, m_fScale, cross, tmp );
  187. meshBuilder.Position3fv( tmp.Base() );
  188. meshBuilder.TexCoord2f( 0, 0.0f, fOffset );
  189. meshBuilder.Color4ub( 255, 255, 255, 255 );
  190. meshBuilder.Normal3fv( cross.Base() );
  191. meshBuilder.AdvanceVertex();
  192. VectorMA( vecEnd, -m_fScale, cross, tmp );
  193. meshBuilder.Position3fv( tmp.Base() );
  194. meshBuilder.TexCoord2f( 0, 1.0f, fOffset );
  195. meshBuilder.Color4ub( 255, 255, 255, 255 );
  196. meshBuilder.Normal3fv( cross.Base() );
  197. meshBuilder.AdvanceVertex();
  198. }
  199. meshBuilder.End();
  200. pMesh->Draw();
  201. }
  202. /*
  203. ==================================================
  204. IsActive
  205. ==================================================
  206. */
  207. bool CFXDiscreetLine::IsActive( void )
  208. {
  209. return ( m_fLife > 0.0 ) ? true : false;
  210. }
  211. /*
  212. ==================================================
  213. Destroy
  214. ==================================================
  215. */
  216. void CFXDiscreetLine::Destroy( void )
  217. {
  218. //Release the material
  219. if ( m_pMaterial != NULL )
  220. {
  221. m_pMaterial->DecrementReferenceCount();
  222. m_pMaterial = NULL;
  223. }
  224. }
  225. /*
  226. ==================================================
  227. Update
  228. ==================================================
  229. */
  230. void CFXDiscreetLine::Update( double frametime )
  231. {
  232. m_fStartTime += frametime;
  233. m_fLife -= frametime;
  234. //Move our end points
  235. //VectorMA( m_vecStart, frametime, m_vecStartVelocity, m_vecStart );
  236. //VectorMA( m_vecEnd, frametime, m_vecStartVelocity, m_vecEnd );
  237. }