diff --git a/src/OpenMesh/Core/IO/reader/OBJReader.cc b/src/OpenMesh/Core/IO/reader/OBJReader.cc index 3a854ac8..9b918706 100644 --- a/src/OpenMesh/Core/IO/reader/OBJReader.cc +++ b/src/OpenMesh/Core/IO/reader/OBJReader.cc @@ -574,9 +574,10 @@ read(std::istream& _in, BaseImporter& _bi, Options& _opt) } - fh = _bi.add_face(faceVertices); - + // note that add_face can possibly triangulate the faces, which is why we have to + // store the current number of faces first size_t n_faces = _bi.n_faces(); + fh = _bi.add_face(faceVertices); if (!vhandles.empty() && fh.is_valid() ) _bi.add_face_texcoords(fh, vhandles[0], face_texcoords); diff --git a/src/Unittests/TestFiles/square_material.mtl b/src/Unittests/TestFiles/square_material.mtl new file mode 100644 index 00000000..3ac76a88 --- /dev/null +++ b/src/Unittests/TestFiles/square_material.mtl @@ -0,0 +1,4 @@ +newmtl Colored +Ka 0.500 0.500 0.500 +Kd 0.500 0.500 0.500 +Ks 0.500 0.500 0.500 diff --git a/src/Unittests/TestFiles/square_material.obj b/src/Unittests/TestFiles/square_material.obj new file mode 100644 index 00000000..d2a116fd --- /dev/null +++ b/src/Unittests/TestFiles/square_material.obj @@ -0,0 +1,13 @@ +mtllib square_material.mtl + +v 0.000000 2.000000 0.000000 +v 0.000000 0.000000 0.000000 +v 2.000000 0.000000 0.000000 +v 2.000000 2.000000 0.000000 +vt 0.000000 1.000000 0.000000 +vt 0.000000 0.000000 0.000000 +vt 1.000000 0.000000 0.000000 +vt 1.000000 1.000000 0.000000 + +usemtl Colored +f 1/1 2/2 3/3 4/4 diff --git a/src/Unittests/unittests_read_write_OBJ.hh b/src/Unittests/unittests_read_write_OBJ.hh index ae550bef..6c4dc762 100644 --- a/src/Unittests/unittests_read_write_OBJ.hh +++ b/src/Unittests/unittests_read_write_OBJ.hh @@ -185,6 +185,34 @@ TEST_F(OpenMeshReadWriteOBJ, LoadSimpleOBJCheckTexCoords) { mesh_.release_halfedge_texcoords2D(); } +/* + * Just load a obj file of a square with a material + */ +TEST_F(OpenMeshReadWriteOBJ, LoadObjWithMaterial) { + + mesh_.clear(); + + mesh_.request_face_colors(); + + OpenMesh::IO::Options options; + options += OpenMesh::IO::Options::FaceColor; + + std::string file_name = "square_material.obj"; + + bool ok = OpenMesh::IO::read_mesh(mesh_, file_name,options); + + EXPECT_TRUE(ok) << file_name; + + OpenMesh::FaceHandle fh = mesh_.face_handle(mesh_.halfedge_handle(0)); + + EXPECT_TRUE(fh.is_valid()) << "fh should be valid"; + + EXPECT_EQ(128, mesh_.color(fh)[0] ) << "Wrong vertex color at vertex 0 component 0"; + EXPECT_EQ(128, mesh_.color(fh)[1] ) << "Wrong vertex color at vertex 0 component 1"; + EXPECT_EQ(128, mesh_.color(fh)[2] ) << "Wrong vertex color at vertex 0 component 2"; + + mesh_.release_face_colors(); +} /* * Just load a obj file of a cube with vertex colors defined directly after the vertex definitions