texturev.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "common.h"
  6. #include <bgfx/bgfx.h>
  7. #include <bx/commandline.h>
  8. #include <bx/os.h>
  9. #include <bx/string.h>
  10. #include <bx/uint32_t.h>
  11. #include <entry/entry.h>
  12. #include <entry/input.h>
  13. #include <entry/cmd.h>
  14. #include <imgui/imgui.h>
  15. #include <bgfx_utils.h>
  16. #include <dirent.h>
  17. #include "vs_texture.bin.h"
  18. #include "fs_texture.bin.h"
  19. #include "vs_texture_cube.bin.h"
  20. #include "fs_texture_cube.bin.h"
  21. #include <bx/crtimpl.h>
  22. #include <tinystl/allocator.h>
  23. #include <tinystl/vector.h>
  24. #include <string>
  25. namespace stl = tinystl;
  26. #include "image.h"
  27. static const char* s_supportedExt[] =
  28. {
  29. "dds",
  30. "jpg",
  31. "jpeg",
  32. "hdr",
  33. "ktx",
  34. "png",
  35. "pvr",
  36. "tga",
  37. };
  38. struct Binding
  39. {
  40. enum Enum
  41. {
  42. App,
  43. View,
  44. Count
  45. };
  46. };
  47. static const InputBinding s_bindingApp[] =
  48. {
  49. { entry::Key::Esc, entry::Modifier::None, 1, NULL, "exit" },
  50. { entry::Key::KeyF, entry::Modifier::None, 1, NULL, "graphics fullscreen" },
  51. INPUT_BINDING_END
  52. };
  53. static const InputBinding s_bindingView[] =
  54. {
  55. { entry::Key::Comma, entry::Modifier::None, 1, NULL, "view mip prev" },
  56. { entry::Key::Period, entry::Modifier::None, 1, NULL, "view mip next" },
  57. { entry::Key::Comma, entry::Modifier::LeftShift, 1, NULL, "view mip" },
  58. { entry::Key::Comma, entry::Modifier::RightShift, 1, NULL, "view mip" },
  59. { entry::Key::Slash, entry::Modifier::None, 1, NULL, "view filter" },
  60. { entry::Key::Key0, entry::Modifier::None, 1, NULL, "view zoom 1.0" },
  61. { entry::Key::Plus, entry::Modifier::None, 1, NULL, "view zoom +0.1" },
  62. { entry::Key::Minus, entry::Modifier::None, 1, NULL, "view zoom -0.1" },
  63. { entry::Key::Up, entry::Modifier::None, 1, NULL, "view file-up" },
  64. { entry::Key::Down, entry::Modifier::None, 1, NULL, "view file-down" },
  65. { entry::Key::PageUp, entry::Modifier::None, 1, NULL, "view file-pgup" },
  66. { entry::Key::PageDown, entry::Modifier::None, 1, NULL, "view file-pgdown" },
  67. { entry::Key::KeyH, entry::Modifier::None, 1, NULL, "view help" },
  68. INPUT_BINDING_END
  69. };
  70. static const char* s_bindingName[] =
  71. {
  72. "App",
  73. "View",
  74. };
  75. BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_bindingName) );
  76. static const InputBinding* s_binding[] =
  77. {
  78. s_bindingApp,
  79. s_bindingView,
  80. };
  81. BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_binding) );
  82. struct View
  83. {
  84. View()
  85. : m_fileIndex(0)
  86. , m_scaleFn(0)
  87. , m_mip(0)
  88. , m_zoom(1.0f)
  89. , m_filter(true)
  90. , m_help(false)
  91. {
  92. }
  93. ~View()
  94. {
  95. }
  96. int32_t cmd(int32_t _argc, char const* const* _argv)
  97. {
  98. if (_argc >= 2)
  99. {
  100. if (0 == strcmp(_argv[1], "mip") )
  101. {
  102. if (_argc >= 3)
  103. {
  104. uint32_t mip = m_mip;
  105. if (0 == strcmp(_argv[2], "next") )
  106. {
  107. ++mip;
  108. }
  109. else if (0 == strcmp(_argv[2], "prev") )
  110. {
  111. --mip;
  112. }
  113. else if (0 == strcmp(_argv[2], "last") )
  114. {
  115. mip = INT32_MAX;
  116. }
  117. else
  118. {
  119. mip = atoi(_argv[2]);
  120. }
  121. m_mip = bx::uint32_iclamp(mip, 0, m_info.numMips-1);
  122. }
  123. else
  124. {
  125. m_mip = 0;
  126. }
  127. }
  128. else if (0 == strcmp(_argv[1], "zoom") )
  129. {
  130. if (_argc >= 3)
  131. {
  132. float zoom = (float)atof(_argv[2]);
  133. if (_argv[2][0] == '+'
  134. || _argv[2][0] == '-')
  135. {
  136. m_zoom += zoom;
  137. }
  138. else
  139. {
  140. m_zoom = zoom;
  141. }
  142. m_zoom = bx::fclamp(m_zoom, 0.001f, 10.0f);
  143. }
  144. else
  145. {
  146. m_zoom = 1.0f;
  147. }
  148. }
  149. else if (0 == strcmp(_argv[1], "filter") )
  150. {
  151. if (_argc >= 3)
  152. {
  153. m_filter = bx::toBool(_argv[2]);
  154. }
  155. else
  156. {
  157. m_filter ^= true;
  158. }
  159. }
  160. else if (0 == strcmp(_argv[1], "file-up") )
  161. {
  162. m_fileIndex = bx::uint32_satsub(m_fileIndex, 1);
  163. }
  164. else if (0 == strcmp(_argv[1], "file-down") )
  165. {
  166. uint32_t numFiles = bx::uint32_satsub(uint32_t(m_fileList.size() ), 1);
  167. ++m_fileIndex;
  168. m_fileIndex = bx::uint32_min(m_fileIndex, numFiles);
  169. }
  170. else if (0 == strcmp(_argv[1], "help") )
  171. {
  172. m_help ^= true;
  173. }
  174. }
  175. return 0;
  176. }
  177. void updateFileList(const char* _path, const char* _fileName = "")
  178. {
  179. std::string path = _path;
  180. DIR* dir = opendir(_path);
  181. if (NULL == dir)
  182. {
  183. path = ".";
  184. }
  185. dir = opendir(path.c_str() );
  186. if (NULL != dir)
  187. {
  188. for (dirent* item = readdir(dir); NULL != item; item = readdir(dir) )
  189. {
  190. if (0 == (item->d_type & DT_DIR) )
  191. {
  192. const char* ext = strrchr(item->d_name, '.');
  193. if (NULL != ext)
  194. {
  195. ext += 1;
  196. bool supported = false;
  197. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  198. {
  199. if (0 == bx::stricmp(ext, s_supportedExt[ii]) )
  200. {
  201. supported = true;
  202. break;
  203. }
  204. }
  205. if (supported)
  206. {
  207. if (0 == strcmp(_fileName, item->d_name) )
  208. {
  209. m_fileIndex = uint32_t(m_fileList.size() );
  210. }
  211. std::string name = path;
  212. char ch = name[name.size()-1];
  213. name += '/' == ch || '\\' == ch ? "" : "/";
  214. name += item->d_name;
  215. m_fileList.push_back(name);
  216. }
  217. }
  218. }
  219. }
  220. closedir(dir);
  221. }
  222. }
  223. typedef stl::vector<std::string> FileList;
  224. FileList m_fileList;
  225. bgfx::TextureInfo m_info;
  226. uint32_t m_fileIndex;
  227. uint32_t m_scaleFn;
  228. uint32_t m_mip;
  229. float m_zoom;
  230. bool m_filter;
  231. bool m_help;
  232. };
  233. int cmdView(CmdContext* /*_context*/, void* _userData, int _argc, char const* const* _argv)
  234. {
  235. View* view = static_cast<View*>(_userData);
  236. return view->cmd(_argc, _argv);
  237. }
  238. struct PosUvColorVertex
  239. {
  240. float m_x;
  241. float m_y;
  242. float m_u;
  243. float m_v;
  244. uint32_t m_abgr;
  245. static void init()
  246. {
  247. ms_decl
  248. .begin()
  249. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  250. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  251. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  252. .end();
  253. }
  254. static bgfx::VertexDecl ms_decl;
  255. };
  256. bgfx::VertexDecl PosUvColorVertex::ms_decl;
  257. bool screenQuad(int32_t _x, int32_t _y, int32_t _width, uint32_t _height, bool _originBottomLeft = false)
  258. {
  259. if (bgfx::checkAvailTransientVertexBuffer(6, PosUvColorVertex::ms_decl) )
  260. {
  261. bgfx::TransientVertexBuffer vb;
  262. bgfx::allocTransientVertexBuffer(&vb, 6, PosUvColorVertex::ms_decl);
  263. PosUvColorVertex* vertex = (PosUvColorVertex*)vb.data;
  264. const float widthf = float(_width);
  265. const float heightf = float(_height);
  266. const float minx = float(_x);
  267. const float miny = float(_y);
  268. const float maxx = minx+widthf;
  269. const float maxy = miny+heightf;
  270. float m_halfTexel = 0.0f;
  271. const float texelHalfW = m_halfTexel/widthf;
  272. const float texelHalfH = m_halfTexel/heightf;
  273. const float minu = texelHalfW;
  274. const float maxu = 1.0f - texelHalfW;
  275. const float minv = _originBottomLeft ? texelHalfH+1.0f : texelHalfH ;
  276. const float maxv = _originBottomLeft ? texelHalfH : texelHalfH+1.0f;
  277. vertex[0].m_x = minx;
  278. vertex[0].m_y = miny;
  279. vertex[0].m_u = minu;
  280. vertex[0].m_v = minv;
  281. vertex[1].m_x = maxx;
  282. vertex[1].m_y = miny;
  283. vertex[1].m_u = maxu;
  284. vertex[1].m_v = minv;
  285. vertex[2].m_x = maxx;
  286. vertex[2].m_y = maxy;
  287. vertex[2].m_u = maxu;
  288. vertex[2].m_v = maxv;
  289. vertex[3].m_x = maxx;
  290. vertex[3].m_y = maxy;
  291. vertex[3].m_u = maxu;
  292. vertex[3].m_v = maxv;
  293. vertex[4].m_x = minx;
  294. vertex[4].m_y = maxy;
  295. vertex[4].m_u = minu;
  296. vertex[4].m_v = maxv;
  297. vertex[5].m_x = minx;
  298. vertex[5].m_y = miny;
  299. vertex[5].m_u = minu;
  300. vertex[5].m_v = minv;
  301. vertex[0].m_abgr = UINT32_MAX;
  302. vertex[1].m_abgr = UINT32_MAX;
  303. vertex[2].m_abgr = UINT32_MAX;
  304. vertex[3].m_abgr = UINT32_MAX;
  305. vertex[4].m_abgr = UINT32_MAX;
  306. vertex[5].m_abgr = UINT32_MAX;
  307. bgfx::setVertexBuffer(&vb);
  308. return true;
  309. }
  310. return false;
  311. }
  312. struct Interpolator
  313. {
  314. float from;
  315. float to;
  316. float duration;
  317. int64_t offset;
  318. Interpolator(float _value)
  319. {
  320. reset(_value);
  321. }
  322. void reset(float _value)
  323. {
  324. from = _value;
  325. to = _value;
  326. duration = 0.0;
  327. offset = bx::getHPCounter();
  328. }
  329. void set(float _value, float _duration)
  330. {
  331. if (_value != to)
  332. {
  333. from = getValue();
  334. to = _value;
  335. duration = _duration;
  336. offset = bx::getHPCounter();
  337. }
  338. }
  339. float getValue()
  340. {
  341. if (duration > 0.0)
  342. {
  343. const double freq = double(bx::getHPFrequency() );
  344. int64_t now = bx::getHPCounter();
  345. float time = (float)(double(now - offset) / freq);
  346. float lerp = bx::fclamp(time, 0.0, duration) / duration;
  347. return bx::flerp(from, to, lerp);
  348. }
  349. return to;
  350. }
  351. };
  352. void help(const char* _error = NULL)
  353. {
  354. if (NULL != _error)
  355. {
  356. fprintf(stderr, "Error:\n%s\n\n", _error);
  357. }
  358. fprintf(stderr
  359. , "texturev, bgfx texture viewer tool\n"
  360. "Copyright 2011-2016 Branimir Karadzic. All rights reserved.\n"
  361. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  362. );
  363. fprintf(stderr
  364. , "Usage: texturev <file path>\n"
  365. "\n"
  366. "Supported input file types:\n"
  367. );
  368. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  369. {
  370. fprintf(stderr, " *.%s\n", s_supportedExt[ii]);
  371. }
  372. fprintf(stderr
  373. , "\n"
  374. "Options:\n"
  375. " --associate Associate file extensions with texturev.\n"
  376. "\n"
  377. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  378. );
  379. }
  380. void associate()
  381. {
  382. #if BX_PLATFORM_WINDOWS
  383. std::string str;
  384. char exec[MAX_PATH];
  385. GetModuleFileNameA(GetModuleHandleA(NULL), exec, MAX_PATH);
  386. std::string strExec = bx::replaceAll(exec, "\\", "\\\\");
  387. std::string value;
  388. bx::stringPrintf(value, "@=\"\\\"%s\\\" \\\"%%1\\\"\"\r\n\r\n", strExec.c_str() );
  389. str += "Windows Registry Editor Version 5.00\r\n\r\n";
  390. str += "[HKEY_CLASSES_ROOT\\texturev\\shell\\open\\command]\r\n";
  391. str += value;
  392. str += "[HKEY_CLASSES_ROOT\\Applications\\texturev.exe\\shell\\open\\command]\r\n";
  393. str += value;
  394. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  395. {
  396. const char* ext = s_supportedExt[ii];
  397. bx::stringPrintf(str, "[-HKEY_CLASSES_ROOT\\.%s]\r\n\r\n", ext);
  398. bx::stringPrintf(str, "[-HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n\r\n", ext);
  399. bx::stringPrintf(str, "[HKEY_CLASSES_ROOT\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  400. bx::stringPrintf(str, "[HKEY_CURRENT_USER\\Software\\Classes\\.%s]\r\n@=\"texturev\"\r\n\r\n", ext);
  401. }
  402. char temp[MAX_PATH];
  403. GetTempPathA(MAX_PATH, temp);
  404. strcat(temp, "\\texturev.reg");
  405. bx::CrtFileWriter writer;
  406. bx::Error err;
  407. if (bx::open(&writer, temp, false, &err) )
  408. {
  409. bx::write(&writer, str.c_str(), str.length(), &err);
  410. bx::close(&writer);
  411. if (err.isOk() )
  412. {
  413. std::string cmd;
  414. bx::stringPrintf(cmd, "regedit.exe /s %s", temp);
  415. bx::ProcessReader reader;
  416. if (bx::open(&reader, cmd.c_str(), &err) )
  417. {
  418. bx::close(&reader);
  419. }
  420. }
  421. }
  422. #elif BX_PLATFORM_LINUX
  423. std::string str;
  424. str += "#/bin/bash\n\n";
  425. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  426. {
  427. const char* ext = s_supportedExt[ii];
  428. bx::stringPrintf(str, "xdg-mime default texturev.desktop image/%s\n", ext);
  429. }
  430. str += "\n";
  431. bx::CrtFileWriter writer;
  432. bx::Error err;
  433. if (bx::open(&writer, "/tmp/texturev.sh", false, &err) )
  434. {
  435. bx::write(&writer, str.c_str(), str.length(), &err);
  436. bx::close(&writer);
  437. if (err.isOk() )
  438. {
  439. bx::ProcessReader reader;
  440. if (bx::open(&reader, "/bin/bash /tmp/texturev.sh", &err) )
  441. {
  442. bx::close(&reader);
  443. }
  444. }
  445. }
  446. #endif // BX_PLATFORM_WINDOWS
  447. }
  448. int _main_(int _argc, char** _argv)
  449. {
  450. bx::CommandLine cmdLine(_argc, _argv);
  451. if (cmdLine.hasArg('h', "help") )
  452. {
  453. help();
  454. return EXIT_FAILURE;
  455. }
  456. else if (cmdLine.hasArg("associate") )
  457. {
  458. associate();
  459. return EXIT_FAILURE;
  460. }
  461. uint32_t width = 1280;
  462. uint32_t height = 720;
  463. uint32_t debug = BGFX_DEBUG_TEXT;
  464. uint32_t reset = BGFX_RESET_VSYNC;
  465. inputAddBindings(s_bindingName[Binding::App], s_binding[Binding::App]);
  466. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  467. View view;
  468. cmdAdd("view", cmdView, &view);
  469. bgfx::init();
  470. bgfx::reset(width, height, reset);
  471. // Set view 0 clear state.
  472. bgfx::setViewClear(0
  473. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  474. , 0x101010ff
  475. , 1.0f
  476. , 0
  477. );
  478. imguiCreate();
  479. PosUvColorVertex::init();
  480. const bgfx::Memory* vs_texture;
  481. const bgfx::Memory* fs_texture;
  482. const bgfx::Memory* vs_texture_cube;
  483. const bgfx::Memory* fs_texture_cube;
  484. switch (bgfx::getRendererType())
  485. {
  486. case bgfx::RendererType::Direct3D9:
  487. vs_texture = bgfx::makeRef(vs_texture_dx9, sizeof(vs_texture_dx9) );
  488. fs_texture = bgfx::makeRef(fs_texture_dx9, sizeof(fs_texture_dx9) );
  489. vs_texture_cube = bgfx::makeRef(vs_texture_cube_dx9, sizeof(vs_texture_cube_dx9) );
  490. fs_texture_cube = bgfx::makeRef(fs_texture_cube_dx9, sizeof(fs_texture_cube_dx9) );
  491. break;
  492. case bgfx::RendererType::Direct3D11:
  493. case bgfx::RendererType::Direct3D12:
  494. vs_texture = bgfx::makeRef(vs_texture_dx11, sizeof(vs_texture_dx11) );
  495. fs_texture = bgfx::makeRef(fs_texture_dx11, sizeof(fs_texture_dx11) );
  496. vs_texture_cube = bgfx::makeRef(vs_texture_cube_dx11, sizeof(vs_texture_cube_dx11) );
  497. fs_texture_cube = bgfx::makeRef(fs_texture_cube_dx11, sizeof(fs_texture_cube_dx11) );
  498. break;
  499. default:
  500. vs_texture = bgfx::makeRef(vs_texture_glsl, sizeof(vs_texture_glsl) );
  501. fs_texture = bgfx::makeRef(fs_texture_glsl, sizeof(fs_texture_glsl) );
  502. vs_texture_cube = bgfx::makeRef(vs_texture_cube_glsl, sizeof(vs_texture_cube_glsl) );
  503. fs_texture_cube = bgfx::makeRef(fs_texture_cube_glsl, sizeof(fs_texture_cube_glsl) );
  504. break;
  505. }
  506. bgfx::ProgramHandle textureProgram = bgfx::createProgram(
  507. bgfx::createShader(vs_texture)
  508. , bgfx::createShader(fs_texture)
  509. , true
  510. );
  511. bgfx::ProgramHandle textureCubeProgram = bgfx::createProgram(
  512. bgfx::createShader(vs_texture_cube)
  513. , bgfx::createShader(fs_texture_cube)
  514. , true
  515. );
  516. bgfx::UniformHandle s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  517. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  518. bgfx::UniformHandle u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
  519. float speed = 0.37f;
  520. float time = 0.0f;
  521. Interpolator mip(0.0);
  522. Interpolator zoom(1.0);
  523. Interpolator scale(1.0);
  524. const char* filePath = _argc < 2 ? "" : _argv[1];
  525. bool directory = false;
  526. bx::FileInfo fi;
  527. bx::stat(filePath, fi);
  528. directory = bx::FileInfo::Directory == fi.m_type;
  529. std::string path = filePath;
  530. if (!directory)
  531. {
  532. const char* fileName = directory ? filePath : bx::baseName(filePath);
  533. path.assign(filePath, fileName);
  534. view.updateFileList(path.c_str(), fileName);
  535. }
  536. else
  537. {
  538. view.updateFileList(path.c_str() );
  539. }
  540. int exitcode = EXIT_SUCCESS;
  541. bgfx::TextureHandle texture = BGFX_INVALID_HANDLE;
  542. if (view.m_fileList.empty() )
  543. {
  544. exitcode = EXIT_FAILURE;
  545. if (2 > _argc)
  546. {
  547. help("File path is not specified.");
  548. }
  549. else
  550. {
  551. fprintf(stderr, "Unable to load '%s' texture.\n", filePath);
  552. }
  553. }
  554. else
  555. {
  556. uint32_t fileIndex = 0;
  557. entry::MouseState mouseState;
  558. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  559. {
  560. imguiBeginFrame(mouseState.m_mx
  561. , mouseState.m_my
  562. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  563. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  564. | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  565. , mouseState.m_mz
  566. , width
  567. , height
  568. );
  569. static bool help = false;
  570. if (help == false
  571. && help != view.m_help)
  572. {
  573. ImGui::OpenPopup("Help");
  574. }
  575. if (ImGui::BeginPopupModal("Help", NULL, ImGuiWindowFlags_AlwaysAutoResize) )
  576. {
  577. ImGui::SetWindowFontScale(1.2f);
  578. ImGui::Text(
  579. "texturev, bgfx texture viewer tool\n"
  580. "Copyright 2011-2016 Branimir Karadzic. All rights reserved.\n"
  581. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n"
  582. );
  583. ImGui::Separator();
  584. ImGui::NextLine();
  585. ImGui::Text("Key bindings:\n\n");
  586. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "ESC"); ImGui::SameLine(64); ImGui::Text("Exit.");
  587. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "h"); ImGui::SameLine(64); ImGui::Text("Toggle help screen.");
  588. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "f"); ImGui::SameLine(64); ImGui::Text("Toggle full-screen.");
  589. ImGui::NextLine();
  590. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "-"); ImGui::SameLine(64); ImGui::Text("Zoom out.");
  591. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "="); ImGui::SameLine(64); ImGui::Text("Zoom in.");
  592. ImGui::NextLine();
  593. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), ","); ImGui::SameLine(64); ImGui::Text("MIP level up.");
  594. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "."); ImGui::SameLine(64); ImGui::Text("MIP level down.");
  595. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "/"); ImGui::SameLine(64); ImGui::Text("Toggle linear/point texture sampling.");
  596. ImGui::NextLine();
  597. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "up"); ImGui::SameLine(64); ImGui::Text("Previous texture.");
  598. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "down"); ImGui::SameLine(64); ImGui::Text("Next texture.");
  599. ImGui::NextLine();
  600. ImGui::Dummy(ImVec2(0.0f, 0.0f) );
  601. ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
  602. if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
  603. || !view.m_help)
  604. {
  605. view.m_help = false;
  606. ImGui::CloseCurrentPopup();
  607. }
  608. ImGui::EndPopup();
  609. }
  610. help = view.m_help;
  611. // bool b;
  612. // ImGui::ShowTestWindow(&b);
  613. imguiEndFrame();
  614. if (!bgfx::isValid(texture)
  615. || view.m_fileIndex != fileIndex)
  616. {
  617. if (bgfx::isValid(texture) )
  618. {
  619. bgfx::destroyTexture(texture);
  620. }
  621. fileIndex = view.m_fileIndex;
  622. filePath = view.m_fileList[view.m_fileIndex].c_str();
  623. texture = loadTexture(filePath
  624. , 0
  625. | BGFX_TEXTURE_U_CLAMP
  626. | BGFX_TEXTURE_V_CLAMP
  627. | BGFX_TEXTURE_W_CLAMP
  628. , 0
  629. , &view.m_info
  630. );
  631. std::string title;
  632. bx::stringPrintf(title, "%s (%d x %d%s, %s)"
  633. , filePath
  634. , view.m_info.width
  635. , view.m_info.height
  636. , view.m_info.cubeMap ? " CubeMap" : ""
  637. , bgfx::getName(view.m_info.format)
  638. );
  639. entry::WindowHandle handle = { 0 };
  640. entry::setWindowTitle(handle, title.c_str() );
  641. }
  642. int64_t now = bx::getHPCounter();
  643. static int64_t last = now;
  644. const int64_t frameTime = now - last;
  645. last = now;
  646. const double freq = double(bx::getHPFrequency() );
  647. time += (float)(frameTime*speed/freq);
  648. float ortho[16];
  649. bx::mtxOrtho(ortho, 0.0f, (float)width, (float)height, 0.0f, 0.0f, 1000.0f);
  650. bgfx::setViewTransform(0, NULL, ortho);
  651. bgfx::setViewRect(0, 0, 0, width, height);
  652. bgfx::touch(0);
  653. bgfx::dbgTextClear();
  654. scale.set(
  655. bx::fmin( float(width) / float(view.m_info.width)
  656. , float(height) / float(view.m_info.height)
  657. )
  658. , 0.1f
  659. );
  660. zoom.set(view.m_zoom, 0.25);
  661. float ss = scale.getValue() * zoom.getValue();
  662. screenQuad( int(width - view.m_info.width * ss)/2
  663. , int(height - view.m_info.height * ss)/2
  664. , int(view.m_info.width * ss)
  665. , int(view.m_info.height * ss)
  666. );
  667. float mtx[16];
  668. bx::mtxRotateXY(mtx, 0.0f, time);
  669. bgfx::setUniform(u_mtx, mtx);
  670. mip.set( float(view.m_mip), 0.5f);
  671. float params[4] = { mip.getValue(), 0.0f, 0.0f, 0.0f };
  672. bgfx::setUniform(u_params, params);
  673. bgfx::setTexture(0
  674. , s_texColor
  675. , texture
  676. , view.m_filter
  677. ? BGFX_TEXTURE_NONE
  678. : 0
  679. | BGFX_TEXTURE_MIN_POINT
  680. | BGFX_TEXTURE_MIP_POINT
  681. | BGFX_TEXTURE_MAG_POINT
  682. );
  683. bgfx::setState(0
  684. | BGFX_STATE_RGB_WRITE
  685. | BGFX_STATE_ALPHA_WRITE
  686. );
  687. bgfx::submit(0, view.m_info.cubeMap ? textureCubeProgram : textureProgram);
  688. bgfx::frame();
  689. }
  690. }
  691. if (bgfx::isValid(texture) )
  692. {
  693. bgfx::destroyTexture(texture);
  694. }
  695. bgfx::destroyUniform(s_texColor);
  696. bgfx::destroyUniform(u_mtx);
  697. bgfx::destroyUniform(u_params);
  698. bgfx::destroyProgram(textureProgram);
  699. bgfx::destroyProgram(textureCubeProgram);
  700. imguiDestroy();
  701. bgfx::shutdown();
  702. return exitcode;
  703. }