lod.cpp 7.3 KB

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