source: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeHierarchyInterface.cpp @ 1187

Revision 1187, 4.2 KB checked in by szydlowski, 18 years ago (diff)

visualization in test app working, some issues to resolve

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of the GameTools Project
4http://www.gametools.org
5
6Author: Martin Szydlowski
7-----------------------------------------------------------------------------
8*/
9
10#include "OgreKdRenderable.h"
11#include "OgreKdTreeSceneManager.h"
12#include "OgreKdTreeHierarchyInterface.h"
13
14namespace Ogre
15{
16
17KdTreeHierarchyInterface::KdTreeHierarchyInterface(KdTreeSceneManager *sm, RenderSystem *rsys):
18PlatformHierarchyInterface(sm, rsys)
19{
20
21}
22
23bool KdTreeHierarchyInterface::IsLeaf(GtpVisibility::HierarchyNode *node) const
24{
25        return KDNODEPTR_CAST(node)->isLeaf();
26}
27
28void KdTreeHierarchyInterface::TraverseNode(GtpVisibility::HierarchyNode *node)
29{
30        ++ mNumTraversedNodes;
31
32        KdTree::Node * kdnode = KDNODEPTR_CAST(node);
33
34        // if the node is a leaf and has geometry => render it
35        if (kdnode->isLeaf())
36        {
37                if (!kdnode->isEmpty())
38                {
39                        RenderNode(node);
40                }
41        }
42        else
43        {
44                KdTree::Branch * kdbranch = KDBRANCHPTR_CAST(node);
45                if (kdbranch->mLeft)
46                        mDistanceQueue->push(kdbranch->mLeft);
47                if (kdbranch->mRight)
48                        mDistanceQueue->push(kdbranch->mRight);
49        }
50}
51
52void KdTreeHierarchyInterface::RenderNode(GtpVisibility::HierarchyNode *node)
53{
54        KdTree::Node * kdnode = KDNODEPTR_CAST(node);
55        if (kdnode->lastRendered() != mFrameId)
56        {
57                kdnode->setLastRendered(mFrameId);
58                KdTreeSceneManager * ksm = static_cast<KdTreeSceneManager *>(mSceneManager);
59
60                ksm->_renderNode(kdnode, mCamera, mOnlyShadowCasters, mLeavePassesInQueue);
61
62                mVisibleNodes.push_back(node);
63        }
64}
65
66void KdTreeHierarchyInterface::PullUpVisibility(GtpVisibility::HierarchyNode *node) const
67{
68        KdTree::Node * kdnode = KDNODEPTR_CAST(node);
69
70        while (kdnode && !kdnode->isNodeVisible())
71        {
72                kdnode->setNodeVisible(true);
73                kdnode = kdnode->getParent();
74        }
75}
76
77float KdTreeHierarchyInterface::GetSquaredDistance(GtpVisibility::HierarchyNode *node) const
78{
79        const Vector3 pos = KDNODEPTR_CAST(node)->mAABB.getCenter();
80        return (mCameraPosition - pos).squaredLength();
81}
82
83AxisAlignedBox * KdTreeHierarchyInterface::GetBoundingBox(GtpVisibility::HierarchyNode *node)
84{
85        // reuse box if node is the same
86        // only create renderable bounding box for new node
87        if (node != mSavedNode)
88        {
89                mSavedNode = node;
90                mBox = KDNODEPTR_CAST(node)->_getWorldAABB();
91        }
92
93        return &mBox;
94}
95
96bool KdTreeHierarchyInterface::HasGeometry(GtpVisibility::HierarchyNode *node) const
97{
98        return KDNODEPTR_CAST(node)->hasGeometry();
99}
100
101void KdTreeHierarchyInterface::SetNodeVisible(GtpVisibility::HierarchyNode *node, const bool visible) const
102{
103        KDNODEPTR_CAST(node)->setNodeVisible(visible);
104}
105
106bool KdTreeHierarchyInterface::IsNodeVisible(GtpVisibility::HierarchyNode *node) const
107{
108        return KDNODEPTR_CAST(node)->isNodeVisible();
109}
110
111void KdTreeHierarchyInterface::SetLastVisited(GtpVisibility::HierarchyNode *node, const unsigned int frameId) const
112{
113        KDNODEPTR_CAST(node)->setLastVisited(frameId);
114}
115
116unsigned int KdTreeHierarchyInterface::LastVisited(GtpVisibility::HierarchyNode *node) const
117{
118        return KDNODEPTR_CAST(node)->lastVisited();
119}
120
121void KdTreeHierarchyInterface::VisualizeCulledNode(GtpVisibility::HierarchyNode *node,
122                                                                                                   GtpVisibility::CullingType type) const
123{
124        WireBoundingBox *box = KDNODEPTR_CAST(node)->getWireBoundingBox();
125
126        if (type == GtpVisibility::FRUSTUM_CULLED)
127        {
128                box->setMaterial("FrustumCulledNodesMaterial");
129        }
130        else // type == GtpVisibility::QUERY_CULLED
131        {
132                box->setMaterial("QueryCulledNodesMaterial");
133        }
134
135        // TODO: maybe boxlist?
136        dynamic_cast<KdTreeSceneManager *>(mSceneManager)->getRenderQueue()->addRenderable(box);
137}
138
139void KdTreeHierarchyInterface::GetNodeGeometryList(GtpVisibility::HierarchyNode *node,
140                                                                                                   GtpVisibility::GeometryVector *geometryList,
141                                                                                                   bool includeChildren)
142{
143        KdTree::Node * kdnode = KDNODEPTR_CAST(node);
144        if (kdnode->isLeaf())
145        {
146                KdTree::Leaf * kdleaf = KDLEAFPTR_CAST(node);
147                KdRenderableList::iterator it = kdleaf->mKdRenderables.begin();
148                KdRenderableList::iterator end = kdleaf->mKdRenderables.end();
149                while (it != end)
150                {
151                        (*it)->getGeometryList(geometryList);
152                        it++;
153                }
154        }
155}
156
157} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.