diff options
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 32 |
1 files changed, 24 insertions, 8 deletions
| @@ -297,6 +297,7 @@ static bool Render(State* state) { | |||
| 297 | (sgVec2){1.0, 1.0}, | 297 | (sgVec2){1.0, 1.0}, |
| 298 | }; | 298 | }; |
| 299 | sgTrianglesIndexed(state->gfx, 3, indices, positions, texcoords);*/ | 299 | sgTrianglesIndexed(state->gfx, 3, indices, positions, texcoords);*/ |
| 300 | sgGammaInv(state->gfx, sgColourBuffer(state->gfx), BufferWidth, BufferHeight); | ||
| 300 | sgPresent(state->gfx, WindowDims, window_surface->pixels); | 301 | sgPresent(state->gfx, WindowDims, window_surface->pixels); |
| 301 | 302 | ||
| 302 | if (!SDL_UpdateWindowSurface(state->window)) { | 303 | if (!SDL_UpdateWindowSurface(state->window)) { |
| @@ -324,6 +325,28 @@ static bool Resize(State* state) { | |||
| 324 | return true; | 325 | return true; |
| 325 | } | 326 | } |
| 326 | 327 | ||
| 328 | static bool LoadTexture(State* state, const char* path) { | ||
| 329 | assert(state); | ||
| 330 | if (state->numTextures >= MaxTextures) { | ||
| 331 | fprintf(stderr, "Cannot load texture. Maximum number of textures loaded\n"); | ||
| 332 | return false; | ||
| 333 | } | ||
| 334 | // TODO: This indexing into the textures array assumes that we have loaded a | ||
| 335 | // single model. Generalize later. | ||
| 336 | sgImage* texture = &state->textures[state->numTextures++]; | ||
| 337 | int channels = 0; | ||
| 338 | constexpr int desired_channels = 4; | ||
| 339 | texture->pixels = (sgPixel*)stbi_load(path, &texture->width, &texture->height, &channels, desired_channels); | ||
| 340 | if (!texture->pixels) { | ||
| 341 | fprintf(stderr, "Failed to read texture: [%s]\n", path); | ||
| 342 | return false; | ||
| 343 | } | ||
| 344 | assert(channels == desired_channels); | ||
| 345 | // Gamma-correct for lighting. | ||
| 346 | sgGamma(state->gfx, texture->pixels, texture->width, texture->height); | ||
| 347 | return true; | ||
| 348 | } | ||
| 349 | |||
| 327 | static bool Initialize(State* state) { | 350 | static bool Initialize(State* state) { |
| 328 | assert(state); | 351 | assert(state); |
| 329 | 352 | ||
| @@ -359,16 +382,9 @@ static bool Initialize(State* state) { | |||
| 359 | const ModelMaterial* materials = modelMaterials(state->model); | 382 | const ModelMaterial* materials = modelMaterials(state->model); |
| 360 | for (size_t i = 0; i < state->model->numMaterials; ++i) { | 383 | for (size_t i = 0; i < state->model->numMaterials; ++i) { |
| 361 | const ModelMaterial* material = &materials[i]; | 384 | const ModelMaterial* material = &materials[i]; |
| 362 | // TODO: When doing lighting, need to gamma-correct here. | 385 | if (!LoadTexture(state, material->diffuseTexture)) { |
| 363 | sgImage* texture = &state->textures[state->numTextures++]; | ||
| 364 | int channels = 0; | ||
| 365 | constexpr int desired_channels = 4; | ||
| 366 | texture->pixels = (sgPixel*)stbi_load(material->diffuseTexture, &texture->width, &texture->height, &channels, desired_channels); | ||
| 367 | if (!texture->pixels) { | ||
| 368 | fprintf(stderr, "Failed to read texture: [%s]\n", material->diffuseTexture); | ||
| 369 | return false; | 386 | return false; |
| 370 | } | 387 | } |
| 371 | assert(channels == desired_channels); | ||
| 372 | } | 388 | } |
| 373 | 389 | ||
| 374 | Camera* camera = &state->camera; | 390 | Camera* camera = &state->camera; |
