lod.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright 2013 Milos Tosic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/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. 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. int _main_(int /*_argc*/, char** /*_argv*/)
  22. {
  23. uint32_t width = 1280;
  24. uint32_t height = 720;
  25. uint32_t debug = BGFX_DEBUG_TEXT;
  26. uint32_t reset = BGFX_RESET_VSYNC;
  27. bgfx::init();
  28. bgfx::reset(width, height, reset);
  29. // Enable debug text.
  30. bgfx::setDebug(debug);
  31. // Set view 0 clear state.
  32. bgfx::setViewClear(0
  33. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  34. , 0x303030ff
  35. , 1.0f
  36. , 0
  37. );
  38. bgfx::UniformHandle u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1iv);
  39. bgfx::UniformHandle u_stipple = bgfx::createUniform("u_stipple", bgfx::UniformType::Uniform3fv);
  40. bgfx::UniformHandle u_texStipple = bgfx::createUniform("u_texStipple", bgfx::UniformType::Uniform1iv);
  41. bgfx::ProgramHandle program = loadProgram("vs_tree", "fs_tree");
  42. bgfx::TextureHandle textureLeafs = loadTexture("leafs1.dds");
  43. bgfx::TextureHandle textureBark = loadTexture("bark1.dds");
  44. bgfx::TextureHandle textureStipple;
  45. const bgfx::Memory* stipple = bgfx::alloc(8*4);
  46. memset(stipple->data, 0, stipple->size);
  47. for (uint32_t ii = 0; ii < 32; ++ii)
  48. {
  49. stipple->data[knightTour[ii].m_y * 8 + knightTour[ii].m_x] = ii*4;
  50. }
  51. textureStipple = bgfx::createTexture2D(8, 4, 1, bgfx::TextureFormat::R8, BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIN_POINT, stipple);
  52. Mesh* meshTop[3] =
  53. {
  54. meshLoad("meshes/tree1b_lod0_1.bin"),
  55. meshLoad("meshes/tree1b_lod1_1.bin"),
  56. meshLoad("meshes/tree1b_lod2_1.bin"),
  57. };
  58. Mesh* meshTrunk[3] =
  59. {
  60. meshLoad("meshes/tree1b_lod0_2.bin"),
  61. meshLoad("meshes/tree1b_lod1_2.bin"),
  62. meshLoad("meshes/tree1b_lod2_2.bin"),
  63. };
  64. // Imgui.
  65. imguiCreate();
  66. const uint64_t stateCommon = 0
  67. | BGFX_STATE_RGB_WRITE
  68. | BGFX_STATE_ALPHA_WRITE
  69. | BGFX_STATE_DEPTH_TEST_LESS
  70. | BGFX_STATE_CULL_CCW
  71. | BGFX_STATE_MSAA
  72. ;
  73. const uint64_t stateTransparent = stateCommon
  74. | BGFX_STATE_BLEND_ALPHA
  75. ;
  76. const uint64_t stateOpaque = stateCommon
  77. | BGFX_STATE_DEPTH_WRITE
  78. ;
  79. int32_t scrollArea = 0;
  80. bool transitions = true;
  81. int transitionFrame = 0;
  82. int currLOD = 0;
  83. int targetLOD = 0;
  84. float at[3] = { 0.0f, 1.0f, 0.0f };
  85. float eye[3] = { 0.0f, 1.0f, -2.0f };
  86. entry::MouseState mouseState;
  87. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  88. {
  89. imguiBeginFrame(mouseState.m_mx
  90. , mouseState.m_my
  91. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  92. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  93. , 0
  94. , width
  95. , height
  96. );
  97. imguiBeginScrollArea("Toggle transitions", width - width / 5 - 10, 10, width / 5, height / 6, &scrollArea);
  98. imguiSeparatorLine();
  99. if (imguiButton(transitions ? "ON" : "OFF") )
  100. {
  101. transitions = !transitions;
  102. }
  103. static float distance = 2.0f;
  104. imguiSlider("Distance", distance, 2.0f, 6.0f, .01f);
  105. imguiEndScrollArea();
  106. imguiEndFrame();
  107. // Set view 0 default viewport.
  108. bgfx::setViewRect(0, 0, 0, width, height);
  109. // This dummy draw call is here to make sure that view 0 is cleared
  110. // if no other draw calls are submitted to view 0.
  111. bgfx::submit(0);
  112. int64_t now = bx::getHPCounter();
  113. static int64_t last = now;
  114. const int64_t frameTime = now - last;
  115. last = now;
  116. const double freq = double(bx::getHPFrequency() );
  117. const double toMs = 1000.0/freq;
  118. // Use debug font to print information about this example.
  119. bgfx::dbgTextClear();
  120. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/12-lod");
  121. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Mesh LOD transitions.");
  122. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  123. bgfx::dbgTextPrintf(0, 4, transitions ? 0x2f : 0x1f, transitions ? "Transitions on" : "Transitions off");
  124. eye[2] = -distance;
  125. // Set view and projection matrix for view 0.
  126. const bgfx::HMD* hmd = bgfx::getHMD();
  127. if (NULL != hmd)
  128. {
  129. float view[16];
  130. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  131. float proj[16];
  132. bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
  133. bgfx::setViewTransform(0, view, proj);
  134. // Set view 0 default viewport.
  135. //
  136. // Use HMD's width/height since HMD's internal frame buffer size
  137. // might be much larger than window size.
  138. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  139. }
  140. else
  141. {
  142. float view[16];
  143. bx::mtxLookAt(view, eye, at);
  144. float proj[16];
  145. bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  146. bgfx::setViewTransform(0, view, proj);
  147. // Set view 0 default viewport.
  148. bgfx::setViewRect(0, 0, 0, width, height);
  149. }
  150. float mtx[16];
  151. bx::mtxScale(mtx, 0.1f, 0.1f, 0.1f);
  152. float stipple[3];
  153. float stippleInv[3];
  154. const int currentLODframe = transitions ? 32-transitionFrame : 32;
  155. const int mainLOD = transitions ? currLOD : targetLOD;
  156. stipple[0] = 0.0f;
  157. stipple[1] = -1.0f;
  158. stipple[2] = (float(currentLODframe)*4.0f/255.0f) - (1.0f/255.0f);
  159. stippleInv[0] = (float(31)*4.0f/255.0f);
  160. stippleInv[1] = 1.0f;
  161. stippleInv[2] = (float(transitionFrame)*4.0f/255.0f) - (1.0f/255.0f);
  162. bgfx::setTexture(0, u_texColor, textureBark);
  163. bgfx::setTexture(1, u_texStipple, textureStipple);
  164. bgfx::setUniform(u_stipple, stipple);
  165. meshSubmit(meshTrunk[mainLOD], 0, program, mtx, stateOpaque);
  166. bgfx::setTexture(0, u_texColor, textureLeafs);
  167. bgfx::setTexture(1, u_texStipple, textureStipple);
  168. bgfx::setUniform(u_stipple, stipple);
  169. meshSubmit(meshTop[mainLOD], 0, program, mtx, stateTransparent);
  170. if (transitions
  171. && (transitionFrame != 0) )
  172. {
  173. bgfx::setTexture(0, u_texColor, textureBark);
  174. bgfx::setTexture(1, u_texStipple, textureStipple);
  175. bgfx::setUniform(u_stipple, stippleInv);
  176. meshSubmit(meshTrunk[targetLOD], 0, program, mtx, stateOpaque);
  177. bgfx::setTexture(0, u_texColor, textureLeafs);
  178. bgfx::setTexture(1, u_texStipple, textureStipple);
  179. bgfx::setUniform(u_stipple, stippleInv);
  180. meshSubmit(meshTop[targetLOD], 0, program, mtx, stateTransparent);
  181. }
  182. int lod = 0;
  183. if (eye[2] < -2.5f)
  184. {
  185. lod = 1;
  186. }
  187. if (eye[2] < -5.0f)
  188. {
  189. lod = 2;
  190. }
  191. if (targetLOD!=lod)
  192. {
  193. if (targetLOD==currLOD)
  194. {
  195. targetLOD = lod;
  196. }
  197. }
  198. if (currLOD != targetLOD)
  199. {
  200. transitionFrame++;
  201. }
  202. if (transitionFrame>32)
  203. {
  204. currLOD = targetLOD;
  205. transitionFrame = 0;
  206. }
  207. // Advance to next frame. Rendering thread will be kicked to
  208. // process submitted rendering primitives.
  209. bgfx::frame();
  210. }
  211. imguiDestroy();
  212. for (uint32_t ii = 0; ii < 3; ++ii)
  213. {
  214. meshUnload(meshTop[ii]);
  215. meshUnload(meshTrunk[ii]);
  216. }
  217. // Cleanup.
  218. bgfx::destroyProgram(program);
  219. bgfx::destroyUniform(u_texColor);
  220. bgfx::destroyUniform(u_stipple);
  221. bgfx::destroyUniform(u_texStipple);
  222. bgfx::destroyTexture(textureStipple);
  223. bgfx::destroyTexture(textureLeafs);
  224. bgfx::destroyTexture(textureBark);
  225. // Shutdown bgfx.
  226. bgfx::shutdown();
  227. return 0;
  228. }