From eea896ce47551bf8116922c126f5d3d1e3733071 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 5 Feb 2026 19:07:28 -0800 Subject: Gamma-correct textures and output in preparation for lighting --- src/main.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 6d7a72d..86ab2ae 100644 --- a/src/main.c +++ b/src/main.c @@ -297,6 +297,7 @@ static bool Render(State* state) { (sgVec2){1.0, 1.0}, }; sgTrianglesIndexed(state->gfx, 3, indices, positions, texcoords);*/ + sgGammaInv(state->gfx, sgColourBuffer(state->gfx), BufferWidth, BufferHeight); sgPresent(state->gfx, WindowDims, window_surface->pixels); if (!SDL_UpdateWindowSurface(state->window)) { @@ -324,6 +325,28 @@ static bool Resize(State* state) { return true; } +static bool LoadTexture(State* state, const char* path) { + assert(state); + if (state->numTextures >= MaxTextures) { + fprintf(stderr, "Cannot load texture. Maximum number of textures loaded\n"); + return false; + } + // TODO: This indexing into the textures array assumes that we have loaded a + // single model. Generalize later. + sgImage* texture = &state->textures[state->numTextures++]; + int channels = 0; + constexpr int desired_channels = 4; + texture->pixels = (sgPixel*)stbi_load(path, &texture->width, &texture->height, &channels, desired_channels); + if (!texture->pixels) { + fprintf(stderr, "Failed to read texture: [%s]\n", path); + return false; + } + assert(channels == desired_channels); + // Gamma-correct for lighting. + sgGamma(state->gfx, texture->pixels, texture->width, texture->height); + return true; +} + static bool Initialize(State* state) { assert(state); @@ -359,16 +382,9 @@ static bool Initialize(State* state) { const ModelMaterial* materials = modelMaterials(state->model); for (size_t i = 0; i < state->model->numMaterials; ++i) { const ModelMaterial* material = &materials[i]; - // TODO: When doing lighting, need to gamma-correct here. - sgImage* texture = &state->textures[state->numTextures++]; - int channels = 0; - constexpr int desired_channels = 4; - texture->pixels = (sgPixel*)stbi_load(material->diffuseTexture, &texture->width, &texture->height, &channels, desired_channels); - if (!texture->pixels) { - fprintf(stderr, "Failed to read texture: [%s]\n", material->diffuseTexture); + if (!LoadTexture(state, material->diffuseTexture)) { return false; } - assert(channels == desired_channels); } Camera* camera = &state->camera; -- cgit v1.2.3