particles.cpp 9.0 KB

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