From 9037820f3014bd39a1f18b79fb27cdc7fa4da179 Mon Sep 17 00:00:00 2001 From: Max Rigout <71095568+maxrigout@users.noreply.github.com> Date: Wed, 2 Aug 2023 16:59:18 -0400 Subject: [PATCH] improved color parsing --- fast_obj.h | 61 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/fast_obj.h b/fast_obj.h index b19dd18..f1e3695 100644 --- a/fast_obj.h +++ b/fast_obj.h @@ -206,6 +206,9 @@ typedef struct /* Final mesh */ fastObjMesh* mesh; + /* Position index goes to color index map */ + unsigned int* vertex_color_index; + /* Current object/group */ fastObjGroup object; fastObjGroup group; @@ -652,25 +655,27 @@ const char* parse_vertex(fastObjData* data, const char* ptr) unsigned int ii; float v; - for (ii = 0; ii < 3; ii++) { ptr = parse_float(ptr, &v); array_push(data->mesh->positions, v); } - // extract color data - for (ii = 0; ii < 3; ii++) +#ifdef FAST_OBJ_ENABLE_COLOURS + /* Default the vertex color to the default color index */ + array_push(data->vertex_color_index, 0); + ptr = skip_whitespace(ptr); + if (!is_newline(*ptr)) { - const char* tmp = parse_float(ptr, &v); - // if there's no data, we default to white - if (tmp == ptr) + /* We associate the current vertex position with the vertex color */ + data->vertex_color_index[array_size(data->vertex_color_index) - 1] = array_size(data->mesh->colors) / 3; + for (ii = 0; ii < 3; ++ii) { - v = 1.0f; + ptr = parse_float(ptr, &v); + array_push(data->mesh->colors, v); } - ptr = tmp; - array_push(data->mesh->colors, v); } +#endif return ptr; } @@ -762,7 +767,11 @@ const char* parse_face(fastObjData* data, const char* ptr) else vn.n = 0; - vn.c = vn.p; +#ifdef FAST_OBJ_ENABLE_COLOURS + vn.c = data->vertex_color_index[vn.p]; +#else + vn.c = 0; +#endif array_push(data->mesh->indices, vn); count++; @@ -1365,7 +1374,9 @@ void fast_obj_destroy(fastObjMesh* m) array_clean(m->positions); array_clean(m->texcoords); array_clean(m->normals); +#ifdef FAST_OBJ_ENABLE_COLOURS array_clean(m->colors); +#endif array_clean(m->face_vertices); array_clean(m->face_materials); array_clean(m->indices); @@ -1420,7 +1431,9 @@ fastObjMesh* fast_obj_read_with_callbacks(const char* path, const fastObjCallbac m->positions = 0; m->texcoords = 0; m->normals = 0; +#ifdef FAST_OBJ_ENABLE_COLOURS m->colors = 0; +#endif m->face_vertices = 0; m->face_materials = 0; m->indices = 0; @@ -1441,17 +1454,24 @@ fastObjMesh* fast_obj_read_with_callbacks(const char* path, const fastObjCallbac array_push(m->normals, 0.0f); array_push(m->normals, 1.0f); - array_push(m->colors, 0.0f); - array_push(m->colors, 0.0f); - array_push(m->colors, 0.0f); +#ifdef FAST_OBJ_ENABLE_COLOURS + /* Default white color */ + array_push(m->colors, 1.0f); + array_push(m->colors, 1.0f); + array_push(m->colors, 1.0f); +#endif /* Data needed during parsing */ - data.mesh = m; - data.object = object_default(); - data.group = group_default(); - data.material = 0; - data.line = 1; - data.base = 0; + data.mesh = m; + data.vertex_color_index = 0; + data.object = object_default(); + data.group = group_default(); + data.material = 0; + data.line = 1; + data.base = 0; + + /* Dummy position is assigned to the dummy color */ + array_push(data.vertex_color_index, 0); /* Find base path for materials/textures */ @@ -1531,7 +1551,9 @@ fastObjMesh* fast_obj_read_with_callbacks(const char* path, const fastObjCallbac m->position_count = array_size(m->positions) / 3; m->texcoord_count = array_size(m->texcoords) / 2; m->normal_count = array_size(m->normals) / 3; +#ifdef FAST_OBJ_ENABLE_COLOURS m->color_count = array_size(m->colors) / 3; +#endif m->face_count = array_size(m->face_vertices); m->index_count = array_size(m->indices); m->material_count = array_size(m->materials); @@ -1542,6 +1564,7 @@ fastObjMesh* fast_obj_read_with_callbacks(const char* path, const fastObjCallbac /* Clean up */ memory_dealloc(buffer); memory_dealloc(data.base); + array_clean(data.vertex_color_index); callbacks->file_close(file, user_data);