0

I have a mesh class Chunk that I instantiate for every leaf node in an octree. As of right now I create an std::vector<Chunk*> and create chunks based on the positions of the leaf nodes. However, when I try to implement a Chunk* data member in the Octree class, inserting the data and unloading it (which must be done eventually) causes a crash.

#ifndef Octree_H
#define Octree_H

#include <cstddef>
#include <unordered_set>
#include <glm.hpp>
#include "Chunk.hpp"

namespace Camera {

    class Octree {

        // Physical position/size. This implicitly defines the bounding 
        // box of this node
        glm::ivec3 _origin;         // The physical center of this node
        glm::vec3 _halfDimension;  // Half the width/height/depth of this node
        // The tree has up to eight _children and can additionally store
        // a point, though in many applications only, the leaves will store _data.
        Octree *_children[8]; // Pointers to child octants

        public:
        Chunk* data;
        static std::unordered_set<Octree*> leafNodes;

        Octree(const glm::ivec3& _origin, const glm::vec3& _halfDimension);
        Octree(const Octree& copy);
        ~Octree();

        double getDistanceToNode(const glm::vec3& p);
        glm::ivec3 getOrigin() const;
        int getHalfSize() const;
        int getOctantContainingPoint(const glm::vec3& point) const;
        bool isLeafNode() const;
        void insert(const glm::vec3& camPos);

    };

}
#endif

This is how the data is loaded into the octree every frame in my chunk manager render function

    std::vector<Chunk*> renderList;

    for (auto i : _octree->leafNodes)
    {
        Chunk* c = _chunkLoader.Get(i->getOrigin());

        if (c)
        {
            c->Load();
            i->data = c;
        }
    }

    for (int i = 0; i < _chunks.size(); i++)
        if (camera.getFrustum().boxInFrustum(_chunks[i]->GetAABB()))
            renderList.push_back(_chunks[i]);

    Chunk::Render(cam_data, renderList);

When I eventually call the Unload member function, it causes a the program to crash.

void Chunk::Unload()
{
    glDeleteBuffers(1, &_trianglesID);
    glDeleteBuffers(1, &_normalsID);
    glDeleteBuffers(1, &_IndiceID);
    glDeleteVertexArrays(1, &_VAO);
}

It worked fine until I changed the container for the data. If I had to guess I'd say it has to do with copying pointers which is causing issues.

GraphicsMuncher
  • 4,583
  • 4
  • 35
  • 50
  • 1
    What does the `~OctTree()` do? What is it destroying? If it's destroying anything, there should be an associated user-defined copy constructor and assignment operator that handles copying. Your code is missing the assignment operator. If nothing is being destroyed, then `~OctTree()` shouldn't be there. – PaulMcKenzie Jul 14 '20 at 03:32
  • The destructor deletes all ```_children```. Why is an assignment operator necessary? Is it implicitly called for something? I noticed another error related to the octree where calling ```insert```after loading the mesh causes a crash. ```insert``` is a function that subdivides the octree based on the position of the camera. – HeartUnder8lade Jul 14 '20 at 03:49
  • 1
    It may be called by the compiler. You have no control over this except to turn it off completely, and your class doesn't do that. Thus your class violates the [rule of 3](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three/4172961) – PaulMcKenzie Jul 14 '20 at 03:53
  • 1
    *It worked fine until I changed the container for the data* -- If that container makes object copies, then that is probably one reason why there is a failure. Your `OctTree` does not have proper copy semantics. – PaulMcKenzie Jul 14 '20 at 04:00
  • Thanks! I didn't know about that, I'll make the necessary changes! – HeartUnder8lade Jul 14 '20 at 05:06

0 Answers0