mvs.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 init;
  106. init.type = args.m_type;
  107. init.vendorId = args.m_pciId;
  108. init.resolution.width = m_width;
  109. init.resolution.height = m_height;
  110. init.resolution.reset = m_reset;
  111. bgfx::init(init);
  112. // Enable debug text.
  113. bgfx::setDebug(m_debug);
  114. // Set view 0 clear state.
  115. bgfx::setViewClear(0
  116. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  117. , 0x303030ff
  118. , 1.0f
  119. , 0
  120. );
  121. // Create vertex stream declaration.
  122. PosVertex::init();
  123. ColorVertex::init();
  124. // Create static vertex buffer.
  125. m_vbh[0] = bgfx::createVertexBuffer(
  126. // Static data can be passed with bgfx::makeRef
  127. bgfx::makeRef(s_cubePosVertices, sizeof(s_cubePosVertices) )
  128. , PosVertex::ms_decl
  129. );
  130. m_vbh[1] = bgfx::createVertexBuffer(
  131. // Static data can be passed with bgfx::makeRef
  132. bgfx::makeRef(s_cubeColorVertices, sizeof(s_cubeColorVertices) )
  133. , ColorVertex::ms_decl
  134. );
  135. // Create static index buffer.
  136. m_ibh = bgfx::createIndexBuffer(
  137. // Static data can be passed with bgfx::makeRef
  138. bgfx::makeRef(s_cubeTriStrip, sizeof(s_cubeTriStrip) )
  139. );
  140. // Create program from shaders.
  141. m_program = loadProgram("vs_cubes", "fs_cubes");
  142. m_timeOffset = bx::getHPCounter();
  143. imguiCreate();
  144. }
  145. virtual int shutdown() override
  146. {
  147. imguiDestroy();
  148. // Cleanup.
  149. bgfx::destroy(m_ibh);
  150. bgfx::destroy(m_vbh[0]);
  151. bgfx::destroy(m_vbh[1]);
  152. bgfx::destroy(m_program);
  153. // Shutdown bgfx.
  154. bgfx::shutdown();
  155. return 0;
  156. }
  157. bool update() override
  158. {
  159. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  160. {
  161. imguiBeginFrame(m_mouseState.m_mx
  162. , m_mouseState.m_my
  163. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  164. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  165. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  166. , m_mouseState.m_mz
  167. , uint16_t(m_width)
  168. , uint16_t(m_height)
  169. );
  170. showExampleDialog(this);
  171. imguiEndFrame();
  172. float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
  173. float at[3] = { 0.0f, 0.0f, 0.0f };
  174. float eye[3] = { 0.0f, 0.0f, -35.0f };
  175. // Set view and projection matrix for view 0.
  176. {
  177. float view[16];
  178. bx::mtxLookAt(view, eye, at);
  179. float proj[16];
  180. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  181. bgfx::setViewTransform(0, view, proj);
  182. // Set view 0 default viewport.
  183. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  184. }
  185. // This dummy draw call is here to make sure that view 0 is cleared
  186. // if no other draw calls are submitted to view 0.
  187. bgfx::touch(0);
  188. // Submit 11x11 cubes.
  189. for (uint32_t yy = 0; yy < 11; ++yy)
  190. {
  191. for (uint32_t xx = 0; xx < 11; ++xx)
  192. {
  193. float mtx[16];
  194. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  195. mtx[12] = -15.0f + float(xx)*3.0f;
  196. mtx[13] = -15.0f + float(yy)*3.0f;
  197. mtx[14] = 0.0f;
  198. // Set model matrix for rendering.
  199. bgfx::setTransform(mtx);
  200. // Set vertex and index buffer.
  201. bgfx::setVertexBuffer(0, m_vbh[0]);
  202. bgfx::setVertexBuffer(1, m_vbh[1]);
  203. bgfx::setIndexBuffer(m_ibh);
  204. // Set render states.
  205. bgfx::setState(0
  206. | BGFX_STATE_DEFAULT
  207. | BGFX_STATE_PT_TRISTRIP
  208. );
  209. // Submit primitive for rendering to view 0.
  210. bgfx::submit(0, m_program);
  211. }
  212. }
  213. // Advance to next frame. Rendering thread will be kicked to
  214. // process submitted rendering primitives.
  215. bgfx::frame();
  216. return true;
  217. }
  218. return false;
  219. }
  220. entry::MouseState m_mouseState;
  221. uint32_t m_width;
  222. uint32_t m_height;
  223. uint32_t m_debug;
  224. uint32_t m_reset;
  225. bgfx::VertexBufferHandle m_vbh[2];
  226. bgfx::IndexBufferHandle m_ibh;
  227. bgfx::ProgramHandle m_program;
  228. int64_t m_timeOffset;
  229. };
  230. } // namespace
  231. ENTRY_IMPLEMENT_MAIN(ExampleMvs, "34-mvs", "Multiple vertex streams.");