update variables, loops

This commit is contained in:
raveenajain 2019-03-29 18:25:17 -07:00
parent b283bb303d
commit 269b910d24
2 changed files with 56 additions and 53 deletions

View file

@ -734,8 +734,8 @@ glm::mat4 GLTFSerializer::getModelTransform(const GLTFNode& node) {
return tmat; return tmat;
} }
std::vector<QVector<float>> GLTFSerializer::getSkinInverseBindMatrices() { std::vector<std::vector<float>> GLTFSerializer::getSkinInverseBindMatrices() {
std::vector<QVector<float>> inverseBindMatrixValues; std::vector<std::vector<float>> inverseBindMatrixValues;
for (auto &skin : _file.skins) { for (auto &skin : _file.skins) {
GLTFAccessor& indicesAccessor = _file.accessors[skin.inverseBindMatrices]; GLTFAccessor& indicesAccessor = _file.accessors[skin.inverseBindMatrices];
GLTFBufferView& indicesBufferview = _file.bufferviews[indicesAccessor.bufferView]; GLTFBufferView& indicesBufferview = _file.bufferviews[indicesAccessor.bufferView];
@ -748,14 +748,14 @@ std::vector<QVector<float>> GLTFSerializer::getSkinInverseBindMatrices() {
matrices, matrices,
indicesAccessor.type, indicesAccessor.type,
indicesAccessor.componentType); indicesAccessor.componentType);
inverseBindMatrixValues.push_back(matrices); inverseBindMatrixValues.push_back(matrices.toStdVector());
} }
return inverseBindMatrixValues; return inverseBindMatrixValues;
} }
QVector<int> GLTFSerializer::nodeDFS(int n, std::vector<int>& children, bool order) { std::vector<int> GLTFSerializer::nodeDFS(int n, std::vector<int>& children, bool order) {
QVector<int> result; std::vector<int> result;
result.append(n); result.push_back(n);
int begin = 0; int begin = 0;
int finish = (int)children.size(); int finish = (int)children.size();
if (order) { if (order) {
@ -768,9 +768,11 @@ QVector<int> GLTFSerializer::nodeDFS(int n, std::vector<int>& children, bool ord
std::vector<int> nested = _file.nodes[c].children.toStdVector(); std::vector<int> nested = _file.nodes[c].children.toStdVector();
if (nested.size() != 0) { if (nested.size() != 0) {
std::sort(nested.begin(), nested.end()); std::sort(nested.begin(), nested.end());
result.append(nodeDFS(c, nested, order)); for (int n : nodeDFS(c, nested, order)) {
result.push_back(n);
}
} else { } else {
result.append(c); result.push_back(c);
} }
begin < finish ? index++ : index--; begin < finish ? index++ : index--;
} }
@ -779,10 +781,10 @@ QVector<int> GLTFSerializer::nodeDFS(int n, std::vector<int>& children, bool ord
bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::URL& url) { bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::URL& url) {
int nodesSize = _file.nodes.size(); int numNodes = _file.nodes.size();
//Build dependencies //Build dependencies
QVector<QVector<int>> nodeDependencies(nodesSize); QVector<QVector<int>> nodeDependencies(numNodes);
int nodecount = 0; int nodecount = 0;
foreach(auto &node, _file.nodes) { foreach(auto &node, _file.nodes) {
//nodes_transforms.push_back(getModelTransform(node)); //nodes_transforms.push_back(getModelTransform(node));
@ -810,35 +812,40 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::URL& url) {
// initialize order in which nodes will be parsed // initialize order in which nodes will be parsed
QVector<int> nodeQueue; QVector<int> nodeQueue;
int start = 0; nodeQueue.reserve(numNodes);
int end = nodesSize; int rootNode = 0;
int finalNode = numNodes;
if (!_file.scenes[_file.scene].nodes.contains(0)) { if (!_file.scenes[_file.scene].nodes.contains(0)) {
end = -1; rootNode = numNodes - 1;
start = nodesSize - 1; finalNode = -1;
} }
QVector<int> init = _file.scenes[_file.scene].nodes; bool rootAtStartOfList = rootNode < finalNode;
std::sort(init.begin(), init.end()); int nodeListStride = 1;
int begin = 0; if (!rootAtStartOfList) { nodeListStride = -1; }
int finish = init.size();
if (start > end) { QVector<int> initialSceneNodes = _file.scenes[_file.scene].nodes;
begin = init.size() - 1; std::sort(initialSceneNodes.begin(), initialSceneNodes.end());
finish = -1; int sceneRootNode = 0;
int sceneFinalNode = initialSceneNodes.size();
if (!rootAtStartOfList) {
sceneRootNode = initialSceneNodes.size() - 1;
sceneFinalNode = -1;
} }
int index = begin; for (int index = sceneRootNode; index != sceneFinalNode; index += nodeListStride) {
while (index != finish) { int i = initialSceneNodes[index];
int i = init[index];
std::vector<int> children = _file.nodes[i].children.toStdVector(); std::vector<int> children = _file.nodes[i].children.toStdVector();
std::sort(children.begin(), children.end()); std::sort(children.begin(), children.end());
nodeQueue.append(nodeDFS(i, children, start > end)); for (int n : nodeDFS(i, children, !rootAtStartOfList)) {
begin < finish ? index++ : index--; nodeQueue.append(n);
}
} }
// Build joints // Build joints
HFMJoint joint; HFMJoint joint;
joint.distanceToParent = 0; joint.distanceToParent = 0;
hfmModel.jointIndices["x"] = nodesSize; hfmModel.jointIndices["x"] = numNodes;
hfmModel.hasSkeletonJoints = false; hfmModel.hasSkeletonJoints = false;
for (int nodeIndex : nodeQueue) { for (int nodeIndex : nodeQueue) {
@ -858,46 +865,46 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::URL& url) {
joint.translation = extractTranslation(joint.transform); joint.translation = extractTranslation(joint.transform);
joint.name = node.name; joint.name = node.name;
joint.isSkeletonJoint = false;
hfmModel.joints.push_back(joint); hfmModel.joints.push_back(joint);
} }
// Build skeleton // Build skeleton
int matrixIndex = 0;
std::vector<QVector<float>> inverseBindValues = getSkinInverseBindMatrices();
std::vector<glm::mat4> jointInverseBindTransforms; std::vector<glm::mat4> jointInverseBindTransforms;
jointInverseBindTransforms.resize(nodesSize); if (!_file.skins.isEmpty()) {
int matrixIndex = 0;
std::vector<std::vector<float>> inverseBindValues = getSkinInverseBindMatrices();
jointInverseBindTransforms.resize(numNodes);
int jointIndex = end; int jointIndex = finalNode;
while (jointIndex != start) { while (jointIndex != rootNode) {
start < end ? jointIndex-- : jointIndex++; rootAtStartOfList ? jointIndex-- : jointIndex++;
int jOffset = nodeQueue[jointIndex]; int jOffset = nodeQueue[jointIndex];
auto joint = hfmModel.joints[jointIndex]; auto joint = hfmModel.joints[jointIndex];
joint.isSkeletonJoint = false;
if (!_file.skins.isEmpty()) {
hfmModel.hasSkeletonJoints = true; hfmModel.hasSkeletonJoints = true;
for (int s = 0; s < _file.skins.size(); s++) { for (int s = 0; s < _file.skins.size(); s++) {
auto skin = _file.skins[s]; auto skin = _file.skins[s];
joint.isSkeletonJoint = skin.joints.contains(jOffset); joint.isSkeletonJoint = skin.joints.contains(jOffset);
if (joint.isSkeletonJoint) { if (joint.isSkeletonJoint) {
QVector<float> value = inverseBindValues[s]; std::vector<float> value = inverseBindValues[s];
int matrixCount = 16 * skin.joints.indexOf(jOffset); int matrixCount = 16 * skin.joints.indexOf(jOffset);
jointInverseBindTransforms[jointIndex] = jointInverseBindTransforms[jointIndex] =
glm::mat4(value[matrixCount], value[matrixCount + 1], value[matrixCount + 2], value[matrixCount + 3], glm::mat4(value[matrixCount], value[matrixCount + 1], value[matrixCount + 2], value[matrixCount + 3],
value[matrixCount + 4], value[matrixCount + 5], value[matrixCount + 6], value[matrixCount + 7], value[matrixCount + 4], value[matrixCount + 5], value[matrixCount + 6], value[matrixCount + 7],
value[matrixCount + 8], value[matrixCount + 9], value[matrixCount + 10], value[matrixCount + 11], value[matrixCount + 8], value[matrixCount + 9], value[matrixCount + 10], value[matrixCount + 11],
value[matrixCount + 12], value[matrixCount + 13], value[matrixCount + 14], value[matrixCount + 15]); value[matrixCount + 12], value[matrixCount + 13], value[matrixCount + 14], value[matrixCount + 15]);
matrixIndex++; matrixIndex++;
} else { } else {
jointInverseBindTransforms[jointIndex] = glm::mat4(); jointInverseBindTransforms[jointIndex] = glm::mat4();
} }
glm::vec3 bindTranslation = extractTranslation(hfmModel.offset * glm::inverse(jointInverseBindTransforms[jointIndex]));
hfmModel.bindExtents.addPoint(bindTranslation);
} }
glm::vec3 bindTranslation = extractTranslation(hfmModel.offset * glm::inverse(jointInverseBindTransforms[jointIndex])); hfmModel.joints[jointIndex] = joint;
hfmModel.bindExtents.addPoint(bindTranslation);
} }
hfmModel.joints[jointIndex] = joint;
} }
@ -921,8 +928,7 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::URL& url) {
// Build meshes // Build meshes
nodecount = 0; nodecount = 0;
int nodeIndex = start; for (int nodeIndex = rootNode; nodeIndex != finalNode; nodeIndex += nodeListStride) {
while (nodeIndex != end) {
auto& node = _file.nodes[nodeIndex]; auto& node = _file.nodes[nodeIndex];
if (node.defined["mesh"]) { if (node.defined["mesh"]) {
@ -937,14 +943,12 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::URL& url) {
cluster.inverseBindTransform = Transform(cluster.inverseBindMatrix); cluster.inverseBindTransform = Transform(cluster.inverseBindMatrix);
mesh.clusters.append(cluster); mesh.clusters.append(cluster);
} else { } else {
int j = start; for (int j = rootNode; j != finalNode; j += nodeListStride) {
while (j != end) {
HFMCluster cluster; HFMCluster cluster;
cluster.jointIndex = j; cluster.jointIndex = j;
cluster.inverseBindMatrix = jointInverseBindTransforms[j]; cluster.inverseBindMatrix = jointInverseBindTransforms[j];
cluster.inverseBindTransform = Transform(cluster.inverseBindMatrix); cluster.inverseBindTransform = Transform(cluster.inverseBindMatrix);
mesh.clusters.append(cluster); mesh.clusters.append(cluster);
start < end ? j++ : j--;
} }
} }
@ -1148,7 +1152,7 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::URL& url) {
// populate the texture coordinates if they don't exist // populate the texture coordinates if they don't exist
if (mesh.texCoords.size() == 0 && !hfmModel.hasSkeletonJoints) { if (mesh.texCoords.size() == 0 && !hfmModel.hasSkeletonJoints) {
for (int i = 0; i < part.triangleIndices.size(); i++) mesh.texCoords.push_back(glm::vec2(0.0, 1.0)); for (int i = 0; i < part.triangleIndices.size(); i++) { mesh.texCoords.push_back(glm::vec2(0.0, 1.0)); }
} }
mesh.meshExtents.reset(); mesh.meshExtents.reset();
foreach(const glm::vec3& vertex, mesh.vertices) { foreach(const glm::vec3& vertex, mesh.vertices) {
@ -1161,7 +1165,6 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::URL& url) {
} }
nodecount++; nodecount++;
start < end ? nodeIndex++ : nodeIndex--;
} }

View file

@ -712,8 +712,8 @@ private:
hifi::ByteArray _glbBinary; hifi::ByteArray _glbBinary;
glm::mat4 getModelTransform(const GLTFNode& node); glm::mat4 getModelTransform(const GLTFNode& node);
std::vector<QVector<float>> getSkinInverseBindMatrices(); std::vector<std::vector<float>> getSkinInverseBindMatrices();
QVector<int> nodeDFS(int n, std::vector<int>& children, bool order); std::vector<int> nodeDFS(int n, std::vector<int>& children, bool order);
bool buildGeometry(HFMModel& hfmModel, const hifi::URL& url); bool buildGeometry(HFMModel& hfmModel, const hifi::URL& url);
bool parseGLTF(const hifi::ByteArray& data); bool parseGLTF(const hifi::ByteArray& data);