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> /// </summary>
public class PrimitiveBatch : IDisposable public class PrimitiveBatch : IDisposable
{ {
List<VertexPositionColor> vertices; 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; } }
int bufferPosition; int bufferPosition;
int maxVertices;
BasicEffect basicEffect; BasicEffect basicEffect;
PrimitiveType primitiveType; PrimitiveType primitiveType;
/// <summary> int verticesPerPrimitive;
/// 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;
GraphicsDevice graphicsDevice; GraphicsDevice graphicsDevice;
bool began; bool began;
bool disposed; bool disposed;
@ -41,7 +36,8 @@ namespace RecrownedAthenaeum.Render
this.camera = camera ?? (Configuration.Camera2D); this.camera = camera ?? (Configuration.Camera2D);
basicEffect = new BasicEffect(this.graphicsDevice); basicEffect = new BasicEffect(this.graphicsDevice);
basicEffect.VertexColorEnabled = true; basicEffect.VertexColorEnabled = true;
vertices = new List<VertexPositionColor>(verticesPerBatch); vertices = new VertexPositionColor[verticesPerBatch+1];
maxVertices = verticesPerBatch;
} }
/// <summary> /// <summary>
@ -58,7 +54,7 @@ namespace RecrownedAthenaeum.Render
{ {
case PrimitiveType.LineList: verticesPerPrimitive = 2; break; case PrimitiveType.LineList: verticesPerPrimitive = 2; break;
case PrimitiveType.TriangleList: verticesPerPrimitive = 3; break; case PrimitiveType.TriangleList: verticesPerPrimitive = 3; break;
default: verticesPerPrimitive = -1; break; default: verticesPerPrimitive = 1; break;
} }
basicEffect.World = camera.worldMatrix; 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 (!began) throw new InvalidOperationException("Begin needs to be called before adding vertex.");
if (disposed) throw new ObjectDisposedException(this.GetType().Name); 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(); Flush();
} }
else else
{
throw new InvalidOperationException("Buffer size doesn't match with primitive type.");
}
}
else if (bufferPosition + verticesPerPrimitive > maxVertices)
{ {
throw new InvalidOperationException("Buffer size isn't large enough."); throw new InvalidOperationException("Buffer size isn't large enough.");
} }
}
vertices.Add(new VertexPositionColor(new Vector3(vertex, 0), color)); vertices[bufferPosition] = new VertexPositionColor(new Vector3(vertex, 0), color);
bufferPosition++; bufferPosition++;
} }
@ -111,9 +112,7 @@ namespace RecrownedAthenaeum.Render
if (!began) throw new InvalidOperationException("Begin needs to be called before flushing."); if (!began) throw new InvalidOperationException("Begin needs to be called before flushing.");
if (disposed) throw new ObjectDisposedException(this.GetType().Name); if (disposed) throw new ObjectDisposedException(this.GetType().Name);
if (bufferPosition == 0) return; 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.LineStrip, vertices, 0, bufferPosition / verticesPerPrimitive);
graphicsDevice.DrawUserPrimitives(primitiveType, vertices.ToArray(), 0, bufferPosition / verticesPerPrimitive);
vertices.Clear();
bufferPosition = 0; bufferPosition = 0;
} }