Skip to main content
added 51 characters in body
Source Link

EDIT: I am using Visual Studio 2013 with C++11.

EDIT: I am using Visual Studio 2013 with C++11.

Source Link

How to move octree data correctly from parent to child?

This is my first question, so apologies if it is a repeat, I have had a look at the other questions and I still can't seem to find an answer to this.

So I am making a recursive octree in OpenGL. So far I have everything that I think should make up the octree correctly. If I draw all of the partitions (nodes) first (as in, through the constructor of the octree), the octree displays fully and correctly (with a depth level of 3).

However, I am wanting to add partitions only when I need them, I have cubes moving around inside the big root node and they bounce around inside and never leave. The problem I am having is trying to make the partitions appear if the maximum threshold of the nodes' items are exceed. Currently, it only works for the bottom left front node.

Here is where I think the problem is occuring, and also, where I am trying to dynamically add in partitions:

// Inserts data from the main function into here for checks.
void Node::Insert(Data* all_data)
{
    // If there are children.
    if (!nodes_.empty())
    {
        // Check to see if the data collides with the child node.
        Node* child_node = GetChild(all_data);

        // If the data does collide.
        if (child_node != NULL)
        {
            // Recurvisely call the insert function on data and the child node.
            child_node->Insert(all_data);
        }

    // If the amount of data exceeds the limit of the node.
    if (data_.size() >= MAX_ITEMS_)
    {
        // If we have not gone too deep into the octree already.
        if (depth_level_ < MAX_DEPTH_LEVEL)
        {
            // Split the current parent octant up into smaller child octants.
            Partition();

            // THE PROBLEM IS HERE.
            // THIS FOR LOOP DOES NOTHING.
            // Passing in the data from the parent node.
            // Iterate through all of the data in the parent node.
            for (auto node_data = data_.begin(); node_data != data_.end(); node_data++)
            {
                // See if any of the data collides with the NEW child node.
                Node* child_node = GetChild((*node_data));

                // If the data does collide with the NEW child node.
                if (child_node != NULL)
                {
                    // ACTUAL PROBLEM HERE.
                    // Push the data from the parent node into the child node respectively.
                    child_node->data_.push_back((*node_data));
                }
            }

            // Empty the data vector for the parent/this.
            data_.clear();
        }
    }

    // If there are no child nodes.
    // Push the data into the parent/this data vector.
    data_.push_back(all_data);
}

This is all inside of my "Insert(Data* data)" function, this function is called every frame on all of the cubes (data) bouncing around inside. Can anyone see what I am doing wrong? Or point me in the direction of some good material for insertion please? From testing, all I can tell is that the iterator shown above seemingly does nothing to the application.

Thanks.