2
0

lod.cpp 8.1 KB

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