From 311e4bebcc75f59e123b80d45f99af9e286e0b67 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Wed, 31 Dec 2025 14:47:12 -0800 Subject: Texturing --- src/main.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src') diff --git a/src/main.c b/src/main.c index f1bb893..1f900bc 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,8 @@ #include #include #include +#define STB_IMAGE_IMPLEMENTATION +#include #include #include @@ -67,6 +69,7 @@ typedef struct State { void* gfx_mem; swgfx* gfx; Model* model; + sgTexture_t texture; Camera camera; CameraController camera_controller; Uint64 last_tick; @@ -222,6 +225,7 @@ static bool Render(State* state) { sgModelId(state->gfx); sgView(state->gfx, SgVec3FromMathVec3(cam->spatial.p), SgVec3FromMathVec3(cam->spatial.f)); sgPerspective(state->gfx, cam->fovy, cam->aspect, cam->near, cam->far); + sgTexture(state->gfx, &state->texture); RenderModel(state->gfx, state->model); /*sgIdx indices[3] = {0, 1, 2}; sgVec3 positions[3] = { @@ -285,6 +289,19 @@ static bool Initialize(State* state) { fprintf(stderr, "Failed to load model: [%s]\n", model_path); return false; } + if (state->model->material.diffuseTexture[0] != 0) { + // TODO: When doing lighting, need to gamma-correct here. + sgTexture_t texture = {0}; + int channels = 0; + constexpr int desired_channels = 4; + texture.pixels = (sgPixel*)stbi_load(state->model->material.diffuseTexture, &texture.width, &texture.height, &channels, desired_channels); + if (!texture.pixels) { + fprintf(stderr, "Failed to read texture: [%s]\n", state->model->material.diffuseTexture); + return false; + } + assert(channels == desired_channels); + state->texture = texture; + } Camera* camera = &state->camera; camera->fovy = Fovy; @@ -307,6 +324,11 @@ static bool Initialize(State* state) { static void Shutdown(State* state) { assert(state); + if (state->texture.pixels) { + free(state->texture.pixels); + state->texture = (sgTexture_t){0}; + } + if (state->model) { free(state->model); state->model = nullptr; -- cgit v1.2.3