lod.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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_mz
  98. , width
  99. , height
  100. );
  101. imguiBeginScrollArea("Toggle transitions", width - width / 5 - 10, 10, width / 5, height / 6, &scrollArea);
  102. imguiSeparatorLine();
  103. if (imguiButton(transitions ? "ON" : "OFF") )
  104. {
  105. transitions = !transitions;
  106. }
  107. static float distance = 2.0f;
  108. imguiSlider("Distance", distance, 2.0f, 6.0f, .01f);
  109. imguiEndScrollArea();
  110. imguiEndFrame();
  111. // Set view 0 default viewport.
  112. bgfx::setViewRect(0, 0, 0, width, height);
  113. // This dummy draw call is here to make sure that view 0 is cleared
  114. // if no other draw calls are submitted to view 0.
  115. bgfx::touch(0);
  116. int64_t now = bx::getHPCounter();
  117. static int64_t last = now;
  118. const int64_t frameTime = now - last;
  119. last = now;
  120. const double freq = double(bx::getHPFrequency() );
  121. const double toMs = 1000.0/freq;
  122. // Use debug font to print information about this example.
  123. bgfx::dbgTextClear();
  124. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/12-lod");
  125. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Mesh LOD transitions.");
  126. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  127. bgfx::dbgTextPrintf(0, 4, transitions ? 0x2f : 0x1f, transitions ? "Transitions on" : "Transitions off");
  128. eye[2] = -distance;
  129. // Set view and projection matrix for view 0.
  130. const bgfx::HMD* hmd = bgfx::getHMD();
  131. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  132. {
  133. float view[16];
  134. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  135. float proj[16];
  136. bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
  137. bgfx::setViewTransform(0, view, proj);
  138. // Set view 0 default viewport.
  139. //
  140. // Use HMD's width/height since HMD's internal frame buffer size
  141. // might be much larger than window size.
  142. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  143. }
  144. else
  145. {
  146. float view[16];
  147. bx::mtxLookAt(view, eye, at);
  148. float proj[16];
  149. bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  150. bgfx::setViewTransform(0, view, proj);
  151. // Set view 0 default viewport.
  152. bgfx::setViewRect(0, 0, 0, width, 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 = transitions ? 32-transitionFrame : 32;
  159. const int mainLOD = transitions ? currLOD : 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(transitionFrame)*4.0f/255.0f) - (1.0f/255.0f);
  166. bgfx::setTexture(0, s_texColor, textureBark);
  167. bgfx::setTexture(1, s_texStipple, textureStipple);
  168. bgfx::setUniform(u_stipple, stipple);
  169. meshSubmit(meshTrunk[mainLOD], 0, program, mtx, stateOpaque);
  170. bgfx::setTexture(0, s_texColor, textureLeafs);
  171. bgfx::setTexture(1, s_texStipple, textureStipple);
  172. bgfx::setUniform(u_stipple, stipple);
  173. meshSubmit(meshTop[mainLOD], 0, program, mtx, stateTransparent);
  174. if (transitions
  175. && (transitionFrame != 0) )
  176. {
  177. bgfx::setTexture(0, s_texColor, textureBark);
  178. bgfx::setTexture(1, s_texStipple, textureStipple);
  179. bgfx::setUniform(u_stipple, stippleInv);
  180. meshSubmit(meshTrunk[targetLOD], 0, program, mtx, stateOpaque);
  181. bgfx::setTexture(0, s_texColor, textureLeafs);
  182. bgfx::setTexture(1, s_texStipple, textureStipple);
  183. bgfx::setUniform(u_stipple, stippleInv);
  184. meshSubmit(meshTop[targetLOD], 0, program, mtx, stateTransparent);
  185. }
  186. int lod = 0;
  187. if (eye[2] < -2.5f)
  188. {
  189. lod = 1;
  190. }
  191. if (eye[2] < -5.0f)
  192. {
  193. lod = 2;
  194. }
  195. if (targetLOD!=lod)
  196. {
  197. if (targetLOD==currLOD)
  198. {
  199. targetLOD = lod;
  200. }
  201. }
  202. if (currLOD != targetLOD)
  203. {
  204. transitionFrame++;
  205. }
  206. if (transitionFrame>32)
  207. {
  208. currLOD = targetLOD;
  209. transitionFrame = 0;
  210. }
  211. // Advance to next frame. Rendering thread will be kicked to
  212. // process submitted rendering primitives.
  213. bgfx::frame();
  214. }
  215. imguiDestroy();
  216. for (uint32_t ii = 0; ii < 3; ++ii)
  217. {
  218. meshUnload(meshTop[ii]);
  219. meshUnload(meshTrunk[ii]);
  220. }
  221. // Cleanup.
  222. bgfx::destroyProgram(program);
  223. bgfx::destroyUniform(s_texColor);
  224. bgfx::destroyUniform(s_texStipple);
  225. bgfx::destroyUniform(u_stipple);
  226. bgfx::destroyTexture(textureStipple);
  227. bgfx::destroyTexture(textureLeafs);
  228. bgfx::destroyTexture(textureBark);
  229. // Shutdown bgfx.
  230. bgfx::shutdown();
  231. return 0;
  232. }