From 95ec51e37bb27b6d273433bb32f149285ac150d1 Mon Sep 17 00:00:00 2001 From: Saracen Date: Mon, 29 Apr 2019 20:35:38 +0100 Subject: [PATCH] Formatting and naming changes. Tweak to structure of skinning code. --- libraries/fbx/src/GLTFSerializer.cpp | 49 ++++++++++++++-------------- libraries/fbx/src/GLTFSerializer.h | 2 +- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/libraries/fbx/src/GLTFSerializer.cpp b/libraries/fbx/src/GLTFSerializer.cpp index f5cc9ff03c..99211fb6fa 100755 --- a/libraries/fbx/src/GLTFSerializer.cpp +++ b/libraries/fbx/src/GLTFSerializer.cpp @@ -813,7 +813,7 @@ void GLTFSerializer::getSkinInverseBindMatrices(std::vector>& for (auto &skin : _file.skins) { GLTFAccessor& indicesAccessor = _file.accessors[skin.inverseBindMatrices]; QVector matrices; - addArrayOfFromAccessor(indicesAccessor, matrices); + addArrayFromAccessor(indicesAccessor, matrices); inverseBindMatrixValues.push_back(matrices.toStdVector()); } } @@ -821,7 +821,7 @@ void GLTFSerializer::getSkinInverseBindMatrices(std::vector>& void GLTFSerializer::generateTargetData(int index, float weight, QVector& returnVector) { GLTFAccessor& accessor = _file.accessors[index]; QVector storedValues; - addArrayOfFromAccessor(accessor, storedValues); + addArrayFromAccessor(accessor, storedValues); for (int n = 0; n < storedValues.size(); n = n + 3) { returnVector.push_back(glm::vec3(weight * storedValues[n], weight * storedValues[n + 1], weight * storedValues[n + 2])); } @@ -849,7 +849,7 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::VariantHash& nodecount = 0; foreach(auto &node, _file.nodes) { // collect node transform - _file.nodes[nodecount].transforms.push_back(getModelTransform(node)); + _file.nodes[nodecount].transforms.push_back(getModelTransform(node)); int parentIndex = parents[nodecount]; while (parentIndex != -1) { const auto& parentNode = _file.nodes[parentIndex]; @@ -1038,7 +1038,7 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::VariantHash& QVector weights; int weightStride = 0; - bool success = addArrayOfFromAccessor(indicesAccessor, indices); + bool success = addArrayFromAccessor(indicesAccessor, indices); if (!success) { qWarning(modelformat) << "There was a problem reading glTF INDICES data for model " << _url; @@ -1058,7 +1058,7 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::VariantHash& GLTFAccessor& accessor = _file.accessors[accessorIdx]; if (key == "POSITION") { - success = addArrayOfFromAccessor(accessor, vertices); + success = addArrayFromAccessor(accessor, vertices); if (!success) { qWarning(modelformat) << "There was a problem reading glTF POSITION data for model " << _url; continue; @@ -1069,7 +1069,7 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::VariantHash& continue; } } else if (key == "NORMAL") { - success = addArrayOfFromAccessor(accessor, normals); + success = addArrayFromAccessor(accessor, normals); if (!success) { qWarning(modelformat) << "There was a problem reading glTF NORMAL data for model " << _url; continue; @@ -1080,7 +1080,7 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::VariantHash& continue; } } else if (key == "TANGENT") { - success = addArrayOfFromAccessor(accessor, tangents); + success = addArrayFromAccessor(accessor, tangents); if (!success) { qWarning(modelformat) << "There was a problem reading glTF TANGENT data for model " << _url; continue; @@ -1095,7 +1095,7 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::VariantHash& continue; } } else if (key == "TEXCOORD_0") { - success = addArrayOfFromAccessor(accessor, texcoords); + success = addArrayFromAccessor(accessor, texcoords); if (!success) { qWarning(modelformat) << "There was a problem reading glTF TEXCOORD_0 data for model " << _url; continue; @@ -1106,7 +1106,7 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::VariantHash& continue; } } else if (key == "TEXCOORD_1") { - success = addArrayOfFromAccessor(accessor, texcoords2); + success = addArrayFromAccessor(accessor, texcoords2); if (!success) { qWarning(modelformat) << "There was a problem reading glTF TEXCOORD_1 data for model " << _url; continue; @@ -1117,7 +1117,7 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::VariantHash& continue; } } else if (key == "COLOR_0") { - success = addArrayOfFromAccessor(accessor, colors); + success = addArrayFromAccessor(accessor, colors); if (!success) { qWarning(modelformat) << "There was a problem reading glTF COLOR_0 data for model " << _url; continue; @@ -1132,7 +1132,7 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::VariantHash& continue; } } else if (key == "JOINTS_0") { - success = addArrayOfFromAccessor(accessor, joints); + success = addArrayFromAccessor(accessor, joints); if (!success) { qWarning(modelformat) << "There was a problem reading glTF JOINTS_0 data for model " << _url; continue; @@ -1151,7 +1151,7 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::VariantHash& continue; } } else if (key == "WEIGHTS_0") { - success = addArrayOfFromAccessor(accessor, weights); + success = addArrayFromAccessor(accessor, weights); if (!success) { qWarning(modelformat) << "There was a problem reading glTF WEIGHTS_0 data for model " << _url; continue; @@ -1252,15 +1252,10 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::VariantHash& const float ALMOST_HALF = 0.499f; int numVertices = mesh.vertices.size() - prevMeshVerticesCount; - // Append new cluster indices and weights for this mesh part - for (int i = 0; i < numVertices * WEIGHTS_PER_VERTEX; i++) { - mesh.clusterIndices.push_back(mesh.clusters.size() - 1); - mesh.clusterWeights.push_back(0); - } - - for (int c = 0; c < clusterJoints.size(); c++) { - mesh.clusterIndices[prevMeshClusterIndexCount+c] = originalToNewNodeIndexMap[_file.skins[node.skin].joints[clusterJoints[c]]]; - } + mesh.clusterIndices.resize(mesh.clusterIndices.size() + + numVertices * WEIGHTS_PER_VERTEX); + mesh.clusterWeights.resize(mesh.clusterWeights.size() + + numVertices * WEIGHTS_PER_VERTEX); // normalize and compress to 16-bits for (int i = 0; i < numVertices; ++i) { @@ -1268,15 +1263,21 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::VariantHash& float totalWeight = 0.0f; for (int k = j; k < j + WEIGHTS_PER_VERTEX; ++k) { + mesh.clusterIndices[prevMeshClusterIndexCount + k] = + originalToNewNodeIndexMap[_file.skins[node.skin].joints[clusterJoints[k]]]; + totalWeight += clusterWeights[k]; } if (totalWeight > 0.0f) { float weightScalingFactor = (float)(UINT16_MAX) / totalWeight; for (int k = j; k < j + WEIGHTS_PER_VERTEX; ++k) { - mesh.clusterWeights[prevMeshClusterWeightCount+k] = (uint16_t)(weightScalingFactor * clusterWeights[k] + ALMOST_HALF); + mesh.clusterWeights[prevMeshClusterWeightCount + k] = (uint16_t)(weightScalingFactor * clusterWeights[k] + ALMOST_HALF); } } else { - mesh.clusterWeights[prevMeshClusterWeightCount+j] = (uint16_t)((float)(UINT16_MAX) + ALMOST_HALF); + mesh.clusterWeights[prevMeshClusterWeightCount + j] = (uint16_t)((float)(UINT16_MAX) + ALMOST_HALF); + for (int k = j + 1; k < j + WEIGHTS_PER_VERTEX; ++k) { + mesh.clusterWeights[prevMeshClusterWeightCount + k] = 0; + } } } } @@ -1678,7 +1679,7 @@ bool GLTFSerializer::addArrayOfType(const hifi::ByteArray& bin, int byteOffset, } template -bool GLTFSerializer::addArrayOfFromAccessor(GLTFAccessor& accessor, QVector& outarray) { +bool GLTFSerializer::addArrayFromAccessor(GLTFAccessor& accessor, QVector& outarray) { bool success = true; if (accessor.defined["bufferView"]) { diff --git a/libraries/fbx/src/GLTFSerializer.h b/libraries/fbx/src/GLTFSerializer.h index e7dc76e084..4d72805863 100755 --- a/libraries/fbx/src/GLTFSerializer.h +++ b/libraries/fbx/src/GLTFSerializer.h @@ -841,7 +841,7 @@ private: QVector& outarray, int accessorType, int componentType); template - bool addArrayOfFromAccessor(GLTFAccessor& accessor, QVector& outarray); + bool addArrayFromAccessor(GLTFAccessor& accessor, QVector& outarray); void retriangulate(const QVector& in_indices, const QVector& in_vertices, const QVector& in_normals, QVector& out_indices,