debugdraw.cpp 6.2 KB

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