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.

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