Skip to content

Commit

Permalink
Process model nodes using a queue, rather than recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthayhurst committed Feb 2, 2024
1 parent 8016ae7 commit 1b4c7a1
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/ammonite/models/modelLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include <vector>
#include <queue>

#include <assimp/Importer.hpp>
#include <assimp/scene.h>
Expand Down Expand Up @@ -104,17 +105,27 @@ namespace ammonite {
textureIds->push_back(textureIdGroup);
}

static void processNode(aiNode* nodePtr, const aiScene* scenePtr,
models::internal::ModelData* modelObjectData,
ModelLoadInfo modelLoadInfo, bool* externalSuccess) {
for (unsigned int i = 0; i < nodePtr->mNumMeshes; i++) {
processMesh(scenePtr->mMeshes[nodePtr->mMeshes[i]], scenePtr, modelObjectData,
modelLoadInfo, externalSuccess);
}
static void processNodes(const aiScene* scenePtr,
models::internal::ModelData* modelObjectData,
ModelLoadInfo modelLoadInfo, bool* externalSuccess) {
std::queue<aiNode*> nodePtrQueue;
nodePtrQueue.push(scenePtr->mRootNode);

//Process root node, then process any more connected to it
while (!nodePtrQueue.empty()) {
aiNode* nodePtr = nodePtrQueue.front();
nodePtrQueue.pop();

//Process meshes
for (unsigned int i = 0; i < nodePtr->mNumMeshes; i++) {
processMesh(scenePtr->mMeshes[nodePtr->mMeshes[i]], scenePtr, modelObjectData,
modelLoadInfo, externalSuccess);
}

for (unsigned int i = 0; i < nodePtr->mNumChildren; i++) {
processNode(nodePtr->mChildren[i], scenePtr, modelObjectData,
modelLoadInfo, externalSuccess);
//Add connected nodes to queue
for (unsigned int i = 0; i < nodePtr->mNumChildren; i++) {
nodePtrQueue.push(nodePtr->mChildren[i]);
}
}
}
}
Expand Down Expand Up @@ -146,7 +157,7 @@ namespace ammonite {
}

//Recursively process nodes
processNode(scenePtr->mRootNode, scenePtr, modelObjectData, modelLoadInfo, externalSuccess);
processNodes(scenePtr, modelObjectData, modelLoadInfo, externalSuccess);
}
}
}
Expand Down

0 comments on commit 1b4c7a1

Please sign in to comment.