PrimitiveBatch.cs 6.2 KB

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