From d00d1029f9e2e9f55217ad3d21e5b826601ba210 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 6 Feb 2023 14:13:39 -0600 Subject: implement kai_read_mesh() --- src/geometry/geometry.c | 77 ++++++++++++++++++++++++++++++++++++++++++++ src/geometry/geometry.h | 3 ++ src/geometry/geometry.test.c | 58 +++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) (limited to 'src/geometry') diff --git a/src/geometry/geometry.c b/src/geometry/geometry.c index 78fbaea..72af92c 100644 --- a/src/geometry/geometry.c +++ b/src/geometry/geometry.c @@ -340,3 +340,80 @@ void kai_release_triangles(struct ka_triangles_t t) } +int kai_read_mesh(struct ka_mesh_t *dest, struct kai_tag_t *src) +{ + CHECK_TAG_TYPE("mesh"); + + dest->source_count = 0; + dest->source = NULL; + dest->triangles_count = 0; + dest->triangles = NULL; + + /* load vertices first bc if we don't kai_release_mesh will do bad things if we hit an error */ + struct kai_tag_t *child; + int result; + + child = kai_tag_get_first_child_with_type(src, "vertices"); + if (child == NULL) { return -1; } + result = kai_read_vertices(&(dest->vertices), child); + if (result != 0) { return -1; } + + /* load sources */ + unsigned int source_count = 0; + FOR_CHILD_OF_TYPE(src, child, "source") { source_count += 1; } + + dest->source = kai_alloc(source_count * sizeof(struct ka_source_t), "mesh source array"); + if (dest->source == NULL) { return -1; } + + int i = 0; + FOR_CHILD_OF_TYPE(src, child, "source") { + result = kai_read_source(dest->source + i, child); + if (result != 0) { + dest->source_count = i; + kai_release_mesh(*dest); + return -1; + } + i += 1; + } + dest->source_count = source_count; + + /* load triangles */ + unsigned int triangles_count = 0; + FOR_CHILD_OF_TYPE(src, child, "triangles") { triangles_count += 1; } + + dest->triangles = kai_alloc(triangles_count * sizeof(struct ka_triangles_t), "mesh triangles array"); + if (dest->triangles == NULL) { + kai_release_mesh(*dest); + return -1; + } + + i = 0; + FOR_CHILD_OF_TYPE(src, child, "triangles") { + result = kai_read_triangles(dest->triangles + i, child); + if (result != 0) { + dest->triangles_count = i; + kai_release_mesh(*dest); + return -1; + } + i += 1; + } + dest->triangles_count = triangles_count; + + return 0; +} + +void kai_release_mesh(struct ka_mesh_t m) +{ + int i; + for (i=0; i" + ); + + struct ka_mesh_t mesh; + int result = kai_read_mesh(&mesh, t); + CHECK_EQ(result, -1, "%d"); + + kai_tag_destroy(t); +} +#include LILY_PUSH_TEST() + + +LILY_TEST("read mesh tag") +{ + struct kai_tag_t *t = kai_parse_string( + "" + " " + " 0.0" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + "

" + " 0 0 1 3 2 1" + " 0 0 2 1 3 2" + "

" + "
" + "
" + ); + + struct ka_mesh_t mesh; + int result = kai_read_mesh(&mesh, t); + kai_tag_destroy(t); + + REQUIRE_EQ(result, 0, "%d"); + + CHECK_EQ(mesh.source_count, 1, "%d"); + CHECK_EQS(mesh.source[0].id, "source"); + CHECK_EQS(mesh.vertices.id, "box-Vtx"); + CHECK_EQ(mesh.triangles_count, 1, "%d"); + CHECK_EQ(mesh.triangles[0].count, 2, "%d"); + + kai_release_mesh(mesh); +} +#include LILY_PUSH_TEST() + + #define LILY_FILE_END #include LILY_REGISTER_TESTS() -- cgit v1.2.1