2018-09-18 03:17:35 +00:00
|
|
|
#include "skinnedmeshcreator.h"
|
2018-10-26 23:04:45 +00:00
|
|
|
#include "theme.h"
|
2018-09-18 03:17:35 +00:00
|
|
|
|
2018-10-25 00:19:38 +00:00
|
|
|
SkinnedMeshCreator::SkinnedMeshCreator(const Outcome &outcome,
|
2018-10-26 23:04:45 +00:00
|
|
|
const std::map<int, RiggerVertexWeights> &resultWeights) :
|
2018-10-25 00:19:38 +00:00
|
|
|
m_outcome(outcome),
|
2018-09-18 03:17:35 +00:00
|
|
|
m_resultWeights(resultWeights)
|
|
|
|
{
|
2018-11-17 23:02:12 +00:00
|
|
|
m_verticesOldIndices.resize(m_outcome.triangles.size());
|
2018-10-26 23:04:45 +00:00
|
|
|
m_verticesBindNormals.resize(m_outcome.triangles.size());
|
|
|
|
m_verticesBindPositions.resize(m_outcome.triangles.size());
|
|
|
|
const std::vector<std::vector<QVector3D>> *triangleVertexNormals = m_outcome.triangleVertexNormals();
|
|
|
|
for (size_t triangleIndex = 0; triangleIndex < m_outcome.triangles.size(); triangleIndex++) {
|
|
|
|
for (int j = 0; j < 3; j++) {
|
|
|
|
int oldIndex = m_outcome.triangles[triangleIndex][j];
|
2018-11-17 23:02:12 +00:00
|
|
|
m_verticesOldIndices[triangleIndex].push_back(oldIndex);
|
2018-10-26 23:04:45 +00:00
|
|
|
m_verticesBindPositions[triangleIndex].push_back(m_outcome.vertices[oldIndex]);
|
|
|
|
if (nullptr != triangleVertexNormals)
|
|
|
|
m_verticesBindNormals[triangleIndex].push_back((*triangleVertexNormals)[triangleIndex][j]);
|
|
|
|
else
|
|
|
|
m_verticesBindNormals[triangleIndex].push_back(QVector3D());
|
|
|
|
}
|
2018-09-18 03:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 23:04:45 +00:00
|
|
|
std::map<std::pair<QUuid, QUuid>, QColor> sourceNodeToColorMap;
|
|
|
|
for (const auto &node: outcome.nodes)
|
|
|
|
sourceNodeToColorMap.insert({{node.partId, node.nodeId}, node.color});
|
|
|
|
|
|
|
|
m_triangleColors.resize(m_outcome.triangles.size(), Theme::white);
|
|
|
|
const std::vector<std::pair<QUuid, QUuid>> *triangleSourceNodes = outcome.triangleSourceNodes();
|
|
|
|
if (nullptr != triangleSourceNodes) {
|
|
|
|
for (size_t triangleIndex = 0; triangleIndex < m_outcome.triangles.size(); triangleIndex++) {
|
|
|
|
const auto &source = (*triangleSourceNodes)[triangleIndex];
|
|
|
|
m_triangleColors[triangleIndex] = sourceNodeToColorMap[source];
|
|
|
|
}
|
2018-09-18 03:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MeshLoader *SkinnedMeshCreator::createMeshFromTransform(const std::vector<QMatrix4x4> &matricies)
|
|
|
|
{
|
2018-10-26 23:04:45 +00:00
|
|
|
std::vector<std::vector<QVector3D>> transformedPositions = m_verticesBindPositions;
|
|
|
|
std::vector<std::vector<QVector3D>> transformedPoseNormals = m_verticesBindNormals;
|
2018-09-18 03:17:35 +00:00
|
|
|
|
|
|
|
if (!matricies.empty()) {
|
2018-10-26 23:04:45 +00:00
|
|
|
for (size_t i = 0; i < transformedPositions.size(); ++i) {
|
|
|
|
for (size_t j = 0; j < 3; ++j) {
|
2018-11-17 23:02:12 +00:00
|
|
|
const auto &weight = m_resultWeights[m_verticesOldIndices[i][j]];
|
2018-10-26 23:04:45 +00:00
|
|
|
QMatrix4x4 mixedMatrix;
|
|
|
|
transformedPositions[i][j] = QVector3D();
|
|
|
|
transformedPoseNormals[i][j] = QVector3D();
|
|
|
|
for (int x = 0; x < 4; x++) {
|
|
|
|
float factor = weight.boneWeights[x];
|
|
|
|
if (factor > 0) {
|
2018-11-17 23:02:12 +00:00
|
|
|
transformedPositions[i][j] += matricies[weight.boneIndices[x]] * m_verticesBindPositions[i][j] * factor;
|
|
|
|
transformedPoseNormals[i][j] += matricies[weight.boneIndices[x]] * m_verticesBindNormals[i][j] * factor;
|
2018-10-26 23:04:45 +00:00
|
|
|
}
|
2018-09-18 03:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 09:36:50 +00:00
|
|
|
ShaderVertex *triangleVertices = new ShaderVertex[m_outcome.triangles.size() * 3];
|
2018-09-18 03:17:35 +00:00
|
|
|
int triangleVerticesNum = 0;
|
2018-10-25 00:19:38 +00:00
|
|
|
for (size_t triangleIndex = 0; triangleIndex < m_outcome.triangles.size(); triangleIndex++) {
|
2018-09-18 03:17:35 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2019-07-04 09:36:50 +00:00
|
|
|
ShaderVertex ¤tVertex = triangleVertices[triangleVerticesNum++];
|
2018-10-26 23:04:45 +00:00
|
|
|
const auto &sourcePosition = transformedPositions[triangleIndex][i];
|
|
|
|
const auto &sourceColor = m_triangleColors[triangleIndex];
|
|
|
|
const auto &sourceNormal = transformedPoseNormals[triangleIndex][i];
|
2018-09-18 03:17:35 +00:00
|
|
|
currentVertex.posX = sourcePosition.x();
|
|
|
|
currentVertex.posY = sourcePosition.y();
|
|
|
|
currentVertex.posZ = sourcePosition.z();
|
|
|
|
currentVertex.texU = 0;
|
|
|
|
currentVertex.texV = 0;
|
|
|
|
currentVertex.colorR = sourceColor.redF();
|
|
|
|
currentVertex.colorG = sourceColor.greenF();
|
|
|
|
currentVertex.colorB = sourceColor.blueF();
|
|
|
|
currentVertex.normX = sourceNormal.x();
|
|
|
|
currentVertex.normY = sourceNormal.y();
|
|
|
|
currentVertex.normZ = sourceNormal.z();
|
2018-10-09 02:19:12 +00:00
|
|
|
currentVertex.metalness = MeshLoader::m_defaultMetalness;
|
|
|
|
currentVertex.roughness = MeshLoader::m_defaultRoughness;
|
2018-09-18 03:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MeshLoader(triangleVertices, triangleVerticesNum);
|
|
|
|
}
|