wireframe.cpp 12 KB

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