DebugDraw.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "graphics/dgl.h"
  23. #include "2d/scene/DebugDraw.h"
  24. // Debug Profiling.
  25. #include "debug/profiler.h"
  26. //-----------------------------------------------------------------------------
  27. void DebugDraw::DrawAABB( const b2AABB& aabb, const ColorF& color )
  28. {
  29. // Debug Profiling.
  30. PROFILE_SCOPE(DebugDraw_DrawAABB);
  31. // Calculate AABB vertices.
  32. b2Vec2 aabbVertex[4];
  33. aabbVertex[0].Set(aabb.lowerBound.x, aabb.lowerBound.y);
  34. aabbVertex[1].Set(aabb.upperBound.x, aabb.lowerBound.y);
  35. aabbVertex[2].Set(aabb.upperBound.x, aabb.upperBound.y);
  36. aabbVertex[3].Set(aabb.lowerBound.x, aabb.upperBound.y);
  37. DrawPolygon( aabbVertex, 4, color );
  38. }
  39. //-----------------------------------------------------------------------------
  40. void DebugDraw::DrawOOBB( const b2Vec2* pOOBB, const ColorF& color )
  41. {
  42. // Debug Profiling.
  43. PROFILE_SCOPE(DebugDraw_DrawOOBB);
  44. DrawPolygon( pOOBB, 4, color );
  45. }
  46. //-----------------------------------------------------------------------------
  47. void DebugDraw::DrawAsleep( const b2Vec2* pOOBB, const ColorF& color )
  48. {
  49. // Debug Profiling.
  50. PROFILE_SCOPE(DebugDraw_DrawAsleep);
  51. DrawSegment( pOOBB[0], pOOBB[2], color );
  52. DrawSegment( pOOBB[1], pOOBB[3], color );
  53. }
  54. //-----------------------------------------------------------------------------
  55. void DebugDraw::DrawCollisionShapes( const b2Transform& xf, b2Body* pBody )
  56. {
  57. // Debug Profiling.
  58. PROFILE_SCOPE(DebugDraw_DrawCollisionShapes);
  59. // Iterate fixtures.
  60. for ( b2Fixture* pFixture = pBody->GetFixtureList(); pFixture; pFixture = pFixture->GetNext() )
  61. {
  62. // Inactive fixture.
  63. if ( pBody->IsActive() == false )
  64. {
  65. DrawShape(pFixture, xf, ColorF(0.5f, 0.5f, 0.5f));
  66. }
  67. // Active static fixture.
  68. else if ( pBody->GetType() == b2_staticBody )
  69. {
  70. DrawShape(pFixture, xf, ColorF(0.5f, 0.9f, 0.5f));
  71. }
  72. // Active kinematic fixture.
  73. else if ( pBody->GetType() == b2_kinematicBody )
  74. {
  75. DrawShape(pFixture, xf, ColorF(0.5f, 0.5f, 0.9f));
  76. }
  77. // Active, asleep dynamic fixture.
  78. else if ( pBody->IsAwake() == false )
  79. {
  80. DrawShape(pFixture, xf, ColorF(0.6f, 0.6f, 0.2f));
  81. }
  82. // Active, awake dynamic fixture.
  83. else
  84. {
  85. DrawShape(pFixture, xf, ColorF(0.9f, 0.9f, 0.2f));
  86. }
  87. }
  88. }
  89. //-----------------------------------------------------------------------------
  90. void DebugDraw::DrawSortPoint( const b2Vec2& worldPosition, const b2Vec2& size, const b2Vec2& localSortPoint )
  91. {
  92. // Debug Profiling.
  93. PROFILE_SCOPE(DebugDraw_DrawSortPoint);
  94. // Calculate sort point marker size based upon the object size.
  95. const b2Vec2 halfSize( size.x * 0.5f, size.y * 0.5f );
  96. const b2Vec2 objectSize( size.x * 0.2f, size.y * 0.2f );
  97. const F32 markerSize = getMin(objectSize.x, objectSize.y);
  98. // Calculate world sort point.
  99. b2Vec2 worldPoint( worldPosition.x + (localSortPoint.x * halfSize.x), worldPosition.y + (localSortPoint.y * halfSize.y) );
  100. // Calculate The Sort Point "X".
  101. b2Vec2 worldSortPoint[4];
  102. worldSortPoint[0].Set( worldPoint.x - markerSize, worldPoint.y - markerSize );
  103. worldSortPoint[1].Set( worldPoint.x + markerSize, worldPoint.y + markerSize );
  104. worldSortPoint[2].Set( worldPoint.x - markerSize, worldPoint.y + markerSize );
  105. worldSortPoint[3].Set( worldPoint.x + markerSize, worldPoint.y - markerSize );
  106. DrawSegment( worldSortPoint[0], worldSortPoint[1], ColorF( 0.0f, 1.0f, 0.8f ) );
  107. DrawSegment( worldSortPoint[2], worldSortPoint[3], ColorF( 0.0f, 1.0f, 0.8f ) );
  108. }
  109. //-----------------------------------------------------------------------------
  110. void DebugDraw::DrawShape( b2Fixture* fixture, const b2Transform& xf, const ColorF& color )
  111. {
  112. // Debug Profiling.
  113. PROFILE_SCOPE(DebugDraw_DrawShape);
  114. switch (fixture->GetType())
  115. {
  116. case b2Shape::e_circle:
  117. {
  118. b2CircleShape* circle = (b2CircleShape*)fixture->GetShape();
  119. b2Vec2 center = b2Mul(xf, circle->m_p);
  120. float32 radius = circle->m_radius;
  121. b2Vec2 axis = b2Mul(xf.q, b2Vec2(0.0f, 1.0f));
  122. DrawSolidCircle(center, radius, axis, color);
  123. }
  124. break;
  125. case b2Shape::e_edge:
  126. {
  127. b2EdgeShape* edge = (b2EdgeShape*)fixture->GetShape();
  128. b2Vec2 v1 = b2Mul(xf, edge->m_vertex1);
  129. b2Vec2 v2 = b2Mul(xf, edge->m_vertex2);
  130. DrawSegment(v1, v2, color);
  131. }
  132. break;
  133. case b2Shape::e_chain:
  134. {
  135. b2ChainShape* chain = (b2ChainShape*)fixture->GetShape();
  136. int32 count = chain->m_count;
  137. const b2Vec2* vertices = chain->m_vertices;
  138. b2Vec2 v1 = b2Mul(xf, vertices[0]);
  139. for (int32 i = 1; i < count; ++i)
  140. {
  141. b2Vec2 v2 = b2Mul(xf, vertices[i]);
  142. DrawSegment(v1, v2, color);
  143. DrawCircle(v1, 0.05f, color);
  144. v1 = v2;
  145. }
  146. }
  147. break;
  148. case b2Shape::e_polygon:
  149. {
  150. b2PolygonShape* poly = (b2PolygonShape*)fixture->GetShape();
  151. int32 vertexCount = poly->m_count;
  152. b2Assert(vertexCount <= b2_maxPolygonVertices);
  153. b2Vec2 vertices[b2_maxPolygonVertices];
  154. for (int32 i = 0; i < vertexCount; ++i)
  155. {
  156. vertices[i] = b2Mul(xf, poly->m_vertices[i]);
  157. }
  158. DrawSolidPolygon(vertices, vertexCount, color);
  159. }
  160. break;
  161. default:
  162. break;
  163. }
  164. }
  165. //-----------------------------------------------------------------------------
  166. void DebugDraw::DrawJoints( b2World* pWorld )
  167. {
  168. // Debug Profiling.
  169. PROFILE_SCOPE(DebugDraw_Joints);
  170. // Draw all joints in world.
  171. for ( b2Joint* pJoint = pWorld->GetJointList(); pJoint; pJoint = pJoint->GetNext() )
  172. {
  173. // Fetch joint configuration.
  174. b2Body* pBodyA = pJoint->GetBodyA();
  175. b2Body* pBodyB = pJoint->GetBodyB();
  176. const b2Transform& xf1 = pBodyA->GetTransform();
  177. const b2Transform& xf2 = pBodyB->GetTransform();
  178. b2Vec2 x1 = xf1.p;
  179. b2Vec2 x2 = xf2.p;
  180. b2Vec2 p1 = pJoint->GetAnchorA();
  181. b2Vec2 p2 = pJoint->GetAnchorB();
  182. ColorF color( 0.5f, 0.8f, 0.8f );
  183. switch ( pJoint->GetType() )
  184. {
  185. // Distance joint.
  186. case e_distanceJoint:
  187. DrawSegment(p1, p2, color);
  188. break;
  189. // Pullet joint.
  190. case e_pulleyJoint:
  191. {
  192. b2PulleyJoint* pPulley = (b2PulleyJoint*)pJoint;
  193. b2Vec2 s1 = pPulley->GetGroundAnchorA();
  194. b2Vec2 s2 = pPulley->GetGroundAnchorB();
  195. DrawSegment( s1, p1, color );
  196. DrawSegment( s2, p2, color );
  197. DrawSegment( s1, s2, color );
  198. }
  199. break;
  200. case e_mouseJoint:
  201. {
  202. b2MouseJoint* pMouseJoint = (b2MouseJoint*)pJoint;
  203. b2Vec2 p1 = pBodyB->GetPosition();
  204. p1 = pMouseJoint->GetAnchorB();
  205. b2Vec2 p2 = pMouseJoint->GetTarget();
  206. DrawSegment( p1, p2, color );
  207. }
  208. break;
  209. // All other joints.
  210. default:
  211. DrawSegment( x1, p1, color );
  212. DrawSegment( p1, p2, color );
  213. DrawSegment( x2, p2, color );
  214. }
  215. }
  216. }
  217. //-----------------------------------------------------------------------------
  218. void DebugDraw::DrawPolygon( const b2Vec2* vertices, int32 vertexCount, const ColorF& color )
  219. {
  220. // Debug Profiling.
  221. PROFILE_SCOPE(DebugDraw_DrawPolygon);
  222. glColor3f(color.red, color.green, color.blue);
  223. glBegin(GL_LINE_LOOP);
  224. for (int32 i = 0; i < vertexCount; ++i)
  225. {
  226. glVertex2f(vertices[i].x, vertices[i].y);
  227. }
  228. glEnd();
  229. }
  230. //-----------------------------------------------------------------------------
  231. void DebugDraw::DrawSolidPolygon( const b2Vec2* vertices, int32 vertexCount, const ColorF& color )
  232. {
  233. // Debug Profiling.
  234. PROFILE_SCOPE(DebugDraw_DrawSolidPolygon);
  235. glEnable(GL_BLEND);
  236. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  237. glColor4f(0.5f * color.red, 0.5f * color.green, 0.5f * color.blue, 0.15f);
  238. glBegin(GL_TRIANGLE_FAN);
  239. for (int32 i = 0; i < vertexCount; ++i)
  240. {
  241. glVertex2f(vertices[i].x, vertices[i].y);
  242. }
  243. glEnd();
  244. glDisable(GL_BLEND);
  245. glColor4f(color.red, color.green, color.blue, 1.0f);
  246. glBegin(GL_LINE_LOOP);
  247. for (int32 i = 0; i < vertexCount; ++i)
  248. {
  249. glVertex2f(vertices[i].x, vertices[i].y);
  250. }
  251. glEnd();
  252. }
  253. //-----------------------------------------------------------------------------
  254. void DebugDraw::DrawCircle( const b2Vec2& center, float32 radius, const ColorF& color )
  255. {
  256. // Debug Profiling.
  257. PROFILE_SCOPE(DebugDraw_DrawCircle);
  258. const float32 k_segments = 16.0f;
  259. const float32 k_increment = 2.0f * b2_pi / k_segments;
  260. float32 theta = 0.0f;
  261. glColor3f(color.red, color.green, color.blue);
  262. glBegin(GL_LINE_LOOP);
  263. for (int32 i = 0; i < k_segments; ++i)
  264. {
  265. b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
  266. glVertex2f(v.x, v.y);
  267. theta += k_increment;
  268. }
  269. glEnd();
  270. }
  271. //-----------------------------------------------------------------------------
  272. void DebugDraw::DrawSolidCircle( const b2Vec2& center, float32 radius, const b2Vec2& axis, const ColorF& color )
  273. {
  274. // Debug Profiling.
  275. PROFILE_SCOPE(DebugDraw_DrawSolidCircle);
  276. const float32 k_segments = 12.0f;
  277. const float32 k_increment = 2.0f * b2_pi / k_segments;
  278. float32 theta = 0.0f;
  279. glEnable(GL_BLEND);
  280. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  281. glColor4f(0.5f * color.red, 0.5f * color.green, 0.5f * color.blue, 0.15f);
  282. glBegin(GL_TRIANGLE_FAN);
  283. for (int32 i = 0; i < k_segments; ++i)
  284. {
  285. b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
  286. glVertex2f(v.x, v.y);
  287. theta += k_increment;
  288. }
  289. glEnd();
  290. glDisable(GL_BLEND);
  291. theta = 0.0f;
  292. glColor4f(color.red, color.green, color.blue, 1.0f);
  293. glBegin(GL_LINE_LOOP);
  294. for (int32 i = 0; i < k_segments; ++i)
  295. {
  296. b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
  297. glVertex2f(v.x, v.y);
  298. theta += k_increment;
  299. }
  300. glEnd();
  301. b2Vec2 p = center + radius * axis;
  302. glBegin(GL_LINES);
  303. glVertex2f(center.x, center.y);
  304. glVertex2f(p.x, p.y);
  305. glEnd();
  306. }
  307. //-----------------------------------------------------------------------------
  308. void DebugDraw::DrawSegment( const b2Vec2& p1, const b2Vec2& p2, const ColorF& color )
  309. {
  310. glColor3f(color.red, color.green, color.blue);
  311. glBegin(GL_LINES);
  312. glVertex2f(p1.x, p1.y);
  313. glVertex2f(p2.x, p2.y);
  314. glEnd();
  315. }
  316. //-----------------------------------------------------------------------------
  317. void DebugDraw::DrawTransform( const b2Transform& xf )
  318. {
  319. b2Vec2 p1 = xf.p, p2;
  320. const float32 k_axisScale = 0.4f;
  321. glBegin(GL_LINES);
  322. glColor3f(1.0f, 0.0f, 0.0f);
  323. glVertex2f(p1.x, p1.y);
  324. p2 = p1 + k_axisScale * xf.q.GetXAxis();
  325. glVertex2f(p2.x, p2.y);
  326. glColor3f(0.0f, 1.0f, 0.0f);
  327. glVertex2f(p1.x, p1.y);
  328. p2 = p1 + k_axisScale * xf.q.GetYAxis();
  329. glVertex2f(p2.x, p2.y);
  330. glEnd();
  331. }
  332. //-----------------------------------------------------------------------------
  333. void DebugDraw::DrawPoint( const b2Vec2& p, float32 size, const ColorF& color )
  334. {
  335. glPointSize(size);
  336. glBegin(GL_POINTS);
  337. glColor3f(color.red, color.green, color.blue);
  338. glVertex2f(p.x, p.y);
  339. glEnd();
  340. glPointSize(1.0f);
  341. }