Spatial Data Structures
In this example, I am using a base class Spatial.
It comes with the vertex list and the triangle list copied from Mesh.
It also defines common methods which can be shared by subclasses, such as compute bounding box.
It also defines virtual methods that the subclass needs to implement, such as Insert a triangle, ray intersection test, AABB test.
class Spatial
{
public:
AABB bbox;
// this is not the most efficient way, should use some pointers
// I tried references,but coming with bugs
std::vector<Vertex> vertexList = std::vector<Vertex>();
std::vector<unsigned int> triIdxList = std::vector<unsigned int>();
glm::mat4 matModel;
Spatial() { }
virtual ~Spatial() {}
virtual void Build(const std::vector<Vertex> & vList, const std::vector<unsigned int> & tIdxList, glm::mat4 mat);
void ComputeBounds(AABB &out);
void InsertTriangles();
Triangle & getTriangle(int triIdx);
virtual void Insert(int triIdx) = 0;
virtual bool Raycast(const Ray &ray, HitInfo &outHit) = 0;
virtual void QueryAABB(const AABB &box, std::vector<int> &results) const = 0;
};Last updated