#156742 - brett47 - Tue May 13, 2008 3:24 pm
I've been developing some 3d stuff on the DS, making use of quadtrees for partitioning the 3d world. However a 16kb stack isn't exactly a friendly match for recursive functions so I wanted to go for an non-recursive solution. However the algorithm to do so evades me as my brain gets lost trying to work out the solution. Here's my recursive function which is simple enough:
Here's my effort so far, it really starts to get messier towards the end of the function at the point where you have to start thinking about coming back down the tree. Any suggestions?
Code: |
void RenderQuadtreeNode(CQuadtree* pNode)
{ if (pNode == NULL) { return; } if (pNode->IsLeaf == true) { DrawNode(); } else { CQuadtree **pSubNodes = pNode->GetSubNodes(); RenderQuadtreeNode(pSubNodes[FRONTLEFT]); RenderQuadtreeNode(pSubNodes[FRONTRIGHT]); RenderQuadtreeNode(pSubNodes[BACKLEFT]); RenderQuadtreeNode(pSubNodes[BACKRIGHT]); } } |
Here's my effort so far, it really starts to get messier towards the end of the function at the point where you have to start thinking about coming back down the tree. Any suggestions?
Code: |
void RenderQuadTree(CQuadtree* pNode) // Pass in root node
{ int Level = 0; int CurrentChild[MaxLevels]; // Set these all to 0 if (pNode == NULL) { return; } // Process the root node CQuadtree **pSubNodes = pNode->GetSubNodes(); pNode = pSubNodes[CurrentChild[Level]]; Level = 1; while (Level > 0) { if (pNode->IsLeaf == false) { // Get children and go up tree pSubNodes = pNode->GetSubNodes(); pNode = pSubNodes[CurrentChild[Level]]; Level++; } else { if (pNode != NULL) { // The node has polygons to draw DrawNode(); } // At this stage need to go across to next child Level--; CurrentChild[Level]++; if (CurrentChild[Level] > 3) { Level--; CurrentChild[Level]++; } Level++; } } } |