source: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreOctreeHierarchyInterface.cpp @ 944

Revision 944, 6.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.empty())
24        {
25                RenderNode(node);
26        }
27       
28       
29        // if not all subtrees are empty
30        if (!IsLeaf(node))
31        {
32                Octree *nextChild;
33
34                if ((nextChild = octree->mChildren[0][0][0]) != NULL)
35                        mDistanceQueue->push(nextChild);
36                if ((nextChild = octree->mChildren[0][0][1]) != NULL)
37                        mDistanceQueue->push(nextChild);
38                if ((nextChild = octree->mChildren[0][1][0]) != NULL)
39                        mDistanceQueue->push(nextChild);
40                if ((nextChild = octree->mChildren[0][1][1]) != NULL)
41                mDistanceQueue->push(nextChild);
42                if ((nextChild = octree->mChildren[1][0][0]) != NULL)
43                        mDistanceQueue->push(nextChild);
44                if ((nextChild = octree->mChildren[1][0][1]) != NULL)
45                        mDistanceQueue->push(nextChild);
46                if ((nextChild = octree->mChildren[1][1][0]) != NULL)
47                        mDistanceQueue->push(nextChild);
48                if ((nextChild = octree->mChildren[1][1][1]) != NULL)
49                        mDistanceQueue->push(nextChild);
50        }
51}
52//-----------------------------------------------------------------------
53bool OctreeHierarchyInterface::IsLeaf(GtpVisibility::HierarchyNode *node) const
54{
55        Octree *octree = static_cast<Octree *>(node);
56
57        // HACK: if there are subtrees, they are empty => we are not interested in them
58        return octree->numNodes() == (int)octree->mNodes.size();
59}
60//-----------------------------------------------------------------------
61bool OctreeHierarchyInterface::HasGeometry(GtpVisibility::HierarchyNode *node) const
62{
63        return !(static_cast<Octree *>(node))->mNodes.empty();
64}
65//-----------------------------------------------------------------------
66float OctreeHierarchyInterface::GetSquaredDistance(GtpVisibility::HierarchyNode *node) const
67{
68        const Vector3 bmin = static_cast<Octree *>(node)->mBox.getMinimum();
69        const Vector3 bmax = static_cast<Octree *>(node)->mBox.getMaximum();
70
71        const Vector3 pos = (bmax - bmin) * 0.5 + bmin;
72       
73/*      std::stringstream d;
74        d << "a: " << (mCameraPosition - pos).squaredLength()
75          << " b: " << (mCullCamera->getDerivedPosition() - pos).squaredLength();
76        LogManager::getSingleton().logMessage(d.str());*/
77        return (mCameraPosition - pos).squaredLength();
78}
79//-----------------------------------------------------------------------
80void OctreeHierarchyInterface::SetNodeVisible(GtpVisibility::HierarchyNode *node,
81                                                                                          const bool visible) const
82{
83#ifdef GTP_VISIBILITY_MODIFIED_OGRE
84        static_cast<Octree *>(node)->setOctreeVisible(visible);
85#endif
86}
87//-----------------------------------------------------------------------
88void OctreeHierarchyInterface::SetLastVisited(GtpVisibility::HierarchyNode *node,
89                                                                                          const unsigned int frameId) const
90{
91#ifdef GTP_VISIBILITY_MODIFIED_OGRE
92        static_cast<Octree *>(node)->setLastVisited(frameId);
93#endif
94}
95//-----------------------------------------------------------------------
96void OctreeHierarchyInterface::PullUpVisibility(GtpVisibility::HierarchyNode *node) const
97{               
98#ifdef GTP_VISIBILITY_MODIFIED_OGRE
99        Octree *octant = static_cast<Octree *>(node);
100
101        while (octant && !octant->isOctreeVisible())
102        {
103                octant->setOctreeVisible(true);
104                octant = octant->getParent();
105        }
106#endif
107}
108//-----------------------------------------------------------------------
109void OctreeHierarchyInterface::RenderNode(GtpVisibility::HierarchyNode *node)
110{
111#ifdef GTP_VISIBILITY_MODIFIED_OGRE
112        Octree *octant = static_cast<Octree *>(node);
113
114        if (octant->lastRendered() != mFrameId)
115        {
116                octant->setLastRendered(mFrameId);
117                OctreeSceneManager *ocm =
118                        dynamic_cast<OctreeSceneManager *>(mSceneManager);
119
120                ocm->_renderOctant(mCamera, octant, mOnlyShadowCasters, mLeavePassesInQueue);
121
122                mVisibleNodes.push_back(node);
123        }
124#endif
125       
126}
127//-----------------------------------------------------------------------
128bool OctreeHierarchyInterface::IsNodeVisible(GtpVisibility::HierarchyNode *node) const
129{
130#ifdef GTP_VISIBILITY_MODIFIED_OGRE
131        return static_cast<Octree *>(node)->isOctreeVisible();
132#else
133        return true;
134#endif
135}
136//-----------------------------------------------------------------------
137unsigned int OctreeHierarchyInterface::LastVisited(GtpVisibility::HierarchyNode *node) const
138{
139#ifdef GTP_VISIBILITY_MODIFIED_OGRE
140        return static_cast<Octree *>(node)->lastVisited();
141#else
142        return 0;
143#endif
144}
145//-----------------------------------------------------------------------
146AxisAlignedBox *OctreeHierarchyInterface::GetBoundingBox(GtpVisibility::HierarchyNode *node)
147{
148        // reuse box if node is the same
149        // only create renderable bounding box for new node
150        if (node != mSavedNode)
151        {
152                mSavedNode = node;
153            //static_cast<Octree *>(node)->_getCullBounds(&mBox);
154                mBox = static_cast<Octree *>(node)->_getWorldAABB();
155        }
156
157        return &mBox;
158}
159//-----------------------------------------------------------------------
160void OctreeHierarchyInterface::VisualizeCulledNode(GtpVisibility::HierarchyNode *node,
161                                                                                                   GtpVisibility::CullingType type) const
162{
163        WireBoundingBox *box = static_cast<Octree *>(node)->getWireBoundingBox();
164
165        if (type == GtpVisibility::FRUSTUM_CULLED)
166        {
167                box->setMaterial("FrustumCulledNodesMaterial");
168        }
169        else // type == GtpVisibility::QUERY_CULLED
170        {
171                box->setMaterial("QueryCulledNodesMaterial");
172        }
173
174        //dynamic_cast<OctreeSceneManager *>(mSceneManager)->getBoxes()->push_back(box);
175        dynamic_cast<OctreeSceneManager *>(mSceneManager)->getBoxes()->push_back(box);
176}
177//-----------------------------------------------------------------------
178void OctreeHierarchyInterface::GetNodeGeometryList(GtpVisibility::HierarchyNode *node,
179                                                                                                   GtpVisibility::GeometryVector *geometryList,
180                                                                                                   bool includeChildren)
181{
182        NodeList::const_iterator nodeIt, nodeIt_end;
183        nodeIt_end = static_cast<Octree *>(node)->mNodes.end();
184
185        for (nodeIt = static_cast<Octree *>(node)->mNodes.begin(); nodeIt != nodeIt_end; ++nodeIt)
186        {
187                SceneNodeHierarchyInterface::GetNodeGeometryList(*nodeIt, geometryList, includeChildren);
188        }
189}
190
191} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.