DebugDraw.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //-----------------------------------------------------------------------------
  2. // DebugDraw.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Diagnostics;
  9. using System.Collections.Generic;
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Graphics;
  12. namespace CollisionSample
  13. {
  14. /// <summary>
  15. /// Debug drawing routines for common collision shapes. These are not designed to be the most
  16. /// efficent way to submit geometry to the graphics device as they are intended for use in
  17. /// visualizing collision for debugging purposes.
  18. /// </summary>
  19. public class DebugDraw : IDisposable
  20. {
  21. public const int MAX_VERTS = 2000;
  22. public const int MAX_INDICES = 2000;
  23. // Indices for drawing the edges of a cube, given the vertex ordering
  24. // used by Bounding(Frustum|Box|OrientedBox).GetCorners()
  25. static ushort[] cubeIndices = new ushort[] { 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 };
  26. BasicEffect basicEffect;
  27. DynamicVertexBuffer vertexBuffer;
  28. DynamicIndexBuffer indexBuffer;
  29. ushort[] Indices = new ushort[MAX_INDICES];
  30. VertexPositionColor[] Vertices = new VertexPositionColor[MAX_VERTS];
  31. int IndexCount;
  32. int VertexCount;
  33. public DebugDraw(GraphicsDevice device)
  34. {
  35. vertexBuffer = new DynamicVertexBuffer(device, typeof(VertexPositionColor), MAX_VERTS, BufferUsage.WriteOnly);
  36. indexBuffer = new DynamicIndexBuffer(device, typeof(ushort), MAX_INDICES, BufferUsage.WriteOnly);
  37. basicEffect = new BasicEffect(device); //(device, null);
  38. basicEffect.LightingEnabled = false;
  39. basicEffect.VertexColorEnabled = true;
  40. basicEffect.TextureEnabled = false;
  41. }
  42. ~DebugDraw()
  43. {
  44. Dispose(false);
  45. }
  46. public void Dispose()
  47. {
  48. Dispose(true);
  49. GC.SuppressFinalize(this);
  50. }
  51. protected virtual void Dispose(bool disposing)
  52. {
  53. if (disposing)
  54. {
  55. if (vertexBuffer != null)
  56. vertexBuffer.Dispose();
  57. if (indexBuffer != null)
  58. indexBuffer.Dispose();
  59. if (basicEffect != null)
  60. basicEffect.Dispose();
  61. }
  62. }
  63. /// <summary>
  64. /// Starts debug drawing by setting the required render states and camera information
  65. /// </summary>
  66. public void Begin(Matrix view, Matrix projection)
  67. {
  68. basicEffect.World = Matrix.Identity;
  69. basicEffect.View = view;
  70. basicEffect.Projection = projection;
  71. VertexCount = 0;
  72. IndexCount = 0;
  73. }
  74. /// <summary>
  75. /// Ends debug drawing and restores standard render states
  76. /// </summary>
  77. public void End()
  78. {
  79. FlushDrawing();
  80. }
  81. public void DrawWireShape(Vector3[] positionArray, ushort[] indexArray, Color color)
  82. {
  83. if (Reserve(positionArray.Length, indexArray.Length))
  84. {
  85. for (int i = 0; i < indexArray.Length; i++)
  86. Indices[IndexCount++] = (ushort)(VertexCount + indexArray[i]);
  87. for (int i = 0; i < positionArray.Length; i++)
  88. Vertices[VertexCount++] = new VertexPositionColor(positionArray[i], color);
  89. }
  90. }
  91. // Draw any queued objects and reset our line buffers
  92. private void FlushDrawing()
  93. {
  94. if (IndexCount > 0)
  95. {
  96. vertexBuffer.SetData(Vertices, 0, VertexCount, SetDataOptions.Discard);
  97. indexBuffer.SetData(Indices, 0, IndexCount, SetDataOptions.Discard);
  98. GraphicsDevice device = basicEffect.GraphicsDevice;
  99. device.SetVertexBuffer(vertexBuffer);
  100. device.Indices = indexBuffer;
  101. foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
  102. {
  103. pass.Apply();
  104. device.DrawIndexedPrimitives(PrimitiveType.LineList, 0, 0, VertexCount, 0, IndexCount / 2);
  105. }
  106. device.SetVertexBuffer(null);
  107. device.Indices = null;
  108. }
  109. IndexCount = 0;
  110. VertexCount = 0;
  111. }
  112. // Check if there's enough space to draw an object with the given vertex/index counts.
  113. // If necessary, call FlushDrawing() to make room.
  114. private bool Reserve(int numVerts, int numIndices)
  115. {
  116. if(numVerts > MAX_VERTS || numIndices > MAX_INDICES)
  117. {
  118. // Whatever it is, we can't draw it
  119. return false;
  120. }
  121. if (VertexCount + numVerts > MAX_VERTS || IndexCount + numIndices >= MAX_INDICES)
  122. {
  123. // We can draw it, but we need to make room first
  124. FlushDrawing();
  125. }
  126. return true;
  127. }
  128. /// <summary>
  129. /// Renders a 2D grid (must be called within a Begin/End pair)
  130. /// </summary>
  131. /// <param name="xAxis">Vector direction for the local X-axis direction of the grid</param>
  132. /// <param name="yAxis">Vector direction for the local Y-axis of the grid</param>
  133. /// <param name="origin">3D starting anchor point for the grid</param>
  134. /// <param name="iXDivisions">Number of divisions in the local X-axis direction</param>
  135. /// <param name="iYDivisions">Number of divisions in the local Y-axis direction</param>
  136. /// <param name="color">Color of the grid lines</param>
  137. public void DrawWireGrid(Vector3 xAxis, Vector3 yAxis, Vector3 origin, int iXDivisions, int iYDivisions, Color color)
  138. {
  139. Vector3 pos, step;
  140. pos = origin;
  141. step = xAxis / iXDivisions;
  142. for (int i = 0; i <= iXDivisions; i++)
  143. {
  144. DrawLine(pos, pos + yAxis, color);
  145. pos += step;
  146. }
  147. pos = origin;
  148. step = yAxis / iYDivisions;
  149. for (int i = 0; i <= iYDivisions; i++)
  150. {
  151. DrawLine(pos, pos + xAxis, color);
  152. pos += step;
  153. }
  154. }
  155. /// <summary>
  156. /// Renders the outline of a bounding frustum
  157. /// </summary>
  158. /// <param name="frustum">Bounding frustum to render</param>
  159. /// <param name="color">Color of the frustum lines</param>
  160. public void DrawWireFrustum(BoundingFrustum frustum, Color color)
  161. {
  162. DrawWireShape(frustum.GetCorners(), cubeIndices, color);
  163. }
  164. /// <summary>
  165. /// Renders the outline of an axis-aligned bounding box
  166. /// </summary>
  167. /// <param name="box">Bounding box to render</param>
  168. /// <param name="color">Color of the box lines</param>
  169. public void DrawWireBox(BoundingBox box, Color color)
  170. {
  171. DrawWireShape(box.GetCorners(), cubeIndices, color);
  172. }
  173. /// <summary>
  174. /// Renders the outline of an oriented bounding box
  175. /// </summary>
  176. /// <param name="box">Oriented bounding box to render</param>
  177. /// <param name="color">Color of the box lines</param>
  178. public void DrawWireBox(BoundingOrientedBox box, Color color)
  179. {
  180. DrawWireShape(box.GetCorners(), cubeIndices, color);
  181. }
  182. /// <summary>
  183. /// Renders a circular ring (tessellated circle)
  184. /// </summary>
  185. /// <param name="origin">Center point for the ring</param>
  186. /// <param name="majorAxis">Direction of the major-axis of the circle</param>
  187. /// <param name="minorAxis">Direction of hte minor-axis of the circle</param>
  188. /// <param name="color">Color of the ring lines</param>
  189. public void DrawRing(Vector3 origin, Vector3 majorAxis, Vector3 minorAxis, Color color)
  190. {
  191. const int RING_SEGMENTS = 32;
  192. const float fAngleDelta = 2.0F * (float)Math.PI / RING_SEGMENTS;
  193. if (Reserve(RING_SEGMENTS, RING_SEGMENTS * 2))
  194. {
  195. for (int i = 0; i < RING_SEGMENTS; i++)
  196. {
  197. Indices[IndexCount++] = (ushort)(VertexCount + i);
  198. Indices[IndexCount++] = (ushort)(VertexCount + (i + 1) % RING_SEGMENTS);
  199. }
  200. float cosDelta = (float)Math.Cos(fAngleDelta);
  201. float sinDelta = (float)Math.Sin(fAngleDelta);
  202. float cosAcc = 1;
  203. float sinAcc = 0;
  204. for (int i = 0; i < RING_SEGMENTS; ++i)
  205. {
  206. Vector3 pos = new Vector3(majorAxis.X * cosAcc + minorAxis.X * sinAcc + origin.X,
  207. majorAxis.Y * cosAcc + minorAxis.Y * sinAcc + origin.Y,
  208. majorAxis.Z * cosAcc + minorAxis.Z * sinAcc + origin.Z);
  209. Vertices[VertexCount++] = new VertexPositionColor(pos, color);
  210. float newCos = cosAcc * cosDelta - sinAcc * sinDelta;
  211. float newSin = cosAcc * sinDelta + sinAcc * cosDelta;
  212. cosAcc = newCos;
  213. sinAcc = newSin;
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// Renders the outline of a bounding sphere.
  219. ///
  220. /// This code assumes that the model and view matrices contain only rigid motion.
  221. /// </summary>
  222. /// <param name="sphere">Bounding sphere to render</param>
  223. /// <param name="color">Color of the outline lines</param>
  224. public void DrawWireSphere(BoundingSphere sphere, Color color)
  225. {
  226. // Invert the modelview matrix to get direction vectors
  227. // in screen space, so we can draw a circle that always
  228. // faces the camera.
  229. Matrix view = basicEffect.World * basicEffect.View;
  230. Matrix.Transpose(ref view, out view);
  231. DrawRing(sphere.Center, view.Right * sphere.Radius, view.Up * sphere.Radius, color);
  232. }
  233. /// <summary>
  234. /// Draw a ray of the given length
  235. /// </summary>
  236. /// <param name="ray"></param>
  237. /// <param name="color"></param>
  238. /// <param name="length"></param>
  239. public void DrawRay(Ray ray, Color color, float length)
  240. {
  241. DrawLine(ray.Position, ray.Position + ray.Direction * length, color);
  242. }
  243. public void DrawLine(Vector3 v0, Vector3 v1, Color color)
  244. {
  245. if(Reserve(2, 2))
  246. {
  247. Indices[IndexCount++] = (ushort)VertexCount;
  248. Indices[IndexCount++] = (ushort)(VertexCount+1);
  249. Vertices[VertexCount++] = new VertexPositionColor(v0, color);
  250. Vertices[VertexCount++] = new VertexPositionColor(v1, color);
  251. }
  252. }
  253. public void DrawWireTriangle(Vector3 v0, Vector3 v1, Vector3 v2, Color color)
  254. {
  255. if(Reserve(3, 6))
  256. {
  257. Indices[IndexCount++] = (ushort)(VertexCount+0);
  258. Indices[IndexCount++] = (ushort)(VertexCount+1);
  259. Indices[IndexCount++] = (ushort)(VertexCount+1);
  260. Indices[IndexCount++] = (ushort)(VertexCount+2);
  261. Indices[IndexCount++] = (ushort)(VertexCount+2);
  262. Indices[IndexCount++] = (ushort)(VertexCount+0);
  263. Vertices[VertexCount++] = new VertexPositionColor(v0, color);
  264. Vertices[VertexCount++] = new VertexPositionColor(v1, color);
  265. Vertices[VertexCount++] = new VertexPositionColor(v2, color);
  266. }
  267. }
  268. public void DrawWireTriangle(Triangle t, Color color)
  269. {
  270. DrawWireTriangle(t.V0, t.V1, t.V2, color);
  271. }
  272. }
  273. }