loader_aw

Load OBJ/MTL file formats
git clone git://git.meso-star.fr/loader_aw.git
Log | Files | Refs | README | LICENSE

commit 54588b198b8a19b3674271190485496726b72bc7
parent bbcb90efbee800e2a97233252642a2f88585db06
Author: vaplv <vaplv@free.fr>
Date:   Sun, 20 Jul 2014 14:49:08 +0200

Push further the tests on mtl loading

Diffstat:
Msrc/aw_mtl.c | 24+++++++++++++-----------
Msrc/test_aw_mtl.c | 153+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 166 insertions(+), 11 deletions(-)

diff --git a/src/aw_mtl.c b/src/aw_mtl.c @@ -245,7 +245,7 @@ parse_color(struct aw_color* col, char** word_tk) enum aw_result res = AW_OK; ASSERT(col && word_tk); - word = strtok_r(NULL, " ", word_tk); + word = strtok_r(NULL, " \t", word_tk); if(!word) return AW_BAD_ARGUMENT; @@ -256,7 +256,7 @@ parse_color(struct aw_color* col, char** word_tk) col->color_space = strcmp(word, "xyz") == 0 ? AW_COLOR_XYZ : AW_COLOR_RGB; if(col->color_space == AW_COLOR_XYZ - && !(word = strtok_r(NULL, " ", word_tk))) + && !(word = strtok_r(NULL, " \t", word_tk))) return AW_BAD_ARGUMENT; if(AW_OK != (res = string_to_float(word, &col->value[0]))) @@ -264,13 +264,13 @@ parse_color(struct aw_color* col, char** word_tk) /* If only the first component is defined the second and third components are * assumed to be equal to the first one */ - word = strtok_r(NULL, " ", word_tk); + word = strtok_r(NULL, " \t", word_tk); if(!word) { col->value[1] = col->value[2] = col->value[0]; } else { if(AW_OK != (res = string_to_float(word, &col->value[1]))) return res; - word = strtok_r(NULL, " ", word_tk); + word = strtok_r(NULL, " \t", word_tk); if(AW_OK != (res = string_to_float(word, &col->value[2]))) return res; } @@ -301,7 +301,7 @@ parse_bool_option { char* word; ASSERT(options_mask && tk_ctxt); - word = strtok_r(NULL, " ", tk_ctxt); + word = strtok_r(NULL, " \t", tk_ctxt); if(!word) return AW_BAD_ARGUMENT; if(strcmp(word, "on") == 0) { @@ -317,7 +317,7 @@ parse_imfchan_option(enum aw_map_channel* channel, char** tk_ctxt) { char* word; ASSERT(channel && tk_ctxt); - word = strtok_r(NULL, " ", tk_ctxt); + word = strtok_r(NULL, " \t", tk_ctxt); if(!word) return AW_BAD_ARGUMENT; if(!strcmp(word, "r")) { @@ -347,7 +347,7 @@ parse_map char* word; ASSERT(map && tk_ctxt); - word = strtok_r(NULL, " ", tk_ctxt); + word = strtok_r(NULL, " \t", tk_ctxt); while(word) { enum aw_result res; @@ -390,7 +390,7 @@ parse_map } if(res != AW_OK) return res; - word = strtok_r(NULL, " ", tk_ctxt); + word = strtok_r(NULL, " \t", tk_ctxt); } return AW_OK; } @@ -407,7 +407,7 @@ parse_mtl_file(struct aw_mtl* mtl, const char* path, char* content) iline = 1; while(line) { char* word, *word_tk; - word = strtok_r(line, " ", &word_tk); + word = strtok_r(line, " \t", &word_tk); if(word) { if(!strcmp(word, "newmtl")) { /* Material name declaration */ res = parse_newmtl(mtl, &word_tk); @@ -444,7 +444,7 @@ parse_mtl_file(struct aw_mtl* mtl, const char* path, char* content) res = AW_OK; fprintf(stderr, "%s:%lu: warning: ignored or malformed directive %s\n", path, iline, word); - while((word = strtok_r(NULL, " ", &word_tk))); + while((word = strtok_r(NULL, " \t", &word_tk))); } if(res != AW_OK) goto error; @@ -548,11 +548,11 @@ aw_mtl_load(struct aw_mtl* mtl, const char* filename) res = AW_BAD_ARGUMENT; goto error; } + mtl_clear(mtl); if(AW_OK != (res = read_file(mtl->allocator, filename, &file_content))) goto error; - mtl_clear(mtl); if(AW_OK != (res = parse_mtl_file(mtl, filename, file_content))) goto error; @@ -561,6 +561,8 @@ exit: MEM_FREE(mtl->allocator, file_content); return res; error: + if(mtl) + mtl_clear(mtl); goto exit; } diff --git a/src/test_aw_mtl.c b/src/test_aw_mtl.c @@ -129,6 +129,156 @@ test_common(struct aw_mtl* mtl) CHECK(mtr->bump_map.bump_multiplier, 1.f); } +static void +test_multiple_materials(struct aw_mtl* mtl) +{ + const char* mtl_multi = + "newmtl material_0\n" + "\tNs 8.0\n" + "\tNi 1.5\n" + "\td 1.0\n" + "\tTf 1.0 1 1\n" + "\tillum 2\n" + "\tKa 0 0 0\n" + "\tKd 0.734118\t0.730588 0.674118\n" + "\tKs 0 0 0\n" + "\tmap_Ka my_long_and_verbose_filename_of_a_RED_GREEN_BLUE_1024x64_image.png\n" + "\tmap_Kd tp.png\n" + "\n" + "newmtl textured_material\n" + "Ns 6\n" + "Ni 1.70\n" + "d 1\n" + "Tf 1 1.2 1.3 \n" + "illum 0\n" + "Ka\t\t 0 0 0\n" + "Kd \t 0.734118 0.709412 0.674118\n" + "Ks 0 0 0\n" + "map_Ka\ttex6x6.png\n" + "map_Kd tex6x6.png\n" + "bump -imfchan r -bm 0.2 tex6x6-bump.png\n" + "\n" + "newmtl hello_world\n" + "Ns 8\n" + "Ni 1.5\n" + "d 1\n" + "Tr 0\n" + "Tf 1 1 1\n" + "illum 2\n" + "Ka\t0 \t0\t0000\n" + "Kd 0.546274 0.219608 0.183922\n" + "Ks 2\n"; + FILE* file; + size_t nmtls; + float tmp[3]; + const struct aw_material* mtr; + + NCHECK(mtl, NULL); + + file = fopen("test_mtl_multi.mtl", "w"); + NCHECK(file, NULL); + fwrite(mtl_multi, sizeof(char), strlen(mtl_multi), file); + fclose(file); + + CHECK(aw_mtl_load(mtl, "test_mtl_multi.mtl"), AW_OK); + CHECK(aw_mtl_materials_count_get(mtl, &nmtls), AW_OK); + CHECK(nmtls, 3); + + CHECK(aw_mtl_material_get(mtl, 0, &mtr), AW_OK); + CHECK(strcmp(mtr->name, "material_0"), 0); + CHECK(mtr->specular_exponent, 8.f); + CHECK(mtr->refraction_index, 1.5f); + CHECK(mtr->transmission.color_space, AW_COLOR_RGB); + CHECK(f3_eq(mtr->transmission.value, f3_splat(tmp, 1.f)), 1); + CHECK(mtr->illumination_model, 2); + CHECK(mtr->ambient.color_space, AW_COLOR_RGB); + CHECK(f3_eq(mtr->ambient.value, f3_splat(tmp, 0.f)), 1); + CHECK(mtr->diffuse.color_space, AW_COLOR_RGB); + CHECK(f3_eq(mtr->diffuse.value, f3(tmp, 0.734118f, 0.730588f, 0.674118f)), 1); + CHECK(mtr->specular.color_space, AW_COLOR_RGB); + CHECK(f3_eq(mtr->specular.value, f3_splat(tmp, 0.f)), 1); + CHECK(strcmp + (mtr->ambient_map.filename, + "my_long_and_verbose_filename_of_a_RED_GREEN_BLUE_1024x64_image.png"), 0); + CHECK(mtr->ambient_map.options_mask, 0); + CHECK(mtr->ambient_map.image_bias, 0.f); + CHECK(mtr->ambient_map.image_scale, 1.f); + CHECK(f3_eq(mtr->ambient_map.texcoord_bias, f3_splat(tmp, 0.f)), 1); + CHECK(f3_eq(mtr->ambient_map.texcoord_scale, f3_splat(tmp, 1.f)), 1); + CHECK(f3_eq(mtr->ambient_map.texcoord_turbulence, f3_splat(tmp, 0.f)), 1); + CHECK(mtr->ambient_map.resolution, 0); + CHECK(strcmp(mtr->diffuse_map.filename, "tp.png"), 0); + CHECK(mtr->specular_map.filename, NULL); + CHECK(mtr->specular_exponent_map.filename, NULL); + CHECK(mtr->bump_map.filename, NULL); + + CHECK(aw_mtl_material_get(mtl, 1, &mtr), AW_OK); + CHECK(strcmp(mtr->name, "textured_material"), 0); + CHECK(mtr->specular_exponent, 6.f); + CHECK(mtr->refraction_index, 1.7f); + CHECK(mtr->transmission.color_space, AW_COLOR_RGB); + CHECK(f3_eq(mtr->transmission.value, f3(tmp, 1.f, 1.2f, 1.3f)), 1); + CHECK(mtr->illumination_model, 0); + CHECK(mtr->ambient.color_space, AW_COLOR_RGB); + CHECK(f3_eq(mtr->ambient.value, f3_splat(tmp, 0.f)), 1); + CHECK(mtr->diffuse.color_space, AW_COLOR_RGB); + CHECK(f3_eq(mtr->diffuse.value, f3(tmp, 0.734118f, 0.709412f, 0.674118f)), 1); + CHECK(mtr->specular.color_space, AW_COLOR_RGB); + CHECK(f3_eq(mtr->specular.value, f3_splat(tmp, 0.f)), 1); + CHECK(strcmp(mtr->ambient_map.filename, "tex6x6.png"), 0); + CHECK(strcmp(mtr->diffuse_map.filename, "tex6x6.png"), 0); + CHECK(strcmp(mtr->bump_map.filename, "tex6x6-bump.png"), 0); + CHECK(mtr->bump_map.scalar, AW_MAP_CHANNEL_RED); + CHECK(mtr->bump_map.bump_multiplier, 0.2f); + CHECK(mtr->specular_exponent_map.filename, NULL); + + CHECK(aw_mtl_material_get(mtl, 2, &mtr), AW_OK); + CHECK(strcmp(mtr->name, "hello_world"), 0); + CHECK(mtr->specular_exponent, 8.f); + CHECK(mtr->refraction_index, 1.5f); + CHECK(mtr->transmission.color_space, AW_COLOR_RGB); + CHECK(f3_eq(mtr->transmission.value, f3_splat(tmp, 1.f)), 1); + CHECK(mtr->illumination_model, 2); + CHECK(mtr->ambient.color_space, AW_COLOR_RGB); + CHECK(f3_eq(mtr->ambient.value, f3_splat(tmp, 0.f)), 1); + CHECK(mtr->diffuse.color_space, AW_COLOR_RGB); + CHECK(f3_eq(mtr->diffuse.value, f3(tmp, 0.546274f, 0.219608f, 0.183922f)), 1); + CHECK(mtr->specular.color_space, AW_COLOR_RGB); + CHECK(f3_eq(mtr->specular.value, f3_splat(tmp, 2.f)), 1); + CHECK(mtr->ambient_map.filename, NULL); + CHECK(mtr->diffuse_map.filename, NULL); + CHECK(mtr->bump_map.filename, NULL); + CHECK(mtr->specular_exponent_map.filename, NULL); +} + +static void +test_unloadable(struct aw_mtl* mtl) +{ + const char* mtl0 = + "newmtl material_0\n" + "\tNs 8.0\n" + "\tNii 1.5\n" + "\td 1.0\n" + "\tTf 1.0 1 1\n" + "\tillum 2\n" + "\tKa 0 0 0\n" + "\tmap_Kambient hop.png\n" + "\tKd\n" + "\tKs 0 0 0\n"; + FILE* file; + size_t nmtls; + + NCHECK(mtl, NULL); + + file = fopen("mtl0.mtl", "w"); + NCHECK(file, NULL); + fwrite(mtl0, sizeof(char), strlen(mtl0), file); + fclose(file); + + CHECK(aw_mtl_load(mtl, "mtl0.mtl"), AW_BAD_ARGUMENT); + CHECK(aw_mtl_materials_count_get(mtl, &nmtls), AW_OK); + CHECK(nmtls, 0); +} int main(int argc, char** argv) { @@ -151,6 +301,8 @@ main(int argc, char** argv) CHECK(aw_mtl_create(&allocator, &mtl), AW_OK); test_common(mtl); + test_multiple_materials(mtl); + test_unloadable(mtl); CHECK(aw_mtl_ref_put(mtl), AW_OK); @@ -164,3 +316,4 @@ main(int argc, char** argv) CHECK(mem_allocated_size(), 0); return 0; } +