2
0

PrimitiveBatch.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #region IDisposable Members
  45. public void Dispose()
  46. {
  47. Dispose(true);
  48. GC.SuppressFinalize(this);
  49. }
  50. #endregion
  51. public void SetProjection(ref Matrix projection)
  52. {
  53. _basicEffect.Projection = projection;
  54. }
  55. protected virtual void Dispose(bool disposing)
  56. {
  57. if (disposing && !_isDisposed)
  58. {
  59. if (_basicEffect != null)
  60. _basicEffect.Dispose();
  61. _isDisposed = true;
  62. }
  63. }
  64. /// <summary>
  65. /// Begin is called to tell the PrimitiveBatch what kind of primitives will be
  66. /// drawn, and to prepare the graphics card to render those primitives.
  67. /// </summary>
  68. /// <param name="projection">The projection.</param>
  69. /// <param name="view">The view.</param>
  70. public void Begin(ref Matrix projection, ref Matrix view)
  71. {
  72. if (_hasBegun)
  73. {
  74. throw new InvalidOperationException("End must be called before Begin can be called again.");
  75. }
  76. //tell our basic effect to begin.
  77. _basicEffect.Projection = projection;
  78. _basicEffect.View = view;
  79. _basicEffect.CurrentTechnique.Passes[0].Apply();
  80. // flip the error checking boolean. It's now ok to call AddVertex, Flush,
  81. // and End.
  82. _hasBegun = true;
  83. }
  84. public bool IsReady()
  85. {
  86. return _hasBegun;
  87. }
  88. public void AddVertex(Vector2 vertex, Color color, PrimitiveType primitiveType)
  89. {
  90. if (!_hasBegun)
  91. {
  92. throw new InvalidOperationException("Begin must be called before AddVertex can be called.");
  93. }
  94. if (primitiveType == PrimitiveType.LineStrip ||
  95. primitiveType == PrimitiveType.TriangleStrip)
  96. {
  97. throw new NotSupportedException("The specified primitiveType is not supported by PrimitiveBatch.");
  98. }
  99. if (primitiveType == PrimitiveType.TriangleList)
  100. {
  101. if (_triangleVertsCount >= _triangleVertices.Length)
  102. {
  103. FlushTriangles();
  104. }
  105. _triangleVertices[_triangleVertsCount].Position = new Vector3(vertex, -0.1f);
  106. _triangleVertices[_triangleVertsCount].Color = color;
  107. _triangleVertsCount++;
  108. }
  109. if (primitiveType == PrimitiveType.LineList)
  110. {
  111. if (_lineVertsCount >= _lineVertices.Length)
  112. {
  113. FlushLines();
  114. }
  115. _lineVertices[_lineVertsCount].Position = new Vector3(vertex, 0f);
  116. _lineVertices[_lineVertsCount].Color = color;
  117. _lineVertsCount++;
  118. }
  119. }
  120. /// <summary>
  121. /// End is called once all the primitives have been drawn using AddVertex.
  122. /// it will call Flush to actually submit the draw call to the graphics card, and
  123. /// then tell the basic effect to end.
  124. /// </summary>
  125. public void End()
  126. {
  127. if (!_hasBegun)
  128. {
  129. throw new InvalidOperationException("Begin must be called before End can be called.");
  130. }
  131. // Draw whatever the user wanted us to draw
  132. FlushTriangles();
  133. FlushLines();
  134. _hasBegun = false;
  135. }
  136. private void FlushTriangles()
  137. {
  138. if (!_hasBegun)
  139. {
  140. throw new InvalidOperationException("Begin must be called before Flush can be called.");
  141. }
  142. if (_triangleVertsCount >= 3)
  143. {
  144. int primitiveCount = _triangleVertsCount / 3;
  145. // submit the draw call to the graphics card
  146. _device.SamplerStates[0] = SamplerState.AnisotropicClamp;
  147. _device.DrawUserPrimitives(PrimitiveType.TriangleList, _triangleVertices, 0, primitiveCount);
  148. _triangleVertsCount -= primitiveCount * 3;
  149. }
  150. }
  151. private void FlushLines()
  152. {
  153. if (!_hasBegun)
  154. {
  155. throw new InvalidOperationException("Begin must be called before Flush can be called.");
  156. }
  157. if (_lineVertsCount >= 2)
  158. {
  159. int primitiveCount = _lineVertsCount / 2;
  160. // submit the draw call to the graphics card
  161. _device.SamplerStates[0] = SamplerState.AnisotropicClamp;
  162. _device.DrawUserPrimitives(PrimitiveType.LineList, _lineVertices, 0, primitiveCount);
  163. _lineVertsCount -= primitiveCount * 2;
  164. }
  165. }
  166. }
  167. }