strip primitives now works.

This commit is contained in:
Harrison Deng 2019-02-11 23:21:35 -06:00
parent f8285c8786
commit b072701a22

View File

@ -11,19 +11,14 @@ namespace RecrownedAthenaeum.Render
/// </summary>
public class PrimitiveBatch : IDisposable
{
List<VertexPositionColor> vertices;
/// <summary>
/// The maximum vertices expected. The further off this expectancy is from the true value, the less efficient.
/// </summary>
public int MaxVertices { get { return vertices.Capacity; } set { vertices.Capacity = value; } }
VertexPositionColor[] vertices;
int bufferPosition;
int maxVertices;
BasicEffect basicEffect;
PrimitiveType primitiveType;
/// <summary>
/// The vertices per primitive. Can be ignored if using any of the list primitive types. Otherwise, needs to be manually set.
/// </summary>
public int verticesPerPrimitive;
int verticesPerPrimitive;
GraphicsDevice graphicsDevice;
bool began;
bool disposed;
@ -41,7 +36,8 @@ namespace RecrownedAthenaeum.Render
this.camera = camera ?? (Configuration.Camera2D);
basicEffect = new BasicEffect(this.graphicsDevice);
basicEffect.VertexColorEnabled = true;
vertices = new List<VertexPositionColor>(verticesPerBatch);
vertices = new VertexPositionColor[verticesPerBatch+1];
maxVertices = verticesPerBatch;
}
/// <summary>
@ -58,7 +54,7 @@ namespace RecrownedAthenaeum.Render
{
case PrimitiveType.LineList: verticesPerPrimitive = 2; break;
case PrimitiveType.TriangleList: verticesPerPrimitive = 3; break;
default: verticesPerPrimitive = -1; break;
default: verticesPerPrimitive = 1; break;
}
basicEffect.World = camera.worldMatrix;
@ -88,18 +84,23 @@ namespace RecrownedAthenaeum.Render
{
if (!began) throw new InvalidOperationException("Begin needs to be called before adding vertex.");
if (disposed) throw new ObjectDisposedException(this.GetType().Name);
if (bufferPosition + verticesPerPrimitive >= MaxVertices && (bufferPosition % MaxVertices == 0))
if (primitiveType != PrimitiveType.LineStrip && primitiveType != PrimitiveType.TriangleStrip)
{
if (primitiveType != PrimitiveType.LineStrip && primitiveType != PrimitiveType.TriangleStrip)
if (bufferPosition + verticesPerPrimitive > maxVertices && (bufferPosition % maxVertices == 0))
{
Flush();
}
else
{
throw new InvalidOperationException("Buffer size isn't large enough.");
throw new InvalidOperationException("Buffer size doesn't match with primitive type.");
}
}
vertices.Add(new VertexPositionColor(new Vector3(vertex, 0), color));
else if (bufferPosition + verticesPerPrimitive > maxVertices)
{
throw new InvalidOperationException("Buffer size isn't large enough.");
}
vertices[bufferPosition] = new VertexPositionColor(new Vector3(vertex, 0), color);
bufferPosition++;
}
@ -111,9 +112,7 @@ namespace RecrownedAthenaeum.Render
if (!began) throw new InvalidOperationException("Begin needs to be called before flushing.");
if (disposed) throw new ObjectDisposedException(this.GetType().Name);
if (bufferPosition == 0) return;
if (verticesPerPrimitive == -1) throw new InvalidOperationException("Vertices per primitive variable not set and using primitive type other than list.");
graphicsDevice.DrawUserPrimitives(primitiveType, vertices.ToArray(), 0, bufferPosition / verticesPerPrimitive);
vertices.Clear();
graphicsDevice.DrawUserPrimitives(PrimitiveType.LineStrip, vertices, 0, bufferPosition / verticesPerPrimitive);
bufferPosition = 0;
}