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.