1
\$\begingroup\$

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.

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

\$\endgroup\$
6
  • \$\begingroup\$ I'm not sure what's wrong with your code. To answer your question, the easiest way to put the items back in the tree correctly after a partition is to remove all the items in the node that was partitioned and simply re-add them to the tree, using your regular add method. Don't try anything fancy. \$\endgroup\$ Commented May 5, 2015 at 17:21
  • \$\begingroup\$ Thanks for the comment! That's what I thought, I use: data_.clear(); to remove all of the objects from the parent node, and I assume child_node->data_.push_back((*node_data)); is pushing the data from the parent to the child; but if I comment out that whole for loop, the program is the exact same, nothing changes. \$\endgroup\$ Commented May 5, 2015 at 17:35
  • \$\begingroup\$ You need to attach the debugger. Ensure that your Partition method is not clearing out the items or something else is going wrong elsewhere. \$\endgroup\$ Commented May 5, 2015 at 17:38
  • \$\begingroup\$ The only time data is cleared out is in that insert function. The way the tree is built is through main, the tree is deleted every frame and then rebuilt, because I was told and later on read some material saying that this was way easier. The Partition function creates new instances of the octant nodes, and adds them into the local nodes vector. The Destructor for the node class only deletes the local node variables. \$\endgroup\$ Commented May 5, 2015 at 17:43
  • \$\begingroup\$ Yes, so attach the debugger and find out why your for loop does nothing. \$\endgroup\$ Commented May 5, 2015 at 17:46

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.