Fix normal interpolation on export

master
Jeremy Hu 2018-09-23 11:34:35 +08:00
parent d6b38f5750
commit e2f44dded7
2 changed files with 28 additions and 8 deletions

View File

@ -31,7 +31,8 @@ MeshResultContext::MeshResultContext() :
m_bmeshNodeMapResolved(false),
m_resultPartsResolved(false),
m_resultTriangleUvsResolved(false),
m_resultRearrangedVerticesResolved(false)
m_resultRearrangedVerticesResolved(false),
m_vertexNormalsInterpolated(false)
{
}
@ -334,7 +335,7 @@ void MeshResultContext::calculateResultParts(std::map<QUuid, ResultPart> &parts)
bool isSeamVertex = m_seamVertices.end() != m_seamVertices.find(triangle.indicies[i]);
if (isNewVertex || isSeamVertex) {
int newIndex = resultPart.vertices.size();
resultPart.interpolatedVertexNormals.push_back(newTriangle.normal);
resultPart.interpolatedVertexNormals.push_back(interpolatedVertexNormals()[triangle.indicies[i]]);
resultPart.verticesOldIndicies.push_back(triangle.indicies[i]);
resultPart.vertices.push_back(vertices[triangle.indicies[i]]);
ResultVertexUv vertexUv;
@ -346,17 +347,11 @@ void MeshResultContext::calculateResultParts(std::map<QUuid, ResultPart> &parts)
newTriangle.indicies[i] = newIndex;
} else {
newTriangle.indicies[i] = it->second;
resultPart.interpolatedVertexNormals[it->second] += newTriangle.normal;
}
}
resultPart.triangles.push_back(newTriangle);
resultPart.uvs.push_back(triangleUvs()[x]);
}
for (auto &partIt: parts) {
for (auto &normalIt: partIt.second.interpolatedVertexNormals) {
normalIt.normalize();
}
}
}
void MeshResultContext::calculateResultTriangleUvs(std::vector<ResultTriangleUv> &uvs, std::set<int> &seamVertices)
@ -539,3 +534,24 @@ void MeshResultContext::calculateResultRearrangedVertices(std::vector<ResultRear
rearrangedTriangles.push_back(newTriangle);
}
}
void MeshResultContext::interpolateVertexNormals(std::vector<QVector3D> &resultNormals)
{
resultNormals.resize(vertices.size());
for (size_t triangleIndex = 0; triangleIndex < triangles.size(); triangleIndex++) {
const auto &sourceTriangle = triangles[triangleIndex];
for (int i = 0; i < 3; i++)
resultNormals[sourceTriangle.indicies[i]] += sourceTriangle.normal;
}
for (auto &item: resultNormals)
item.normalize();
}
const std::vector<QVector3D> &MeshResultContext::interpolatedVertexNormals()
{
if (!m_vertexNormalsInterpolated) {
m_vertexNormalsInterpolated = true;
interpolateVertexNormals(m_interpolatedVertexNormals);
}
return m_interpolatedVertexNormals;
}

View File

@ -91,6 +91,7 @@ public:
const std::vector<ResultRearrangedVertex> &rearrangedVertices();
const std::vector<ResultRearrangedTriangle> &rearrangedTriangles();
const std::map<int, std::pair<QUuid, QUuid>> &vertexSourceMap();
const std::vector<QVector3D> &interpolatedVertexNormals();
private:
bool m_triangleSourceResolved;
bool m_triangleColorResolved;
@ -99,6 +100,7 @@ private:
bool m_resultPartsResolved;
bool m_resultTriangleUvsResolved;
bool m_resultRearrangedVerticesResolved;
bool m_vertexNormalsInterpolated;
private:
std::vector<std::pair<QUuid, QUuid>> m_triangleSourceNodes;
std::vector<QColor> m_triangleColors;
@ -111,6 +113,7 @@ private:
std::vector<ResultRearrangedTriangle> m_rearrangedTriangles;
std::map<int, std::pair<QUuid, QUuid>> m_vertexSourceMap;
std::map<int, int> m_rearrangedVerticesToOldIndexMap;
std::vector<QVector3D> m_interpolatedVertexNormals;
private:
void calculateTriangleSourceNodes(std::vector<std::pair<QUuid, QUuid>> &triangleSourceNodes, std::map<int, std::pair<QUuid, QUuid>> &vertexSourceMap);
void calculateRemainingVertexSourceNodesAfterTriangleSourceNodesSolved(std::map<int, std::pair<QUuid, QUuid>> &vertexSourceMap);
@ -120,6 +123,7 @@ private:
void calculateResultParts(std::map<QUuid, ResultPart> &parts);
void calculateResultTriangleUvs(std::vector<ResultTriangleUv> &uvs, std::set<int> &seamVertices);
void calculateResultRearrangedVertices(std::vector<ResultRearrangedVertex> &rearrangedVertices, std::vector<ResultRearrangedTriangle> &rearrangedTriangles);
void interpolateVertexNormals(std::vector<QVector3D> &resultNormals);
};
#endif