debugdraw.cpp 5.1 KB

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