diff --git a/premake4.lua b/premake4.lua index ad020a6..222020b 100644 --- a/premake4.lua +++ b/premake4.lua @@ -11,7 +11,7 @@ solution "TinyObjLoaderSolution" configurations { "Release", "Debug" } if (os.is("windows")) then - platforms { "x32", "x64" } + platforms { "x64", "x32" } else platforms { "native", "x32", "x64" } end diff --git a/test.cc b/test.cc index 1ad6d8c..a1e067f 100644 --- a/test.cc +++ b/test.cc @@ -15,6 +15,8 @@ static void PrintInfo(const std::vector& shapes, const std::ve for (size_t i = 0; i < shapes.size(); i++) { printf("shape[%ld].name = %s\n", i, shapes[i].name.c_str()); printf("Size of shape[%ld].indices: %ld\n", i, shapes[i].mesh.indices.size()); + printf("Size of shape[%ld].normal_indices: %ld\n", i, shapes[i].mesh.normal_indices.size()); + printf("Size of shape[%ld].texcoord_indices: %ld\n", i, shapes[i].mesh.texcoord_indices.size()); printf("Size of 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() / 3; f++) { diff --git a/tiny_obj_loader.cc b/tiny_obj_loader.cc index 93218e9..4ad05e2 100644 --- a/tiny_obj_loader.cc +++ b/tiny_obj_loader.cc @@ -300,14 +300,14 @@ static vertex_index parseTriple(const char *&token, int vsize, int vnsize, return vi; } -static unsigned int -updateVertex(std::map &vertexCache, +static vertex_index +updateVertex(std::map &vertexCache, std::vector &positions, std::vector &normals, std::vector &texcoords, const std::vector &in_positions, const std::vector &in_normals, const std::vector &in_texcoords, const vertex_index &i) { - const std::map::iterator it = vertexCache.find(i); + const std::map::iterator it = vertexCache.find(i); if (it != vertexCache.end()) { // found cache @@ -331,10 +331,14 @@ updateVertex(std::map &vertexCache, texcoords.push_back(in_texcoords[2 * i.vt_idx + 1]); } - unsigned int idx = static_cast(positions.size() / 3 - 1); - vertexCache[i] = idx; + unsigned int v_idx = static_cast(positions.size() / 3 - 1); + unsigned int vn_idx = static_cast(normals.size() / 3 - 1); + unsigned int vt_idx = static_cast(texcoords.size() / 2 - 1); + vertexCache[i].v_idx = v_idx; + vertexCache[i].vn_idx = vn_idx; + vertexCache[i].vt_idx = vt_idx; - return idx; + return vertexCache[i]; } void InitMaterial(material_t &material) { @@ -358,7 +362,7 @@ void InitMaterial(material_t &material) { } static bool exportFaceGroupToShape( - shape_t &shape, std::map vertexCache, + shape_t &shape, std::map vertexCache, const std::vector &in_positions, const std::vector &in_normals, const std::vector &in_texcoords, @@ -383,19 +387,27 @@ static bool exportFaceGroupToShape( i1 = i2; i2 = face[k]; - unsigned int v0 = updateVertex( + vertex_index vi0 = updateVertex( vertexCache, shape.mesh.positions, shape.mesh.normals, shape.mesh.texcoords, in_positions, in_normals, in_texcoords, i0); - unsigned int v1 = updateVertex( + vertex_index vi1 = updateVertex( vertexCache, shape.mesh.positions, shape.mesh.normals, shape.mesh.texcoords, in_positions, in_normals, in_texcoords, i1); - unsigned int v2 = updateVertex( + vertex_index vi2 = updateVertex( vertexCache, shape.mesh.positions, shape.mesh.normals, shape.mesh.texcoords, in_positions, in_normals, in_texcoords, i2); - shape.mesh.indices.push_back(v0); - shape.mesh.indices.push_back(v1); - shape.mesh.indices.push_back(v2); + shape.mesh.indices.push_back(vi0.v_idx); + shape.mesh.indices.push_back(vi1.v_idx); + shape.mesh.indices.push_back(vi2.v_idx); + + shape.mesh.normal_indices.push_back(vi0.vn_idx); + shape.mesh.normal_indices.push_back(vi1.vn_idx); + shape.mesh.normal_indices.push_back(vi2.vn_idx); + + shape.mesh.texcoord_indices.push_back(vi0.vt_idx); + shape.mesh.texcoord_indices.push_back(vi1.vt_idx); + shape.mesh.texcoord_indices.push_back(vi2.vt_idx); shape.mesh.material_ids.push_back(material_id); } @@ -662,7 +674,7 @@ std::string LoadObj(std::vector &shapes, // material std::map material_map; - std::map vertexCache; + std::map vertexCache; int material = -1; shape_t shape; diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 512f32b..e3f1b8f 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -37,7 +37,9 @@ typedef struct { std::vector positions; std::vector normals; std::vector texcoords; - std::vector indices; + std::vector indices; // indices for vertex + std::vector normal_indices; // indices for normal + std::vector texcoord_indices; // indices for texcoord std::vector material_ids; // per-mesh material ID } mesh_t;