Kemena3D
Loading...
Searching...
No Matches
koctree.h
Go to the documentation of this file.
1
5
6#ifndef KOCTREE_H
7#define KOCTREE_H
8
9#include "kexport.h"
10#include "kdatatype.h"
11
12#include <vector>
13#include <memory>
14#include <unordered_set>
15#include <functional>
16
17namespace kemena
18{
19 class kMesh;
20 class kScene;
21 class kObject;
22
23 // -----------------------------------------------------------------------
24
34
41 {
42 public:
52 void extractFromMatrix(const kMat4 &viewProjection);
53
60 kFrustumTestResult testAABB(const kAABB &aabb) const;
61
63 bool intersectsAABB(const kAABB &aabb) const;
64
65 private:
66 kVec4 planes[6]; // (nx, ny, nz, d) — positive half-space is inside
67 };
68
69 // -----------------------------------------------------------------------
70
84 {
85 public:
91 kOctree(int maxDepth = 6, int maxObjectsPerNode = 8);
92
95
103 void build(kScene *scene);
104
112 std::vector<kMesh*> queryVisible(const kFrustum &frustum) const;
113
115 void clear();
116
118 int getNodeCount() const;
119
121 int getMeshCount() const { return totalMeshes; }
122
131 void traverse(const std::function<void(const kAABB &, int, bool)> &visitor) const;
132
133 private:
140 struct Node
141 {
142 kAABB bounds;
143 std::vector<kMesh*> meshes;
144 std::unique_ptr<Node> children[8];
145 bool leaf = true;
146
148 void subdivide();
149
158 void insert(kMesh *mesh, const kAABB &meshBounds, int depth,
159 int maxDepth, int maxObj);
160
165 void collectAll(std::vector<kMesh*> &out) const;
166
172 void query(const kFrustum &frustum, std::vector<kMesh*> &out) const;
173
179 void traverse(const std::function<void(const kAABB &, int, bool)> &visitor,
180 int depth) const;
181
183 int nodeCount() const;
184 };
185
186 std::unique_ptr<Node> root;
187 int maxDepth;
188 int maxObjectsPerNode;
189 int totalMeshes = 0;
190
196 static void collectMeshes(kObject *node, std::vector<kMesh*> &out);
197 };
198}
199
200#endif // KOCTREE_H
View frustum defined by six planes extracted from a view-projection matrix.
Definition koctree.h:41
bool intersectsAABB(const kAABB &aabb) const
Quick reject: returns false if the AABB is fully outside.
void extractFromMatrix(const kMat4 &viewProjection)
Extract the six frustum planes from a combined view-projection matrix.
kFrustumTestResult testAABB(const kAABB &aabb) const
Test an AABB against the frustum.
Scene-graph node that holds renderable geometry.
Definition kmesh.h:35
Base scene-graph node.
Definition kobject.h:43
~kOctree()
Destroy the octree and release all nodes.
int getMeshCount() const
Get the total number of meshes indexed by the tree.
Definition koctree.h:121
kOctree(int maxDepth=6, int maxObjectsPerNode=8)
Construct an empty octree with the given subdivision limits.
std::vector< kMesh * > queryVisible(const kFrustum &frustum) const
Return all meshes whose world AABB intersects the frustum.
void clear()
Discard all nodes and mesh references.
void traverse(const std::function< void(const kAABB &, int, bool)> &visitor) const
Visit every node in the tree top-down.
void build(kScene *scene)
Rebuild the tree from all loaded meshes in the scene.
int getNodeCount() const
Get the total number of nodes currently in the tree.
Holds all objects, lights, and rendering settings for one scene.
Definition kscene.h:36
Core type aliases, enumerations, structs, and utility functions used throughout the engine.
Symbol visibility / linkage macro for the Kemena3D library.
#define KEMENA3D_API
Definition kexport.h:35
Top-level Kemena3D engine namespace.
Definition kanimation.h:23
glm::vec4 kVec4
4-component float vector.
Definition kdatatype.h:34
glm::mat4 kMat4
4x4 float matrix.
Definition kdatatype.h:39
kFrustumTestResult
Result of testing a volume against a view frustum.
Definition koctree.h:29
@ Outside
Volume lies entirely outside the frustum.
Definition koctree.h:30
@ Inside
Volume lies entirely inside all six planes.
Definition koctree.h:32
@ Intersecting
Volume straddles one or more frustum planes.
Definition koctree.h:31
Axis-aligned bounding box (min/max in the same space).
Definition kdatatype.h:572