geometryv.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. /*
  2. * Copyright 2019-2019 Attila Kocsis. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "common.h"
  6. #include <bgfx/bgfx.h>
  7. #include <bx/commandline.h>
  8. #include <bx/easing.h>
  9. #include <bx/file.h>
  10. #include <bx/math.h>
  11. #include <bx/os.h>
  12. #include <bx/settings.h>
  13. #include <entry/entry.h>
  14. #include <entry/input.h>
  15. #include <entry/cmd.h>
  16. #include <entry/dialog.h>
  17. #include <imgui/imgui.h>
  18. #include <bgfx_utils.h>
  19. #include <tinystl/allocator.h>
  20. #include <tinystl/vector.h>
  21. namespace stl = tinystl;
  22. #include <string>
  23. #include <algorithm>
  24. #include <bgfx/embedded_shader.h>
  25. #include "vs_mesh.bin.h"
  26. #include "fs_mesh.bin.h"
  27. #define SCENE_VIEW_ID 0
  28. #define BGFX_GEOMETRYV_VERSION_MAJOR 1
  29. #define BGFX_GEOMETRYV_VERSION_MINOR 0
  30. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  31. {
  32. BGFX_EMBEDDED_SHADER(vs_mesh),
  33. BGFX_EMBEDDED_SHADER(fs_mesh),
  34. BGFX_EMBEDDED_SHADER_END()
  35. };
  36. static const char* s_supportedExt[] =
  37. {
  38. "bin",
  39. };
  40. struct Binding
  41. {
  42. enum Enum
  43. {
  44. App,
  45. View,
  46. Help,
  47. About,
  48. Count
  49. };
  50. };
  51. static const InputBinding s_bindingApp[] =
  52. {
  53. { entry::Key::KeyQ, entry::Modifier::None, 1, NULL, "exit" },
  54. { entry::Key::KeyF, entry::Modifier::None, 1, NULL, "graphics fullscreen" },
  55. INPUT_BINDING_END
  56. };
  57. const char* s_resetCmd =
  58. "view dolly\n"
  59. "view orbit\n"
  60. ;
  61. static const InputBinding s_bindingView[] =
  62. {
  63. { entry::Key::Esc, entry::Modifier::None, 1, NULL, "exit" },
  64. { entry::Key::Key1, entry::Modifier::None, 1, NULL, "view dolly" },
  65. { entry::Key::Key0, entry::Modifier::None, 1, NULL, s_resetCmd },
  66. { entry::Key::Plus, entry::Modifier::None, 1, NULL, "view dolly +0.1" },
  67. { entry::Key::Minus, entry::Modifier::None, 1, NULL, "view dolly -0.1" },
  68. { entry::Key::KeyW, entry::Modifier::None, 1, NULL, "view orbit y -0.1" },
  69. { entry::Key::KeyS, entry::Modifier::None, 1, NULL, "view orbit y +0.1" },
  70. { entry::Key::KeyA, entry::Modifier::None, 1, NULL, "view orbit x +0.1" },
  71. { entry::Key::KeyD, entry::Modifier::None, 1, NULL, "view orbit x -0.1" },
  72. { entry::Key::Up, entry::Modifier::None, 1, NULL, "view file-up" },
  73. { entry::Key::Down, entry::Modifier::None, 1, NULL, "view file-down" },
  74. { entry::Key::KeyI, entry::Modifier::None, 1, NULL, "view info" },
  75. { entry::Key::KeyH, entry::Modifier::None, 1, NULL, "view help" },
  76. { entry::Key::Return, entry::Modifier::None, 1, NULL, "view files" },
  77. { entry::Key::Space, entry::Modifier::None, 1, NULL, "view geo\n" },
  78. INPUT_BINDING_END
  79. };
  80. static const InputBinding s_bindingHelp[] =
  81. {
  82. { entry::Key::Esc, entry::Modifier::None, 1, NULL, "view help" },
  83. { entry::Key::KeyH, entry::Modifier::None, 1, NULL, "view help" },
  84. INPUT_BINDING_END
  85. };
  86. static const InputBinding s_bindingAbout[] =
  87. {
  88. { entry::Key::Esc, entry::Modifier::None, 1, NULL, "view about" },
  89. INPUT_BINDING_END
  90. };
  91. static const char* s_bindingName[] =
  92. {
  93. "App",
  94. "View",
  95. "Help",
  96. "About",
  97. };
  98. BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_bindingName) );
  99. static const InputBinding* s_binding[] =
  100. {
  101. s_bindingApp,
  102. s_bindingView,
  103. s_bindingHelp,
  104. s_bindingAbout,
  105. };
  106. BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_binding) );
  107. static const char* s_filter = ""
  108. "Bgfx geometry (bin) | *.bin\n"
  109. ;
  110. struct Camera
  111. {
  112. Camera()
  113. {
  114. init(bx::Vec3(0.0f,0.0f,0.0f), 2.0f);
  115. }
  116. void init(const bx::Vec3& _center, float _distance)
  117. {
  118. m_target.curr = _center;
  119. m_target.dest = _center;
  120. m_pos.curr = _center;
  121. m_pos.curr.z -= _distance;
  122. m_pos.dest = _center;
  123. m_pos.dest.z -= _distance;
  124. m_orbit[0] = 0.0f;
  125. m_orbit[1] = 0.0f;
  126. }
  127. void mtxLookAt(float* _outViewMtx)
  128. {
  129. bx::mtxLookAt(_outViewMtx, m_pos.curr, m_target.curr);
  130. }
  131. void orbit(float _dx, float _dy)
  132. {
  133. m_orbit[0] += _dx;
  134. m_orbit[1] += _dy;
  135. }
  136. void distance(float _z)
  137. {
  138. const float cnear = 1.0f;
  139. const float cfar = 100.0f;
  140. _z = bx::clamp(_z, cnear, cfar);
  141. bx::Vec3 toTarget = bx::sub(m_target.dest, m_pos.dest);
  142. bx::Vec3 toTargetNorm = bx::normalize(toTarget);
  143. m_pos.dest = bx::mad(toTargetNorm, -_z, m_target.dest);
  144. }
  145. void dolly(float _dz)
  146. {
  147. const float cnear = 1.0f;
  148. const float cfar = 100.0f;
  149. const bx::Vec3 toTarget = bx::sub(m_target.dest, m_pos.dest);
  150. const float toTargetLen = bx::length(toTarget);
  151. const float invToTargetLen = 1.0f / (toTargetLen + bx::kFloatMin);
  152. const bx::Vec3 toTargetNorm = bx::mul(toTarget, invToTargetLen);
  153. float delta = toTargetLen * _dz;
  154. float newLen = toTargetLen + delta;
  155. if ( (cnear < newLen || _dz < 0.0f)
  156. && (newLen < cfar || _dz > 0.0f) )
  157. {
  158. m_pos.dest = bx::mad(toTargetNorm, delta, m_pos.dest);
  159. }
  160. }
  161. void consumeOrbit(float _amount)
  162. {
  163. float consume[2];
  164. consume[0] = m_orbit[0] * _amount;
  165. consume[1] = m_orbit[1] * _amount;
  166. m_orbit[0] -= consume[0];
  167. m_orbit[1] -= consume[1];
  168. const bx::Vec3 toPos = bx::sub(m_pos.curr, m_target.curr);
  169. const float toPosLen = bx::length(toPos);
  170. const float invToPosLen = 1.0f / (toPosLen + bx::kFloatMin);
  171. const bx::Vec3 toPosNorm = bx::mul(toPos, invToPosLen);
  172. float ll[2];
  173. bx::toLatLong(&ll[0], &ll[1], toPosNorm);
  174. ll[0] += consume[0];
  175. ll[1] -= consume[1];
  176. ll[1] = bx::clamp(ll[1], 0.02f, 0.98f);
  177. const bx::Vec3 tmp = bx::fromLatLong(ll[0], ll[1]);
  178. const bx::Vec3 diff = bx::mul(bx::sub(tmp, toPosNorm), toPosLen);
  179. m_pos.curr = bx::add(m_pos.curr, diff);
  180. m_pos.dest = bx::add(m_pos.dest, diff);
  181. }
  182. void update(float _dt)
  183. {
  184. const float amount = bx::min(_dt / 0.12f, 1.0f);
  185. consumeOrbit(amount);
  186. m_target.curr = bx::lerp(m_target.curr, m_target.dest, amount);
  187. m_pos.curr = bx::lerp(m_pos.curr, m_pos.dest, amount);
  188. }
  189. struct Interp3f
  190. {
  191. bx::Vec3 curr;
  192. bx::Vec3 dest;
  193. };
  194. Interp3f m_target;
  195. Interp3f m_pos;
  196. float m_orbit[2];
  197. };
  198. struct Mouse
  199. {
  200. Mouse()
  201. {
  202. m_dx = 0.0f;
  203. m_dy = 0.0f;
  204. m_prevMx = 0.0f;
  205. m_prevMx = 0.0f;
  206. m_scroll = 0;
  207. m_scrollPrev = 0;
  208. }
  209. void update(float _mx, float _my, int32_t _mz, uint32_t _width, uint32_t _height)
  210. {
  211. const float widthf = float(int32_t(_width));
  212. const float heightf = float(int32_t(_height));
  213. // Delta movement.
  214. m_dx = float(_mx - m_prevMx)/widthf;
  215. m_dy = float(_my - m_prevMy)/heightf;
  216. m_prevMx = _mx;
  217. m_prevMy = _my;
  218. // Scroll.
  219. m_scroll = _mz - m_scrollPrev;
  220. m_scrollPrev = _mz;
  221. }
  222. float m_dx; // Screen space.
  223. float m_dy;
  224. float m_prevMx;
  225. float m_prevMy;
  226. int32_t m_scroll;
  227. int32_t m_scrollPrev;
  228. };
  229. struct View
  230. {
  231. View()
  232. : m_fileIndex(0)
  233. , m_width(1280)
  234. , m_height(720)
  235. , m_help(false)
  236. , m_about(false)
  237. , m_info(false)
  238. , m_files(false)
  239. , m_meshCenter(0.0f,0.0f,0.0f)
  240. , m_meshRadius(1.0f)
  241. , m_idleTimer(0.0f)
  242. {
  243. load();
  244. }
  245. ~View()
  246. {
  247. }
  248. int32_t cmd(int32_t _argc, char const* const* _argv)
  249. {
  250. if (_argc >= 2)
  251. {
  252. if (0 == bx::strCmp(_argv[1], "file-up") )
  253. {
  254. m_fileIndex = bx::uint32_satsub(m_fileIndex, 1);
  255. }
  256. else if (0 == bx::strCmp(_argv[1], "file-down") )
  257. {
  258. uint32_t numFiles = bx::uint32_satsub(uint32_t(m_fileList.size() ), 1);
  259. ++m_fileIndex;
  260. m_fileIndex = bx::uint32_min(m_fileIndex, numFiles);
  261. }
  262. else if (0 == bx::strCmp(_argv[1], "help") )
  263. {
  264. m_help ^= true;
  265. }
  266. else if (0 == bx::strCmp(_argv[1], "about") )
  267. {
  268. m_about ^= true;
  269. }
  270. else if (0 == bx::strCmp(_argv[1], "save") )
  271. {
  272. save();
  273. }
  274. else if (0 == bx::strCmp(_argv[1], "info") )
  275. {
  276. m_info ^= true;
  277. }
  278. else if (0 == bx::strCmp(_argv[1], "files") )
  279. {
  280. m_files ^= true;
  281. }
  282. else if (0 == bx::strCmp(_argv[1], "dolly") )
  283. {
  284. if (_argc >= 3)
  285. {
  286. float dolly;
  287. bx::fromString(&dolly, _argv[2]);
  288. if (_argv[2][0] == '+'
  289. || _argv[2][0] == '-')
  290. {
  291. m_camera.dolly(dolly);
  292. m_idleTimer = 0.0f;
  293. }
  294. }
  295. else
  296. {
  297. m_camera.distance(m_meshRadius * 2.0f);
  298. }
  299. }
  300. else if (0 == bx::strCmp(_argv[1], "orbit") )
  301. {
  302. if (_argc >= 4)
  303. {
  304. int axis = (_argv[2][0] == 'x' ? 0 : 1);
  305. float orbit[2] = { 0.0f, 0.0f};
  306. bx::fromString(&orbit[axis], _argv[3]);
  307. m_camera.orbit(orbit[0], orbit[1]);
  308. m_idleTimer = 0.0f;
  309. }
  310. else
  311. {
  312. m_camera.m_target.dest = m_meshCenter;
  313. m_camera.m_pos.dest = m_meshCenter;
  314. m_camera.m_pos.dest.z -= m_meshRadius * 2.0f;
  315. m_camera.m_orbit[0] = 0.0f;
  316. m_camera.m_orbit[1] = 0.0f;
  317. }
  318. }
  319. }
  320. return 0;
  321. }
  322. static bool sortNameAscending(const std::string& _lhs, const std::string& _rhs)
  323. {
  324. return 0 > bx::strCmpV(_lhs.c_str(), _rhs.c_str() );
  325. }
  326. void updateFileList(const bx::FilePath& _filePath)
  327. {
  328. bx::DirectoryReader dr;
  329. if (bx::open(&dr, _filePath) )
  330. {
  331. m_path = _filePath;
  332. }
  333. else if (bx::open(&dr, _filePath.getPath() ) )
  334. {
  335. m_path = _filePath.getPath();
  336. }
  337. else
  338. {
  339. DBG("File path `%s` not found.", _filePath.getCPtr() );
  340. return;
  341. }
  342. bx::Error err;
  343. m_fileList.clear();
  344. while (err.isOk() )
  345. {
  346. bx::FileInfo fi;
  347. bx::read(&dr, fi, &err);
  348. if (err.isOk()
  349. && bx::FileType::File == fi.type)
  350. {
  351. bx::StringView ext = fi.filePath.getExt();
  352. if (!ext.isEmpty() )
  353. {
  354. ext.set(ext.getPtr()+1, ext.getTerm() );
  355. bool supported = false;
  356. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  357. {
  358. const bx::StringView supportedExt(s_supportedExt[ii]);
  359. if (0 == bx::strCmpI(bx::max(ext.getPtr(), ext.getTerm() - supportedExt.getLength() ), supportedExt) )
  360. {
  361. supported = true;
  362. break;
  363. }
  364. }
  365. if (supported)
  366. {
  367. const bx::StringView fileName = fi.filePath.getFileName();
  368. m_fileList.push_back(std::string(fileName.getPtr(), fileName.getTerm() ) );
  369. }
  370. }
  371. }
  372. }
  373. bx::close(&dr);
  374. std::sort(m_fileList.begin(), m_fileList.end(), sortNameAscending);
  375. m_fileIndex = 0;
  376. uint32_t idx = 0;
  377. const bx::StringView fileName = _filePath.getFileName();
  378. for (FileList::const_iterator it = m_fileList.begin(); it != m_fileList.end(); ++it, ++idx)
  379. {
  380. if (0 == bx::strCmpI(it->c_str(), fileName) )
  381. {
  382. // If it is case-insensitive match then might be correct one, but keep
  383. // searching.
  384. m_fileIndex = idx;
  385. if (0 == bx::strCmp(it->c_str(), fileName) )
  386. {
  387. // If it is exact match we're done.
  388. break;
  389. }
  390. }
  391. }
  392. }
  393. void load()
  394. {
  395. bx::FilePath filePath(bx::Dir::Home);
  396. filePath.join(".config/bgfx/geometryv.ini");
  397. bx::Settings settings(entry::getAllocator() );
  398. bx::FileReader reader;
  399. if (bx::open(&reader, filePath) )
  400. {
  401. bx::read(&reader, settings);
  402. bx::close(&reader);
  403. if (!bx::fromString(&m_width, settings.get("view/width") ) )
  404. {
  405. m_width = 1280;
  406. }
  407. if (!bx::fromString(&m_height, settings.get("view/height") ) )
  408. {
  409. m_height = 720;
  410. }
  411. }
  412. }
  413. void save()
  414. {
  415. bx::FilePath filePath(bx::Dir::Home);
  416. filePath.join(".config/bgfx/geometryv.ini");
  417. if (bx::makeAll(filePath.getPath() ) )
  418. {
  419. bx::Settings settings(entry::getAllocator() );
  420. char tmp[256];
  421. bx::toString(tmp, sizeof(tmp), m_width);
  422. settings.set("view/width", tmp);
  423. bx::toString(tmp, sizeof(tmp), m_height);
  424. settings.set("view/height", tmp);
  425. bx::FileWriter writer;
  426. if (bx::open(&writer, filePath) )
  427. {
  428. bx::write(&writer, settings);
  429. bx::close(&writer);
  430. }
  431. }
  432. }
  433. bx::FilePath m_path;
  434. typedef stl::vector<std::string> FileList;
  435. FileList m_fileList;
  436. uint32_t m_fileIndex;
  437. uint32_t m_width;
  438. uint32_t m_height;
  439. bool m_help;
  440. bool m_about;
  441. bool m_info;
  442. bool m_files;
  443. Camera m_camera;
  444. Mouse m_mouse;
  445. bx::Vec3 m_meshCenter;
  446. float m_meshRadius;
  447. float m_idleTimer;
  448. };
  449. int cmdView(CmdContext* /*_context*/, void* _userData, int _argc, char const* const* _argv)
  450. {
  451. View* view = static_cast<View*>(_userData);
  452. return view->cmd(_argc, _argv);
  453. }
  454. template<bx::LerpFn lerpT, bx::EaseFn easeT>
  455. struct InterpolatorT
  456. {
  457. float from;
  458. float to;
  459. float duration;
  460. int64_t offset;
  461. InterpolatorT(float _value)
  462. {
  463. reset(_value);
  464. }
  465. void reset(float _value)
  466. {
  467. from = _value;
  468. to = _value;
  469. duration = 0.0;
  470. offset = bx::getHPCounter();
  471. }
  472. void set(float _value, float _duration)
  473. {
  474. if (_value != to)
  475. {
  476. from = getValue();
  477. to = _value;
  478. duration = _duration;
  479. offset = bx::getHPCounter();
  480. }
  481. }
  482. float getValue()
  483. {
  484. if (isActive() )
  485. {
  486. const double freq = double(bx::getHPFrequency() );
  487. int64_t now = bx::getHPCounter();
  488. float time = (float)(double(now - offset) / freq);
  489. float lerp = bx::clamp(time, 0.0f, duration) / duration;
  490. return lerpT(from, to, easeT(lerp) );
  491. }
  492. return to;
  493. }
  494. bool isActive() const
  495. {
  496. const double freq = double(bx::getHPFrequency() );
  497. int64_t now = bx::getHPCounter();
  498. float time = (float)(double(now - offset) / freq);
  499. float lerp = bx::clamp(time, 0.0f, duration) / duration;
  500. return lerp < 1.0f;
  501. }
  502. };
  503. typedef InterpolatorT<bx::lerp, bx::easeInOutQuad> Interpolator;
  504. void keyBindingHelp(const char* _bindings, const char* _description)
  505. {
  506. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), _bindings);
  507. ImGui::SameLine(140);
  508. ImGui::Text(_description);
  509. }
  510. void help(const char* _error = NULL)
  511. {
  512. if (NULL != _error)
  513. {
  514. fprintf(stderr, "Error:\n%s\n\n", _error);
  515. }
  516. fprintf(stderr
  517. , "geometryv, bgfx geometry viewer tool, version %d.%d.%d.\n"
  518. "Copyright 2019-2019 Attila Kocsis. All rights reserved.\n"
  519. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  520. , BGFX_GEOMETRYV_VERSION_MAJOR
  521. , BGFX_GEOMETRYV_VERSION_MINOR
  522. , BGFX_API_VERSION
  523. );
  524. fprintf(stderr
  525. , "Usage: geometryv <file path>\n"
  526. "\n"
  527. "Supported input file types:\n"
  528. );
  529. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  530. {
  531. fprintf(stderr, " *.%s\n", s_supportedExt[ii]);
  532. }
  533. fprintf(stderr
  534. , "\n"
  535. "Options:\n"
  536. " -h, --help Help.\n"
  537. " -v, --version Version information only.\n"
  538. "\n"
  539. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  540. );
  541. }
  542. int _main_(int _argc, char** _argv)
  543. {
  544. bx::CommandLine cmdLine(_argc, _argv);
  545. if (cmdLine.hasArg('v', "version") )
  546. {
  547. fprintf(stderr
  548. , "geometryv, bgfx geometry viewer tool, version %d.%d.%d.\n"
  549. , BGFX_GEOMETRYV_VERSION_MAJOR
  550. , BGFX_GEOMETRYV_VERSION_MINOR
  551. , BGFX_API_VERSION
  552. );
  553. return bx::kExitSuccess;
  554. }
  555. if (cmdLine.hasArg('h', "help") )
  556. {
  557. help();
  558. return bx::kExitFailure;
  559. }
  560. uint32_t debug = BGFX_DEBUG_TEXT;
  561. inputAddBindings(s_bindingName[Binding::App], s_binding[Binding::App]);
  562. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  563. View view;
  564. cmdAdd("view", cmdView, &view);
  565. entry::setWindowFlags(entry::WindowHandle{0}, ENTRY_WINDOW_FLAG_ASPECT_RATIO, false);
  566. entry::setWindowSize(entry::WindowHandle{0}, view.m_width, view.m_height);
  567. bgfx::Init init;
  568. init.resolution.width = view.m_width;
  569. init.resolution.width = view.m_height;
  570. init.resolution.reset = 0
  571. | BGFX_RESET_VSYNC
  572. | BGFX_RESET_MSAA_X16
  573. ;
  574. bgfx::init(init);
  575. // Set view 0 clear state.
  576. bgfx::setViewClear(SCENE_VIEW_ID
  577. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  578. , 0x000000ff
  579. , 1.0f
  580. , 0
  581. );
  582. imguiCreate();
  583. const bgfx::Caps* caps = bgfx::getCaps();
  584. bgfx::RendererType::Enum type = caps->rendererType;
  585. bgfx::ShaderHandle vsMesh = bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_mesh");
  586. bgfx::ShaderHandle fsMesh = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_mesh");
  587. bgfx::ProgramHandle meshProgram = bgfx::createProgram(
  588. vsMesh
  589. , fsMesh
  590. , true
  591. );
  592. float speed = 0.37f;
  593. float time = 0.0f;
  594. Interpolator menuFade(5.0f);
  595. auto anyActive = [&]() -> bool
  596. {
  597. return false
  598. || ImGui::MouseOverArea()
  599. || menuFade.isActive()
  600. ;
  601. };
  602. const char* filePath = _argc < 2 ? "" : _argv[1];
  603. std::string path = filePath;
  604. {
  605. bx::FilePath fp(filePath);
  606. view.updateFileList(fp);
  607. }
  608. int exitcode = bx::kExitSuccess;
  609. Mesh* mesh = NULL;
  610. {
  611. uint32_t fileIndex = 0;
  612. entry::WindowState windowState;
  613. while (!entry::processWindowEvents(windowState, debug, init.resolution.reset) )
  614. {
  615. const entry::MouseState& mouseState = windowState.m_mouse;
  616. view.m_width = windowState.m_width;
  617. view.m_height = windowState.m_height;
  618. if (!windowState.m_dropFile.isEmpty() )
  619. {
  620. view.updateFileList(windowState.m_dropFile);
  621. windowState.m_dropFile.clear();
  622. }
  623. imguiBeginFrame(mouseState.m_mx
  624. , mouseState.m_my
  625. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  626. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  627. | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  628. , mouseState.m_mz
  629. , uint16_t(view.m_width)
  630. , uint16_t(view.m_height)
  631. );
  632. bool modalWindow = view.m_help || view.m_about;
  633. bool overArea = false
  634. || ImGui::GetMousePos().y <= ImGui::GetTextLineHeightWithSpacing()
  635. || ImGui::MouseOverArea()
  636. ;
  637. overArea &= !modalWindow;
  638. if (overArea)
  639. {
  640. menuFade.set(5.0f, 0.25f);
  641. }
  642. else if (modalWindow)
  643. {
  644. menuFade.reset(0.0f);
  645. }
  646. else
  647. {
  648. menuFade.set(0.0f, 2.0f);
  649. }
  650. ImGui::PushStyleVar(ImGuiStyleVar_Alpha, bx::clamp(menuFade.getValue(), 0.0f, 1.0f) );
  651. ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 0.0f);
  652. if (ImGui::BeginMainMenuBar() )
  653. {
  654. if (ImGui::BeginMenu("File"))
  655. {
  656. if (ImGui::MenuItem("Open File") )
  657. {
  658. bx::FilePath tmp = view.m_path;
  659. if (openFileSelectionDialog(
  660. tmp
  661. , FileSelectionDialogType::Open
  662. , "geometryv: Open File"
  663. , s_filter
  664. ) )
  665. {
  666. view.updateFileList(tmp);
  667. }
  668. }
  669. if (ImGui::MenuItem("Show File List", NULL, view.m_files) )
  670. {
  671. cmdExec("view files");
  672. }
  673. ImGui::Separator();
  674. if (ImGui::MenuItem("Exit") )
  675. {
  676. cmdExec("exit");
  677. }
  678. ImGui::EndMenu();
  679. }
  680. if (ImGui::BeginMenu("View") )
  681. {
  682. if (ImGui::MenuItem("Info", NULL, view.m_info) )
  683. {
  684. cmdExec("view info");
  685. }
  686. if (ImGui::MenuItem("Reset") )
  687. {
  688. cmdExec(s_resetCmd);
  689. }
  690. ImGui::Separator();
  691. if (ImGui::MenuItem("Save Options") )
  692. {
  693. cmdExec("view save");
  694. }
  695. ImGui::EndMenu();
  696. }
  697. if (ImGui::BeginMenu("Help") )
  698. {
  699. if (ImGui::MenuItem("View Help") )
  700. {
  701. cmdExec("view help");
  702. }
  703. ImGui::Separator();
  704. if (ImGui::MenuItem("About") )
  705. {
  706. cmdExec("view about");
  707. }
  708. ImGui::EndMenu();
  709. }
  710. if (0 != view.m_fileList.size() )
  711. {
  712. ImGui::Separator();
  713. ImGui::TextColored(
  714. ImVec4(0.0f, 1.0f, 1.0f, 1.0f)
  715. , view.m_fileList[view.m_fileIndex].c_str()
  716. );
  717. }
  718. ImGui::EndMainMenuBar();
  719. }
  720. ImGui::PopStyleVar(2);
  721. static bool help = false;
  722. static bool about = false;
  723. view.m_mouse.update(float(mouseState.m_mx), float(mouseState.m_my), mouseState.m_mz, view.m_width, view.m_height);
  724. if (!overArea)
  725. {
  726. if (mouseState.m_buttons[entry::MouseButton::Left])
  727. {
  728. view.m_idleTimer = 0.0f;
  729. view.m_camera.orbit(view.m_mouse.m_dx, view.m_mouse.m_dy);
  730. }
  731. else if (mouseState.m_buttons[entry::MouseButton::Right])
  732. {
  733. view.m_idleTimer = 0.0f;
  734. view.m_camera.dolly(view.m_mouse.m_dx + view.m_mouse.m_dy);
  735. }
  736. else if (0 != view.m_mouse.m_scroll)
  737. {
  738. view.m_idleTimer = 0.0f;
  739. view.m_camera.dolly(float(view.m_mouse.m_scroll)*0.1f);
  740. }
  741. }
  742. if (help != view.m_help)
  743. {
  744. if (!help)
  745. {
  746. ImGui::OpenPopup("Help");
  747. inputRemoveBindings(s_bindingName[Binding::View]);
  748. inputAddBindings(s_bindingName[Binding::Help], s_binding[Binding::Help]);
  749. }
  750. else
  751. {
  752. inputRemoveBindings(s_bindingName[Binding::Help]);
  753. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  754. }
  755. help = view.m_help;
  756. }
  757. if (about != view.m_about)
  758. {
  759. if (!about)
  760. {
  761. ImGui::OpenPopup("About");
  762. inputRemoveBindings(s_bindingName[Binding::View]);
  763. inputAddBindings(s_bindingName[Binding::About], s_binding[Binding::About]);
  764. }
  765. else
  766. {
  767. inputRemoveBindings(s_bindingName[Binding::About]);
  768. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  769. }
  770. about = view.m_about;
  771. }
  772. if (view.m_info)
  773. {
  774. ImGui::SetNextWindowSize(
  775. ImVec2(300.0f, 320.0f)
  776. , ImGuiCond_FirstUseEver
  777. );
  778. if (ImGui::Begin("Info", &view.m_info) )
  779. {
  780. if (ImGui::BeginChild("##info", ImVec2(0.0f, 0.0f) ) )
  781. {
  782. if (NULL == mesh)
  783. {
  784. ImGui::Text("Geometry is not loaded.");
  785. }
  786. else
  787. {
  788. ImGui::Text("Name: %s", view.m_fileList[view.m_fileIndex].c_str() );
  789. ImGui::Indent();
  790. for (GroupArray::const_iterator itGroup = mesh->m_groups.begin(), itGroupEnd = mesh->m_groups.end(); itGroup != itGroupEnd; ++itGroup)
  791. {
  792. ImGui::Text("Group v %d i %d", itGroup->m_numVertices, itGroup->m_numIndices);
  793. ImGui::Indent();
  794. for (PrimitiveArray::const_iterator itPrim = itGroup->m_prims.begin(), itPrimEnd = itGroup->m_prims.end(); itPrim != itPrimEnd; ++itPrim)
  795. {
  796. ImGui::Text("Primitive v %d i %d", itPrim->m_numVertices, itPrim->m_numIndices);
  797. }
  798. ImGui::Unindent();
  799. }
  800. ImGui::Unindent();
  801. ImGui::Separator();
  802. }
  803. ImGui::EndChild();
  804. }
  805. }
  806. ImGui::End();
  807. }
  808. if (view.m_files)
  809. {
  810. char temp[bx::kMaxFilePath];
  811. bx::snprintf(temp, BX_COUNTOF(temp), "%s##File", view.m_path.getCPtr() );
  812. ImGui::SetNextWindowSize(
  813. ImVec2(400.0f, 400.0f)
  814. , ImGuiCond_FirstUseEver
  815. );
  816. if (ImGui::Begin(temp, &view.m_files) )
  817. {
  818. if (ImGui::BeginChild("##file_list", ImVec2(0.0f, 0.0f) ) )
  819. {
  820. ImGui::PushFont(ImGui::Font::Mono);
  821. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  822. const float listHeight =
  823. bx::max(1.0f, bx::floor(ImGui::GetWindowHeight()/itemHeight) )
  824. * itemHeight
  825. ;
  826. ImGui::PushItemWidth(-1);
  827. if (ImGui::ListBoxHeader("##empty", ImVec2(0.0f, listHeight) ) )
  828. {
  829. const int32_t itemCount = int32_t(view.m_fileList.size() );
  830. int32_t start, end;
  831. ImGui::CalcListClipping(itemCount, itemHeight, &start, &end);
  832. const int32_t index = int32_t(view.m_fileIndex);
  833. if (index <= start)
  834. {
  835. ImGui::SetScrollY(ImGui::GetScrollY() - (start-index+1)*itemHeight);
  836. }
  837. else if (index >= end)
  838. {
  839. ImGui::SetScrollY(ImGui::GetScrollY() + (index-end+1)*itemHeight);
  840. }
  841. ImGuiListClipper clipper(itemCount, itemHeight);
  842. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  843. {
  844. ImGui::PushID(pos);
  845. bool isSelected = uint32_t(pos) == view.m_fileIndex;
  846. if (ImGui::Selectable(view.m_fileList[pos].c_str(), &isSelected) )
  847. {
  848. view.m_fileIndex = pos;
  849. }
  850. ImGui::PopID();
  851. }
  852. clipper.End();
  853. ImGui::ListBoxFooter();
  854. }
  855. ImGui::PopFont();
  856. ImGui::EndChild();
  857. }
  858. }
  859. ImGui::End();
  860. }
  861. if (ImGui::BeginPopupModal("About", &view.m_about, ImGuiWindowFlags_AlwaysAutoResize) )
  862. {
  863. ImGui::SetWindowFontScale(1.0f);
  864. ImGui::Text(
  865. "geometryv, bgfx geometry viewer tool " ICON_KI_WRENCH ", version %d.%d.%d.\n"
  866. "Copyright 2019-2019 Attila Kocsis. All rights reserved.\n"
  867. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n"
  868. , BGFX_GEOMETRYV_VERSION_MAJOR
  869. , BGFX_GEOMETRYV_VERSION_MINOR
  870. , BGFX_API_VERSION
  871. );
  872. ImGui::Dummy(ImVec2(0.0f, 0.0f) );
  873. ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
  874. if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
  875. || !view.m_about)
  876. {
  877. view.m_about = false;
  878. ImGui::CloseCurrentPopup();
  879. }
  880. ImGui::EndPopup();
  881. }
  882. if (ImGui::BeginPopupModal("Help", &view.m_help, ImGuiWindowFlags_AlwaysAutoResize) )
  883. {
  884. ImGui::SetWindowFontScale(1.0f);
  885. ImGui::Text("Key bindings:\n\n");
  886. ImGui::PushFont(ImGui::Font::Mono);
  887. keyBindingHelp("ESC", "Exit.");
  888. keyBindingHelp("h", "Toggle help screen.");
  889. keyBindingHelp("i", "Toggle info screen.");
  890. keyBindingHelp("f", "Toggle full-screen.");
  891. ImGui::NextLine();
  892. keyBindingHelp("LMB+drag", "Orbit.");
  893. keyBindingHelp("W/A/S/D", "Orbit.");
  894. keyBindingHelp("RMB+drag or MW", "Dolly.");
  895. keyBindingHelp("=/-", "Dolly.");
  896. keyBindingHelp("0", "Reset.");
  897. keyBindingHelp("1", "Fit to window.");
  898. ImGui::NextLine();
  899. keyBindingHelp("up", "Previous geometry.");
  900. keyBindingHelp("down", "Next geometry.");
  901. ImGui::NextLine();
  902. ImGui::NextLine();
  903. ImGui::PopFont();
  904. ImGui::Dummy(ImVec2(0.0f, 0.0f) );
  905. ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
  906. if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
  907. || !view.m_help)
  908. {
  909. view.m_help = false;
  910. ImGui::CloseCurrentPopup();
  911. }
  912. ImGui::EndPopup();
  913. }
  914. imguiEndFrame();
  915. if ( (NULL == mesh || view.m_fileIndex != fileIndex)
  916. && 0 != view.m_fileList.size() )
  917. {
  918. if (NULL != mesh )
  919. {
  920. meshUnload(mesh);
  921. }
  922. fileIndex = view.m_fileIndex;
  923. bx::FilePath fp = view.m_path;
  924. fp.join(view.m_fileList[view.m_fileIndex].c_str() );
  925. mesh = meshLoad(fp.getCPtr());
  926. std::string title;
  927. if (NULL != mesh )
  928. {
  929. uint32_t numPrimitives = 0;
  930. uint32_t numVertices = 0;
  931. uint32_t numIndices = 0;
  932. Aabb boundingBox = {};
  933. for (GroupArray::const_iterator it = mesh->m_groups.begin(), itEnd = mesh->m_groups.end(); it != itEnd; ++it)
  934. {
  935. if ( it == mesh->m_groups.begin())
  936. {
  937. boundingBox = it->m_aabb;
  938. }
  939. else
  940. {
  941. aabbExpand(boundingBox, it->m_aabb.min);
  942. aabbExpand(boundingBox, it->m_aabb.max);
  943. }
  944. numPrimitives += (uint32_t)it->m_prims.size();
  945. numVertices += (uint32_t)it->m_numVertices;
  946. numIndices += (uint32_t)it->m_numIndices;
  947. }
  948. bx::stringPrintf(title, "%s (g %d, p %d, v %d, i %d)"
  949. , fp.getCPtr()
  950. , mesh->m_groups.size()
  951. , numPrimitives
  952. , numVertices
  953. , numIndices
  954. );
  955. view.m_meshCenter = getCenter(boundingBox);
  956. view.m_meshRadius = bx::length(getExtents(boundingBox));
  957. view.m_camera.init( view.m_meshCenter, view.m_meshRadius * 2.0f);
  958. }
  959. else
  960. {
  961. bx::stringPrintf(title, "Failed to load %s!", filePath);
  962. }
  963. entry::WindowHandle handle = { 0 };
  964. entry::setWindowTitle(handle, title.c_str() );
  965. }
  966. int64_t now = bx::getHPCounter();
  967. static int64_t last = now;
  968. const int64_t frameTime = now - last;
  969. last = now;
  970. const double freq = double(bx::getHPFrequency() );
  971. const float deltaTime = float(frameTime/freq);
  972. time += (float)(frameTime*speed/freq);
  973. // Update camera.
  974. float viewMatrix[16];
  975. view.m_camera.update(deltaTime);
  976. view.m_camera.mtxLookAt(viewMatrix);
  977. float projMatrix[16];
  978. const float aspect = float(view.m_width)/float(view.m_height);
  979. bx::mtxProj(projMatrix, 60.0f, aspect, 0.1f, 1000.0f, caps->homogeneousDepth);
  980. bgfx::setViewTransform(SCENE_VIEW_ID, viewMatrix, projMatrix);
  981. bgfx::setViewRect(SCENE_VIEW_ID, 0, 0, uint16_t(view.m_width), uint16_t(view.m_height) );
  982. bgfx::touch(SCENE_VIEW_ID);
  983. bgfx::dbgTextClear();
  984. float orientation[16];
  985. bx::mtxIdentity(orientation);
  986. bgfx::setTransform(orientation);
  987. float mtx[16];
  988. bx::mtxIdentity(mtx);
  989. if (NULL != mesh)
  990. {
  991. meshSubmit(mesh
  992. , SCENE_VIEW_ID
  993. , meshProgram
  994. , mtx);
  995. }
  996. bgfx::frame();
  997. // Slow down when nothing is animating...
  998. if (view.m_idleTimer > 2.0f
  999. && !anyActive() )
  1000. {
  1001. bx::sleep(100);
  1002. }
  1003. view.m_idleTimer += deltaTime;
  1004. }
  1005. }
  1006. if (NULL != mesh )
  1007. {
  1008. meshUnload(mesh);
  1009. }
  1010. bgfx::destroy(meshProgram);
  1011. imguiDestroy();
  1012. bgfx::shutdown();
  1013. return exitcode;
  1014. }