particles.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Copyright 2011-2018 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. bx::mtxSRT(mtx
  183. , 1.0f, 1.0f, 1.0f
  184. , m_uniforms.m_angle[0], m_uniforms.m_angle[1], m_uniforms.m_angle[2]
  185. , m_uniforms.m_position[0], m_uniforms.m_position[1], m_uniforms.m_position[2]
  186. );
  187. ImGuiIO& io = ImGui::GetIO();
  188. ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y);
  189. ImGuizmo::Manipulate(
  190. _view
  191. , _proj
  192. , ImGuizmo::TRANSLATE
  193. , ImGuizmo::LOCAL
  194. , mtx
  195. );
  196. float scale[3];
  197. ImGuizmo::DecomposeMatrixToComponents(mtx, m_uniforms.m_position, m_uniforms.m_angle, scale);
  198. }
  199. };
  200. class ExampleParticles : public entry::AppI
  201. {
  202. public:
  203. ExampleParticles(const char* _name, const char* _description)
  204. : entry::AppI(_name, _description)
  205. {
  206. }
  207. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  208. {
  209. Args args(_argc, _argv);
  210. m_width = _width;
  211. m_height = _height;
  212. m_debug = BGFX_DEBUG_NONE;
  213. m_reset = BGFX_RESET_VSYNC;
  214. bgfx::init(args.m_type, args.m_pciId);
  215. bgfx::reset(m_width, m_height, m_reset);
  216. // Enable m_debug text.
  217. bgfx::setDebug(m_debug);
  218. // Set view 0 clear state.
  219. bgfx::setViewClear(0
  220. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  221. , 0x202020ff
  222. , 1.0f
  223. , 0
  224. );
  225. ddInit();
  226. psInit();
  227. bimg::ImageContainer* image = imageLoad(
  228. "textures/particle.ktx"
  229. , bgfx::TextureFormat::BGRA8
  230. );
  231. EmitterSpriteHandle sprite = psCreateSprite(
  232. uint16_t(image->m_width)
  233. , uint16_t(image->m_height)
  234. , image->m_data
  235. );
  236. bimg::imageFree(image);
  237. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  238. {
  239. m_emitter[ii].create();
  240. m_emitter[ii].m_uniforms.m_handle = sprite;
  241. m_emitter[ii].update();
  242. }
  243. imguiCreate();
  244. cameraCreate();
  245. const float initialPos[3] = { 0.0f, 2.0f, -12.0f };
  246. cameraSetPosition(initialPos);
  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. const bgfx::HMD* hmd = bgfx::getHMD();
  283. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  284. {
  285. float eye[3];
  286. cameraGetPosition(eye);
  287. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  288. bgfx::setViewTransform(0, view, hmd->eye[0].projection, BGFX_VIEW_STEREO, hmd->eye[1].projection);
  289. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  290. }
  291. else
  292. {
  293. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  294. bgfx::setViewTransform(0, view, proj);
  295. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  296. }
  297. imguiBeginFrame(
  298. m_mouseState.m_mx
  299. , m_mouseState.m_my
  300. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  301. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  302. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  303. , m_mouseState.m_mz
  304. , uint16_t(m_width)
  305. , uint16_t(m_height)
  306. );
  307. showExampleDialog(this);
  308. ImGui::SetNextWindowPos(
  309. ImVec2(m_width - m_width / 4.0f - 10.0f, 10.0f)
  310. , ImGuiCond_FirstUseEver
  311. );
  312. ImGui::SetNextWindowSize(
  313. ImVec2(m_width / 4.0f, m_height - 20.0f)
  314. , ImGuiCond_FirstUseEver
  315. );
  316. ImGui::Begin("Settings"
  317. , NULL
  318. );
  319. static float timeScale = 1.0f;
  320. ImGui::SliderFloat("Time scale"
  321. , &timeScale
  322. , 0.0f
  323. , 1.0f
  324. );
  325. static bool showBounds;
  326. ImGui::Checkbox("Show bounds", &showBounds);
  327. ImGui::Text("Emitter:");
  328. static int currentEmitter = 0;
  329. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  330. {
  331. ImGui::SameLine();
  332. char name[16];
  333. bx::snprintf(name, BX_COUNTOF(name), "%d", ii);
  334. ImGui::RadioButton(name, &currentEmitter, ii);
  335. }
  336. m_emitter[currentEmitter].imgui();
  337. ImGui::End();
  338. m_emitter[currentEmitter].gizmo(view, proj);
  339. imguiEndFrame();
  340. ddBegin(0);
  341. float center[3] = { 0.0f, 0.0f, 0.0f };
  342. ddDrawGrid(Axis::Y, center);
  343. float eye[3];
  344. cameraGetPosition(eye);
  345. m_emitter[currentEmitter].update();
  346. psUpdate(deltaTime * timeScale);
  347. psRender(0, view, eye);
  348. if (showBounds)
  349. {
  350. Aabb aabb;
  351. psGetAabb(m_emitter[currentEmitter].m_handle, aabb);
  352. ddPush();
  353. ddSetWireframe(true);
  354. ddSetColor(0xff0000ff);
  355. ddDraw(aabb);
  356. ddPop();
  357. }
  358. ddEnd();
  359. // Advance to next frame. Rendering thread will be kicked to
  360. // process submitted rendering primitives.
  361. bgfx::frame();
  362. return true;
  363. }
  364. return false;
  365. }
  366. entry::MouseState m_mouseState;
  367. int64_t m_timeOffset;
  368. uint32_t m_width;
  369. uint32_t m_height;
  370. uint32_t m_debug;
  371. uint32_t m_reset;
  372. Emitter m_emitter[4];
  373. };
  374. } // namespace
  375. ENTRY_IMPLEMENT_MAIN(ExampleParticles, "32-particles", "Particles.");