mirror of
https://github.com/MaddyThorson/StrawberryBF.git
synced 2024-11-29 17:08:55 +08:00
OpenGL Batcher work
This commit is contained in:
parent
c53bc96a5d
commit
ae0f4c6e19
|
@ -1,36 +1,98 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace Strawberry
|
namespace Strawberry
|
||||||
{
|
{
|
||||||
public class Batcher
|
public class Batcher
|
||||||
{
|
{
|
||||||
List<Batch> batchStack = new System.Collections.List<Batch>() ~ DeleteContainerAndItems!(_);
|
static public int VertexSize => sizeof(Vertex);
|
||||||
|
|
||||||
Batch top => batchStack.Count > 0 ? batchStack[batchStack.Count - 1] : null;
|
private List<Batch> batchStack = new List<Batch>() ~ DeleteContainerAndItems!(_);
|
||||||
|
private Batch top => batchStack.Count > 0 ? batchStack[batchStack.Count - 1] : null;
|
||||||
|
|
||||||
public void PushQuad(Vertex a, Vertex b, Vertex c, Vertex d)
|
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()
|
||||||
|
{
|
||||||
|
GL.glDeleteBuffers(1, &vertexBufferID);
|
||||||
|
GL.glDeleteBuffers(1, &indexBufferID);
|
||||||
|
GL.glDeleteVertexArrays(1, &vaoID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Draw()
|
||||||
|
{
|
||||||
|
GL.glBindVertexArray(vaoID);
|
||||||
|
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexBufferID);
|
||||||
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Batch
|
private class Batch
|
||||||
{
|
{
|
||||||
uint32 bufferHandle;
|
uint32 bufferHandle;
|
||||||
|
|
||||||
List<Vertex> Vertices;
|
|
||||||
|
|
||||||
public this()
|
public this()
|
||||||
{
|
{
|
||||||
GL.glGenBuffers(1, &bufferHandle);
|
//GL.glGenBuffers(1, &bufferHandle);
|
||||||
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferHandle);
|
//GL.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferHandle);
|
||||||
GL.glDeleteBuffers(1, &bufferHandle);
|
//GL.glDeleteBuffers(1, &bufferHandle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Ordered, Packed, CRepr]
|
||||||
private struct Vertex
|
private struct Vertex
|
||||||
{
|
{
|
||||||
public Vector Position;
|
public Vector Position;
|
||||||
public Color Color;
|
public Color Color;
|
||||||
|
public Vector TexCoord;
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -942,7 +942,7 @@ namespace Strawberry
|
||||||
public function void GlGetIntegerv(uint pname, int32* data);
|
public function void GlGetIntegerv(uint pname, int32* data);
|
||||||
public static GlGetIntegerv glGetIntegerv;
|
public static GlGetIntegerv glGetIntegerv;
|
||||||
|
|
||||||
public function uint8 GlGetString(uint name);
|
public function uint8* GlGetString(uint name);
|
||||||
public static GlGetString glGetString;
|
public static GlGetString glGetString;
|
||||||
|
|
||||||
public function void GlGetTexImage(uint target, int level, uint format, uint type, void* pixels);
|
public function void GlGetTexImage(uint target, int level, uint format, uint type, void* pixels);
|
||||||
|
|
|
@ -13,8 +13,6 @@ namespace Strawberry
|
||||||
private SDL2Shader shader;
|
private SDL2Shader shader;
|
||||||
private SDL.SDL_GameController*[] gamepads;
|
private SDL.SDL_GameController*[] gamepads;
|
||||||
private bool* keyboard;
|
private bool* keyboard;
|
||||||
private uint32 vertexArray;
|
|
||||||
private uint32 vertexbuffer;
|
|
||||||
|
|
||||||
private SDL.SDL_GLContext glContext;
|
private SDL.SDL_GLContext glContext;
|
||||||
private uint glProgram;
|
private uint glProgram;
|
||||||
|
@ -59,7 +57,7 @@ namespace Strawberry
|
||||||
// vertex shader
|
// vertex shader
|
||||||
"""
|
"""
|
||||||
#version 330
|
#version 330
|
||||||
uniform mat3x2 u_matrix;
|
uniform mat4 u_matrix;
|
||||||
layout(location=0) in vec2 a_position;
|
layout(location=0) in vec2 a_position;
|
||||||
layout(location=1) in vec2 a_tex;
|
layout(location=1) in vec2 a_tex;
|
||||||
layout(location=2) in vec4 a_color;
|
layout(location=2) in vec4 a_color;
|
||||||
|
@ -105,18 +103,14 @@ namespace Strawberry
|
||||||
if (logLen > 0)
|
if (logLen > 0)
|
||||||
Calc.Log(&log, logLen);
|
Calc.Log(&log, logLen);
|
||||||
|
|
||||||
GL.glGenVertexArrays(1, &vertexArray);
|
GL.glEnableVertexAttribArray(0);
|
||||||
GL.glBindVertexArray(vertexArray);
|
GL.glVertexAttribPointer(0, 2, GL.GL_FLOAT, GL.GL_FALSE, Batcher.VertexSize, (void*)0);
|
||||||
|
GL.glEnableVertexAttribArray(1);
|
||||||
float[?] g_vertex_buffer_data = .(
|
GL.glVertexAttribPointer(1, 4, GL.GL_UNSIGNED_BYTE, GL.GL_TRUE, Batcher.VertexSize, (void*)8);
|
||||||
-1.0f, -1.0f,
|
GL.glEnableVertexAttribArray(2);
|
||||||
1.0f, -1.0f,
|
GL.glVertexAttribPointer(2, 2, GL.GL_FLOAT, GL.GL_FALSE, Batcher.VertexSize, (void*)12);
|
||||||
0.0f, 1.0f,
|
GL.glEnableVertexAttribArray(3);
|
||||||
);
|
GL.glVertexAttribPointer(3, 3, GL.GL_UNSIGNED_BYTE, GL.GL_TRUE, Batcher.VertexSize, (void*)20);
|
||||||
|
|
||||||
GL.glGenBuffers(1, &vertexbuffer);
|
|
||||||
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexbuffer);
|
|
||||||
GL.glBufferData(GL.GL_ARRAY_BUFFER, sizeof(float[6]), &g_vertex_buffer_data, GL.GL_STATIC_DRAW);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Audio
|
//Audio
|
||||||
|
@ -163,16 +157,18 @@ namespace Strawberry
|
||||||
GL.glClear(GL.GL_COLOR_BUFFER_BIT);
|
GL.glClear(GL.GL_COLOR_BUFFER_BIT);
|
||||||
GL.glUseProgram(glProgram);
|
GL.glUseProgram(glProgram);
|
||||||
|
|
||||||
int loc = GL.glGetUniformLocation(glProgram, "u_matrix");
|
float[16] mat =
|
||||||
Matrix mat = Matrix.Identity;
|
.(1, 0, 0, 0,
|
||||||
GL.glUniformMatrix3x2fv(loc, 1, GL.GL_FALSE, &mat.Values);
|
0, 1, 0, 0,
|
||||||
|
0, 0, 1, 0,
|
||||||
|
0, 0, 0, 1);
|
||||||
|
|
||||||
GL.glEnableVertexAttribArray(0);
|
let loc = GL.glGetUniformLocation(glProgram, "u_matrix");
|
||||||
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexbuffer);
|
GL.glUniformMatrix4fv(loc, 1, GL.GL_FALSE, &mat);
|
||||||
GL.glVertexAttribPointer(0, 2, GL.GL_FLOAT, GL.GL_FALSE, 0, (void*)0 );
|
|
||||||
// Draw the triangle !
|
Batcher b = scope Batcher();
|
||||||
GL.glDrawArrays(GL.GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
|
b.PushQuad(.(-1, -1), .(1, -1), .(1, 1), .(-1, 1), .Magenta);
|
||||||
GL.glDisableVertexAttribArray(0);
|
b.Draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RenderEnd()
|
public override void RenderEnd()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user