source: trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp @ 114

Revision 114, 8.5 KB checked in by mattausch, 20 years ago (diff)
Line 
1#include "OgreVisibilityOctreeSceneManager.h"
2#include "OgreVisibilityOptionsManager.h"
3#include <OgreMath.h>
4#include <OgreIteratorWrappers.h>
5#include <OgreRenderSystem.h>
6#include <OgreCamera.h>
7#include <OgreLogManager.h>
8
9namespace Ogre {
10
11//-----------------------------------------------------------------------
12VisibilityOctreeSceneManager::VisibilityOctreeSceneManager(
13        GtpVisibility::VisibilityManager *visManager)
14:
15mVisibilityManager(visManager),
16mUseVisibilityCulling(true),
17mShowVisualization(false),
18mRenderNodesForViz(false),
19mVisualizeCulledNodes(false),
20mRenderTransparentObjects(false)
21{
22        mHierarchyInterface =
23                new OctreeHierarchyInterface(this, mDestRenderSystem);
24               
25        //mDisplayNodes = true;
26        //mShowBoundingBoxes = true;
27        //mShowBoxes = true;
28
29        // TODO: find reasonable value for max depth
30        mMaxDepth = 50;
31}
32//-----------------------------------------------------------------------
33VisibilityOctreeSceneManager::~VisibilityOctreeSceneManager()
34{
35        delete mHierarchyInterface;
36}
37//-----------------------------------------------------------------------
38void VisibilityOctreeSceneManager::_renderVisibleObjects()
39{
40        // two cameras (one for culling, one for rendering)
41        mHierarchyInterface->InitFrame(mOctree, mCameraInProgress,
42                                                        mCullCamera ? getCamera("CullCamera") : NULL);
43
44        if (!mShowVisualization)
45        {
46                // two cameras (one for culling, one for rendering)
47                mHierarchyInterface->InitFrame(mOctree, mCameraInProgress,
48                                                                mCullCamera ? getCamera("CullCamera") : NULL);
49
50                // call initframe to reset culling manager stats
51                mVisibilityManager->GetCullingManager()->InitFrame(mVisualizeCulledNodes);
52        }
53       
54        //-- hierarchical culling
55        // the objects of different layers (e.g., background, scene,
56        // overlay) must be identified and rendered one after another
57
58        //-- render background
59        clearSpecialCaseRenderQueues();
60        addSpecialCaseRenderQueue(RENDER_QUEUE_BACKGROUND);
61        addSpecialCaseRenderQueue(RENDER_QUEUE_SKIES_EARLY);
62
63        setSpecialCaseRenderQueueMode(SceneManager::SCRQM_INCLUDE);
64        SceneManager::_renderVisibleObjects();
65
66#ifdef GTP_VISIBILITY_MODIFIED_OGRE
67        _deleteRenderedQueueGroups();
68#endif
69
70        //-- render visible objects (i.e., all but overlay)
71        clearSpecialCaseRenderQueues();
72        addSpecialCaseRenderQueue(RENDER_QUEUE_OVERLAY);
73        setSpecialCaseRenderQueueMode(SceneManager::SCRQM_EXCLUDE);
74
75
76        /**
77        * the hierarchical culling algorithm
78        **/
79        mVisibilityManager->ApplyVisibilityCulling();
80
81
82#ifdef GTP_VISIBILITY_MODIFIED_OGRE
83        _deleteRenderedQueueGroups();
84#endif
85
86        //-- render overlay
87        clearSpecialCaseRenderQueues();
88        SceneManager::_renderVisibleObjects();
89
90        //WriteLog(); // write out stats
91}
92//-----------------------------------------------------------------------
93void VisibilityOctreeSceneManager::_findVisibleObjects(Camera* cam, bool onlyShadowCasters)
94{
95        // only shadow casters will be rendered in shadow texture pass
96        mHierarchyInterface->SetOnlyShadowCasters(onlyShadowCasters);
97
98        // does nothing if hierarchical culling is used =>
99        // we interleave identification and rendering of objects
100        // in _renderVisibibleObjects
101        if (!mUseVisibilityCulling)
102        {
103                OctreeSceneManager::_findVisibleObjects(cam, onlyShadowCasters);
104                return;
105        }
106
107        //-- show visibile scene nodes and octree bounding boxes from last frame
108        if (mShowVisualization)
109    {
110                // add player camera for visualization purpose
111                try {
112                        Camera *c;
113                        if ((c = getCamera("PlayerCam")) != NULL)
114                        {
115                                getRenderQueue()->addRenderable(c);
116                        }   
117        }
118        catch(...)
119        {
120            // ignore
121        }
122               
123                if (mRenderNodesForViz || mRenderNodesContentForViz)
124                {
125                        // change node material so it is better suited for visualization
126                /*      MaterialPtr nodeMat = MaterialManager::getSingleton().getByName("Core/NodeMaterial");
127                        nodeMat->setAmbient(1, 1, 0);
128                        nodeMat->setLightingEnabled(true);
129                        nodeMat->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();*/
130
131                        for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
132                        {
133                                if (mRenderNodesForViz)
134                                {
135                                        getRenderQueue()->addRenderable(*it);
136                                }
137                                if (mRenderNodesContentForViz)
138                                {
139                                        (*it)->_addToRenderQueue(cam, getRenderQueue(), false);
140                                }
141                        }
142                }
143                for (BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it)
144                {
145                        getRenderQueue()->addRenderable(*it);
146                }
147        }
148       
149        mVisible.clear();
150    mBoxes.clear();
151}
152//-----------------------------------------------------------------------
153void VisibilityOctreeSceneManager::_updateSceneGraph(Camera* cam)
154{
155        mVisibilityManager->GetCullingManager()->SetHierarchyInterface(mHierarchyInterface);
156        mHierarchyInterface->SetRenderSystem(mDestRenderSystem);
157
158#ifdef GTP_VISIBILITY_MODIFIED_OGRE
159    mHierarchyInterface->SetNumOctreeNodes(mNumOctreeNodes);
160#endif
161        OctreeSceneManager::_updateSceneGraph(cam);
162}
163//-----------------------------------------------------------------------
164bool VisibilityOctreeSceneManager::setOption(const String & key, const void * val)
165{
166        if (key == "UseVisibilityCulling")
167        {
168                mUseVisibilityCulling = (*static_cast<const bool *>(val));
169                return true;
170        }
171        if (key == "ShowVisualization")
172        {
173                mShowVisualization = (*static_cast<const bool *>(val));
174                return true;
175        }
176        if (key == "RenderNodesForViz")
177        {
178                mRenderNodesForViz = (*static_cast<const bool *>(val));
179                return true;
180        }
181        if (key == "SkyBoxEnabled")
182        {
183                mSkyBoxEnabled = (*static_cast<const bool *>(val));
184                return true;
185        }
186        if (key == "SkyPlaneEnabled")
187        {
188                mSkyPlaneEnabled = (*static_cast<const bool *>(val));
189                return true;
190        }
191        if (key == "SkyDomeEnabled")
192        {
193                mSkyDomeEnabled = (*static_cast<const bool *>(val));
194                return true;
195        }
196        if (key == "VisualizeCulledNodes")
197        {
198                mVisualizeCulledNodes = (*static_cast<const bool *>(val));
199                return true;
200        }
201
202        return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
203                setOption(key, val) || OctreeSceneManager::setOption(key, val);
204}
205//-----------------------------------------------------------------------
206bool VisibilityOctreeSceneManager::getOption(const String & key, void *val)
207{
208        if (key == "NumHierarchyNodes")
209        {
210                * static_cast<unsigned int *>(val) = (unsigned int)mNumOctreeNodes;
211                return true;
212        }
213
214        return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
215                getOption(key, val) && OctreeSceneManager::getOption(key, val);
216}
217//-----------------------------------------------------------------------
218bool VisibilityOctreeSceneManager::getOptionValues(const String & key, StringVector  &refValueList)
219{
220        return OctreeSceneManager::getOptionValues( key, refValueList );
221}
222//-----------------------------------------------------------------------
223bool VisibilityOctreeSceneManager::getOptionKeys(StringVector & refKeys)
224{
225        return  VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
226                getOptionKeys (refKeys) || OctreeSceneManager::getOptionKeys(refKeys);
227}
228//-----------------------------------------------------------------------
229void VisibilityOctreeSceneManager::setVisibilityManager(GtpVisibility::VisibilityManager *visManager)
230{
231    mVisibilityManager = visManager;
232}
233//-----------------------------------------------------------------------
234GtpVisibility::VisibilityManager *VisibilityOctreeSceneManager::getVisibilityManager()
235{
236        return mVisibilityManager;
237}
238//-----------------------------------------------------------------------
239void VisibilityOctreeSceneManager::renderObjects(const RenderPriorityGroup::TransparentRenderablePassList& objs,
240            bool doLightIteration, const LightList* manualLightList)
241{
242        if (mRenderTransparentObjects)
243        {
244                OctreeSceneManager::renderObjects(objs, doLightIteration, manualLightList);
245        }
246}
247//-----------------------------------------------------------------------
248void VisibilityOctreeSceneManager::WriteLog()
249{
250        std::stringstream d;
251
252        d << "Algorithm: " << mVisibilityManager->GetCullingManagerType() << ", "
253          << "Hierarchy nodes: " << mNumOctreeNodes << ", " << "Traversed nodes: "
254          << mHierarchyInterface->GetNumTraversedNodes() << ", "
255          << "Rendered nodes: " << mHierarchyInterface->GetNumRenderedNodes() << ", "
256          << "Query culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumQueryCulledNodes() << ", "
257          << "Frustum culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumFrustumCulledNodes() << ", "
258      << "Queries issued: " << mVisibilityManager->GetCullingManager()->GetNumQueriesIssued() << "\n";
259
260        LogManager::getSingleton().logMessage(d.str());
261}
262}   // namespace Ogre
Note: See TracBrowser for help on using the repository browser.