summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c41
1 files changed, 35 insertions, 6 deletions
diff --git a/src/main.c b/src/main.c
index e0b54c1..1f900bc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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>
@@ -25,7 +27,9 @@ static constexpr int WindowWidth = 640;
25static constexpr int WindowHeight = 480; 27static constexpr int WindowHeight = 480;
26static constexpr sgVec2i WindowDims = (sgVec2i){.x = WindowWidth, .y = WindowHeight}; 28static constexpr sgVec2i WindowDims = (sgVec2i){.x = WindowWidth, .y = WindowHeight};
27 29
28static const R Fovy = (R)(90 * TO_RAD); 30static constexpr R Fovy = (R)(90 * TO_RAD);
31static constexpr R Near = 0.1f;
32static constexpr R Far = 1000.0f;
29 33
30#define DEBUG_EVENT_LOOP 1 34#define DEBUG_EVENT_LOOP 1
31 35
@@ -65,6 +69,7 @@ typedef struct State {
65 void* gfx_mem; 69 void* gfx_mem;
66 swgfx* gfx; 70 swgfx* gfx;
67 Model* model; 71 Model* model;
72 sgTexture_t texture;
68 Camera camera; 73 Camera camera;
69 CameraController camera_controller; 74 CameraController camera_controller;
70 Uint64 last_tick; 75 Uint64 last_tick;
@@ -146,7 +151,8 @@ static void RenderIndexedModel(swgfx* gfx, const IndexedModel* model) {
146 assert(model); 151 assert(model);
147 const sgTriIdx* tris = (const sgTriIdx*)(model->data + model->offsetTris); 152 const sgTriIdx* tris = (const sgTriIdx*)(model->data + model->offsetTris);
148 const sgVec3* positions = (const sgVec3*) (model->data + model->offsetPositions); 153 const sgVec3* positions = (const sgVec3*) (model->data + model->offsetPositions);
149 sgTrianglesIndexedNonUniform(gfx, model->numTris, tris, positions); 154 const sgVec2* texcoords = (const sgVec2*)(model->data + model->offsetTexcoords);
155 sgTrianglesIndexedNonUniform(gfx, model->numTris, tris, positions, texcoords);
150} 156}
151 157
152static void RenderModel(swgfx* gfx, const Model* model) { 158static void RenderModel(swgfx* gfx, const Model* model) {
@@ -164,7 +170,11 @@ static void RenderTriangle2d(swgfx* gfx) {
164 const sgVec2 p0 = (sgVec2){20, 20}; 170 const sgVec2 p0 = (sgVec2){20, 20};
165 const sgVec2 p1 = (sgVec2){80, 20}; 171 const sgVec2 p1 = (sgVec2){80, 20};
166 const sgVec2 p2 = (sgVec2){50, 50}; 172 const sgVec2 p2 = (sgVec2){50, 50};
167 const sgTri2 tri = (sgTri2){p0, p1, p2}; 173 const sgVec2 uv = (sgVec2){0,0}; // Not used.
174 const sgTri2 tri = (sgTri2){
175 (sgVert2){p0, uv},
176 (sgVert2){p1, uv},
177 (sgVert2){p2, uv}};
168 sgTriangles2(gfx, 1, &tri); 178 sgTriangles2(gfx, 1, &tri);
169} 179}
170 180
@@ -215,6 +225,7 @@ static bool Render(State* state) {
215 sgModelId(state->gfx); 225 sgModelId(state->gfx);
216 sgView(state->gfx, SgVec3FromMathVec3(cam->spatial.p), SgVec3FromMathVec3(cam->spatial.f)); 226 sgView(state->gfx, SgVec3FromMathVec3(cam->spatial.p), SgVec3FromMathVec3(cam->spatial.f));
217 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);
218 RenderModel(state->gfx, state->model); 229 RenderModel(state->gfx, state->model);
219 /*sgIdx indices[3] = {0, 1, 2}; 230 /*sgIdx indices[3] = {0, 1, 2};
220 sgVec3 positions[3] = { 231 sgVec3 positions[3] = {
@@ -273,17 +284,30 @@ static bool Initialize(State* state) {
273 return false; 284 return false;
274 } 285 }
275 286
276 const char* model_path = "/home/jeanne/blender/box.mdl"; 287 const char* model_path = "/home/jeanne/blender/box_textured.mdl";
277 if (!(state->model = read_file(model_path))) { 288 if (!(state->model = read_file(model_path))) {
278 fprintf(stderr, "Failed to load model: [%s]\n", model_path); 289 fprintf(stderr, "Failed to load model: [%s]\n", model_path);
279 return false; 290 return false;
280 } 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 }
281 305
282 Camera* camera = &state->camera; 306 Camera* camera = &state->camera;
283 camera->fovy = Fovy; 307 camera->fovy = Fovy;
284 camera->aspect = Aspect; 308 camera->aspect = Aspect;
285 camera->near = 0.1f; 309 camera->near = Near;
286 camera->far = 1000.f; 310 camera->far = Far;
287 camera->spatial = spatial3_make(); 311 camera->spatial = spatial3_make();
288 camera->spatial.p = vec3_make(0, 1, 10); 312 camera->spatial.p = vec3_make(0, 1, 10);
289 313
@@ -300,6 +324,11 @@ static bool Initialize(State* state) {
300static void Shutdown(State* state) { 324static void Shutdown(State* state) {
301 assert(state); 325 assert(state);
302 326
327 if (state->texture.pixels) {
328 free(state->texture.pixels);
329 state->texture = (sgTexture_t){0};
330 }
331
303 if (state->model) { 332 if (state->model) {
304 free(state->model); 333 free(state->model);
305 state->model = nullptr; 334 state->model = nullptr;