lod.cpp 7.2 KB

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