debugdraw.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 <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) 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() override
  64. {
  65. imguiDestroy();
  66. ddDestroy(m_sprite);
  67. ddShutdown();
  68. cameraDestroy();
  69. // Shutdown bgfx.
  70. bgfx::shutdown();
  71. return 0;
  72. }
  73. template<typename Ty>
  74. bool intersect(const Ray& _ray, const Ty& _shape)
  75. {
  76. Hit hit;
  77. if (::intersect(_ray, _shape, &hit) )
  78. {
  79. ddPush();
  80. ddSetWireframe(false);
  81. ddSetColor(0xff0000ff);
  82. float tmp[3];
  83. bx::vec3Mul(tmp, hit.m_normal, 0.7f);
  84. float end[3];
  85. bx::vec3Add(end, hit.m_pos, tmp);
  86. ddDrawCone(hit.m_pos, end, 0.1f);
  87. ddPop();
  88. return true;
  89. }
  90. return false;
  91. }
  92. bool update() override
  93. {
  94. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  95. {
  96. imguiBeginFrame(
  97. m_mouseState.m_mx
  98. , m_mouseState.m_my
  99. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  100. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  101. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  102. , m_mouseState.m_mz
  103. , uint16_t(m_width)
  104. , uint16_t(m_height)
  105. );
  106. showExampleDialog(this);
  107. imguiEndFrame();
  108. int64_t now = bx::getHPCounter() - m_timeOffset;
  109. static int64_t last = now;
  110. const int64_t frameTime = now - last;
  111. last = now;
  112. const double freq = double(bx::getHPFrequency() );
  113. const float deltaTime = float(frameTime/freq);
  114. // Update camera.
  115. cameraUpdate(deltaTime, m_mouseState);
  116. float view[16];
  117. cameraGetViewMtx(view);
  118. float proj[16];
  119. // Set view and projection matrix for view 0.
  120. const bgfx::HMD* hmd = bgfx::getHMD();
  121. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  122. {
  123. float eye[3];
  124. cameraGetPosition(eye);
  125. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  126. bgfx::setViewTransform(0, view, hmd->eye[0].projection, BGFX_VIEW_STEREO, hmd->eye[1].projection);
  127. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  128. }
  129. else
  130. {
  131. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  132. bgfx::setViewTransform(0, view, proj);
  133. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  134. }
  135. float mtxVp[16];
  136. bx::mtxMul(mtxVp, view, proj);
  137. float mtxInvVp[16];
  138. bx::mtxInverse(mtxInvVp, mtxVp);
  139. float zero[3] = {};
  140. float eye[] = { 5.0f, 10.0f, 5.0f };
  141. bx::mtxLookAt(view, eye, zero);
  142. bx::mtxProj(proj, 45.0f, float(m_width)/float(m_height), 1.0f, 15.0f, bgfx::getCaps()->homogeneousDepth);
  143. bx::mtxMul(mtxVp, view, proj);
  144. Ray ray = makeRay(
  145. (float(m_mouseState.m_mx)/float(m_width) * 2.0f - 1.0f)
  146. , -(float(m_mouseState.m_my)/float(m_height) * 2.0f - 1.0f)
  147. , mtxInvVp
  148. );
  149. const uint32_t selected = 0xff80ffff;
  150. ddBegin(0);
  151. ddDrawAxis(0.0f, 0.0f, 0.0f);
  152. ddPush();
  153. Aabb aabb =
  154. {
  155. { 5.0f, 1.0f, 1.0f },
  156. { 10.0f, 5.0f, 5.0f },
  157. };
  158. ddSetWireframe(true);
  159. ddSetColor(intersect(ray, aabb) ? selected : 0xff00ff00);
  160. ddDraw(aabb);
  161. ddPop();
  162. float time = float(now/freq);
  163. Obb obb;
  164. bx::mtxRotateX(obb.m_mtx, time);
  165. ddSetWireframe(true);
  166. ddSetColor(intersect(ray, obb) ? selected : 0xffffffff);
  167. ddDraw(obb);
  168. bx::mtxSRT(obb.m_mtx, 1.0f, 1.0f, 1.0f, time*0.23f, time, 0.0f, 3.0f, 0.0f, 0.0f);
  169. ddPush();
  170. toAabb(aabb, obb);
  171. ddSetWireframe(true);
  172. ddSetColor(0xff0000ff);
  173. ddDraw(aabb);
  174. ddPop();
  175. ddSetWireframe(false);
  176. ddSetColor(intersect(ray, obb) ? selected : 0xffffffff);
  177. ddDraw(obb);
  178. ddSetColor(0xffffffff);
  179. ddSetTranslate(0.0f, -2.0f, 0.0f);
  180. ddDrawGrid(Axis::Y, zero, 20, 1.0f);
  181. ddSetTransform(NULL);
  182. ddDrawFrustum(mtxVp);
  183. ddPush();
  184. Sphere sphere = { { 0.0f, 5.0f, 0.0f }, 1.0f };
  185. ddSetColor(intersect(ray, sphere) ? selected : 0xfff0c0ff);
  186. ddSetWireframe(true);
  187. ddSetLod(3);
  188. ddDraw(sphere);
  189. ddSetWireframe(false);
  190. sphere.m_center[0] = -2.0f;
  191. ddSetColor(intersect(ray, sphere) ? selected : 0xc0ffc0ff);
  192. ddSetLod(2);
  193. ddDraw(sphere);
  194. sphere.m_center[0] = -4.0f;
  195. ddSetColor(intersect(ray, sphere) ? selected : 0xa0f0ffff);
  196. ddSetLod(1);
  197. ddDraw(sphere);
  198. sphere.m_center[0] = -6.0f;
  199. ddSetColor(intersect(ray, sphere) ? selected : 0xffc0ff00);
  200. ddSetLod(0);
  201. ddDraw(sphere);
  202. ddPop();
  203. ddSetColor(0xffffffff);
  204. ddPush();
  205. {
  206. float normal[3] = { 0.0f, 0.0f, 1.0f };
  207. float center[3] = { -8.0f, 0.0f, 0.0f };
  208. ddPush();
  209. ddSetStipple(true, 1.0f, time*0.1f);
  210. ddSetColor(0xff0000ff);
  211. ddDrawCircle(normal, center, 1.0f, 0.5f + bx::fsin(time*10.0f) );
  212. ddPop();
  213. ddSetSpin(time);
  214. ddDrawQuad(m_sprite, normal, center, 2.0f);
  215. }
  216. ddPop();
  217. ddPush();
  218. ddSetStipple(true, 1.0f, -time*0.1f);
  219. ddDrawCircle(Axis::Z, -8.0f, 0.0f, 0.0f, 1.25f, 2.0f);
  220. ddPop();
  221. ddPush();
  222. ddSetLod(UINT8_MAX);
  223. ddPush();
  224. ddSetSpin(time*0.3f);
  225. {
  226. Cone cone =
  227. {
  228. { -11.0f, 4.0f, 0.0f },
  229. { -13.0f, 6.0f, 1.0f },
  230. 1.0f
  231. };
  232. Cylinder cylinder =
  233. {
  234. { -9.0f, 2.0f, -1.0f },
  235. { -11.0f, 4.0f, 0.0f },
  236. 0.5f
  237. };
  238. ddSetColor(false
  239. || intersect(ray, cone)
  240. || intersect(ray, cylinder)
  241. ? selected
  242. : 0xffffffff
  243. );
  244. ddDraw(cone);
  245. ddDraw(cylinder);
  246. }
  247. ddPop();
  248. {
  249. ddSetLod(0);
  250. Capsule capsule =
  251. {
  252. { 0.0f, 7.0f, 0.0f },
  253. { -6.0f, 7.0f, 0.0f },
  254. 0.5f
  255. };
  256. ddSetColor(intersect(ray, capsule) ? selected : 0xffffffff);
  257. ddDraw(capsule);
  258. }
  259. ddPop();
  260. ddPush();
  261. float mtx[16];
  262. bx::mtxSRT(mtx
  263. , 1.0f, 1.0f, 1.0f
  264. , 0.0f, time, time*0.53f
  265. , -10.0f, 1.0f, 10.0f
  266. );
  267. Cylinder cylinder =
  268. {
  269. { -10.0f, 1.0f, 10.0f },
  270. { 0.0f, 0.0f, 0.0f },
  271. 1.0f
  272. };
  273. float up[3] = { 0.0f, 4.0f, 0.0f };
  274. bx::vec3MulMtx(cylinder.m_end, up, mtx);
  275. ddSetColor(intersect(ray, cylinder) ? selected : 0xffffffff);
  276. ddDraw(cylinder);
  277. ddPush();
  278. toAabb(aabb, cylinder);
  279. ddSetWireframe(true);
  280. ddSetColor(0xff0000ff);
  281. ddDraw(aabb);
  282. ddPop();
  283. ddPop();
  284. ddDrawOrb(-11.0f, 0.0f, 0.0f, 1.0f);
  285. ddEnd();
  286. // Advance to next frame. Rendering thread will be kicked to
  287. // process submitted rendering primitives.
  288. bgfx::frame();
  289. return true;
  290. }
  291. return false;
  292. }
  293. entry::MouseState m_mouseState;
  294. SpriteHandle m_sprite;
  295. int64_t m_timeOffset;
  296. uint32_t m_width;
  297. uint32_t m_height;
  298. uint32_t m_debug;
  299. uint32_t m_reset;
  300. };
  301. } // namespace
  302. ENTRY_IMPLEMENT_MAIN(ExampleDebugDraw, "29-debugdraw", "Debug draw.");