I have a quadtree which organize my terrain chunks (heightmap) and all the static objects of my 3D world. I use Axially Aligned Bounding Box as a volume for each node of the quadtree. Due to the static nature of the objects, the quadtree will not be modified during the execution of the program. The quadtree is generated on a homemade editor so I need to implement a system that writes/reads the entire tree from a binary file.

How should I organize the nodes in the binary file (what order...) and how to build the quadtree when I read the file?

function QuadNode ( box, children )
{
this.boundingBox = box;
this.children = children; // undefined if leef node
this.objects = [];
}


I precise that my quadtree may be incomplete and that any node can have objects attached to it. Moreover, the extents of the AABB will not have X, Z components in the file as those can be computed during the parsing.

So the scheme for a node in the binary file might be:

struct QuadNode {
struct AABB {
float         (MinHeight)
float         (MaxHeight)
}
uint32            (NbObjects)
struct Object {
...
} [NbObjects]
}


Walk the quadtree recursively when saving:

save_node(node n) {
write_bbox(n.bbox)
write_int(n.objects.length)
for (object in n.objects) {
write_object(object)
}
write_int(n.children.length)
for (child in n.children) {
save_node(child)
}
}



read_node() {
node n
while (num_objs--) {