added some code to fix vertex duplicates

This commit is contained in:
Anselme 2015-12-15 15:44:07 +01:00
parent ee9c7040e9
commit ed0cf67819
3 changed files with 72 additions and 3 deletions

6
mesh.h
View File

@ -27,17 +27,17 @@ struct Mesh
std::vector<Tangents> tangents; std::vector<Tangents> tangents;
std::vector<Group> indiceGroups; std::vector<Group> indiceGroups;
bool hasNormals() bool hasNormals() const
{ {
return normals.size() != 0; return normals.size() != 0;
} }
bool hasTexCoords() bool hasTexCoords() const
{ {
return texCoords.size() != 0; return texCoords.size() != 0;
} }
bool hasTangents() bool hasTangents() const
{ {
return tangents.size() != 0; return tangents.size() != 0;
} }

View File

@ -1,5 +1,6 @@
#include "meshbuilder.h" #include "meshbuilder.h"
#include <glm/ext.hpp> #include <glm/ext.hpp>
#include <set>
void MeshBuilder::addPosition(float x, float y, float z) void MeshBuilder::addPosition(float x, float y, float z)
{ {
@ -81,6 +82,71 @@ int MeshBuilder::getNbGroups()
return indiceGroups.size(); return indiceGroups.size();
} }
bool MeshBuilder::operator() (const int& vertId1, const int& vertId2) const
{
if(positions[vertId1] != positions[vertId2])
return (positions[vertId1].x + positions[vertId1].y + positions[vertId1].z) > (positions[vertId2].x + positions[vertId2].y + positions[vertId2].z);
if(hasTexCoords())
{
if(texCoords[vertId1] != texCoords[vertId2])
return (texCoords[vertId1].x + texCoords[vertId1].y) > (texCoords[vertId2].x + texCoords[vertId2].y);
}
if(hasNormals())
{
if(normals[vertId1] != normals[vertId2])
return (normals[vertId1].x + normals[vertId1].y + normals[vertId1].z) > (normals[vertId2].x + normals[vertId2].y + normals[vertId2].z);
}
return false;
}
void MeshBuilder::mergeVertices()
{
std::vector<int> swapped;
std::set<int, MeshBuilder> vertexSet;
int size = positions.size();
for(Group &g : indiceGroups)
for(int i=0; i<g.indices.size(); ++i)
{
std::pair<std::set<int,MeshBuilder>::iterator,bool> ret = vertexSet.insert(g.indices[i]);
if(!ret.second) // duplicate found
{
// updating indices references
int toDelete = g.indices[i];
swapped.push_back(toDelete);
g.indices[i] = *(ret.first);
// deleting the duplicate
positions[toDelete] = positions.back();
positions.pop_back();
if(hasTexCoords())
{
texCoords[toDelete] = texCoords.back();
texCoords.pop_back();
}
if(hasNormals())
{
normals[toDelete] = normals.back();
normals.pop_back();
}
if(hasTangents())
{
tangents[toDelete] = tangents.back();
tangents.pop_back();
}
}
}
fprintf(stdout, "found %d vertex duplicates\n", swapped.size());
for(Group &g : indiceGroups)
for(int i=0; i<g.indices.size(); ++i)
{
if(g.indices[i] >= positions.size())
g.indices[i] = swapped[size - g.indices[i]];
}
}
void MeshBuilder::computeNormals() void MeshBuilder::computeNormals()
{ {
normals.resize(positions.size()); normals.resize(positions.size());

View File

@ -26,6 +26,9 @@ public:
void setCurrentGroupMaterial(Material* myMaterial); void setCurrentGroupMaterial(Material* myMaterial);
int getNbGroups(); int getNbGroups();
// merge same vertices
void mergeVertices();
bool operator() (const int& vertId1, const int& vertId2) const;
// require positions and indices // require positions and indices
void computeNormals(); void computeNormals();
// require normals and texCoord // require normals and texCoord