diff options
author | Syoyo Fujita <syoyo@lighttransport.com> | 2014-09-14 19:51:24 +0900 |
---|---|---|
committer | Syoyo Fujita <syoyo@lighttransport.com> | 2014-09-14 19:51:24 +0900 |
commit | e4d4c65a172c87c2da67984c6680502459b62c9f (patch) | |
tree | a7339f64266acaf3b3bf40e964f8651adcb5ad81 /examples | |
parent | cbba0a807e09d55261a83cea5b26930ea4b811ce (diff) | |
download | tinyobjloader-e4d4c65a172c87c2da67984c6680502459b62c9f.tar.gz |
Fix material ID assignment.
Fix build of example/obj_sticher.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/obj_sticher/obj_sticher.cc | 35 | ||||
-rw-r--r-- | examples/obj_sticher/obj_writer.cc | 30 | ||||
-rw-r--r-- | examples/obj_sticher/obj_writer.h | 2 |
3 files changed, 45 insertions, 22 deletions
diff --git a/examples/obj_sticher/obj_sticher.cc b/examples/obj_sticher/obj_sticher.cc index 1d230b8..1833216 100644 --- a/examples/obj_sticher/obj_sticher.cc +++ b/examples/obj_sticher/obj_sticher.cc @@ -10,11 +10,14 @@ #include <cstdio> typedef std::vector<tinyobj::shape_t> Shape; +typedef std::vector<tinyobj::material_t> Material; void StichObjs( - std::vector<tinyobj::shape_t>& out, - const std::vector<Shape>& shapes) + std::vector<tinyobj::shape_t>& out_shape, + std::vector<tinyobj::material_t>& out_material, + const std::vector<Shape>& shapes, + const std::vector<Material>& materials) { int numShapes = 0; for (size_t i = 0; i < shapes.size(); i++) { @@ -22,9 +25,11 @@ StichObjs( } printf("Total # of shapes = %d\n", numShapes); + int materialIdOffset = 0; size_t face_offset = 0; for (size_t i = 0; i < shapes.size(); i++) { + for (size_t k = 0; k < shapes[i].size(); k++) { std::string new_name = shapes[i][k].name; @@ -38,12 +43,26 @@ StichObjs( assert((shapes[i][k].mesh.positions.size() % 3) == 0); tinyobj::shape_t new_shape = shapes[i][k]; + // Add offset. + for (size_t f = 0; f < new_shape.mesh.material_ids.size(); f++) { + new_shape.mesh.material_ids[f] += materialIdOffset; + } + new_shape.name = new_name; printf("shape[%ld][%ld].new_name = %s\n", i, k, new_shape.name.c_str()); - out.push_back(new_shape); + out_shape.push_back(new_shape); } + + materialIdOffset += materials[i].size(); } + + for (size_t i = 0; i < materials.size(); i++) { + for (size_t k = 0; k < materials[i].size(); k++) { + out_material.push_back(materials[i][k]); + } + } + } int @@ -60,12 +79,13 @@ main( std::string out_filename = std::string(argv[argc-1]); // last element std::vector<Shape> shapes; + std::vector<Material> materials; shapes.resize(num_objfiles); for (int i = 0; i < num_objfiles; i++) { std::cout << "Loading " << argv[i+1] << " ... " << std::flush; - std::string err = tinyobj::LoadObj(shapes[i], argv[i+1]); + std::string err = tinyobj::LoadObj(shapes[i], materials[i], argv[i+1]); if (!err.empty()) { std::cerr << err << std::endl; exit(1); @@ -74,10 +94,11 @@ main( std::cout << "DONE." << std::endl; } - std::vector<tinyobj::shape_t> out; - StichObjs(out, shapes); + std::vector<tinyobj::shape_t> out_shape; + std::vector<tinyobj::material_t> out_material; + StichObjs(out_shape, out_material, shapes, materials); - bool ret = WriteObj(out_filename, out); + bool ret = WriteObj(out_filename, out_shape, out_material); assert(ret); return 0; diff --git a/examples/obj_sticher/obj_writer.cc b/examples/obj_sticher/obj_writer.cc index f44d3f3..bb12457 100644 --- a/examples/obj_sticher/obj_writer.cc +++ b/examples/obj_sticher/obj_writer.cc @@ -11,22 +11,16 @@ static std::string GetFileBasename(const std::string& FileName) return ""; } -bool WriteMat(const std::string& filename, std::vector<tinyobj::shape_t> shapes) { +bool WriteMat(const std::string& filename, const std::vector<tinyobj::material_t>& materials) { FILE* fp = fopen(filename.c_str(), "w"); if (!fp) { fprintf(stderr, "Failed to open file [ %s ] for write.\n", filename.c_str()); return false; } - std::map<std::string, tinyobj::material_t> mtl_table; + for (size_t i = 0; i < materials.size(); i++) { - for (size_t i = 0; i < shapes.size(); i++) { - mtl_table[shapes[i].material.name] = shapes[i].material; - } - - for (std::map<std::string, tinyobj::material_t>::iterator it = mtl_table.begin(); it != mtl_table.end(); it++) { - - tinyobj::material_t mat = it->second; + tinyobj::material_t mat = materials[i]; fprintf(fp, "newmtl %s\n", mat.name.c_str()); fprintf(fp, "Ka %f %f %f\n", mat.ambient[0], mat.ambient[1], mat.ambient[2]); @@ -44,7 +38,7 @@ bool WriteMat(const std::string& filename, std::vector<tinyobj::shape_t> shapes) return true; } -bool WriteObj(const std::string& filename, std::vector<tinyobj::shape_t> shapes) { +bool WriteObj(const std::string& filename, const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials) { FILE* fp = fopen(filename.c_str(), "w"); if (!fp) { fprintf(stderr, "Failed to open file [ %s ] for write.\n", filename.c_str()); @@ -57,6 +51,7 @@ bool WriteObj(const std::string& filename, std::vector<tinyobj::shape_t> shapes) int v_offset = 0; int vn_offset = 0; int vt_offset = 0; + int prev_material_id = -1; fprintf(fp, "mtllib %s\n", material_filename.c_str()); @@ -71,9 +66,9 @@ bool WriteObj(const std::string& filename, std::vector<tinyobj::shape_t> shapes) fprintf(fp, "g %s\n", shapes[i].name.c_str()); } - if (!shapes[i].material.name.empty()) { - fprintf(fp, "usemtl %s\n", shapes[i].material.name.c_str()); - } + //if (!shapes[i].material.name.empty()) { + // fprintf(fp, "usemtl %s\n", shapes[i].material.name.c_str()); + //} // facevarying vtx for (size_t k = 0; k < shapes[i].mesh.indices.size() / 3; k++) { @@ -124,6 +119,13 @@ bool WriteObj(const std::string& filename, std::vector<tinyobj::shape_t> shapes) int v1 = (3*k + 1) + 1 + v_offset; int v2 = (3*k + 2) + 1 + v_offset; + int material_id = shapes[i].mesh.material_ids[k]; + if (material_id != prev_material_id) { + std::string material_name = materials[material_id].name; + fprintf(fp, "usemtl %s\n", material_name.c_str()); + prev_material_id = material_id; + } + if (has_vn && has_vt) { fprintf(fp, "f %d/%d/%d %d/%d/%d %d/%d/%d\n", v0, v0, v0, v1, v1, v1, v2, v2, v2); @@ -148,7 +150,7 @@ bool WriteObj(const std::string& filename, std::vector<tinyobj::shape_t> shapes) // // Write material file // - bool ret = WriteMat(material_filename, shapes); + bool ret = WriteMat(material_filename, materials); return ret; } diff --git a/examples/obj_sticher/obj_writer.h b/examples/obj_sticher/obj_writer.h index e31e0e4..00cd792 100644 --- a/examples/obj_sticher/obj_writer.h +++ b/examples/obj_sticher/obj_writer.h @@ -3,7 +3,7 @@ #include "../../tiny_obj_loader.h" -extern bool WriteObj(const std::string& filename, std::vector<tinyobj::shape_t> shapes); +extern bool WriteObj(const std::string& filename, const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials); #endif // __OBJ_WRITER_H__ |