From 4f5bc7b5cd2aaf0e6891cb13f5236e8335e12d1d Mon Sep 17 00:00:00 2001 From: Shylie Date: Tue, 14 May 2024 17:16:06 -0500 Subject: [PATCH] Fix layering --- examples/basic.cpp | 30 +++++++++++++++++++----------- source/glerminal.cpp | 38 ++++++++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/examples/basic.cpp b/examples/basic.cpp index c1303e8..345439b 100644 --- a/examples/basic.cpp +++ b/examples/basic.cpp @@ -4,19 +4,27 @@ namespace { void init() { - glerminal_update_palette(0, 0xFF0000FF); - glerminal_update_palette(1, 0x00FF00FF); + glerminal_update_palette(0, 0x00000000); + glerminal_update_palette(1, 0xFF00001F); + glerminal_update_palette(2, 0x00FF001F); - glerminal_update_sprite(0, { - 1, 1, 1, 1, 1, 1, 1, 1, - 1, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0 + glerminal_update_sprite(1, { + 2, 2, 2, 2, 2, 2, 2, 2, + 2, 1, 1, 1, 1, 1, 1, 2, + 2, 1, 1, 1, 1, 1, 1, 2, + 2, 1, 1, 1, 1, 1, 1, 2, + 2, 1, 1, 1, 1, 1, 1, 2, + 2, 1, 1, 1, 1, 1, 1, 2, + 2, 1, 1, 1, 1, 1, 1, 2, + 2, 2, 2, 2, 2, 2, 2, 2 }); + + glerminal_set(0, 0, 0, 1); + glerminal_set(1, 0, 0, 1); + glerminal_set(1, 0, 1, 1); + glerminal_set(2, 0, 0, 1); + glerminal_set(2, 0, 1, 1); + glerminal_set(2, 0, 2, 1); } void mainloop(float dt) diff --git a/source/glerminal.cpp b/source/glerminal.cpp index a416872..0c2cb56 100644 --- a/source/glerminal.cpp +++ b/source/glerminal.cpp @@ -25,18 +25,19 @@ namespace constexpr char* VERTEX_SHADER_SOURCE = "#version 400 core\n" "layout (location = 0) in vec2 position;\n" - "layout (location = 1) in int sprite;\n" + "layout (location = 1) in ivec4 sprite[4];\n" "uniform vec4 " SCREEN_SIZE_UNIFORM_NAME ";\n" "out VS_OUT {\n" - " flat int sprite;\n" + " flat ivec4 sprite[4];\n" " vec2 texcoord;\n" "} vs_out;\n" "void main()\n" "{\n" " vs_out.sprite = sprite;\n" - " vs_out.texcoord = position + 1;\n" + " vs_out.texcoord = vec2(position.x + 1, -position.y);\n" " vec2 cell_position = vec2(1 + gl_InstanceID - " SCREEN_SIZE_UNIFORM_NAME ".x * floor(gl_InstanceID * " SCREEN_SIZE_UNIFORM_NAME ".z), -floor((gl_InstanceID) * " SCREEN_SIZE_UNIFORM_NAME ".z));\n" - " gl_Position = vec4((position + cell_position) * " SCREEN_SIZE_UNIFORM_NAME ".zw * 2 + vec2(-1, 1), 0, 1);\n" + " vec2 temp = vec2((position + cell_position) * " SCREEN_SIZE_UNIFORM_NAME ".zw * 2 + vec2(-1, 1));\n" + " gl_Position = vec4(temp.x, -temp.y, 0, 1);\n" "}"; constexpr char* GEOMETRY_SHADER_SOURCE = @@ -45,7 +46,7 @@ namespace "layout (triangle_strip, max_vertices = 48) out;\n" "layout (invocations = 16) in;\n" "in VS_OUT {\n" - " flat int sprite;\n" + " flat ivec4 sprite[4];\n" " vec2 texcoord;\n" "} gs_in[];\n" "flat out int sprite;\n" @@ -54,17 +55,17 @@ namespace "{\n" " gl_Layer = gl_InvocationID;\n" " gl_Position = gl_in[0].gl_Position;\n" - " sprite = gs_in[0].sprite;\n" + " sprite = gs_in[0].sprite[gl_InvocationID / 16][gl_InvocationID % 16];\n" " texcoord = gs_in[0].texcoord;\n" " EmitVertex();\n" " gl_Layer = gl_InvocationID;\n" " gl_Position = gl_in[1].gl_Position;\n" - " sprite = gs_in[1].sprite;\n" + " sprite = gs_in[1].sprite[gl_InvocationID / 16][gl_InvocationID % 16];\n" " texcoord = gs_in[1].texcoord;\n" " EmitVertex();\n" " gl_Layer = gl_InvocationID;\n" " gl_Position = gl_in[2].gl_Position;\n" - " sprite = gs_in[2].sprite;\n" + " sprite = gs_in[2].sprite[gl_InvocationID / 16][gl_InvocationID % 16];\n" " texcoord = gs_in[2].texcoord;\n" " EmitVertex();\n" " EndPrimitive();\n" @@ -100,10 +101,10 @@ namespace "void main()\n" "{\n" " vec3 current_color = vec3(0);\n" - " for (int i = 15; i >= 0; i--)\n" + " for (int i = 0; i < 16; i++)\n" " {\n" " vec4 texsample = texture(" LAYERS_UNIFORM_NAME ", vec3(texcoord, i));\n" - " current_color = mix(current_color, texsample.xyz, texsample.w);\n" + " current_color = mix(current_color, texsample.rgb, texsample.a);\n" " }\n" " FragColor = vec4(current_color, 1);\n" "}"; @@ -195,7 +196,7 @@ namespace glerminal { if (x < GRID_WIDTH && y < GRID_HEIGHT && layer < LAYER_COUNT) { - m_cells[x + y * GRID_WIDTH + layer * GRID_AREA] = sprite; + m_cells[layer + x * LAYER_COUNT + y * LAYER_COUNT * GRID_WIDTH] = sprite; } } @@ -223,8 +224,8 @@ namespace glerminal { m_palette[4 * id + 0] = ((color >> 24) & 0xFF) / 255.0f; m_palette[4 * id + 1] = ((color >> 16) & 0xFF) / 255.0f; - m_palette[4 * id + 2] = ((color >> 8) & 0xFF) / 255.0f; - m_palette[4 * id + 3] = ((color >> 0) & 0xFF) / 255.0f; + m_palette[4 * id + 2] = ((color >> 8) & 0xFF) / 255.0f; + m_palette[4 * id + 3] = ((color >> 0) & 0xFF) / 255.0f; } } @@ -350,8 +351,17 @@ namespace glerminal glBindBuffer(GL_ARRAY_BUFFER, m_instance_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(m_cells), m_cells, GL_STREAM_DRAW); glEnableVertexAttribArray(1); - glVertexAttribIPointer(1, 1, GL_UNSIGNED_BYTE, sizeof(*m_cells), reinterpret_cast(0)); + glVertexAttribIPointer(1, 4, GL_UNSIGNED_BYTE, 16 * sizeof(*m_cells), reinterpret_cast(0)); glVertexAttribDivisor(1, 1); + glEnableVertexAttribArray(2); + glVertexAttribIPointer(2, 4, GL_UNSIGNED_BYTE, 16 * sizeof(*m_cells), reinterpret_cast(4)); + glVertexAttribDivisor(2, 1); + glEnableVertexAttribArray(3); + glVertexAttribIPointer(3, 4, GL_UNSIGNED_BYTE, 16 * sizeof(*m_cells), reinterpret_cast(8)); + glVertexAttribDivisor(3, 1); + glEnableVertexAttribArray(4); + glVertexAttribIPointer(4, 4, GL_UNSIGNED_BYTE, 16 * sizeof(*m_cells), reinterpret_cast(12)); + glVertexAttribDivisor(4, 1); // set up static vertex attributes glGenVertexArrays(1, &m_screen_vao);