cubes.cpp 6.4 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 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. , m_r(true)
  74. , m_g(true)
  75. , m_b(true)
  76. , m_a(true)
  77. {
  78. }
  79. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  80. {
  81. BX_UNUSED(s_cubeTriList, s_cubeTriStrip);
  82. Args args(_argc, _argv);
  83. m_width = _width;
  84. m_height = _height;
  85. m_debug = BGFX_DEBUG_NONE;
  86. m_reset = BGFX_RESET_VSYNC;
  87. bgfx::init(args.m_type, args.m_pciId);
  88. bgfx::reset(m_width, m_height, m_reset);
  89. // Enable debug text.
  90. bgfx::setDebug(m_debug);
  91. // Set view 0 clear state.
  92. bgfx::setViewClear(0
  93. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  94. , 0x303030ff
  95. , 1.0f
  96. , 0
  97. );
  98. // Create vertex stream declaration.
  99. PosColorVertex::init();
  100. // Create static vertex buffer.
  101. m_vbh = bgfx::createVertexBuffer(
  102. // Static data can be passed with bgfx::makeRef
  103. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  104. , PosColorVertex::ms_decl
  105. );
  106. // Create static index buffer.
  107. m_ibh = bgfx::createIndexBuffer(
  108. // Static data can be passed with bgfx::makeRef
  109. bgfx::makeRef(s_cubeTriStrip, sizeof(s_cubeTriStrip) )
  110. );
  111. // Create program from shaders.
  112. m_program = loadProgram("vs_cubes", "fs_cubes");
  113. m_timeOffset = bx::getHPCounter();
  114. imguiCreate();
  115. }
  116. virtual int shutdown() override
  117. {
  118. imguiDestroy();
  119. // Cleanup.
  120. bgfx::destroy(m_ibh);
  121. bgfx::destroy(m_vbh);
  122. bgfx::destroy(m_program);
  123. // Shutdown bgfx.
  124. bgfx::shutdown();
  125. return 0;
  126. }
  127. bool update() override
  128. {
  129. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  130. {
  131. imguiBeginFrame(m_mouseState.m_mx
  132. , m_mouseState.m_my
  133. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  134. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  135. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  136. , m_mouseState.m_mz
  137. , uint16_t(m_width)
  138. , uint16_t(m_height)
  139. );
  140. showExampleDialog(this);
  141. ImGui::SetNextWindowPos(
  142. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  143. , ImGuiCond_FirstUseEver
  144. );
  145. ImGui::SetNextWindowSize(
  146. ImVec2(m_width / 5.0f, m_height / 4.0f)
  147. , ImGuiCond_FirstUseEver
  148. );
  149. ImGui::Begin("Settings"
  150. , NULL
  151. , 0
  152. );
  153. ImGui::Checkbox("Write R", &m_r);
  154. ImGui::Checkbox("Write G", &m_g);
  155. ImGui::Checkbox("Write B", &m_b);
  156. ImGui::Checkbox("Write A", &m_a);
  157. ImGui::End();
  158. imguiEndFrame();
  159. float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
  160. float at[3] = { 0.0f, 0.0f, 0.0f };
  161. float eye[3] = { 0.0f, 0.0f, -35.0f };
  162. // Set view and projection matrix for view 0.
  163. const bgfx::HMD* hmd = bgfx::getHMD();
  164. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  165. {
  166. float view[16];
  167. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  168. bgfx::setViewTransform(0, view, hmd->eye[0].projection, BGFX_VIEW_STEREO, hmd->eye[1].projection);
  169. // Set view 0 default viewport.
  170. //
  171. // Use HMD's width/height since HMD's internal frame buffer size
  172. // might be much larger than window size.
  173. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  174. }
  175. else
  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);
  202. bgfx::setIndexBuffer(m_ibh);
  203. // Set render states.
  204. bgfx::setState(0
  205. | (m_r ? BGFX_STATE_WRITE_R : 0)
  206. | (m_g ? BGFX_STATE_WRITE_G : 0)
  207. | (m_b ? BGFX_STATE_WRITE_B : 0)
  208. | (m_a ? BGFX_STATE_WRITE_A : 0)
  209. | BGFX_STATE_WRITE_Z
  210. | BGFX_STATE_DEPTH_TEST_LESS
  211. | BGFX_STATE_CULL_CW
  212. | BGFX_STATE_MSAA
  213. | BGFX_STATE_PT_TRISTRIP
  214. );
  215. // Submit primitive for rendering to view 0.
  216. bgfx::submit(0, m_program);
  217. }
  218. }
  219. // Advance to next frame. Rendering thread will be kicked to
  220. // process submitted rendering primitives.
  221. bgfx::frame();
  222. return true;
  223. }
  224. return false;
  225. }
  226. entry::MouseState m_mouseState;
  227. uint32_t m_width;
  228. uint32_t m_height;
  229. uint32_t m_debug;
  230. uint32_t m_reset;
  231. bgfx::VertexBufferHandle m_vbh;
  232. bgfx::IndexBufferHandle m_ibh;
  233. bgfx::ProgramHandle m_program;
  234. int64_t m_timeOffset;
  235. bool m_r;
  236. bool m_g;
  237. bool m_b;
  238. bool m_a;
  239. };
  240. } // namespace
  241. ENTRY_IMPLEMENT_MAIN(ExampleCubes, "01-cubes", "Rendering simple static mesh.");