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

Revision 115, 9.6 KB checked in by mattausch, 19 years ago (diff)

added depth pass algorithm + delayed transparent object rendering (so depth ordering is right)

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