wireframe.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * Copyright 2016 Dario Manesku. 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 "imgui/imgui.h"
  8. namespace
  9. {
  10. struct DrawMode
  11. {
  12. enum
  13. {
  14. WireframeShaded,
  15. Wireframe,
  16. Shaded,
  17. };
  18. };
  19. struct Camera
  20. {
  21. Camera()
  22. {
  23. reset();
  24. }
  25. void reset()
  26. {
  27. m_target.curr[0] = 0.0f;
  28. m_target.curr[1] = 0.0f;
  29. m_target.curr[2] = 0.0f;
  30. m_target.dest[0] = 0.0f;
  31. m_target.dest[1] = 0.0f;
  32. m_target.dest[2] = 0.0f;
  33. m_pos.curr[0] = 0.0f;
  34. m_pos.curr[1] = 0.0f;
  35. m_pos.curr[2] = -2.0f;
  36. m_pos.dest[0] = 0.0f;
  37. m_pos.dest[1] = 0.0f;
  38. m_pos.dest[2] = -2.0f;
  39. m_orbit[0] = 0.0f;
  40. m_orbit[1] = 0.0f;
  41. }
  42. void mtxLookAt(float* _outViewMtx)
  43. {
  44. bx::mtxLookAt(_outViewMtx, m_pos.curr, m_target.curr);
  45. }
  46. void orbit(float _dx, float _dy)
  47. {
  48. m_orbit[0] += _dx;
  49. m_orbit[1] += _dy;
  50. }
  51. void dolly(float _dz)
  52. {
  53. const float cnear = 0.01f;
  54. const float cfar = 10.0f;
  55. const float toTarget[3] =
  56. {
  57. m_target.dest[0] - m_pos.dest[0],
  58. m_target.dest[1] - m_pos.dest[1],
  59. m_target.dest[2] - m_pos.dest[2],
  60. };
  61. const float toTargetLen = bx::vec3Length(toTarget);
  62. const float invToTargetLen = 1.0f/(toTargetLen+FLT_MIN);
  63. const float toTargetNorm[3] =
  64. {
  65. toTarget[0]*invToTargetLen,
  66. toTarget[1]*invToTargetLen,
  67. toTarget[2]*invToTargetLen,
  68. };
  69. float delta = toTargetLen*_dz;
  70. float newLen = toTargetLen + delta;
  71. if ( (cnear < newLen || _dz < 0.0f)
  72. && (newLen < cfar || _dz > 0.0f) )
  73. {
  74. m_pos.dest[0] += toTargetNorm[0]*delta;
  75. m_pos.dest[1] += toTargetNorm[1]*delta;
  76. m_pos.dest[2] += toTargetNorm[2]*delta;
  77. }
  78. }
  79. void consumeOrbit(float _amount)
  80. {
  81. float consume[2];
  82. consume[0] = m_orbit[0]*_amount;
  83. consume[1] = m_orbit[1]*_amount;
  84. m_orbit[0] -= consume[0];
  85. m_orbit[1] -= consume[1];
  86. const float toPos[3] =
  87. {
  88. m_pos.curr[0] - m_target.curr[0],
  89. m_pos.curr[1] - m_target.curr[1],
  90. m_pos.curr[2] - m_target.curr[2],
  91. };
  92. const float toPosLen = bx::vec3Length(toPos);
  93. const float invToPosLen = 1.0f/(toPosLen+FLT_MIN);
  94. const float toPosNorm[3] =
  95. {
  96. toPos[0]*invToPosLen,
  97. toPos[1]*invToPosLen,
  98. toPos[2]*invToPosLen,
  99. };
  100. float ll[2];
  101. bx::vec3ToLatLong(&ll[0], &ll[1], toPosNorm);
  102. ll[0] += consume[0];
  103. ll[1] -= consume[1];
  104. ll[1] = bx::fclamp(ll[1], 0.02f, 0.98f);
  105. float tmp[3];
  106. bx::vec3FromLatLong(tmp, ll[0], ll[1]);
  107. float diff[3];
  108. diff[0] = (tmp[0]-toPosNorm[0])*toPosLen;
  109. diff[1] = (tmp[1]-toPosNorm[1])*toPosLen;
  110. diff[2] = (tmp[2]-toPosNorm[2])*toPosLen;
  111. m_pos.curr[0] += diff[0];
  112. m_pos.curr[1] += diff[1];
  113. m_pos.curr[2] += diff[2];
  114. m_pos.dest[0] += diff[0];
  115. m_pos.dest[1] += diff[1];
  116. m_pos.dest[2] += diff[2];
  117. }
  118. void update(float _dt)
  119. {
  120. const float amount = bx::fmin(_dt/0.12f, 1.0f);
  121. consumeOrbit(amount);
  122. m_target.curr[0] = bx::flerp(m_target.curr[0], m_target.dest[0], amount);
  123. m_target.curr[1] = bx::flerp(m_target.curr[1], m_target.dest[1], amount);
  124. m_target.curr[2] = bx::flerp(m_target.curr[2], m_target.dest[2], amount);
  125. m_pos.curr[0] = bx::flerp(m_pos.curr[0], m_pos.dest[0], amount);
  126. m_pos.curr[1] = bx::flerp(m_pos.curr[1], m_pos.dest[1], amount);
  127. m_pos.curr[2] = bx::flerp(m_pos.curr[2], m_pos.dest[2], amount);
  128. }
  129. struct Interp3f
  130. {
  131. float curr[3];
  132. float dest[3];
  133. };
  134. Interp3f m_target;
  135. Interp3f m_pos;
  136. float m_orbit[2];
  137. };
  138. struct Mouse
  139. {
  140. Mouse()
  141. {
  142. m_dx = 0.0f;
  143. m_dy = 0.0f;
  144. m_prevMx = 0.0f;
  145. m_prevMx = 0.0f;
  146. m_scroll = 0;
  147. m_scrollPrev = 0;
  148. }
  149. void update(float _mx, float _my, int32_t _mz, uint32_t _width, uint32_t _height)
  150. {
  151. const float widthf = float(int32_t(_width));
  152. const float heightf = float(int32_t(_height));
  153. // Delta movement.
  154. m_dx = float(_mx - m_prevMx)/widthf;
  155. m_dy = float(_my - m_prevMy)/heightf;
  156. m_prevMx = _mx;
  157. m_prevMy = _my;
  158. // Scroll.
  159. m_scroll = _mz - m_scrollPrev;
  160. m_scrollPrev = _mz;
  161. }
  162. float m_dx; // Screen space.
  163. float m_dy;
  164. float m_prevMx;
  165. float m_prevMy;
  166. int32_t m_scroll;
  167. int32_t m_scrollPrev;
  168. };
  169. struct MeshMtx
  170. {
  171. MeshMtx()
  172. {
  173. m_mesh = NULL;
  174. }
  175. void init(const char* _path
  176. , float _scale = 1.0f
  177. , float _rotX = 0.0f
  178. , float _rotY = 0.0f
  179. , float _rotZ = 0.0f
  180. , float _transX = 0.0f
  181. , float _transY = 0.0f
  182. , float _transZ = 0.0f
  183. )
  184. {
  185. m_mesh = meshLoad(_path);
  186. bx::mtxSRT(m_mtx
  187. , _scale
  188. , _scale
  189. , _scale
  190. , _rotX
  191. , _rotY
  192. , _rotZ
  193. , _transX
  194. , _transY
  195. , _transZ
  196. );
  197. }
  198. void destroy()
  199. {
  200. if (NULL != m_mesh)
  201. {
  202. meshUnload(m_mesh);
  203. }
  204. }
  205. Mesh* m_mesh;
  206. float m_mtx[16];
  207. };
  208. struct Uniforms
  209. {
  210. enum { NumVec4 = 3 };
  211. void init()
  212. {
  213. m_camPos[0] = 0.0f;
  214. m_camPos[1] = 1.0f;
  215. m_camPos[2] = -2.5f;
  216. m_wfColor[0] = 1.0f;
  217. m_wfColor[1] = 0.0f;
  218. m_wfColor[2] = 0.0f;
  219. m_wfColor[3] = 1.0f;
  220. m_drawEdges = 0.0f;
  221. m_wfThickness = 1.5f;
  222. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, NumVec4);
  223. }
  224. void submit()
  225. {
  226. bgfx::setUniform(u_params, m_params, NumVec4);
  227. }
  228. void destroy()
  229. {
  230. bgfx::destroy(u_params);
  231. }
  232. union
  233. {
  234. struct
  235. {
  236. /*0*/struct { float m_camPos[3], m_unused0; };
  237. /*1*/struct { float m_wfColor[4]; };
  238. /*2*/struct { float m_drawEdges, m_wfThickness, m_unused2[2]; };
  239. };
  240. float m_params[NumVec4*4];
  241. };
  242. bgfx::UniformHandle u_params;
  243. };
  244. class ExampleWireframe : public entry::AppI
  245. {
  246. public:
  247. ExampleWireframe(const char* _name, const char* _description)
  248. : entry::AppI(_name, _description)
  249. {
  250. }
  251. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  252. {
  253. Args args(_argc, _argv);
  254. m_width = _width;
  255. m_height = _height;
  256. m_debug = BGFX_DEBUG_NONE;
  257. m_reset = 0
  258. | BGFX_RESET_VSYNC
  259. | BGFX_RESET_MSAA_X16
  260. ;
  261. bgfx::init(args.m_type, args.m_pciId);
  262. bgfx::reset(m_width, m_height, m_reset);
  263. // Enable m_debug text.
  264. bgfx::setDebug(m_debug);
  265. // Set view 0 clear state.
  266. bgfx::setViewClear(0
  267. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  268. , 0x303030ff
  269. , 1.0f
  270. , 0
  271. );
  272. m_wfProgram = loadProgram("vs_wf_wireframe", "fs_wf_wireframe");
  273. m_meshProgram = loadProgram("vs_wf_mesh", "fs_wf_mesh");
  274. m_uniforms.init();
  275. m_meshes[0].init("meshes/bunny.bin", 1.0f, 0.0f, bx::kPi, 0.0f, 0.0f, -0.8f, 0.0f);
  276. m_meshes[1].init("meshes/hollowcube.bin", 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
  277. m_meshes[2].init("meshes/orb.bin", 1.2f, 0.0f, 0.0f, 0.0f, 0.0f, -0.65f, 0.0f);
  278. // Imgui.
  279. imguiCreate();
  280. m_oldWidth = 0;
  281. m_oldHeight = 0;
  282. m_oldReset = m_reset;
  283. m_meshSelection = 1;
  284. m_drawMode = DrawMode::WireframeShaded;
  285. }
  286. virtual int shutdown() override
  287. {
  288. // Cleanup.
  289. imguiDestroy();
  290. m_meshes[0].destroy();
  291. m_meshes[1].destroy();
  292. m_meshes[2].destroy();
  293. bgfx::destroy(m_wfProgram);
  294. bgfx::destroy(m_meshProgram);
  295. m_uniforms.destroy();
  296. // Shutdown bgfx.
  297. bgfx::shutdown();
  298. return 0;
  299. }
  300. bool update() override
  301. {
  302. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  303. {
  304. if (m_oldWidth != m_width
  305. || m_oldHeight != m_height
  306. || m_oldReset != m_reset)
  307. {
  308. // Recreate variable size render targets when resolution changes.
  309. m_oldWidth = m_width;
  310. m_oldHeight = m_height;
  311. m_oldReset = m_reset;
  312. }
  313. imguiBeginFrame(m_mouseState.m_mx
  314. , m_mouseState.m_my
  315. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  316. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  317. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  318. , m_mouseState.m_mz
  319. , uint16_t(m_width)
  320. , uint16_t(m_height)
  321. );
  322. showExampleDialog(this);
  323. ImGui::SetNextWindowPos(
  324. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  325. , ImGuiCond_FirstUseEver
  326. );
  327. ImGui::SetNextWindowSize(
  328. ImVec2(m_width / 5.0f, m_height * 0.75f)
  329. , ImGuiCond_FirstUseEver
  330. );
  331. ImGui::Begin("Settings"
  332. , NULL
  333. , 0
  334. );
  335. ImGui::Separator();
  336. ImGui::Text("Draw mode:");
  337. ImGui::RadioButton("Wireframe + Shaded", &m_drawMode, 0);
  338. ImGui::RadioButton("Wireframe", &m_drawMode, 1);
  339. ImGui::RadioButton("Shaded", &m_drawMode, 2);
  340. const bool wfEnabled = (DrawMode::Shaded != m_drawMode);
  341. if ( wfEnabled )
  342. {
  343. ImGui::Separator();
  344. ImGui::ColorWheel("Color", m_uniforms.m_wfColor, 0.6f);
  345. ImGui::SliderFloat("Thickness", &m_uniforms.m_wfThickness, 0.6f, 2.2f);
  346. }
  347. ImGui::Separator();
  348. ImGui::Text("Mesh:");
  349. {
  350. bool meshChanged = false;
  351. meshChanged |= ImGui::RadioButton("Bunny", &m_meshSelection, 0);
  352. meshChanged |= ImGui::RadioButton("Hollowcubes", &m_meshSelection, 1);
  353. meshChanged |= ImGui::RadioButton("Orb", &m_meshSelection, 2);
  354. if (meshChanged)
  355. {
  356. m_camera.reset();
  357. }
  358. }
  359. ImGui::End();
  360. imguiEndFrame();
  361. // This dummy draw call is here to make sure that view 0 is cleared
  362. // if no other draw calls are submitted to view 0.
  363. bgfx::touch(0);
  364. int64_t now = bx::getHPCounter();
  365. static int64_t last = now;
  366. const int64_t frameTime = now - last;
  367. last = now;
  368. const double freq = double(bx::getHPFrequency() );
  369. const float deltaTimeSec = float(double(frameTime)/freq);
  370. // Setup view.
  371. bgfx::setViewRect(0, 0, 0, bgfx::BackbufferRatio::Equal);
  372. bgfx::setViewClear(0, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
  373. const bool mouseOverGui = ImGui::MouseOverArea();
  374. m_mouse.update(float(m_mouseState.m_mx), float(m_mouseState.m_my), m_mouseState.m_mz, m_width, m_height);
  375. if (!mouseOverGui)
  376. {
  377. if (m_mouseState.m_buttons[entry::MouseButton::Left])
  378. {
  379. m_camera.orbit(m_mouse.m_dx, m_mouse.m_dy);
  380. }
  381. else if (m_mouseState.m_buttons[entry::MouseButton::Right])
  382. {
  383. m_camera.dolly(m_mouse.m_dx + m_mouse.m_dy);
  384. }
  385. else if (0 != m_mouse.m_scroll)
  386. {
  387. m_camera.dolly(float(m_mouse.m_scroll)*0.1f);
  388. }
  389. }
  390. float view[16];
  391. float proj[16];
  392. m_camera.update(deltaTimeSec);
  393. bx::memCopy(m_uniforms.m_camPos, m_camera.m_pos.curr, 3*sizeof(float));
  394. m_camera.mtxLookAt(view);
  395. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  396. bgfx::setViewTransform(0, view, proj);
  397. m_uniforms.m_drawEdges = (DrawMode::WireframeShaded == m_drawMode) ? 1.0f : 0.0f;
  398. m_uniforms.submit();
  399. if (DrawMode::Wireframe == m_drawMode)
  400. {
  401. uint64_t state = 0
  402. | BGFX_STATE_RGB_WRITE
  403. | BGFX_STATE_ALPHA_WRITE
  404. | BGFX_STATE_DEPTH_WRITE
  405. | BGFX_STATE_CULL_CCW
  406. | BGFX_STATE_MSAA
  407. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  408. ;
  409. meshSubmit(m_meshes[m_meshSelection].m_mesh, 0, m_wfProgram, m_meshes[m_meshSelection].m_mtx, state);
  410. }
  411. else
  412. {
  413. uint64_t state = 0
  414. | BGFX_STATE_RGB_WRITE
  415. | BGFX_STATE_ALPHA_WRITE
  416. | BGFX_STATE_DEPTH_TEST_LESS
  417. | BGFX_STATE_DEPTH_WRITE
  418. | BGFX_STATE_CULL_CCW
  419. | BGFX_STATE_MSAA
  420. ;
  421. meshSubmit(m_meshes[m_meshSelection].m_mesh, 0, m_meshProgram, m_meshes[m_meshSelection].m_mtx, state);
  422. }
  423. // Advance to next frame. Rendering thread will be kicked to
  424. // process submitted rendering primitives.
  425. bgfx::frame();
  426. return true;
  427. }
  428. return false;
  429. }
  430. entry::MouseState m_mouseState;
  431. bgfx::ProgramHandle m_wfProgram;
  432. bgfx::ProgramHandle m_meshProgram;
  433. uint32_t m_width;
  434. uint32_t m_height;
  435. uint32_t m_debug;
  436. uint32_t m_reset;
  437. uint32_t m_oldWidth;
  438. uint32_t m_oldHeight;
  439. uint32_t m_oldReset;
  440. Camera m_camera;
  441. Mouse m_mouse;
  442. Uniforms m_uniforms;
  443. MeshMtx m_meshes[3];
  444. int32_t m_meshSelection;
  445. int32_t m_drawMode; // Holds data for 'DrawMode'.
  446. };
  447. } // namespace
  448. ENTRY_IMPLEMENT_MAIN(ExampleWireframe, "28-wirefame", "Drawing wireframe mesh.");