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.

435 lines
14 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef PARTICLE_UTIL_H
  9. #define PARTICLE_UTIL_H
  10. #include "materialsystem/imesh.h"
  11. #include "particledraw.h"
  12. #include "particlemgr.h"
  13. #include "cdll_client_int.h"
  14. #include "timedevent.h"
  15. // Lerp between two floating point numbers.
  16. inline float FLerp(float minVal, float maxVal, float t)
  17. {
  18. return minVal + (maxVal - minVal) * t;
  19. }
  20. inline Vector VecLerp(const Vector &minVal, const Vector &maxVal, float t)
  21. {
  22. return minVal + (maxVal - minVal) * t;
  23. }
  24. // Get a random floating point number between the two specified numbers.
  25. inline float FRand(float minVal, float maxVal)
  26. {
  27. return RandomFloat( minVal, maxVal );
  28. }
  29. // Apply velocity and acceleration to position and acceleration to velocity.
  30. // If you're going to keep acceleration around, you should zero it out after calling this.
  31. inline void PhysicallySimulate(Vector &pos, Vector &velocity, const Vector &acceleration, const float fTimeDelta)
  32. {
  33. pos = pos + (velocity + (acceleration*fTimeDelta*0.5f)) * fTimeDelta;
  34. velocity = velocity + acceleration * fTimeDelta;
  35. }
  36. inline Vector GetGravityVector()
  37. {
  38. return Vector(0, 0, -150);
  39. }
  40. // Render a quad on the screen where you pass in color and size.
  41. // Color and alpha range is 0 to 254.9
  42. // You also get an extra texture coordinate to pass in.
  43. inline void RenderParticle_Color255SizeSpecularTCoord3(
  44. ParticleDraw* pDraw,
  45. const Vector &pos,
  46. const Vector &color,
  47. const float alpha,
  48. const float size,
  49. const unsigned char *specular,
  50. const float tCoord
  51. )
  52. {
  53. // Don't render totally transparent particles.
  54. if( alpha < 0.5f )
  55. return;
  56. CMeshBuilder *pBuilder = pDraw->GetMeshBuilder();
  57. if( !pBuilder )
  58. return;
  59. unsigned char ubColor[4];
  60. ubColor[0] = (unsigned char)RoundFloatToInt( color.x );
  61. ubColor[1] = (unsigned char)RoundFloatToInt( color.y );
  62. ubColor[2] = (unsigned char)RoundFloatToInt( color.z );
  63. ubColor[3] = (unsigned char)RoundFloatToInt( alpha );
  64. // Add the 4 corner vertices.
  65. pBuilder->Position3f( pos.x-size, pos.y-size, pos.z );
  66. pBuilder->Color4ubv( ubColor );
  67. pBuilder->TexCoord3f( 0, pDraw->m_pSubTexture->m_tCoordMins[0], pDraw->m_pSubTexture->m_tCoordMaxs[1], tCoord );
  68. pBuilder->Specular3ubv( specular );
  69. pBuilder->AdvanceVertex();
  70. pBuilder->Position3f( pos.x-size, pos.y+size, pos.z );
  71. pBuilder->Color4ubv( ubColor );
  72. pBuilder->TexCoord3f( 0, pDraw->m_pSubTexture->m_tCoordMins[0], pDraw->m_pSubTexture->m_tCoordMins[1], tCoord );
  73. pBuilder->Specular3ubv( specular );
  74. pBuilder->AdvanceVertex();
  75. pBuilder->Position3f( pos.x+size, pos.y+size, pos.z );
  76. pBuilder->Color4ubv( ubColor );
  77. pBuilder->TexCoord3f( 0, pDraw->m_pSubTexture->m_tCoordMaxs[0], pDraw->m_pSubTexture->m_tCoordMins[1], tCoord );
  78. pBuilder->Specular3ubv( specular );
  79. pBuilder->AdvanceVertex();
  80. pBuilder->Position3f( pos.x+size, pos.y-size, pos.z );
  81. pBuilder->Color4ubv( ubColor );
  82. pBuilder->TexCoord3f( 0, pDraw->m_pSubTexture->m_tCoordMaxs[0], pDraw->m_pSubTexture->m_tCoordMaxs[1], tCoord );
  83. pBuilder->Specular3ubv( specular );
  84. pBuilder->AdvanceVertex();
  85. }
  86. // Render a quad on the screen where you pass in color and size.
  87. // Color and alpha range is 0 to 254.9
  88. inline void RenderParticle_Color255Size(
  89. ParticleDraw* pDraw,
  90. const Vector &pos,
  91. const Vector &color,
  92. const float alpha,
  93. const float size)
  94. {
  95. // Don't render totally transparent particles.
  96. if( alpha < 0.5f )
  97. return;
  98. CMeshBuilder *pBuilder = pDraw->GetMeshBuilder();
  99. if( !pBuilder )
  100. return;
  101. unsigned char ubColor[4];
  102. ubColor[0] = (unsigned char)RoundFloatToInt( color.x );
  103. ubColor[1] = (unsigned char)RoundFloatToInt( color.y );
  104. ubColor[2] = (unsigned char)RoundFloatToInt( color.z );
  105. ubColor[3] = (unsigned char)RoundFloatToInt( alpha );
  106. // Add the 4 corner vertices.
  107. pBuilder->Position3f( pos.x-size, pos.y-size, pos.z );
  108. pBuilder->Color4ubv( ubColor );
  109. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMins[0], pDraw->m_pSubTexture->m_tCoordMaxs[1] );
  110. pBuilder->AdvanceVertex();
  111. pBuilder->Position3f( pos.x-size, pos.y+size, pos.z );
  112. pBuilder->Color4ubv( ubColor );
  113. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMins[0], pDraw->m_pSubTexture->m_tCoordMins[1] );
  114. pBuilder->AdvanceVertex();
  115. pBuilder->Position3f( pos.x+size, pos.y+size, pos.z );
  116. pBuilder->Color4ubv( ubColor );
  117. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMaxs[0], pDraw->m_pSubTexture->m_tCoordMins[1] );
  118. pBuilder->AdvanceVertex();
  119. pBuilder->Position3f( pos.x+size, pos.y-size, pos.z );
  120. pBuilder->Color4ubv( ubColor );
  121. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMaxs[0], pDraw->m_pSubTexture->m_tCoordMaxs[1] );
  122. pBuilder->AdvanceVertex();
  123. }
  124. // Render a quad on the screen where you pass in color and size.
  125. // Color and alpha range is 0 to 254.9
  126. inline void RenderParticle_Color255SizeNormal(
  127. ParticleDraw* pDraw,
  128. const Vector &pos,
  129. const Vector &color,
  130. const float alpha,
  131. const float size,
  132. const Vector &vNormal )
  133. {
  134. // Don't render totally transparent particles.
  135. if( alpha < 0.5f )
  136. return;
  137. CMeshBuilder *pBuilder = pDraw->GetMeshBuilder();
  138. if( !pBuilder )
  139. return;
  140. unsigned char ubColor[4];
  141. ubColor[0] = (unsigned char)RoundFloatToInt( color.x );
  142. ubColor[1] = (unsigned char)RoundFloatToInt( color.y );
  143. ubColor[2] = (unsigned char)RoundFloatToInt( color.z );
  144. ubColor[3] = (unsigned char)RoundFloatToInt( alpha );
  145. // Add the 4 corner vertices.
  146. pBuilder->Position3f( pos.x-size, pos.y-size, pos.z );
  147. pBuilder->Color4ubv( ubColor );
  148. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMins[0], pDraw->m_pSubTexture->m_tCoordMaxs[1] );
  149. pBuilder->Normal3fv( (float*)&vNormal );
  150. pBuilder->AdvanceVertex();
  151. pBuilder->Position3f( pos.x-size, pos.y+size, pos.z );
  152. pBuilder->Color4ubv( ubColor );
  153. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMins[0], pDraw->m_pSubTexture->m_tCoordMins[1] );
  154. pBuilder->Normal3fv( (float*)&vNormal );
  155. pBuilder->AdvanceVertex();
  156. pBuilder->Position3f( pos.x+size, pos.y+size, pos.z );
  157. pBuilder->Color4ubv( ubColor );
  158. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMaxs[0], pDraw->m_pSubTexture->m_tCoordMins[1] );
  159. pBuilder->Normal3fv( (float*)&vNormal );
  160. pBuilder->AdvanceVertex();
  161. pBuilder->Position3f( pos.x+size, pos.y-size, pos.z );
  162. pBuilder->Color4ubv( ubColor );
  163. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMaxs[0], pDraw->m_pSubTexture->m_tCoordMaxs[1] );
  164. pBuilder->Normal3fv( (float*)&vNormal );
  165. pBuilder->AdvanceVertex();
  166. }
  167. // Render a quad on the screen where you pass in color and size.
  168. // Color and alpha range is 0 to 254.9
  169. // Angle is in radians.
  170. inline void RenderParticle_Color255SizeNormalAngle(
  171. ParticleDraw* pDraw,
  172. const Vector &pos,
  173. const Vector &color,
  174. const float alpha,
  175. const float size,
  176. const Vector &vNormal,
  177. const float angle )
  178. {
  179. // Don't render totally transparent particles.
  180. if( alpha < 0.5f )
  181. return;
  182. CMeshBuilder *pBuilder = pDraw->GetMeshBuilder();
  183. if( !pBuilder )
  184. return;
  185. unsigned char ubColor[4];
  186. ubColor[0] = (unsigned char)RoundFloatToInt( color.x );
  187. ubColor[1] = (unsigned char)RoundFloatToInt( color.y );
  188. ubColor[2] = (unsigned char)RoundFloatToInt( color.z );
  189. ubColor[3] = (unsigned char)RoundFloatToInt( alpha );
  190. float ca = (float)cos(angle);
  191. float sa = (float)sin(angle);
  192. // Add the 4 corner vertices.
  193. pBuilder->Position3f( pos.x + (-ca + sa) * size, pos.y + (-sa - ca) * size, pos.z );
  194. pBuilder->Color4ubv( ubColor );
  195. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMins[0], pDraw->m_pSubTexture->m_tCoordMaxs[1] );
  196. pBuilder->Normal3fv( (float*)&vNormal );
  197. pBuilder->AdvanceVertex();
  198. pBuilder->Position3f( pos.x + (-ca - sa) * size, pos.y + (-sa + ca) * size, pos.z );
  199. pBuilder->Color4ubv( ubColor );
  200. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMins[0], pDraw->m_pSubTexture->m_tCoordMins[1] );
  201. pBuilder->Normal3fv( (float*)&vNormal );
  202. pBuilder->AdvanceVertex();
  203. pBuilder->Position3f( pos.x + (ca - sa) * size, pos.y + (sa + ca) * size, pos.z );
  204. pBuilder->Color4ubv( ubColor );
  205. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMaxs[0], pDraw->m_pSubTexture->m_tCoordMins[1] );
  206. pBuilder->Normal3fv( (float*)&vNormal );
  207. pBuilder->AdvanceVertex();
  208. pBuilder->Position3f( pos.x + (ca + sa) * size, pos.y + (sa - ca) * size, pos.z );
  209. pBuilder->Color4ubv( ubColor );
  210. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMaxs[0], pDraw->m_pSubTexture->m_tCoordMaxs[1] );
  211. pBuilder->Normal3fv( (float*)&vNormal );
  212. pBuilder->AdvanceVertex();
  213. }
  214. // Render a quad on the screen where you pass in color and size.
  215. inline void RenderParticle_ColorSize(
  216. ParticleDraw* pDraw,
  217. const Vector &pos,
  218. const Vector &color,
  219. const float alpha,
  220. const float size
  221. )
  222. {
  223. // Don't render totally transparent particles.
  224. if( alpha < 0.001f )
  225. return;
  226. CMeshBuilder *pBuilder = pDraw->GetMeshBuilder();
  227. if( !pBuilder )
  228. return;
  229. unsigned char ubColor[4];
  230. ubColor[0] = (unsigned char)RoundFloatToInt( color.x * 254.9f );
  231. ubColor[1] = (unsigned char)RoundFloatToInt( color.y * 254.9f );
  232. ubColor[2] = (unsigned char)RoundFloatToInt( color.z * 254.9f );
  233. ubColor[3] = (unsigned char)RoundFloatToInt( alpha * 254.9f );
  234. // Add the 4 corner vertices.
  235. pBuilder->Position3f( pos.x-size, pos.y-size, pos.z );
  236. pBuilder->Color4ubv( ubColor );
  237. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMins[0], pDraw->m_pSubTexture->m_tCoordMaxs[1] );
  238. pBuilder->AdvanceVertex();
  239. pBuilder->Position3f( pos.x-size, pos.y+size, pos.z );
  240. pBuilder->Color4ubv( ubColor );
  241. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMins[0], pDraw->m_pSubTexture->m_tCoordMins[1] );
  242. pBuilder->AdvanceVertex();
  243. pBuilder->Position3f( pos.x+size, pos.y+size, pos.z );
  244. pBuilder->Color4ubv( ubColor );
  245. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMaxs[0], pDraw->m_pSubTexture->m_tCoordMins[1] );
  246. pBuilder->AdvanceVertex();
  247. pBuilder->Position3f( pos.x+size, pos.y-size, pos.z );
  248. pBuilder->Color4ubv( ubColor );
  249. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMaxs[0], pDraw->m_pSubTexture->m_tCoordMaxs[1] );
  250. pBuilder->AdvanceVertex();
  251. }
  252. inline void RenderParticle_ColorSizeAngle(
  253. ParticleDraw* pDraw,
  254. const Vector &pos,
  255. const Vector &color,
  256. const float alpha,
  257. const float size,
  258. const float angle)
  259. {
  260. // Don't render totally transparent particles.
  261. if(alpha < 0.001f)
  262. return;
  263. CMeshBuilder *pBuilder = pDraw->GetMeshBuilder();
  264. if( !pBuilder )
  265. return;
  266. unsigned char ubColor[4];
  267. ubColor[0] = (unsigned char)RoundFloatToInt( color.x * 254.9f );
  268. ubColor[1] = (unsigned char)RoundFloatToInt( color.y * 254.9f );
  269. ubColor[2] = (unsigned char)RoundFloatToInt( color.z * 254.9f );
  270. ubColor[3] = (unsigned char)RoundFloatToInt( alpha * 254.9f );
  271. float sa, ca;
  272. SinCos(angle, &sa, &ca );
  273. pBuilder->Position3f( pos.x + (-ca + sa) * size, pos.y + (-sa - ca) * size, pos.z );
  274. pBuilder->Color4ubv( ubColor );
  275. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMins[0], pDraw->m_pSubTexture->m_tCoordMaxs[1] );
  276. pBuilder->AdvanceVertex();
  277. pBuilder->Position3f( pos.x + (-ca - sa) * size, pos.y + (-sa + ca) * size, pos.z );
  278. pBuilder->Color4ubv( ubColor );
  279. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMins[0], pDraw->m_pSubTexture->m_tCoordMins[1] );
  280. pBuilder->AdvanceVertex();
  281. pBuilder->Position3f( pos.x + (ca - sa) * size, pos.y + (sa + ca) * size, pos.z );
  282. pBuilder->Color4ubv( ubColor );
  283. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMaxs[0], pDraw->m_pSubTexture->m_tCoordMins[1] );
  284. pBuilder->AdvanceVertex();
  285. pBuilder->Position3f( pos.x + (ca + sa) * size, pos.y + (sa - ca) * size, pos.z );
  286. pBuilder->Color4ubv( ubColor );
  287. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMaxs[0], pDraw->m_pSubTexture->m_tCoordMaxs[1] );
  288. pBuilder->AdvanceVertex();
  289. }
  290. inline void RenderParticle_ColorSizeAngles(
  291. ParticleDraw* pDraw,
  292. const Vector &pos,
  293. const Vector &color,
  294. const float alpha,
  295. const float size,
  296. const QAngle &angles)
  297. {
  298. // Don't render totally transparent particles.
  299. if(alpha < 0.001f)
  300. return;
  301. CMeshBuilder *pBuilder = pDraw->GetMeshBuilder();
  302. if( !pBuilder )
  303. return;
  304. unsigned char ubColor[4];
  305. ubColor[0] = (unsigned char)RoundFloatToInt( color.x * 254.9f );
  306. ubColor[1] = (unsigned char)RoundFloatToInt( color.y * 254.9f );
  307. ubColor[2] = (unsigned char)RoundFloatToInt( color.z * 254.9f );
  308. ubColor[3] = (unsigned char)RoundFloatToInt( alpha * 254.9f );
  309. Vector vNorm,vWidth,vHeight;
  310. AngleVectors(angles,&vNorm,&vWidth,&vHeight);
  311. Vector vVertex = pos;
  312. pBuilder->Position3f( vVertex.x , vVertex.y , vVertex.z );
  313. pBuilder->Color4ubv( ubColor );
  314. pBuilder->Normal3f( VectorExpand(vNorm) );
  315. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMins[0], pDraw->m_pSubTexture->m_tCoordMaxs[1] );
  316. pBuilder->AdvanceVertex();
  317. vVertex = vVertex + vWidth*size;
  318. pBuilder->Position3f( vVertex.x, vVertex.y, vVertex.z );
  319. pBuilder->Color4ubv( ubColor );
  320. pBuilder->Normal3f( VectorExpand(vNorm) );
  321. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMins[0], pDraw->m_pSubTexture->m_tCoordMins[1] );
  322. pBuilder->AdvanceVertex();
  323. vVertex = vVertex + vHeight*size;
  324. pBuilder->Position3f( vVertex.x, vVertex.y , vVertex.z );
  325. pBuilder->Color4ubv( ubColor );
  326. pBuilder->Normal3f( VectorExpand(vNorm) );
  327. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMaxs[0], pDraw->m_pSubTexture->m_tCoordMins[1] );
  328. pBuilder->AdvanceVertex();
  329. vVertex = vVertex - vWidth*size;
  330. pBuilder->Position3f( vVertex.x, vVertex.y, vVertex.z );
  331. pBuilder->Color4ubv( ubColor );
  332. pBuilder->Normal3f( VectorExpand(vNorm) );
  333. pBuilder->TexCoord2f( 0, pDraw->m_pSubTexture->m_tCoordMaxs[0], pDraw->m_pSubTexture->m_tCoordMaxs[1] );
  334. pBuilder->AdvanceVertex();
  335. }
  336. inline float GetAlphaDistanceFade(
  337. const Vector &pos,
  338. const float fadeNearDist,
  339. const float fadeFarDist)
  340. {
  341. if(-pos.z > fadeFarDist)
  342. {
  343. return 1;
  344. }
  345. else if(-pos.z > fadeNearDist)
  346. {
  347. return (-pos.z - fadeNearDist) / (fadeFarDist - fadeNearDist);
  348. }
  349. else
  350. {
  351. return 0;
  352. }
  353. }
  354. inline Vector WorldGetLightForPoint(const Vector &vPos, bool bClamp)
  355. {
  356. #if defined(PARTICLEPROTOTYPE_APP)
  357. return Vector(1,1,1);
  358. #else
  359. return engine->GetLightForPoint(vPos, bClamp);
  360. #endif
  361. }
  362. #endif