mvs.cpp 6.1 KB

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