2020-08-15 09:23:02 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System;
|
2020-08-09 16:29:46 +08:00
|
|
|
|
|
|
|
namespace Strawberry
|
|
|
|
{
|
|
|
|
public class Batcher
|
|
|
|
{
|
2020-08-15 09:23:02 +08:00
|
|
|
static public int VertexSize => sizeof(Vertex);
|
2020-08-09 16:29:46 +08:00
|
|
|
|
2020-08-15 09:23:02 +08:00
|
|
|
private List<Batch> batchStack = new List<Batch>() ~ DeleteContainerAndItems!(_);
|
|
|
|
private Batch top => batchStack.Count > 0 ? batchStack[batchStack.Count - 1] : null;
|
2020-08-09 16:29:46 +08:00
|
|
|
|
2020-08-15 09:23:02 +08:00
|
|
|
private List<Vertex> vertices = new .() ~ delete _;
|
|
|
|
private List<uint32> indices = new .() ~ delete _;
|
|
|
|
|
|
|
|
private uint32 vaoID;
|
|
|
|
private uint32 vertexBufferID;
|
|
|
|
private uint32 indexBufferID;
|
|
|
|
|
|
|
|
public this()
|
|
|
|
{
|
|
|
|
GL.glGenVertexArrays(1, &vaoID);
|
|
|
|
GL.glBindVertexArray(vaoID);
|
|
|
|
GL.glGenBuffers(1, &vertexBufferID);
|
|
|
|
GL.glGenBuffers(1, &indexBufferID);
|
|
|
|
GL.glBindVertexArray(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ~this()
|
2020-08-10 09:28:55 +08:00
|
|
|
{
|
2020-08-15 09:23:02 +08:00
|
|
|
GL.glDeleteBuffers(1, &vertexBufferID);
|
|
|
|
GL.glDeleteBuffers(1, &indexBufferID);
|
|
|
|
GL.glDeleteVertexArrays(1, &vaoID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Draw()
|
|
|
|
{
|
2020-08-15 09:40:07 +08:00
|
|
|
GL.glDisable(GL.GL_CULL_FACE);
|
|
|
|
|
2020-08-15 09:23:02 +08:00
|
|
|
GL.glBindVertexArray(vaoID);
|
2020-08-15 09:40:07 +08:00
|
|
|
|
2020-08-15 09:23:02 +08:00
|
|
|
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexBufferID);
|
2020-08-15 09:40:07 +08:00
|
|
|
GL.glEnableVertexAttribArray(0);
|
|
|
|
GL.glVertexAttribPointer(0, 2, GL.GL_FLOAT, GL.GL_FALSE, Batcher.VertexSize, (void*)0);
|
|
|
|
GL.glEnableVertexAttribArray(1);
|
|
|
|
GL.glVertexAttribPointer(1, 2, GL.GL_FLOAT, GL.GL_FALSE, Batcher.VertexSize, (void*)8);
|
|
|
|
GL.glEnableVertexAttribArray(2);
|
|
|
|
GL.glVertexAttribPointer(2, 4, GL.GL_UNSIGNED_BYTE, GL.GL_TRUE, Batcher.VertexSize, (void*)16);
|
|
|
|
GL.glEnableVertexAttribArray(3);
|
|
|
|
GL.glVertexAttribPointer(3, 3, GL.GL_UNSIGNED_BYTE, GL.GL_TRUE, Batcher.VertexSize, (void*)20);
|
2020-08-15 09:23:02 +08:00
|
|
|
GL.glBufferData(GL.GL_ARRAY_BUFFER, vertices.Count * sizeof(Vertex), vertices.Ptr, GL.GL_DYNAMIC_DRAW);
|
2020-08-15 09:40:07 +08:00
|
|
|
|
2020-08-15 09:23:02 +08:00
|
|
|
GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
|
|
|
|
GL.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, indices.Count * sizeof(uint32), indices.Ptr, GL.GL_DYNAMIC_DRAW);
|
|
|
|
|
|
|
|
GL.glDrawElements(GL.GL_TRIANGLES, indices.Count, GL.GL_UNSIGNED_INT, (void*)0);
|
|
|
|
GL.glBindVertexArray(0);
|
|
|
|
|
|
|
|
vertices.Clear();
|
|
|
|
indices.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void PushQuad(Vector a, Vector b, Vector c, Vector d, Color color)
|
|
|
|
{
|
|
|
|
uint32 count = (uint32)vertices.Count;
|
|
|
|
|
|
|
|
vertices.Add(Vertex.Shape(a, color));
|
|
|
|
vertices.Add(Vertex.Shape(b, color));
|
|
|
|
vertices.Add(Vertex.Shape(c, color));
|
|
|
|
vertices.Add(Vertex.Shape(d, color));
|
|
|
|
|
|
|
|
indices.Add(count + 0);
|
|
|
|
indices.Add(count + 1);
|
|
|
|
indices.Add(count + 2);
|
|
|
|
indices.Add(count + 0);
|
|
|
|
indices.Add(count + 2);
|
|
|
|
indices.Add(count + 3);
|
2020-08-10 09:28:55 +08:00
|
|
|
}
|
|
|
|
|
2020-08-09 16:29:46 +08:00
|
|
|
private class Batch
|
|
|
|
{
|
2020-08-10 09:28:55 +08:00
|
|
|
uint32 bufferHandle;
|
|
|
|
|
|
|
|
public this()
|
|
|
|
{
|
2020-08-15 09:23:02 +08:00
|
|
|
//GL.glGenBuffers(1, &bufferHandle);
|
|
|
|
//GL.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferHandle);
|
|
|
|
//GL.glDeleteBuffers(1, &bufferHandle);
|
2020-08-10 09:28:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 09:23:02 +08:00
|
|
|
[Ordered, Packed, CRepr]
|
2020-08-10 09:28:55 +08:00
|
|
|
private struct Vertex
|
|
|
|
{
|
|
|
|
public Vector Position;
|
2020-08-15 09:23:02 +08:00
|
|
|
public Vector TexCoord;
|
2020-08-15 09:40:07 +08:00
|
|
|
public Color Color;
|
2020-08-15 09:23:02 +08:00
|
|
|
public (uint8, uint8, uint8) Mode;
|
|
|
|
|
|
|
|
static public Vertex Shape(Vector pos, Color color)
|
|
|
|
{
|
|
|
|
Vertex v = Vertex();
|
|
|
|
v.Position = pos;
|
|
|
|
v.Color = color;
|
|
|
|
v.Mode = (0, 0, 255);
|
|
|
|
return v;
|
|
|
|
}
|
2020-08-09 16:29:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|