cubes.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 2011-2017 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. struct PosColorVertex
  8. {
  9. float m_x;
  10. float m_y;
  11. float m_z;
  12. uint32_t m_abgr;
  13. static void init()
  14. {
  15. ms_decl
  16. .begin()
  17. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  18. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  19. .end();
  20. };
  21. static bgfx::VertexDecl ms_decl;
  22. };
  23. bgfx::VertexDecl PosColorVertex::ms_decl;
  24. static PosColorVertex s_cubeVertices[] =
  25. {
  26. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  27. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  28. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  29. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  30. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  31. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  32. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  33. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  34. };
  35. static const uint16_t s_cubeTriList[] =
  36. {
  37. 0, 1, 2, // 0
  38. 1, 3, 2,
  39. 4, 6, 5, // 2
  40. 5, 6, 7,
  41. 0, 2, 4, // 4
  42. 4, 2, 6,
  43. 1, 5, 3, // 6
  44. 5, 7, 3,
  45. 0, 4, 1, // 8
  46. 4, 5, 1,
  47. 2, 3, 6, // 10
  48. 6, 3, 7,
  49. };
  50. static const uint16_t s_cubeTriStrip[] =
  51. {
  52. 0, 1, 2,
  53. 3,
  54. 7,
  55. 1,
  56. 5,
  57. 0,
  58. 4,
  59. 2,
  60. 6,
  61. 7,
  62. 4,
  63. 5,
  64. };
  65. class ExampleCubes : public entry::AppI
  66. {
  67. void init(int _argc, char** _argv) BX_OVERRIDE
  68. {
  69. BX_UNUSED(s_cubeTriList, s_cubeTriStrip);
  70. Args args(_argc, _argv);
  71. m_width = 1280;
  72. m_height = 720;
  73. m_debug = BGFX_DEBUG_TEXT;
  74. m_reset = BGFX_RESET_VSYNC;
  75. bgfx::init(args.m_type, args.m_pciId);
  76. bgfx::reset(m_width, m_height, m_reset);
  77. // Enable debug text.
  78. bgfx::setDebug(m_debug);
  79. // Set view 0 clear state.
  80. bgfx::setViewClear(0
  81. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  82. , 0x303030ff
  83. , 1.0f
  84. , 0
  85. );
  86. // Create vertex stream declaration.
  87. PosColorVertex::init();
  88. // Create static vertex buffer.
  89. m_vbh = bgfx::createVertexBuffer(
  90. // Static data can be passed with bgfx::makeRef
  91. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  92. , PosColorVertex::ms_decl
  93. );
  94. // Create static index buffer.
  95. m_ibh = bgfx::createIndexBuffer(
  96. // Static data can be passed with bgfx::makeRef
  97. bgfx::makeRef(s_cubeTriStrip, sizeof(s_cubeTriStrip) )
  98. );
  99. // Create program from shaders.
  100. m_program = loadProgram("vs_cubes", "fs_cubes");
  101. m_timeOffset = bx::getHPCounter();
  102. }
  103. virtual int shutdown() BX_OVERRIDE
  104. {
  105. // Cleanup.
  106. bgfx::destroyIndexBuffer(m_ibh);
  107. bgfx::destroyVertexBuffer(m_vbh);
  108. bgfx::destroyProgram(m_program);
  109. // Shutdown bgfx.
  110. bgfx::shutdown();
  111. return 0;
  112. }
  113. bool update() BX_OVERRIDE
  114. {
  115. if (!entry::processEvents(m_width, m_height, m_debug, m_reset) )
  116. {
  117. int64_t now = bx::getHPCounter();
  118. static int64_t last = now;
  119. const int64_t frameTime = now - last;
  120. last = now;
  121. const double freq = double(bx::getHPFrequency() );
  122. const double toMs = 1000.0/freq;
  123. float time = (float)( (now-m_timeOffset)/double(bx::getHPFrequency() ) );
  124. // Use debug font to print information about this example.
  125. bgfx::dbgTextClear();
  126. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/01-cube");
  127. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Rendering simple static mesh.");
  128. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  129. float at[3] = { 0.0f, 0.0f, 0.0f };
  130. float eye[3] = { 0.0f, 0.0f, -35.0f };
  131. // Set view and projection matrix for view 0.
  132. const bgfx::HMD* hmd = bgfx::getHMD();
  133. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  134. {
  135. float view[16];
  136. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  137. bgfx::setViewTransform(0, view, hmd->eye[0].projection, BGFX_VIEW_STEREO, hmd->eye[1].projection);
  138. // Set view 0 default viewport.
  139. //
  140. // Use HMD's width/height since HMD's internal frame buffer size
  141. // might be much larger than window size.
  142. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  143. }
  144. else
  145. {
  146. float view[16];
  147. bx::mtxLookAt(view, eye, at);
  148. float proj[16];
  149. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f);
  150. bgfx::setViewTransform(0, view, proj);
  151. // Set view 0 default viewport.
  152. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  153. }
  154. // This dummy draw call is here to make sure that view 0 is cleared
  155. // if no other draw calls are submitted to view 0.
  156. bgfx::touch(0);
  157. // Submit 11x11 cubes.
  158. for (uint32_t yy = 0; yy < 11; ++yy)
  159. {
  160. for (uint32_t xx = 0; xx < 11; ++xx)
  161. {
  162. float mtx[16];
  163. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  164. mtx[12] = -15.0f + float(xx)*3.0f;
  165. mtx[13] = -15.0f + float(yy)*3.0f;
  166. mtx[14] = 0.0f;
  167. // Set model matrix for rendering.
  168. bgfx::setTransform(mtx);
  169. // Set vertex and index buffer.
  170. bgfx::setVertexBuffer(m_vbh);
  171. bgfx::setIndexBuffer(m_ibh);
  172. // Set render states.
  173. bgfx::setState(0
  174. | BGFX_STATE_DEFAULT
  175. | BGFX_STATE_PT_TRISTRIP
  176. );
  177. // Submit primitive for rendering to view 0.
  178. bgfx::submit(0, m_program);
  179. }
  180. }
  181. // Advance to next frame. Rendering thread will be kicked to
  182. // process submitted rendering primitives.
  183. bgfx::frame();
  184. return true;
  185. }
  186. return false;
  187. }
  188. uint32_t m_width;
  189. uint32_t m_height;
  190. uint32_t m_debug;
  191. uint32_t m_reset;
  192. bgfx::VertexBufferHandle m_vbh;
  193. bgfx::IndexBufferHandle m_ibh;
  194. bgfx::ProgramHandle m_program;
  195. int64_t m_timeOffset;
  196. };
  197. ENTRY_IMPLEMENT_MAIN(ExampleCubes);