From 2bd6d6849087f634d8399457177f98365ded20cc Mon Sep 17 00:00:00 2001 From: Recrown Date: Sun, 10 Feb 2019 23:32:00 -0600 Subject: [PATCH] Primitive batch now works. Probably. --- RecrownedAthenaeum/Render/PrimitiveBatch.cs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/RecrownedAthenaeum/Render/PrimitiveBatch.cs b/RecrownedAthenaeum/Render/PrimitiveBatch.cs index 3ddc247..d796444 100644 --- a/RecrownedAthenaeum/Render/PrimitiveBatch.cs +++ b/RecrownedAthenaeum/Render/PrimitiveBatch.cs @@ -19,10 +19,15 @@ namespace RecrownedAthenaeum.Render int bufferPosition; BasicEffect basicEffect; PrimitiveType primitiveType; - int verticesPerPrimitive; + + /// + /// The vertices per primitive. Can be ignored if using any of the list primitive types. Otherwise, needs to be manually set. + /// + public int verticesPerPrimitive; GraphicsDevice graphicsDevice; bool began; bool disposed; + Camera3D camera; /// /// Creates a batch used to draw primitives. @@ -33,12 +38,9 @@ namespace RecrownedAthenaeum.Render public PrimitiveBatch(Camera2D camera = null, GraphicsDevice graphicsDevice = null, int verticesPerBatch = 500) { this.graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice); - camera = camera ?? (Configuration.Camera2D); + this.camera = camera ?? (Configuration.Camera2D); basicEffect = new BasicEffect(this.graphicsDevice); basicEffect.VertexColorEnabled = true; - basicEffect.Projection = camera.projectionMatrix; - basicEffect.View = camera.ViewMatrix; - basicEffect.World = Matrix.Identity; vertices = new List(verticesPerBatch); } @@ -56,9 +58,12 @@ 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; + basicEffect.View = camera.ViewMatrix; + basicEffect.Projection = camera.projectionMatrix; basicEffect.CurrentTechnique.Passes[0].Apply(); began = true; } @@ -106,7 +111,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(); bufferPosition = 0;