lod.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright 2013 Milos Tosic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. #include "imgui/imgui.h"
  8. #include <bx/readerwriter.h>
  9. namespace
  10. {
  11. struct KnightPos
  12. {
  13. int32_t m_x;
  14. int32_t m_y;
  15. };
  16. static const KnightPos knightTour[8*4] =
  17. {
  18. {0,0}, {1,2}, {3,3}, {4,1}, {5,3}, {7,2}, {6,0}, {5,2},
  19. {7,3}, {6,1}, {4,0}, {3,2}, {2,0}, {0,1}, {1,3}, {2,1},
  20. {0,2}, {1,0}, {2,2}, {0,3}, {1,1}, {3,0}, {4,2}, {5,0},
  21. {7,1}, {6,3}, {5,1}, {7,0}, {6,2}, {4,3}, {3,1}, {2,3},
  22. };
  23. class ExampleLod : public entry::AppI
  24. {
  25. public:
  26. ExampleLod(const char* _name, const char* _description, const char* _url)
  27. : entry::AppI(_name, _description, _url)
  28. {
  29. }
  30. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  31. {
  32. Args args(_argc, _argv);
  33. m_width = _width;
  34. m_height = _height;
  35. m_debug = BGFX_DEBUG_NONE;
  36. m_reset = BGFX_RESET_VSYNC;
  37. bgfx::Init init;
  38. init.type = args.m_type;
  39. init.vendorId = args.m_pciId;
  40. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  41. init.platformData.ndt = entry::getNativeDisplayHandle();
  42. init.resolution.width = m_width;
  43. init.resolution.height = m_height;
  44. init.resolution.reset = m_reset;
  45. bgfx::init(init);
  46. // Enable debug text.
  47. bgfx::setDebug(m_debug);
  48. // Set view 0 clear state.
  49. bgfx::setViewClear(0
  50. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  51. , 0x303030ff
  52. , 1.0f
  53. , 0
  54. );
  55. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
  56. s_texStipple = bgfx::createUniform("s_texStipple", bgfx::UniformType::Sampler);
  57. u_stipple = bgfx::createUniform("u_stipple", bgfx::UniformType::Vec4);
  58. m_program = loadProgram("vs_tree", "fs_tree");
  59. m_textureLeafs = loadTexture("textures/leafs1.dds");
  60. m_textureBark = loadTexture("textures/bark1.dds");
  61. const bgfx::Memory* stippleTex = bgfx::alloc(8*4);
  62. bx::memSet(stippleTex->data, 0, stippleTex->size);
  63. for (uint8_t ii = 0; ii < 32; ++ii)
  64. {
  65. stippleTex->data[knightTour[ii].m_y * 8 + knightTour[ii].m_x] = ii*4;
  66. }
  67. m_textureStipple = bgfx::createTexture2D(8, 4, false, 1
  68. , bgfx::TextureFormat::R8
  69. , BGFX_SAMPLER_MAG_POINT|BGFX_SAMPLER_MIN_POINT
  70. , stippleTex
  71. );
  72. m_meshTop[0] = meshLoad("meshes/tree1b_lod0_1.bin");
  73. m_meshTop[1] = meshLoad("meshes/tree1b_lod1_1.bin");
  74. m_meshTop[2] = meshLoad("meshes/tree1b_lod2_1.bin");
  75. m_meshTrunk[0] = meshLoad("meshes/tree1b_lod0_2.bin");
  76. m_meshTrunk[1] = meshLoad("meshes/tree1b_lod1_2.bin");
  77. m_meshTrunk[2] = meshLoad("meshes/tree1b_lod2_2.bin");
  78. // Imgui.
  79. imguiCreate();
  80. m_scrollArea = 0;
  81. m_transitions = true;
  82. m_transitionFrame = 0;
  83. m_currLod = 0;
  84. m_targetLod = 0;
  85. }
  86. virtual int shutdown() override
  87. {
  88. imguiDestroy();
  89. for (uint32_t ii = 0; ii < BX_COUNTOF(m_meshTop); ++ii)
  90. {
  91. meshUnload(m_meshTop[ii]);
  92. meshUnload(m_meshTrunk[ii]);
  93. }
  94. // Cleanup.
  95. bgfx::destroy(m_program);
  96. bgfx::destroy(s_texColor);
  97. bgfx::destroy(s_texStipple);
  98. bgfx::destroy(u_stipple);
  99. bgfx::destroy(m_textureStipple);
  100. bgfx::destroy(m_textureLeafs);
  101. bgfx::destroy(m_textureBark);
  102. // Shutdown bgfx.
  103. bgfx::shutdown();
  104. return 0;
  105. }
  106. bool update() override
  107. {
  108. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  109. {
  110. imguiBeginFrame(m_mouseState.m_mx
  111. , m_mouseState.m_my
  112. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  113. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  114. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  115. , m_mouseState.m_mz
  116. , uint16_t(m_width)
  117. , uint16_t(m_height)
  118. );
  119. showExampleDialog(this);
  120. ImGui::SetNextWindowPos(
  121. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  122. , ImGuiCond_FirstUseEver
  123. );
  124. ImGui::SetNextWindowSize(
  125. ImVec2(m_width / 5.0f, m_height / 2.0f)
  126. , ImGuiCond_FirstUseEver
  127. );
  128. ImGui::Begin("Settings"
  129. , NULL
  130. , 0
  131. );
  132. ImGui::Checkbox("Transition", &m_transitions);
  133. static float distance = 2.0f;
  134. ImGui::SliderFloat("Distance", &distance, 2.0f, 6.0f);
  135. ImGui::End();
  136. imguiEndFrame();
  137. // Set view 0 default viewport.
  138. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  139. // This dummy draw call is here to make sure that view 0 is cleared
  140. // if no other draw calls are submitted to view 0.
  141. bgfx::touch(0);
  142. const bx::Vec3 at = { 0.0f, 1.0f, 0.0f };
  143. const bx::Vec3 eye = { 0.0f, 2.0f, -distance };
  144. // Set view and projection matrix for view 0.
  145. {
  146. float view[16];
  147. bx::mtxLookAt(view, eye, at);
  148. float proj[16];
  149. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  150. bgfx::setViewTransform(0, view, proj);
  151. // Set view 0 default viewport.
  152. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  153. }
  154. float mtx[16];
  155. bx::mtxScale(mtx, 0.1f, 0.1f, 0.1f);
  156. float stipple[3];
  157. float stippleInv[3];
  158. const int currentLODframe = m_transitions ? 32-m_transitionFrame : 32;
  159. const int mainLOD = m_transitions ? m_currLod : m_targetLod;
  160. stipple[0] = 0.0f;
  161. stipple[1] = -1.0f;
  162. stipple[2] = (float(currentLODframe)*4.0f/255.0f) - (1.0f/255.0f);
  163. stippleInv[0] = (float(31)*4.0f/255.0f);
  164. stippleInv[1] = 1.0f;
  165. stippleInv[2] = (float(m_transitionFrame)*4.0f/255.0f) - (1.0f/255.0f);
  166. const uint64_t stateTransparent = 0
  167. | BGFX_STATE_WRITE_RGB
  168. | BGFX_STATE_WRITE_A
  169. | BGFX_STATE_DEPTH_TEST_LESS
  170. | BGFX_STATE_CULL_CCW
  171. | BGFX_STATE_MSAA
  172. | BGFX_STATE_BLEND_ALPHA
  173. ;
  174. const uint64_t stateOpaque = BGFX_STATE_DEFAULT;
  175. bgfx::setTexture(0, s_texColor, m_textureBark);
  176. bgfx::setTexture(1, s_texStipple, m_textureStipple);
  177. bgfx::setUniform(u_stipple, stipple);
  178. meshSubmit(m_meshTrunk[mainLOD], 0, m_program, mtx, stateOpaque);
  179. bgfx::setTexture(0, s_texColor, m_textureLeafs);
  180. bgfx::setTexture(1, s_texStipple, m_textureStipple);
  181. bgfx::setUniform(u_stipple, stipple);
  182. meshSubmit(m_meshTop[mainLOD], 0, m_program, mtx, stateTransparent);
  183. if (m_transitions
  184. && (m_transitionFrame != 0) )
  185. {
  186. bgfx::setTexture(0, s_texColor, m_textureBark);
  187. bgfx::setTexture(1, s_texStipple, m_textureStipple);
  188. bgfx::setUniform(u_stipple, stippleInv);
  189. meshSubmit(m_meshTrunk[m_targetLod], 0, m_program, mtx, stateOpaque);
  190. bgfx::setTexture(0, s_texColor, m_textureLeafs);
  191. bgfx::setTexture(1, s_texStipple, m_textureStipple);
  192. bgfx::setUniform(u_stipple, stippleInv);
  193. meshSubmit(m_meshTop[m_targetLod], 0, m_program, mtx, stateTransparent);
  194. }
  195. int lod = 0;
  196. if (eye.z < -2.5f)
  197. {
  198. lod = 1;
  199. }
  200. if (eye.z < -5.0f)
  201. {
  202. lod = 2;
  203. }
  204. if (m_targetLod != lod)
  205. {
  206. if (m_targetLod == m_currLod)
  207. {
  208. m_targetLod = lod;
  209. }
  210. }
  211. if (m_currLod != m_targetLod)
  212. {
  213. m_transitionFrame++;
  214. }
  215. if (m_transitionFrame > 32)
  216. {
  217. m_currLod = m_targetLod;
  218. m_transitionFrame = 0;
  219. }
  220. // Advance to next frame. Rendering thread will be kicked to
  221. // process submitted rendering primitives.
  222. bgfx::frame();
  223. return true;
  224. }
  225. return false;
  226. }
  227. entry::MouseState m_mouseState;
  228. uint32_t m_width;
  229. uint32_t m_height;
  230. uint32_t m_debug;
  231. uint32_t m_reset;
  232. Mesh* m_meshTop[3];
  233. Mesh* m_meshTrunk[3];
  234. bgfx::ProgramHandle m_program;
  235. bgfx::UniformHandle s_texColor;
  236. bgfx::UniformHandle s_texStipple;
  237. bgfx::UniformHandle u_stipple;
  238. bgfx::TextureHandle m_textureStipple;
  239. bgfx::TextureHandle m_textureLeafs;
  240. bgfx::TextureHandle m_textureBark;
  241. int32_t m_scrollArea;
  242. int32_t m_transitionFrame;
  243. int32_t m_currLod;
  244. int32_t m_targetLod;
  245. bool m_transitions;
  246. };
  247. } // namespace
  248. ENTRY_IMPLEMENT_MAIN(
  249. ExampleLod
  250. , "12-lod"
  251. , "Mesh LOD transitions."
  252. , "https://bkaradzic.github.io/bgfx/examples.html#lod"
  253. );