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