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 _OgreBiHierarchy_H__
|
---|
11 | #define _OgreBiHierarchy_H__
|
---|
12 |
|
---|
13 | #define BIHNODE_CAST(a) (static_cast<BiHierarchy::Node>(a))
|
---|
14 | #define BIHBRANCH_CAST(a) (static_cast<BiHierarchy::Branch>(a))
|
---|
15 | #define BIHLEAF_CAST(a) (static_cast<BiHierarchy::Leaf>(a))
|
---|
16 | #define BIHNODEPTR_CAST(a) (static_cast<BiHierarchy::Node *>(a))
|
---|
17 | #define BIHBRANCHPTR_CAST(a) (static_cast<BiHierarchy::Branch *>(a))
|
---|
18 | #define BIHLEAFPTR_CAST(a) (static_cast<BiHierarchy::Leaf *>(a))
|
---|
19 |
|
---|
20 | #define BiHierarchy_LOGNAME "BiHierarchyBuild.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 "OgreBiHierarchyCamera.h"
|
---|
32 | #include "HierarchyInterface.h"
|
---|
33 | #include "OgreKdTree.h"
|
---|
34 |
|
---|
35 | namespace Ogre
|
---|
36 | {
|
---|
37 | class BiHierarchyCamera;
|
---|
38 | class BihRenderable;
|
---|
39 | struct BihSplitInfo;
|
---|
40 |
|
---|
41 | class BihPlaneEvent
|
---|
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 | BihPlaneEvent(): mRenderable(0), mPosition(Vector3()), mDimension(PED_X), mType(PET_ON)
|
---|
66 | { };
|
---|
67 |
|
---|
68 | BihPlaneEvent(BihRenderable *rend, const Vector3& pos, BihPlaneEvent::Dimension dim, BihPlaneEvent::Type type):
|
---|
69 | mRenderable(rend), mPosition(pos), mDimension(dim), mType(type)
|
---|
70 | { };
|
---|
71 |
|
---|
72 | ~BihPlaneEvent() {};
|
---|
73 |
|
---|
74 | // the less operator for plane events
|
---|
75 | // first sort by position, then by dimension and finally by type
|
---|
76 | bool operator < (const BihPlaneEvent& 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 BihPlaneEvent& 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 BihPlaneEvent& e, BihPlaneEvent::Type t)
|
---|
110 | {
|
---|
111 | return (mPosition[mDimension] == e.mPosition[e.mDimension]) &&
|
---|
112 | (mDimension == e.mDimension) &&
|
---|
113 | (mType == t);
|
---|
114 | };
|
---|
115 |
|
---|
116 | void classify(const BihPlaneEvent& e, BihPlaneEvent::Side side, bool absolute);
|
---|
117 |
|
---|
118 | BihPlaneEvent clip(AxisAlignedBox& box, BihPlaneEvent::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 | BihRenderable * getRenderable() const //??
|
---|
128 | {
|
---|
129 | return mRenderable;
|
---|
130 | };
|
---|
131 |
|
---|
132 | BihPlaneEvent::Dimension getDimension() const //??
|
---|
133 | {
|
---|
134 | return mDimension;
|
---|
135 | };
|
---|
136 |
|
---|
137 | // DEBUG
|
---|
138 | String print();
|
---|
139 | protected:
|
---|
140 | // event info
|
---|
141 | BihRenderable * mRenderable;
|
---|
142 | Vector3 mPosition;
|
---|
143 | BihPlaneEvent::Dimension mDimension;
|
---|
144 | BihPlaneEvent::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: BihSplitInfo ...
|
---|
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, BihSplitInfo& 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, BihSplitInfo& split);
|
---|
160 | void SAH(const AxisAlignedBox& parent, int nLeft, int nPlane, int nRight, BihSplitInfo& split,AxisAlignedBox left,AxisAlignedBox right);
|
---|
161 |
|
---|
162 | // the variables determining the cost of a branch traversal (KT) and a leaf intersection (KI)
|
---|
163 | static Real KT;
|
---|
164 | static Real KI;
|
---|
165 | // helper functions
|
---|
166 | static Real splitCost(Real pl, Real pr, int nLeft, int nRight);
|
---|
167 | static Real splitCost(Real pl, Real pr, int nLeft, int nRight, Real mu);
|
---|
168 | static Real surfaceArea(const AxisAlignedBox& box);
|
---|
169 | static Real lookupPenalty(Real p);
|
---|
170 | protected:
|
---|
171 | Real splitBox(const AxisAlignedBox& parent, AxisAlignedBox& left, AxisAlignedBox& right);
|
---|
172 | };
|
---|
173 |
|
---|
174 | // Holds all the important information on a split
|
---|
175 | struct BihSplitInfo
|
---|
176 | {
|
---|
177 | AxisAlignedBox bleft;
|
---|
178 | AxisAlignedBox bright;
|
---|
179 | int nleft;
|
---|
180 | int nright;
|
---|
181 | Real cost;
|
---|
182 | BihPlaneEvent::Side side;
|
---|
183 | BihPlaneEvent event;
|
---|
184 |
|
---|
185 | // DEBUG
|
---|
186 | String print();
|
---|
187 | };
|
---|
188 |
|
---|
189 | typedef std::list<BihRenderable *> BihRenderableList;
|
---|
190 | typedef std::list<BihPlaneEvent> BihPlaneEventList;
|
---|
191 |
|
---|
192 | class BiHierarchy
|
---|
193 | {
|
---|
194 | protected:
|
---|
195 | class Branch;
|
---|
196 | class Leaf;
|
---|
197 |
|
---|
198 | class Node
|
---|
199 | {
|
---|
200 | public:
|
---|
201 | Node(BiHierarchy * owner, int level, AxisAlignedBox& aabb, Branch * parent):
|
---|
202 | mOwner(owner),
|
---|
203 | mLevel(level),
|
---|
204 | mAABB(aabb),
|
---|
205 | mParent(parent),
|
---|
206 | mWBB(0),
|
---|
207 | m_iSide(0)
|
---|
208 | { };
|
---|
209 |
|
---|
210 | virtual ~Node()
|
---|
211 | {
|
---|
212 | delete mWBB;
|
---|
213 | };
|
---|
214 |
|
---|
215 | virtual bool isLeaf() const = 0;
|
---|
216 | virtual bool isEmpty() const = 0;
|
---|
217 | virtual bool hasGeometry() const = 0;
|
---|
218 |
|
---|
219 | virtual void mergeLeaves(std::set<Leaf *>& leaves) = 0;
|
---|
220 |
|
---|
221 | // Gets this node's parent (NULL if this is the root).
|
---|
222 | BiHierarchy::Node *getParent(void) const { return mParent; };
|
---|
223 |
|
---|
224 | // Gets the nodes left & right child nodes, or NULL if not present or node is leaf
|
---|
225 | virtual BiHierarchy::Node *getLeftChild(void) const = 0;
|
---|
226 | virtual BiHierarchy::Node *getRightChild(void) const = 0;
|
---|
227 |
|
---|
228 | // add contained objects to render queue
|
---|
229 | virtual void queueVisibleObjects(unsigned long currentFrame,
|
---|
230 | Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes, bool fullVis = false) = 0;
|
---|
231 |
|
---|
232 | // add contained geometry (Entities) to list
|
---|
233 | virtual void getGeometryList(GeometryVector *geometryList) = 0;
|
---|
234 |
|
---|
235 | // create (when necessary), setup and return wire bounding box representing the node
|
---|
236 | WireBoundingBox * getWireBoundingBox()
|
---|
237 | {
|
---|
238 | if (mWBB == 0)
|
---|
239 | mWBB = new WireBoundingBox();
|
---|
240 |
|
---|
241 | if (mOwner->getShowNodes())
|
---|
242 | mWBB->setupBoundingBox(mAABB);
|
---|
243 | else
|
---|
244 | mWBB->setupBoundingBox(mWorldAABB);
|
---|
245 |
|
---|
246 | // cse
|
---|
247 | if (mOwner->getHiLiteLevel() == mLevel)
|
---|
248 | switch (m_iSide)
|
---|
249 | {
|
---|
250 | case 0:
|
---|
251 | {
|
---|
252 | mWBB->setMaterial("BiHierarchy/BoxViz"); break;
|
---|
253 | }
|
---|
254 | case 1: {
|
---|
255 | mWBB->setMaterial("BiHierarchy/BoxLeft"); break;
|
---|
256 | }
|
---|
257 | default:
|
---|
258 | {
|
---|
259 | mWBB->setMaterial("BiHierarchy/BoxRight");
|
---|
260 | }
|
---|
261 | }
|
---|
262 | else if (mOwner->getHiLiteLevel() == (mLevel+1))
|
---|
263 | {
|
---|
264 |
|
---|
265 | mWBB->setMaterial("BiHierarchy/BoxViz");
|
---|
266 |
|
---|
267 |
|
---|
268 | } else
|
---|
269 | {
|
---|
270 | switch (m_iSide)
|
---|
271 | {
|
---|
272 | case 0:
|
---|
273 | {
|
---|
274 | mWBB->setMaterial("BiHierarchy/BoxViz"); break;
|
---|
275 | }
|
---|
276 | case 1: {
|
---|
277 | mWBB->setMaterial("BiHierarchy/BoxLeftDark"); break;
|
---|
278 | }
|
---|
279 | default:
|
---|
280 | {
|
---|
281 | mWBB->setMaterial("BiHierarchy/BoxRightDark");
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 |
|
---|
286 | }
|
---|
287 |
|
---|
288 | return mWBB;
|
---|
289 | }
|
---|
290 |
|
---|
291 | // returns the level the node is on
|
---|
292 | int getLevel(void) const { return mLevel; };
|
---|
293 |
|
---|
294 | void SetToLeft() {
|
---|
295 | m_iSide=1;
|
---|
296 | };
|
---|
297 | void SetToRight() {
|
---|
298 | m_iSide=2;
|
---|
299 | };
|
---|
300 | int GetSide() { return m_iSide; };
|
---|
301 |
|
---|
302 |
|
---|
303 | // functions for the CHC hierarchy interface
|
---|
304 |
|
---|
305 | /** Returns last visited frame id. */
|
---|
306 | unsigned int lastVisited(void) { return mLastVisited; };
|
---|
307 | /** Set to current frame id.
|
---|
308 | @param current frame id.
|
---|
309 | */
|
---|
310 | void setLastVisited(unsigned int frameid) { mLastVisited = frameid; };
|
---|
311 | /** Makes this octree become visible / invisble.
|
---|
312 | @param visible Whether this node is to be made visible or invisible
|
---|
313 | */
|
---|
314 | void setNodeVisible(bool visible) { mVisible = visible; };
|
---|
315 | /** Returns true if this node is marked visible, false otherwise.
|
---|
316 | */
|
---|
317 | bool isNodeVisible(void) { return mVisible; };
|
---|
318 | /** Frame id when this octree was last rendered.
|
---|
319 | @return last rendered frame id
|
---|
320 | */
|
---|
321 | unsigned int lastRendered(void) { return mLastRendered; };
|
---|
322 | /** Sets frame id when this octree was last rendered.
|
---|
323 | @param last rendered frame id
|
---|
324 | */
|
---|
325 | void setLastRendered(unsigned int frameid) { mLastRendered = frameid; };
|
---|
326 | /** Returns real extent of the BiHierarchy, i.e., the merged extent of the bounding boxes.
|
---|
327 | */
|
---|
328 | AxisAlignedBox _getWorldAABB(void) const { return mAABB; };
|
---|
329 | /** Updates bound of the real aabb of BiHierarchy
|
---|
330 | */
|
---|
331 | virtual void _updateBounds(bool recurse = true) = 0;
|
---|
332 |
|
---|
333 | /** bounding box of the node
|
---|
334 | */
|
---|
335 | AxisAlignedBox mAABB;
|
---|
336 |
|
---|
337 | /** bounding box of all objects inside the node
|
---|
338 | */
|
---|
339 | AxisAlignedBox mWorldAABB;
|
---|
340 |
|
---|
341 |
|
---|
342 | protected:
|
---|
343 | BiHierarchy * mOwner;
|
---|
344 | Branch * mParent;
|
---|
345 | int mLevel;
|
---|
346 | int m_iSide; // defines the side ( left / right ) for better debugging.
|
---|
347 |
|
---|
348 | WireBoundingBox * mWBB;
|
---|
349 |
|
---|
350 | // for the CHC hierarchy interface
|
---|
351 | unsigned int mLastRendered;
|
---|
352 | unsigned int mLastVisited;
|
---|
353 | bool mVisible;
|
---|
354 | };
|
---|
355 |
|
---|
356 | class Branch : public Node
|
---|
357 | {
|
---|
358 | public:
|
---|
359 | Branch(BiHierarchy * owner, int level, AxisAlignedBox& aabb, Branch * parent,
|
---|
360 | Plane * splitplane, BihPlaneEvent::Side side):
|
---|
361 | Node(owner, level, aabb, parent),
|
---|
362 | mSplitPlane(splitplane),
|
---|
363 | mLeft(0),
|
---|
364 | mRight(0),
|
---|
365 | mPlaneSide(side)
|
---|
366 | { };
|
---|
367 |
|
---|
368 | virtual ~Branch()
|
---|
369 | {
|
---|
370 | delete mLeft;
|
---|
371 | delete mRight;
|
---|
372 | delete mSplitPlane;
|
---|
373 | };
|
---|
374 |
|
---|
375 | // a branch is not a leaf
|
---|
376 | virtual bool isLeaf() const { return false; };
|
---|
377 |
|
---|
378 | // s branch is empty when it does not have children
|
---|
379 | virtual bool isEmpty() const { return (mLeft == 0 && mRight == 0); }
|
---|
380 |
|
---|
381 | // a branch never has geometry
|
---|
382 | virtual bool hasGeometry() const { return false; };
|
---|
383 |
|
---|
384 | virtual void mergeLeaves(std::set<Leaf *>& leaves)
|
---|
385 | {
|
---|
386 | for (std::set<Leaf *>::iterator it = mLeaves.begin(); it != mLeaves.end(); it++)
|
---|
387 | leaves.insert(*it);
|
---|
388 | }
|
---|
389 |
|
---|
390 | // a branch should have at least one child
|
---|
391 | virtual BiHierarchy::Node * getLeftChild() const { return mLeft; };
|
---|
392 | virtual BiHierarchy::Node * getRightChild() const { return mRight; };
|
---|
393 |
|
---|
394 | virtual void queueVisibleObjects(unsigned long currentFrame,
|
---|
395 | Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes, bool fullVis = false)
|
---|
396 | {
|
---|
397 | // cse
|
---|
398 | //showBoxes=true;
|
---|
399 | if (showBoxes)
|
---|
400 | {
|
---|
401 | if (mLevel == mOwner->getHiLiteLevel()|| (mLevel-1) == mOwner->getHiLiteLevel() || mOwner->getShowAllBoxes())
|
---|
402 | queue->addRenderable(getWireBoundingBox());
|
---|
403 | /*
|
---|
404 | if (mLevel == mOwner->getHiLiteLevel() || mOwner->getShowAllBoxes())
|
---|
405 | queue->addRenderable(getWireBoundingBox());
|
---|
406 | */
|
---|
407 | }
|
---|
408 |
|
---|
409 | if (fullVis)
|
---|
410 | for (std::set<Leaf *>::iterator it = mLeaves.begin(); it != mLeaves.end(); it ++)
|
---|
411 | (*it)->queueVisibleObjects(currentFrame, cam, queue, onlyShadowCasters, showBoxes, fullVis);
|
---|
412 | }
|
---|
413 |
|
---|
414 | // a branch has no geometry, do nothing
|
---|
415 | virtual void getGeometryList(GeometryVector *geometryList) { }
|
---|
416 |
|
---|
417 | // branches do not posses geometry => just merge child aabbs
|
---|
418 | virtual void _updateBounds(bool recurse = true)
|
---|
419 | {
|
---|
420 | // reset box
|
---|
421 | mWorldAABB.setNull();
|
---|
422 |
|
---|
423 | // merge box & leaves
|
---|
424 | if (mLeft)
|
---|
425 | {
|
---|
426 | mWorldAABB.merge(mLeft->mWorldAABB);
|
---|
427 | mLeft->mergeLeaves(mLeaves);
|
---|
428 | }
|
---|
429 | if (mRight)
|
---|
430 | {
|
---|
431 | mWorldAABB.merge(mRight->mWorldAABB);
|
---|
432 | mRight->mergeLeaves(mLeaves);
|
---|
433 | }
|
---|
434 |
|
---|
435 | // update parent recursively
|
---|
436 | if (recurse && mParent)
|
---|
437 | mParent->_updateBounds(recurse);
|
---|
438 | }
|
---|
439 |
|
---|
440 | Node * mLeft;
|
---|
441 | Node * mRight;
|
---|
442 | Plane * mSplitPlane;
|
---|
443 | BihPlaneEvent::Side mPlaneSide;
|
---|
444 | protected:
|
---|
445 | std::set<Leaf *> mLeaves;
|
---|
446 | };
|
---|
447 |
|
---|
448 | class Leaf : public Node
|
---|
449 | {
|
---|
450 | public:
|
---|
451 | Leaf(BiHierarchy * owner, int level, AxisAlignedBox& aabb, Branch *parent):
|
---|
452 | Node(owner, level, aabb, parent)
|
---|
453 | {};
|
---|
454 |
|
---|
455 | virtual ~Leaf();
|
---|
456 |
|
---|
457 | // a leaf is a leaf, dammit
|
---|
458 | virtual bool isLeaf() const { return true; }
|
---|
459 |
|
---|
460 | // a leaf is empty when it does not posses renderables
|
---|
461 | virtual bool isEmpty() const { return mBihRenderables.empty(); }
|
---|
462 |
|
---|
463 | // a leaf has geometry when it has renderables
|
---|
464 | virtual bool hasGeometry() const { return !mBihRenderables.empty(); }
|
---|
465 |
|
---|
466 | // a leaf adds itself to the leaf set
|
---|
467 | virtual void mergeLeaves(std::set<Leaf *>& leaves) { leaves.insert(this); }
|
---|
468 |
|
---|
469 | // a leaf never has children
|
---|
470 | virtual BiHierarchy::Node * getLeftChild() const { return 0; };
|
---|
471 | virtual BiHierarchy::Node * getRightChild() const { return 0; };
|
---|
472 |
|
---|
473 | virtual void queueVisibleObjects(unsigned long currentFrame,
|
---|
474 | Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes, bool fullVis = false);
|
---|
475 |
|
---|
476 | virtual void getGeometryList(GeometryVector *geometryList);
|
---|
477 |
|
---|
478 | // update the world aabb based on the contained geometry
|
---|
479 | virtual void _updateBounds(bool recurse = true);
|
---|
480 |
|
---|
481 | virtual void remove(BihRenderable * rend)
|
---|
482 | {
|
---|
483 | mBihRenderables.remove(rend);
|
---|
484 | //mBihRenderables.erase(find(mBihRenderables.begin(), mBihRenderables.end(), rend));
|
---|
485 | };
|
---|
486 |
|
---|
487 | virtual void insert(BihRenderable * rend)
|
---|
488 | {
|
---|
489 | mBihRenderables.push_back(rend);
|
---|
490 | };
|
---|
491 |
|
---|
492 | BihRenderableList mBihRenderables;
|
---|
493 | };
|
---|
494 |
|
---|
495 | struct BihSubdivisionCandidate
|
---|
496 | {
|
---|
497 | BihSubdivisionCandidate(BihPlaneEventList * e, int n, AxisAlignedBox& a,
|
---|
498 | BiHierarchy::Branch * p, Real c, Real d, BihSplitInfo * b, BihPlaneEvent::Side s):
|
---|
499 | events(e), nObjects(n), aabb(a), parent(p), cost(c), decrease(d), best(b), side(s)
|
---|
500 | { };
|
---|
501 |
|
---|
502 | bool operator < (const BihSubdivisionCandidate& rhs) const
|
---|
503 | {
|
---|
504 | return decrease < rhs.decrease;
|
---|
505 | };
|
---|
506 |
|
---|
507 | void operator = (const BihSubdivisionCandidate& rhs)
|
---|
508 | {
|
---|
509 | decrease = rhs.decrease;
|
---|
510 | cost = rhs.cost;
|
---|
511 | nObjects = rhs.nObjects;
|
---|
512 | side = rhs.side;
|
---|
513 | aabb = rhs.aabb;
|
---|
514 | events = rhs.events;
|
---|
515 | parent = rhs.parent;
|
---|
516 | best = rhs.best;
|
---|
517 | };
|
---|
518 |
|
---|
519 | // DEBUG
|
---|
520 | String print();
|
---|
521 |
|
---|
522 | Real decrease;
|
---|
523 | Real cost;
|
---|
524 | int nObjects;
|
---|
525 | BihPlaneEvent::Side side;
|
---|
526 | AxisAlignedBox aabb;
|
---|
527 | BihPlaneEventList *events;
|
---|
528 | BiHierarchy::Branch * parent;
|
---|
529 | BihSplitInfo * best;
|
---|
530 | };
|
---|
531 |
|
---|
532 | // typedef std::stack<BihSubdivisionCandidate> SplitCandidatePQ;
|
---|
533 | typedef std::priority_queue<BihSubdivisionCandidate> SplitCandidatePQ;
|
---|
534 |
|
---|
535 | // nodestack for the stack-based rendering function
|
---|
536 | typedef std::stack<BiHierarchy::Node *> NodeStack;
|
---|
537 | public:
|
---|
538 | friend class BiHierarchyInterface;
|
---|
539 |
|
---|
540 | typedef BiHierarchy::Node * NodePtr;
|
---|
541 | typedef BiHierarchy::Branch * BranchPtr;
|
---|
542 | typedef BiHierarchy::Leaf * LeafPtr;
|
---|
543 |
|
---|
544 | typedef std::list<NodePtr> NodeList;
|
---|
545 | typedef std::set<LeafPtr> LeafSet;
|
---|
546 |
|
---|
547 | struct TreeStats
|
---|
548 | {
|
---|
549 | unsigned int mNumNodes;
|
---|
550 | unsigned int mNumLeaves;
|
---|
551 | unsigned int mNumSceneNodes;
|
---|
552 |
|
---|
553 | void clear(void)
|
---|
554 | {
|
---|
555 | mNumNodes = 0;
|
---|
556 | mNumLeaves = 0;
|
---|
557 | mNumSceneNodes = 0;
|
---|
558 | }
|
---|
559 | };
|
---|
560 |
|
---|
561 | struct FrameStats
|
---|
562 | {
|
---|
563 | unsigned int mTraversedNodes;
|
---|
564 | unsigned int mRenderedNodes;
|
---|
565 | unsigned int mFrustumCulledNodes;
|
---|
566 |
|
---|
567 | void clear(void)
|
---|
568 | {
|
---|
569 | mTraversedNodes = 0;
|
---|
570 | mRenderedNodes = 0;
|
---|
571 | mFrustumCulledNodes = 0;
|
---|
572 | }
|
---|
573 | };
|
---|
574 |
|
---|
575 | enum RenderMethod
|
---|
576 | {
|
---|
577 | BIHRM_INTERNAL,
|
---|
578 | BIHRM_GTP_VFC,
|
---|
579 | BIHRM_GTP_SWC,
|
---|
580 | BIHRM_GTP_CHC,
|
---|
581 | // invalid modes, just for convenience
|
---|
582 | BIHRM_SIZE,
|
---|
583 | BIHRM_NOTSET
|
---|
584 | };
|
---|
585 |
|
---|
586 | enum BuildMethod
|
---|
587 | {
|
---|
588 | BIHBM_RECURSIVE,
|
---|
589 | BIHBM_PRIORITYQUEUE,
|
---|
590 | // invalid modes, just for convenience
|
---|
591 | BIHBM_SIZE,
|
---|
592 | BIHBM_NOTSET
|
---|
593 | };
|
---|
594 |
|
---|
595 | enum PlaneAlorithm
|
---|
596 | {
|
---|
597 | BIHAL_SAH,
|
---|
598 | BIHAL_MID
|
---|
599 | };
|
---|
600 |
|
---|
601 |
|
---|
602 | const static int HILITE_OFF = -1;
|
---|
603 |
|
---|
604 | BiHierarchy(int maxdepth, BuildMethod bm);
|
---|
605 | BiHierarchy(int maxdepth, BuildMethod bm, int hilite, bool allboxes, bool shownodes);
|
---|
606 | virtual ~BiHierarchy();
|
---|
607 |
|
---|
608 | // DEBUG
|
---|
609 | void dump(void);
|
---|
610 | Real calcCost(void);
|
---|
611 |
|
---|
612 | // sets the level to highlight or turns it off (when hilite < 0)
|
---|
613 | inline void setHiLiteLevel(int hilite) { mHiLiteLevel = hilite; };
|
---|
614 | inline int getHiLiteLevel(void) { return mHiLiteLevel; };
|
---|
615 |
|
---|
616 | // toggles displaying the BiHierarchy boxes
|
---|
617 | inline void setShowAllBoxes(bool show) { mShowAllBoxes = show; };
|
---|
618 | inline bool getShowAllBoxes(void) { return mShowAllBoxes; };
|
---|
619 |
|
---|
620 | // toggles between displaying the bounding box of the node and
|
---|
621 | // the box of the contained scene nodes
|
---|
622 | inline void setShowNodes(bool show = true) { mShowNodes = show; };
|
---|
623 | inline bool getShowNodes(void) { return mShowNodes; };
|
---|
624 |
|
---|
625 | // changes vis mode (simple/enhanced with NONE/PART/FULL vis)
|
---|
626 | void setEnhancedVis(bool enh);
|
---|
627 | bool getEnhancedVis(void);
|
---|
628 |
|
---|
629 | NodePtr getRoot(void) const { return mBihRoot; };
|
---|
630 |
|
---|
631 | // insert a new scene node into an existing Bihierarchy
|
---|
632 | void insert(BihRenderable * rend);
|
---|
633 | // remove a scene node from the tree
|
---|
634 | void remove(BihRenderable * rend);
|
---|
635 | // function to initialize a kd-tree based on the contents of the scene
|
---|
636 | void build(BihRenderable * sceneRoot);
|
---|
637 |
|
---|
638 | // test visibility of objects and add to render queue
|
---|
639 | void queueVisibleObjects(BiHierarchyCamera* cam, RenderQueue* queue, bool onlyShadowCasters,
|
---|
640 | bool showBoxes, BiHierarchy::NodeList& visibleNodes);
|
---|
641 |
|
---|
642 | // find visible nodes & place in list
|
---|
643 | //void findVisibleNodes(NodeList& visibleNodes, Camera * cam);
|
---|
644 |
|
---|
645 | /** Recurses the BiHierarchy, adding any nodes intersecting with the
|
---|
646 | box/sphere/volume/ray into the given list.
|
---|
647 | It ignores the exclude scene node.
|
---|
648 | */
|
---|
649 | void findNodesIn(const AxisAlignedBox &box, std::list<SceneNode *> &list, SceneNode *exclude = 0);
|
---|
650 | void findNodesIn(const Sphere &sphere, std::list<SceneNode *> &list, SceneNode *exclude = 0);
|
---|
651 | void findNodesIn(const PlaneBoundedVolume &volume, std::list<SceneNode *> &list, SceneNode *exclude = 0);
|
---|
652 | void findNodesIn(const Ray &ray, std::list<SceneNode *> &list, SceneNode *exclude = 0);
|
---|
653 |
|
---|
654 | // self-explanatory ...
|
---|
655 | int getMaxDepth(void) { return mMaxDepth; }
|
---|
656 | const TreeStats& getTreeStats(void) const { return mTreeStats; }
|
---|
657 | const FrameStats& getFramesStats(void) const { return mFrameStats; }
|
---|
658 | AxisAlignedBox getBox(void) { if (mBihRoot) return mBihRoot->mAABB; else return AxisAlignedBox(); }
|
---|
659 | void setBuildMethod(BuildMethod bm) { mBuildMethod = bm; }
|
---|
660 | protected:
|
---|
661 | // init materials, logs and stuff
|
---|
662 | void init();
|
---|
663 | // recursive insert funciton
|
---|
664 | void recInsert(BiHierarchy::Node * node, BihRenderable * rend);
|
---|
665 | // helper functions for insert
|
---|
666 | void recInsertNew(BiHierarchy::Branch * parent, BihPlaneEvent::Side side, BihRenderable * rend);
|
---|
667 | void splitBox(const BiHierarchy::Branch& parent, AxisAlignedBox& left, AxisAlignedBox& right);
|
---|
668 | void rebuildSubtree(BiHierarchy::Node * node, BihRenderable * rend);
|
---|
669 | // build scene from a list of nodes rather than a hierarchy
|
---|
670 | BiHierarchy::Node * buildFromList(BihRenderableList& nodelist, BiHierarchy::Branch * parent, AxisAlignedBox& aabb);
|
---|
671 | void addRendToList(BiHierarchy::Node * node, BihRenderableList& nodelist);
|
---|
672 |
|
---|
673 | // recursively delete empty nodes
|
---|
674 | void recDelete(BiHierarchy::Node * node);
|
---|
675 |
|
---|
676 | // find the best plane for node division
|
---|
677 | BihSplitInfo * pqFindPlane(BihPlaneEventList * events, int nObjects, AxisAlignedBox& aabb, Real globalSA);
|
---|
678 |
|
---|
679 | // priority queue based build function
|
---|
680 | BiHierarchy::Node * pqBuild(BihPlaneEventList& events, int nObjects, AxisAlignedBox& aabb, BiHierarchy::Branch * parent);
|
---|
681 | // recursive build function
|
---|
682 | BiHierarchy::Node * recBuild(BihPlaneEventList& events, int nObjects, AxisAlignedBox& aabb, BiHierarchy::Branch * parent);
|
---|
683 |
|
---|
684 | // recursive rendering function
|
---|
685 | void recQueueVisibleObjects(BiHierarchy::Node * node, unsigned long currentFrame, BiHierarchyCamera* cam,
|
---|
686 | RenderQueue* queue, bool onlyShadowCasters, bool showBoxes,
|
---|
687 | BiHierarchy::NodeList& visibleNodes, bool fullVis = false);
|
---|
688 |
|
---|
689 | // recursively find visible nodes
|
---|
690 | //void recFindVisibleNodes(BiHierarchy::Node * node, NodeList& visibleNodes, Camera * cam);
|
---|
691 |
|
---|
692 | /** Recurses the BiHierarchy, adding any nodes intersecting with the
|
---|
693 | box/sphere/volume/ray into the given list.
|
---|
694 | It ignores the exclude scene node.
|
---|
695 | */
|
---|
696 | void recFindNodesIn(const AxisAlignedBox &box, std::list<SceneNode *> &list, SceneNode *exclude, Node * node, bool full = false);
|
---|
697 | void recFindNodesIn(const Sphere &sphere, std::list<SceneNode *> &list, SceneNode *exclude, Node * node, bool full = false);
|
---|
698 | void recFindNodesIn(const PlaneBoundedVolume &volume, std::list<SceneNode *> &list, SceneNode *exclude, Node * node, bool full = false);
|
---|
699 | void recFindNodesIn(const Ray &ray, std::list<SceneNode *> &list, SceneNode *exclude, Node * node, bool full = false);
|
---|
700 |
|
---|
701 | // the root node of the BiHierarchy
|
---|
702 | BiHierarchy::Node * mBihRoot;
|
---|
703 | // Maximum depth of the tree
|
---|
704 | int mMaxDepth;
|
---|
705 |
|
---|
706 | // how to build the tree
|
---|
707 | BuildMethod mBuildMethod;
|
---|
708 |
|
---|
709 | // what algorithm will be used to find the split plane
|
---|
710 | PlaneAlorithm mAlgorithm;
|
---|
711 |
|
---|
712 | // logfile for tree creation
|
---|
713 | Log * mBuildLog;
|
---|
714 |
|
---|
715 | // statistical information on the tree
|
---|
716 | TreeStats mTreeStats;
|
---|
717 |
|
---|
718 | // statistical info on a single rendered frame
|
---|
719 | FrameStats mFrameStats;
|
---|
720 |
|
---|
721 | /** Visualization flags **/
|
---|
722 | // show/highlight selected level in BiHierarchy
|
---|
723 | int mHiLiteLevel;
|
---|
724 | // show whole kd-tree
|
---|
725 | bool mShowAllBoxes;
|
---|
726 | // show node or object boxes
|
---|
727 | bool mShowNodes;
|
---|
728 |
|
---|
729 | // function pointer to the getVisibility function
|
---|
730 | // allows choosing between regular vis (NONE/PART, same es isVisible)
|
---|
731 | // and enhaced vis (NONE/PART/FULL) for early traversal abort
|
---|
732 | BiHierarchyCamera::NodeVisibility (BiHierarchyCamera::*getVisibility)(const AxisAlignedBox& box) const;
|
---|
733 |
|
---|
734 | // DEBUG
|
---|
735 | void BiHierarchy::dump(BiHierarchy::Node * node);
|
---|
736 | Real BiHierarchy::calcCost(BiHierarchy::Node * node, Real vs);
|
---|
737 | }; // class BiHierarchy
|
---|
738 |
|
---|
739 | } // namespace Ogre
|
---|
740 |
|
---|
741 | #endif // _OgreBiHierarchy_H__
|
---|