lod.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright 2013 Milos Tosic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <string>
  6. #include <vector>
  7. #include "common.h"
  8. #include "bgfx_utils.h"
  9. #include "imgui/imgui.h"
  10. #include <bx/readerwriter.h>
  11. struct KnightPos
  12. {
  13. int32_t m_x;
  14. int32_t m_y;
  15. };
  16. KnightPos knightTour[8*4] =
  17. {
  18. {0,0}, {1,2}, {3,3}, {4,1}, {5,3}, {7,2}, {6,0}, {5,2},
  19. {7,3}, {6,1}, {4,0}, {3,2}, {2,0}, {0,1}, {1,3}, {2,1},
  20. {0,2}, {1,0}, {2,2}, {0,3}, {1,1}, {3,0}, {4,2}, {5,0},
  21. {7,1}, {6,3}, {5,1}, {7,0}, {6,2}, {4,3}, {3,1}, {2,3}
  22. };
  23. int _main_(int /*_argc*/, char** /*_argv*/)
  24. {
  25. uint32_t width = 1280;
  26. uint32_t height = 720;
  27. uint32_t debug = BGFX_DEBUG_TEXT;
  28. uint32_t reset = BGFX_RESET_VSYNC;
  29. bgfx::init();
  30. bgfx::reset(width, height, reset);
  31. // Enable debug text.
  32. bgfx::setDebug(debug);
  33. // Set view 0 clear state.
  34. bgfx::setViewClear(0
  35. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  36. , 0x303030ff
  37. , 1.0f
  38. , 0
  39. );
  40. bgfx::UniformHandle u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1iv);
  41. bgfx::UniformHandle u_stipple = bgfx::createUniform("u_stipple", bgfx::UniformType::Uniform3fv);
  42. bgfx::UniformHandle u_texStipple = bgfx::createUniform("u_texStipple", bgfx::UniformType::Uniform1iv);
  43. bgfx::ProgramHandle program = loadProgram("vs_tree", "fs_tree");
  44. bgfx::TextureHandle textureLeafs = loadTexture("leafs1.dds");
  45. bgfx::TextureHandle textureBark = loadTexture("bark1.dds");
  46. bgfx::TextureHandle textureStipple;
  47. const bgfx::Memory* stipple = bgfx::alloc(8*4);
  48. memset(stipple->data, 0, stipple->size);
  49. for (uint32_t ii = 0; ii < 32; ++ii)
  50. {
  51. stipple->data[knightTour[ii].m_y * 8 + knightTour[ii].m_x] = ii*4;
  52. }
  53. textureStipple = bgfx::createTexture2D(8, 4, 1, bgfx::TextureFormat::R8, BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIN_POINT, stipple);
  54. Mesh* meshTop[3] =
  55. {
  56. meshLoad("meshes/tree1b_lod0_1.bin"),
  57. meshLoad("meshes/tree1b_lod1_1.bin"),
  58. meshLoad("meshes/tree1b_lod2_1.bin"),
  59. };
  60. Mesh* meshTrunk[3] =
  61. {
  62. meshLoad("meshes/tree1b_lod0_2.bin"),
  63. meshLoad("meshes/tree1b_lod1_2.bin"),
  64. meshLoad("meshes/tree1b_lod2_2.bin"),
  65. };
  66. // Imgui.
  67. void* data = load("font/droidsans.ttf");
  68. imguiCreate(data);
  69. free(data);
  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. , 0
  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::submit(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. float view[16];
  130. float proj[16];
  131. bx::mtxLookAt(view, eye, at);
  132. bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  133. // Set view and projection matrix for view 0.
  134. bgfx::setViewTransform(0, view, proj);
  135. float mtx[16];
  136. bx::mtxIdentity(mtx);
  137. float stipple[3];
  138. float stippleInv[3];
  139. const int currentLODframe = transitions ? 32-transitionFrame : 32;
  140. const int mainLOD = transitions ? currLOD : targetLOD;
  141. stipple[0] = 0.0f;
  142. stipple[1] = -1.0f;
  143. stipple[2] = (float(currentLODframe)*4.0f/255.0f) - (1.0f/255.0f);
  144. stippleInv[0] = (float(31)*4.0f/255.0f);
  145. stippleInv[1] = 1.0f;
  146. stippleInv[2] = (float(transitionFrame)*4.0f/255.0f) - (1.0f/255.0f);
  147. bgfx::setTexture(0, u_texColor, textureBark);
  148. bgfx::setTexture(1, u_texStipple, textureStipple);
  149. bgfx::setUniform(u_stipple, stipple);
  150. meshSubmit(meshTrunk[mainLOD], 0, program, mtx, stateOpaque);
  151. bgfx::setTexture(0, u_texColor, textureLeafs);
  152. bgfx::setTexture(1, u_texStipple, textureStipple);
  153. bgfx::setUniform(u_stipple, stipple);
  154. meshSubmit(meshTop[mainLOD], 0, program, mtx, stateTransparent);
  155. if (transitions
  156. && (transitionFrame != 0) )
  157. {
  158. bgfx::setTexture(0, u_texColor, textureBark);
  159. bgfx::setTexture(1, u_texStipple, textureStipple);
  160. bgfx::setUniform(u_stipple, stippleInv);
  161. meshSubmit(meshTrunk[targetLOD], 0, program, mtx, stateOpaque);
  162. bgfx::setTexture(0, u_texColor, textureLeafs);
  163. bgfx::setTexture(1, u_texStipple, textureStipple);
  164. bgfx::setUniform(u_stipple, stippleInv);
  165. meshSubmit(meshTop[targetLOD], 0, program, mtx, stateTransparent);
  166. }
  167. int lod = 0;
  168. if (eye[2] < -2.5f)
  169. {
  170. lod = 1;
  171. }
  172. if (eye[2] < -5.0f)
  173. {
  174. lod = 2;
  175. }
  176. if (targetLOD!=lod)
  177. {
  178. if (targetLOD==currLOD)
  179. {
  180. targetLOD = lod;
  181. }
  182. }
  183. if (currLOD != targetLOD)
  184. {
  185. transitionFrame++;
  186. }
  187. if (transitionFrame>32)
  188. {
  189. currLOD = targetLOD;
  190. transitionFrame = 0;
  191. }
  192. // Advance to next frame. Rendering thread will be kicked to
  193. // process submitted rendering primitives.
  194. bgfx::frame();
  195. }
  196. imguiDestroy();
  197. for (uint32_t ii = 0; ii < 3; ++ii)
  198. {
  199. meshUnload(meshTop[ii]);
  200. meshUnload(meshTrunk[ii]);
  201. }
  202. // Cleanup.
  203. bgfx::destroyProgram(program);
  204. bgfx::destroyUniform(u_texColor);
  205. bgfx::destroyUniform(u_stipple);
  206. bgfx::destroyUniform(u_texStipple);
  207. bgfx::destroyTexture(textureStipple);
  208. bgfx::destroyTexture(textureLeafs);
  209. bgfx::destroyTexture(textureBark);
  210. // Shutdown bgfx.
  211. bgfx::shutdown();
  212. return 0;
  213. }