diff --git a/graphics/src/ColladaLoader.cc b/graphics/src/ColladaLoader.cc index d9117537c..9bdd56f5c 100644 --- a/graphics/src/ColladaLoader.cc +++ b/graphics/src/ColladaLoader.cc @@ -1998,7 +1998,7 @@ void ColladaLoaderPrivate::LoadPolylist(tinyxml2::XMLElement *_polylistXml, std::vector verts; std::vector norms; std::map> texcoords; - std::map texcoordsOffsetToSet; + std::vector> texcoordsOffsetToSet; const unsigned int VERTEX = 0; const unsigned int NORMAL = 1; @@ -2042,16 +2042,13 @@ void ColladaLoaderPrivate::LoadPolylist(tinyxml2::XMLElement *_polylistXml, else if (semantic == "TEXCOORD") { int offsetInt = ignition::math::parseInt(offset); - if (inputs[TEXCOORD].find(offsetInt) == inputs[TEXCOORD].end()) - { - unsigned int set = 0u; - auto setStr = polylistInputXml->Attribute("set"); - if (setStr) - set = ignition::math::parseInt(setStr); - this->LoadTexCoords(source, texcoords[set], texDupMap[set]); - inputs[TEXCOORD].insert(offsetInt); - texcoordsOffsetToSet[offsetInt] = set; - } + unsigned int set = 0u; + auto setStr = polylistInputXml->Attribute("set"); + if (setStr) + set = ignition::math::parseInt(setStr); + this->LoadTexCoords(source, texcoords[set], texDupMap[set]); + inputs[TEXCOORD].insert(offsetInt); + texcoordsOffsetToSet.push_back(std::make_pair(offsetInt, set)); } else { @@ -2170,9 +2167,10 @@ void ColladaLoaderPrivate::LoadPolylist(tinyxml2::XMLElement *_polylistXml, if (!inputs[TEXCOORD].empty()) { texEqual = true; - for (auto offset : inputs[TEXCOORD]) + for (auto &pair : texcoordsOffsetToSet) { - int set = texcoordsOffsetToSet[offset]; + unsigned int offset = pair.first; + unsigned int set = pair.second; // Get the vertex texcoord index value. If the texcoord is a // duplicate then reset the index to the first instance of the // duplicated texcoord @@ -2254,13 +2252,14 @@ void ColladaLoaderPrivate::LoadPolylist(tinyxml2::XMLElement *_polylistXml, if (!inputs[TEXCOORD].empty()) { - for (auto offset : inputs[TEXCOORD]) + for (auto &pair : texcoordsOffsetToSet) { + unsigned int offset = pair.first; + unsigned int set = pair.second; + unsigned int inputRemappedTexcoordIndex = values[offset]; - int set = texcoordsOffsetToSet[offset]; - auto &texDupMapSet = texDupMap[set]; auto texDupMapSetIt = texDupMapSet.find( inputRemappedTexcoordIndex); @@ -2327,7 +2326,7 @@ void ColladaLoaderPrivate::LoadTriangles(tinyxml2::XMLElement *_trianglesXml, std::vector verts; std::vector norms; std::map> texcoords; - std::map texcoordsOffsetToSet; + std::vector> texcoordsOffsetToSet; const unsigned int VERTEX = 0; const unsigned int NORMAL = 1; @@ -2373,16 +2372,13 @@ void ColladaLoaderPrivate::LoadTriangles(tinyxml2::XMLElement *_trianglesXml, else if (semantic == "TEXCOORD") { int offsetInt = ignition::math::parseInt(offset); - if (inputs[TEXCOORD].find(offsetInt) == inputs[TEXCOORD].end()) - { - unsigned int set = 0u; - auto setStr = trianglesInputXml->Attribute("set"); - if (setStr) - set = ignition::math::parseInt(setStr); - this->LoadTexCoords(source, texcoords[set], texDupMap[set]); - inputs[TEXCOORD].insert(offsetInt); - texcoordsOffsetToSet[offsetInt] = set; - } + unsigned int set = 0u; + auto setStr = trianglesInputXml->Attribute("set"); + if (setStr) + set = ignition::math::parseInt(setStr); + this->LoadTexCoords(source, texcoords[set], texDupMap[set]); + inputs[TEXCOORD].insert(offsetInt); + texcoordsOffsetToSet.push_back(std::make_pair(offsetInt, set)); hasTexcoords = true; } else @@ -2495,14 +2491,16 @@ void ColladaLoaderPrivate::LoadTriangles(tinyxml2::XMLElement *_trianglesXml, if (hasTexcoords) { texEqual = true; - for (auto offset : inputs[TEXCOORD]) + for (auto &pair : texcoordsOffsetToSet) { + unsigned int offset = pair.first; + unsigned int set = pair.second; + // Get the vertex texcoord index value. If the texcoord is a // duplicate then reset the index to the first instance of the // duplicated texcoord unsigned int remappedTexcoordIndex = values.at(offset); - int set = texcoordsOffsetToSet[offset]; auto &texDupMapSet = texDupMap[set]; auto texDupMapSetIt = texDupMapSet.find(remappedTexcoordIndex); if (texDupMapSetIt != texDupMapSet.end()) @@ -2577,12 +2575,14 @@ void ColladaLoaderPrivate::LoadTriangles(tinyxml2::XMLElement *_trianglesXml, } if (hasTexcoords) { - for (auto offset : inputs[TEXCOORD]) + for (auto &pair : texcoordsOffsetToSet) { + unsigned int offset = pair.first; + unsigned int set = pair.second; + unsigned int inputRemappedTexcoordIndex = values.at(offset); - int set = texcoordsOffsetToSet[offset]; auto &texDupMapSet = texDupMap[set]; auto texDupMapSetIt = texDupMapSet.find(inputRemappedTexcoordIndex); if (texDupMapSetIt != texDupMapSet.end()) diff --git a/graphics/src/ColladaLoader_TEST.cc b/graphics/src/ColladaLoader_TEST.cc index 828cd4783..0936e9ed4 100644 --- a/graphics/src/ColladaLoader_TEST.cc +++ b/graphics/src/ColladaLoader_TEST.cc @@ -229,7 +229,7 @@ TEST_F(ColladaLoader, TexCoordSets) EXPECT_FALSE(subMeshB->HasTexCoord(3u)); // test texture coordinate set API - EXPECT_EQ(2u, subMeshB->TexCoordSetCount()); + EXPECT_EQ(3u, subMeshB->TexCoordSetCount()); EXPECT_EQ(3u, subMeshB->TexCoordCountBySet(0u)); EXPECT_EQ(math::Vector2d(0, 1), subMeshB->TexCoordBySet(0u, 0u)); EXPECT_EQ(math::Vector2d(0, 1), subMeshB->TexCoordBySet(1u, 0u)); @@ -252,6 +252,16 @@ TEST_F(ColladaLoader, TexCoordSets) EXPECT_TRUE(subMeshB->HasTexCoordBySet(2u, 1u)); EXPECT_FALSE(subMeshB->HasTexCoordBySet(3u, 1u)); + EXPECT_EQ(3u, subMeshB->TexCoordCountBySet(2u)); + EXPECT_EQ(math::Vector2d(0, 0.8), subMeshB->TexCoordBySet(0u, 2u)); + EXPECT_EQ(math::Vector2d(0, 0.7), subMeshB->TexCoordBySet(1u, 2u)); + EXPECT_EQ(math::Vector2d(0, 0.6), subMeshB->TexCoordBySet(2u, 2u)); + + EXPECT_TRUE(subMeshB->HasTexCoordBySet(0u, 2u)); + EXPECT_TRUE(subMeshB->HasTexCoordBySet(1u, 2u)); + EXPECT_TRUE(subMeshB->HasTexCoordBySet(2u, 2u)); + EXPECT_FALSE(subMeshB->HasTexCoordBySet(3u, 2u)); + subMeshB->SetTexCoordBySet(2u, math::Vector2d(0.1, 0.2), 1u); EXPECT_EQ(math::Vector2d(0.1, 0.2), subMeshB->TexCoordBySet(2u, 1u)); } diff --git a/test/data/multiple_texture_coordinates_triangle.dae b/test/data/multiple_texture_coordinates_triangle.dae index d3c13b8f3..3fb4c1fd0 100644 --- a/test/data/multiple_texture_coordinates_triangle.dae +++ b/test/data/multiple_texture_coordinates_triangle.dae @@ -113,6 +113,15 @@ + + 0 0.2 0 0.3 0 0.4 + + + + + + + @@ -121,6 +130,7 @@ +

0 0 0 0 1 0 1 1 2 0 2 2