debugdraw.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #include <entry/cmd.h>
  8. #include <entry/input.h>
  9. #include <debugdraw/debugdraw.h>
  10. #include "camera.h"
  11. #include "imgui/imgui.h"
  12. #include <bx/uint32_t.h>
  13. namespace
  14. {
  15. void imageCheckerboard(void* _dst, uint32_t _width, uint32_t _height, uint32_t _step, uint32_t _0, uint32_t _1)
  16. {
  17. uint32_t* dst = (uint32_t*)_dst;
  18. for (uint32_t yy = 0; yy < _height; ++yy)
  19. {
  20. for (uint32_t xx = 0; xx < _width; ++xx)
  21. {
  22. uint32_t abgr = ( (xx/_step)&1) ^ ( (yy/_step)&1) ? _1 : _0;
  23. *dst++ = abgr;
  24. }
  25. }
  26. }
  27. class ExampleDebugDraw : public entry::AppI
  28. {
  29. public:
  30. ExampleDebugDraw(const char* _name, const char* _description)
  31. : entry::AppI(_name, _description)
  32. {
  33. }
  34. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) BX_OVERRIDE
  35. {
  36. Args args(_argc, _argv);
  37. m_width = _width;
  38. m_height = _height;
  39. m_debug = BGFX_DEBUG_NONE;
  40. m_reset = BGFX_RESET_VSYNC | BGFX_RESET_MSAA_X16;
  41. bgfx::init(args.m_type, args.m_pciId);
  42. bgfx::reset(m_width, m_height, m_reset);
  43. // Enable m_debug text.
  44. bgfx::setDebug(m_debug);
  45. // Set view 0 clear state.
  46. bgfx::setViewClear(0
  47. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  48. , 0x303030ff
  49. , 1.0f
  50. , 0
  51. );
  52. m_timeOffset = bx::getHPCounter();
  53. cameraCreate();
  54. const float initialPos[3] = { 0.0f, 2.0f, -12.0f };
  55. cameraSetPosition(initialPos);
  56. cameraSetVerticalAngle(0.0f);
  57. ddInit();
  58. uint8_t data[32*32*4];
  59. imageCheckerboard(data, 32, 32, 4, 0xff808080, 0xffc0c0c0);
  60. m_sprite = ddCreateSprite(32, 32, data);
  61. imguiCreate();
  62. }
  63. virtual int shutdown() BX_OVERRIDE
  64. {
  65. imguiDestroy();
  66. ddDestroy(m_sprite);
  67. ddShutdown();
  68. cameraDestroy();
  69. // Shutdown bgfx.
  70. bgfx::shutdown();
  71. return 0;
  72. }
  73. bool update() BX_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. bool restart = showExampleDialog(this);
  87. imguiEndFrame();
  88. int64_t now = bx::getHPCounter() - m_timeOffset;
  89. static int64_t last = now;
  90. const int64_t frameTime = now - last;
  91. last = now;
  92. const double freq = double(bx::getHPFrequency() );
  93. const float deltaTime = float(frameTime/freq);
  94. // Update camera.
  95. cameraUpdate(deltaTime, m_mouseState);
  96. float view[16];
  97. cameraGetViewMtx(view);
  98. float proj[16];
  99. // Set view and projection matrix for view 0.
  100. const bgfx::HMD* hmd = bgfx::getHMD();
  101. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  102. {
  103. float eye[3];
  104. cameraGetPosition(eye);
  105. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  106. bgfx::setViewTransform(0, view, hmd->eye[0].projection, BGFX_VIEW_STEREO, hmd->eye[1].projection);
  107. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  108. }
  109. else
  110. {
  111. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  112. bgfx::setViewTransform(0, view, proj);
  113. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  114. }
  115. float zero[3] = {};
  116. float mvp[16];
  117. float eye[] = { 5.0f, 10.0f, 5.0f };
  118. bx::mtxLookAt(view, eye, zero);
  119. bx::mtxProj(proj, 45.0f, float(m_width)/float(m_height), 1.0f, 15.0f, bgfx::getCaps()->homogeneousDepth);
  120. bx::mtxMul(mvp, view, proj);
  121. ddBegin(0);
  122. ddDrawAxis(0.0f, 0.0f, 0.0f);
  123. ddPush();
  124. ddSetColor(0xff00ff00);
  125. Aabb aabb =
  126. {
  127. { 5.0f, 1.0f, 1.0f },
  128. { 10.0f, 5.0f, 5.0f },
  129. };
  130. ddDraw(aabb);
  131. ddPop();
  132. float time = float(now/freq);
  133. Obb obb;
  134. bx::mtxRotateX(obb.m_mtx, time);
  135. ddSetWireframe(true);
  136. ddDraw(obb);
  137. ddSetColor(0xffffffff);
  138. bx::mtxSRT(obb.m_mtx, 1.0f, 1.0f, 1.0f, 0.0f, time, 0.0f, 3.0f, 0.0f, 0.0f);
  139. ddSetWireframe(false);
  140. ddDraw(obb);
  141. ddSetTranslate(0.0f, -2.0f, 0.0f);
  142. ddDrawGrid(Axis::Y, zero, 20, 1.0f);
  143. ddSetTransform(NULL);
  144. ddDrawFrustum(mvp);
  145. ddPush();
  146. Sphere sphere = { { 0.0f, 5.0f, 0.0f }, 1.0f };
  147. ddSetColor(0xfff0c0ff);
  148. ddSetWireframe(true);
  149. ddSetLod(3);
  150. ddDraw(sphere);
  151. ddSetWireframe(false);
  152. ddSetColor(0xc0ffc0ff);
  153. sphere.m_center[0] = -2.0f;
  154. ddSetLod(2);
  155. ddDraw(sphere);
  156. ddSetColor(0xa0f0ffff);
  157. sphere.m_center[0] = -4.0f;
  158. ddSetLod(1);
  159. ddDraw(sphere);
  160. ddSetColor(0xffc0ff00);
  161. sphere.m_center[0] = -6.0f;
  162. ddSetLod(0);
  163. ddDraw(sphere);
  164. ddPop();
  165. ddSetColor(0xffffffff);
  166. ddPush();
  167. {
  168. float normal[3] = { 0.0f, 0.0f, 1.0f };
  169. float center[3] = { -8.0f, 0.0f, 0.0f };
  170. ddPush();
  171. ddSetStipple(true, 1.0f, time*0.1f);
  172. ddSetColor(0xff0000ff);
  173. ddDrawCircle(normal, center, 1.0f, 0.5f + bx::fsin(time*10.0f) );
  174. ddPop();
  175. ddSetSpin(time);
  176. ddDrawQuad(m_sprite, normal, center, 2.0f);
  177. }
  178. ddPop();
  179. ddPush();
  180. ddSetStipple(true, 1.0f, -time*0.1f);
  181. ddDrawCircle(Axis::Z, -8.0f, 0.0f, 0.0f, 1.25f, 2.0f);
  182. ddPop();
  183. ddPush();
  184. ddSetLod(UINT8_MAX);
  185. ddPush();
  186. ddSetSpin(time*0.3f);
  187. {
  188. float from[3] = { -11.0f, 4.0f, 0.0f };
  189. float to[3] = { -13.0f, 6.0f, 1.0f };
  190. ddDrawCone(from, to, 1.0f );
  191. }
  192. {
  193. float from[3] = { -9.0f, 2.0f, -1.0f };
  194. float to[3] = { -11.0f, 4.0f, 0.0f };
  195. ddDrawCylinder(from, to, 0.5f );
  196. }
  197. ddPop();
  198. {
  199. float from[3] = { 0.0f, 7.0f, 0.0f };
  200. float to[3] = { -6.0f, 7.0f, 0.0f };
  201. ddDrawCylinder(from, to, 0.5f, true);
  202. }
  203. ddPop();
  204. ddPush();
  205. float mtx[16];
  206. bx::mtxSRT(mtx
  207. , 1.0f, 1.0f, 1.0f
  208. , 0.0f, time, time*0.53f
  209. , -10.0f, 1.0f, 10.0f
  210. );
  211. Cylinder cylinder =
  212. {
  213. { -10.0f, 1.0f, 10.0f },
  214. { 0.0f, 0.0f, 0.0f },
  215. 1.0f
  216. };
  217. float up[3] = { 0.0f, 4.0f, 0.0f };
  218. bx::vec3MulMtx(cylinder.m_end, up, mtx);
  219. ddDraw(cylinder);
  220. toAabb(aabb, cylinder);
  221. ddSetColor(0xff0000ff);
  222. ddDraw(aabb);
  223. ddPop();
  224. ddDrawOrb(-11.0f, 0.0f, 0.0f, 1.0f);
  225. ddEnd();
  226. // Advance to next frame. Rendering thread will be kicked to
  227. // process submitted rendering primitives.
  228. bgfx::frame();
  229. return !restart;
  230. }
  231. return false;
  232. }
  233. entry::MouseState m_mouseState;
  234. SpriteHandle m_sprite;
  235. int64_t m_timeOffset;
  236. uint32_t m_width;
  237. uint32_t m_height;
  238. uint32_t m_debug;
  239. uint32_t m_reset;
  240. };
  241. } // namespace
  242. ENTRY_IMPLEMENT_MAIN(ExampleDebugDraw, "29-debugdraw", "Debug draw.");