particles.cpp 9.6 KB

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