1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of the GameTools Project
|
---|
4 | http://www.gametools.org
|
---|
5 |
|
---|
6 | Author: Martin Szydlowski
|
---|
7 | -----------------------------------------------------------------------------
|
---|
8 | */
|
---|
9 |
|
---|
10 | #ifndef _OgreBvHierarchy_H__
|
---|
11 | #define _OgreBvHierarchy_H__
|
---|
12 |
|
---|
13 | #define BVHNODE_CAST(a) (static_cast<BvHierarchy::Node>(a))
|
---|
14 | #define BVHBRANCH_CAST(a) (static_cast<BvHierarchy::Branch>(a))
|
---|
15 | #define BVHLEAF_CAST(a) (static_cast<BvHierarchy::Leaf>(a))
|
---|
16 | #define BVHNODEPTR_CAST(a) (static_cast<BvHierarchy::Node *>(a))
|
---|
17 | #define BVHBRANCHPTR_CAST(a) (static_cast<BvHierarchy::Branch *>(a))
|
---|
18 | #define BVHLEAFPTR_CAST(a) (static_cast<BvHierarchy::Leaf *>(a))
|
---|
19 |
|
---|
20 | #define BvHierarchy_LOGNAME "BvHierarchyBuild.log"
|
---|
21 |
|
---|
22 | #include <OgreAxisAlignedBox.h>
|
---|
23 | #include <OgreWireBoundingBox.h>
|
---|
24 | #include <OgrePlane.h>
|
---|
25 | #include <OgreVector3.h>
|
---|
26 |
|
---|
27 | #include <OgreRoot.h>
|
---|
28 |
|
---|
29 | #include <stack>
|
---|
30 |
|
---|
31 | #include "OgreBvHierarchyCamera.h"
|
---|
32 | #include "HierarchyInterface.h"
|
---|
33 | #include "OgreKdTree.h"
|
---|
34 |
|
---|
35 | namespace Ogre
|
---|
36 | {
|
---|
37 | class BvHierarchyCamera;
|
---|
38 | class BvhRenderable;
|
---|
39 | struct BvhSplitInfo;
|
---|
40 |
|
---|
41 | class BvhPlaneEvent
|
---|
42 | {
|
---|
43 | public:
|
---|
44 | enum Type
|
---|
45 | {
|
---|
46 | PET_END,
|
---|
47 | PET_ON,
|
---|
48 | PET_START
|
---|
49 | };
|
---|
50 |
|
---|
51 | enum Dimension
|
---|
52 | {
|
---|
53 | PED_X,
|
---|
54 | PED_Y,
|
---|
55 | PED_Z
|
---|
56 | };
|
---|
57 |
|
---|
58 | enum Side
|
---|
59 | {
|
---|
60 | PES_LEFT = 0x01,
|
---|
61 | PES_RIGHT = 0x02,
|
---|
62 | PES_BOTH = PES_LEFT | PES_RIGHT
|
---|
63 | };
|
---|
64 |
|
---|
65 | BvhPlaneEvent(): mRenderable(0), mPosition(Vector3()), mDimension(PED_X), mType(PET_ON)
|
---|
66 | { };
|
---|
67 |
|
---|
68 | BvhPlaneEvent(BvhRenderable *rend, const Vector3& pos, BvhPlaneEvent::Dimension dim, BvhPlaneEvent::Type type):
|
---|
69 | mRenderable(rend), mPosition(pos), mDimension(dim), mType(type)
|
---|
70 | { };
|
---|
71 |
|
---|
72 | ~BvhPlaneEvent() {};
|
---|
73 |
|
---|
74 | // the less operator for plane events
|
---|
75 | // first sort by position, then by dimension and finally by type
|
---|
76 | bool operator < (const BvhPlaneEvent& e) const
|
---|
77 | {
|
---|
78 | if(mPosition[mDimension] < e.mPosition[e.mDimension])
|
---|
79 | {
|
---|
80 | return true;
|
---|
81 | }
|
---|
82 |
|
---|
83 | if(mPosition[mDimension] == e.mPosition[e.mDimension])
|
---|
84 | {
|
---|
85 | if (mDimension < e.mDimension)
|
---|
86 | {
|
---|
87 | return true;
|
---|
88 | }
|
---|
89 | if (mDimension == e.mDimension)
|
---|
90 | {
|
---|
91 | if (mType < e.mType)
|
---|
92 | {
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | return false;
|
---|
99 | };
|
---|
100 |
|
---|
101 | // the equals operator for tree events
|
---|
102 | bool operator == (const BvhPlaneEvent& e) const
|
---|
103 | {
|
---|
104 | return (mPosition[mDimension] == e.mPosition[e.mDimension]) &&
|
---|
105 | (mDimension == e.mDimension) &&
|
---|
106 | (mType == e.mType);
|
---|
107 | };
|
---|
108 |
|
---|
109 | bool equalsType(const BvhPlaneEvent& e, BvhPlaneEvent::Type t)
|
---|
110 | {
|
---|
111 | return (mPosition[mDimension] == e.mPosition[e.mDimension]) &&
|
---|
112 | (mDimension == e.mDimension) &&
|
---|
113 | (mType == t);
|
---|
114 | };
|
---|
115 |
|
---|
116 | void classify(const BvhPlaneEvent& e, BvhPlaneEvent::Side side);
|
---|
117 |
|
---|
118 | BvhPlaneEvent clip(AxisAlignedBox& box, BvhPlaneEvent::Dimension dim);
|
---|
119 |
|
---|
120 | Plane * getSplitPlane() const
|
---|
121 | {
|
---|
122 | Vector3 normal(0,0,0);
|
---|
123 | normal[mDimension] = 1;
|
---|
124 | return new Plane(normal, mPosition);
|
---|
125 | }
|
---|
126 |
|
---|
127 | BvhRenderable * getRenderable() const //??
|
---|
128 | {
|
---|
129 | return mRenderable;
|
---|
130 | };
|
---|
131 |
|
---|
132 | BvhPlaneEvent::Dimension getDimension() const //??
|
---|
133 | {
|
---|
134 | return mDimension;
|
---|
135 | };
|
---|
136 |
|
---|
137 | // DEBUG
|
---|
138 | String print();
|
---|
139 | protected:
|
---|
140 | // event info
|
---|
141 | BvhRenderable * mRenderable;
|
---|
142 | Vector3 mPosition;
|
---|
143 | BvhPlaneEvent::Dimension mDimension;
|
---|
144 | BvhPlaneEvent::Type mType;
|
---|
145 |
|
---|
146 | // ------------------------------------------------------------------------------//
|
---|
147 | // functions to determine the cost of splitting the node parent with the plane
|
---|
148 | // represented by this event
|
---|
149 | // TODO discuss if these functions really belong here, OO & SE - wise
|
---|
150 | // pros: convenient, don't have to pass internal data to the outside
|
---|
151 | // cons: BvhSplitInfo ...
|
---|
152 | public:
|
---|
153 | // compute "global" surface area heuristic (SAH) for the plane represented by this event
|
---|
154 | // use only with priority queue build method
|
---|
155 | void pqSAH(Real globalSA, Real parentSA, int nLeft, int nPlane, int nRight, AxisAlignedBox& parentBox, BvhSplitInfo& split);
|
---|
156 | static Real pqSplitCost(Real p, Real pl, Real pr, int nLeft, int nRight, Real mu);
|
---|
157 |
|
---|
158 | // compute the surface area heuristic (SAH) for the plane represented by this event
|
---|
159 | void SAH(const AxisAlignedBox& parent, int nLeft, int nPlane, int nRight, BvhSplitInfo& split);
|
---|
160 | // the variables determining the cost of a branch traversal (KT) and a leaf intersection (KI)
|
---|
161 | static Real KT;
|
---|
162 | static Real KI;
|
---|
163 | // helper functions
|
---|
164 | static Real splitCost(Real pl, Real pr, int nLeft, int nRight);
|
---|
165 | static Real splitCost(Real pl, Real pr, int nLeft, int nRight, Real mu);
|
---|
166 | static Real surfaceArea(const AxisAlignedBox& box);
|
---|
167 | static Real lookupPenalty(Real p);
|
---|
168 | protected:
|
---|
169 | Real splitBox(const AxisAlignedBox& parent, AxisAlignedBox& left, AxisAlignedBox& right);
|
---|
170 | };
|
---|
171 |
|
---|
172 | // Holds all the important information on a split
|
---|
173 | struct BvhSplitInfo
|
---|
174 | {
|
---|
175 | AxisAlignedBox bleft;
|
---|
176 | AxisAlignedBox bright;
|
---|
177 | int nleft;
|
---|
178 | int nright;
|
---|
179 | Real cost;
|
---|
180 | BvhPlaneEvent::Side side;
|
---|
181 | BvhPlaneEvent event;
|
---|
182 |
|
---|
183 | // DEBUG
|
---|
184 | String print();
|
---|
185 | };
|
---|
186 |
|
---|
187 | typedef std::list<BvhRenderable *> BvhRenderableList;
|
---|
188 | typedef std::list<BvhPlaneEvent> BvhPlaneEventList;
|
---|
189 |
|
---|
190 | class BvHierarchy
|
---|
191 | {
|
---|
192 | protected:
|
---|
193 | class Branch;
|
---|
194 | class Leaf;
|
---|
195 |
|
---|
196 | class Node
|
---|
197 | {
|
---|
198 | public:
|
---|
199 | Node(BvHierarchy * owner, int level, AxisAlignedBox& aabb, Branch * parent):
|
---|
200 | mOwner(owner),
|
---|
201 | mLevel(level),
|
---|
202 | mAABB(aabb),
|
---|
203 | mParent(parent),
|
---|
204 | mWBB(0)
|
---|
205 | { };
|
---|
206 |
|
---|
207 | virtual ~Node()
|
---|
208 | {
|
---|
209 | delete mWBB;
|
---|
210 | };
|
---|
211 |
|
---|
212 | virtual bool isLeaf() const = 0;
|
---|
213 | virtual bool isEmpty() const = 0;
|
---|
214 | virtual bool hasGeometry() const = 0;
|
---|
215 |
|
---|
216 | virtual void mergeLeaves(std::set<Leaf *>& leaves) = 0;
|
---|
217 |
|
---|
218 | // Gets this node's parent (NULL if this is the root).
|
---|
219 | BvHierarchy::Node *getParent(void) const { return mParent; };
|
---|
220 |
|
---|
221 | // Gets the nodes left & right child nodes, or NULL if not present or node is leaf
|
---|
222 | virtual BvHierarchy::Node *getLeftChild(void) const = 0;
|
---|
223 | virtual BvHierarchy::Node *getRightChild(void) const = 0;
|
---|
224 |
|
---|
225 | // add contained objects to render queue
|
---|
226 | virtual void queueVisibleObjects(unsigned long currentFrame,
|
---|
227 | Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes, bool fullVis = false) = 0;
|
---|
228 |
|
---|
229 | // add contained geometry (Entities) to list
|
---|
230 | virtual void getGeometryList(GeometryVector *geometryList) = 0;
|
---|
231 |
|
---|
232 | // create (when necessary), setup and return wire bounding box representing the node
|
---|
233 | WireBoundingBox * getWireBoundingBox()
|
---|
234 | {
|
---|
235 | if (mWBB == 0)
|
---|
236 | mWBB = new WireBoundingBox();
|
---|
237 |
|
---|
238 | if (mOwner->getShowNodes())
|
---|
239 | mWBB->setupBoundingBox(mAABB);
|
---|
240 | else
|
---|
241 | mWBB->setupBoundingBox(mWorldAABB);
|
---|
242 |
|
---|
243 | if (mOwner->getHiLiteLevel() == mLevel)
|
---|
244 | mWBB->setMaterial("BvHierarchy/BoxHiLite");
|
---|
245 | else
|
---|
246 | mWBB->setMaterial("BaseWhiteNoLighting");
|
---|
247 |
|
---|
248 | return mWBB;
|
---|
249 | }
|
---|
250 |
|
---|
251 | // returns the level the node is on
|
---|
252 | int getLevel(void) const { return mLevel; };
|
---|
253 |
|
---|
254 | // functions for the CHC hierarchy interface
|
---|
255 |
|
---|
256 | /** Returns last visited frame id. */
|
---|
257 | unsigned int lastVisited(void) { return mLastVisited; };
|
---|
258 | /** Set to current frame id.
|
---|
259 | @param current frame id.
|
---|
260 | */
|
---|
261 | void setLastVisited(unsigned int frameid) { mLastVisited = frameid; };
|
---|
262 | /** Makes this octree become visible / invisble.
|
---|
263 | @param visible Whether this node is to be made visible or invisible
|
---|
264 | */
|
---|
265 | void setNodeVisible(bool visible) { mVisible = visible; };
|
---|
266 | /** Returns true if this node is marked visible, false otherwise.
|
---|
267 | */
|
---|
268 | bool isNodeVisible(void) { return mVisible; };
|
---|
269 | /** Frame id when this octree was last rendered.
|
---|
270 | @return last rendered frame id
|
---|
271 | */
|
---|
272 | unsigned int lastRendered(void) { return mLastRendered; };
|
---|
273 | /** Sets frame id when this octree was last rendered.
|
---|
274 | @param last rendered frame id
|
---|
275 | */
|
---|
276 | void setLastRendered(unsigned int frameid) { mLastRendered = frameid; };
|
---|
277 | /** Returns real extent of the BvHierarchy, i.e., the merged extent of the bounding boxes.
|
---|
278 | */
|
---|
279 | AxisAlignedBox _getWorldAABB(void) const { return mAABB; };
|
---|
280 | /** Updates bound of the real aabb of BvHierarchy
|
---|
281 | */
|
---|
282 | virtual void _updateBounds(bool recurse = true) = 0;
|
---|
283 |
|
---|
284 | /** bounding box of the node
|
---|
285 | */
|
---|
286 | AxisAlignedBox mAABB;
|
---|
287 |
|
---|
288 | /** bounding box of all objects inside the node
|
---|
289 | */
|
---|
290 | AxisAlignedBox mWorldAABB;
|
---|
291 |
|
---|
292 |
|
---|
293 | protected:
|
---|
294 | BvHierarchy * mOwner;
|
---|
295 | Branch * mParent;
|
---|
296 | int mLevel;
|
---|
297 |
|
---|
298 | WireBoundingBox * mWBB;
|
---|
299 |
|
---|
300 | // for the CHC hierarchy interface
|
---|
301 | unsigned int mLastRendered;
|
---|
302 | unsigned int mLastVisited;
|
---|
303 | bool mVisible;
|
---|
304 | };
|
---|
305 |
|
---|
306 | class Branch : public Node
|
---|
307 | {
|
---|
308 | public:
|
---|
309 | Branch(BvHierarchy * owner, int level, AxisAlignedBox& aabb, Branch * parent,
|
---|
310 | Plane * splitplane, BvhPlaneEvent::Side side):
|
---|
311 | Node(owner, level, aabb, parent),
|
---|
312 | mSplitPlane(splitplane),
|
---|
313 | mLeft(0),
|
---|
314 | mRight(0),
|
---|
315 | mPlaneSide(side)
|
---|
316 | { };
|
---|
317 |
|
---|
318 | virtual ~Branch()
|
---|
319 | {
|
---|
320 | delete mLeft;
|
---|
321 | delete mRight;
|
---|
322 | delete mSplitPlane;
|
---|
323 | };
|
---|
324 |
|
---|
325 | // a branch is not a leaf
|
---|
326 | virtual bool isLeaf() const { return false; };
|
---|
327 |
|
---|
328 | // s branch is empty when it does not have children
|
---|
329 | virtual bool isEmpty() const { return (mLeft == 0 && mRight == 0); }
|
---|
330 |
|
---|
331 | // a branch never has geometry
|
---|
332 | virtual bool hasGeometry() const { return false; };
|
---|
333 |
|
---|
334 | virtual void mergeLeaves(std::set<Leaf *>& leaves)
|
---|
335 | {
|
---|
336 | for (std::set<Leaf *>::iterator it = mLeaves.begin(); it != mLeaves.end(); it++)
|
---|
337 | leaves.insert(*it);
|
---|
338 | }
|
---|
339 |
|
---|
340 | // a branch should have at least one child
|
---|
341 | virtual BvHierarchy::Node * getLeftChild() const { return mLeft; };
|
---|
342 | virtual BvHierarchy::Node * getRightChild() const { return mRight; };
|
---|
343 |
|
---|
344 | virtual void queueVisibleObjects(unsigned long currentFrame,
|
---|
345 | Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes, bool fullVis = false)
|
---|
346 | {
|
---|
347 | if (showBoxes)
|
---|
348 | if (mLevel == mOwner->getHiLiteLevel() || mOwner->getShowAllBoxes())
|
---|
349 | queue->addRenderable(getWireBoundingBox());
|
---|
350 |
|
---|
351 | if (fullVis)
|
---|
352 | for (std::set<Leaf *>::iterator it = mLeaves.begin(); it != mLeaves.end(); it ++)
|
---|
353 | (*it)->queueVisibleObjects(currentFrame, cam, queue, onlyShadowCasters, showBoxes, fullVis);
|
---|
354 | }
|
---|
355 |
|
---|
356 | // a branch has no geometry, do nothing
|
---|
357 | virtual void getGeometryList(GeometryVector *geometryList) { }
|
---|
358 |
|
---|
359 | // branches do not posses geometry => just merge child aabbs
|
---|
360 | virtual void _updateBounds(bool recurse = true)
|
---|
361 | {
|
---|
362 | // reset box
|
---|
363 | mWorldAABB.setNull();
|
---|
364 |
|
---|
365 | // merge box & leaves
|
---|
366 | if (mLeft)
|
---|
367 | {
|
---|
368 | mWorldAABB.merge(mLeft->mWorldAABB);
|
---|
369 | mLeft->mergeLeaves(mLeaves);
|
---|
370 | }
|
---|
371 | if (mRight)
|
---|
372 | {
|
---|
373 | mWorldAABB.merge(mRight->mWorldAABB);
|
---|
374 | mRight->mergeLeaves(mLeaves);
|
---|
375 | }
|
---|
376 |
|
---|
377 | // update parent recursively
|
---|
378 | if (recurse && mParent)
|
---|
379 | mParent->_updateBounds(recurse);
|
---|
380 | }
|
---|
381 |
|
---|
382 | Node * mLeft;
|
---|
383 | Node * mRight;
|
---|
384 | Plane * mSplitPlane;
|
---|
385 | BvhPlaneEvent::Side mPlaneSide;
|
---|
386 | protected:
|
---|
387 | std::set<Leaf *> mLeaves;
|
---|
388 | };
|
---|
389 |
|
---|
390 | class Leaf : public Node
|
---|
391 | {
|
---|
392 | public:
|
---|
393 | Leaf(BvHierarchy * owner, int level, AxisAlignedBox& aabb, Branch *parent):
|
---|
394 | Node(owner, level, aabb, parent)
|
---|
395 | {};
|
---|
396 |
|
---|
397 | virtual ~Leaf();
|
---|
398 |
|
---|
399 | // a leaf is a leaf, dammit
|
---|
400 | virtual bool isLeaf() const { return true; }
|
---|
401 |
|
---|
402 | // a leaf is empty when it does not posses renderables
|
---|
403 | virtual bool isEmpty() const { return mBvhRenderables.empty(); }
|
---|
404 |
|
---|
405 | // a leaf has geometry when it has renderables
|
---|
406 | virtual bool hasGeometry() const { return !mBvhRenderables.empty(); }
|
---|
407 |
|
---|
408 | // a leaf adds itself to the leaf set
|
---|
409 | virtual void mergeLeaves(std::set<Leaf *>& leaves) { leaves.insert(this); }
|
---|
410 |
|
---|
411 | // a leaf never has children
|
---|
412 | virtual BvHierarchy::Node * getLeftChild() const { return 0; };
|
---|
413 | virtual BvHierarchy::Node * getRightChild() const { return 0; };
|
---|
414 |
|
---|
415 | virtual void queueVisibleObjects(unsigned long currentFrame,
|
---|
416 | Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes, bool fullVis = false);
|
---|
417 |
|
---|
418 | virtual void getGeometryList(GeometryVector *geometryList);
|
---|
419 |
|
---|
420 | // update the world aabb based on the contained geometry
|
---|
421 | virtual void _updateBounds(bool recurse = true);
|
---|
422 |
|
---|
423 | virtual void remove(BvhRenderable * rend)
|
---|
424 | {
|
---|
425 | mBvhRenderables.remove(rend);
|
---|
426 | //mBvhRenderables.erase(find(mBvhRenderables.begin(), mBvhRenderables.end(), rend));
|
---|
427 | };
|
---|
428 |
|
---|
429 | virtual void insert(BvhRenderable * rend)
|
---|
430 | {
|
---|
431 | mBvhRenderables.push_back(rend);
|
---|
432 | };
|
---|
433 |
|
---|
434 | BvhRenderableList mBvhRenderables;
|
---|
435 | };
|
---|
436 |
|
---|
437 | struct BvhSubdivisionCandidate
|
---|
438 | {
|
---|
439 | BvhSubdivisionCandidate(BvhPlaneEventList * e, int n, AxisAlignedBox& a,
|
---|
440 | BvHierarchy::Branch * p, Real c, Real d, BvhSplitInfo * b, BvhPlaneEvent::Side s):
|
---|
441 | events(e), nObjects(n), aabb(a), parent(p), cost(c), decrease(d), best(b), side(s)
|
---|
442 | { };
|
---|
443 |
|
---|
444 | bool operator < (const BvhSubdivisionCandidate& rhs) const
|
---|
445 | {
|
---|
446 | return decrease < rhs.decrease;
|
---|
447 | };
|
---|
448 |
|
---|
449 | void operator = (const BvhSubdivisionCandidate& rhs)
|
---|
450 | {
|
---|
451 | decrease = rhs.decrease;
|
---|
452 | cost = rhs.cost;
|
---|
453 | nObjects = rhs.nObjects;
|
---|
454 | side = rhs.side;
|
---|
455 | aabb = rhs.aabb;
|
---|
456 | events = rhs.events;
|
---|
457 | parent = rhs.parent;
|
---|
458 | best = rhs.best;
|
---|
459 | };
|
---|
460 |
|
---|
461 | // DEBUG
|
---|
462 | String print();
|
---|
463 |
|
---|
464 | Real decrease;
|
---|
465 | Real cost;
|
---|
466 | int nObjects;
|
---|
467 | BvhPlaneEvent::Side side;
|
---|
468 | AxisAlignedBox aabb;
|
---|
469 | BvhPlaneEventList *events;
|
---|
470 | BvHierarchy::Branch * parent;
|
---|
471 | BvhSplitInfo * best;
|
---|
472 | };
|
---|
473 |
|
---|
474 | // typedef std::stack<BvhSubdivisionCandidate> SplitCandidatePQ;
|
---|
475 | typedef std::priority_queue<BvhSubdivisionCandidate> SplitCandidatePQ;
|
---|
476 |
|
---|
477 | // nodestack for the stack-based rendering function
|
---|
478 | typedef std::stack<BvHierarchy::Node *> NodeStack;
|
---|
479 | public:
|
---|
480 | friend class BvHierarchyInterface;
|
---|
481 |
|
---|
482 | typedef BvHierarchy::Node * NodePtr;
|
---|
483 | typedef BvHierarchy::Branch * BranchPtr;
|
---|
484 | typedef BvHierarchy::Leaf * LeafPtr;
|
---|
485 |
|
---|
486 | typedef std::list<NodePtr> NodeList;
|
---|
487 | typedef std::set<LeafPtr> LeafSet;
|
---|
488 |
|
---|
489 | struct TreeStats
|
---|
490 | {
|
---|
491 | unsigned int mNumNodes;
|
---|
492 | unsigned int mNumLeaves;
|
---|
493 | unsigned int mNumSceneNodes;
|
---|
494 |
|
---|
495 | void clear(void)
|
---|
496 | {
|
---|
497 | mNumNodes = 0;
|
---|
498 | mNumLeaves = 0;
|
---|
499 | mNumSceneNodes = 0;
|
---|
500 | }
|
---|
501 | };
|
---|
502 |
|
---|
503 | struct FrameStats
|
---|
504 | {
|
---|
505 | unsigned int mTraversedNodes;
|
---|
506 | unsigned int mRenderedNodes;
|
---|
507 | unsigned int mFrustumCulledNodes;
|
---|
508 |
|
---|
509 | void clear(void)
|
---|
510 | {
|
---|
511 | mTraversedNodes = 0;
|
---|
512 | mRenderedNodes = 0;
|
---|
513 | mFrustumCulledNodes = 0;
|
---|
514 | }
|
---|
515 | };
|
---|
516 |
|
---|
517 | enum RenderMethod
|
---|
518 | {
|
---|
519 | BVHRM_INTERNAL,
|
---|
520 | BVHRM_GTP_VFC,
|
---|
521 | BVHRM_GTP_SWC,
|
---|
522 | BVHRM_GTP_CHC,
|
---|
523 | // invalid modes, just for convenience
|
---|
524 | BVHRM_SIZE,
|
---|
525 | BVHRM_NOTSET
|
---|
526 | };
|
---|
527 |
|
---|
528 | enum BuildMethod
|
---|
529 | {
|
---|
530 | BVHBM_RECURSIVE,
|
---|
531 | BVHBM_PRIORITYQUEUE,
|
---|
532 | // invalid modes, just for convenience
|
---|
533 | BVHBM_SIZE,
|
---|
534 | BVHBM_NOTSET
|
---|
535 | };
|
---|
536 |
|
---|
537 |
|
---|
538 | const static int HILITE_OFF = -1;
|
---|
539 |
|
---|
540 | BvHierarchy(int maxdepth, BuildMethod bm);
|
---|
541 | BvHierarchy(int maxdepth, BuildMethod bm, int hilite, bool allboxes, bool shownodes);
|
---|
542 | virtual ~BvHierarchy();
|
---|
543 |
|
---|
544 | // DEBUG
|
---|
545 | void dump(void);
|
---|
546 | Real calcCost(void);
|
---|
547 |
|
---|
548 | // sets the level to highlight or turns it off (when hilite < 0)
|
---|
549 | inline void setHiLiteLevel(int hilite) { mHiLiteLevel = hilite; };
|
---|
550 | inline int getHiLiteLevel(void) { return mHiLiteLevel; };
|
---|
551 |
|
---|
552 | // toggles displaying the BvHierarchy boxes
|
---|
553 | inline void setShowAllBoxes(bool show) { mShowAllBoxes = show; };
|
---|
554 | inline bool getShowAllBoxes(void) { return mShowAllBoxes; };
|
---|
555 |
|
---|
556 | // toggles between displaying the bounding box of the node and
|
---|
557 | // the box of the contained scene nodes
|
---|
558 | inline void setShowNodes(bool show = true) { mShowNodes = show; };
|
---|
559 | inline bool getShowNodes(void) { return mShowNodes; };
|
---|
560 |
|
---|
561 | // changes vis mode (simple/enhanced with NONE/PART/FULL vis)
|
---|
562 | void setEnhancedVis(bool enh);
|
---|
563 | bool getEnhancedVis(void);
|
---|
564 |
|
---|
565 | NodePtr getRoot(void) const { return mBvhRoot; };
|
---|
566 |
|
---|
567 | // insert a new scene node into an existing bvhierarchy
|
---|
568 | void insert(BvhRenderable * rend);
|
---|
569 | // remove a scene node from the tree
|
---|
570 | void remove(BvhRenderable * rend);
|
---|
571 | // function to initialize a kd-tree based on the contents of the scene
|
---|
572 | void build(BvhRenderable * sceneRoot);
|
---|
573 |
|
---|
574 | // test visibility of objects and add to render queue
|
---|
575 | void queueVisibleObjects(BvHierarchyCamera* cam, RenderQueue* queue, bool onlyShadowCasters,
|
---|
576 | bool showBoxes, BvHierarchy::NodeList& visibleNodes);
|
---|
577 |
|
---|
578 | // find visible nodes & place in list
|
---|
579 | //void findVisibleNodes(NodeList& visibleNodes, Camera * cam);
|
---|
580 |
|
---|
581 | /** Recurses the BvHierarchy, adding any nodes intersecting with the
|
---|
582 | box/sphere/volume/ray into the given list.
|
---|
583 | It ignores the exclude scene node.
|
---|
584 | */
|
---|
585 | void findNodesIn(const AxisAlignedBox &box, std::list<SceneNode *> &list, SceneNode *exclude = 0);
|
---|
586 | void findNodesIn(const Sphere &sphere, std::list<SceneNode *> &list, SceneNode *exclude = 0);
|
---|
587 | void findNodesIn(const PlaneBoundedVolume &volume, std::list<SceneNode *> &list, SceneNode *exclude = 0);
|
---|
588 | void findNodesIn(const Ray &ray, std::list<SceneNode *> &list, SceneNode *exclude = 0);
|
---|
589 |
|
---|
590 | // self-explanatory ...
|
---|
591 | int getMaxDepth(void) { return mMaxDepth; }
|
---|
592 | const TreeStats& getTreeStats(void) const { return mTreeStats; }
|
---|
593 | const FrameStats& getFramesStats(void) const { return mFrameStats; }
|
---|
594 | AxisAlignedBox getBox(void) { if (mBvhRoot) return mBvhRoot->mAABB; else return AxisAlignedBox(); }
|
---|
595 | void setBuildMethod(BuildMethod bm) { mBuildMethod = bm; }
|
---|
596 | protected:
|
---|
597 | // init materials, logs and stuff
|
---|
598 | void init();
|
---|
599 | // recursive insert funciton
|
---|
600 | void recInsert(BvHierarchy::Node * node, BvhRenderable * rend);
|
---|
601 | // helper functions for insert
|
---|
602 | void recInsertNew(BvHierarchy::Branch * parent, BvhPlaneEvent::Side side, BvhRenderable * rend);
|
---|
603 | void splitBox(const BvHierarchy::Branch& parent, AxisAlignedBox& left, AxisAlignedBox& right);
|
---|
604 | void rebuildSubtree(BvHierarchy::Node * node, BvhRenderable * rend);
|
---|
605 | // build scene from a list of nodes rather than a hierarchy
|
---|
606 | BvHierarchy::Node * buildFromList(BvhRenderableList& nodelist, BvHierarchy::Branch * parent, AxisAlignedBox& aabb);
|
---|
607 | void addRendToList(BvHierarchy::Node * node, BvhRenderableList& nodelist);
|
---|
608 |
|
---|
609 | // recursively delete empty nodes
|
---|
610 | void recDelete(BvHierarchy::Node * node);
|
---|
611 |
|
---|
612 | // find the best plane for node division
|
---|
613 | BvhSplitInfo * pqFindPlane(BvhPlaneEventList * events, int nObjects, AxisAlignedBox& aabb, Real globalSA);
|
---|
614 |
|
---|
615 | // priority queue based build function
|
---|
616 | BvHierarchy::Node * pqBuild(BvhPlaneEventList& events, int nObjects, AxisAlignedBox& aabb, BvHierarchy::Branch * parent);
|
---|
617 | // recursive build function
|
---|
618 | BvHierarchy::Node * recBuild(BvhPlaneEventList& events, int nObjects, AxisAlignedBox& aabb, BvHierarchy::Branch * parent);
|
---|
619 |
|
---|
620 | // recursive rendering function
|
---|
621 | void recQueueVisibleObjects(BvHierarchy::Node * node, unsigned long currentFrame, BvHierarchyCamera* cam,
|
---|
622 | RenderQueue* queue, bool onlyShadowCasters, bool showBoxes,
|
---|
623 | BvHierarchy::NodeList& visibleNodes, bool fullVis = false);
|
---|
624 |
|
---|
625 | // recursively find visible nodes
|
---|
626 | //void recFindVisibleNodes(BvHierarchy::Node * node, NodeList& visibleNodes, Camera * cam);
|
---|
627 |
|
---|
628 | /** Recurses the BvHierarchy, adding any nodes intersecting with the
|
---|
629 | box/sphere/volume/ray into the given list.
|
---|
630 | It ignores the exclude scene node.
|
---|
631 | */
|
---|
632 | void recFindNodesIn(const AxisAlignedBox &box, std::list<SceneNode *> &list, SceneNode *exclude, Node * node, bool full = false);
|
---|
633 | void recFindNodesIn(const Sphere &sphere, std::list<SceneNode *> &list, SceneNode *exclude, Node * node, bool full = false);
|
---|
634 | void recFindNodesIn(const PlaneBoundedVolume &volume, std::list<SceneNode *> &list, SceneNode *exclude, Node * node, bool full = false);
|
---|
635 | void recFindNodesIn(const Ray &ray, std::list<SceneNode *> &list, SceneNode *exclude, Node * node, bool full = false);
|
---|
636 |
|
---|
637 | // the root node of the BvHierarchy
|
---|
638 | BvHierarchy::Node * mBvhRoot;
|
---|
639 | // Maximum depth of the tree
|
---|
640 | int mMaxDepth;
|
---|
641 |
|
---|
642 | // how to build the tree
|
---|
643 | BuildMethod mBuildMethod;
|
---|
644 |
|
---|
645 | // logfile for tree creation
|
---|
646 | Log * mBuildLog;
|
---|
647 |
|
---|
648 | // statistical information on the tree
|
---|
649 | TreeStats mTreeStats;
|
---|
650 |
|
---|
651 | // statistical info on a single rendered frame
|
---|
652 | FrameStats mFrameStats;
|
---|
653 |
|
---|
654 | /** Visualization flags **/
|
---|
655 | // show/highlight selected level in BvHierarchy
|
---|
656 | int mHiLiteLevel;
|
---|
657 | // show whole kd-tree
|
---|
658 | bool mShowAllBoxes;
|
---|
659 | // show node or object boxes
|
---|
660 | bool mShowNodes;
|
---|
661 |
|
---|
662 | // function pointer to the getVisibility function
|
---|
663 | // allows choosing between regular vis (NONE/PART, same es isVisible)
|
---|
664 | // and enhaced vis (NONE/PART/FULL) for early traversal abort
|
---|
665 | BvHierarchyCamera::NodeVisibility (BvHierarchyCamera::*getVisibility)(const AxisAlignedBox& box) const;
|
---|
666 |
|
---|
667 | // DEBUG
|
---|
668 | void BvHierarchy::dump(BvHierarchy::Node * node);
|
---|
669 | Real BvHierarchy::calcCost(BvHierarchy::Node * node, Real vs);
|
---|
670 | }; // class BvHierarchy
|
---|
671 |
|
---|
672 | } // namespace Ogre
|
---|
673 |
|
---|
674 | #endif // _OgreBvHierarchy_H__
|
---|