diff options
Diffstat (limited to 'src/geometry/geometry.c')
-rw-r--r-- | src/geometry/geometry.c | 77 |
1 files changed, 77 insertions, 0 deletions
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<m.source_count; i++) { + kai_release_source(m.source[i]); + } + free(m.source); + + kai_release_vertices(m.vertices); + + for (i=0; i<m.triangles_count; i++) { + kai_release_triangles(m.triangles[i]); + } + free(m.triangles); +} |