particles.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 init;
  215. init.type = args.m_type;
  216. init.vendorId = args.m_pciId;
  217. init.resolution.width = m_width;
  218. init.resolution.height = m_height;
  219. init.resolution.reset = m_reset;
  220. bgfx::init(init);
  221. // Enable m_debug text.
  222. bgfx::setDebug(m_debug);
  223. // Set view 0 clear state.
  224. bgfx::setViewClear(0
  225. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  226. , 0x202020ff
  227. , 1.0f
  228. , 0
  229. );
  230. ddInit();
  231. psInit();
  232. bimg::ImageContainer* image = imageLoad(
  233. "textures/particle.ktx"
  234. , bgfx::TextureFormat::BGRA8
  235. );
  236. EmitterSpriteHandle sprite = psCreateSprite(
  237. uint16_t(image->m_width)
  238. , uint16_t(image->m_height)
  239. , image->m_data
  240. );
  241. bimg::imageFree(image);
  242. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  243. {
  244. m_emitter[ii].create();
  245. m_emitter[ii].m_uniforms.m_handle = sprite;
  246. m_emitter[ii].update();
  247. }
  248. imguiCreate();
  249. cameraCreate();
  250. const float initialPos[3] = { 0.0f, 2.0f, -12.0f };
  251. cameraSetPosition(initialPos);
  252. cameraSetVerticalAngle(0.0f);
  253. m_timeOffset = bx::getHPCounter();
  254. }
  255. virtual int shutdown() override
  256. {
  257. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  258. {
  259. m_emitter[ii].destroy();
  260. }
  261. psShutdown();
  262. ddShutdown();
  263. imguiDestroy();
  264. cameraDestroy();
  265. // Shutdown bgfx.
  266. bgfx::shutdown();
  267. return 0;
  268. }
  269. bool update() override
  270. {
  271. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  272. {
  273. // Set view 0 default viewport.
  274. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  275. bgfx::touch(0);
  276. int64_t now = bx::getHPCounter() - m_timeOffset;
  277. static int64_t last = now;
  278. const int64_t frameTime = now - last;
  279. last = now;
  280. const double freq = double(bx::getHPFrequency() );
  281. const float deltaTime = float(frameTime/freq);
  282. cameraUpdate(deltaTime, m_mouseState);
  283. float view[16];
  284. cameraGetViewMtx(view);
  285. float proj[16];
  286. // Set view and projection matrix for view 0.
  287. const bgfx::HMD* hmd = bgfx::getHMD();
  288. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  289. {
  290. float eye[3];
  291. cameraGetPosition(eye);
  292. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  293. bgfx::setViewTransform(0, view, hmd->eye[0].projection, BGFX_VIEW_STEREO, hmd->eye[1].projection);
  294. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  295. }
  296. else
  297. {
  298. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  299. bgfx::setViewTransform(0, view, proj);
  300. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  301. }
  302. imguiBeginFrame(
  303. m_mouseState.m_mx
  304. , m_mouseState.m_my
  305. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  306. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  307. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  308. , m_mouseState.m_mz
  309. , uint16_t(m_width)
  310. , uint16_t(m_height)
  311. );
  312. showExampleDialog(this);
  313. ImGui::SetNextWindowPos(
  314. ImVec2(m_width - m_width / 4.0f - 10.0f, 10.0f)
  315. , ImGuiCond_FirstUseEver
  316. );
  317. ImGui::SetNextWindowSize(
  318. ImVec2(m_width / 4.0f, m_height - 20.0f)
  319. , ImGuiCond_FirstUseEver
  320. );
  321. ImGui::Begin("Settings"
  322. , NULL
  323. );
  324. static float timeScale = 1.0f;
  325. ImGui::SliderFloat("Time scale"
  326. , &timeScale
  327. , 0.0f
  328. , 1.0f
  329. );
  330. static bool showBounds;
  331. ImGui::Checkbox("Show bounds", &showBounds);
  332. ImGui::Text("Emitter:");
  333. static int currentEmitter = 0;
  334. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  335. {
  336. ImGui::SameLine();
  337. char name[16];
  338. bx::snprintf(name, BX_COUNTOF(name), "%d", ii);
  339. ImGui::RadioButton(name, &currentEmitter, ii);
  340. }
  341. m_emitter[currentEmitter].imgui();
  342. ImGui::End();
  343. m_emitter[currentEmitter].gizmo(view, proj);
  344. imguiEndFrame();
  345. ddBegin(0);
  346. float center[3] = { 0.0f, 0.0f, 0.0f };
  347. ddDrawGrid(Axis::Y, center);
  348. float eye[3];
  349. cameraGetPosition(eye);
  350. m_emitter[currentEmitter].update();
  351. psUpdate(deltaTime * timeScale);
  352. psRender(0, view, eye);
  353. if (showBounds)
  354. {
  355. Aabb aabb;
  356. psGetAabb(m_emitter[currentEmitter].m_handle, aabb);
  357. ddPush();
  358. ddSetWireframe(true);
  359. ddSetColor(0xff0000ff);
  360. ddDraw(aabb);
  361. ddPop();
  362. }
  363. ddEnd();
  364. // Advance to next frame. Rendering thread will be kicked to
  365. // process submitted rendering primitives.
  366. bgfx::frame();
  367. return true;
  368. }
  369. return false;
  370. }
  371. entry::MouseState m_mouseState;
  372. int64_t m_timeOffset;
  373. uint32_t m_width;
  374. uint32_t m_height;
  375. uint32_t m_debug;
  376. uint32_t m_reset;
  377. Emitter m_emitter[4];
  378. };
  379. } // namespace
  380. ENTRY_IMPLEMENT_MAIN(ExampleParticles, "32-particles", "Particles.");