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

Revision 2258, 9.1 KB checked in by mattausch, 17 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,
12                                                                                                   RenderSystem *rsys):
13SceneNodeHierarchyInterface(sm, rsys)
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//-----------------------------------------------------------------------
53HierarchyNode *OctreeHierarchyInterface::GetRandomLeaf()
54{
55        return NULL;
56}
57//-----------------------------------------------------------------------
58bool OctreeHierarchyInterface::GIsNodeFullyVisible(GtpVisibility::HierarchyNode *node) const
59{
60        return static_cast<Octree *>(node)->isOctreeFullyVisible();
61}
62//-----------------------------------------------------------------------
63void OctreeHierarchyInterface::TraverseNode2(GtpVisibility::HierarchyNode *node)
64{
65        ++ mNumTraversedNodes;
66
67        Octree *octree = static_cast<Octree *>(node);
68
69        // if we come across some renderable geometry => render it
70        if (!octree->mNodes.empty())
71        {
72                // render everything from here
73                if (octree->isOctreeFullyVisible())
74                {               
75                        RenderNodeRecursive(node);
76                        return;
77                }
78
79                RenderNode(node);
80        }
81               
82        // if not all subtrees are empty
83        if (!IsLeaf(node))
84        {
85                Octree *nextChild;
86
87                if ((nextChild = octree->mChildren[0][0][0]) != NULL)
88                        mDistanceQueue->push(nextChild);
89                if ((nextChild = octree->mChildren[0][0][1]) != NULL)
90                        mDistanceQueue->push(nextChild);
91                if ((nextChild = octree->mChildren[0][1][0]) != NULL)
92                        mDistanceQueue->push(nextChild);
93                if ((nextChild = octree->mChildren[0][1][1]) != NULL)
94                mDistanceQueue->push(nextChild);
95                if ((nextChild = octree->mChildren[1][0][0]) != NULL)
96                        mDistanceQueue->push(nextChild);
97                if ((nextChild = octree->mChildren[1][0][1]) != NULL)
98                        mDistanceQueue->push(nextChild);
99                if ((nextChild = octree->mChildren[1][1][0]) != NULL)
100                        mDistanceQueue->push(nextChild);
101                if ((nextChild = octree->mChildren[1][1][1]) != NULL)
102                        mDistanceQueue->push(nextChild);
103        }
104}
105//-----------------------------------------------------------------------
106bool OctreeHierarchyInterface::IsLeaf(GtpVisibility::HierarchyNode *node) const
107{
108        Octree *octree = static_cast<Octree *>(node);
109
110        // HACK: if there are subtrees, they are empty => we are not interested in them
111        return octree->numNodes() == (int)octree->mNodes.size();
112}
113//-----------------------------------------------------------------------
114bool OctreeHierarchyInterface::HasGeometry(GtpVisibility::HierarchyNode *node) const
115{
116        return !(static_cast<Octree *>(node))->mNodes.empty();
117}
118//-----------------------------------------------------------------------
119float OctreeHierarchyInterface::GetSquaredDistance(GtpVisibility::HierarchyNode *node) const
120{
121        const Vector3 bmin = static_cast<Octree *>(node)->mBox.getMinimum();
122        const Vector3 bmax = static_cast<Octree *>(node)->mBox.getMaximum();
123
124        const Vector3 pos = (bmax - bmin) * 0.5 + bmin;
125       
126/*      std::stringstream d;
127        d << "a: " << (mCameraPosition - pos).squaredLength()
128          << " b: " << (mCullCamera->getDerivedPosition() - pos).squaredLength();
129        LogManager::getSingleton().logMessage(d.str());*/
130        return (mCameraPosition - pos).squaredLength();
131}
132//-----------------------------------------------------------------------
133void OctreeHierarchyInterface::SetNodeVisible(GtpVisibility::HierarchyNode *node,
134                                                                                          const bool visible) const
135{
136#ifdef GTP_VISIBILITY_MODIFIED_OGRE
137        static_cast<Octree *>(node)->setOctreeVisible(visible);
138#endif
139}
140//-----------------------------------------------------------------------
141void OctreeHierarchyInterface::SetLastVisited(GtpVisibility::HierarchyNode *node,
142                                                                                          const unsigned int frameId) const
143{
144#ifdef GTP_VISIBILITY_MODIFIED_OGRE
145        static_cast<Octree *>(node)->setLastVisited(frameId);
146#endif
147}
148//-----------------------------------------------------------------------
149void OctreeHierarchyInterface::PullUpVisibility(GtpVisibility::HierarchyNode *node) const
150{               
151#ifdef GTP_VISIBILITY_MODIFIED_OGRE
152        Octree *octant = static_cast<Octree *>(node);
153
154        while (octant && !octant->isOctreeVisible())
155        {
156                octant->setOctreeVisible(true);
157                octant = octant->getParent();
158        }
159#endif
160}
161//-----------------------------------------------------------------------
162void OctreeHierarchyInterface::DetermineFullVisibility(GtpVisibility::HierarchyNode *node) const
163{               
164        Octree *octant = static_cast<Octree *>(node);
165
166        // leaf node: terminate recursion
167        if (IsLeaf(node))
168        {
169                octant->setOctreeFullyVisible(octant->isOctreeVisible());
170                return;
171        }
172
173        octant->setOctreeFullyVisible(false);
174
175        Octree *nextChild;
176
177        for (int i = 0; i < 8; ++ i)
178        {
179                int x = i & 4;
180                int y = i & 2;
181                int z = i & 1;
182
183                if ((nextChild = octant->mChildren[x][y][z]) != NULL)
184                {
185                        DetermineFullVisibility(nextChild);
186                        if (!nextChild->isOctreeFullyVisible())
187                        return;
188                }
189        }
190
191        // all children fully visible => pull up
192        octant->setOctreeFullyVisible(true);
193}
194//-----------------------------------------------------------------------
195void OctreeHierarchyInterface::RenderNode(GtpVisibility::HierarchyNode *node)
196{
197#ifdef GTP_VISIBILITY_MODIFIED_OGRE
198        Octree *octant = static_cast<Octree *>(node);
199
200        if (octant->lastRendered() != mFrameId)
201        {
202                octant->setLastRendered(mFrameId);
203                OctreeSceneManager *ocm =
204                        static_cast<OctreeSceneManager *>(mSceneManager);
205
206                ocm->_renderOctant(mCamera,
207                                                   octant,
208                                                   mOnlyShadowCasters,
209                                                   mLeavePassesInQueue);
210
211                mVisibleNodes.push_back(node);
212        }
213#endif 
214}
215//-----------------------------------------------------------------------
216bool OctreeHierarchyInterface::IsNodeVisible(GtpVisibility::HierarchyNode *node) const
217{
218#ifdef GTP_VISIBILITY_MODIFIED_OGRE
219        return static_cast<Octree *>(node)->isOctreeVisible();
220#else
221        return true;
222#endif
223}
224//-----------------------------------------------------------------------
225unsigned int OctreeHierarchyInterface::LastVisited(GtpVisibility::HierarchyNode *node) const
226{
227#ifdef GTP_VISIBILITY_MODIFIED_OGRE
228        return static_cast<Octree *>(node)->lastVisited();
229#else
230        return 0;
231#endif
232}
233//-----------------------------------------------------------------------
234AxisAlignedBox *OctreeHierarchyInterface::GetBoundingBox(GtpVisibility::HierarchyNode *node)
235{
236        // reuse box if node is the same
237        // only create renderable bounding box for new node
238        if (node != mSavedNode)
239        {
240                mSavedNode = node;
241            //static_cast<Octree *>(node)->_getCullBounds(&mBox);
242                mBox = static_cast<Octree *>(node)->_getWorldAABB();
243        }
244
245        return &mBox;
246}
247//-----------------------------------------------------------------------
248void OctreeHierarchyInterface::VisualizeCulledNode(GtpVisibility::HierarchyNode *node,
249                                                                                                   GtpVisibility::CullingType type) const
250{
251        WireBoundingBox *box = static_cast<Octree *>(node)->getWireBoundingBox();
252
253        if (type == GtpVisibility::FRUSTUM_CULLED)
254        {
255                box->setMaterial("FrustumCulledNodesMaterial");
256        }
257        else // type == GtpVisibility::QUERY_CULLED
258        {
259                box->setMaterial("QueryCulledNodesMaterial");
260        }
261
262        //static_cast<OctreeSceneManager *>(mSceneManager)->getBoxes()->push_back(box);
263        static_cast<OctreeSceneManager *>(mSceneManager)->getBoxes()->push_back(box);
264}
265//-----------------------------------------------------------------------
266void OctreeHierarchyInterface::GetNodeGeometryList(GtpVisibility::HierarchyNode *node,
267                                                                                                   GtpVisibility::GeometryVector *geometryList,
268                                                                                                   bool includeChildren)
269{
270        NodeList::const_iterator nodeIt, nodeIt_end;
271        nodeIt_end = static_cast<Octree *>(node)->mNodes.end();
272
273        for (nodeIt = static_cast<Octree *>(node)->mNodes.begin(); nodeIt != nodeIt_end; ++nodeIt)
274        {
275                SceneNodeHierarchyInterface::GetNodeGeometryList(*nodeIt, geometryList, includeChildren);
276        }
277}
278
279
280} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.