Ocular Engine
BVHSceneTree.hpp
1 
17 #pragma once
18 #ifndef __H__OCULAR_CORE_SCENE_BVH_SCENE_TREE__H__
19 #define __H__OCULAR_CORE_SCENE_BVH_SCENE_TREE__H__
20 
21 #include "ISceneTree.hpp"
22 #include "SceneObject.hpp"
23 #include "BVHSceneNode.hpp"
24 
25 #include <vector>
26 #include <utility>
27 
28 //------------------------------------------------------------------------------------------
29 
34 namespace Ocular
35 {
40  namespace Core
41  {
78  class BVHSceneTree : public ISceneTree
79  {
80  typedef std::pair<uint64_t, SceneObject*> MortonPair;
81 
82  public:
83 
84  BVHSceneTree();
85  virtual ~BVHSceneTree();
86 
87  //------------------------------------------------------------
88  // Inherited Methods
89  //------------------------------------------------------------
90 
91  virtual void restructure() override;
92  virtual void destroy() override;
93  virtual void addObject(SceneObject* object) override;
94  virtual void addObjects(std::vector<SceneObject*> const& objects) override;
95  virtual bool removeObject(SceneObject* object) override;
96  virtual void removeObjects(std::vector<SceneObject*> const& objects) override;
97  virtual void getAllObjects(std::vector<SceneObject*>& objects) const override;
98  virtual void getAllVisibleObjects(Math::Frustum const& frustum, std::vector<SceneObject*>& objects) const override;
99  virtual void getIntersections(Math::Ray const& ray, std::vector<SceneObject*>& objects) const override;
100  virtual void getIntersections(Math::BoundsSphere const& bounds, std::vector<SceneObject*>& objects) const override;
101  virtual void getIntersections(Math::BoundsAABB const& bounds, std::vector<SceneObject*>& objects) const override;
102  virtual void getIntersections(Math::BoundsOBB const& bounds, std::vector<SceneObject*>& objects) const override;
103  virtual void setDirty(UUID const& uuid) override;
104 
105  virtual SceneTreeType getType() const override;
106 
107  protected:
108 
115  void rebuild();
116 
123  void insertNewObjects();
124 
128  void updateDirtyNodes();
129 
133  bool rebuildNeeded() const;
134 
138  void destroyNode(BVHSceneNode* node) const;
139 
144  void insertObject(SceneObject* object);
145 
146  //------------------------------------------------------------
147  // Traversal Methods
148  //------------------------------------------------------------
149 
158  BVHSceneNode* findParent(BVHSceneNode* node, SceneObject* object) const;
159 
168  BVHSceneNode* findNearest(BVHSceneNode* node, uint64_t const& morton) const;
169 
176  void findVisible(BVHSceneNode* node, Math::Frustum const& frustum, std::vector<SceneObject*>& objects) const;
177 
185  void findIntersections(BVHSceneNode* node, Math::Ray const& ray, std::vector<std::pair<SceneObject*, float>>& objects) const;
186 
194  void findIntersections(BVHSceneNode* node, Math::BoundsSphere const& bounds, std::vector<SceneObject*>& objects) const;
195 
203  void findIntersections(BVHSceneNode* node, Math::BoundsAABB const& bounds, std::vector<SceneObject*>& objects) const;
204 
212  void findIntersections(BVHSceneNode* node, Math::BoundsOBB const& bounds, std::vector<SceneObject*>& objects) const;
213 
214  //------------------------------------------------------------
215  // Build Methods
216  //------------------------------------------------------------
217 
221  void build();
222 
227  void createMortonPairs(std::vector<MortonPair>& pairs) const;
228 
239  BVHSceneNode* generateTree(BVHSceneNode* parent, std::vector<MortonPair> const& pairs, uint32_t first, uint32_t last) const;
240 
250  uint32_t findSplit(std::vector<MortonPair> const& pairs, uint32_t first, uint32_t last) const;
251 
256  void fitNodeBounds(BVHSceneNode* node) const;
257 
258  private:
259 
260  bool m_IsDirty;
261 
262  BVHSceneNode* m_Root;
263 
264  std::vector<BVHSceneNode*> m_DirtyNodes;
265  std::vector<SceneObject*> m_AllObjects;
266  };
267  }
271 }
276 //------------------------------------------------------------------------------------------
277 
278 #endif
void insertNewObjects()
Definition: BVHSceneTree.cpp:359
void insertObject(SceneObject *object)
Definition: BVHSceneTree.cpp:407
Definition: Ray.hpp:47
virtual bool removeObject(SceneObject *object) override
Definition: BVHSceneTree.cpp:106
void createMortonPairs(std::vector< MortonPair > &pairs) const
Definition: BVHSceneTree.cpp:689
virtual void getAllVisibleObjects(Math::Frustum const &frustum, std::vector< SceneObject * > &objects) const override
Definition: BVHSceneTree.cpp:253
uint32_t findSplit(std::vector< MortonPair > const &pairs, uint32_t first, uint32_t last) const
Definition: BVHSceneTree.cpp:784
BVHSceneNode * findNearest(BVHSceneNode *node, uint64_t const &morton) const
Definition: BVHSceneTree.cpp:530
void destroyNode(BVHSceneNode *node) const
Definition: BVHSceneTree.cpp:395
Definition: Frustum.hpp:57
Note: Once this library is made dynamic, this will no longer be needed.
Definition: Common.hpp:70
Definition: BoundsOBB.hpp:56
virtual void addObject(SceneObject *object) override
Definition: BVHSceneTree.cpp:81
virtual void getIntersections(Math::Ray const &ray, std::vector< SceneObject * > &objects) const override
Definition: BVHSceneTree.cpp:259
virtual SceneTreeType getType() const override
Definition: BVHSceneTree.cpp:319
void findVisible(BVHSceneNode *node, Math::Frustum const &frustum, std::vector< SceneObject * > &objects) const
Definition: BVHSceneTree.cpp:550
Definition: BVHSceneNode.hpp:44
bool rebuildNeeded() const
Definition: BVHSceneTree.cpp:379
void findIntersections(BVHSceneNode *node, Math::Ray const &ray, std::vector< std::pair< SceneObject *, float >> &objects) const
Definition: BVHSceneTree.cpp:576
Definition: SceneObject.hpp:93
void fitNodeBounds(BVHSceneNode *node) const
Definition: BVHSceneTree.cpp:832
Definition: BVHSceneTree.hpp:78
Definition: UUID.hpp:45
void rebuild()
Definition: BVHSceneTree.cpp:328
virtual void removeObjects(std::vector< SceneObject * > const &objects) override
Definition: BVHSceneTree.cpp:239
void build()
Definition: BVHSceneTree.cpp:662
virtual void restructure() override
Definition: BVHSceneTree.cpp:48
virtual void setDirty(UUID const &uuid) override
Definition: BVHSceneTree.cpp:314
virtual void addObjects(std::vector< SceneObject * > const &objects) override
Definition: BVHSceneTree.cpp:90
void updateDirtyNodes()
Definition: BVHSceneTree.cpp:374
BVHSceneNode * findParent(BVHSceneNode *node, SceneObject *object) const
Definition: BVHSceneTree.cpp:506
BVHSceneNode * generateTree(BVHSceneNode *parent, std::vector< MortonPair > const &pairs, uint32_t first, uint32_t last) const
Definition: BVHSceneTree.cpp:749
Definition: BoundsAABB.hpp:63
Definition: BoundsSphere.hpp:52
virtual void getAllObjects(std::vector< SceneObject * > &objects) const override
Definition: BVHSceneTree.cpp:247
virtual void destroy() override
Definition: BVHSceneTree.cpp:69
Definition: ISceneTree.hpp:51