From 68345ada3c155b0c77c9d1eb09cfcaedb8612f36 Mon Sep 17 00:00:00 2001 From: Recrown Date: Wed, 13 Feb 2019 07:43:35 -0600 Subject: [PATCH] rectangle renderer now has correct corner calculations; switched to vertex buffer for primitive batch. still wip. --- RecrownedAthenaeum/Render/PrimitiveBatch.cs | 46 +++++++++++++------ .../Render/RectangleRenderer.cs | 6 +-- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/RecrownedAthenaeum/Render/PrimitiveBatch.cs b/RecrownedAthenaeum/Render/PrimitiveBatch.cs index 1993a48..9cf2391 100644 --- a/RecrownedAthenaeum/Render/PrimitiveBatch.cs +++ b/RecrownedAthenaeum/Render/PrimitiveBatch.cs @@ -13,15 +13,19 @@ namespace RecrownedAthenaeum.Render { VertexPositionColor[] vertices; int bufferPosition; - int maxVertices; - + VertexBuffer vertexBuffer; BasicEffect basicEffect; PrimitiveType primitiveType; - int verticesPerPrimitive; + /// + /// Sets the vertices per primitive. Default is dependent on the primitive type. Lists will generally satisfy the amount of vertices they need, while strips should be set or it will all be interconnected. + /// + public int verticesPerPrimitive; + GraphicsDevice graphicsDevice; bool began; bool disposed; + bool changed; Camera3D camera; /// @@ -36,8 +40,8 @@ namespace RecrownedAthenaeum.Render this.camera = camera ?? (Configuration.Camera2D); basicEffect = new BasicEffect(this.graphicsDevice); basicEffect.VertexColorEnabled = true; - vertices = new VertexPositionColor[verticesPerBatch+1]; - maxVertices = verticesPerBatch; + vertices = new VertexPositionColor[verticesPerBatch]; + vertexBuffer = new VertexBuffer(this.graphicsDevice, typeof(VertexPositionColor), verticesPerBatch, BufferUsage.WriteOnly); } /// @@ -56,11 +60,9 @@ namespace RecrownedAthenaeum.Render case PrimitiveType.TriangleList: verticesPerPrimitive = 3; break; default: verticesPerPrimitive = 1; break; } - basicEffect.World = camera.worldMatrix; basicEffect.View = camera.ViewMatrix; basicEffect.Projection = camera.projectionMatrix; - basicEffect.CurrentTechnique.Passes[0].Apply(); began = true; } @@ -82,20 +84,24 @@ namespace RecrownedAthenaeum.Render /// The color of that vertex. public void AddVertex(Vector2 vertex, Color color) { + changed = true; if (!began) throw new InvalidOperationException("Begin needs to be called before adding vertex."); if (disposed) throw new ObjectDisposedException(this.GetType().Name); if (primitiveType != PrimitiveType.LineStrip && primitiveType != PrimitiveType.TriangleStrip) { - if (bufferPosition + verticesPerPrimitive > maxVertices && (bufferPosition % maxVertices == 0)) + if (bufferPosition + verticesPerPrimitive >= vertices.Length) { - Flush(); - } - else - { - throw new InvalidOperationException("Buffer size doesn't match with primitive type."); + if ((bufferPosition % vertices.Length == 0)) + { + Flush(); + } + else + { + throw new InvalidOperationException("Buffer size doesn't match with primitive type."); + } } } - else if (bufferPosition + verticesPerPrimitive > maxVertices) + else if (bufferPosition + verticesPerPrimitive > vertices.Length) { throw new InvalidOperationException("Buffer size isn't large enough."); } @@ -109,10 +115,20 @@ namespace RecrownedAthenaeum.Render /// public void Flush() { + if (changed) + { + changed = false; + vertexBuffer.SetData(vertices); + } + graphicsDevice.SetVertexBuffer(vertexBuffer); if (!began) throw new InvalidOperationException("Begin needs to be called before flushing."); if (disposed) throw new ObjectDisposedException(this.GetType().Name); if (bufferPosition == 0) return; - graphicsDevice.DrawUserPrimitives(primitiveType, vertices, 0, bufferPosition / verticesPerPrimitive); + foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes) + { + effectPass.Apply(); + graphicsDevice.DrawPrimitives(primitiveType, 0, bufferPosition/verticesPerPrimitive); + } bufferPosition = 0; } diff --git a/RecrownedAthenaeum/Render/RectangleRenderer.cs b/RecrownedAthenaeum/Render/RectangleRenderer.cs index a9173a8..b36356b 100644 --- a/RecrownedAthenaeum/Render/RectangleRenderer.cs +++ b/RecrownedAthenaeum/Render/RectangleRenderer.cs @@ -64,12 +64,12 @@ namespace RecrownedAthenaeum.Render Vector2[] corners = new Vector2[4]; + double topRightAngleFromOrig = Math.Atan((double)height / width); corners[0] = new Vector2(x, y); corners[1] = new Vector2(x + (float)Math.Cos(rotation) * width, y + (float)Math.Sin(rotation) * width); - double topRightAngleFromOrig = Math.Atan(height / (double)width); - float origDiagonalHypotenuse = (float)Math.Sqrt(width * width * height * height); + float origDiagonalHypotenuse = (float)Math.Sqrt((width * width) + (height * height)); corners[2] = new Vector2(x + (float)Math.Cos(topRightAngleFromOrig + rotation) * origDiagonalHypotenuse, y + (float)Math.Sin(topRightAngleFromOrig + rotation) * origDiagonalHypotenuse); - corners[3] = new Vector2(x + (float)Math.Cos(rotation + (Math.PI / 4f)) * height, y + (float)Math.Sin(rotation) * height); + corners[3] = new Vector2(x - (float)(Math.Cos((Math.PI / 2f) - rotation) * height), y + (float)(Math.Sin((Math.PI / 2f) - rotation) * height)); if (filled) {