diff options
author | kdx <kikoodx@paranoici.org> | 2024-01-11 06:20:14 +0100 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2024-01-11 06:25:43 +0100 |
commit | 8e57c2b6fdf17977a2ffeecafb7f58d7cb37c1f3 (patch) | |
tree | 7578b0919266a4b8a526d1b74a4776d1e4a70769 | |
parent | 88690dfde997bd8d59320a3a553bdfda357b6969 (diff) | |
download | 008-8e57c2b6fdf17977a2ffeecafb7f58d7cb37c1f3.tar.gz |
mesh loading
-rw-r--r-- | inc/mesh.h | 3 | ||||
-rw-r--r-- | res/cube.mesh | 17 | ||||
-rw-r--r-- | res/spike.mesh | 12 | ||||
-rw-r--r-- | src/main.c | 113 | ||||
-rw-r--r-- | src/mesh.c | 78 |
5 files changed, 115 insertions, 108 deletions
@@ -14,8 +14,9 @@ typedef struct { } Mesh; Mesh *mesh_new(MeshType); +Mesh *mesh_load(const char *path); void mesh_destroy(Mesh *this); void mesh_push_vertex(Mesh *this, Point point); void mesh_push_index(Mesh *this, u8 index); void mesh_log(Mesh *this); -void mesh_draw(Mesh *this, Mat4 transform, Mat4 projection); +void mesh_draw(Mesh *this, Mat4 transform); diff --git a/res/cube.mesh b/res/cube.mesh new file mode 100644 index 0000000..d9819cf --- /dev/null +++ b/res/cube.mesh @@ -0,0 +1,17 @@ +QUADS + +-.5 -.5 -.5 +-.5 .5 -.5 + .5 .5 -.5 + .5 -.5 -.5 +-.5 -.5 .5 +-.5 .5 .5 + .5 .5 .5 + .5 -.5 .5 + +0 1 2 3 +4 5 6 7 +0 3 7 4 +1 2 6 5 +0 1 5 4 +2 3 7 6 diff --git a/res/spike.mesh b/res/spike.mesh new file mode 100644 index 0000000..3390bda --- /dev/null +++ b/res/spike.mesh @@ -0,0 +1,12 @@ +TRIANGLES + + .0 .0 .0 +-.5 .5 -.5 + .5 .5 -.5 +-.5 .5 .5 + .5 .5 .5 + +0 1 2 +0 3 4 +0 1 3 +0 2 4 @@ -1,19 +1,21 @@ #include <time.h> #include <cglm/cam.h> -static void _build_cube(void); -static void _build_spike(void); static int _main_loop(void *udata); Mesh *g_cube; Mesh *g_spike; +Mat4 g_projection; int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) { srand(time(nullptr)); - _build_cube(); - _build_spike(); + mat4 proj = GLM_MAT4_IDENTITY_INIT; + glm_perspective(45.0f, 1.0f, 0.1f, 100.0f, proj); + g_projection = mat4_glm(proj); + g_cube = mesh_load("res/cube.mesh"); + g_spike = mesh_load("res/spike.mesh"); defer(wdeinit); assert(TZR_Init(.width=cfg.display_width, .height=cfg.display_height, @@ -31,21 +33,11 @@ static int _main_loop([[maybe_unused]] void *udata) { static f64 x = 0, y = 0; - static f64 rx = 0, ry = 0, rz = 0; - static f64 s = 1; x -= TZR_IsKeyDown(SDL_SCANCODE_A) * 0.1; x += TZR_IsKeyDown(SDL_SCANCODE_D) * 0.1; y -= TZR_IsKeyDown(SDL_SCANCODE_W) * 0.1; y += TZR_IsKeyDown(SDL_SCANCODE_S) * 0.1; - rx -= TZR_IsKeyDown(SDL_SCANCODE_U) * 0.01; - rx += TZR_IsKeyDown(SDL_SCANCODE_J) * 0.01; - ry -= TZR_IsKeyDown(SDL_SCANCODE_I) * 0.01; - ry += TZR_IsKeyDown(SDL_SCANCODE_K) * 0.01; - rz -= TZR_IsKeyDown(SDL_SCANCODE_O) * 0.01; - rz += TZR_IsKeyDown(SDL_SCANCODE_L) * 0.01; - s -= TZR_IsKeyDown(SDL_SCANCODE_R) * 0.01; - s += TZR_IsKeyDown(SDL_SCANCODE_F) * 0.01; assert(TZR_DrawBegin() == 0); @@ -53,14 +45,6 @@ _main_loop([[maybe_unused]] void *udata) TZR_DrawClear(); TZR_BlendMode(SDL_BLENDMODE_ADD); - Point direction = point(0.0, 0.0, 1.0); - direction = point_transform(direction, mat4_rotating(rx, ry, rz)); - - mat4 projection = GLM_MAT4_IDENTITY_INIT; - glm_perspective(45.0f, 1.0f, 0.1f, 100.0f, projection); - Mat4 proj = mat4_identity(); - proj = mat4_dot(proj, mat4_glm(projection)); - Mat4 m = mat4_identity(); m = mat4_dot(m, mat4_scaling(0.9, 0.9, 48)); m = mat4_dot(m, mat4_translating(-7.5 - x, -7.5 - y, -256)); @@ -76,97 +60,16 @@ _main_loop([[maybe_unused]] void *udata) repeat (i, 3) { Mat4 lm = mat4_dot(m_spike, mat4_translating(x, y, i * 16 - 16)); - mesh_draw(g_spike, lm, proj); + mesh_draw(g_spike, lm); } } if (x != 0 && x != 15 && y != 0 && y != 15) continue; Mat4 lm = mat4_dot(m, mat4_translating(x, y, 0)); - mesh_draw(g_cube, lm, proj); + mesh_draw(g_cube, lm); } } assert(TZR_DrawEnd() == 0); return 0; } - -static void -_build_cube(void) -{ - g_cube = mesh_new(MESH_QUADS); - mesh_push_vertex(g_cube, point(-.5, -.5, -.5)); - mesh_push_vertex(g_cube, point(-.5, +.5, -.5)); - mesh_push_vertex(g_cube, point(+.5, +.5, -.5)); - mesh_push_vertex(g_cube, point(+.5, -.5, -.5)); - mesh_push_vertex(g_cube, point(-.5, -.5, +.5)); - mesh_push_vertex(g_cube, point(-.5, +.5, +.5)); - mesh_push_vertex(g_cube, point(+.5, +.5, +.5)); - mesh_push_vertex(g_cube, point(+.5, -.5, +.5)); - - // front - mesh_push_index(g_cube, 0); - mesh_push_index(g_cube, 1); - mesh_push_index(g_cube, 2); - mesh_push_index(g_cube, 3); - - // back - mesh_push_index(g_cube, 4); - mesh_push_index(g_cube, 5); - mesh_push_index(g_cube, 6); - mesh_push_index(g_cube, 7); - - // top - mesh_push_index(g_cube, 0); - mesh_push_index(g_cube, 3); - mesh_push_index(g_cube, 7); - mesh_push_index(g_cube, 4); - - // bottom - mesh_push_index(g_cube, 1); - mesh_push_index(g_cube, 2); - mesh_push_index(g_cube, 6); - mesh_push_index(g_cube, 5); - - // left - mesh_push_index(g_cube, 0); - mesh_push_index(g_cube, 1); - mesh_push_index(g_cube, 5); - mesh_push_index(g_cube, 4); - - // right - mesh_push_index(g_cube, 2); - mesh_push_index(g_cube, 3); - mesh_push_index(g_cube, 7); - mesh_push_index(g_cube, 6); -} - -static void -_build_spike(void) -{ - g_spike = mesh_new(MESH_TRIANGLES); - mesh_push_vertex(g_spike, point( .0, .0, .0)); - mesh_push_vertex(g_spike, point(-.5, +.5, -.5)); - mesh_push_vertex(g_spike, point(+.5, +.5, -.5)); - mesh_push_vertex(g_spike, point(-.5, +.5, +.5)); - mesh_push_vertex(g_spike, point(+.5, +.5, +.5)); - - // front - mesh_push_index(g_spike, 0); - mesh_push_index(g_spike, 1); - mesh_push_index(g_spike, 2); - - // back - mesh_push_index(g_spike, 0); - mesh_push_index(g_spike, 3); - mesh_push_index(g_spike, 4); - - // left - mesh_push_index(g_spike, 0); - mesh_push_index(g_spike, 1); - mesh_push_index(g_spike, 3); - - // right - mesh_push_index(g_spike, 0); - mesh_push_index(g_spike, 2); - mesh_push_index(g_spike, 4); -} @@ -11,6 +11,76 @@ mesh_new(MeshType type) return this; } +Mesh * +mesh_load(const char *path) +{ + FILE *fp = fopen(path, "rb"); + assert(fp != nullptr); + + char line[256]; + assert(fgets(line, sizeof(line) - 1, fp) != nullptr); + const size_t line_len = strlen(line); + if (line[line_len - 1] == '\n') + line[line_len - 1] = '\0'; + + size_t tupsize; + MeshType type; + if (strcasecmp(line, "LINES") == 0) { + tupsize = 2; + type = MESH_LINES; + } else if (strcasecmp(line, "TRIANGLES") == 0) { + tupsize = 3; + type = MESH_TRIANGLES; + } else if (strcasecmp(line, "QUADS") == 0) { + tupsize = 4; + type = MESH_QUADS; + } else { + panic("unknown mesh type '%s'", line); + } + + Mesh *this = mesh_new(type); + + // skip empty line + assert(fgets(line, sizeof(line) - 1, fp) != nullptr); + assert(line[0] == '\n' && line[1] == '\0'); + + static const char *delim = " \t\n\r\v"; + for (;;) { + assert(fgets(line, sizeof(line) - 1, fp) != nullptr); + if (line[0] == '\n' && line[1] == '\0') + break; + + Point vertex; + size_t i = 0; + auto tok = strtok(line, delim); + while (tok != nullptr) { + assert(i < 3); + vertex.a[i] = atof(tok); + tok = strtok(nullptr, delim); + i += 1; + } + assert(i == 3); + mesh_push_vertex(this, vertex); + } + + while (fgets(line, sizeof(line) - 1, fp) != nullptr) { + size_t i = 0; + auto tok = strtok(line, delim); + while (tok != nullptr) { + assert(i < tupsize); + const size_t idx = atoi(tok); + assert(idx <= this->vertices->size); + assert(idx < 256); + mesh_push_index(this, idx); + tok = strtok(nullptr, delim); + i += 1; + } + assert(i == tupsize); + } + + return this; +} + void mesh_destroy(Mesh *this) { @@ -111,15 +181,19 @@ randf(void) } void -mesh_draw(Mesh *this, Mat4 transform, Mat4 projection) +mesh_draw(Mesh *this, Mat4 transform) { + assert(this != nullptr); + + extern Mat4 g_projection; + VecPoint_reserve(this->transformed, this->vertices->size); this->transformed->size = this->vertices->size; repeat (i, this->vertices->size) { auto vertex = this->vertices->p[i]; vertex = point_transform(vertex, transform); - vertex = point_transform(vertex, projection); + vertex = point_transform(vertex, g_projection); vertex = point_screenspace(vertex); vertex = point_add(vertex,point(randf() * 1.2, randf() * 1.2, |