Fixed OBJ reader not reading materials which start with a leading space

This commit is contained in:
Jan Möbius
2021-09-24 11:10:47 +02:00
parent f1464a8424
commit f5e5aa83b1
4 changed files with 84 additions and 5 deletions

View File

@@ -188,17 +188,18 @@ 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;
}

View File

@@ -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

View File

@@ -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

View File

@@ -623,7 +623,48 @@ 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";
EXPECT_FLOAT_EQ(32, mesh_.color(fh0)[0] ) << "Wrong vertex color at face 0 component 0";
EXPECT_FLOAT_EQ(64 , mesh_.color(fh0)[1] ) << "Wrong vertex color at face 0 component 1";
EXPECT_FLOAT_EQ(128, mesh_.color(fh0)[2] ) << "Wrong vertex color at face 0 component 2";
EXPECT_FLOAT_EQ(32, mesh_.color(fh1)[0] ) << "Wrong vertex color at face 1 component 0";
EXPECT_FLOAT_EQ(64, mesh_.color(fh1)[1] ) << "Wrong vertex color at face 1 component 1";
EXPECT_FLOAT_EQ(128, mesh_.color(fh1)[2] ) << "Wrong vertex color at face 1 component 2";
EXPECT_FLOAT_EQ(32, mesh_.color(fh2)[0] ) << "Wrong vertex color at face 2 component 0";
EXPECT_FLOAT_EQ(64, mesh_.color(fh2)[1] ) << "Wrong vertex color at face 2 component 1";
EXPECT_FLOAT_EQ(128, mesh_.color(fh2)[2] ) << "Wrong vertex color at face 2 component 2";
EXPECT_FLOAT_EQ(32, mesh_.color(fh3)[0] ) << "Wrong vertex color at face 3 component 0";
EXPECT_FLOAT_EQ(64, mesh_.color(fh3)[1] ) << "Wrong vertex color at face 3 component 1";
EXPECT_FLOAT_EQ(128, mesh_.color(fh3)[2] ) << "Wrong vertex color at face 3 component 2";
}