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.

310 lines
9.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #include "cbase.h"
  10. #include "materialsystem/imaterial.h"
  11. #include "clientsideeffects.h"
  12. #include "fx_line.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. CFXLine
  20. ==================================================
  21. */
  22. CFXLine::CFXLine( const char *name, const FXLineData_t &data )
  23. : CClientSideEffect( name )
  24. {
  25. m_FXData = data;
  26. m_FXData.m_flLifeTime = 0.0f;
  27. if ( m_FXData.m_pMaterial != NULL )
  28. {
  29. m_FXData.m_pMaterial->IncrementReferenceCount();
  30. }
  31. }
  32. CFXLine::~CFXLine( void )
  33. {
  34. Destroy();
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Purpose:
  38. // Input : frametime -
  39. //-----------------------------------------------------------------------------
  40. void CFXLine::Draw( double frametime )
  41. {
  42. // Update the effect
  43. Update( frametime );
  44. Vector lineDir, viewDir;
  45. //Get the proper orientation for the line
  46. VectorSubtract( m_FXData.m_vecStart, m_FXData.m_vecEnd, lineDir );
  47. VectorSubtract( m_FXData.m_vecEnd, CurrentViewOrigin(), viewDir );
  48. Vector cross = lineDir.Cross( viewDir );
  49. VectorNormalize( cross );
  50. CMatRenderContextPtr pRenderContext( materials );
  51. //Bind the material
  52. IMesh* pMesh = pRenderContext->GetDynamicMesh( true, NULL, NULL, m_FXData.m_pMaterial );
  53. CMeshBuilder meshBuilder;
  54. Vector tmp;
  55. meshBuilder.Begin( pMesh, MATERIAL_QUADS, 1 );
  56. float scaleTimePerc = ( m_FXData.m_flLifeTime / m_FXData.m_flDieTime );
  57. float scale = m_FXData.m_flStartScale + ( ( m_FXData.m_flEndScale - m_FXData.m_flStartScale ) * scaleTimePerc );
  58. color32 color = {255,255,255,255};
  59. float alpha = m_FXData.m_flStartAlpha + ( ( m_FXData.m_flEndAlpha - m_FXData.m_flStartAlpha ) * scaleTimePerc );
  60. alpha = clamp( alpha, 0.0f, 1.0f );
  61. color.a *= alpha;
  62. // Start
  63. VectorMA( m_FXData.m_vecStart, -scale, cross, tmp );
  64. meshBuilder.Position3fv( tmp.Base() );
  65. meshBuilder.TexCoord2f( 0, 1.0f, 1.0f );
  66. meshBuilder.Color4ub( color.r, color.g, color.b, color.a );
  67. meshBuilder.Normal3fv( cross.Base() );
  68. meshBuilder.AdvanceVertex();
  69. VectorMA( m_FXData.m_vecStart, scale, cross, tmp );
  70. meshBuilder.Position3fv( tmp.Base() );
  71. meshBuilder.TexCoord2f( 0, 0.0f, 1.0f );
  72. meshBuilder.Color4ub( color.r, color.g, color.b, color.a );
  73. meshBuilder.Normal3fv( cross.Base() );
  74. meshBuilder.AdvanceVertex();
  75. // End
  76. VectorMA( m_FXData.m_vecEnd, scale, cross, tmp );
  77. meshBuilder.Position3fv( tmp.Base() );
  78. meshBuilder.TexCoord2f( 0, 0.0f, 0.0f );
  79. meshBuilder.Color4ub( color.r, color.g, color.b, color.a );
  80. meshBuilder.Normal3fv( cross.Base() );
  81. meshBuilder.AdvanceVertex();
  82. VectorMA( m_FXData.m_vecEnd, -scale, cross, tmp );
  83. meshBuilder.Position3fv( tmp.Base() );
  84. meshBuilder.TexCoord2f( 0, 1.0f, 0.0f );
  85. meshBuilder.Color4ub( color.r, color.g, color.b, color.a );
  86. meshBuilder.Normal3fv( cross.Base() );
  87. meshBuilder.AdvanceVertex();
  88. meshBuilder.End();
  89. pMesh->Draw();
  90. }
  91. //-----------------------------------------------------------------------------
  92. // Purpose:
  93. // Output : Returns true on success, false on failure.
  94. //-----------------------------------------------------------------------------
  95. bool CFXLine::IsActive( void )
  96. {
  97. return ( m_FXData.m_flLifeTime < m_FXData.m_flDieTime ) ? true : false;
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Purpose:
  101. //-----------------------------------------------------------------------------
  102. void CFXLine::Destroy( void )
  103. {
  104. //Release the material
  105. if ( m_FXData.m_pMaterial != NULL )
  106. {
  107. m_FXData.m_pMaterial->DecrementReferenceCount();
  108. m_FXData.m_pMaterial = NULL;
  109. }
  110. }
  111. //-----------------------------------------------------------------------------
  112. // Purpose:
  113. // Input : frametime -
  114. //-----------------------------------------------------------------------------
  115. void CFXLine::Update( double frametime )
  116. {
  117. m_FXData.m_flLifeTime += frametime;
  118. //Move our end points
  119. VectorMA( m_FXData.m_vecStart, frametime, m_FXData.m_vecStartVelocity, m_FXData.m_vecStart );
  120. VectorMA( m_FXData.m_vecEnd, frametime, m_FXData.m_vecEndVelocity, m_FXData.m_vecEnd );
  121. }
  122. void FX_DrawLine( const Vector &start, const Vector &end, float scale, IMaterial *pMaterial, const color32 &color )
  123. {
  124. Vector lineDir, viewDir;
  125. //Get the proper orientation for the line
  126. VectorSubtract( end, start, lineDir );
  127. VectorSubtract( end, CurrentViewOrigin(), viewDir );
  128. Vector cross = lineDir.Cross( viewDir );
  129. VectorNormalize( cross );
  130. CMatRenderContextPtr pRenderContext( materials );
  131. //Bind the material
  132. IMesh* pMesh = pRenderContext->GetDynamicMesh( true, NULL, NULL, pMaterial );
  133. CMeshBuilder meshBuilder;
  134. Vector tmp;
  135. meshBuilder.Begin( pMesh, MATERIAL_QUADS, 1 );
  136. VectorMA( start, -scale, cross, tmp );
  137. meshBuilder.Position3fv( tmp.Base() );
  138. meshBuilder.TexCoord2f( 0, 1.0f, 1.0f );
  139. meshBuilder.Color4ub( color.r, color.g, color.b, color.a );
  140. meshBuilder.Normal3fv( cross.Base() );
  141. meshBuilder.AdvanceVertex();
  142. VectorMA( start, scale, cross, tmp );
  143. meshBuilder.Position3fv( tmp.Base() );
  144. meshBuilder.TexCoord2f( 0, 0.0f, 1.0f );
  145. meshBuilder.Color4ub( color.r, color.g, color.b, color.a );
  146. meshBuilder.Normal3fv( cross.Base() );
  147. meshBuilder.AdvanceVertex();
  148. VectorMA( end, scale, cross, tmp );
  149. meshBuilder.Position3fv( tmp.Base() );
  150. meshBuilder.TexCoord2f( 0, 0.0f, 0.0f );
  151. meshBuilder.Color4ub( color.r, color.g, color.b, color.a );
  152. meshBuilder.Normal3fv( cross.Base() );
  153. meshBuilder.AdvanceVertex();
  154. VectorMA( end, -scale, cross, tmp );
  155. meshBuilder.Position3fv( tmp.Base() );
  156. meshBuilder.TexCoord2f( 0, 1.0f, 0.0f );
  157. meshBuilder.Color4ub( color.r, color.g, color.b, color.a );
  158. meshBuilder.Normal3fv( cross.Base() );
  159. meshBuilder.AdvanceVertex();
  160. meshBuilder.End();
  161. pMesh->Draw();
  162. }
  163. void FX_DrawLineFade( const Vector &start, const Vector &end, float scale, IMaterial *pMaterial, const color32 &color, float fadeDist )
  164. {
  165. Vector lineDir, viewDir;
  166. //Get the proper orientation for the line
  167. VectorSubtract( end, start, lineDir );
  168. VectorSubtract( end, CurrentViewOrigin(), viewDir );
  169. float lineLength = lineDir.Length();
  170. float t0 = 0.25f;
  171. float t1 = 0.75f;
  172. if ( lineLength > 0 )
  173. {
  174. t0 = fadeDist / lineLength;
  175. t0 = clamp( t0, 0.0f, 0.25f );
  176. t1 = 1.0f - t0;
  177. }
  178. Vector cross = lineDir.Cross( viewDir );
  179. VectorNormalize( cross );
  180. CMatRenderContextPtr pRenderContext( materials );
  181. //Bind the material
  182. IMesh* pMesh = pRenderContext->GetDynamicMesh( true, NULL, NULL, pMaterial );
  183. CMeshBuilder meshBuilder;
  184. Vector tmp;
  185. meshBuilder.Begin( pMesh, MATERIAL_TRIANGLES, 8, 24 );
  186. // 2 5
  187. // 0 1 4 7
  188. // 3 6
  189. // 0,2,1 - 0,1,3 - 7,4,5 - 7,6,4 - 1,4,6, 1,6,3 - 1,5,4 - 1,2,5
  190. // v0
  191. meshBuilder.Position3fv( start.Base() );
  192. meshBuilder.TexCoord2f( 0, 0.5f, 0.0f );
  193. meshBuilder.Color4ub( 0, 0, 0, 0 );
  194. meshBuilder.Normal3fv( cross.Base() );
  195. meshBuilder.AdvanceVertex();
  196. // v1
  197. Vector v1 = start + t0 * lineDir;
  198. meshBuilder.Position3fv( v1.Base() );
  199. meshBuilder.TexCoord2f( 0, 0.5f, t0 );
  200. meshBuilder.Color4ub( color.r, color.g, color.b, color.a );
  201. meshBuilder.Normal3fv( cross.Base() );
  202. meshBuilder.AdvanceVertex();
  203. // v2
  204. tmp = v1 - scale*cross;
  205. meshBuilder.Position3fv( tmp.Base() );
  206. meshBuilder.TexCoord2f( 0, 1.0f, t0 );
  207. meshBuilder.Color4ub( color.r, color.g, color.b, color.a );
  208. meshBuilder.Normal3fv( cross.Base() );
  209. meshBuilder.AdvanceVertex();
  210. // v3
  211. tmp = v1 + scale*cross;
  212. meshBuilder.Position3fv( tmp.Base() );
  213. meshBuilder.TexCoord2f( 0, 0.0f, t0 );
  214. meshBuilder.Color4ub( color.r, color.g, color.b, color.a );
  215. meshBuilder.Normal3fv( cross.Base() );
  216. meshBuilder.AdvanceVertex();
  217. // v4
  218. Vector v4 = start + t1 * lineDir;
  219. meshBuilder.Position3fv( v4.Base() );
  220. meshBuilder.TexCoord2f( 0, 0.5f, t1 );
  221. meshBuilder.Color4ub( color.r, color.g, color.b, color.a );
  222. meshBuilder.Normal3fv( cross.Base() );
  223. meshBuilder.AdvanceVertex();
  224. // v5
  225. tmp = v4 - scale*cross;
  226. meshBuilder.Position3fv( tmp.Base() );
  227. meshBuilder.TexCoord2f( 0, 1.0f, t1 );
  228. meshBuilder.Color4ub( color.r, color.g, color.b, color.a );
  229. meshBuilder.Normal3fv( cross.Base() );
  230. meshBuilder.AdvanceVertex();
  231. // v6
  232. tmp = v4 + scale*cross;
  233. meshBuilder.Position3fv( tmp.Base() );
  234. meshBuilder.TexCoord2f( 0, 0.0f, t1 );
  235. meshBuilder.Color4ub( color.r, color.g, color.b, color.a );
  236. meshBuilder.Normal3fv( cross.Base() );
  237. meshBuilder.AdvanceVertex();
  238. // v7
  239. meshBuilder.Position3fv( end.Base() );
  240. meshBuilder.TexCoord2f( 0, 0.5f, 1.0f );
  241. meshBuilder.Color4ub( 0, 0, 0, 0 );
  242. meshBuilder.Normal3fv( cross.Base() );
  243. meshBuilder.AdvanceVertex();
  244. // triangles - 0,2,1 - 0,1,3 - 7,4,5 - 7,6,4 - 1,4,6, 1,6,3 - 1,5,4 - 1,2,5
  245. meshBuilder.FastIndex( 0 ); meshBuilder.FastIndex( 2 ); meshBuilder.FastIndex( 1 );
  246. meshBuilder.FastIndex( 0 ); meshBuilder.FastIndex( 1 ); meshBuilder.FastIndex( 3 );
  247. meshBuilder.FastIndex( 7 ); meshBuilder.FastIndex( 4 ); meshBuilder.FastIndex( 5 );
  248. meshBuilder.FastIndex( 7 ); meshBuilder.FastIndex( 6 ); meshBuilder.FastIndex( 4 );
  249. meshBuilder.FastIndex( 1 ); meshBuilder.FastIndex( 4 ); meshBuilder.FastIndex( 6 );
  250. meshBuilder.FastIndex( 1 ); meshBuilder.FastIndex( 6 ); meshBuilder.FastIndex( 3 );
  251. meshBuilder.FastIndex( 1 ); meshBuilder.FastIndex( 5 ); meshBuilder.FastIndex( 4 );
  252. meshBuilder.FastIndex( 1 ); meshBuilder.FastIndex( 2 ); meshBuilder.FastIndex( 5 );
  253. meshBuilder.End();
  254. pMesh->Draw();
  255. }