particle_system.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <bgfx/bgfx.h>
  6. #include <bgfx/embedded_shader.h>
  7. #include "particle_system.h"
  8. #include "../bgfx_utils.h"
  9. #include <bx/easing.h>
  10. #include <bx/crtimpl.h>
  11. #include <bx/handlealloc.h>
  12. #include "vs_particle.bin.h"
  13. #include "fs_particle.bin.h"
  14. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  15. {
  16. BGFX_EMBEDDED_SHADER(vs_particle),
  17. BGFX_EMBEDDED_SHADER(fs_particle),
  18. BGFX_EMBEDDED_SHADER_END()
  19. };
  20. static const bx::EaseFn s_easeFunc[] =
  21. {
  22. bx::easeLinear,
  23. bx::easeInQuad,
  24. bx::easeOutQuad,
  25. bx::easeInOutQuad,
  26. bx::easeOutInQuad,
  27. bx::easeInCubic,
  28. bx::easeOutCubic,
  29. bx::easeInOutCubic,
  30. bx::easeOutInCubic,
  31. bx::easeInQuart,
  32. bx::easeOutQuart,
  33. bx::easeInOutQuart,
  34. bx::easeOutInQuart,
  35. bx::easeInQuint,
  36. bx::easeOutQuint,
  37. bx::easeInOutQuint,
  38. bx::easeOutInQuint,
  39. bx::easeInSine,
  40. bx::easeOutSine,
  41. bx::easeInOutSine,
  42. bx::easeOutInSine,
  43. bx::easeInExpo,
  44. bx::easeOutExpo,
  45. bx::easeInOutExpo,
  46. bx::easeOutInExpo,
  47. bx::easeInCirc,
  48. bx::easeOutCirc,
  49. bx::easeInOutCirc,
  50. bx::easeOutInCirc,
  51. bx::easeInElastic,
  52. bx::easeOutElastic,
  53. bx::easeInOutElastic,
  54. bx::easeOutInElastic,
  55. bx::easeInBack,
  56. bx::easeOutBack,
  57. bx::easeInOutBack,
  58. bx::easeOutInBack,
  59. bx::easeInBounce,
  60. bx::easeOutBounce,
  61. bx::easeInOutBounce,
  62. bx::easeOutInBounce,
  63. };
  64. BX_STATIC_ASSERT(BX_COUNTOF(s_easeFunc) == bx::Easing::Count);
  65. struct PosColorTexCoord0Vertex
  66. {
  67. float m_x;
  68. float m_y;
  69. float m_z;
  70. uint32_t m_abgr;
  71. float m_u;
  72. float m_v;
  73. float m_blend;
  74. float m_angle;
  75. static void init()
  76. {
  77. ms_decl
  78. .begin()
  79. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  80. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  81. .add(bgfx::Attrib::TexCoord0, 4, bgfx::AttribType::Float)
  82. .end();
  83. }
  84. static bgfx::VertexDecl ms_decl;
  85. };
  86. bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl;
  87. void EmitterUniforms::reset()
  88. {
  89. m_position[0] = 0.0f;
  90. m_position[1] = 0.0f;
  91. m_position[2] = 0.0f;
  92. m_angle[0] = 0.0f;
  93. m_angle[1] = 0.0f;
  94. m_angle[2] = 0.0f;
  95. m_particlesPerSecond = 0;
  96. m_offsetStart[0] = 0.0f;
  97. m_offsetStart[1] = 1.0f;
  98. m_offsetEnd[0] = 2.0f;
  99. m_offsetEnd[1] = 3.0f;
  100. m_rgba[0] = 0x00ffffff;
  101. m_rgba[1] = UINT32_MAX;
  102. m_rgba[2] = UINT32_MAX;
  103. m_rgba[3] = UINT32_MAX;
  104. m_rgba[4] = 0x00ffffff;
  105. m_blendStart[0] = 0.8f;
  106. m_blendStart[1] = 1.0f;
  107. m_blendEnd[0] = 0.0f;
  108. m_blendEnd[1] = 0.2f;
  109. m_scaleStart[0] = 0.1f;
  110. m_scaleStart[1] = 0.2f;
  111. m_scaleEnd[0] = 0.3f;
  112. m_scaleEnd[1] = 0.4f;
  113. m_lifeSpan[0] = 1.0f;
  114. m_lifeSpan[1] = 2.0f;
  115. m_gravityScale = 0.0f;
  116. m_easePos = bx::Easing::Linear;
  117. m_easeRgba = bx::Easing::Linear;
  118. m_easeBlend = bx::Easing::Linear;
  119. m_easeScale = bx::Easing::Linear;
  120. }
  121. namespace ps
  122. {
  123. struct Particle
  124. {
  125. float start[3];
  126. float end[2][3];
  127. float blendStart;
  128. float blendEnd;
  129. float scaleStart;
  130. float scaleEnd;
  131. uint32_t rgba[5];
  132. float life;
  133. float lifeSpan;
  134. };
  135. struct ParticleSort
  136. {
  137. float dist;
  138. uint32_t idx;
  139. };
  140. inline uint32_t toAbgr(const float* _rgba)
  141. {
  142. return 0
  143. | (uint8_t(_rgba[0]*255.0f)<< 0)
  144. | (uint8_t(_rgba[1]*255.0f)<< 8)
  145. | (uint8_t(_rgba[2]*255.0f)<<16)
  146. | (uint8_t(_rgba[3]*255.0f)<<24)
  147. ;
  148. }
  149. inline uint32_t toAbgr(float _rr, float _gg, float _bb, float _aa)
  150. {
  151. return 0
  152. | (uint8_t(_rr*255.0f)<< 0)
  153. | (uint8_t(_gg*255.0f)<< 8)
  154. | (uint8_t(_bb*255.0f)<<16)
  155. | (uint8_t(_aa*255.0f)<<24)
  156. ;
  157. }
  158. struct Emitter
  159. {
  160. void create(EmitterShape::Enum _shape, EmitterDirection::Enum _direction, uint32_t _maxParticles);
  161. void destroy();
  162. void reset()
  163. {
  164. m_num = 0;
  165. }
  166. void update(float _dt)
  167. {
  168. uint32_t num = m_num;
  169. for (uint32_t ii = 0; ii < num; ++ii)
  170. {
  171. Particle& particle = m_particles[ii];
  172. particle.life += _dt * 1.0f/particle.lifeSpan;
  173. if (particle.life > 1.0f)
  174. {
  175. if (ii != num-1)
  176. {
  177. memcpy(&particle, &m_particles[num-1], sizeof(Particle) );
  178. --ii;
  179. }
  180. --num;
  181. }
  182. }
  183. m_num = num;
  184. if (0 < m_uniforms.m_particlesPerSecond)
  185. {
  186. spawn(_dt);
  187. }
  188. }
  189. void spawn(float _dt)
  190. {
  191. float mtx[16];
  192. bx::mtxSRT(mtx
  193. , 1.0f, 1.0f, 1.0f
  194. , m_uniforms.m_angle[0], m_uniforms.m_angle[1], m_uniforms.m_angle[2]
  195. , m_uniforms.m_position[0], m_uniforms.m_position[1], m_uniforms.m_position[2]
  196. );
  197. const float timePerParticle = 1.0f/m_uniforms.m_particlesPerSecond;
  198. m_dt += _dt;
  199. const uint32_t numParticles = uint32_t(m_dt / timePerParticle);
  200. m_dt -= numParticles * timePerParticle;
  201. float time = 0.0f;
  202. for (uint32_t ii = 0
  203. ; ii < numParticles && m_num < m_max
  204. ; ++ii
  205. )
  206. {
  207. Particle& particle = m_particles[m_num];
  208. m_num++;
  209. const float up[3] = { 0.0f, 1.0f, 0.0f };
  210. float pos[3];
  211. switch (m_shape)
  212. {
  213. default:
  214. case EmitterShape::Sphere:
  215. bx::randUnitSphere(pos, &m_rng);
  216. break;
  217. case EmitterShape::Hemisphere:
  218. bx::randUnitHemisphere(pos, &m_rng, up);
  219. break;
  220. case EmitterShape::Circle:
  221. bx::randUnitCircle(pos, &m_rng);
  222. break;
  223. case EmitterShape::Disc:
  224. {
  225. float tmp[3];
  226. bx::randUnitCircle(tmp, &m_rng);
  227. bx::vec3Mul(pos, tmp, bx::frnd(&m_rng) );
  228. }
  229. break;
  230. case EmitterShape::Rect:
  231. pos[0] = bx::frndh(&m_rng);
  232. pos[1] = 0.0f;
  233. pos[2] = bx::frndh(&m_rng);
  234. break;
  235. }
  236. float dir[3];
  237. switch (m_direction)
  238. {
  239. default:
  240. case EmitterDirection::Up:
  241. bx::vec3Move(dir, up);
  242. break;
  243. case EmitterDirection::Outward:
  244. bx::vec3Norm(dir, pos);
  245. break;
  246. }
  247. float start[3];
  248. float end[3];
  249. const float startOffset = bx::flerp(m_uniforms.m_offsetStart[0], m_uniforms.m_offsetStart[1], bx::frnd(&m_rng) );
  250. bx::vec3Mul(start, pos, startOffset);
  251. const float endOffset = bx::flerp(m_uniforms.m_offsetEnd[0], m_uniforms.m_offsetEnd[1], bx::frnd(&m_rng) );
  252. float tmp1[3];
  253. bx::vec3Mul(tmp1, dir, endOffset);
  254. bx::vec3Add(end, tmp1, start);
  255. particle.life = time;
  256. particle.lifeSpan = bx::flerp(m_uniforms.m_lifeSpan[0], m_uniforms.m_lifeSpan[1], bx::frnd(&m_rng) );
  257. float gravity[3] = { 0.0f, -9.81f * m_uniforms.m_gravityScale * bx::fsq(particle.lifeSpan), 0.0f };
  258. bx::vec3MulMtx(particle.start, start, mtx);
  259. bx::vec3MulMtx(particle.end[0], end, mtx);
  260. bx::vec3Add(particle.end[1], particle.end[0], gravity);
  261. memcpy(particle.rgba, m_uniforms.m_rgba, BX_COUNTOF(m_uniforms.m_rgba)*sizeof(uint32_t) );
  262. particle.blendStart = bx::flerp(m_uniforms.m_blendStart[0], m_uniforms.m_blendStart[1], bx::frnd(&m_rng) );
  263. particle.blendEnd = bx::flerp(m_uniforms.m_blendEnd[0], m_uniforms.m_blendEnd[1], bx::frnd(&m_rng) );
  264. particle.scaleStart = bx::flerp(m_uniforms.m_scaleStart[0], m_uniforms.m_scaleStart[1], bx::frnd(&m_rng) );
  265. particle.scaleEnd = bx::flerp(m_uniforms.m_scaleEnd[0], m_uniforms.m_scaleEnd[1], bx::frnd(&m_rng) );
  266. time += timePerParticle;
  267. }
  268. }
  269. uint32_t render(const float* _mtxView, const float* _eye, uint32_t _first, uint32_t _max, ParticleSort* _outSort, PosColorTexCoord0Vertex* _outVertices) const
  270. {
  271. bx::EaseFn easeRgba = s_easeFunc[m_uniforms.m_easeRgba];
  272. bx::EaseFn easePos = s_easeFunc[m_uniforms.m_easePos];
  273. bx::EaseFn easeBlend = s_easeFunc[m_uniforms.m_easeBlend];
  274. bx::EaseFn easeScale = s_easeFunc[m_uniforms.m_easeScale];
  275. for (uint32_t jj = 0, num = m_num, current = _first
  276. ; jj < num && current < _max
  277. ; ++jj, ++current
  278. )
  279. {
  280. const Particle& particle = m_particles[jj];
  281. const float ttPos = easePos(particle.life);
  282. const float ttScale = easeScale(particle.life);
  283. const float ttBlend = bx::fsaturate(easeBlend(particle.life) );
  284. const float ttRgba = bx::fsaturate(easeRgba(particle.life) );
  285. float p0[3];
  286. bx::vec3Lerp(p0, particle.start, particle.end[0], ttPos);
  287. float p1[3];
  288. bx::vec3Lerp(p1, particle.end[0], particle.end[1], ttPos);
  289. float pos[3];
  290. bx::vec3Lerp(pos, p0, p1, ttPos);
  291. ParticleSort& sort = _outSort[current];
  292. float tmp[3];
  293. bx::vec3Sub(tmp, _eye, pos);
  294. sort.dist = bx::fsqrt(bx::vec3Dot(tmp, tmp) );
  295. sort.idx = current;
  296. uint32_t idx = uint32_t(ttRgba*4);
  297. float ttmod = bx::fmod(ttRgba, 0.25f)/0.25f;
  298. uint32_t rgbaStart = particle.rgba[idx];
  299. uint32_t rgbaEnd = particle.rgba[idx+1];
  300. float rr = bx::flerp( ( (uint8_t*)&rgbaStart)[0], ( (uint8_t*)&rgbaEnd)[0], ttmod)/255.0f;
  301. float gg = bx::flerp( ( (uint8_t*)&rgbaStart)[1], ( (uint8_t*)&rgbaEnd)[1], ttmod)/255.0f;
  302. float bb = bx::flerp( ( (uint8_t*)&rgbaStart)[2], ( (uint8_t*)&rgbaEnd)[2], ttmod)/255.0f;
  303. float aa = bx::flerp( ( (uint8_t*)&rgbaStart)[3], ( (uint8_t*)&rgbaEnd)[3], ttmod)/255.0f;
  304. float blend = bx::flerp(particle.blendStart, particle.blendEnd, ttBlend);
  305. float scale = bx::flerp(particle.scaleStart, particle.scaleEnd, ttScale);
  306. uint32_t abgr = toAbgr(rr, gg, bb, aa);
  307. float udir[3] = { _mtxView[0]*scale, _mtxView[4]*scale, _mtxView[8]*scale };
  308. float vdir[3] = { _mtxView[1]*scale, _mtxView[5]*scale, _mtxView[9]*scale };
  309. PosColorTexCoord0Vertex* vertex = &_outVertices[current*4];
  310. bx::vec3Sub(tmp, pos, udir);
  311. bx::vec3Sub(&vertex->m_x, tmp, vdir);
  312. vertex->m_abgr = abgr;
  313. vertex->m_u = 0.0f;
  314. vertex->m_v = 0.0f;
  315. vertex->m_blend = blend;
  316. ++vertex;
  317. bx::vec3Add(tmp, pos, udir);
  318. bx::vec3Sub(&vertex->m_x, tmp, vdir);
  319. vertex->m_abgr = abgr;
  320. vertex->m_u = 1.0f;
  321. vertex->m_v = 0.0f;
  322. vertex->m_blend = blend;
  323. ++vertex;
  324. bx::vec3Add(tmp, pos, udir);
  325. bx::vec3Add(&vertex->m_x, tmp, vdir);
  326. vertex->m_abgr = abgr;
  327. vertex->m_u = 1.0f;
  328. vertex->m_v = 1.0f;
  329. vertex->m_blend = blend;
  330. ++vertex;
  331. bx::vec3Sub(tmp, pos, udir);
  332. bx::vec3Add(&vertex->m_x, tmp, vdir);
  333. vertex->m_abgr = abgr;
  334. vertex->m_u = 0.0f;
  335. vertex->m_v = 1.0f;
  336. vertex->m_blend = blend;
  337. ++vertex;
  338. }
  339. return m_num;
  340. }
  341. EmitterShape::Enum m_shape;
  342. EmitterDirection::Enum m_direction;
  343. float m_dt;
  344. bx::RngMwc m_rng;
  345. EmitterUniforms m_uniforms;
  346. Particle* m_particles;
  347. uint32_t m_num;
  348. uint32_t m_max;
  349. };
  350. static int32_t particleSortFn(const void* _lhs, const void* _rhs)
  351. {
  352. const ParticleSort& lhs = *(const ParticleSort*)_lhs;
  353. const ParticleSort& rhs = *(const ParticleSort*)_rhs;
  354. return lhs.dist > rhs.dist ? -1 : 1;
  355. }
  356. struct ParticleSystem
  357. {
  358. void init(uint16_t _maxEmitters, bx::AllocatorI* _allocator)
  359. {
  360. m_allocator = _allocator;
  361. #if BX_CONFIG_ALLOCATOR_CRT
  362. if (NULL == _allocator)
  363. {
  364. static bx::CrtAllocator allocator;
  365. m_allocator = &allocator;
  366. }
  367. #endif // BX_CONFIG_ALLOCATOR_CRT
  368. m_emitterAlloc = bx::createHandleAlloc(m_allocator, _maxEmitters);
  369. m_emitter = (Emitter*)BX_ALLOC(m_allocator, sizeof(Emitter)*_maxEmitters);
  370. PosColorTexCoord0Vertex::init();
  371. m_num = 0;
  372. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  373. m_particleTexture = loadTexture("textures/particle.ktx");
  374. bgfx::RendererType::Enum type = bgfx::getRendererType();
  375. m_particleProgram = bgfx::createProgram(
  376. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_particle")
  377. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_particle")
  378. , true
  379. );
  380. }
  381. void shutdown()
  382. {
  383. bgfx::destroyProgram(m_particleProgram);
  384. bgfx::destroyTexture(m_particleTexture);
  385. bgfx::destroyUniform(s_texColor);
  386. bx::destroyHandleAlloc(m_allocator, m_emitterAlloc);
  387. BX_FREE(m_allocator, m_emitter);
  388. m_allocator = NULL;
  389. }
  390. void update(float _dt)
  391. {
  392. uint32_t numParticles = 0;
  393. for (uint16_t ii = 0, num = m_emitterAlloc->getNumHandles(); ii < num; ++ii)
  394. {
  395. const uint16_t idx = m_emitterAlloc->getHandleAt(ii);
  396. Emitter& emitter = m_emitter[idx];
  397. emitter.update(_dt);
  398. numParticles += emitter.m_num;
  399. }
  400. m_num = numParticles;
  401. }
  402. void render(uint8_t _view, const float* _mtxView, const float* _eye)
  403. {
  404. if (0 != m_num)
  405. {
  406. bgfx::TransientVertexBuffer tvb;
  407. bgfx::TransientIndexBuffer tib;
  408. const uint32_t numVertices = bgfx::getAvailTransientVertexBuffer(m_num*4, PosColorTexCoord0Vertex::ms_decl);
  409. const uint32_t numIndices = bgfx::getAvailTransientIndexBuffer(m_num*6);
  410. const uint32_t max = bx::uint32_min(numVertices/4, numIndices/6);
  411. BX_WARN(m_num == max
  412. , "Truncating transient buffer for particles to maximum available (requested %d, available %d)."
  413. , m_num
  414. , max
  415. );
  416. if (0 < max)
  417. {
  418. bgfx::allocTransientBuffers(&tvb
  419. , PosColorTexCoord0Vertex::ms_decl
  420. , max*4
  421. , &tib
  422. , max*6
  423. );
  424. PosColorTexCoord0Vertex* vertices = (PosColorTexCoord0Vertex*)tvb.data;
  425. ParticleSort* particleSort = (ParticleSort*)BX_ALLOC(m_allocator, max*sizeof(ParticleSort) );
  426. uint32_t pos = 0;
  427. for (uint16_t ii = 0, numEmitters = m_emitterAlloc->getNumHandles(); ii < numEmitters; ++ii)
  428. {
  429. const uint16_t idx = m_emitterAlloc->getHandleAt(ii);
  430. const Emitter& emitter = m_emitter[idx];
  431. pos += emitter.render(_mtxView, _eye, pos, max, particleSort, vertices);
  432. }
  433. qsort(particleSort
  434. , max
  435. , sizeof(ParticleSort)
  436. , particleSortFn
  437. );
  438. uint16_t* indices = (uint16_t*)tib.data;
  439. for (uint32_t ii = 0; ii < max; ++ii)
  440. {
  441. const ParticleSort& sort = particleSort[ii];
  442. uint16_t* index = &indices[ii*6];
  443. uint16_t idx = (uint16_t)sort.idx;
  444. index[0] = idx*4+0;
  445. index[1] = idx*4+1;
  446. index[2] = idx*4+2;
  447. index[3] = idx*4+2;
  448. index[4] = idx*4+3;
  449. index[5] = idx*4+0;
  450. }
  451. BX_FREE(m_allocator, particleSort);
  452. bgfx::setState(0
  453. | BGFX_STATE_RGB_WRITE
  454. | BGFX_STATE_ALPHA_WRITE
  455. | BGFX_STATE_DEPTH_TEST_LESS
  456. | BGFX_STATE_CULL_CW
  457. | BGFX_STATE_BLEND_NORMAL
  458. );
  459. bgfx::setVertexBuffer(&tvb);
  460. bgfx::setIndexBuffer(&tib);
  461. bgfx::setTexture(0, s_texColor, m_particleTexture);
  462. bgfx::submit(_view, m_particleProgram);
  463. }
  464. }
  465. }
  466. EmitterHandle createEmitter(EmitterShape::Enum _shape, EmitterDirection::Enum _direction, uint32_t _maxParticles)
  467. {
  468. EmitterHandle handle = { m_emitterAlloc->alloc() };
  469. if (UINT16_MAX != handle.idx)
  470. {
  471. m_emitter[handle.idx].create(_shape, _direction, _maxParticles);
  472. }
  473. return handle;
  474. }
  475. void updateEmitter(EmitterHandle _handle, const EmitterUniforms* _uniforms)
  476. {
  477. BX_CHECK(m_emitterAlloc.isValid(_handle.idx)
  478. , "destroyEmitter handle %d is not valid."
  479. , _handle.idx
  480. );
  481. Emitter& emitter = m_emitter[_handle.idx];
  482. if (NULL == _uniforms)
  483. {
  484. emitter.reset();
  485. }
  486. else
  487. {
  488. memcpy(&emitter.m_uniforms, _uniforms, sizeof(EmitterUniforms) );
  489. }
  490. }
  491. void destroyEmitter(EmitterHandle _handle)
  492. {
  493. BX_CHECK(m_emitterAlloc.isValid(_handle.idx)
  494. , "destroyEmitter handle %d is not valid."
  495. , _handle.idx
  496. );
  497. m_emitter[_handle.idx].destroy();
  498. m_emitterAlloc->free(_handle.idx);
  499. }
  500. bx::AllocatorI* m_allocator;
  501. bx::HandleAlloc* m_emitterAlloc;
  502. Emitter* m_emitter;
  503. bgfx::UniformHandle s_texColor;
  504. bgfx::TextureHandle m_particleTexture;
  505. bgfx::ProgramHandle m_particleProgram;
  506. uint32_t m_num;
  507. };
  508. static ParticleSystem s_ctx;
  509. void Emitter::create(EmitterShape::Enum _shape, EmitterDirection::Enum _direction, uint32_t _maxParticles)
  510. {
  511. m_dt = 0.0f;
  512. m_uniforms.reset();
  513. m_shape = _shape;
  514. m_direction = _direction;
  515. m_num = 0;
  516. m_max = _maxParticles;
  517. m_particles = (Particle*)BX_ALLOC(s_ctx.m_allocator, m_max*sizeof(Particle) );
  518. }
  519. void Emitter::destroy()
  520. {
  521. BX_FREE(s_ctx.m_allocator, m_particles);
  522. m_particles = NULL;
  523. }
  524. } // namespace ps
  525. using namespace ps;
  526. void psInit(uint16_t _maxEmitters, bx::AllocatorI* _allocator)
  527. {
  528. s_ctx.init(_maxEmitters, _allocator);
  529. }
  530. void psShutdown()
  531. {
  532. s_ctx.shutdown();
  533. }
  534. EmitterHandle psCreateEmitter(EmitterShape::Enum _shape, EmitterDirection::Enum _direction, uint32_t _maxParticles)
  535. {
  536. return s_ctx.createEmitter(_shape, _direction, _maxParticles);
  537. }
  538. void psUpdateEmitter(EmitterHandle _handle, const EmitterUniforms* _uniforms)
  539. {
  540. s_ctx.updateEmitter(_handle, _uniforms);
  541. }
  542. void psDestroyEmitter(EmitterHandle _handle)
  543. {
  544. s_ctx.destroyEmitter(_handle);
  545. }
  546. void psUpdate(float _dt)
  547. {
  548. s_ctx.update(_dt);
  549. }
  550. void psRender(uint8_t _view, const float* _mtxView, const float* _eye)
  551. {
  552. s_ctx.render(_view, _mtxView, _eye);
  553. }