cubes.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 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_layout
  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::VertexLayout ms_layout;
  25. };
  26. bgfx::VertexLayout PosColorVertex::ms_layout;
  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. static const uint16_t s_cubeLineList[] =
  69. {
  70. 0, 1,
  71. 0, 2,
  72. 0, 4,
  73. 1, 3,
  74. 1, 5,
  75. 2, 3,
  76. 2, 6,
  77. 3, 7,
  78. 4, 5,
  79. 4, 6,
  80. 5, 7,
  81. 6, 7,
  82. };
  83. static const uint16_t s_cubeLineStrip[] =
  84. {
  85. 0, 2, 3, 1, 5, 7, 6, 4,
  86. 0, 2, 6, 4, 5, 7, 3, 1,
  87. 0,
  88. };
  89. static const uint16_t s_cubePoints[] =
  90. {
  91. 0, 1, 2, 3, 4, 5, 6, 7
  92. };
  93. static const char* s_ptNames[]
  94. {
  95. "Triangle List",
  96. "Triangle Strip",
  97. "Lines",
  98. "Line Strip",
  99. "Points",
  100. };
  101. static const uint64_t s_ptState[]
  102. {
  103. UINT64_C(0),
  104. BGFX_STATE_PT_TRISTRIP,
  105. BGFX_STATE_PT_LINES,
  106. BGFX_STATE_PT_LINESTRIP,
  107. BGFX_STATE_PT_POINTS,
  108. };
  109. BX_STATIC_ASSERT(BX_COUNTOF(s_ptState) == BX_COUNTOF(s_ptNames) );
  110. class ExampleCubes : public entry::AppI
  111. {
  112. public:
  113. ExampleCubes(const char* _name, const char* _description, const char* _url)
  114. : entry::AppI(_name, _description, _url)
  115. , m_pt(0)
  116. , m_r(true)
  117. , m_g(true)
  118. , m_b(true)
  119. , m_a(true)
  120. {
  121. }
  122. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  123. {
  124. Args args(_argc, _argv);
  125. m_width = _width;
  126. m_height = _height;
  127. m_debug = BGFX_DEBUG_NONE;
  128. m_reset = BGFX_RESET_VSYNC;
  129. bgfx::Init init;
  130. init.type = args.m_type;
  131. init.vendorId = args.m_pciId;
  132. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  133. init.platformData.ndt = entry::getNativeDisplayHandle();
  134. init.platformData.type = entry::getNativeWindowHandleType();
  135. init.resolution.width = m_width;
  136. init.resolution.height = m_height;
  137. init.resolution.reset = m_reset;
  138. bgfx::init(init);
  139. // Enable debug text.
  140. bgfx::setDebug(m_debug);
  141. // Set view 0 clear state.
  142. bgfx::setViewClear(0
  143. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  144. , 0x303030ff
  145. , 1.0f
  146. , 0
  147. );
  148. // Create vertex stream declaration.
  149. PosColorVertex::init();
  150. // Create static vertex buffer.
  151. m_vbh = bgfx::createVertexBuffer(
  152. // Static data can be passed with bgfx::makeRef
  153. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  154. , PosColorVertex::ms_layout
  155. );
  156. // Create static index buffer for triangle list rendering.
  157. m_ibh[0] = bgfx::createIndexBuffer(
  158. // Static data can be passed with bgfx::makeRef
  159. bgfx::makeRef(s_cubeTriList, sizeof(s_cubeTriList) )
  160. );
  161. // Create static index buffer for triangle strip rendering.
  162. m_ibh[1] = bgfx::createIndexBuffer(
  163. // Static data can be passed with bgfx::makeRef
  164. bgfx::makeRef(s_cubeTriStrip, sizeof(s_cubeTriStrip) )
  165. );
  166. // Create static index buffer for line list rendering.
  167. m_ibh[2] = bgfx::createIndexBuffer(
  168. // Static data can be passed with bgfx::makeRef
  169. bgfx::makeRef(s_cubeLineList, sizeof(s_cubeLineList) )
  170. );
  171. // Create static index buffer for line strip rendering.
  172. m_ibh[3] = bgfx::createIndexBuffer(
  173. // Static data can be passed with bgfx::makeRef
  174. bgfx::makeRef(s_cubeLineStrip, sizeof(s_cubeLineStrip) )
  175. );
  176. // Create static index buffer for point list rendering.
  177. m_ibh[4] = bgfx::createIndexBuffer(
  178. // Static data can be passed with bgfx::makeRef
  179. bgfx::makeRef(s_cubePoints, sizeof(s_cubePoints) )
  180. );
  181. // Create program from shaders.
  182. m_program = loadProgram("vs_cubes", "fs_cubes");
  183. m_timeOffset = bx::getHPCounter();
  184. imguiCreate();
  185. }
  186. virtual int shutdown() override
  187. {
  188. imguiDestroy();
  189. // Cleanup.
  190. for (uint32_t ii = 0; ii < BX_COUNTOF(m_ibh); ++ii)
  191. {
  192. bgfx::destroy(m_ibh[ii]);
  193. }
  194. bgfx::destroy(m_vbh);
  195. bgfx::destroy(m_program);
  196. // Shutdown bgfx.
  197. bgfx::shutdown();
  198. return 0;
  199. }
  200. bool update() override
  201. {
  202. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  203. {
  204. imguiBeginFrame(m_mouseState.m_mx
  205. , m_mouseState.m_my
  206. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  207. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  208. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  209. , m_mouseState.m_mz
  210. , uint16_t(m_width)
  211. , uint16_t(m_height)
  212. );
  213. showExampleDialog(this);
  214. ImGui::SetNextWindowPos(
  215. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  216. , ImGuiCond_FirstUseEver
  217. );
  218. ImGui::SetNextWindowSize(
  219. ImVec2(m_width / 5.0f, m_height / 3.5f)
  220. , ImGuiCond_FirstUseEver
  221. );
  222. ImGui::Begin("Settings"
  223. , NULL
  224. , 0
  225. );
  226. ImGui::Checkbox("Write R", &m_r);
  227. ImGui::Checkbox("Write G", &m_g);
  228. ImGui::Checkbox("Write B", &m_b);
  229. ImGui::Checkbox("Write A", &m_a);
  230. ImGui::Text("Primitive topology:");
  231. ImGui::Combo("##topology", (int*)&m_pt, s_ptNames, BX_COUNTOF(s_ptNames) );
  232. ImGui::End();
  233. imguiEndFrame();
  234. float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
  235. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  236. const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
  237. // Set view and projection matrix for view 0.
  238. {
  239. float view[16];
  240. bx::mtxLookAt(view, eye, at);
  241. float proj[16];
  242. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  243. bgfx::setViewTransform(0, view, proj);
  244. // Set view 0 default viewport.
  245. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  246. }
  247. // This dummy draw call is here to make sure that view 0 is cleared
  248. // if no other draw calls are submitted to view 0.
  249. bgfx::touch(0);
  250. bgfx::IndexBufferHandle ibh = m_ibh[m_pt];
  251. uint64_t state = 0
  252. | (m_r ? BGFX_STATE_WRITE_R : 0)
  253. | (m_g ? BGFX_STATE_WRITE_G : 0)
  254. | (m_b ? BGFX_STATE_WRITE_B : 0)
  255. | (m_a ? BGFX_STATE_WRITE_A : 0)
  256. | BGFX_STATE_WRITE_Z
  257. | BGFX_STATE_DEPTH_TEST_LESS
  258. | BGFX_STATE_CULL_CW
  259. | BGFX_STATE_MSAA
  260. | s_ptState[m_pt]
  261. ;
  262. // Submit 11x11 cubes.
  263. for (uint32_t yy = 0; yy < 11; ++yy)
  264. {
  265. for (uint32_t xx = 0; xx < 11; ++xx)
  266. {
  267. float mtx[16];
  268. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  269. mtx[12] = -15.0f + float(xx)*3.0f;
  270. mtx[13] = -15.0f + float(yy)*3.0f;
  271. mtx[14] = 0.0f;
  272. // Set model matrix for rendering.
  273. bgfx::setTransform(mtx);
  274. // Set vertex and index buffer.
  275. bgfx::setVertexBuffer(0, m_vbh);
  276. bgfx::setIndexBuffer(ibh);
  277. // Set render states.
  278. bgfx::setState(state);
  279. // Submit primitive for rendering to view 0.
  280. bgfx::submit(0, m_program);
  281. }
  282. }
  283. // Advance to next frame. Rendering thread will be kicked to
  284. // process submitted rendering primitives.
  285. bgfx::frame();
  286. return true;
  287. }
  288. return false;
  289. }
  290. entry::MouseState m_mouseState;
  291. uint32_t m_width;
  292. uint32_t m_height;
  293. uint32_t m_debug;
  294. uint32_t m_reset;
  295. bgfx::VertexBufferHandle m_vbh;
  296. bgfx::IndexBufferHandle m_ibh[BX_COUNTOF(s_ptState)];
  297. bgfx::ProgramHandle m_program;
  298. int64_t m_timeOffset;
  299. int32_t m_pt;
  300. bool m_r;
  301. bool m_g;
  302. bool m_b;
  303. bool m_a;
  304. };
  305. } // namespace
  306. ENTRY_IMPLEMENT_MAIN(
  307. ExampleCubes
  308. , "01-cubes"
  309. , "Rendering simple static mesh."
  310. , "https://bkaradzic.github.io/bgfx/examples.html#cubes"
  311. );