diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-31 14:47:12 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-31 14:47:12 -0800 |
| commit | 311e4bebcc75f59e123b80d45f99af9e286e0b67 (patch) | |
| tree | 11a76591862900947cfee5972d34f7cbd59a5b69 /src/main.c | |
| parent | 487014fcbc540db8941428aadc820fd14768d46e (diff) | |
Texturing
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 22 |
1 files changed, 22 insertions, 0 deletions
| @@ -9,6 +9,8 @@ | |||
| 9 | #include <swgfx.h> | 9 | #include <swgfx.h> |
| 10 | #include <SDL3/SDL.h> | 10 | #include <SDL3/SDL.h> |
| 11 | #include <SDL3/SDL_timer.h> | 11 | #include <SDL3/SDL_timer.h> |
| 12 | #define STB_IMAGE_IMPLEMENTATION | ||
| 13 | #include <stb_image.h> | ||
| 12 | 14 | ||
| 13 | #include <assert.h> | 15 | #include <assert.h> |
| 14 | #include <stdio.h> | 16 | #include <stdio.h> |
| @@ -67,6 +69,7 @@ typedef struct State { | |||
| 67 | void* gfx_mem; | 69 | void* gfx_mem; |
| 68 | swgfx* gfx; | 70 | swgfx* gfx; |
| 69 | Model* model; | 71 | Model* model; |
| 72 | sgTexture_t texture; | ||
| 70 | Camera camera; | 73 | Camera camera; |
| 71 | CameraController camera_controller; | 74 | CameraController camera_controller; |
| 72 | Uint64 last_tick; | 75 | Uint64 last_tick; |
| @@ -222,6 +225,7 @@ static bool Render(State* state) { | |||
| 222 | sgModelId(state->gfx); | 225 | sgModelId(state->gfx); |
| 223 | sgView(state->gfx, SgVec3FromMathVec3(cam->spatial.p), SgVec3FromMathVec3(cam->spatial.f)); | 226 | sgView(state->gfx, SgVec3FromMathVec3(cam->spatial.p), SgVec3FromMathVec3(cam->spatial.f)); |
| 224 | sgPerspective(state->gfx, cam->fovy, cam->aspect, cam->near, cam->far); | 227 | sgPerspective(state->gfx, cam->fovy, cam->aspect, cam->near, cam->far); |
| 228 | sgTexture(state->gfx, &state->texture); | ||
| 225 | RenderModel(state->gfx, state->model); | 229 | RenderModel(state->gfx, state->model); |
| 226 | /*sgIdx indices[3] = {0, 1, 2}; | 230 | /*sgIdx indices[3] = {0, 1, 2}; |
| 227 | sgVec3 positions[3] = { | 231 | sgVec3 positions[3] = { |
| @@ -285,6 +289,19 @@ static bool Initialize(State* state) { | |||
| 285 | fprintf(stderr, "Failed to load model: [%s]\n", model_path); | 289 | fprintf(stderr, "Failed to load model: [%s]\n", model_path); |
| 286 | return false; | 290 | return false; |
| 287 | } | 291 | } |
| 292 | if (state->model->material.diffuseTexture[0] != 0) { | ||
| 293 | // TODO: When doing lighting, need to gamma-correct here. | ||
| 294 | sgTexture_t texture = {0}; | ||
| 295 | int channels = 0; | ||
| 296 | constexpr int desired_channels = 4; | ||
| 297 | texture.pixels = (sgPixel*)stbi_load(state->model->material.diffuseTexture, &texture.width, &texture.height, &channels, desired_channels); | ||
| 298 | if (!texture.pixels) { | ||
| 299 | fprintf(stderr, "Failed to read texture: [%s]\n", state->model->material.diffuseTexture); | ||
| 300 | return false; | ||
| 301 | } | ||
| 302 | assert(channels == desired_channels); | ||
| 303 | state->texture = texture; | ||
| 304 | } | ||
| 288 | 305 | ||
| 289 | Camera* camera = &state->camera; | 306 | Camera* camera = &state->camera; |
| 290 | camera->fovy = Fovy; | 307 | camera->fovy = Fovy; |
| @@ -307,6 +324,11 @@ static bool Initialize(State* state) { | |||
| 307 | static void Shutdown(State* state) { | 324 | static void Shutdown(State* state) { |
| 308 | assert(state); | 325 | assert(state); |
| 309 | 326 | ||
| 327 | if (state->texture.pixels) { | ||
| 328 | free(state->texture.pixels); | ||
| 329 | state->texture = (sgTexture_t){0}; | ||
| 330 | } | ||
| 331 | |||
| 310 | if (state->model) { | 332 | if (state->model) { |
| 311 | free(state->model); | 333 | free(state->model); |
| 312 | state->model = nullptr; | 334 | state->model = nullptr; |
