cubes.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. #include "imgui/imgui.h"
  8. namespace
  9. {
  10. struct PosColorVertex
  11. {
  12. float m_x;
  13. float m_y;
  14. float m_z;
  15. uint32_t m_abgr;
  16. static void init()
  17. {
  18. ms_decl
  19. .begin()
  20. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  21. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  22. .end();
  23. };
  24. static bgfx::VertexDecl ms_decl;
  25. };
  26. bgfx::VertexDecl PosColorVertex::ms_decl;
  27. static PosColorVertex s_cubeVertices[] =
  28. {
  29. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  30. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  31. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  32. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  33. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  34. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  35. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  36. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  37. };
  38. static const uint16_t s_cubeTriList[] =
  39. {
  40. 0, 1, 2, // 0
  41. 1, 3, 2,
  42. 4, 6, 5, // 2
  43. 5, 6, 7,
  44. 0, 2, 4, // 4
  45. 4, 2, 6,
  46. 1, 5, 3, // 6
  47. 5, 7, 3,
  48. 0, 4, 1, // 8
  49. 4, 5, 1,
  50. 2, 3, 6, // 10
  51. 6, 3, 7,
  52. };
  53. static const uint16_t s_cubeTriStrip[] =
  54. {
  55. 0, 1, 2,
  56. 3,
  57. 7,
  58. 1,
  59. 5,
  60. 0,
  61. 4,
  62. 2,
  63. 6,
  64. 7,
  65. 4,
  66. 5,
  67. };
  68. class ExampleCubes : public entry::AppI
  69. {
  70. public:
  71. ExampleCubes(const char* _name, const char* _description)
  72. : entry::AppI(_name, _description)
  73. {
  74. }
  75. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) BX_OVERRIDE
  76. {
  77. BX_UNUSED(s_cubeTriList, s_cubeTriStrip);
  78. Args args(_argc, _argv);
  79. m_width = _width;
  80. m_height = _height;
  81. m_debug = BGFX_DEBUG_NONE;
  82. m_reset = BGFX_RESET_VSYNC;
  83. bgfx::init(args.m_type, args.m_pciId);
  84. bgfx::reset(m_width, m_height, m_reset);
  85. // Enable debug text.
  86. bgfx::setDebug(m_debug);
  87. // Set view 0 clear state.
  88. bgfx::setViewClear(0
  89. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  90. , 0x303030ff
  91. , 1.0f
  92. , 0
  93. );
  94. // Create vertex stream declaration.
  95. PosColorVertex::init();
  96. // Create static vertex buffer.
  97. m_vbh = bgfx::createVertexBuffer(
  98. // Static data can be passed with bgfx::makeRef
  99. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  100. , PosColorVertex::ms_decl
  101. );
  102. // Create static index buffer.
  103. m_ibh = bgfx::createIndexBuffer(
  104. // Static data can be passed with bgfx::makeRef
  105. bgfx::makeRef(s_cubeTriStrip, sizeof(s_cubeTriStrip) )
  106. );
  107. // Create program from shaders.
  108. m_program = loadProgram("vs_cubes", "fs_cubes");
  109. m_timeOffset = bx::getHPCounter();
  110. imguiCreate();
  111. }
  112. virtual int shutdown() BX_OVERRIDE
  113. {
  114. imguiDestroy();
  115. // Cleanup.
  116. bgfx::destroyIndexBuffer(m_ibh);
  117. bgfx::destroyVertexBuffer(m_vbh);
  118. bgfx::destroyProgram(m_program);
  119. // Shutdown bgfx.
  120. bgfx::shutdown();
  121. return 0;
  122. }
  123. bool update() BX_OVERRIDE
  124. {
  125. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  126. {
  127. imguiBeginFrame(m_mouseState.m_mx
  128. , m_mouseState.m_my
  129. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  130. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  131. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  132. , m_mouseState.m_mz
  133. , uint16_t(m_width)
  134. , uint16_t(m_height)
  135. );
  136. bool restart = showExampleDialog(this);
  137. imguiEndFrame();
  138. float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
  139. float at[3] = { 0.0f, 0.0f, 0.0f };
  140. float eye[3] = { 0.0f, 0.0f, -35.0f };
  141. // Set view and projection matrix for view 0.
  142. const bgfx::HMD* hmd = bgfx::getHMD();
  143. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  144. {
  145. float view[16];
  146. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  147. bgfx::setViewTransform(0, view, hmd->eye[0].projection, BGFX_VIEW_STEREO, hmd->eye[1].projection);
  148. // Set view 0 default viewport.
  149. //
  150. // Use HMD's width/height since HMD's internal frame buffer size
  151. // might be much larger than window size.
  152. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  153. }
  154. else
  155. {
  156. float view[16];
  157. bx::mtxLookAt(view, eye, at);
  158. float proj[16];
  159. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  160. bgfx::setViewTransform(0, view, proj);
  161. // Set view 0 default viewport.
  162. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  163. }
  164. // This dummy draw call is here to make sure that view 0 is cleared
  165. // if no other draw calls are submitted to view 0.
  166. bgfx::touch(0);
  167. // Submit 11x11 cubes.
  168. for (uint32_t yy = 0; yy < 11; ++yy)
  169. {
  170. for (uint32_t xx = 0; xx < 11; ++xx)
  171. {
  172. float mtx[16];
  173. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  174. mtx[12] = -15.0f + float(xx)*3.0f;
  175. mtx[13] = -15.0f + float(yy)*3.0f;
  176. mtx[14] = 0.0f;
  177. // Set model matrix for rendering.
  178. bgfx::setTransform(mtx);
  179. // Set vertex and index buffer.
  180. bgfx::setVertexBuffer(0, m_vbh);
  181. bgfx::setIndexBuffer(m_ibh);
  182. // Set render states.
  183. bgfx::setState(0
  184. | BGFX_STATE_DEFAULT
  185. | BGFX_STATE_PT_TRISTRIP
  186. );
  187. // Submit primitive for rendering to view 0.
  188. bgfx::submit(0, m_program);
  189. }
  190. }
  191. // Advance to next frame. Rendering thread will be kicked to
  192. // process submitted rendering primitives.
  193. bgfx::frame();
  194. return !restart;
  195. }
  196. return false;
  197. }
  198. entry::MouseState m_mouseState;
  199. uint32_t m_width;
  200. uint32_t m_height;
  201. uint32_t m_debug;
  202. uint32_t m_reset;
  203. bgfx::VertexBufferHandle m_vbh;
  204. bgfx::IndexBufferHandle m_ibh;
  205. bgfx::ProgramHandle m_program;
  206. int64_t m_timeOffset;
  207. };
  208. } // namespace
  209. ENTRY_IMPLEMENT_MAIN(ExampleCubes, "01-cube", "Rendering simple static mesh.");