From 76143f7ef23636cc27b9a0547bda8ea124506fb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Chatelain?= Date: Mon, 19 Oct 2020 07:30:47 +0000 Subject: [PATCH] Create materials for 'usemtl' declaration Problem: I read a bare OBJ so no MTL is provided. I still want to keep the materials setup across the scene for further edit. Fix: Instead of using a fallback material (idx 0), create a material for each not-defined material while keeping the name so that the structure of the model is conserved. --- fast_obj.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/fast_obj.h b/fast_obj.h index b29c439..b0db70f 100644 --- a/fast_obj.h +++ b/fast_obj.h @@ -820,12 +820,6 @@ const char* parse_usemtl(fastObjData* data, const char* ptr) e = ptr; - - /* If there are no materials yet, add a dummy invalid material at index 0 */ - if (array_empty(data->mesh->materials)) - array_push(data->mesh->materials, mtl_default()); - - /* Find an existing material with the same name */ idx = 0; while (idx < array_size(data->mesh->materials)) @@ -837,8 +831,14 @@ const char* parse_usemtl(fastObjData* data, const char* ptr) idx++; } + /* If doesn't exists, create a default one with this name + Note: this case happens when OBJ doesn't have its MTL */ if (idx == array_size(data->mesh->materials)) - idx = 0; + { + fastObjMaterial new_mtl = mtl_default(); + new_mtl.name = string_copy(s, e); + array_push(data->mesh->materials, new_mtl); + } data->material = idx;