source: branches/VUT/0.2/Ogre/src/OgreOctreeHierarchyInterface.cpp @ 174

Revision 174, 5.6 KB checked in by mattausch, 19 years ago (diff)
Line 
1#include "OgreOctreeHierarchyInterface.h"
2#include "OgreVisibilityOctreeSceneManager.h"
3#include <OgreOctree.h>
4#include <OgreLogManager.h>
5#include <OgreStringConverter.h>
6
7
8namespace Ogre {
9
10//-----------------------------------------------------------------------
11OctreeHierarchyInterface::OctreeHierarchyInterface(OctreeSceneManager *sm, RenderSystem *rsys):
12SceneNodeHierarchyInterface(sm, rsys)
13{
14}
15//-----------------------------------------------------------------------
16void OctreeHierarchyInterface::TraverseNode(GtpVisibility::HierarchyNode *node)
17{
18        ++ mNumTraversedNodes;
19
20        Octree *octree = static_cast<Octree *>(node);
21
22        // if we come across some renderable geometry => render it
23        if (octree->mNodes.size() > 0)
24        {
25                RenderNode(node);
26        }
27       
28        //if (octree->numNodes() > (int)octree->mNodes.size()) // if not all subtrees are empty
29        if (!IsLeaf(node))
30        {
31                for(int i=0; i<8; ++i)
32                {
33                        Octree *nextChild =
34                                octree->mChildren[(i & 4) >> 2][(i & 2) >> 1][i & 1];
35
36                        if (nextChild)
37                        {
38                                mDistanceQueue->push(nextChild);
39                        }
40                }
41        }
42}
43//-----------------------------------------------------------------------
44bool OctreeHierarchyInterface::IsLeaf(GtpVisibility::HierarchyNode *node) const
45{
46        Octree *octree = static_cast<Octree *>(node);
47        // HACK: if there are subtrees, they are empty => we are not interested in them
48        return octree->numNodes() == (int)octree->mNodes.size();
49}
50//-----------------------------------------------------------------------
51bool OctreeHierarchyInterface::HasGeometry(GtpVisibility::HierarchyNode *node) const
52{
53        return static_cast<Octree *>(node)->mNodes.size() > 0;
54}
55//-----------------------------------------------------------------------
56float OctreeHierarchyInterface::GetSquaredDistance(GtpVisibility::HierarchyNode *node) const
57{
58        AxisAlignedBox *box = &static_cast<Octree *>(node)->mBox;
59        Vector3 mid = ((box->getMaximum() - box->getMinimum()) * 0.5) + box->getMinimum();
60
61        return (mCullCamera->getDerivedPosition() - mid).squaredLength();
62}
63//-----------------------------------------------------------------------
64void OctreeHierarchyInterface::SetNodeVisible(GtpVisibility::HierarchyNode *node,
65                                                                                          const bool visible)
66{
67#ifdef GTP_VISIBILITY_MODIFIED_OGRE
68        static_cast<Octree *>(node)->setOctreeVisible(visible);
69#endif
70}
71//-----------------------------------------------------------------------
72void OctreeHierarchyInterface::SetLastVisited(GtpVisibility::HierarchyNode *node,
73                                                                                          const unsigned int frameId)
74{
75#ifdef GTP_VISIBILITY_MODIFIED_OGRE
76        static_cast<Octree *>(node)->setLastVisited(frameId);
77#endif
78}
79//-----------------------------------------------------------------------
80void OctreeHierarchyInterface::PullUpVisibility(GtpVisibility::HierarchyNode *node)
81{               
82#ifdef GTP_VISIBILITY_MODIFIED_OGRE
83        Octree *octant = static_cast<Octree *>(node);
84
85        while (octant && !octant->isOctreeVisible())
86        {
87                octant->setOctreeVisible(true);
88                octant = octant->getParent();
89        }
90#endif
91}
92//-----------------------------------------------------------------------
93void OctreeHierarchyInterface::RenderNode(GtpVisibility::HierarchyNode *node)
94{
95#ifdef GTP_VISIBILITY_MODIFIED_OGRE
96        Octree *octant = static_cast<Octree *>(node);
97
98        if (octant->lastRendered() != mFrameId)
99        {
100                octant->setLastRendered(mFrameId);
101
102                dynamic_cast<OctreeSceneManager *>(mSceneManager)->_renderOctant(mCamera,
103                        octant, mOnlyShadowCasters, mLeavePassesInQueue);
104
105                mVisibleNodes.push_back(node);
106        }
107#endif
108}
109//-----------------------------------------------------------------------
110bool OctreeHierarchyInterface::IsNodeVisible(GtpVisibility::HierarchyNode *node) const
111{
112#ifdef GTP_VISIBILITY_MODIFIED_OGRE
113        return static_cast<Octree *>(node)->isOctreeVisible();
114#else
115        return true;
116#endif
117}
118//-----------------------------------------------------------------------
119unsigned int OctreeHierarchyInterface::LastVisited(GtpVisibility::HierarchyNode *node) const
120{
121#ifdef GTP_VISIBILITY_MODIFIED_OGRE
122        return static_cast<Octree *>(node)->lastVisited();
123#else
124        return 0;
125#endif
126}
127//-----------------------------------------------------------------------
128AxisAlignedBox *OctreeHierarchyInterface::GetBoundingBox(GtpVisibility::HierarchyNode *node)
129{
130        if (node != mSavedNode)
131        {
132                mSavedNode = node;
133            //static_cast<Octree *>(node)->_getCullBounds(&mBox);
134                mBox = static_cast<Octree *>(node)->_getWorldAABB();
135        }
136
137        return &mBox;
138}
139//-----------------------------------------------------------------------
140void OctreeHierarchyInterface::VisualizeCulledNode(GtpVisibility::HierarchyNode *node,
141                                                                                                   GtpVisibility::CullingType type)
142{
143        WireBoundingBox *box = static_cast<Octree *>(node)->getWireBoundingBox();
144
145        if (type == GtpVisibility::FRUSTUM_CULLED)
146        {
147                box->setMaterial("FrustumCulledNodesMaterial");
148        }
149        else // type == GtpVisibility::QUERY_CULLED
150        {
151                box->setMaterial("QueryCulledNodesMaterial");
152        }
153
154        dynamic_cast<OctreeSceneManager *>(mSceneManager)->getBoxes()->push_back(box);
155}
156//-----------------------------------------------------------------------
157void OctreeHierarchyInterface::GetNodeGeometryList(GtpVisibility::HierarchyNode *node, 
158                                                                                   GtpVisibility::GeometryList *geometryList,
159                                                                                   bool includeChildren)
160{
161        NodeList::const_iterator nodeIt, nodeIt_end;
162        nodeIt_end = static_cast<Octree *>(node)->mNodes.end();
163
164        for (nodeIt = static_cast<Octree *>(node)->mNodes.begin(); nodeIt != nodeIt_end; ++nodeIt)
165        {
166                SceneNodeHierarchyInterface::GetNodeGeometryList(*nodeIt, geometryList, includeChildren);
167        }
168}
169
170} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.