From 407620387d7542b548f3a592aede10eb9dbbf9e0 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Thu, 26 Mar 2026 14:48:20 -0500 Subject: [PATCH] Test paletted stuff --- src/dusk/asset/type/assettexture.c | 5 ++- src/dusk/display/display.c | 29 +++++++++++----- src/dusk/display/texture/CMakeLists.txt | 1 + src/dusk/display/texture/palette.c | 10 ++++++ src/dusk/display/texture/palette.h | 19 ++++++++++ src/dusk/display/texture/texture.h | 9 +++-- src/duskgl/display/shader/shadergl.c | 27 +++++++++++++++ src/duskgl/display/shader/shaderunlitgl.c | 25 ++++++++++---- src/duskgl/display/texture/texturegl.c | 42 +++++++++++++++-------- src/duskgl/display/texture/texturegl.h | 4 +++ 10 files changed, 138 insertions(+), 33 deletions(-) create mode 100644 src/dusk/display/texture/palette.c create mode 100644 src/dusk/display/texture/palette.h diff --git a/src/dusk/asset/type/assettexture.c b/src/dusk/asset/type/assettexture.c index bcdca88..a45aa94 100644 --- a/src/dusk/asset/type/assettexture.c +++ b/src/dusk/asset/type/assettexture.c @@ -50,7 +50,10 @@ errorret_t assetTextureLoad(assetentire_t entire) { assetData->height, TEXTURE_FORMAT_PALETTE, (texturedata_t){ - .paletteData = assetData->palette + .paletted = { + .indices = NULL, + .palette = NULL + } } ); diff --git a/src/dusk/display/display.c b/src/dusk/display/display.c index 1bb12ef..1e080e9 100644 --- a/src/dusk/display/display.c +++ b/src/dusk/display/display.c @@ -38,16 +38,27 @@ errorret_t displayInit(void) { errorChain(textInit()); errorChain(screenInit()); - color_t pixels[2 * 2] = { - COLOR_RED, COLOR_GREEN, - COLOR_BLUE, COLOR_WHITE + PALETTES[0].colors[0] = COLOR_RED; + PALETTES[0].colors[1] = COLOR_GREEN; + PALETTES[0].colors[2] = COLOR_BLUE; + PALETTES[0].colors[3] = COLOR_WHITE; + PALETTES[0].count = 4; + + uint8_t indices[4 * 4] = { + 0, 1, 2, 3, + 3, 2, 1, 0, + 0, 1, 2, 3, + 3, 2, 1, 0, }; errorChain(textureInit( &TEXTURE, - 2, 2, - TEXTURE_FORMAT_RGBA, + 4, 4, + TEXTURE_FORMAT_PALETTE, (texturedata_t){ - .rgbaColors = pixels + .paletted = { + .indices = indices, + .palette = &PALETTES[0] + } } )); @@ -79,9 +90,9 @@ errorret_t displayUpdate(void) { // camera.orthographic.top = SCREEN.height; // camera.orthographic.bottom = 0.0f; cameraInitPerspective(&camera); - camera.lookat.position[0] = 10.0f; - camera.lookat.position[1] = 10.0f; - camera.lookat.position[2] = 10.0f; + camera.lookat.position[0] = 3.0f; + camera.lookat.position[1] = 3.0f; + camera.lookat.position[2] = 3.0f; mat4 proj, view, model; cameraGetProjectionMatrix(&camera, proj); diff --git a/src/dusk/display/texture/CMakeLists.txt b/src/dusk/display/texture/CMakeLists.txt index b31d832..23ab16b 100644 --- a/src/dusk/display/texture/CMakeLists.txt +++ b/src/dusk/display/texture/CMakeLists.txt @@ -8,4 +8,5 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME} PUBLIC tileset.c texture.c + palette.c ) \ No newline at end of file diff --git a/src/dusk/display/texture/palette.c b/src/dusk/display/texture/palette.c new file mode 100644 index 0000000..e5e0b6e --- /dev/null +++ b/src/dusk/display/texture/palette.c @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "palette.h" + +palette_t PALETTES[PALETTE_COUNT]; \ No newline at end of file diff --git a/src/dusk/display/texture/palette.h b/src/dusk/display/texture/palette.h new file mode 100644 index 0000000..25c7c8c --- /dev/null +++ b/src/dusk/display/texture/palette.h @@ -0,0 +1,19 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "display/color.h" + +#define PALETTE_COLOR_COUNT 256 +#define PALETTE_COUNT 6 + +typedef struct { + color_t colors[PALETTE_COLOR_COUNT]; + uint8_t count; +} palette_t; + +extern palette_t PALETTES[PALETTE_COUNT]; \ No newline at end of file diff --git a/src/dusk/display/texture/texture.h b/src/dusk/display/texture/texture.h index a7fdbff..ba4fb45 100644 --- a/src/dusk/display/texture/texture.h +++ b/src/dusk/display/texture/texture.h @@ -7,7 +7,7 @@ #pragma once #include "error/error.h" -#include "display/color.h" +#include "display/texture/palette.h" #include "display/texture/textureplatform.h" #ifndef textureInitPlatform @@ -20,8 +20,13 @@ typedef textureformatplatform_t textureformat_t; typedef textureplatform_t texture_t; +typedef struct { + uint8_t *indices; + palette_t *palette; +} texturepalettedata_t; + typedef union texturedata_u { - uint8_t *paletteData; + texturepalettedata_t paletted; color_t *rgbaColors; } texturedata_t; diff --git a/src/duskgl/display/shader/shadergl.c b/src/duskgl/display/shader/shadergl.c index 10e0baa..31f1bdb 100644 --- a/src/duskgl/display/shader/shadergl.c +++ b/src/duskgl/display/shader/shadergl.c @@ -224,6 +224,33 @@ errorret_t shaderSetTextureGL( errorChain(errorGLCheck()); glUniform1i(location, 0); errorChain(errorGLCheck()); + + if(texture->format == TEXTURE_FORMAT_PALETTE) { + shaderParamGetLocationGL(shader, "u_ColorCount", &location); + glUniform1i(location, texture->palette->count); + errorChain(errorGLCheck()); + + shaderParamGetLocationGL(shader, "u_Colors", &location); + GLuint paletteData[texture->palette->count]; + for(size_t i = 0; i < texture->palette->count; i++) { + color_t color = texture->palette->colors[i]; + paletteData[i] = ( + ((uint32_t)color.r << 24) | + ((uint32_t)color.g << 16) | + ((uint32_t)color.b << 8) | + ((uint32_t)color.a << 0) + ); + } + glUniform1uiv(location, texture->palette->count, paletteData); + errorChain(errorGLCheck()); + } + + // PALETTE TEST + // errorChain(shaderParamGetLocationGL(shader, "u_Palette", &location)); + // glActiveTexture(GL_TEXTURE1); + // errorChain(errorGLCheck()); + + #endif errorOk(); diff --git a/src/duskgl/display/shader/shaderunlitgl.c b/src/duskgl/display/shader/shaderunlitgl.c index 3d7c33d..052aa1f 100644 --- a/src/duskgl/display/shader/shaderunlitgl.c +++ b/src/duskgl/display/shader/shaderunlitgl.c @@ -13,28 +13,39 @@ shaderdefinition_t SHADER_UNLIT_DEFINITION = { .vert = "#version 330 core\n" - "layout(location = 0) in vec3 aPos;\n" - "layout(location = 1) in vec2 aTexCoord;\n" - "layout(location = 2) in vec4 aColor;\n" + "layout(location = 0) in vec3 a_Pos;\n" + "layout(location = 1) in vec2 a_TexCoord;\n" + "layout(location = 2) in vec4 a_Color;\n" "uniform mat4 u_Proj;\n" "uniform mat4 u_View;\n" "uniform mat4 u_Model;\n" "out vec4 v_Color;\n" "out vec2 v_TexCoord;\n" "void main() {\n" - " gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n" - " v_Color = aColor;\n" - " v_TexCoord = aTexCoord;\n" + " gl_Position = u_Proj * u_View * u_Model * vec4(a_Pos, 1.0);\n" + " v_Color = a_Color;\n" + " v_TexCoord = a_TexCoord;\n" "}\n", .frag = "#version 330 core\n" "uniform sampler2D u_Texture;\n" + "uniform uint u_Colors[256];\n" + "uniform int u_ColorCount;\n" "in vec4 v_Color;\n" "in vec2 v_TexCoord;\n" "out vec4 FragColor;\n" "void main() {\n" - " FragColor = texture(u_Texture, v_TexCoord) * v_Color;\n" + " vec4 texColor = texture(u_Texture, v_TexCoord);\n" + " uint index = uint(floor(texColor.r * 255.0));\n" + " uint palColor = u_Colors[index];\n" + " float r = float((palColor >> 24) & 0xFFu) / 255.0;\n" + " float g = float((palColor >> 16) & 0xFFu) / 255.0;\n" + " float b = float((palColor >> 8) & 0xFFu) / 255.0;\n" + " float a = float((palColor >> 0) & 0xFFu) / 255.0;\n" + " vec4 paletteColor = vec4(r, g, b, a);\n" + " FragColor = paletteColor;\n" + // " FragColor = paletteColor * v_Color;\n" "}\n" }; #endif \ No newline at end of file diff --git a/src/duskgl/display/texture/texturegl.c b/src/duskgl/display/texture/texturegl.c index 9b890a6..1bd3564 100644 --- a/src/duskgl/display/texture/texturegl.c +++ b/src/duskgl/display/texture/texturegl.c @@ -8,6 +8,7 @@ #include "display/texture/texture.h" #include "assert/assert.h" #include "error/errorgl.h" +#include "util/memory.h" errorret_t textureInitGL( texturegl_t *texture, @@ -29,20 +30,32 @@ errorret_t textureInitGL( break; case TEXTURE_FORMAT_PALETTE: - assertNotNull(data.paletteData, "Palette texture data cannot be NULL"); - glTexImage2D( - GL_TEXTURE_2D, - 0, GL_COLOR_INDEX8_EXT, - width, height, - 0, GL_COLOR_INDEX8_EXT, - GL_UNSIGNED_BYTE, (void*)data.paletteData - ); - errorChain(errorGLCheck()); - - // glColorTableEXT( - // GL_TEXTURE_2D, GL_RGBA, data.palette.palette->colorCount, GL_RGBA, - // GL_UNSIGNED_BYTE, (const void*)data.palette.palette->colors - // ); + assertNotNull(data.paletted.indices, "Palette indices cannot be NULL"); + assertNotNull(data.paletted.palette, "Palette colors cannot be NULL"); + #ifdef DUSK_OPENGL_LEGACY + glTexImage2D( + GL_TEXTURE_2D, + 0, GL_COLOR_INDEX8_EXT, + width, height, + 0, GL_COLOR_INDEX8_EXT, + GL_UNSIGNED_BYTE, (void*)data.paletted.indices + ); + errorChain(errorGLCheck()); + glColorTableEXT( + GL_TEXTURE_2D, GL_RGBA, data.paletted.palette->count, GL_RGBA, + GL_UNSIGNED_BYTE, (const void*)data.paletted.palette->colors + ); + errorChain(errorGLCheck()); + #else + // For modern systems we send to only the R channel and the shader does + // the rest. + glTexImage2D( + GL_TEXTURE_2D, 0, GL_RED, width, height, 0, + GL_RED, GL_UNSIGNED_BYTE, (void*)data.paletted.indices + ); + errorChain(errorGLCheck()); + texture->palette = data.paletted.palette; + #endif break; default: @@ -69,5 +82,6 @@ errorret_t textureDisposeGL(texturegl_t *texture) { glDeleteTextures(1, &texture->id); errorChain(errorGLCheck()); + errorOk(); } \ No newline at end of file diff --git a/src/duskgl/display/texture/texturegl.h b/src/duskgl/display/texture/texturegl.h index bfe408a..427b744 100644 --- a/src/duskgl/display/texture/texturegl.h +++ b/src/duskgl/display/texture/texturegl.h @@ -20,6 +20,10 @@ typedef struct { textureformatgl_t format; int32_t width; int32_t height; + + union { + palette_t *palette; + }; } texturegl_t; /**