2018-09-18 03:17:35 +00:00
|
|
|
#include "skinnedmeshcreator.h"
|
|
|
|
|
2018-10-25 00:19:38 +00:00
|
|
|
SkinnedMeshCreator::SkinnedMeshCreator(const Outcome &outcome,
|
2018-09-18 03:17:35 +00:00
|
|
|
const std::map<int, AutoRiggerVertexWeights> &resultWeights) :
|
2018-10-25 00:19:38 +00:00
|
|
|
m_outcome(outcome),
|
2018-09-18 03:17:35 +00:00
|
|
|
m_resultWeights(resultWeights)
|
|
|
|
{
|
2018-10-25 00:19:38 +00:00
|
|
|
for (const auto &vertex: m_outcome.vertices) {
|
2018-09-18 03:17:35 +00:00
|
|
|
m_verticesBindPositions.push_back(vertex.position);
|
|
|
|
}
|
|
|
|
|
2018-10-25 00:19:38 +00:00
|
|
|
m_verticesBindNormals.resize(m_outcome.vertices.size());
|
|
|
|
for (size_t triangleIndex = 0; triangleIndex < m_outcome.triangles.size(); triangleIndex++) {
|
|
|
|
const auto &sourceTriangle = m_outcome.triangles[triangleIndex];
|
2018-09-18 03:17:35 +00:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
m_verticesBindNormals[sourceTriangle.indicies[i]] += sourceTriangle.normal;
|
|
|
|
}
|
|
|
|
for (auto &item: m_verticesBindNormals)
|
|
|
|
item.normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
MeshLoader *SkinnedMeshCreator::createMeshFromTransform(const std::vector<QMatrix4x4> &matricies)
|
|
|
|
{
|
|
|
|
std::vector<QVector3D> transformedPositions = m_verticesBindPositions;
|
|
|
|
std::vector<QVector3D> transformedPoseNormals = m_verticesBindNormals;
|
|
|
|
|
|
|
|
if (!matricies.empty()) {
|
|
|
|
for (const auto &weightItem: m_resultWeights) {
|
|
|
|
size_t vertexIndex = weightItem.first;
|
|
|
|
const auto &weight = weightItem.second;
|
|
|
|
QMatrix4x4 mixedMatrix;
|
|
|
|
transformedPositions[vertexIndex] = QVector3D();
|
|
|
|
transformedPoseNormals[vertexIndex] = QVector3D();
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
float factor = weight.boneWeights[i];
|
|
|
|
if (factor > 0) {
|
|
|
|
transformedPositions[vertexIndex] += matricies[weight.boneIndicies[i]] * m_verticesBindPositions[vertexIndex] * factor;
|
|
|
|
transformedPoseNormals[vertexIndex] += matricies[weight.boneIndicies[i]] * m_verticesBindNormals[vertexIndex] * factor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 00:19:38 +00:00
|
|
|
Vertex *triangleVertices = new Vertex[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++) {
|
|
|
|
const auto &sourceTriangle = m_outcome.triangles[triangleIndex];
|
|
|
|
OutcomeMaterial triangleMaterial = m_outcome.triangleMaterials()[triangleIndex];
|
2018-09-18 03:17:35 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
Vertex ¤tVertex = triangleVertices[triangleVerticesNum++];
|
|
|
|
const auto &sourcePosition = transformedPositions[sourceTriangle.indicies[i]];
|
2018-10-04 12:51:01 +00:00
|
|
|
const auto &sourceColor = triangleMaterial.color;
|
2018-09-18 03:17:35 +00:00
|
|
|
const auto &sourceNormal = transformedPoseNormals[sourceTriangle.indicies[i]];
|
|
|
|
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);
|
|
|
|
}
|