wireframe.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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::clamp(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::min(_dt/0.12f, 1.0f);
  121. consumeOrbit(amount);
  122. m_target.curr[0] = bx::lerp(m_target.curr[0], m_target.dest[0], amount);
  123. m_target.curr[1] = bx::lerp(m_target.curr[1], m_target.dest[1], amount);
  124. m_target.curr[2] = bx::lerp(m_target.curr[2], m_target.dest[2], amount);
  125. m_pos.curr[0] = bx::lerp(m_pos.curr[0], m_pos.dest[0], amount);
  126. m_pos.curr[1] = bx::lerp(m_pos.curr[1], m_pos.dest[1], amount);
  127. m_pos.curr[2] = bx::lerp(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 init;
  262. init.type = args.m_type;
  263. init.vendorId = args.m_pciId;
  264. init.resolution.width = m_width;
  265. init.resolution.height = m_height;
  266. init.resolution.reset = m_reset;
  267. bgfx::init(init);
  268. // Enable m_debug text.
  269. bgfx::setDebug(m_debug);
  270. // Set view 0 clear state.
  271. bgfx::setViewClear(0
  272. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  273. , 0x303030ff
  274. , 1.0f
  275. , 0
  276. );
  277. m_wfProgram = loadProgram("vs_wf_wireframe", "fs_wf_wireframe");
  278. m_meshProgram = loadProgram("vs_wf_mesh", "fs_wf_mesh");
  279. m_uniforms.init();
  280. m_meshes[0].init("meshes/bunny.bin", 1.0f, 0.0f, bx::kPi, 0.0f, 0.0f, -0.8f, 0.0f);
  281. m_meshes[1].init("meshes/hollowcube.bin", 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
  282. m_meshes[2].init("meshes/orb.bin", 1.2f, 0.0f, 0.0f, 0.0f, 0.0f, -0.65f, 0.0f);
  283. // Imgui.
  284. imguiCreate();
  285. m_oldWidth = 0;
  286. m_oldHeight = 0;
  287. m_oldReset = m_reset;
  288. m_meshSelection = 1;
  289. m_drawMode = DrawMode::WireframeShaded;
  290. }
  291. virtual int shutdown() override
  292. {
  293. // Cleanup.
  294. imguiDestroy();
  295. m_meshes[0].destroy();
  296. m_meshes[1].destroy();
  297. m_meshes[2].destroy();
  298. bgfx::destroy(m_wfProgram);
  299. bgfx::destroy(m_meshProgram);
  300. m_uniforms.destroy();
  301. // Shutdown bgfx.
  302. bgfx::shutdown();
  303. return 0;
  304. }
  305. bool update() override
  306. {
  307. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  308. {
  309. if (m_oldWidth != m_width
  310. || m_oldHeight != m_height
  311. || m_oldReset != m_reset)
  312. {
  313. // Recreate variable size render targets when resolution changes.
  314. m_oldWidth = m_width;
  315. m_oldHeight = m_height;
  316. m_oldReset = m_reset;
  317. }
  318. imguiBeginFrame(m_mouseState.m_mx
  319. , m_mouseState.m_my
  320. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  321. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  322. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  323. , m_mouseState.m_mz
  324. , uint16_t(m_width)
  325. , uint16_t(m_height)
  326. );
  327. showExampleDialog(this);
  328. ImGui::SetNextWindowPos(
  329. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  330. , ImGuiCond_FirstUseEver
  331. );
  332. ImGui::SetNextWindowSize(
  333. ImVec2(m_width / 5.0f, m_height * 0.75f)
  334. , ImGuiCond_FirstUseEver
  335. );
  336. ImGui::Begin("Settings"
  337. , NULL
  338. , 0
  339. );
  340. ImGui::Separator();
  341. ImGui::Text("Draw mode:");
  342. ImGui::RadioButton("Wireframe + Shaded", &m_drawMode, 0);
  343. ImGui::RadioButton("Wireframe", &m_drawMode, 1);
  344. ImGui::RadioButton("Shaded", &m_drawMode, 2);
  345. const bool wfEnabled = (DrawMode::Shaded != m_drawMode);
  346. if ( wfEnabled )
  347. {
  348. ImGui::Separator();
  349. ImGui::ColorWheel("Color", m_uniforms.m_wfColor, 0.6f);
  350. ImGui::SliderFloat("Thickness", &m_uniforms.m_wfThickness, 0.6f, 2.2f);
  351. }
  352. ImGui::Separator();
  353. ImGui::Text("Mesh:");
  354. {
  355. bool meshChanged = false;
  356. meshChanged |= ImGui::RadioButton("Bunny", &m_meshSelection, 0);
  357. meshChanged |= ImGui::RadioButton("Hollowcubes", &m_meshSelection, 1);
  358. meshChanged |= ImGui::RadioButton("Orb", &m_meshSelection, 2);
  359. if (meshChanged)
  360. {
  361. m_camera.reset();
  362. }
  363. }
  364. ImGui::End();
  365. imguiEndFrame();
  366. // This dummy draw call is here to make sure that view 0 is cleared
  367. // if no other draw calls are submitted to view 0.
  368. bgfx::touch(0);
  369. int64_t now = bx::getHPCounter();
  370. static int64_t last = now;
  371. const int64_t frameTime = now - last;
  372. last = now;
  373. const double freq = double(bx::getHPFrequency() );
  374. const float deltaTimeSec = float(double(frameTime)/freq);
  375. // Setup view.
  376. bgfx::setViewRect(0, 0, 0, bgfx::BackbufferRatio::Equal);
  377. bgfx::setViewClear(0, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
  378. const bool mouseOverGui = ImGui::MouseOverArea();
  379. m_mouse.update(float(m_mouseState.m_mx), float(m_mouseState.m_my), m_mouseState.m_mz, m_width, m_height);
  380. if (!mouseOverGui)
  381. {
  382. if (m_mouseState.m_buttons[entry::MouseButton::Left])
  383. {
  384. m_camera.orbit(m_mouse.m_dx, m_mouse.m_dy);
  385. }
  386. else if (m_mouseState.m_buttons[entry::MouseButton::Right])
  387. {
  388. m_camera.dolly(m_mouse.m_dx + m_mouse.m_dy);
  389. }
  390. else if (0 != m_mouse.m_scroll)
  391. {
  392. m_camera.dolly(float(m_mouse.m_scroll)*0.1f);
  393. }
  394. }
  395. float view[16];
  396. float proj[16];
  397. m_camera.update(deltaTimeSec);
  398. bx::memCopy(m_uniforms.m_camPos, m_camera.m_pos.curr, 3*sizeof(float));
  399. m_camera.mtxLookAt(view);
  400. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  401. bgfx::setViewTransform(0, view, proj);
  402. m_uniforms.m_drawEdges = (DrawMode::WireframeShaded == m_drawMode) ? 1.0f : 0.0f;
  403. m_uniforms.submit();
  404. if (DrawMode::Wireframe == m_drawMode)
  405. {
  406. uint64_t state = 0
  407. | BGFX_STATE_WRITE_RGB
  408. | BGFX_STATE_WRITE_A
  409. | BGFX_STATE_WRITE_Z
  410. | BGFX_STATE_CULL_CCW
  411. | BGFX_STATE_MSAA
  412. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  413. ;
  414. meshSubmit(m_meshes[m_meshSelection].m_mesh, 0, m_wfProgram, m_meshes[m_meshSelection].m_mtx, state);
  415. }
  416. else
  417. {
  418. uint64_t state = 0
  419. | BGFX_STATE_WRITE_RGB
  420. | BGFX_STATE_WRITE_A
  421. | BGFX_STATE_DEPTH_TEST_LESS
  422. | BGFX_STATE_WRITE_Z
  423. | BGFX_STATE_CULL_CCW
  424. | BGFX_STATE_MSAA
  425. ;
  426. meshSubmit(m_meshes[m_meshSelection].m_mesh, 0, m_meshProgram, m_meshes[m_meshSelection].m_mtx, state);
  427. }
  428. // Advance to next frame. Rendering thread will be kicked to
  429. // process submitted rendering primitives.
  430. bgfx::frame();
  431. return true;
  432. }
  433. return false;
  434. }
  435. entry::MouseState m_mouseState;
  436. bgfx::ProgramHandle m_wfProgram;
  437. bgfx::ProgramHandle m_meshProgram;
  438. uint32_t m_width;
  439. uint32_t m_height;
  440. uint32_t m_debug;
  441. uint32_t m_reset;
  442. uint32_t m_oldWidth;
  443. uint32_t m_oldHeight;
  444. uint32_t m_oldReset;
  445. Camera m_camera;
  446. Mouse m_mouse;
  447. Uniforms m_uniforms;
  448. MeshMtx m_meshes[3];
  449. int32_t m_meshSelection;
  450. int32_t m_drawMode; // Holds data for 'DrawMode'.
  451. };
  452. } // namespace
  453. ENTRY_IMPLEMENT_MAIN(ExampleWireframe, "28-wirefame", "Drawing wireframe mesh.");