main.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright 2014 Kai Jourdan. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. *
  5. */
  6. #include "common.h"
  7. #include "bgfx_utils.h"
  8. #include "vectordisplay.h"
  9. #include "imgui/imgui.h"
  10. namespace
  11. {
  12. struct PosColorVertex
  13. {
  14. float m_x;
  15. float m_y;
  16. float m_z;
  17. uint32_t m_abgr;
  18. static void init()
  19. {
  20. ms_layout
  21. .begin()
  22. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  23. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  24. .end();
  25. }
  26. static bgfx::VertexLayout ms_layout;
  27. };
  28. bgfx::VertexLayout PosColorVertex::ms_layout;
  29. class ExampleVectorDisplay : public entry::AppI
  30. {
  31. public:
  32. ExampleVectorDisplay(const char* _name, const char* _description, const char* _url)
  33. : entry::AppI(_name, _description, _url)
  34. {
  35. }
  36. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  37. {
  38. Args args(_argc, _argv);
  39. m_width = _width;
  40. m_height = _height;
  41. m_debug = BGFX_DEBUG_NONE;
  42. m_reset = BGFX_RESET_VSYNC;
  43. bgfx::Init init;
  44. init.type = args.m_type;
  45. init.vendorId = args.m_pciId;
  46. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  47. init.platformData.ndt = entry::getNativeDisplayHandle();
  48. init.resolution.width = m_width;
  49. init.resolution.height = m_height;
  50. init.resolution.reset = m_reset;
  51. bgfx::init(init);
  52. m_vd.init(bgfx::getCaps()->originBottomLeft);
  53. m_vd.setup(uint16_t(m_width), uint16_t(m_height) );
  54. // Enable debug text.
  55. bgfx::setDebug(m_debug);
  56. // Set view 0 clear state.
  57. bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
  58. // Create vertex stream declaration.
  59. PosColorVertex::init();
  60. m_oldWidth = m_width;
  61. m_oldHeight = m_height;
  62. imguiCreate();
  63. }
  64. virtual int shutdown() override
  65. {
  66. imguiDestroy();
  67. // Cleanup.
  68. m_vd.teardown();
  69. // Shutdown bgfx.
  70. bgfx::shutdown();
  71. return 0;
  72. }
  73. bool update() override
  74. {
  75. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  76. {
  77. imguiBeginFrame(m_mouseState.m_mx
  78. , m_mouseState.m_my
  79. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  80. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  81. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  82. , m_mouseState.m_mz
  83. , uint16_t(m_width)
  84. , uint16_t(m_height)
  85. );
  86. showExampleDialog(this);
  87. imguiEndFrame();
  88. if (m_oldWidth != m_width
  89. || m_oldHeight != m_height)
  90. {
  91. m_oldWidth = m_width;
  92. m_oldHeight = m_height;
  93. m_vd.resize(uint16_t(m_width), uint16_t(m_height) );
  94. }
  95. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  96. const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
  97. float view[16];
  98. float proj[16];
  99. bx::mtxLookAt(view, eye, at);
  100. bx::mtxProj(proj, 60.0f, float(m_width) / float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  101. // Set view and projection matrix for view 0.
  102. bgfx::setViewTransform(0, view, proj);
  103. // Set view 0 default viewport.
  104. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  105. // This dummy draw call is here to make sure that view 0 is cleared
  106. // if no other draw calls are submitted to view 0.
  107. bgfx::touch(0);
  108. m_vd.beginFrame();
  109. //simplex test
  110. m_vd.setDrawColor(0.7f, 0.7f, 1.0f);
  111. m_vd.drawSimplexFont(50.0f, 80.0f, 1.5f, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  112. m_vd.drawSimplexFont(50.0f, 140.0f, 1.5f, "abcdefghijklmnopqrstuvwxyz");
  113. m_vd.drawSimplexFont(50.0f, 200.0f, 1.5f, "!@#$%^&*()-=<>/?;:'\"{}[]|\\+=-_");
  114. m_vd.setDrawColor(1.0f, 0.7f, 0.7f);
  115. //test pattern for lines
  116. for (int ii = 0; ii < 4; ii++)
  117. {
  118. for (int jj = 0; jj < ii; jj++) //draw more intensive lines
  119. {
  120. m_vd.drawLine(50.0f, 350.0f + 40 * ii, 200.0f, 350.0f + 40 * ii);
  121. }
  122. }
  123. for (int ii = 0; ii < 4; ii++)
  124. {
  125. for (int jj = 0; jj <= ii; jj++)
  126. {
  127. m_vd.drawLine(50.0f + 40 * ii, 600.0f, 50.0f + 40 * ii, 700.0f);
  128. }
  129. }
  130. //
  131. // test pattern for shapes
  132. //
  133. m_vd.setDrawColor(0.7f, 0.7f, 1.0f);
  134. m_vd.drawCircle(250.0f, 450.0f, 10.0f, 32.0f);
  135. m_vd.drawCircle(300.0f, 450.0f, 30.0f, 32.0f);
  136. m_vd.drawCircle(400.0f, 450.0f, 60.0f, 32.0f);
  137. m_vd.drawCircle(500.0f, 450.0f, 80.0f, 64.0f);
  138. m_vd.setDrawColor(0.7f, 1.0f, 0.7f);
  139. m_vd.drawBox(250.0f, 600.0f, 10.0f, 10.0f);
  140. m_vd.drawBox(300.0f, 600.0f, 30.0f, 30.0f);
  141. m_vd.drawBox(350.0f, 600.0f, 60.0f, 60.0f);
  142. m_vd.drawBox(450.0f, 600.0f, 80.0f, 80.0f);
  143. m_vd.setDrawColor(1.0f, 0.7f, 1.0f);
  144. m_vd.drawWheel(bx::kPi, 800.0f, 450.0f, 80.0f);
  145. m_vd.drawWheel(3.0f * bx::kPi / 4.0f, 95.0f, 450.0f, 60.0f);
  146. m_vd.drawWheel(bx::kPi / 2.0f, 1150.0f, 450.0f, 30.0f);
  147. m_vd.drawWheel(bx::kPi / 4.0f, 1250.0f, 450.0f, 10.0f);
  148. // draw moving shape
  149. static float counter = 0.0f;
  150. counter += 0.01f;
  151. const float posX = m_width / 2.0f + bx::sin(counter * 3.18378f) * (m_width / 2.0f);
  152. const float posY = m_height / 2.0f + bx::cos(counter) * (m_height / 2.0f);
  153. m_vd.drawCircle(posX, posY, 5.0f, 10.0f);
  154. m_vd.endFrame();
  155. // Advance to next frame. Rendering thread will be kicked to
  156. // process submitted rendering primitives.
  157. bgfx::frame();
  158. return true;
  159. }
  160. return false;
  161. }
  162. entry::MouseState m_mouseState;
  163. uint32_t m_width;
  164. uint32_t m_height;
  165. uint32_t m_debug;
  166. uint32_t m_reset;
  167. VectorDisplay m_vd;
  168. uint32_t m_oldWidth;
  169. uint32_t m_oldHeight;
  170. };
  171. } // namespace
  172. ENTRY_IMPLEMENT_MAIN(
  173. ExampleVectorDisplay
  174. , "23-vectordisplay"
  175. , "Rendering lines as oldschool vectors."
  176. , "https://bkaradzic.github.io/bgfx/examples.html#vectordisplay"
  177. );