texturev.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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. bool supported = false;
  196. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  197. {
  198. if (0 == bx::stricmp(ext, s_supportedExt[ii]) )
  199. {
  200. supported = true;
  201. break;
  202. }
  203. }
  204. if (supported)
  205. {
  206. if (0 == strcmp(_fileName, item->d_name) )
  207. {
  208. m_fileIndex = uint32_t(m_fileList.size() );
  209. }
  210. std::string name = path;
  211. char ch = name[name.size()-1];
  212. name += '/' == ch || '\\' == ch ? "" : "/";
  213. name += item->d_name;
  214. m_fileList.push_back(name);
  215. }
  216. }
  217. }
  218. }
  219. closedir(dir);
  220. }
  221. }
  222. typedef stl::vector<std::string> FileList;
  223. FileList m_fileList;
  224. bgfx::TextureInfo m_info;
  225. uint32_t m_fileIndex;
  226. uint32_t m_scaleFn;
  227. uint32_t m_mip;
  228. float m_zoom;
  229. bool m_filter;
  230. bool m_help;
  231. };
  232. int cmdView(CmdContext* /*_context*/, void* _userData, int _argc, char const* const* _argv)
  233. {
  234. View* view = static_cast<View*>(_userData);
  235. return view->cmd(_argc, _argv);
  236. }
  237. struct PosUvColorVertex
  238. {
  239. float m_x;
  240. float m_y;
  241. float m_u;
  242. float m_v;
  243. uint32_t m_abgr;
  244. static void init()
  245. {
  246. ms_decl
  247. .begin()
  248. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  249. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  250. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  251. .end();
  252. }
  253. static bgfx::VertexDecl ms_decl;
  254. };
  255. bgfx::VertexDecl PosUvColorVertex::ms_decl;
  256. bool screenQuad(int32_t _x, int32_t _y, int32_t _width, uint32_t _height, bool _originBottomLeft = false)
  257. {
  258. if (bgfx::checkAvailTransientVertexBuffer(6, PosUvColorVertex::ms_decl) )
  259. {
  260. bgfx::TransientVertexBuffer vb;
  261. bgfx::allocTransientVertexBuffer(&vb, 6, PosUvColorVertex::ms_decl);
  262. PosUvColorVertex* vertex = (PosUvColorVertex*)vb.data;
  263. const float widthf = float(_width);
  264. const float heightf = float(_height);
  265. const float minx = float(_x);
  266. const float miny = float(_y);
  267. const float maxx = minx+widthf;
  268. const float maxy = miny+heightf;
  269. float m_halfTexel = 0.0f;
  270. const float texelHalfW = m_halfTexel/widthf;
  271. const float texelHalfH = m_halfTexel/heightf;
  272. const float minu = texelHalfW;
  273. const float maxu = 1.0f - texelHalfW;
  274. const float minv = _originBottomLeft ? texelHalfH+1.0f : texelHalfH ;
  275. const float maxv = _originBottomLeft ? texelHalfH : texelHalfH+1.0f;
  276. vertex[0].m_x = minx;
  277. vertex[0].m_y = miny;
  278. vertex[0].m_u = minu;
  279. vertex[0].m_v = minv;
  280. vertex[1].m_x = maxx;
  281. vertex[1].m_y = miny;
  282. vertex[1].m_u = maxu;
  283. vertex[1].m_v = minv;
  284. vertex[2].m_x = maxx;
  285. vertex[2].m_y = maxy;
  286. vertex[2].m_u = maxu;
  287. vertex[2].m_v = maxv;
  288. vertex[3].m_x = maxx;
  289. vertex[3].m_y = maxy;
  290. vertex[3].m_u = maxu;
  291. vertex[3].m_v = maxv;
  292. vertex[4].m_x = minx;
  293. vertex[4].m_y = maxy;
  294. vertex[4].m_u = minu;
  295. vertex[4].m_v = maxv;
  296. vertex[5].m_x = minx;
  297. vertex[5].m_y = miny;
  298. vertex[5].m_u = minu;
  299. vertex[5].m_v = minv;
  300. vertex[0].m_abgr = UINT32_MAX;
  301. vertex[1].m_abgr = UINT32_MAX;
  302. vertex[2].m_abgr = UINT32_MAX;
  303. vertex[3].m_abgr = UINT32_MAX;
  304. vertex[4].m_abgr = UINT32_MAX;
  305. vertex[5].m_abgr = UINT32_MAX;
  306. bgfx::setVertexBuffer(&vb);
  307. return true;
  308. }
  309. return false;
  310. }
  311. struct Interpolator
  312. {
  313. float from;
  314. float to;
  315. float duration;
  316. int64_t offset;
  317. Interpolator(float _value)
  318. {
  319. reset(_value);
  320. }
  321. void reset(float _value)
  322. {
  323. from = _value;
  324. to = _value;
  325. duration = 0.0;
  326. offset = bx::getHPCounter();
  327. }
  328. void set(float _value, float _duration)
  329. {
  330. if (_value != to)
  331. {
  332. from = getValue();
  333. to = _value;
  334. duration = _duration;
  335. offset = bx::getHPCounter();
  336. }
  337. }
  338. float getValue()
  339. {
  340. if (duration > 0.0)
  341. {
  342. const double freq = double(bx::getHPFrequency() );
  343. int64_t now = bx::getHPCounter();
  344. float time = (float)(double(now - offset) / freq);
  345. float lerp = bx::fclamp(time, 0.0, duration) / duration;
  346. return bx::flerp(from, to, lerp);
  347. }
  348. return to;
  349. }
  350. };
  351. void help(const char* _error = NULL)
  352. {
  353. if (NULL != _error)
  354. {
  355. fprintf(stderr, "Error:\n%s\n\n", _error);
  356. }
  357. fprintf(stderr
  358. , "texturev, bgfx texture viewer tool\n"
  359. "Copyright 2011-2016 Branimir Karadzic. All rights reserved.\n"
  360. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  361. );
  362. fprintf(stderr
  363. , "Usage: texturev <file path>\n"
  364. "\n"
  365. "Supported input file types:\n"
  366. );
  367. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  368. {
  369. fprintf(stderr, " *.%s\n", s_supportedExt[ii]);
  370. }
  371. fprintf(stderr
  372. , "\n"
  373. "Options:\n"
  374. " --associate Associate file extensions with texturev.\n"
  375. "\n"
  376. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  377. );
  378. }
  379. void associate()
  380. {
  381. #if BX_PLATFORM_WINDOWS
  382. std::string str;
  383. char exec[MAX_PATH];
  384. GetModuleFileNameA(GetModuleHandleA(NULL), exec, MAX_PATH);
  385. std::string value;
  386. bx::stringPrintf(value, "@=\"\\\"%s\\\" \\\"%1\\\"\"\n\n", exec);
  387. str += "Windows Registry Editor Version 5.00\n\n";
  388. str += "[HKEY_CLASSES_ROOT\\texturev\\shell\\open\\command]\n";
  389. str += value;
  390. str += "[HKEY_CLASSES_ROOT\\Applications\\texturev.exe\\shell\\open\\command]\n";
  391. str += value;
  392. for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
  393. {
  394. const char* ext = s_supportedExt[ii];
  395. bx::stringPrintf(str, "[HKEY_CLASSES_ROOT\\.%s]\n@=\"texturev\"\n\n", ext);
  396. bx::stringPrintf(str, "[HKEY_CURRENT_USER\\Software\\Classes\\.%s]\n@=\"texturev\"\n\n", ext);
  397. }
  398. char temp[MAX_PATH];
  399. GetTempPathA(MAX_PATH, temp);
  400. strcat(temp, "\\texturev.reg");
  401. bx::CrtFileWriter writer;
  402. bx::Error err;
  403. if (bx::open(&writer, temp, false, &err) )
  404. {
  405. bx::write(&writer, str.c_str(), str.length(), &err);
  406. bx::close(&writer);
  407. if (err.isOk() )
  408. {
  409. std::string cmd;
  410. bx::stringPrintf(cmd, "regedit.exe /s %s", temp);
  411. bx::ProcessReader reader;
  412. if (bx::open(&reader, cmd.c_str(), &err) )
  413. {
  414. bx::close(&reader);
  415. }
  416. }
  417. }
  418. #endif // BX_PLATFORM_WINDOWS
  419. }
  420. int _main_(int _argc, char** _argv)
  421. {
  422. bx::CommandLine cmdLine(_argc, _argv);
  423. if (cmdLine.hasArg('h', "help") )
  424. {
  425. help();
  426. return EXIT_FAILURE;
  427. }
  428. else if (cmdLine.hasArg("associate") )
  429. {
  430. associate();
  431. return EXIT_FAILURE;
  432. }
  433. uint32_t width = 1280;
  434. uint32_t height = 720;
  435. uint32_t debug = BGFX_DEBUG_TEXT;
  436. uint32_t reset = BGFX_RESET_VSYNC;
  437. inputAddBindings(s_bindingName[Binding::App], s_binding[Binding::App]);
  438. inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
  439. View view;
  440. cmdAdd("view", cmdView, &view);
  441. bgfx::init();
  442. bgfx::reset(width, height, reset);
  443. // Set view 0 clear state.
  444. bgfx::setViewClear(0
  445. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  446. , 0x101010ff
  447. , 1.0f
  448. , 0
  449. );
  450. imguiCreate();
  451. PosUvColorVertex::init();
  452. const bgfx::Memory* vs_texture;
  453. const bgfx::Memory* fs_texture;
  454. const bgfx::Memory* vs_texture_cube;
  455. const bgfx::Memory* fs_texture_cube;
  456. switch (bgfx::getRendererType())
  457. {
  458. case bgfx::RendererType::Direct3D9:
  459. vs_texture = bgfx::makeRef(vs_texture_dx9, sizeof(vs_texture_dx9) );
  460. fs_texture = bgfx::makeRef(fs_texture_dx9, sizeof(fs_texture_dx9) );
  461. vs_texture_cube = bgfx::makeRef(vs_texture_cube_dx9, sizeof(vs_texture_cube_dx9) );
  462. fs_texture_cube = bgfx::makeRef(fs_texture_cube_dx9, sizeof(fs_texture_cube_dx9) );
  463. break;
  464. case bgfx::RendererType::Direct3D11:
  465. case bgfx::RendererType::Direct3D12:
  466. vs_texture = bgfx::makeRef(vs_texture_dx11, sizeof(vs_texture_dx11) );
  467. fs_texture = bgfx::makeRef(fs_texture_dx11, sizeof(fs_texture_dx11) );
  468. vs_texture_cube = bgfx::makeRef(vs_texture_cube_dx11, sizeof(vs_texture_cube_dx11) );
  469. fs_texture_cube = bgfx::makeRef(fs_texture_cube_dx11, sizeof(fs_texture_cube_dx11) );
  470. break;
  471. default:
  472. vs_texture = bgfx::makeRef(vs_texture_glsl, sizeof(vs_texture_glsl) );
  473. fs_texture = bgfx::makeRef(fs_texture_glsl, sizeof(fs_texture_glsl) );
  474. vs_texture_cube = bgfx::makeRef(vs_texture_cube_glsl, sizeof(vs_texture_cube_glsl) );
  475. fs_texture_cube = bgfx::makeRef(fs_texture_cube_glsl, sizeof(fs_texture_cube_glsl) );
  476. break;
  477. }
  478. bgfx::ProgramHandle textureProgram = bgfx::createProgram(
  479. bgfx::createShader(vs_texture)
  480. , bgfx::createShader(fs_texture)
  481. , true
  482. );
  483. bgfx::ProgramHandle textureCubeProgram = bgfx::createProgram(
  484. bgfx::createShader(vs_texture_cube)
  485. , bgfx::createShader(fs_texture_cube)
  486. , true
  487. );
  488. bgfx::UniformHandle s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  489. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  490. bgfx::UniformHandle u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
  491. float speed = 0.37f;
  492. float time = 0.0f;
  493. Interpolator mip(0.0);
  494. Interpolator zoom(1.0);
  495. Interpolator scale(1.0);
  496. const char* filePath = _argc < 2 ? "" : _argv[1];
  497. bool directory = false;
  498. bx::FileInfo fi;
  499. bx::stat(filePath, fi);
  500. directory = bx::FileInfo::Directory == fi.m_type;
  501. std::string path = filePath;
  502. if (!directory)
  503. {
  504. const char* fileName = directory ? filePath : bx::baseName(filePath);
  505. path.assign(filePath, fileName);
  506. view.updateFileList(path.c_str(), fileName);
  507. }
  508. else
  509. {
  510. view.updateFileList(path.c_str() );
  511. }
  512. int exitcode = EXIT_SUCCESS;
  513. bgfx::TextureHandle texture = BGFX_INVALID_HANDLE;
  514. if (view.m_fileList.empty() )
  515. {
  516. exitcode = EXIT_FAILURE;
  517. if (2 > _argc)
  518. {
  519. help("File path is not specified.");
  520. }
  521. else
  522. {
  523. fprintf(stderr, "Unable to load '%s' texture.\n", filePath);
  524. }
  525. }
  526. else
  527. {
  528. uint32_t fileIndex = 0;
  529. entry::MouseState mouseState;
  530. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  531. {
  532. imguiBeginFrame(mouseState.m_mx
  533. , mouseState.m_my
  534. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  535. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  536. | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  537. , mouseState.m_mz
  538. , width
  539. , height
  540. );
  541. static bool help = false;
  542. if (help == false
  543. && help != view.m_help)
  544. {
  545. ImGui::OpenPopup("Help");
  546. }
  547. if (ImGui::BeginPopupModal("Help", NULL, ImGuiWindowFlags_AlwaysAutoResize) )
  548. {
  549. ImGui::SetWindowFontScale(1.2f);
  550. ImGui::Text(
  551. "texturev, bgfx texture viewer tool\n"
  552. "Copyright 2011-2016 Branimir Karadzic. All rights reserved.\n"
  553. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n"
  554. );
  555. ImGui::Separator();
  556. ImGui::NextLine();
  557. ImGui::Text("Key bindings:\n\n");
  558. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "ESC"); ImGui::SameLine(64); ImGui::Text("Exit.");
  559. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "h"); ImGui::SameLine(64); ImGui::Text("Toggle help screen.");
  560. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "f"); ImGui::SameLine(64); ImGui::Text("Toggle full-screen.");
  561. ImGui::NextLine();
  562. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "-"); ImGui::SameLine(64); ImGui::Text("Zoom out.");
  563. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "="); ImGui::SameLine(64); ImGui::Text("Zoom in.");
  564. ImGui::NextLine();
  565. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), ","); ImGui::SameLine(64); ImGui::Text("MIP level up.");
  566. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "."); ImGui::SameLine(64); ImGui::Text("MIP level down.");
  567. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "/"); ImGui::SameLine(64); ImGui::Text("Toggle linear/point texture sampling.");
  568. ImGui::NextLine();
  569. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "up"); ImGui::SameLine(64); ImGui::Text("Previous texture.");
  570. ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "down"); ImGui::SameLine(64); ImGui::Text("Next texture.");
  571. ImGui::NextLine();
  572. ImGui::Dummy(ImVec2(0.0f, 0.0f) );
  573. ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
  574. if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
  575. || !view.m_help)
  576. {
  577. view.m_help = false;
  578. ImGui::CloseCurrentPopup();
  579. }
  580. ImGui::EndPopup();
  581. }
  582. help = view.m_help;
  583. // bool b;
  584. // ImGui::ShowTestWindow(&b);
  585. imguiEndFrame();
  586. if (!bgfx::isValid(texture)
  587. || view.m_fileIndex != fileIndex)
  588. {
  589. if (bgfx::isValid(texture) )
  590. {
  591. bgfx::destroyTexture(texture);
  592. }
  593. fileIndex = view.m_fileIndex;
  594. filePath = view.m_fileList[view.m_fileIndex].c_str();
  595. texture = loadTexture(filePath
  596. , 0
  597. | BGFX_TEXTURE_U_CLAMP
  598. | BGFX_TEXTURE_V_CLAMP
  599. | BGFX_TEXTURE_W_CLAMP
  600. , 0
  601. , &view.m_info
  602. );
  603. std::string title;
  604. bx::stringPrintf(title, "%s (%d x %d%s, %s)"
  605. , filePath
  606. , view.m_info.width
  607. , view.m_info.height
  608. , view.m_info.cubeMap ? " CubeMap" : ""
  609. , bgfx::getName(view.m_info.format)
  610. );
  611. entry::WindowHandle handle = { 0 };
  612. entry::setWindowTitle(handle, title.c_str() );
  613. }
  614. int64_t now = bx::getHPCounter();
  615. static int64_t last = now;
  616. const int64_t frameTime = now - last;
  617. last = now;
  618. const double freq = double(bx::getHPFrequency() );
  619. time += (float)(frameTime*speed/freq);
  620. float ortho[16];
  621. bx::mtxOrtho(ortho, 0.0f, (float)width, (float)height, 0.0f, 0.0f, 1000.0f);
  622. bgfx::setViewTransform(0, NULL, ortho);
  623. bgfx::setViewRect(0, 0, 0, width, height);
  624. bgfx::touch(0);
  625. bgfx::dbgTextClear();
  626. scale.set(
  627. bx::fmin( float(width) / float(view.m_info.width)
  628. , float(height) / float(view.m_info.height)
  629. )
  630. , 0.1f
  631. );
  632. zoom.set(view.m_zoom, 0.25);
  633. float ss = scale.getValue() * zoom.getValue();
  634. screenQuad( int(width - view.m_info.width * ss)/2
  635. , int(height - view.m_info.height * ss)/2
  636. , int(view.m_info.width * ss)
  637. , int(view.m_info.height * ss)
  638. );
  639. float mtx[16];
  640. bx::mtxRotateXY(mtx, 0.0f, time);
  641. bgfx::setUniform(u_mtx, mtx);
  642. mip.set( float(view.m_mip), 0.5f);
  643. float params[4] = { mip.getValue(), 0.0f, 0.0f, 0.0f };
  644. bgfx::setUniform(u_params, params);
  645. bgfx::setTexture(0
  646. , s_texColor
  647. , texture
  648. , view.m_filter
  649. ? BGFX_TEXTURE_NONE
  650. : 0
  651. | BGFX_TEXTURE_MIN_POINT
  652. | BGFX_TEXTURE_MIP_POINT
  653. | BGFX_TEXTURE_MAG_POINT
  654. );
  655. bgfx::setState(0
  656. | BGFX_STATE_RGB_WRITE
  657. | BGFX_STATE_ALPHA_WRITE
  658. );
  659. bgfx::submit(0, view.m_info.cubeMap ? textureCubeProgram : textureProgram);
  660. bgfx::frame();
  661. }
  662. }
  663. if (bgfx::isValid(texture) )
  664. {
  665. bgfx::destroyTexture(texture);
  666. }
  667. bgfx::destroyUniform(s_texColor);
  668. bgfx::destroyUniform(u_mtx);
  669. bgfx::destroyUniform(u_params);
  670. bgfx::destroyProgram(textureProgram);
  671. bgfx::destroyProgram(textureCubeProgram);
  672. imguiDestroy();
  673. bgfx::shutdown();
  674. return exitcode;
  675. }