2

I am creating a quad tree for my 3d terrain as I only need to worry about the Z and X axis and I'm having some trouble wrapping my head around how to actually traverse down once the tree is created.

I have created the tree and they are stored in my QuadTree object as QuadNode objects. Each QuadNode represents a 'Quad' of the quad tree. It takes the terrain and figures out whether it needs subdividing into new nodes until it finds the bottom-most level nodes and there is a sufficient amount of vertices per node.

I have stored a vertex/index buffer in each node object, but they only get populated once they reach the bottom-most nodes. So the buffers I am trying to obtain (to get the buffers to draw) are right at the bottom of the tree.

Now I can do this fairly simply with a basic tree that just has 4 nodes from the root, but as the tree gets bigger, I get confused. Here is an image to demonstrate

enter image description here

I store

  • How many levels are in the quad tree (to perhaps help with searching, like traverse to level 6)
    • The total node count

The buffers are stored in the bottom-most nodes. Does anyone have an example function or pseudocode as how I would make a function to traverse down the tree given a specific level and it would give me the nodes for that level? Or is there a better approach?

kbz
  • 984
  • 2
  • 12
  • 30
  • 1
    The number of nodes at a level would be `4^n + 4^(n-1) + 4^(n-2)` and so forth until `n == 0`. So at level 2, you'd have `4^2 + 4^1 + 4^0 = 21`. At level 3 you'd have `21 + 4^3 = 85`. At level 4 you'd have `85 + 4^4 = 341`, and so on. –  Jan 18 '18 at 10:02

1 Answers1

2

Although it's not what I'm looking for, I've found this is how you traverse to the bottom nodes and seems to work nicely for what I want to do

How to iterating a Quad/Oct tree

void drawQuadtreeNodes()
{
    drawNode(quadtree->getRoot());
}

void drawNode(QuadNode * node)
{
    if (node->hasNodes) {
        drawNode(node->nodes[0]);
        drawNode(node->nodes[1]);
        drawNode(node->nodes[2]);
        drawNode(node->nodes[3]);
    }
    else {
        //bottom node
    }
}
kbz
  • 984
  • 2
  • 12
  • 30