• Create Account

Banner advertising on our site currently available from just \$5!

# QuadTree Terrain with Odd # of Vertices

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1Darkbouncer4689  Members   -  Reputation: 110

Like
0Likes
Like

Posted 01 September 2011 - 09:50 PM

Hey all,

I'm implementing a quadtree for my terrain as the first step in GeoMipMap LoD. The problem I'm facing is that for GeoMipMap LoD one must have 2^n + 1 vertices along the width and length of the terrain. Since quadtree's split evenly into 4 boxes on each step, it would mean that I will have a huge number of triangles that get intersected right down the middle each time. That is not very ideal =(

As quadtree's and GeoMipMap LoD go hand in hand, I'm sure there must be an elegant solution for this. Anyone have any resources that I could check out?

### #2Trienco  Crossbones+   -  Reputation: 2256

Like
2Likes
Like

Posted 01 September 2011 - 10:18 PM

Hey all,

I'm implementing a quadtree for my terrain as the first step in GeoMipMap LoD. The problem I'm facing is that for GeoMipMap LoD one must have 2^n + 1 vertices along the width and length of the terrain. Since quadtree's split evenly into 4 boxes on each step, it would mean that I will have a huge number of triangles that get intersected right down the middle each time. That is not very ideal =(

It is very ideal, because you don't split the vertices, but the the area between vertices and as you will notice, every chunk of terrain will obviously share some vertices with its neighbor. Yes, you end up duplicating a few vertices or indices (depending on your approach).

Also, there is no point in starting with the quadtree for this as you can do implement it completely without any trees. In fact, the first time around I ended up making it slower with a quadtree unless you had a few thousand chunks.

Thinks to avoid were:

-a "naive" tree implementation using pointers and wasting a ton of time chasing pointers to collect leaf nodes. Since your terrain is always a full quad tree, you don't need any pointers at all. Store each row of the tree consecutively as array and going from node to first child is a matter of (index * 4 + 1). Knowing the depth of the tree, once a node is fully visible, you can calculate the offset and number of leaf nodes and directly get to them.

-don't try to be super precise in your culling. The false positives of using spheres are more than made up for with the faster frustum test.

But first, the interesting part is deciding how you want to connect chunks of different lod. You can be lazy and just use skirts or create index buffers for connectors. Also, there is no point in storing all three coordinates per vertex. You can use offsets per chunk and a single set of x/z coordinates and index buffers. A second buffer holds only y values. That should save enough memory to allow for fairly large terrains.
f@dzhttp://festini.device-zero.de

### #3Darkbouncer4689  Members   -  Reputation: 110

Like
0Likes
Like

Posted 01 September 2011 - 11:31 PM

I thought when you have triangles that get intersected they would just get stuck at the top root node?

If I understand what you're saying, the solution is to split an intersected triangle into two triangles that will each be fully contained in their respected boxes?

### #4Infernal-rk  Members   -  Reputation: 135

Like
1Likes
Like

Posted 02 September 2011 - 11:21 AM

When you have 2^n+1 vertices you will have vertices along the edge of any 2^n division.

. . .
. . .
. . .

That is the simplest. Your leaf level triangles will always fall on boundaries. Now, higher levels in the tree will indeed be problematic. This is why when I wrote a geomimap terrain renderer I dropped the quadtree for a simple array of patches evenly chopping the world. Each having a set of index buffers defining the LOD level triangles. Your neighbor determination becomes trivial and frustum culling isn't that big a deal against the patch boundaries. Your LOD level choice is also trivial.

I have seen others want to use quadtrees as well but I also believe the 2d or 1d with fancy indicing arrays are easier to use for this style of LOD.

### #5Trienco  Crossbones+   -  Reputation: 2256

Like
1Likes
Like

Posted 02 September 2011 - 12:56 PM

If I understand what you're saying, the solution is to split an intersected triangle into two triangles that will each be fully contained in their respected boxes?

It was already mentioned, but just to be sure: there will never be "intersected triangles". You select a chunk size (32x32 should be the absolute minimum or the overhead for draw calls will kill whatever benefit the lower lod might give you). Each chunk is a leaf node in your tree. For nodes form a node in the next level of the tree. At no point should your tree care about what a vertex is, since it only deals with groups of chunks.

For a map size of 1024x1024 or less you will also have to really optimize your tree to make it faster than just culling every single chunk. In fact, you should do that and get the rest working before worrying about it. Changing culling to use a tree later on shouldn't require huge changes.
f@dzhttp://festini.device-zero.de

### #6Darkbouncer4689  Members   -  Reputation: 110

Like
0Likes
Like

Posted 02 September 2011 - 08:44 PM

Thanks for the tips guys. I was following a quadtree terrain tutorial at http://www.rastertek.com/tertut05.html, but I'm beginning to think it it not very good unfortunately.

His method for constructing the quadtree is to have some maximum number of triangles that you want in each node, and then to keep dividing until you get down to a node with less than that max number. He then performs a geometry check for each triangle in the terrain to see if it is contained in the nodes box, and if so adds it to the quadtree's node.

I'm thinking that this isn't a very nice design and I could, as Trienco has suggested, recursively break my terrain into blocks of 4 pages until I get to some minimum page size (such as 32x32). I could then just add each page to the nodes of the quadtree in hierarchical order. It seems like this design would be much faster and less code, but I haven't gotten into the details of it yet so I'm not positive it will work.

I'll keep at it until I have a nice design!

Update:
After spending a few hours I was able to implement the design above. I am pretty happy with it. I feel like it is a better design than the tutorial I was following. Using his method a 1024x1024 terrain grid would require around 1 million triangle/box containment tests, where as this method could build a quad tree for even a 4096x4096 terrain pretty fast.

### #7Trienco  Crossbones+   -  Reputation: 2256

Like
4Likes
Like

Posted 03 September 2011 - 02:07 AM

Actually I suggested the opposite. There is no breaking or dividing, because you don't build your tree top down, you build it bottom up. You already use a terrain that is power of two + 1, so any chunk size that is a power of two will work.

Your tree isnt about vertices, polygons or anything else related to actual geometry. It contains nodes and at the leaf nodes it has a magical black box that is called a chunk (or patch or Billy).

A node stores nothing but the center coordinates and it's radius (or bounding box if you prefer). The tree is created by calculating the number of nodes in it (since you know the terrain and chunk size, the depth and # of nodes is already known). Create a vector of n nodes. So let's pretend your terrain is 128x128 and your chunk size is 32x32 (or any other case where the chunk size is 1/4 of the terrain size).

You end up with a tree depth of log2(128*2) - log2(32) (since you don't have a log2 function, you will use log(x)/log(2))
Ergo 8 - 5 = 3 levels for your tree (counting the single root node of course).

So the number of nodes in your quadtree is (4^3 - 1) / (4 - 1) = 21

The first leaf index you get by determining the number of nodes of a tree with one less level: (4 ^ (3-1)) - 1) / (4 - 1) = (16 - 1) / 3 = 5.

So you know your leaves begin at index 5 and that there are 21 - 5 = 16 leaf nodes (ie. chunks in your terrain). Though you should have already figure that out anyway, since it is (mapSize/chunkSize)^2.

You will store your chunks in a separate array, but in the pattern you see in the tree (starting at node 5).

Let's draw it and count. Nodes with index 0-20.

If you would draw this from the "side", you will notice at each level of a tree is consecutively indexed:
Level 0: 0
Level 1: 1, 2, 3, 4
Level 2: 5 ... 20

You will also notice that to get from a node to its first child, you simply do (index * 4) + 1. To get the first child of node 3 you get (3*4)+1 = 13. To get the nth child (0-3) you do (index + n) * 4 +1.

Truncation of integers also lets you go back up. Parent of a node is (index - 1)/4. So parent of 12 is (12-1)/4 = 2.75 = 2.

You can use bitshifts by 2, but unless you compiler is completely useless it will turn all the *4 and /4 into <<2 and >>2 anyway and spare you from messing up your code.

Now the part that will save you a lot of time when a node is completely visible. You can calculate the offset of the first leaf node of any node in particular. I was lazy figuring out the math to get the first leaf for a node, so I just do

unsigned GetLeaves(unsigned& node)
{
int num=1;
while (node < firstLeaf)
{
++(node<<=2);
num<<=2;
}
return num;
}


To get the index into your chunk array you subtract the index of the first leaf node you calculated above.

void drawAllLeaves(unsigned node)
{
unsigned numChunks = GetLeaves(node);
unsigned chunkIndex = node - firstLeafIndex;
while (numChunks--)
{
draw( chunks[chunkIndex++] );
}
}


Maybe this confused you enough to forget about that tree for now and get the geomipmap terrain working before worrying about optimizing the culling process (which is pretty much the only thing this is good for... that and ray picking).
f@dzhttp://festini.device-zero.de

### #8Darkbouncer4689  Members   -  Reputation: 110

Like
0Likes
Like

Posted 03 September 2011 - 01:43 PM

Wow, great post. Thank you very much, I'll start implementing this today =)

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS