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

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