Very basic rendering

This commit is contained in:
Matt Thorson 2020-08-09 18:28:55 -07:00
parent 637dcb21a3
commit c53bc96a5d
4 changed files with 93 additions and 19 deletions

View File

@ -157,7 +157,7 @@ namespace Strawberry
private void Render() private void Render()
{ {
PlatformLayer.RenderBegin(); PlatformLayer.RenderBegin();
Draw(); //Draw();
PlatformLayer.RenderEnd(); PlatformLayer.RenderEnd();
} }

View File

@ -8,9 +8,29 @@ namespace Strawberry
Batch top => batchStack.Count > 0 ? batchStack[batchStack.Count - 1] : null; Batch top => batchStack.Count > 0 ? batchStack[batchStack.Count - 1] : null;
private class Batch public void PushQuad(Vertex a, Vertex b, Vertex c, Vertex d)
{ {
} }
private class Batch
{
uint32 bufferHandle;
List<Vertex> 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;
}
} }
} }

View File

@ -11,11 +11,13 @@ namespace Strawberry
private SDL.Rect screenRect; private SDL.Rect screenRect;
private SDL.Renderer* renderer; private SDL.Renderer* renderer;
private SDL2Shader shader; private SDL2Shader shader;
private SDL.SDL_GLContext glContext;
private uint glProgram;
private uint glVertexShader;
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 uint glProgram;
public override void Init() public override void Init()
{ {
@ -57,7 +59,7 @@ namespace Strawberry
// vertex shader // vertex shader
""" """
#version 330 #version 330
uniform mat4 u_matrix; uniform mat3x2 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;
@ -102,6 +104,19 @@ namespace Strawberry
GL.glGetProgramInfoLog(glProgram, 1024, &logLen, &log); GL.glGetProgramInfoLog(glProgram, 1024, &logLen, &log);
if (logLen > 0) if (logLen > 0)
Calc.Log(&log, logLen); 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 //Audio
@ -146,11 +161,24 @@ namespace Strawberry
{ {
GL.glClearColor(Game.ClearColor.Rf, Game.ClearColor.Gf, Game.ClearColor.Bf, Game.ClearColor.Af); GL.glClearColor(Game.ClearColor.Rf, Game.ClearColor.Gf, Game.ClearColor.Bf, Game.ClearColor.Af);
GL.glClear(GL.GL_COLOR_BUFFER_BIT); 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() public override void RenderEnd()
{ {
GL.glUseProgram(0);
GL.glFlush();
SDL.GL_SwapWindow(window); SDL.GL_SwapWindow(window);
} }

View File

@ -13,22 +13,48 @@ namespace Strawberry
{ {
static public readonly Matrix Identity = Matrix(1, 0, 0, 1, 0, 0); static public readonly Matrix Identity = Matrix(1, 0, 0, 1, 0, 0);
// Row--vv--Column public float[6] Values = .(0, 0, 0, 0, 0, 0);
public float M11;
public float M12;
public float M21;
public float M22;
public float M31;
public float M32;
public this(float m11, float m12, float m21, float m22, float m31, float m32) public this(float m11, float m12, float m21, float m22, float m31, float m32)
{ {
M11 = m11; Values = .(m11, m12, m21, m22, m31, m32);
M12 = m12; }
M21 = m21;
M22 = m22; // Row--vv--Column
M31 = m31; public float M11
M32 = m32; {
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 public bool IsIdentity