DebugShapeRenderer.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // DebugShapeRenderer.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. namespace ShapeRenderingSample
  15. {
  16. /// <summary>
  17. /// A system for handling rendering of various debug shapes.
  18. /// </summary>
  19. /// <remarks>
  20. /// The DebugShapeRenderer allows for rendering line-base shapes in a batched fashion. Games
  21. /// will call one of the many Add* methods to add a shape to the renderer and then a call to
  22. /// Draw will cause all shapes to be rendered. This mechanism was chosen because it allows
  23. /// game code to call the Add* methods wherever is most convenient, rather than having to
  24. /// add draw methods to all of the necessary objects.
  25. ///
  26. /// Additionally the renderer supports a lifetime for all shapes added. This allows for things
  27. /// like visualization of raycast bullets. The game would call the AddLine overload with the
  28. /// lifetime parameter and pass in a positive value. The renderer will then draw that shape
  29. /// for the given amount of time without any more calls to AddLine being required.
  30. ///
  31. /// The renderer's batching mechanism uses a cache system to avoid garbage and also draws as
  32. /// many lines in one call to DrawUserPrimitives as possible. If the renderer is trying to draw
  33. /// more lines than are allowed in the Reach profile, it will break them up into multiple draw
  34. /// calls to make sure the game continues to work for any game.</remarks>
  35. public static class DebugShapeRenderer
  36. {
  37. // A single shape in our debug renderer
  38. class DebugShape
  39. {
  40. /// <summary>
  41. /// The array of vertices the shape can use.
  42. /// </summary>
  43. public VertexPositionColor[] Vertices;
  44. /// <summary>
  45. /// The number of lines to draw for this shape.
  46. /// </summary>
  47. public int LineCount;
  48. /// <summary>
  49. /// The length of time to keep this shape visible.
  50. /// </summary>
  51. public float Lifetime;
  52. }
  53. // We use a cache system to reuse our DebugShape instances to avoid creating garbage
  54. private static readonly List<DebugShape> cachedShapes = new List<DebugShape>();
  55. private static readonly List<DebugShape> activeShapes = new List<DebugShape>();
  56. // Allocate an array to hold our vertices; this will grow as needed by our renderer
  57. private static VertexPositionColor[] verts = new VertexPositionColor[64];
  58. // Our graphics device and the effect we use to render the shapes
  59. private static GraphicsDevice graphics;
  60. private static BasicEffect effect;
  61. // An array we use to get corners from frustums and bounding boxes
  62. private static Vector3[] corners = new Vector3[8];
  63. // This holds the vertices for our unit sphere that we will use when drawing bounding spheres
  64. private const int sphereResolution = 30;
  65. private const int sphereLineCount = (sphereResolution + 1) * 3;
  66. private static Vector3[] unitSphere;
  67. /// <summary>
  68. /// Initializes the renderer.
  69. /// </summary>
  70. /// <param name="graphicsDevice">The GraphicsDevice to use for rendering.</param>
  71. [Conditional("DEBUG")]
  72. public static void Initialize(GraphicsDevice graphicsDevice)
  73. {
  74. // If we already have a graphics device, we've already initialized once. We don't allow that.
  75. if (graphics != null)
  76. throw new InvalidOperationException("Initialize can only be called once.");
  77. // Save the graphics device
  78. graphics = graphicsDevice;
  79. // Create and initialize our effect
  80. effect = new BasicEffect(graphicsDevice);
  81. effect.VertexColorEnabled = true;
  82. effect.TextureEnabled = false;
  83. effect.DiffuseColor = Vector3.One;
  84. effect.World = Matrix.Identity;
  85. // Create our unit sphere vertices
  86. InitializeSphere();
  87. }
  88. /// <summary>
  89. /// Adds a line to be rendered for just one frame.
  90. /// </summary>
  91. /// <param name="a">The first point of the line.</param>
  92. /// <param name="b">The second point of the line.</param>
  93. /// <param name="color">The color in which to draw the line.</param>
  94. [Conditional("DEBUG")]
  95. public static void AddLine(Vector3 a, Vector3 b, Color color)
  96. {
  97. AddLine(a, b, color, 0f);
  98. }
  99. /// <summary>
  100. /// Adds a line to be rendered for a set amount of time.
  101. /// </summary>
  102. /// <param name="a">The first point of the line.</param>
  103. /// <param name="b">The second point of the line.</param>
  104. /// <param name="color">The color in which to draw the line.</param>
  105. /// <param name="life">The amount of time, in seconds, to keep rendering the line.</param>
  106. [Conditional("DEBUG")]
  107. public static void AddLine(Vector3 a, Vector3 b, Color color, float life)
  108. {
  109. // Get a DebugShape we can use to draw the line
  110. DebugShape shape = GetShapeForLines(1, life);
  111. // Add the two vertices to the shape
  112. shape.Vertices[0] = new VertexPositionColor(a, color);
  113. shape.Vertices[1] = new VertexPositionColor(b, color);
  114. }
  115. /// <summary>
  116. /// Adds a triangle to be rendered for just one frame.
  117. /// </summary>
  118. /// <param name="a">The first vertex of the triangle.</param>
  119. /// <param name="b">The second vertex of the triangle.</param>
  120. /// <param name="c">The third vertex of the triangle.</param>
  121. /// <param name="color">The color in which to draw the triangle.</param>
  122. [Conditional("DEBUG")]
  123. public static void AddTriangle(Vector3 a, Vector3 b, Vector3 c, Color color)
  124. {
  125. AddTriangle(a, b, c, color, 0f);
  126. }
  127. /// <summary>
  128. /// Adds a triangle to be rendered for a set amount of time.
  129. /// </summary>
  130. /// <param name="a">The first vertex of the triangle.</param>
  131. /// <param name="b">The second vertex of the triangle.</param>
  132. /// <param name="c">The third vertex of the triangle.</param>
  133. /// <param name="color">The color in which to draw the triangle.</param>
  134. /// <param name="life">The amount of time, in seconds, to keep rendering the triangle.</param>
  135. [Conditional("DEBUG")]
  136. public static void AddTriangle(Vector3 a, Vector3 b, Vector3 c, Color color, float life)
  137. {
  138. // Get a DebugShape we can use to draw the triangle
  139. DebugShape shape = GetShapeForLines(3, life);
  140. // Add the vertices to the shape
  141. shape.Vertices[0] = new VertexPositionColor(a, color);
  142. shape.Vertices[1] = new VertexPositionColor(b, color);
  143. shape.Vertices[2] = new VertexPositionColor(b, color);
  144. shape.Vertices[3] = new VertexPositionColor(c, color);
  145. shape.Vertices[4] = new VertexPositionColor(c, color);
  146. shape.Vertices[5] = new VertexPositionColor(a, color);
  147. }
  148. /// <summary>
  149. /// Adds a frustum to be rendered for just one frame.
  150. /// </summary>
  151. /// <param name="frustum">The frustum to render.</param>
  152. /// <param name="color">The color in which to draw the frustum.</param>
  153. [Conditional("DEBUG")]
  154. public static void AddBoundingFrustum(BoundingFrustum frustum, Color color)
  155. {
  156. AddBoundingFrustum(frustum, color, 0f);
  157. }
  158. /// <summary>
  159. /// Adds a frustum to be rendered for a set amount of time.
  160. /// </summary>
  161. /// <param name="frustum">The frustum to render.</param>
  162. /// <param name="color">The color in which to draw the frustum.</param>
  163. /// <param name="life">The amount of time, in seconds, to keep rendering the frustum.</param>
  164. [Conditional("DEBUG")]
  165. public static void AddBoundingFrustum(BoundingFrustum frustum, Color color, float life)
  166. {
  167. // Get a DebugShape we can use to draw the frustum
  168. DebugShape shape = GetShapeForLines(12, life);
  169. // Get the corners of the frustum
  170. frustum.GetCorners(corners);
  171. // Fill in the vertices for the bottom of the frustum
  172. shape.Vertices[0] = new VertexPositionColor(corners[0], color);
  173. shape.Vertices[1] = new VertexPositionColor(corners[1], color);
  174. shape.Vertices[2] = new VertexPositionColor(corners[1], color);
  175. shape.Vertices[3] = new VertexPositionColor(corners[2], color);
  176. shape.Vertices[4] = new VertexPositionColor(corners[2], color);
  177. shape.Vertices[5] = new VertexPositionColor(corners[3], color);
  178. shape.Vertices[6] = new VertexPositionColor(corners[3], color);
  179. shape.Vertices[7] = new VertexPositionColor(corners[0], color);
  180. // Fill in the vertices for the top of the frustum
  181. shape.Vertices[8] = new VertexPositionColor(corners[4], color);
  182. shape.Vertices[9] = new VertexPositionColor(corners[5], color);
  183. shape.Vertices[10] = new VertexPositionColor(corners[5], color);
  184. shape.Vertices[11] = new VertexPositionColor(corners[6], color);
  185. shape.Vertices[12] = new VertexPositionColor(corners[6], color);
  186. shape.Vertices[13] = new VertexPositionColor(corners[7], color);
  187. shape.Vertices[14] = new VertexPositionColor(corners[7], color);
  188. shape.Vertices[15] = new VertexPositionColor(corners[4], color);
  189. // Fill in the vertices for the vertical sides of the frustum
  190. shape.Vertices[16] = new VertexPositionColor(corners[0], color);
  191. shape.Vertices[17] = new VertexPositionColor(corners[4], color);
  192. shape.Vertices[18] = new VertexPositionColor(corners[1], color);
  193. shape.Vertices[19] = new VertexPositionColor(corners[5], color);
  194. shape.Vertices[20] = new VertexPositionColor(corners[2], color);
  195. shape.Vertices[21] = new VertexPositionColor(corners[6], color);
  196. shape.Vertices[22] = new VertexPositionColor(corners[3], color);
  197. shape.Vertices[23] = new VertexPositionColor(corners[7], color);
  198. }
  199. /// <summary>
  200. /// Adds a bounding box to be rendered for just one frame.
  201. /// </summary>
  202. /// <param name="box">The bounding box to render.</param>
  203. /// <param name="color">The color in which to draw the bounding box.</param>
  204. [Conditional("DEBUG")]
  205. public static void AddBoundingBox(BoundingBox box, Color color)
  206. {
  207. AddBoundingBox(box, color, 0f);
  208. }
  209. /// <summary>
  210. /// Adds a bounding box to be rendered for a set amount of time.
  211. /// </summary>
  212. /// <param name="box">The bounding box to render.</param>
  213. /// <param name="color">The color in which to draw the bounding box.</param>
  214. /// <param name="life">The amount of time, in seconds, to keep rendering the bounding box.</param>
  215. [Conditional("DEBUG")]
  216. public static void AddBoundingBox(BoundingBox box, Color color, float life)
  217. {
  218. // Get a DebugShape we can use to draw the box
  219. DebugShape shape = GetShapeForLines(12, life);
  220. // Get the corners of the box
  221. box.GetCorners(corners);
  222. // Fill in the vertices for the bottom of the box
  223. shape.Vertices[0] = new VertexPositionColor(corners[0], color);
  224. shape.Vertices[1] = new VertexPositionColor(corners[1], color);
  225. shape.Vertices[2] = new VertexPositionColor(corners[1], color);
  226. shape.Vertices[3] = new VertexPositionColor(corners[2], color);
  227. shape.Vertices[4] = new VertexPositionColor(corners[2], color);
  228. shape.Vertices[5] = new VertexPositionColor(corners[3], color);
  229. shape.Vertices[6] = new VertexPositionColor(corners[3], color);
  230. shape.Vertices[7] = new VertexPositionColor(corners[0], color);
  231. // Fill in the vertices for the top of the box
  232. shape.Vertices[8] = new VertexPositionColor(corners[4], color);
  233. shape.Vertices[9] = new VertexPositionColor(corners[5], color);
  234. shape.Vertices[10] = new VertexPositionColor(corners[5], color);
  235. shape.Vertices[11] = new VertexPositionColor(corners[6], color);
  236. shape.Vertices[12] = new VertexPositionColor(corners[6], color);
  237. shape.Vertices[13] = new VertexPositionColor(corners[7], color);
  238. shape.Vertices[14] = new VertexPositionColor(corners[7], color);
  239. shape.Vertices[15] = new VertexPositionColor(corners[4], color);
  240. // Fill in the vertices for the vertical sides of the box
  241. shape.Vertices[16] = new VertexPositionColor(corners[0], color);
  242. shape.Vertices[17] = new VertexPositionColor(corners[4], color);
  243. shape.Vertices[18] = new VertexPositionColor(corners[1], color);
  244. shape.Vertices[19] = new VertexPositionColor(corners[5], color);
  245. shape.Vertices[20] = new VertexPositionColor(corners[2], color);
  246. shape.Vertices[21] = new VertexPositionColor(corners[6], color);
  247. shape.Vertices[22] = new VertexPositionColor(corners[3], color);
  248. shape.Vertices[23] = new VertexPositionColor(corners[7], color);
  249. }
  250. /// <summary>
  251. /// Adds a bounding sphere to be rendered for just one frame.
  252. /// </summary>
  253. /// <param name="sphere">The bounding sphere to render.</param>
  254. /// <param name="color">The color in which to draw the bounding sphere.</param>
  255. [Conditional("DEBUG")]
  256. public static void AddBoundingSphere(BoundingSphere sphere, Color color)
  257. {
  258. AddBoundingSphere(sphere, color, 0f);
  259. }
  260. /// <summary>
  261. /// Adds a bounding sphere to be rendered for a set amount of time.
  262. /// </summary>
  263. /// <param name="sphere">The bounding sphere to render.</param>
  264. /// <param name="color">The color in which to draw the bounding sphere.</param>
  265. /// <param name="life">The amount of time, in seconds, to keep rendering the bounding sphere.</param>
  266. [Conditional("DEBUG")]
  267. public static void AddBoundingSphere(BoundingSphere sphere, Color color, float life)
  268. {
  269. // Get a DebugShape we can use to draw the sphere
  270. DebugShape shape = GetShapeForLines(sphereLineCount, life);
  271. // Iterate our unit sphere vertices
  272. for (int i = 0; i < unitSphere.Length; i++)
  273. {
  274. // Compute the vertex position by transforming the point by the radius and center of the sphere
  275. Vector3 vertPos = unitSphere[i] * sphere.Radius + sphere.Center;
  276. // Add the vertex to the shape
  277. shape.Vertices[i] = new VertexPositionColor(vertPos, color);
  278. }
  279. }
  280. /// <summary>
  281. /// Draws the shapes that were added to the renderer and are still alive.
  282. /// </summary>
  283. /// <param name="gameTime">The current game timestamp.</param>
  284. /// <param name="view">The view matrix to use when rendering the shapes.</param>
  285. /// <param name="projection">The projection matrix to use when rendering the shapes.</param>
  286. [Conditional("DEBUG")]
  287. public static void Draw(GameTime gameTime, Matrix view, Matrix projection)
  288. {
  289. // Update our effect with the matrices.
  290. effect.View = view;
  291. effect.Projection = projection;
  292. // Calculate the total number of vertices we're going to be rendering.
  293. int vertexCount = 0;
  294. foreach (var shape in activeShapes)
  295. vertexCount += shape.LineCount * 2;
  296. // If we have some vertices to draw
  297. if (vertexCount > 0)
  298. {
  299. // Make sure our array is large enough
  300. if (verts.Length < vertexCount)
  301. {
  302. // If we have to resize, we make our array twice as large as necessary so
  303. // we hopefully won't have to resize it for a while.
  304. verts = new VertexPositionColor[vertexCount * 2];
  305. }
  306. // Now go through the shapes again to move the vertices to our array and
  307. // add up the number of lines to draw.
  308. int lineCount = 0;
  309. int vertIndex = 0;
  310. foreach (DebugShape shape in activeShapes)
  311. {
  312. lineCount += shape.LineCount;
  313. int shapeVerts = shape.LineCount * 2;
  314. for (int i = 0; i < shapeVerts; i++)
  315. verts[vertIndex++] = shape.Vertices[i];
  316. }
  317. // Start our effect to begin rendering.
  318. effect.CurrentTechnique.Passes[0].Apply();
  319. // We draw in a loop because the Reach profile only supports 65,535 primitives. While it's
  320. // not incredibly likely, if a game tries to render more than 65,535 lines we don't want to
  321. // crash. We handle this by doing a loop and drawing as many lines as we can at a time, capped
  322. // at our limit. We then move ahead in our vertex array and draw the next set of lines.
  323. int vertexOffset = 0;
  324. while (lineCount > 0)
  325. {
  326. // Figure out how many lines we're going to draw
  327. int linesToDraw = Math.Min(lineCount, 65535);
  328. // Draw the lines
  329. graphics.DrawUserPrimitives(PrimitiveType.LineList, verts, vertexOffset, linesToDraw);
  330. // Move our vertex offset ahead based on the lines we drew
  331. vertexOffset += linesToDraw * 2;
  332. // Remove these lines from our total line count
  333. lineCount -= linesToDraw;
  334. }
  335. }
  336. // Go through our active shapes and retire any shapes that have expired to the
  337. // cache list.
  338. bool resort = false;
  339. for (int i = activeShapes.Count - 1; i >= 0; i--)
  340. {
  341. DebugShape s = activeShapes[i];
  342. s.Lifetime -= (float)gameTime.ElapsedGameTime.TotalSeconds;
  343. if (s.Lifetime <= 0)
  344. {
  345. cachedShapes.Add(s);
  346. activeShapes.RemoveAt(i);
  347. resort = true;
  348. }
  349. }
  350. // If we move any shapes around, we need to resort the cached list
  351. // to ensure that the smallest shapes are first in the list.
  352. if (resort)
  353. cachedShapes.Sort(CachedShapesSort);
  354. }
  355. /// <summary>
  356. /// Creates the unitSphere array of vertices.
  357. /// </summary>
  358. private static void InitializeSphere()
  359. {
  360. // We need two vertices per line, so we can allocate our vertices
  361. unitSphere = new Vector3[sphereLineCount * 2];
  362. // Compute our step around each circle
  363. float step = MathHelper.TwoPi / sphereResolution;
  364. // Used to track the index into our vertex array
  365. int index = 0;
  366. // Create the loop on the XY plane first
  367. for (float a = 0f; a < MathHelper.TwoPi; a += step)
  368. {
  369. unitSphere[index++] = new Vector3((float)Math.Cos(a), (float)Math.Sin(a), 0f);
  370. unitSphere[index++] = new Vector3((float)Math.Cos(a + step), (float)Math.Sin(a + step), 0f);
  371. }
  372. // Next on the XZ plane
  373. for (float a = 0f; a < MathHelper.TwoPi; a += step)
  374. {
  375. unitSphere[index++] = new Vector3((float)Math.Cos(a), 0f, (float)Math.Sin(a));
  376. unitSphere[index++] = new Vector3((float)Math.Cos(a + step), 0f, (float)Math.Sin(a + step));
  377. }
  378. // Finally on the YZ plane
  379. for (float a = 0f; a < MathHelper.TwoPi; a += step)
  380. {
  381. unitSphere[index++] = new Vector3(0f, (float)Math.Cos(a), (float)Math.Sin(a));
  382. unitSphere[index++] = new Vector3(0f, (float)Math.Cos(a + step), (float)Math.Sin(a + step));
  383. }
  384. }
  385. /// <summary>
  386. /// A method used for sorting our cached shapes based on the size of their vertex arrays.
  387. /// </summary>
  388. private static int CachedShapesSort(DebugShape s1, DebugShape s2)
  389. {
  390. return s1.Vertices.Length.CompareTo(s2.Vertices.Length);
  391. }
  392. /// <summary>
  393. /// Gets a DebugShape instance for a given line counta and lifespan.
  394. /// </summary>
  395. private static DebugShape GetShapeForLines(int lineCount, float life)
  396. {
  397. DebugShape shape = null;
  398. // We go through our cached list trying to find a shape that contains
  399. // a large enough array to hold our desired line count. If we find such
  400. // a shape, we move it from our cached list to our active list and break
  401. // out of the loop.
  402. int vertCount = lineCount * 2;
  403. for (int i = 0; i < cachedShapes.Count; i++)
  404. {
  405. if (cachedShapes[i].Vertices.Length >= vertCount)
  406. {
  407. shape = cachedShapes[i];
  408. cachedShapes.RemoveAt(i);
  409. activeShapes.Add(shape);
  410. break;
  411. }
  412. }
  413. // If we didn't find a shape in our cache, we create a new shape and add it
  414. // to the active list.
  415. if (shape == null)
  416. {
  417. shape = new DebugShape { Vertices = new VertexPositionColor[vertCount] };
  418. activeShapes.Add(shape);
  419. }
  420. // Set the line count and lifetime of the shape based on our parameters.
  421. shape.LineCount = lineCount;
  422. shape.Lifetime = life;
  423. return shape;
  424. }
  425. }
  426. }