diff --git a/Doc/changelog.docu b/Doc/changelog.docu index d4236358..06043ea4 100644 --- a/Doc/changelog.docu +++ b/Doc/changelog.docu @@ -35,6 +35,7 @@
  • OM Writer/Reader: Added support for (re)storing properties of type vector
  • OM Reader: Properties can now be restored from files without the need to create the properties beforehand. Custom types need to be registered for this to work using the OM_REGISTER_PROPERTY_TYPE macro.
  • PLY writer: Added option to choose if we want to write texture coordinates as uv or st (Thanks to Ossi Saukko from Centria Research and Development for the patch)
  • +
  • OBJ Reader: Fix material reader, when starting with spaces
  • Tools diff --git a/src/OpenMesh/Core/IO/reader/OBJReader.cc b/src/OpenMesh/Core/IO/reader/OBJReader.cc index bdefcd7d..6fe6141e 100644 --- a/src/OpenMesh/Core/IO/reader/OBJReader.cc +++ b/src/OpenMesh/Core/IO/reader/OBJReader.cc @@ -188,23 +188,24 @@ read_material(std::fstream& _in) stream >> keyWrd; - if( ( isspace(line[0]) && line[0] != '\t' ) || line[0] == '#' ) + if (keyWrd == "newmtl") // begin new material definition { + // If we are in a material definition (Already reading a material) + // And a material name has been set + // And the current material definition is valid + // Then Store the current material in our lookup table if (indef && !key.empty() && mat.is_valid()) { materials_[key] = mat; mat.cleanup(); } - } - else if (keyWrd == "newmtl") // begin new material definition - { stream >> key; indef = true; } else if (keyWrd == "Kd") // diffuse color - { + { stream >> f1; stream >> f2; stream >> f3; if( !stream.fail() ) diff --git a/src/Unittests/TestFiles/CubeCol.mtl b/src/Unittests/TestFiles/CubeCol.mtl new file mode 100644 index 00000000..bcbb1b20 --- /dev/null +++ b/src/Unittests/TestFiles/CubeCol.mtl @@ -0,0 +1,9 @@ +newmtl cube +Ns 10.0000 +Ni 1.000 +d 1.0000 +illum 2 +#Ambient, Diffuse, Specular colors + Ka 1.0000 0.7000 0.1000 + Kd 0.1250 0.2500 0.5000 + Ks 1.0000 0.6000 0.4000 diff --git a/src/Unittests/TestFiles/CubeCol.obj b/src/Unittests/TestFiles/CubeCol.obj new file mode 100644 index 00000000..b6087333 --- /dev/null +++ b/src/Unittests/TestFiles/CubeCol.obj @@ -0,0 +1,28 @@ +# cube.obj +# +o cube +mtllib CubeCol.mtl + +v -0.500000 -0.500000 0.500000 +v 0.500000 -0.500000 0.500000 +v -0.500000 0.500000 0.500000 +v 0.500000 0.500000 0.500000 +v -0.500000 0.500000 -0.500000 +v 0.500000 0.500000 -0.500000 +v -0.500000 -0.500000 -0.500000 +v 0.500000 -0.500000 -0.500000 + +g cube +usemtl cube +f 1 2 3 +f 3 2 4 +f 3 4 5 +f 5 4 6 +f 5 6 7 +f 7 6 8 +f 7 8 1 +f 1 8 2 +f 2 8 4 +f 4 8 6 +f 7 1 5 +f 5 1 3 \ No newline at end of file diff --git a/src/Unittests/unittests_read_write_OBJ.cc b/src/Unittests/unittests_read_write_OBJ.cc index c779daa0..3f9288b3 100644 --- a/src/Unittests/unittests_read_write_OBJ.cc +++ b/src/Unittests/unittests_read_write_OBJ.cc @@ -623,7 +623,63 @@ TEST_F(OpenMeshReadWriteOBJ, FaceTexCoordTest) { } +/* + * Load, save and load a simple obj + */ +TEST_F(OpenMeshReadWriteOBJ, ReadOBJMTL) { + mesh_.clear(); + mesh_.request_face_colors(); + + OpenMesh::IO::Options options; + options += OpenMesh::IO::Options::FaceColor; + bool ok = OpenMesh::IO::read_mesh(mesh_, "CubeCol.obj", options); + + EXPECT_TRUE(ok) << "Unable to load CubeCol.obj"; + + OpenMesh::FaceHandle fh0 = mesh_.face_handle(0); + OpenMesh::FaceHandle fh1 = mesh_.face_handle(1); + OpenMesh::FaceHandle fh2 = mesh_.face_handle(2); + OpenMesh::FaceHandle fh3 = mesh_.face_handle(3); + + + EXPECT_TRUE(fh0.is_valid()) << "fh0 should be valid"; + EXPECT_TRUE(fh1.is_valid()) << "fh1 should be valid"; + EXPECT_TRUE(fh2.is_valid()) << "fh2 should be valid"; + EXPECT_TRUE(fh3.is_valid()) << "fh3 should be valid"; + + + +#ifdef TEST_DOUBLE_TRAITS + const float value1 = 32.0/255.0; + const float value2 = 64.0/255.0; + const float value3 = 128.0/255.0; +#else + const float value1 = 32; + const float value2 = 64; + const float value3 = 128; +#endif + + + + + EXPECT_FLOAT_EQ(value1, mesh_.color(fh0)[0] ) << "Wrong vertex color at face 0 component 0"; + EXPECT_FLOAT_EQ(value2 , mesh_.color(fh0)[1] ) << "Wrong vertex color at face 0 component 1"; + EXPECT_FLOAT_EQ(value3, mesh_.color(fh0)[2] ) << "Wrong vertex color at face 0 component 2"; + + EXPECT_FLOAT_EQ(value1, mesh_.color(fh1)[0] ) << "Wrong vertex color at face 1 component 0"; + EXPECT_FLOAT_EQ(value2, mesh_.color(fh1)[1] ) << "Wrong vertex color at face 1 component 1"; + EXPECT_FLOAT_EQ(value3, mesh_.color(fh1)[2] ) << "Wrong vertex color at face 1 component 2"; + + EXPECT_FLOAT_EQ(value1, mesh_.color(fh2)[0] ) << "Wrong vertex color at face 2 component 0"; + EXPECT_FLOAT_EQ(value2, mesh_.color(fh2)[1] ) << "Wrong vertex color at face 2 component 1"; + EXPECT_FLOAT_EQ(value3, mesh_.color(fh2)[2] ) << "Wrong vertex color at face 2 component 2"; + + EXPECT_FLOAT_EQ(value1, mesh_.color(fh3)[0] ) << "Wrong vertex color at face 3 component 0"; + EXPECT_FLOAT_EQ(value2, mesh_.color(fh3)[1] ) << "Wrong vertex color at face 3 component 1"; + EXPECT_FLOAT_EQ(value3, mesh_.color(fh3)[2] ) << "Wrong vertex color at face 3 component 2"; + +}