From c28261dcfa4fb1a27fb1b6aaff42643744690641 Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Sat, 22 Aug 2020 22:39:05 -0500 Subject: [PATCH] Fixed jumpy interpolation caused by multiple interpolations per frame. --- .../Graphics/Camera.cs | 4 +-- .../Graphics/Render/IPositionInterpolable.cs | 4 +-- .../Graphics/Render/MeshBatchRenderer.cs | 11 +------ .../Graphics/Render/RectangleMesh.cs | 29 +++++++++---------- 4 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/SlatedGameToolkit.Framework/Graphics/Camera.cs b/src/SlatedGameToolkit.Framework/Graphics/Camera.cs index 01687f3..8b413ad 100644 --- a/src/SlatedGameToolkit.Framework/Graphics/Camera.cs +++ b/src/SlatedGameToolkit.Framework/Graphics/Camera.cs @@ -180,9 +180,9 @@ namespace SlatedGameToolkit.Framework.Graphics Logger.Log(string.Format("Camera initial dimensions: {0}x{1}, ratio of view: {2}", width, height, width / height), LogLevel.DEBUG); } - public void InterpolatePosition(float delta) + public void InterpolatePosition(double delta) { - this.Position += delta * (MoveTo - Position); + this.Position += (float) delta * (MoveTo - Position); } } } \ No newline at end of file diff --git a/src/SlatedGameToolkit.Framework/Graphics/Render/IPositionInterpolable.cs b/src/SlatedGameToolkit.Framework/Graphics/Render/IPositionInterpolable.cs index 1a249d2..ead6336 100644 --- a/src/SlatedGameToolkit.Framework/Graphics/Render/IPositionInterpolable.cs +++ b/src/SlatedGameToolkit.Framework/Graphics/Render/IPositionInterpolable.cs @@ -5,9 +5,9 @@ namespace SlatedGameToolkit.Framework.Graphics.Render public interface IPositionInterpolable { /// - /// Is called between each update to allow rendered meshes to be interpolated between positions and time steps. + /// Allows this object to perform interpolation based on the position (delta) between the update frames. /// /// A normalized value between [0, 1] representing the current position between the updates. - void InterpolatePosition(float delta); + void InterpolatePosition(double delta); } } \ No newline at end of file diff --git a/src/SlatedGameToolkit.Framework/Graphics/Render/MeshBatchRenderer.cs b/src/SlatedGameToolkit.Framework/Graphics/Render/MeshBatchRenderer.cs index 0033a79..1fbc263 100644 --- a/src/SlatedGameToolkit.Framework/Graphics/Render/MeshBatchRenderer.cs +++ b/src/SlatedGameToolkit.Framework/Graphics/Render/MeshBatchRenderer.cs @@ -20,7 +20,6 @@ namespace SlatedGameToolkit.Framework.Graphics.Render public bool Debug { get; set; } private const int VERTEX_LENGTH = 9; private bool disposed; - private float batchDelta; public GLContext GLContext {get; private set; } private int projULoc, viewULoc, modelULoc, texturedULoc, singleChanneledULoc, flippedULoc; private Camera camera; @@ -123,12 +122,10 @@ namespace SlatedGameToolkit.Framework.Graphics.Render /// /// The models matrix. /// The time elapsed since the last update. - public virtual void Begin(Matrix4x4 modelsMatrix, double delta) { + public virtual void Begin(Matrix4x4 modelsMatrix) { if (Batching) throw new InvalidOperationException("This batch is already started."); this.Batching = true; this.modelsMatrix = modelsMatrix; - - this.batchDelta = (float)delta; } /// @@ -137,10 +134,6 @@ namespace SlatedGameToolkit.Framework.Graphics.Render /// Draws the given mesh. public virtual void Draw(IMesh mesh) { if (!Batching) throw new InvalidOperationException("This batch has not been begun."); - IPositionInterpolable moveable = mesh as IPositionInterpolable; - if (moveable != null) { - moveable.InterpolatePosition(batchDelta); - } if (mesh.Texture?.Handle != this.texture?.Handle) { Flush(); this.texture = mesh.Texture; @@ -200,8 +193,6 @@ namespace SlatedGameToolkit.Framework.Graphics.Render GLContext.BindTexture(TextureTarget.Texture2D, texture.Handle); } renderProgram.Use(); - camera.InterpolatePosition(batchDelta); - vertexBuffers.BufferVertices(data, true); vertexBuffers.BufferIndices(indices, true); GLContext.UniformMatrix4fv(modelULoc, 1, false, modelsMatrix.ToColumnMajorArray()); diff --git a/src/SlatedGameToolkit.Framework/Graphics/Render/RectangleMesh.cs b/src/SlatedGameToolkit.Framework/Graphics/Render/RectangleMesh.cs index e256a74..ab2cda7 100644 --- a/src/SlatedGameToolkit.Framework/Graphics/Render/RectangleMesh.cs +++ b/src/SlatedGameToolkit.Framework/Graphics/Render/RectangleMesh.cs @@ -9,8 +9,8 @@ namespace SlatedGameToolkit.Framework.Graphics.Render { private Matrix4x4 matRot; private bool changed; + private RectangleF rectangle; private Vector3 rotation; - private Vector2 origin, dimensions; private RectangleF textureBounds; private ValueTuple[] vertices; private uint[] indices; @@ -26,26 +26,26 @@ namespace SlatedGameToolkit.Framework.Graphics.Render public float X { get { - return origin.X; + return rectangle.X; } set { changed = true; - origin.X = value; + rectangle.X = value; } } public float Y { get { - return origin.Y; + return rectangle.Y; } set { changed = true; - origin.Y = value; + rectangle.Y = value; } } @@ -53,13 +53,13 @@ namespace SlatedGameToolkit.Framework.Graphics.Render { get { - return dimensions.X; + return rectangle.Width; } set { changed = true; - dimensions.X = value; + rectangle.Width = value; } } @@ -67,14 +67,14 @@ namespace SlatedGameToolkit.Framework.Graphics.Render { get { - return dimensions.Y; + return rectangle.Height; } set { changed = true; - dimensions.Y = value; + rectangle.Height = value; } } @@ -140,8 +140,7 @@ namespace SlatedGameToolkit.Framework.Graphics.Render public RectangleMesh(RectangleF meshBounds, ITexture texture, Color color) { this.changed = true; this.rotation = Vector3.Zero; - this.origin = Vector2.Zero; - this.dimensions = Vector2.Zero; + this.rectangle = RectangleF.Empty; this.Texture = texture; this.Color = color; this.indices = new uint[] {0, 1, 3, 1, 2, 3}; @@ -166,10 +165,10 @@ namespace SlatedGameToolkit.Framework.Graphics.Render private void CalculateVertices() { if (!changed) return; Vector3[] baseVerts = new Vector3[4]; - baseVerts[0] = new Vector3(this.origin, 0); - baseVerts[1] = new Vector3(this.origin.X + this.dimensions.X, this.origin.Y, 0); - baseVerts[2] = new Vector3(baseVerts[1].X, this.origin.Y + this.dimensions.Y, 0); - baseVerts[3] = new Vector3(this.origin.X, baseVerts[2].Y, 0); + baseVerts[0] = new Vector3(this.rectangle.X, this.rectangle.Y, 0); + baseVerts[1] = new Vector3(this.rectangle.Right, this.rectangle.Y, 0); + baseVerts[2] = new Vector3(baseVerts[1].X, this.rectangle.Bottom, 0); + baseVerts[3] = new Vector3(this.rectangle.X, baseVerts[2].Y, 0); for (int i = 0; i < vertices.Length; i++) {