From c53bc96a5d938e8839f86b7ca0e5e7616d02da70 Mon Sep 17 00:00:00 2001 From: Matt Thorson Date: Sun, 9 Aug 2020 18:28:55 -0700 Subject: [PATCH] Very basic rendering --- src/Core/Game.bf | 2 +- src/PlatformLayer/Batcher.bf | 20 ++++++++++ src/PlatformLayer/SDL2PlatformLayer.bf | 38 ++++++++++++++++--- src/Struct/Matrix.bf | 52 +++++++++++++++++++------- 4 files changed, 93 insertions(+), 19 deletions(-) diff --git a/src/Core/Game.bf b/src/Core/Game.bf index 84b8731..e3b733e 100644 --- a/src/Core/Game.bf +++ b/src/Core/Game.bf @@ -157,7 +157,7 @@ namespace Strawberry private void Render() { PlatformLayer.RenderBegin(); - Draw(); + //Draw(); PlatformLayer.RenderEnd(); } diff --git a/src/PlatformLayer/Batcher.bf b/src/PlatformLayer/Batcher.bf index dace874..4d4b961 100644 --- a/src/PlatformLayer/Batcher.bf +++ b/src/PlatformLayer/Batcher.bf @@ -8,9 +8,29 @@ namespace Strawberry Batch top => batchStack.Count > 0 ? batchStack[batchStack.Count - 1] : null; + public void PushQuad(Vertex a, Vertex b, Vertex c, Vertex d) + { + + } + private class Batch { + uint32 bufferHandle; + List Vertices; + + public this() + { + GL.glGenBuffers(1, &bufferHandle); + GL.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferHandle); + GL.glDeleteBuffers(1, &bufferHandle); + } + } + + private struct Vertex + { + public Vector Position; + public Color Color; } } } diff --git a/src/PlatformLayer/SDL2PlatformLayer.bf b/src/PlatformLayer/SDL2PlatformLayer.bf index d8a7a9f..64d30ea 100644 --- a/src/PlatformLayer/SDL2PlatformLayer.bf +++ b/src/PlatformLayer/SDL2PlatformLayer.bf @@ -11,11 +11,13 @@ namespace Strawberry private SDL.Rect screenRect; private SDL.Renderer* renderer; private SDL2Shader shader; - private SDL.SDL_GLContext glContext; - private uint glProgram; - private uint glVertexShader; private SDL.SDL_GameController*[] gamepads; private bool* keyboard; + private uint32 vertexArray; + private uint32 vertexbuffer; + + private SDL.SDL_GLContext glContext; + private uint glProgram; public override void Init() { @@ -57,7 +59,7 @@ namespace Strawberry // vertex shader """ #version 330 - uniform mat4 u_matrix; + uniform mat3x2 u_matrix; layout(location=0) in vec2 a_position; layout(location=1) in vec2 a_tex; layout(location=2) in vec4 a_color; @@ -102,6 +104,19 @@ namespace Strawberry GL.glGetProgramInfoLog(glProgram, 1024, &logLen, &log); if (logLen > 0) Calc.Log(&log, logLen); + + GL.glGenVertexArrays(1, &vertexArray); + GL.glBindVertexArray(vertexArray); + + float[?] g_vertex_buffer_data = .( + -1.0f, -1.0f, + 1.0f, -1.0f, + 0.0f, 1.0f, + ); + + 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 @@ -146,11 +161,24 @@ namespace Strawberry { GL.glClearColor(Game.ClearColor.Rf, Game.ClearColor.Gf, Game.ClearColor.Bf, Game.ClearColor.Af); GL.glClear(GL.GL_COLOR_BUFFER_BIT); - GL.glCreateProgram(); + GL.glUseProgram(glProgram); + + int loc = GL.glGetUniformLocation(glProgram, "u_matrix"); + Matrix mat = Matrix.Identity; + GL.glUniformMatrix3x2fv(loc, 1, GL.GL_FALSE, &mat.Values); + + GL.glEnableVertexAttribArray(0); + GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexbuffer); + GL.glVertexAttribPointer(0, 2, GL.GL_FLOAT, GL.GL_FALSE, 0, (void*)0 ); + // Draw the triangle ! + GL.glDrawArrays(GL.GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle + GL.glDisableVertexAttribArray(0); } public override void RenderEnd() { + GL.glUseProgram(0); + GL.glFlush(); SDL.GL_SwapWindow(window); } diff --git a/src/Struct/Matrix.bf b/src/Struct/Matrix.bf index e356912..2b27d3d 100644 --- a/src/Struct/Matrix.bf +++ b/src/Struct/Matrix.bf @@ -13,22 +13,48 @@ namespace Strawberry { static public readonly Matrix Identity = Matrix(1, 0, 0, 1, 0, 0); - // Row--vv--Column - public float M11; - public float M12; - public float M21; - public float M22; - public float M31; - public float M32; + public float[6] Values = .(0, 0, 0, 0, 0, 0); public this(float m11, float m12, float m21, float m22, float m31, float m32) { - M11 = m11; - M12 = m12; - M21 = m21; - M22 = m22; - M31 = m31; - M32 = m32; + Values = .(m11, m12, m21, m22, m31, m32); + } + + // Row--vv--Column + public float M11 + { + get => Values[0]; + set mut => Values[0] = value; + } + + public float M12 + { + get => Values[1]; + set mut => Values[1] = value; + } + + public float M21 + { + get => Values[2]; + set mut => Values[2] = value; + } + + public float M22 + { + get => Values[3]; + set mut => Values[3] = value; + } + + public float M31 + { + get => Values[4]; + set mut => Values[4] = value; + } + + public float M32 + { + get => Values[5]; + set mut => Values[5] = value; } public bool IsIdentity