diff --git a/test.cc b/test.cc index 82b6178..1eeec7a 100644 --- a/test.cc +++ b/test.cc @@ -7,16 +7,18 @@ #include #include -static void PrintInfo(const std::vector& shapes) +static void PrintInfo(const std::vector& shapes, const std::vector& materials) { - std::cout << "# of shapes : " << shapes.size() << std::endl; + std::cout << "# of shapes : " << shapes.size() << std::endl; + std::cout << "# of materials : " << materials.size() << std::endl; for (size_t i = 0; i < shapes.size(); i++) { printf("shape[%ld].name = %s\n", i, shapes[i].name.c_str()); printf("shape[%ld].indices: %ld\n", i, shapes[i].mesh.indices.size()); + printf("shape[%ld].material_ids: %ld\n", i, shapes[i].mesh.material_ids.size()); assert((shapes[i].mesh.indices.size() % 3) == 0); - for (size_t f = 0; f < shapes[i].mesh.indices.size(); f++) { - printf(" idx[%ld] = %d\n", f, shapes[i].mesh.indices[f]); + for (size_t f = 0; f < shapes[i].mesh.indices.size() / 3; f++) { + printf(" idx[%ld] = %d, %d, %d. mat_id = %d\n", f, shapes[i].mesh.indices[3*f+0], shapes[i].mesh.indices[3*f+1], shapes[i].mesh.indices[3*f+2], shapes[i].mesh.material_ids[f]); } printf("shape[%ld].vertices: %ld\n", i, shapes[i].mesh.positions.size()); @@ -28,6 +30,7 @@ static void PrintInfo(const std::vector& shapes) shapes[i].mesh.positions[3*v+2]); } +#if 0 printf("shape[%ld].material.name = %s\n", i, shapes[i].material.name.c_str()); printf(" material.Ka = (%f, %f ,%f)\n", shapes[i].material.ambient[0], shapes[i].material.ambient[1], shapes[i].material.ambient[2]); printf(" material.Kd = (%f, %f ,%f)\n", shapes[i].material.diffuse[0], shapes[i].material.diffuse[1], shapes[i].material.diffuse[2]); @@ -47,6 +50,7 @@ static void PrintInfo(const std::vector& shapes) for (; it != itEnd; it++) { printf(" material.%s = %s\n", it->first.c_str(), it->second.c_str()); } +#endif printf("\n"); } } @@ -59,19 +63,21 @@ TestLoadObj( std::cout << "Loading " << filename << std::endl; std::vector shapes; - std::string err = tinyobj::LoadObj(shapes, filename, basepath); + std::vector materials; + std::string err = tinyobj::LoadObj(shapes, materials, filename, basepath); if (!err.empty()) { std::cerr << err << std::endl; return false; } - PrintInfo(shapes); + PrintInfo(shapes, materials); return true; } +#if 0 // @todo static bool TestStreamLoadObj() { @@ -168,6 +174,7 @@ std::string matStream( return true; } +#endif int main( @@ -184,7 +191,7 @@ main( } else { assert(true == TestLoadObj("cornell_box.obj")); assert(true == TestLoadObj("cube.obj")); - assert(true == TestStreamLoadObj()); + //assert(true == TestStreamLoadObj()); @todo } return 0; diff --git a/tiny_obj_loader.cc b/tiny_obj_loader.cc index 5c28379..d650688 100755 --- a/tiny_obj_loader.cc +++ b/tiny_obj_loader.cc @@ -229,7 +229,7 @@ exportFaceGroupToShape( const std::vector &in_normals, const std::vector &in_texcoords, const std::vector >& faceGroup, - const int material, + const int material_id, const std::string &name, bool clearCache) { @@ -263,19 +263,14 @@ exportFaceGroupToShape( shape.mesh.indices.push_back(v0); shape.mesh.indices.push_back(v1); shape.mesh.indices.push_back(v2); + + shape.mesh.material_ids.push_back(material_id); } } - // - // Construct shape. - // - shape.submeshes.push_back(std::pair(offset, shape.mesh.indices.size()-offset)); - shape.name = name; - shape.materials.push_back(material); - if (clearCache) vertexCache.clear(); diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index a832aa5..1fe36d7 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -40,14 +40,15 @@ typedef struct std::vector normals; std::vector texcoords; std::vector indices; + std::vector material_ids; // per-mesh material ID } mesh_t; typedef struct { std::string name; mesh_t mesh; - std::vector< std::pair > submeshes; - std::vector< int > materials; + //std::vector< std::pair > submeshes; + //std::vector< int > materials; } shape_t; class MaterialReader