diff --git a/CameraTest/CameraTest.cs b/CameraTest/CameraTest.cs index 2ddd8a7..7c47999 100644 --- a/CameraTest/CameraTest.cs +++ b/CameraTest/CameraTest.cs @@ -107,42 +107,13 @@ namespace CameraTest protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black); + rr.Begin(true); rr.Draw(25, 25, 70, 70, Color.Purple); - /* - * float rotation = 0; - double width = 20; - double height = 20; - int x = 50; - int y = 50; - double topRightAngleFromOrig = Math.Atan(height / width); - pb.Begin(PrimitiveType.LineList); + rr.End(); - - Vector2 bottomLeft = new Vector2(x, y); - Vector2 bottomRight = new Vector2(x + (float)(Math.Cos(rotation) * width), y + (float)(Math.Sin(rotation) * width)); - float origDiagonalHypotenuse = (float)Math.Sqrt(width * width + height * height); - Vector2 topRight = new Vector2(x + (float)Math.Cos(topRightAngleFromOrig + rotation) * origDiagonalHypotenuse, y + (float)Math.Sin(topRightAngleFromOrig + rotation) * origDiagonalHypotenuse); - Vector2 topLeft = new Vector2(x - (float)(Math.Cos((Math.PI / 2f) - rotation) * height), y + (float)(Math.Sin((Math.PI / 2f) - rotation) * height)); - - verts[0] = bottomLeft; - verts[1] = bottomRight; - verts[2] = topRight; - verts[3] = topLeft; - - vertposcol[0] = new VertexPositionColor(new Vector3(verts[0], 0), Color.White); - vertposcol[1] = new VertexPositionColor(new Vector3(verts[1], 0), Color.White); - vertposcol[2] = new VertexPositionColor(new Vector3(verts[2], 0), Color.White); - vertposcol[3] = new VertexPositionColor(new Vector3(verts[3], 0), Color.White); - - EffectTechnique effectTechnique = be.Techniques[0]; - EffectPassCollection effectPassCollection = effectTechnique.Passes; - foreach (EffectPass pass in effectPassCollection) - { - pass.Apply(); - GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineStrip, vertposcol, 0, 4); - } - pb.End(); - */ + rr.Begin(false); + rr.Draw(75, 75, 70, 70, Color.Green); + rr.End(); base.Draw(gameTime); } } diff --git a/Demo/Demo.cs b/Demo/Demo.cs index 513f580..4dd1d68 100644 --- a/Demo/Demo.cs +++ b/Demo/Demo.cs @@ -31,16 +31,13 @@ namespace Demo { // TODO: Add your initialization logic here basicEffect = new BasicEffect(GraphicsDevice); - basicEffect.View = Matrix.CreateLookAt(new Vector3(graphics.PreferredBackBufferWidth/2f, graphics.PreferredBackBufferHeight/2f, 1f), Vector3.Forward, Vector3.Up); - basicEffect.World = Matrix.Identity; - basicEffect.Projection = Matrix.CreateOrthographic(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 1); basicEffect.VertexColorEnabled = true; - vertices = new VertexPositionColor[5]; - vertices[0] = new VertexPositionColor(new Vector3(5, 5, 0), Color.Red); - vertices[1] = new VertexPositionColor(new Vector3(10, 5, 0), Color.Red); + vertices = new VertexPositionColor[4]; + vertices[0] = new VertexPositionColor(new Vector3(0, 0, 0), Color.Red); + vertices[1] = new VertexPositionColor(new Vector3(10, 0, 0), Color.Red); vertices[2] = new VertexPositionColor(new Vector3(10, 10, 0), Color.Red); - vertices[3] = new VertexPositionColor(new Vector3(5, 10, 0), Color.Red); + vertices[3] = new VertexPositionColor(new Vector3(0, 10, 0), Color.Red); base.Initialize(); } @@ -52,6 +49,7 @@ namespace Demo { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); + basicEffect.World = Matrix.CreateOrthographicOffCenter(new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), 0, 1); // TODO: use this.Content to load your game content here } diff --git a/RecrownedAthenaeum.sln b/RecrownedAthenaeum.sln index 5777c91..119010d 100644 --- a/RecrownedAthenaeum.sln +++ b/RecrownedAthenaeum.sln @@ -11,8 +11,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RecrownedAthenaeum.Tools", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CameraTest", "CameraTest\CameraTest.csproj", "{4A9796EE-D10D-4ED8-AE6E-9CE96C3D4FE9}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo", "Demo\Demo.csproj", "{A56A7DFD-C5E7-4D9B-9A9F-25F94EC824BD}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -35,10 +33,6 @@ Global {4A9796EE-D10D-4ED8-AE6E-9CE96C3D4FE9}.Debug|Any CPU.Build.0 = Debug|Any CPU {4A9796EE-D10D-4ED8-AE6E-9CE96C3D4FE9}.Release|Any CPU.ActiveCfg = Release|Any CPU {4A9796EE-D10D-4ED8-AE6E-9CE96C3D4FE9}.Release|Any CPU.Build.0 = Release|Any CPU - {A56A7DFD-C5E7-4D9B-9A9F-25F94EC824BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A56A7DFD-C5E7-4D9B-9A9F-25F94EC824BD}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A56A7DFD-C5E7-4D9B-9A9F-25F94EC824BD}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A56A7DFD-C5E7-4D9B-9A9F-25F94EC824BD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/RecrownedAthenaeum/Camera/Camera3D.cs b/RecrownedAthenaeum/Camera/Camera3D.cs index 9651f7a..dc6a2eb 100644 --- a/RecrownedAthenaeum/Camera/Camera3D.cs +++ b/RecrownedAthenaeum/Camera/Camera3D.cs @@ -76,7 +76,7 @@ namespace RecrownedAthenaeum.Camera public virtual void Apply() { ViewMatrix = Matrix.CreateLookAt(position, lookAt, upDirection); - TransformationMatrix = worldMatrix * ViewMatrix * projectionMatrix; + TransformationMatrix = projectionMatrix * ViewMatrix * worldMatrix; } /// diff --git a/RecrownedAthenaeum/Render/PrimitiveBatch.cs b/RecrownedAthenaeum/Render/PrimitiveBatch.cs index 92a13f3..c9f4ed5 100644 --- a/RecrownedAthenaeum/Render/PrimitiveBatch.cs +++ b/RecrownedAthenaeum/Render/PrimitiveBatch.cs @@ -15,17 +15,18 @@ namespace RecrownedAthenaeum.Render int bufferPosition; BasicEffect basicEffect; PrimitiveType primitiveType; + private 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. + /// Amount of primitives. /// - public int verticesPerPrimitive; + public int primitiveCount; GraphicsDevice graphicsDevice; bool began; bool disposed; Camera3D camera; - + /// /// Creates a batch used to draw primitives. /// @@ -55,7 +56,8 @@ namespace RecrownedAthenaeum.Render { case PrimitiveType.LineList: verticesPerPrimitive = 2; break; case PrimitiveType.TriangleList: verticesPerPrimitive = 3; break; - default: verticesPerPrimitive = 1; break; + case PrimitiveType.LineStrip: verticesPerPrimitive = 1; break; + case PrimitiveType.TriangleStrip: verticesPerPrimitive = 3; break; } basicEffect.World = camera.worldMatrix; basicEffect.View = camera.ViewMatrix; @@ -108,18 +110,21 @@ namespace RecrownedAthenaeum.Render /// /// Flushes the batch. Automatically called if required if using or . Otherwise, manual flushing is required. + /// is used if not zero. Is reset to zero every flush. /// public void Flush() { 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 (primitiveCount == 0) primitiveCount = bufferPosition / verticesPerPrimitive; foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes) { effectPass.Apply(); - graphicsDevice.DrawUserPrimitives(primitiveType, vertices, 0, bufferPosition/verticesPerPrimitive); + graphicsDevice.DrawUserPrimitives(primitiveType, vertices, 0, primitiveCount); } bufferPosition = 0; + primitiveCount = 0; } /// diff --git a/RecrownedAthenaeum/Render/RectangleRenderer.cs b/RecrownedAthenaeum/Render/RectangleRenderer.cs index b36356b..c85de92 100644 --- a/RecrownedAthenaeum/Render/RectangleRenderer.cs +++ b/RecrownedAthenaeum/Render/RectangleRenderer.cs @@ -10,6 +10,7 @@ namespace RecrownedAthenaeum.Render /// public class RectangleRenderer : IDisposable { + private bool filled; private bool disposed; /// /// The used. Needs to be disposed. @@ -23,7 +24,7 @@ namespace RecrownedAthenaeum.Render /// Graphics device to use. Default will use . public RectangleRenderer(Camera2D camera = null, GraphicsDevice graphicsDevice = null) { - primitiveBatch = new PrimitiveBatch(camera, graphicsDevice, 4); + primitiveBatch = new PrimitiveBatch(camera, graphicsDevice); } /// @@ -48,6 +49,23 @@ namespace RecrownedAthenaeum.Render disposed = true; } + /// + /// Begins a batch for rectangles. + /// + /// Whether or not this batch should be filled rectangles. + public void Begin(bool filled = false) + { + this.filled = filled; + primitiveBatch.Begin(filled ? PrimitiveType.TriangleStrip : PrimitiveType.LineStrip); + } + + /// + /// Ends the batch. + /// + public void End() + { + primitiveBatch.End(); + } /// /// Draws a basic rectangle given bottom left and top right. /// @@ -58,35 +76,29 @@ namespace RecrownedAthenaeum.Render /// Color of all vertices of this rectangle. /// Rotation of rectangle. Default is 0 radians. /// If this rectangle should be filled. - public void Draw(int x, int y, int width, int height, Color color, double rotation = 0, bool filled = false) + public void Draw(int x, int y, int width, int height, Color color, float rotation = 0) { - primitiveBatch.Begin(filled ? PrimitiveType.TriangleStrip : PrimitiveType.LineStrip); - + primitiveBatch.primitiveCount = filled ? 3 : 4; 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); - 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((Math.PI / 2f) - rotation) * height), y + (float)(Math.Sin((Math.PI / 2f) - rotation) * height)); + corners[1] = new Vector2(x + width, y); + corners[2] = new Vector2(x + width, y + height); + corners[3] = new Vector2(x, y + height); - if (filled) + if (rotation != 0) { - primitiveBatch.AddVertex(corners[1], color); - primitiveBatch.AddVertex(corners[2], color); - primitiveBatch.AddVertex(corners[0], color); - primitiveBatch.AddVertex(corners[3], color); - } - else - { - primitiveBatch.AddVertex(corners[0], color); - primitiveBatch.AddVertex(corners[1], color); - primitiveBatch.AddVertex(corners[2], color); - primitiveBatch.AddVertex(corners[3], color); + Matrix rotMat = Matrix.CreateRotationZ(rotation); + for (int i = 0; i < corners.Length; i++) + { + Vector2.Transform(corners[i], rotMat); + } } - primitiveBatch.End(); + primitiveBatch.AddVertex(corners[0], color); + primitiveBatch.AddVertex(corners[1], color); + primitiveBatch.AddVertex(corners[2], color); + primitiveBatch.AddVertex(corners[3], color); + primitiveBatch.AddVertex(corners[0], color); } } }