More renderer work

This commit is contained in:
Matt Thorson 2020-09-08 20:20:26 -07:00
parent 52d1d5f383
commit ae21809566
7 changed files with 127 additions and 97 deletions

View File

@ -103,7 +103,7 @@ namespace Strawberry.Sample
base.Draw(); base.Draw();
DrawHitbox(.Green); DrawHitbox(.Green);
//Game.Batcher.Tex(Assets.Textures["test"], X - 4, Y - 8); Game.Batcher.Tex(Assets.Textures["test"], X - 4, Y - 8);
} }
} }
} }

View File

@ -54,7 +54,7 @@ namespace Strawberry
Directory.SetCurrentDirectory(exeDir); Directory.SetCurrentDirectory(exeDir);
platformLayer.Init(); platformLayer.Init();
Batcher = new Batcher(); Batcher = platformLayer.CreateBatcher();
VirtualInputs = new List<VirtualInput>(); VirtualInputs = new List<VirtualInput>();
Input.[Friend]Init(); Input.[Friend]Init();

View File

@ -2,108 +2,23 @@ using System.Collections;
namespace Strawberry namespace Strawberry
{ {
public class Batcher public abstract class Batcher
{ {
private List<Batch> batches = new .() ~ delete _; protected List<Batch> batches = new .() ~ delete _;
private List<Vertex> vertices = new .() ~ delete _; protected List<Vertex> vertices = new .() ~ delete _;
private List<uint32> indices = new .() ~ delete _; protected List<uint32> indices = new .() ~ delete _;
private uint32 vaoID; public abstract void Draw();
private uint32 vertexBufferID;
private uint32 indexBufferID;
public this() protected ref Batch GetBatch(BatchModes mode, Texture texture)
{
GL.glGenVertexArrays(1, &vaoID);
GL.glBindVertexArray(vaoID);
GL.glGenBuffers(1, &vertexBufferID);
GL.glGenBuffers(1, &indexBufferID);
GL.glBindVertexArray(0);
}
public ~this()
{
GL.glDeleteBuffers(1, &vertexBufferID);
GL.glDeleteBuffers(1, &indexBufferID);
GL.glDeleteVertexArrays(1, &vaoID);
}
public void Draw()
{
GL.glDisable(GL.GL_CULL_FACE);
GL.glBindVertexArray(vaoID);
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexBufferID);
GL.glEnableVertexAttribArray(0);
GL.glVertexAttribPointer(0, 2, GL.GL_FLOAT, GL.GL_FALSE, sizeof(Vertex), (void*)0);
GL.glEnableVertexAttribArray(1);
GL.glVertexAttribPointer(1, 2, GL.GL_FLOAT, GL.GL_FALSE, sizeof(Vertex), (void*)8);
GL.glEnableVertexAttribArray(2);
GL.glVertexAttribPointer(2, 4, GL.GL_UNSIGNED_BYTE, GL.GL_TRUE, sizeof(Vertex), (void*)16);
GL.glEnableVertexAttribArray(3);
GL.glVertexAttribPointer(3, 3, GL.GL_UNSIGNED_BYTE, GL.GL_TRUE, sizeof(Vertex), (void*)20);
GL.glBufferData(GL.GL_ARRAY_BUFFER, vertices.Count * sizeof(Vertex), vertices.Ptr, GL.GL_DYNAMIC_DRAW);
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);
for (let b in batches)
{
if (b.Mode == .Shape)
GL.glBindTexture(GL.GL_TEXTURE_2D, 0);
else
GL.glBindTexture(GL.GL_TEXTURE_2D, b.Texture.Handle);
GL.glDrawElements(GL.GL_TRIANGLES, b.IndicesCount, GL.GL_UNSIGNED_INT, (void*)(b.IndicesStart * sizeof(uint32)));
}
GL.glBindVertexArray(0);
vertices.Clear();
indices.Clear();
batches.Clear();
}
private ref Batch GetBatch(BatchModes mode, Texture texture)
{ {
if (batches.Count == 0 || !batches.Back.Matches(mode, texture)) if (batches.Count == 0 || !batches.Back.Matches(mode, texture))
batches.Add(Batch(mode, texture, indices.Count)); batches.Add(Batch(mode, texture, indices.Count));
return ref batches.Back; return ref batches.Back;
} }
protected void PushQuad(BatchModes mode, Texture texture, Vertex a, Vertex b, Vertex c, Vertex d) protected abstract void PushQuad(BatchModes mode, Texture texture, Vertex a, Vertex b, Vertex c, Vertex d);
{ protected abstract void PushTri(BatchModes mode, Texture texture, Vertex a, Vertex b, Vertex c);
GetBatch(mode, texture).IndicesCount += 6;
uint32 count = (uint32)vertices.Count;
vertices.Add(a);
vertices.Add(b);
vertices.Add(c);
vertices.Add(d);
indices.Add(count + 0);
indices.Add(count + 1);
indices.Add(count + 2);
indices.Add(count + 0);
indices.Add(count + 2);
indices.Add(count + 3);
}
protected void PushTri(BatchModes mode, Texture texture, Vertex a, Vertex b, Vertex c)
{
GetBatch(mode, texture).IndicesCount += 3;
uint32 count = (uint32)vertices.Count;
vertices.Add(a);
vertices.Add(b);
vertices.Add(c);
indices.Add(count + 0);
indices.Add(count + 1);
indices.Add(count + 2);
}
public void Rect(float x, float y, float w, float h, Color color) public void Rect(float x, float y, float w, float h, Color color)
{ {

View File

@ -23,5 +23,6 @@ namespace Strawberry
//Graphics //Graphics
public abstract Texture LoadTexture(String path); public abstract Texture LoadTexture(String path);
public abstract Batcher CreateBatcher();
} }
} }

View File

@ -0,0 +1,103 @@
namespace Strawberry.SDL2
{
public class SDL2Batcher : Batcher
{
private SDL2PlatformLayer platformLayer;
private uint32 vaoID;
private uint32 vertexBufferID;
private uint32 indexBufferID;
public this(SDL2PlatformLayer platformLayer)
{
this.platformLayer = platformLayer;
GL.glGenVertexArrays(1, &vaoID);
GL.glBindVertexArray(vaoID);
GL.glGenBuffers(1, &vertexBufferID);
GL.glGenBuffers(1, &indexBufferID);
GL.glBindVertexArray(0);
}
public ~this()
{
GL.glDeleteBuffers(1, &vertexBufferID);
GL.glDeleteBuffers(1, &indexBufferID);
GL.glDeleteVertexArrays(1, &vaoID);
}
public override void Draw()
{
GL.glDisable(GL.GL_CULL_FACE);
GL.glBindVertexArray(vaoID);
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexBufferID);
GL.glEnableVertexAttribArray(0);
GL.glVertexAttribPointer(0, 2, GL.GL_FLOAT, GL.GL_FALSE, sizeof(Vertex), (void*)0);
GL.glEnableVertexAttribArray(1);
GL.glVertexAttribPointer(1, 2, GL.GL_FLOAT, GL.GL_FALSE, sizeof(Vertex), (void*)8);
GL.glEnableVertexAttribArray(2);
GL.glVertexAttribPointer(2, 4, GL.GL_UNSIGNED_BYTE, GL.GL_TRUE, sizeof(Vertex), (void*)16);
GL.glEnableVertexAttribArray(3);
GL.glVertexAttribPointer(3, 3, GL.GL_UNSIGNED_BYTE, GL.GL_TRUE, sizeof(Vertex), (void*)20);
GL.glBufferData(GL.GL_ARRAY_BUFFER, vertices.Count * sizeof(Vertex), vertices.Ptr, GL.GL_DYNAMIC_DRAW);
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);
for (let b in batches)
{
if (b.Mode == .Shape)
GL.glBindTexture(GL.GL_TEXTURE_2D, 0);
else
{
GL.glActiveTexture(GL.GL_TEXTURE0);
GL.glBindTexture(GL.GL_TEXTURE_2D, b.Texture.Handle);
int32 num = 0;
GL.glUniform1iv(platformLayer.TextureMatrixLocation, 1, &num);
}
GL.glDrawElements(GL.GL_TRIANGLES, b.IndicesCount, GL.GL_UNSIGNED_INT, (void*)(b.IndicesStart * sizeof(uint32)));
}
GL.glBindVertexArray(0);
vertices.Clear();
indices.Clear();
batches.Clear();
}
protected override void PushQuad(BatchModes mode, Texture texture, Vertex a, Vertex b, Vertex c, Vertex d)
{
GetBatch(mode, texture).IndicesCount += 6;
uint32 count = (uint32)vertices.Count;
vertices.Add(a);
vertices.Add(b);
vertices.Add(c);
vertices.Add(d);
indices.Add(count + 0);
indices.Add(count + 1);
indices.Add(count + 2);
indices.Add(count + 0);
indices.Add(count + 2);
indices.Add(count + 3);
}
protected override void PushTri(BatchModes mode, Texture texture, Vertex a, Vertex b, Vertex c)
{
GetBatch(mode, texture).IndicesCount += 3;
uint32 count = (uint32)vertices.Count;
vertices.Add(a);
vertices.Add(b);
vertices.Add(c);
indices.Add(count + 0);
indices.Add(count + 1);
indices.Add(count + 2);
}
}
}

View File

@ -6,6 +6,9 @@ namespace Strawberry.SDL2
{ {
public class SDL2PlatformLayer : PlatformLayer public class SDL2PlatformLayer : PlatformLayer
{ {
public int TransformMatrixLocation { get; private set; }
public int TextureMatrixLocation { get; private set; }
private SDL.Window* window; private SDL.Window* window;
private SDL.Surface* screen; private SDL.Surface* screen;
private SDL.Rect screenRect; private SDL.Rect screenRect;
@ -97,6 +100,9 @@ namespace Strawberry.SDL2
GL.glAttachShader(glProgram, shader.fragmentHandle); GL.glAttachShader(glProgram, shader.fragmentHandle);
GL.glLinkProgram(glProgram); GL.glLinkProgram(glProgram);
TransformMatrixLocation = GL.glGetUniformLocation(glProgram, "u_matrix");
TextureMatrixLocation = GL.glGetUniformLocation(glProgram, "u_texture");
int32 logLen = 0; int32 logLen = 0;
char8[1024] log; char8[1024] log;
GL.glGetProgramInfoLog(glProgram, 1024, &logLen, &log); GL.glGetProgramInfoLog(glProgram, 1024, &logLen, &log);
@ -153,8 +159,7 @@ namespace Strawberry.SDL2
mat *= Mat4x4.CreateScale(.(1, -1, 1)); mat *= Mat4x4.CreateScale(.(1, -1, 1));
mat *= Mat4x4.CreateTranslation(.(-1, 1, 0)); mat *= Mat4x4.CreateTranslation(.(-1, 1, 0));
let loc = GL.glGetUniformLocation(glProgram, "u_matrix"); GL.glUniformMatrix4fv(TransformMatrixLocation, 1, GL.GL_FALSE, &mat.Values);
GL.glUniformMatrix4fv(loc, 1, GL.GL_FALSE, &mat.Values);
} }
public override void RenderEnd() public override void RenderEnd()
@ -164,6 +169,11 @@ namespace Strawberry.SDL2
SDL.GL_SwapWindow(window); SDL.GL_SwapWindow(window);
} }
public override Batcher CreateBatcher()
{
return new SDL2Batcher(this);
}
public override Texture LoadTexture(String path) public override Texture LoadTexture(String path)
{ {
let surface = SDLImage.Load(path); let surface = SDLImage.Load(path);

View File

@ -1,3 +1,4 @@
using System;
namespace Strawberry namespace Strawberry
{ {
class Shader class Shader