particles.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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()
  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. }
  175. void gizmo(const float* _view, const float* _proj)
  176. {
  177. float mtx[16];
  178. bx::mtxSRT(mtx
  179. , 1.0f, 1.0f, 1.0f
  180. , m_uniforms.m_angle[0], m_uniforms.m_angle[1], m_uniforms.m_angle[2]
  181. , m_uniforms.m_position[0], m_uniforms.m_position[1], m_uniforms.m_position[2]
  182. );
  183. ImGuiIO& io = ImGui::GetIO();
  184. ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y);
  185. ImGuizmo::Manipulate(
  186. _view
  187. , _proj
  188. , ImGuizmo::TRANSLATE
  189. , ImGuizmo::LOCAL
  190. , mtx
  191. );
  192. float scale[3];
  193. ImGuizmo::DecomposeMatrixToComponents(mtx, m_uniforms.m_position, m_uniforms.m_angle, scale);
  194. }
  195. };
  196. class Particles : public entry::AppI
  197. {
  198. void init(int _argc, char** _argv) BX_OVERRIDE
  199. {
  200. Args args(_argc, _argv);
  201. m_width = 1280;
  202. m_height = 720;
  203. m_debug = BGFX_DEBUG_TEXT;
  204. m_reset = BGFX_RESET_VSYNC;
  205. bgfx::init(args.m_type, args.m_pciId);
  206. bgfx::reset(m_width, m_height, m_reset);
  207. // Enable m_debug text.
  208. bgfx::setDebug(m_debug);
  209. // Set view 0 clear state.
  210. bgfx::setViewClear(0
  211. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  212. , 0x202020ff
  213. , 1.0f
  214. , 0
  215. );
  216. ddInit();
  217. psInit();
  218. bimg::ImageContainer* image = imageLoad(
  219. "textures/particle.ktx"
  220. , bgfx::TextureFormat::BGRA8
  221. );
  222. EmitterSpriteHandle sprite = psCreateSprite(
  223. uint16_t(image->m_width)
  224. , uint16_t(image->m_height)
  225. , image->m_data
  226. );
  227. bimg::imageFree(image);
  228. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  229. {
  230. m_emitter[ii].create();
  231. m_emitter[ii].m_uniforms.m_handle = sprite;
  232. }
  233. imguiCreate();
  234. cameraCreate();
  235. const float initialPos[3] = { 0.0f, 2.0f, -12.0f };
  236. cameraSetPosition(initialPos);
  237. cameraSetVerticalAngle(0.0f);
  238. m_timeOffset = bx::getHPCounter();
  239. }
  240. virtual int shutdown() BX_OVERRIDE
  241. {
  242. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  243. {
  244. m_emitter[ii].destroy();
  245. }
  246. psShutdown();
  247. ddShutdown();
  248. imguiDestroy();
  249. cameraDestroy();
  250. // Shutdown bgfx.
  251. bgfx::shutdown();
  252. return 0;
  253. }
  254. bool update() BX_OVERRIDE
  255. {
  256. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  257. {
  258. // Set view 0 default viewport.
  259. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  260. bgfx::touch(0);
  261. int64_t now = bx::getHPCounter() - m_timeOffset;
  262. static int64_t last = now;
  263. const int64_t frameTime = now - last;
  264. last = now;
  265. const double freq = double(bx::getHPFrequency() );
  266. const double toMs = 1000.0/freq;
  267. const float deltaTime = float(frameTime/freq);
  268. // Use debug font to print information about this example.
  269. bgfx::dbgTextClear();
  270. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/32-particles");
  271. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Particles.");
  272. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  273. cameraUpdate(deltaTime, m_mouseState);
  274. float view[16];
  275. cameraGetViewMtx(view);
  276. float proj[16];
  277. // Set view and projection matrix for view 0.
  278. const bgfx::HMD* hmd = bgfx::getHMD();
  279. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  280. {
  281. float eye[3];
  282. cameraGetPosition(eye);
  283. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  284. bgfx::setViewTransform(0, view, hmd->eye[0].projection, BGFX_VIEW_STEREO, hmd->eye[1].projection);
  285. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  286. }
  287. else
  288. {
  289. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  290. bgfx::setViewTransform(0, view, proj);
  291. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  292. }
  293. imguiBeginFrame(
  294. m_mouseState.m_mx
  295. , m_mouseState.m_my
  296. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  297. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  298. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  299. , m_mouseState.m_mz
  300. , uint16_t(m_width)
  301. , uint16_t(m_height)
  302. );
  303. ImGui::Begin("Properties"
  304. , NULL
  305. , ImVec2(400.0f, 600.0f)
  306. , ImGuiWindowFlags_AlwaysAutoResize
  307. );
  308. static float timeScale = 1.0f;
  309. ImGui::SliderFloat("Time scale"
  310. , &timeScale
  311. , 0.0f
  312. , 1.0f
  313. );
  314. static bool showBounds;
  315. ImGui::Checkbox("Show bounds", &showBounds);
  316. ImGui::Text("Emitter:");
  317. static int currentEmitter = 0;
  318. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  319. {
  320. ImGui::SameLine();
  321. char name[16];
  322. bx::snprintf(name, BX_COUNTOF(name), "%d", ii);
  323. ImGui::RadioButton(name, &currentEmitter, ii);
  324. }
  325. m_emitter[currentEmitter].imgui();
  326. ImGui::End();
  327. m_emitter[currentEmitter].gizmo(view, proj);
  328. imguiEndFrame();
  329. ddBegin(0);
  330. float center[3] = { 0.0f, 0.0f, 0.0f };
  331. ddDrawGrid(Axis::Y, center);
  332. float eye[3];
  333. cameraGetPosition(eye);
  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. ddSetColor(0xff0000ff);
  342. ddDraw(aabb);
  343. }
  344. ddEnd();
  345. // Advance to next frame. Rendering thread will be kicked to
  346. // process submitted rendering primitives.
  347. bgfx::frame();
  348. return true;
  349. }
  350. return false;
  351. }
  352. entry::MouseState m_mouseState;
  353. int64_t m_timeOffset;
  354. uint32_t m_width;
  355. uint32_t m_height;
  356. uint32_t m_debug;
  357. uint32_t m_reset;
  358. Emitter m_emitter[4];
  359. };
  360. ENTRY_IMPLEMENT_MAIN(Particles);