MeshBatch now only pushes camera matrices when nessecary.

This commit is contained in:
Harrison Deng 2020-06-27 17:06:32 -05:00
parent 364803bf6c
commit 9f6ef94f06
2 changed files with 16 additions and 14 deletions

View File

@ -47,7 +47,6 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
return Vector3.Cross(CameraFront, -CameraRight);
}
}
public event EventHandler projectionUpdateEvent;
public bool Orthographic {
get {
@ -158,6 +157,18 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
public Vector3 MoveTo { get; set; }
/// <summary>
/// Whether or not the projection has changed from the last time it was read.
/// </summary>
/// <value>A bool value indicating if the projection matrix has changed from the last retrieval.</value>
public bool ProjectionChanged { get => projectionUpdated; }
/// <summary>
/// Whether or not the camera view has changed from the last time it was read.
/// </summary>
/// <value>A bool value indicating if the matrix has change from the last retrieval.</value>
public bool ViewChanged { get => viewUpdated; }
public Camera(float width, float height) {
if (width <= 0) throw new ArgumentException("Width can't be less than or equal to 0.");
if (height <= 0) throw new ArgumentException("Height can't be less than or equal to 0.");
@ -165,9 +176,5 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
this.Height = height;
this.CameraFront = -Vector3.UnitZ;
}
private void OnProjectionUpdated() {
projectionUpdateEvent?.Invoke(this, new EventArgs());
}
}
}

View File

@ -32,8 +32,8 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
public MeshBatch(Camera camera, RenderProgram renderProgram = null, uint BatchVertexSize = 4096) {
if (renderProgram == null) {
VertexShader vert = new VertexShader(EmbeddedResourceUtils.ReadEmbeddedResourceText("default.vert"));
FragmentShader frag = new FragmentShader(EmbeddedResourceUtils.ReadEmbeddedResourceText("default.frag"));
VertexShader vert = new VertexShader(EmbeddedResUtils.ReadEmbeddedResourceText("default.vert"));
FragmentShader frag = new FragmentShader(EmbeddedResUtils.ReadEmbeddedResourceText("default.frag"));
renderProgram = new RenderProgram(null, vert, frag);
}
this.renderProgram = renderProgram;
@ -56,7 +56,6 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
texturedALoc = GLContext.GetUniformLocation(renderProgram.Handle, "textured");
vertexBuffers.defineVertexAttributes(definitions: definitions);
camera.projectionUpdateEvent += ProjectionMatrixChanged;
GLContext.UniformMatrix4fv(projALoc, 1, false, camera.ProjectionMatrix.ToColumnMajorArray());
}
@ -146,17 +145,14 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
vertexBuffers.BufferVertices(data, true);
vertexBuffers.BufferIndices(indices, true);
GLContext.UniformMatrix4fv(modelALoc, 1, false, modelsMatrix.ToColumnMajorArray());
GLContext.UniformMatrix4fv(viewALoc, 1, false, camera.ViewMatrix.ToColumnMajorArray());
if (camera.ViewChanged) GLContext.UniformMatrix4fv(viewALoc, 1, false, camera.ViewMatrix.ToColumnMajorArray());
if (camera.ProjectionChanged) GLContext.UniformMatrix4fv(projALoc, 1, false, camera.ProjectionMatrix.ToColumnMajorArray());
GLContext.MultiDrawElementsBaseVertex(PrimitiveType.Triangles, lengths, DrawElementsType.UnsignedInt, indiceOffsets, offsetIndex, verticeOffsets);
dataIndex = 0;
indicesIndex = 0;
offsetIndex = 0;
}
private void ProjectionMatrixChanged(object sender, EventArgs args) {
GLContext.UniformMatrix4fv(projALoc, 1, false, camera.ProjectionMatrix.ToColumnMajorArray());
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
@ -173,7 +169,6 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
indices = null;
lengths = null;
verticeOffsets = null;
camera.projectionUpdateEvent -= ProjectionMatrixChanged;
}
}