Browse Source

Octree WIP

Panagiotis Christopoulos Charitos 7 years ago
parent
commit
678385904e
3 changed files with 37 additions and 0 deletions
  1. 1 0
      src/anki/collision/Aabb.h
  2. 29 0
      src/anki/scene/Octree.cpp
  3. 7 0
      src/anki/scene/Octree.h

+ 1 - 0
src/anki/collision/Aabb.h

@@ -30,6 +30,7 @@ public:
 		, m_min(min)
 		, m_max(max)
 	{
+		ANKI_ASSERT(min.x() == 0.0f && max.x() == 0.0f);
 		ANKI_ASSERT(m_min.xyz() < m_max.xyz());
 	}
 

+ 29 - 0
src/anki/scene/Octree.cpp

@@ -4,10 +4,39 @@
 // http://www.anki3d.org/LICENSE
 
 #include <anki/scene/Octree.h>
+#include <anki/collision/Tests.h>
+#include <anki/collision/Aabb.h>
 
 namespace anki
 {
 
+void Octree::init(const Vec3& sceneMin, const Vec3& sceneMax, U32 maxDepth)
+{
+	ANKI_ASSERT(sceneMin < sceneMax);
+	ANKI_ASSERT(maxDepth > 0);
+
+	m_maxDepth = maxDepth;
+
+	OctreeLeaf& root = newLeaf();
+	root.m_aabbMin = sceneMin;
+	root.m_aabbMax = sceneMax;
+	m_rootLeaf = &root;
+}
+
+void Octree::placeElement(const Aabb& volume, OctreeElement& element)
+{
+	ANKI_ASSERT(m_rootLeaf);
+	placeElementRecursive(volume, element, *m_rootLeaf);
+}
+
+void Octree::placeElementRecursive(const Aabb& volume, OctreeElement& element, OctreeLeaf& parent)
+{
+	ANKI_ASSERT(testCollisionShapes(volume, Aabb(Vec4(parent.m_aabbMin, 0.0f), Vec4(parent.m_aabbMax, 0.0f)))
+				&& "Should be inside");
+
+	const Vec3 center = (parent.m_aabbMax + parent.m_aabbMin) / 2.0f;
+}
+
 OctreeLeaf& Octree::newLeaf()
 {
 	OctreeLeaf* out = nullptr;

+ 7 - 0
src/anki/scene/Octree.h

@@ -52,6 +52,8 @@ public:
 
 	~Octree();
 
+	void init(const Vec3& sceneMin, const Vec3& sceneMax, U32 maxDepth);
+
 	/// Place or re-place an element in the tree.
 	/// @note It's thread-safe.
 	void placeElement(const Aabb& volume, OctreeElement& element);
@@ -62,6 +64,7 @@ public:
 
 private:
 	SceneAllocator<U8> m_alloc;
+	U32 m_maxDepth = 0;
 
 	/// Keep the allocations of leafes tight because we want quite alot of them.
 	class LeafStorage
@@ -76,8 +79,12 @@ private:
 
 	DynamicArray<LeafStorage> m_leafStorages;
 
+	OctreeLeaf* m_rootLeaf = nullptr;
+
 	OctreeLeaf& newLeaf();
 	void releaseLeaf(OctreeLeaf* leaf);
+
+	void placeElementRecursive(const Aabb& volume, OctreeElement& element, OctreeLeaf& parent);
 };
 /// @}