loader_aw

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

commit 487f04b14be123d9232e20245b183e8973901f74
parent 7258e67ab614621883bdd98b4bd9a562460fc5d1
Author: vaplv <vaplv@free.fr>
Date:   Sun, 29 Jun 2014 23:30:38 +0200

Push further the obj loader tests

Diffstat:
Msrc/obj.c | 40++++++++++++++++++++--------------------
Msrc/test_obj.c | 40+++++++++++++++++++++++++++++++++++-----
2 files changed, 55 insertions(+), 25 deletions(-)

diff --git a/src/obj.c b/src/obj.c @@ -230,28 +230,23 @@ parse_face(struct obj* obj, char** word_tk) face.nvertices = 0; while((word = strtok_r(NULL, " ", word_tk))) { char* id, *id_tk; - struct vertex vertex; + struct vertex vert; /* position index */ id = strtok_r(word, "/", &id_tk); - res = string_to_size_t(id, &vertex.iposition); + res = string_to_size_t(id, &vert.iposition); if(res != OBJ_OK) goto error; /* texcoord index */ - id = strtok_r(NULL, "/", &id_tk); - if(id) { - if(*id != '\0') { - res = string_to_size_t(id, &vertex.itexcoord); - if(res != OBJ_OK) + if((id = strtok_r(NULL, "/", &id_tk))) { + if(*id != '\0' && OBJ_OK != (res = string_to_size_t(id, &vert.itexcoord))) + goto error; + /* normal index */ + if((id = strtok_r(NULL, "/", &id_tk))) { + if(OBJ_OK != (res = string_to_size_t(id, &vert.inormal))) goto error; } - /* normal index */ - id = strtok_r(NULL, "/", &id_tk); - res = string_to_size_t(id, &vertex.inormal); - if(res != OBJ_OK) - goto error; } - - if(darray_vertex_push_back(&obj->vertices, &vertex)) { + if(darray_vertex_push_back(&obj->vertices, &vert)) { res = OBJ_MEMORY_ERROR; goto error; } @@ -319,11 +314,16 @@ parse_smooth_group(struct obj* obj, char** word_tk) flush_smooth_group(&obj->smooth_groups, &obj->faces); word = strtok_r(NULL, " ", word_tk); - res = string_to_size_t(word, &i); - if(res != OBJ_OK) - return res; - - grp.is_enabled = i != 0; + if(!strcmp(word, "off")) { + grp.is_enabled = 0; + } else if(!strcmp(word, "on")) { + grp.is_enabled = 1; + } else { + res = string_to_size_t(word, &i); + if(res != OBJ_OK) + return res; + grp.is_enabled = i != 0; + } grp.iface = darray_face_size_get(&obj->faces); grp.nfaces = 0; @@ -430,7 +430,7 @@ parse_file(struct obj* obj, const char* path, char* content) } else if(!strcmp(word, "vn")) { /* Vertex normal */ res = parse_floatX(&word_tk, 3, 3, 0.f, &obj->normals); } else if(!strcmp(word, "vt")) { /* Vertex texture coordinates */ - res = parse_floatX(&word_tk, 2, 3, 0.f, &obj->texcoords); + res = parse_floatX(&word_tk, 1, 3, 0.f, &obj->texcoords); } else if(!strcmp(word, "f") || !strcmp(word, "fo")) { /* face element */ res = parse_face(obj, &word_tk); } else if(!strcmp(word, "g")) { /* Grouping */ diff --git a/src/test_obj.c b/src/test_obj.c @@ -19,6 +19,30 @@ static const char* plane_obj = "f 1/1 2/2 3/3 4/4\n" "# 1 element\n"; + +static const char* squares_obj = + "v 0.000000 2.000000 0.000000\n" + "v 0.000000 0.000000 0.000000\n" + "v 2.000000 0.000000 0.000000\n" + "v 2.000000 2.000000 0.000000\n" + "v 4.000000 0.000000 -1.255298\n" + "v 4.000000 2.000000 -1.255298\n" + "vn 0.000000 0.000000 1.000000\n" + "vn 0.000000 0.000000 1.000000\n" + "vn 0.276597 0.000000 0.960986\n" + "vn 0.276597 0.000000 0.960986\n" + "vn 0.531611 0.000000 0.846988\n" + "vn 0.531611 0.000000 0.846988\n" + "# 6 vertices\n" + "\n" + "# 6 normals\n" + "\n" + "g all\n" + "s 1\n" + "f 1//1 2//2 3//3 4//4\n" + "f 4//4 3//3 5//5 6//6\n" + "# 2 elements\n"; + static const char* cube_obj = "# Cube with a different material applied to each of its faces\n" "mtllib master.mtl\n" @@ -59,14 +83,19 @@ main(int argc, char** argv) FILE* file; (void)argc, (void)argv; - file = fopen("test_obj_cube.obj", "w"); + file = fopen("test_obj_plane.obj", "w"); NCHECK(file, NULL); - fwrite(cube_obj, sizeof(char), strlen(cube_obj), file); + fwrite(plane_obj, sizeof(char), strlen(plane_obj), file); fclose(file); - file = fopen("test_obj_plane.obj", "w"); + file = fopen("test_obj_squares.obj", "w"); NCHECK(file, NULL); - fwrite(plane_obj, sizeof(char), strlen(plane_obj), file); + fwrite(squares_obj, sizeof(char), strlen(squares_obj), file); + fclose(file); + + file = fopen("test_obj_cube.obj", "w"); + NCHECK(file, NULL); + fwrite(cube_obj, sizeof(char), strlen(cube_obj), file); fclose(file); mem_init_proxy_allocator(&allocator, &mem_default_allocator); @@ -87,8 +116,9 @@ main(int argc, char** argv) CHECK(obj_load(obj, NULL), OBJ_BAD_ARGUMENT); CHECK(obj_load(NULL, "test_obj_cube.obj"), OBJ_BAD_ARGUMENT); CHECK(obj_load(obj, "none.obj"), OBJ_IO_ERROR); - CHECK(obj_load(obj, "test_obj_cube.obj"), OBJ_OK); CHECK(obj_load(obj, "test_obj_plane.obj"), OBJ_OK); + CHECK(obj_load(obj, "test_obj_squares.obj"), OBJ_OK); + CHECK(obj_load(obj, "test_obj_cube.obj"), OBJ_OK); CHECK(obj_ref_put(obj), OBJ_OK);