Lots of progress on rendering textures.

This commit is contained in:
2020-06-05 23:49:45 -05:00
parent 23597f02b1
commit 1c4ca6c97b
47 changed files with 620 additions and 246 deletions

View File

@@ -5,47 +5,60 @@ using SlatedGameToolkit.Framework.Graphics.Meshes;
using SlatedGameToolkit.Framework.Graphics;
using SlatedGameToolkit.Framework.Graphics.Textures;
using SlatedGameToolkit.Framework.Graphics.Shaders;
using SlatedGameToolkit.Framework.Graphics.Programs;
using SlatedGameToolkit.Framework.Exceptions;
namespace SlatedGameToolkit.Framework.Graphics.Render
{
public class Batch : IDisposable {
private int projLoc, viewLoc, modelLoc;
private Camera camera;
private GLShaderProgram shaderProgram;
private Texture texture;
private bool disposed;
private VertexArray vertexArray;
private const int VERTEX_LENGTH = 6;
private const int VERTEX_LENGTH = 9;
private GLMultiDrawElements multiDrawElements;
public bool Batching { get; private set; }
private Matrix4x4 modelsMatrix;
private float[] data;
private uint[] indices;
private uint[] lengths;
private uint[] offsets;
private uint dataIndex, indicesIndex, lengthsIndex;
public Batch(uint BatchVertexSize = 4096) {
multiDrawElements = GLFunctionUtils.RetrieveGLDelegate<GLMultiDrawElements>("glMultiDrawElements");
public Batch(Camera camera, GLShaderProgram shaderProgram, uint BatchVertexSize = 4096) {
multiDrawElements = OpenGL.RetrieveGLDelegate<GLMultiDrawElements>("glMultiDrawElements");
this.camera = camera;
this.shaderProgram = shaderProgram;
indices = new uint[BatchVertexSize];
lengths = new uint[BatchVertexSize];
offsets = new uint[BatchVertexSize];
data = new float[BatchVertexSize * VERTEX_LENGTH];
vertexArray = new VertexArray();
GLGetAttribLocation attribLocation = OpenGL.RetrieveGLDelegate<GLGetAttribLocation>("glGetAttribLocation");
VertexAttributeDefinition[] definitions = new VertexAttributeDefinition[3];
definitions[0] = new VertexAttributeDefinition(0, 3, 3 * sizeof(float), 0);
definitions[1] = new VertexAttributeDefinition(1, 4, 1 * sizeof(float), 3 * sizeof(float));
definitions[2] = new VertexAttributeDefinition(2, 2, 2 * sizeof(float), 4 * sizeof(float));
definitions[0] = new VertexAttributeDefinition((uint)shaderProgram.GetAttributeLocation("aPosition"), 3, 3 * sizeof(float), 0);
definitions[1] = new VertexAttributeDefinition((uint)shaderProgram.GetAttributeLocation("aColor"), 4, 4 * sizeof(float), 3 * sizeof(float));
definitions[2] = new VertexAttributeDefinition((uint)shaderProgram.GetAttributeLocation("aTexCoord"), 2, 2 * sizeof(float), (4 + 3) * sizeof(float));
modelLoc = shaderProgram.GetUniformLocation("models");
viewLoc = shaderProgram.GetUniformLocation("view");
projLoc = shaderProgram.GetUniformLocation("projection");
vertexArray.defineVertexAttributes(definitions: definitions);
}
public virtual void Begin(Texture texture) {
public virtual void Begin(Texture texture, Matrix4x4 modelsMatrix) {
if (Batching) throw new InvalidOperationException("This batch is already started.");
this.texture = texture ?? throw new ArgumentNullException("texture");
this.Batching = true;
this.modelsMatrix = modelsMatrix;
}
public virtual void Dispose()
{
if (disposed) return;
if (Batching) End();
disposed = true;
vertexArray.Dispose();
}
@@ -62,7 +75,13 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
data[dataIndex] = vertices[i].Item1.Z;
dataIndex++;
data[dataIndex] = color.ToArgb();
data[dataIndex] = (float) color.A / byte.MaxValue;
dataIndex++;
data[dataIndex] = (float) color.R / byte.MaxValue;
dataIndex++;
data[dataIndex] = (float) color.G / byte.MaxValue;
dataIndex++;
data[dataIndex] = (float) color.B / byte.MaxValue;
dataIndex++;
data[dataIndex] = vertices[i].Item2.X;
@@ -93,7 +112,10 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
dataIndex = 0;
indicesIndex = 0;
lengthsIndex = 0;
multiDrawElements(GLEnums.GL_TRIANGLE_STRIP, lengths, GLEnums.GL_UNSIGNED_INT, IntPtr.Zero, lengths.Length);
shaderProgram.SetUniformMatrix4x4(modelLoc, modelsMatrix);
shaderProgram.SetUniformMatrix4x4(viewLoc, camera.ViewMatrix);
shaderProgram.SetUniformMatrix4x4(projLoc, camera.ProjectionMatrix);
multiDrawElements(GLEnum.GL_TRIANGLE_STRIP, lengths, GLEnum.GL_UNSIGNED_INT, offsets, lengths.Length);
}
}
}