mvs.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright 2011-2023 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  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_layout
  18. .begin()
  19. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  20. .end();
  21. };
  22. static bgfx::VertexLayout ms_layout;
  23. };
  24. bgfx::VertexLayout PosVertex::ms_layout;
  25. struct ColorVertex
  26. {
  27. uint32_t m_abgr;
  28. static void init()
  29. {
  30. ms_layout
  31. .begin()
  32. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  33. .end();
  34. };
  35. static bgfx::VertexLayout ms_layout;
  36. };
  37. bgfx::VertexLayout ColorVertex::ms_layout;
  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, const char* _url)
  94. : entry::AppI(_name, _description, _url)
  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.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  109. init.platformData.ndt = entry::getNativeDisplayHandle();
  110. init.resolution.width = m_width;
  111. init.resolution.height = m_height;
  112. init.resolution.reset = m_reset;
  113. bgfx::init(init);
  114. // Enable debug text.
  115. bgfx::setDebug(m_debug);
  116. // Set view 0 clear state.
  117. bgfx::setViewClear(0
  118. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  119. , 0x303030ff
  120. , 1.0f
  121. , 0
  122. );
  123. // Create vertex stream declaration.
  124. PosVertex::init();
  125. ColorVertex::init();
  126. // Create static vertex buffer.
  127. m_vbh[0] = bgfx::createVertexBuffer(
  128. // Static data can be passed with bgfx::makeRef
  129. bgfx::makeRef(s_cubePosVertices, sizeof(s_cubePosVertices) )
  130. , PosVertex::ms_layout
  131. );
  132. m_vbh[1] = bgfx::createVertexBuffer(
  133. // Static data can be passed with bgfx::makeRef
  134. bgfx::makeRef(s_cubeColorVertices, sizeof(s_cubeColorVertices) )
  135. , ColorVertex::ms_layout
  136. );
  137. // Create static index buffer.
  138. m_ibh = bgfx::createIndexBuffer(
  139. // Static data can be passed with bgfx::makeRef
  140. bgfx::makeRef(s_cubeTriStrip, sizeof(s_cubeTriStrip) )
  141. );
  142. // Create program from shaders.
  143. m_program = loadProgram("vs_cubes", "fs_cubes");
  144. m_timeOffset = bx::getHPCounter();
  145. imguiCreate();
  146. }
  147. virtual int shutdown() override
  148. {
  149. imguiDestroy();
  150. // Cleanup.
  151. bgfx::destroy(m_ibh);
  152. bgfx::destroy(m_vbh[0]);
  153. bgfx::destroy(m_vbh[1]);
  154. bgfx::destroy(m_program);
  155. // Shutdown bgfx.
  156. bgfx::shutdown();
  157. return 0;
  158. }
  159. bool update() override
  160. {
  161. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  162. {
  163. imguiBeginFrame(m_mouseState.m_mx
  164. , m_mouseState.m_my
  165. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  166. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  167. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  168. , m_mouseState.m_mz
  169. , uint16_t(m_width)
  170. , uint16_t(m_height)
  171. );
  172. showExampleDialog(this);
  173. imguiEndFrame();
  174. float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
  175. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  176. const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
  177. // Set view and projection matrix for view 0.
  178. {
  179. float view[16];
  180. bx::mtxLookAt(view, eye, at);
  181. float proj[16];
  182. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  183. bgfx::setViewTransform(0, view, proj);
  184. // Set view 0 default viewport.
  185. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  186. }
  187. // This dummy draw call is here to make sure that view 0 is cleared
  188. // if no other draw calls are submitted to view 0.
  189. bgfx::touch(0);
  190. // Submit 11x11 cubes.
  191. for (uint32_t yy = 0; yy < 11; ++yy)
  192. {
  193. for (uint32_t xx = 0; xx < 11; ++xx)
  194. {
  195. float mtx[16];
  196. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  197. mtx[12] = -15.0f + float(xx)*3.0f;
  198. mtx[13] = -15.0f + float(yy)*3.0f;
  199. mtx[14] = 0.0f;
  200. // Set model matrix for rendering.
  201. bgfx::setTransform(mtx);
  202. // Set vertex and index buffer.
  203. bgfx::setVertexBuffer(0, m_vbh[0]);
  204. bgfx::setVertexBuffer(1, m_vbh[1]);
  205. bgfx::setIndexBuffer(m_ibh);
  206. // Set render states.
  207. bgfx::setState(0
  208. | BGFX_STATE_DEFAULT
  209. | BGFX_STATE_PT_TRISTRIP
  210. );
  211. // Submit primitive for rendering to view 0.
  212. bgfx::submit(0, m_program);
  213. }
  214. }
  215. // Advance to next frame. Rendering thread will be kicked to
  216. // process submitted rendering primitives.
  217. bgfx::frame();
  218. return true;
  219. }
  220. return false;
  221. }
  222. entry::MouseState m_mouseState;
  223. uint32_t m_width;
  224. uint32_t m_height;
  225. uint32_t m_debug;
  226. uint32_t m_reset;
  227. bgfx::VertexBufferHandle m_vbh[2];
  228. bgfx::IndexBufferHandle m_ibh;
  229. bgfx::ProgramHandle m_program;
  230. int64_t m_timeOffset;
  231. };
  232. } // namespace
  233. ENTRY_IMPLEMENT_MAIN(
  234. ExampleMvs
  235. , "34-mvs"
  236. , "Multiple vertex streams."
  237. , "https://bkaradzic.github.io/bgfx/examples.html#mvs"
  238. );