source: trunk/VUT/Ogre/src/OgreOctreeHierarchyInterface.cpp @ 113

Revision 113, 5.1 KB checked in by mattausch, 19 years ago (diff)

fixed problems with visualization

Line 
1#include "OgreOctreeHierarchyInterface.h"
2#include "OgreVisibilityOctreeSceneManager.h"
3#include <OgreOctree.h>
4#include <OgreLogManager.h>
5#include <OgreStringConverter.h>
6#include <windows.h>
7
8namespace Ogre {
9
10//-----------------------------------------------------------------------
11OctreeHierarchyInterface::OctreeHierarchyInterface(SceneManager *sm, RenderSystem *rsys):
12PlatformHierarchyInterface(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 not all subtrees are empty
29        //if (octree->numNodes() > (int)octree->mNodes.size())
30        if (!IsLeaf(node))
31        {
32                for(int i=0; i<8; ++i)
33                {
34                        Octree *nextChild =
35                                octree->mChildren[(i & 4) >> 2][(i & 2) >> 1][i & 1];
36
37                        if (nextChild)
38                        {
39                                mDistanceQueue->push(nextChild);
40                        }
41                }
42        }
43}
44//-----------------------------------------------------------------------
45bool OctreeHierarchyInterface::IsLeaf(GtpVisibility::HierarchyNode *node) const
46{
47        Octree *octree = static_cast<Octree *>(node);
48        // HACK: if there are subtrees, they are empty => we are not interested in them
49        return octree->numNodes() == (int)octree->mNodes.size();
50}
51//-----------------------------------------------------------------------
52bool OctreeHierarchyInterface::HasGeometry(GtpVisibility::HierarchyNode *node) const
53{
54        return static_cast<Octree *>(node)->mNodes.size() > 0;
55}
56//-----------------------------------------------------------------------
57void OctreeHierarchyInterface::SetNumOctreeNodes(unsigned int num)
58{
59        mNumOctreeNodes = num;
60}
61//-----------------------------------------------------------------------
62float OctreeHierarchyInterface::GetSquaredDistance(GtpVisibility::HierarchyNode *node) const
63{
64        AxisAlignedBox *box = &static_cast<Octree *>(node)->mBox;
65        Vector3 mid = ((box->getMaximum() - box->getMinimum()) * 0.5) + box->getMinimum();
66
67        return (mCullCamera->getDerivedPosition() - mid).squaredLength();
68}
69//-----------------------------------------------------------------------
70void OctreeHierarchyInterface::SetNodeVisible(GtpVisibility::HierarchyNode *node,
71                                                                                          const bool visible)
72{
73#ifdef GTP_VISIBILITY_MODIFIED_OGRE
74        static_cast<Octree *>(node)->setOctreeVisible(visible);
75#endif
76}
77//-----------------------------------------------------------------------
78void OctreeHierarchyInterface::SetLastVisited(GtpVisibility::HierarchyNode *node,
79                                                                                          const unsigned int frameId)
80{
81#ifdef GTP_VISIBILITY_MODIFIED_OGRE
82        static_cast<Octree *>(node)->setLastVisited(frameId);
83#endif
84}
85//-----------------------------------------------------------------------
86void OctreeHierarchyInterface::PullUpVisibility(GtpVisibility::HierarchyNode *node)
87{               
88#ifdef GTP_VISIBILITY_MODIFIED_OGRE
89        Octree *octant = static_cast<Octree *>(node);
90
91        while(octant && !octant->isOctreeVisible())
92        {
93                octant->setOctreeVisible(true);
94                octant = octant->getParent();
95        }
96#endif
97}
98//-----------------------------------------------------------------------
99void OctreeHierarchyInterface::RenderNode(GtpVisibility::HierarchyNode *node)
100{
101#ifdef GTP_VISIBILITY_MODIFIED_OGRE
102        Octree *octant = static_cast<Octree *>(node);
103
104        if (octant->lastRendered() != mFrameId)
105        {
106                octant->setLastRendered(mFrameId);
107
108                static_cast<OctreeSceneManager *>(mSceneManager)->_renderOctant(mCamera,
109                        octant, mOnlyShadowCasters);
110
111                mNumRenderedNodes ++;
112        }
113#endif
114}
115//-----------------------------------------------------------------------
116bool OctreeHierarchyInterface::IsNodeVisible(GtpVisibility::HierarchyNode *node) const
117{
118#ifdef GTP_VISIBILITY_MODIFIED_OGRE
119        return static_cast<Octree *>(node)->isOctreeVisible();
120#else
121        return true;
122#endif
123}
124//-----------------------------------------------------------------------
125unsigned int OctreeHierarchyInterface::LastVisited(GtpVisibility::HierarchyNode *node) const
126{
127#ifdef GTP_VISIBILITY_MODIFIED_OGRE
128        return static_cast<Octree *>(node)->lastVisited();
129#else
130        return 0;
131#endif
132}
133//-----------------------------------------------------------------------
134AxisAlignedBox *OctreeHierarchyInterface::GetBoundingBox(GtpVisibility::HierarchyNode *node)
135{
136        if(node != mPreviousNode)
137        {
138                mPreviousNode = node;
139                static_cast<Octree *>(node)->_getCullBounds(&mBox);
140        }
141
142        return &mBox;
143}
144//-----------------------------------------------------------------------
145void OctreeHierarchyInterface::VisualizeCulledNode(GtpVisibility::HierarchyNode *node,
146                                                                                                   GtpVisibility::CullingType type)
147{
148        WireBoundingBox *box = static_cast<Octree *>(node)->getWireBoundingBox();
149
150        if (type == GtpVisibility::FRUSTUM_CULLED)
151        {
152                box->setMaterial("FrustumCulledNodesMaterial");
153        }
154        else // type == GtpVisibility::QUERY_CULLED
155        {
156                box->setMaterial("QueryCulledNodesMaterial");
157        }
158
159        static_cast<OctreeSceneManager *>(mSceneManager)->getBoxes()->push_back(box);
160}
161} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.