lod.cpp 8.0 KB

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