PrimitiveBatch.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. namespace FarseerPhysics.DebugViews
  5. {
  6. public class PrimitiveBatch : IDisposable
  7. {
  8. private const int DefaultBufferSize = 500;
  9. // a basic effect, which contains the shaders that we will use to draw our
  10. // primitives.
  11. private BasicEffect _basicEffect;
  12. // the device that we will issue draw calls to.
  13. private GraphicsDevice _device;
  14. // hasBegun is flipped to true once Begin is called, and is used to make
  15. // sure users don't call End before Begin is called.
  16. private bool _hasBegun;
  17. private bool _isDisposed;
  18. private VertexPositionColor[] _lineVertices;
  19. private int _lineVertsCount;
  20. private VertexPositionColor[] _triangleVertices;
  21. private int _triangleVertsCount;
  22. /// <summary>
  23. /// the constructor creates a new PrimitiveBatch and sets up all of the internals
  24. /// that PrimitiveBatch will need.
  25. /// </summary>
  26. /// <param name="graphicsDevice">The graphics device.</param>
  27. public PrimitiveBatch(GraphicsDevice graphicsDevice)
  28. : this(graphicsDevice, DefaultBufferSize)
  29. {
  30. }
  31. public PrimitiveBatch(GraphicsDevice graphicsDevice, int bufferSize)
  32. {
  33. if (graphicsDevice == null)
  34. {
  35. throw new ArgumentNullException("graphicsDevice");
  36. }
  37. _device = graphicsDevice;
  38. _triangleVertices = new VertexPositionColor[bufferSize - bufferSize % 3];
  39. _lineVertices = new VertexPositionColor[bufferSize - bufferSize % 2];
  40. // set up a new basic effect, and enable vertex colors.
  41. _basicEffect = new BasicEffect(graphicsDevice);
  42. _basicEffect.VertexColorEnabled = true;
  43. }
  44. public void Dispose()
  45. {
  46. Dispose(true);
  47. GC.SuppressFinalize(this);
  48. }
  49. public void SetProjection(ref Matrix projection)
  50. {
  51. _basicEffect.Projection = projection;
  52. }
  53. protected virtual void Dispose(bool disposing)
  54. {
  55. if (disposing && !_isDisposed)
  56. {
  57. if (_basicEffect != null)
  58. _basicEffect.Dispose();
  59. _isDisposed = true;
  60. }
  61. }
  62. /// <summary>
  63. /// Begin is called to tell the PrimitiveBatch what kind of primitives will be
  64. /// drawn, and to prepare the graphics card to render those primitives.
  65. /// </summary>
  66. /// <param name="projection">The projection.</param>
  67. /// <param name="view">The view.</param>
  68. public void Begin(ref Matrix projection, ref Matrix view)
  69. {
  70. if (_hasBegun)
  71. {
  72. throw new InvalidOperationException("End must be called before Begin can be called again.");
  73. }
  74. //tell our basic effect to begin.
  75. _basicEffect.Projection = projection;
  76. _basicEffect.View = view;
  77. _basicEffect.CurrentTechnique.Passes[0].Apply();
  78. // flip the error checking boolean. It's now ok to call AddVertex, Flush,
  79. // and End.
  80. _hasBegun = true;
  81. }
  82. public bool IsReady()
  83. {
  84. return _hasBegun;
  85. }
  86. public void AddVertex(Vector2 vertex, Color color, PrimitiveType primitiveType)
  87. {
  88. if (!_hasBegun)
  89. {
  90. throw new InvalidOperationException("Begin must be called before AddVertex can be called.");
  91. }
  92. if (primitiveType == PrimitiveType.LineStrip ||
  93. primitiveType == PrimitiveType.TriangleStrip)
  94. {
  95. throw new NotSupportedException("The specified primitiveType is not supported by PrimitiveBatch.");
  96. }
  97. if (primitiveType == PrimitiveType.TriangleList)
  98. {
  99. if (_triangleVertsCount >= _triangleVertices.Length)
  100. {
  101. FlushTriangles();
  102. }
  103. _triangleVertices[_triangleVertsCount].Position = new Vector3(vertex, -0.1f);
  104. _triangleVertices[_triangleVertsCount].Color = color;
  105. _triangleVertsCount++;
  106. }
  107. if (primitiveType == PrimitiveType.LineList)
  108. {
  109. if (_lineVertsCount >= _lineVertices.Length)
  110. {
  111. FlushLines();
  112. }
  113. _lineVertices[_lineVertsCount].Position = new Vector3(vertex, 0f);
  114. _lineVertices[_lineVertsCount].Color = color;
  115. _lineVertsCount++;
  116. }
  117. }
  118. /// <summary>
  119. /// End is called once all the primitives have been drawn using AddVertex.
  120. /// it will call Flush to actually submit the draw call to the graphics card, and
  121. /// then tell the basic effect to end.
  122. /// </summary>
  123. public void End()
  124. {
  125. if (!_hasBegun)
  126. {
  127. throw new InvalidOperationException("Begin must be called before End can be called.");
  128. }
  129. // Draw whatever the user wanted us to draw
  130. FlushTriangles();
  131. FlushLines();
  132. _hasBegun = false;
  133. }
  134. private void FlushTriangles()
  135. {
  136. if (!_hasBegun)
  137. {
  138. throw new InvalidOperationException("Begin must be called before Flush can be called.");
  139. }
  140. if (_triangleVertsCount >= 3)
  141. {
  142. int primitiveCount = _triangleVertsCount / 3;
  143. // submit the draw call to the graphics card
  144. _device.SamplerStates[0] = SamplerState.AnisotropicClamp;
  145. _device.DrawUserPrimitives(PrimitiveType.TriangleList, _triangleVertices, 0, primitiveCount);
  146. _triangleVertsCount -= primitiveCount * 3;
  147. }
  148. }
  149. private void FlushLines()
  150. {
  151. if (!_hasBegun)
  152. {
  153. throw new InvalidOperationException("Begin must be called before Flush can be called.");
  154. }
  155. if (_lineVertsCount >= 2)
  156. {
  157. int primitiveCount = _lineVertsCount / 2;
  158. // submit the draw call to the graphics card
  159. _device.SamplerStates[0] = SamplerState.AnisotropicClamp;
  160. _device.DrawUserPrimitives(PrimitiveType.LineList, _lineVertices, 0, primitiveCount);
  161. _lineVertsCount -= primitiveCount * 2;
  162. }
  163. }
  164. }
  165. }