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

Revision 2258, 9.1 KB checked in by mattausch, 17 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//-----------------------------------------------------------------------
[2258]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//-----------------------------------------------------------------------
[74]106bool OctreeHierarchyInterface::IsLeaf(GtpVisibility::HierarchyNode *node) const
[59]107{
[94]108        Octree *octree = static_cast<Octree *>(node);
[944]109
[94]110        // HACK: if there are subtrees, they are empty => we are not interested in them
111        return octree->numNodes() == (int)octree->mNodes.size();
[59]112}
113//-----------------------------------------------------------------------
[74]114bool OctreeHierarchyInterface::HasGeometry(GtpVisibility::HierarchyNode *node) const
[59]115{
[722]116        return !(static_cast<Octree *>(node))->mNodes.empty();
[59]117}
118//-----------------------------------------------------------------------
[87]119float OctreeHierarchyInterface::GetSquaredDistance(GtpVisibility::HierarchyNode *node) const
[59]120{
[726]121        const Vector3 bmin = static_cast<Octree *>(node)->mBox.getMinimum();
122        const Vector3 bmax = static_cast<Octree *>(node)->mBox.getMaximum();
[722]123
[726]124        const Vector3 pos = (bmax - bmin) * 0.5 + bmin;
[345]125       
[944]126/*      std::stringstream d;
127        d << "a: " << (mCameraPosition - pos).squaredLength()
128          << " b: " << (mCullCamera->getDerivedPosition() - pos).squaredLength();
129        LogManager::getSingleton().logMessage(d.str());*/
[726]130        return (mCameraPosition - pos).squaredLength();
[59]131}
132//-----------------------------------------------------------------------
[74]133void OctreeHierarchyInterface::SetNodeVisible(GtpVisibility::HierarchyNode *node,
[345]134                                                                                          const bool visible) const
[59]135{
136#ifdef GTP_VISIBILITY_MODIFIED_OGRE
137        static_cast<Octree *>(node)->setOctreeVisible(visible);
138#endif
139}
140//-----------------------------------------------------------------------
[74]141void OctreeHierarchyInterface::SetLastVisited(GtpVisibility::HierarchyNode *node,
[345]142                                                                                          const unsigned int frameId) const
[59]143{
144#ifdef GTP_VISIBILITY_MODIFIED_OGRE
145        static_cast<Octree *>(node)->setLastVisited(frameId);
146#endif
147}
148//-----------------------------------------------------------------------
[345]149void OctreeHierarchyInterface::PullUpVisibility(GtpVisibility::HierarchyNode *node) const
[59]150{               
151#ifdef GTP_VISIBILITY_MODIFIED_OGRE
152        Octree *octant = static_cast<Octree *>(node);
153
[155]154        while (octant && !octant->isOctreeVisible())
[59]155        {
156                octant->setOctreeVisible(true);
157                octant = octant->getParent();
158        }
159#endif
160}
161//-----------------------------------------------------------------------
[2258]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//-----------------------------------------------------------------------
[59]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);
[726]203                OctreeSceneManager *ocm =
[2066]204                        static_cast<OctreeSceneManager *>(mSceneManager);
[938]205
[2258]206                ocm->_renderOctant(mCamera,
207                                                   octant,
208                                                   mOnlyShadowCasters,
209                                                   mLeavePassesInQueue);
[59]210
[174]211                mVisibleNodes.push_back(node);
[59]212        }
[2258]213#endif 
[59]214}
215//-----------------------------------------------------------------------
[74]216bool OctreeHierarchyInterface::IsNodeVisible(GtpVisibility::HierarchyNode *node) const
[59]217{
218#ifdef GTP_VISIBILITY_MODIFIED_OGRE
219        return static_cast<Octree *>(node)->isOctreeVisible();
220#else
221        return true;
222#endif
223}
224//-----------------------------------------------------------------------
[74]225unsigned int OctreeHierarchyInterface::LastVisited(GtpVisibility::HierarchyNode *node) const
[59]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{
[925]236        // reuse box if node is the same
237        // only create renderable bounding box for new node
[938]238        if (node != mSavedNode)
[85]239        {
[155]240                mSavedNode = node;
[880]241            //static_cast<Octree *>(node)->_getCullBounds(&mBox);
[135]242                mBox = static_cast<Octree *>(node)->_getWorldAABB();
[85]243        }
[59]244
245        return &mBox;
246}
[112]247//-----------------------------------------------------------------------
248void OctreeHierarchyInterface::VisualizeCulledNode(GtpVisibility::HierarchyNode *node,
[345]249                                                                                                   GtpVisibility::CullingType type) const
[112]250{
251        WireBoundingBox *box = static_cast<Octree *>(node)->getWireBoundingBox();
[59]252
[112]253        if (type == GtpVisibility::FRUSTUM_CULLED)
254        {
255                box->setMaterial("FrustumCulledNodesMaterial");
256        }
257        else // type == GtpVisibility::QUERY_CULLED
258        {
259                box->setMaterial("QueryCulledNodesMaterial");
260        }
[113]261
[2066]262        //static_cast<OctreeSceneManager *>(mSceneManager)->getBoxes()->push_back(box);
263        static_cast<OctreeSceneManager *>(mSceneManager)->getBoxes()->push_back(box);
[112]264}
[130]265//-----------------------------------------------------------------------
[726]266void OctreeHierarchyInterface::GetNodeGeometryList(GtpVisibility::HierarchyNode *node,
267                                                                                                   GtpVisibility::GeometryVector *geometryList,
268                                                                                                   bool includeChildren)
[130]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        {
[155]275                SceneNodeHierarchyInterface::GetNodeGeometryList(*nodeIt, geometryList, includeChildren);
[130]276        }
277}
[174]278
[2258]279
[59]280} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.