particles.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. #include <entry/cmd.h>
  8. #include <entry/input.h>
  9. #include <camera.h>
  10. #include <debugdraw/debugdraw.h>
  11. #include <imgui/imgui.h>
  12. #include <bx/rng.h>
  13. #include <bx/easing.h>
  14. #include <ps/particle_system.h>
  15. namespace
  16. {
  17. static const char* s_shapeNames[] =
  18. {
  19. "Sphere",
  20. "Hemisphere",
  21. "Circle",
  22. "Disc",
  23. "Rect",
  24. };
  25. static const char* s_directionName[] =
  26. {
  27. "Up",
  28. "Outward",
  29. };
  30. static const char* s_easeFuncName[] =
  31. {
  32. "Linear",
  33. "Step",
  34. "SmoothStep",
  35. "InQuad",
  36. "OutQuad",
  37. "InOutQuad",
  38. "OutInQuad",
  39. "InCubic",
  40. "OutCubic",
  41. "InOutCubic",
  42. "OutInCubic",
  43. "InQuart",
  44. "OutQuart",
  45. "InOutQuart",
  46. "OutInQuart",
  47. "InQuint",
  48. "OutQuint",
  49. "InOutQuint",
  50. "OutInQuint",
  51. "InSine",
  52. "OutSine",
  53. "InOutSine",
  54. "OutInSine",
  55. "InExpo",
  56. "OutExpo",
  57. "InOutExpo",
  58. "OutInExpo",
  59. "InCirc",
  60. "OutCirc",
  61. "InOutCirc",
  62. "OutInCirc",
  63. "InElastic",
  64. "OutElastic",
  65. "InOutElastic",
  66. "OutInElastic",
  67. "InBack",
  68. "OutBack",
  69. "InOutBack",
  70. "OutInBack",
  71. "InBounce",
  72. "OutBounce",
  73. "InOutBounce",
  74. "OutInBounce",
  75. };
  76. BX_STATIC_ASSERT(BX_COUNTOF(s_easeFuncName) == bx::Easing::Count);
  77. struct Emitter
  78. {
  79. EmitterUniforms m_uniforms;
  80. EmitterHandle m_handle;
  81. EmitterShape::Enum m_shape;
  82. EmitterDirection::Enum m_direction;
  83. void create()
  84. {
  85. m_shape = EmitterShape::Sphere;
  86. m_direction = EmitterDirection::Outward;
  87. m_handle = psCreateEmitter(m_shape, m_direction, 1024);
  88. m_uniforms.reset();
  89. }
  90. void destroy()
  91. {
  92. psDestroyEmitter(m_handle);
  93. }
  94. void update()
  95. {
  96. psUpdateEmitter(m_handle, &m_uniforms);
  97. }
  98. void imgui()
  99. {
  100. // if (ImGui::CollapsingHeader("General") )
  101. {
  102. if (ImGui::Combo("Shape", (int*)&m_shape, s_shapeNames, BX_COUNTOF(s_shapeNames) )
  103. || ImGui::Combo("Direction", (int*)&m_direction, s_directionName, BX_COUNTOF(s_directionName) ) )
  104. {
  105. psDestroyEmitter(m_handle);
  106. m_handle = psCreateEmitter(m_shape, m_direction, 1024);
  107. }
  108. ImGui::SliderInt("particles / s", (int*)&m_uniforms.m_particlesPerSecond, 0, 1024);
  109. ImGui::SliderFloat("Gravity scale"
  110. , &m_uniforms.m_gravityScale
  111. , -2.0f
  112. , 2.0f
  113. );
  114. ImGui::RangeSliderFloat("Life span"
  115. , &m_uniforms.m_lifeSpan[0]
  116. , &m_uniforms.m_lifeSpan[1]
  117. , 0.1f
  118. , 5.0f
  119. );
  120. if (ImGui::Button("Reset") )
  121. {
  122. psUpdateEmitter(m_handle);
  123. }
  124. }
  125. if (ImGui::CollapsingHeader("Position and scale") )
  126. {
  127. ImGui::Combo("Position Ease", (int*)&m_uniforms.m_easePos, s_easeFuncName, BX_COUNTOF(s_easeFuncName) );
  128. ImGui::RangeSliderFloat("Start offset"
  129. , &m_uniforms.m_offsetStart[0]
  130. , &m_uniforms.m_offsetStart[1]
  131. , 0.0f
  132. , 10.0f
  133. );
  134. ImGui::RangeSliderFloat("End offset"
  135. , &m_uniforms.m_offsetEnd[0]
  136. , &m_uniforms.m_offsetEnd[1]
  137. , 0.0f
  138. , 10.0f
  139. );
  140. ImGui::Text("Scale:");
  141. ImGui::Combo("Scale Ease", (int*)&m_uniforms.m_easeScale, s_easeFuncName, BX_COUNTOF(s_easeFuncName) );
  142. ImGui::RangeSliderFloat("Scale Start"
  143. , &m_uniforms.m_scaleStart[0]
  144. , &m_uniforms.m_scaleStart[1]
  145. , 0.0f
  146. , 3.0f
  147. );
  148. ImGui::RangeSliderFloat("Scale End"
  149. , &m_uniforms.m_scaleEnd[0]
  150. , &m_uniforms.m_scaleEnd[1]
  151. , 0.0f
  152. , 3.0f
  153. );
  154. }
  155. if (ImGui::CollapsingHeader("Blending and color") )
  156. {
  157. ImGui::Combo("Blend Ease", (int*)&m_uniforms.m_easeBlend, s_easeFuncName, BX_COUNTOF(s_easeFuncName) );
  158. ImGui::RangeSliderFloat("Blend Start"
  159. , &m_uniforms.m_blendStart[0]
  160. , &m_uniforms.m_blendStart[1]
  161. , 0.0f
  162. , 1.0f
  163. );
  164. ImGui::RangeSliderFloat("Blend End"
  165. , &m_uniforms.m_blendEnd[0]
  166. , &m_uniforms.m_blendEnd[1]
  167. , 0.0f
  168. , 1.0f
  169. );
  170. ImGui::Text("Color:");
  171. ImGui::Combo("RGBA Ease", (int*)&m_uniforms.m_easeRgba, s_easeFuncName, BX_COUNTOF(s_easeFuncName) );
  172. ImGui::ColorWheel("RGBA0", &m_uniforms.m_rgba[0], 0.3f);
  173. ImGui::ColorWheel("RGBA1", &m_uniforms.m_rgba[1], 0.3f);
  174. ImGui::ColorWheel("RGBA2", &m_uniforms.m_rgba[2], 0.3f);
  175. ImGui::ColorWheel("RGBA3", &m_uniforms.m_rgba[3], 0.3f);
  176. ImGui::ColorWheel("RGBA4", &m_uniforms.m_rgba[4], 0.3f);
  177. }
  178. }
  179. void gizmo(const float* _view, const float* _proj)
  180. {
  181. float mtx[16];
  182. float scale[3] = { 1.0f, 1.0f, 1.0f };
  183. ImGuizmo::RecomposeMatrixFromComponents(m_uniforms.m_position, m_uniforms.m_angle, scale, mtx);
  184. ImGuiIO& io = ImGui::GetIO();
  185. ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y);
  186. ImGuizmo::Manipulate(
  187. _view
  188. , _proj
  189. , ImGuizmo::TRANSLATE
  190. , ImGuizmo::LOCAL
  191. , mtx
  192. );
  193. ImGuizmo::DecomposeMatrixToComponents(mtx, m_uniforms.m_position, m_uniforms.m_angle, scale);
  194. }
  195. };
  196. class ExampleParticles : public entry::AppI
  197. {
  198. public:
  199. ExampleParticles(const char* _name, const char* _description)
  200. : entry::AppI(_name, _description)
  201. {
  202. }
  203. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  204. {
  205. Args args(_argc, _argv);
  206. m_width = _width;
  207. m_height = _height;
  208. m_debug = BGFX_DEBUG_NONE;
  209. m_reset = BGFX_RESET_VSYNC;
  210. bgfx::Init init;
  211. init.type = args.m_type;
  212. init.vendorId = args.m_pciId;
  213. init.resolution.width = m_width;
  214. init.resolution.height = m_height;
  215. init.resolution.reset = m_reset;
  216. bgfx::init(init);
  217. // Enable m_debug text.
  218. bgfx::setDebug(m_debug);
  219. // Set view 0 clear state.
  220. bgfx::setViewClear(0
  221. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  222. , 0x202020ff
  223. , 1.0f
  224. , 0
  225. );
  226. ddInit();
  227. psInit();
  228. bimg::ImageContainer* image = imageLoad(
  229. "textures/particle.ktx"
  230. , bgfx::TextureFormat::BGRA8
  231. );
  232. EmitterSpriteHandle sprite = psCreateSprite(
  233. uint16_t(image->m_width)
  234. , uint16_t(image->m_height)
  235. , image->m_data
  236. );
  237. bimg::imageFree(image);
  238. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  239. {
  240. m_emitter[ii].create();
  241. m_emitter[ii].m_uniforms.m_handle = sprite;
  242. m_emitter[ii].update();
  243. }
  244. imguiCreate();
  245. cameraCreate();
  246. cameraSetPosition({ 0.0f, 2.0f, -12.0f });
  247. cameraSetVerticalAngle(0.0f);
  248. m_timeOffset = bx::getHPCounter();
  249. }
  250. virtual int shutdown() override
  251. {
  252. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  253. {
  254. m_emitter[ii].destroy();
  255. }
  256. psShutdown();
  257. ddShutdown();
  258. imguiDestroy();
  259. cameraDestroy();
  260. // Shutdown bgfx.
  261. bgfx::shutdown();
  262. return 0;
  263. }
  264. bool update() override
  265. {
  266. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  267. {
  268. // Set view 0 default viewport.
  269. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  270. bgfx::touch(0);
  271. int64_t now = bx::getHPCounter() - m_timeOffset;
  272. static int64_t last = now;
  273. const int64_t frameTime = now - last;
  274. last = now;
  275. const double freq = double(bx::getHPFrequency() );
  276. const float deltaTime = float(frameTime/freq);
  277. cameraUpdate(deltaTime, m_mouseState);
  278. float view[16];
  279. cameraGetViewMtx(view);
  280. float proj[16];
  281. // Set view and projection matrix for view 0.
  282. {
  283. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  284. bgfx::setViewTransform(0, view, proj);
  285. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  286. }
  287. imguiBeginFrame(
  288. m_mouseState.m_mx
  289. , m_mouseState.m_my
  290. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  291. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  292. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  293. , m_mouseState.m_mz
  294. , uint16_t(m_width)
  295. , uint16_t(m_height)
  296. );
  297. showExampleDialog(this);
  298. ImGui::SetNextWindowPos(
  299. ImVec2(m_width - m_width / 4.0f - 10.0f, 10.0f)
  300. , ImGuiCond_FirstUseEver
  301. );
  302. ImGui::SetNextWindowSize(
  303. ImVec2(m_width / 4.0f, m_height - 20.0f)
  304. , ImGuiCond_FirstUseEver
  305. );
  306. ImGui::Begin("Settings"
  307. , NULL
  308. );
  309. static float timeScale = 1.0f;
  310. ImGui::SliderFloat("Time scale"
  311. , &timeScale
  312. , 0.0f
  313. , 1.0f
  314. );
  315. static bool showBounds;
  316. ImGui::Checkbox("Show bounds", &showBounds);
  317. ImGui::Text("Emitter:");
  318. static int currentEmitter = 0;
  319. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  320. {
  321. ImGui::SameLine();
  322. char name[16];
  323. bx::snprintf(name, BX_COUNTOF(name), "%d", ii);
  324. ImGui::RadioButton(name, &currentEmitter, ii);
  325. }
  326. m_emitter[currentEmitter].imgui();
  327. ImGui::End();
  328. m_emitter[currentEmitter].gizmo(view, proj);
  329. imguiEndFrame();
  330. DebugDrawEncoder dde;
  331. dde.begin(0);
  332. dde.drawGrid(Axis::Y, { 0.0f, 0.0f, 0.0f });
  333. const bx::Vec3 eye = cameraGetPosition();
  334. m_emitter[currentEmitter].update();
  335. psUpdate(deltaTime * timeScale);
  336. psRender(0, view, eye);
  337. if (showBounds)
  338. {
  339. Aabb aabb;
  340. psGetAabb(m_emitter[currentEmitter].m_handle, aabb);
  341. dde.push();
  342. dde.setWireframe(true);
  343. dde.setColor(0xff0000ff);
  344. dde.draw(aabb);
  345. dde.pop();
  346. }
  347. dde.end();
  348. // Advance to next frame. Rendering thread will be kicked to
  349. // process submitted rendering primitives.
  350. bgfx::frame();
  351. return true;
  352. }
  353. return false;
  354. }
  355. entry::MouseState m_mouseState;
  356. int64_t m_timeOffset;
  357. uint32_t m_width;
  358. uint32_t m_height;
  359. uint32_t m_debug;
  360. uint32_t m_reset;
  361. Emitter m_emitter[4];
  362. };
  363. } // namespace
  364. ENTRY_IMPLEMENT_MAIN(ExampleParticles, "32-particles", "Particles.");