particles.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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::TRANSLATE
  185. , ImGuizmo::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. bimg::ImageContainer* image = imageLoad(
  215. "textures/particle.ktx"
  216. , bgfx::TextureFormat::BGRA8
  217. );
  218. EmitterSpriteHandle sprite = psCreateSprite(
  219. uint16_t(image->m_width)
  220. , uint16_t(image->m_height)
  221. , image->m_data
  222. );
  223. bimg::imageFree(image);
  224. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  225. {
  226. m_emitter[ii].create();
  227. m_emitter[ii].m_uniforms.m_handle = sprite;
  228. }
  229. imguiCreate();
  230. cameraCreate();
  231. const float initialPos[3] = { 0.0f, 2.0f, -12.0f };
  232. cameraSetPosition(initialPos);
  233. cameraSetVerticalAngle(0.0f);
  234. m_timeOffset = bx::getHPCounter();
  235. }
  236. virtual int shutdown() BX_OVERRIDE
  237. {
  238. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  239. {
  240. m_emitter[ii].destroy();
  241. }
  242. psShutdown();
  243. ddShutdown();
  244. imguiDestroy();
  245. cameraDestroy();
  246. // Shutdown bgfx.
  247. bgfx::shutdown();
  248. return 0;
  249. }
  250. bool update() BX_OVERRIDE
  251. {
  252. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  253. {
  254. // Set view 0 default viewport.
  255. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  256. bgfx::touch(0);
  257. int64_t now = bx::getHPCounter() - m_timeOffset;
  258. static int64_t last = now;
  259. const int64_t frameTime = now - last;
  260. last = now;
  261. const double freq = double(bx::getHPFrequency() );
  262. const double toMs = 1000.0/freq;
  263. const float deltaTime = float(frameTime/freq);
  264. // Use debug font to print information about this example.
  265. bgfx::dbgTextClear();
  266. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/32-particles");
  267. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Particles.");
  268. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  269. // Update camera.George RR Martin
  270. cameraUpdate(deltaTime, m_mouseState);
  271. float view[16];
  272. cameraGetViewMtx(view);
  273. float proj[16];
  274. // Set view and projection matrix for view 0.
  275. const bgfx::HMD* hmd = bgfx::getHMD();
  276. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  277. {
  278. float eye[3];
  279. cameraGetPosition(eye);
  280. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  281. bgfx::setViewTransform(0, view, hmd->eye[0].projection, BGFX_VIEW_STEREO, hmd->eye[1].projection);
  282. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  283. }
  284. else
  285. {
  286. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  287. bgfx::setViewTransform(0, view, proj);
  288. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  289. }
  290. imguiBeginFrame(
  291. m_mouseState.m_mx
  292. , m_mouseState.m_my
  293. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  294. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  295. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  296. , m_mouseState.m_mz
  297. , uint16_t(m_width)
  298. , uint16_t(m_height)
  299. );
  300. ImGui::Begin("Properties"
  301. , NULL
  302. , ImVec2(400.0f, 600.0f)
  303. , ImGuiWindowFlags_AlwaysAutoResize
  304. );
  305. static float timeScale = 1.0f;
  306. ImGui::SliderFloat("Time scale"
  307. , &timeScale
  308. , 0.0f
  309. , 1.0f
  310. );
  311. static bool showBounds;
  312. ImGui::Checkbox("Show bounds", &showBounds);
  313. ImGui::Text("Emitter:");
  314. static int currentEmitter = 0;
  315. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  316. {
  317. ImGui::SameLine();
  318. char name[16];
  319. bx::snprintf(name, BX_COUNTOF(name), "%d", ii);
  320. ImGui::RadioButton(name, &currentEmitter, ii);
  321. }
  322. m_emitter[currentEmitter].imgui(view, proj);
  323. imguiEndFrame();
  324. ddBegin(0);
  325. float center[3] = { 0.0f, 0.0f, 0.0f };
  326. ddDrawGrid(Axis::Y, center);
  327. float eye[3];
  328. cameraGetPosition(eye);
  329. m_emitter[currentEmitter].update();
  330. psUpdate(deltaTime * timeScale);
  331. psRender(0, view, eye);
  332. if (showBounds)
  333. {
  334. Aabb aabb;
  335. psGetAabb(m_emitter[currentEmitter].m_handle, aabb);
  336. ddSetColor(0xff0000ff);
  337. ddDraw(aabb);
  338. }
  339. ddEnd();
  340. // Advance to next frame. Rendering thread will be kicked to
  341. // process submitted rendering primitives.
  342. bgfx::frame();
  343. return true;
  344. }
  345. return false;
  346. }
  347. entry::MouseState m_mouseState;
  348. int64_t m_timeOffset;
  349. uint32_t m_width;
  350. uint32_t m_height;
  351. uint32_t m_debug;
  352. uint32_t m_reset;
  353. Emitter m_emitter[4];
  354. };
  355. ENTRY_IMPLEMENT_MAIN(Particles);