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

Revision 120, 12.5 KB checked in by mattausch, 19 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#include <OgreStringConverter.h>
9
10
11namespace Ogre {
12
13//-----------------------------------------------------------------------
14VisibilityOctreeSceneManager::VisibilityOctreeSceneManager(
15        GtpVisibility::VisibilityManager *visManager)
16:
17mVisibilityManager(visManager),
18mIsDepthPass(false),
19mShowVisualization(false),
20mRenderNodesForViz(false),
21mRenderNodesContentForViz(false),
22mVisualizeCulledNodes(false),
23mSkipTransparents(false),
24mDelayRenderTransparents(true),
25mUseDepthPass(true)
26{
27        mHierarchyInterface = new OctreeHierarchyInterface(this, mDestRenderSystem);
28               
29        //mDisplayNodes = true;
30        //mShowBoundingBoxes = true;
31        //mShowBoxes = true;
32
33        // TODO: find reasonable value for max depth
34        mMaxDepth = 50;
35}
36//-----------------------------------------------------------------------
37void VisibilityOctreeSceneManager::InitDepthPass()
38{
39        MaterialPtr depthMat = MaterialManager::getSingleton().getByName("Visibility/DepthPass");
40
41        if (depthMat.isNull())
42    {
43                // Init
44                depthMat = MaterialManager::getSingleton().create(
45                "Visibility/DepthPass",
46                ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
47
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        if (mHierarchyInterface)
62        {
63                delete mHierarchyInterface;
64                mHierarchyInterface = NULL;
65        }
66}
67//-----------------------------------------------------------------------
68Pass *VisibilityOctreeSceneManager::setPass(Pass* pass)
69{
70        // setting vertex program is not efficient
71        Pass *usedPass = ((mIsDepthPass && pass->getDepthWriteEnabled() && !pass->hasVertexProgram()) ? mDepthPass : pass);             
72
73        /*
74        // set depth fill pass only if depth write enabled
75        Pass *usedPass = (mIsDepthPass && pass->getDepthWriteEnabled() ? mDepthPass : pass);
76
77    if (mIsDepthPass && pass->hasVertexProgram())
78    {
79                // set vertex program of current pass to depth pass
80                mDepthPass->setVertexProgram(pass->getVertexProgramName());
81
82                if (mDepthPass->hasVertexProgram())
83                {
84                        const GpuProgramPtr& prg = mDepthPass->getVertexProgram();
85                        // Load this program if not done already
86                        if (!prg->isLoaded())
87                                prg->load();
88                        // Copy params
89                        mDepthPass->setVertexProgramParameters(pass->getVertexProgramParameters());
90                }
91                else if (mDepthPass->hasVertexProgram())
92                {
93                        mDepthPass->setVertexProgram("");
94                }
95        }*/
96
97        SceneManager::setPass(usedPass);
98
99        return usedPass;
100}
101//-----------------------------------------------------------------------
102void VisibilityOctreeSceneManager::ShowVisualization(Camera *cam)
103{
104        // add player camera for visualization purpose
105        try {
106                Camera *c;
107                if ((c = getCamera("PlayerCam")) != NULL)
108                {
109                        getRenderQueue()->addRenderable(c);
110                }   
111    }
112    catch(...)
113    {
114        // ignore
115    }
116        for (BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it)
117{
118                getRenderQueue()->addRenderable(*it);
119        }
120        if (mRenderNodesForViz || mRenderNodesContentForViz)
121        {
122                // change node material so it is better suited for visualization
123                MaterialPtr nodeMat = MaterialManager::getSingleton().getByName("Core/NodeMaterial");
124                nodeMat->setAmbient(1, 1, 0);
125                nodeMat->setLightingEnabled(true);
126                nodeMat->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();
127
128                for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
129        {
130
131                        if (mRenderNodesForViz)
132                        {
133                                getRenderQueue()->addRenderable(*it);
134                                // addbounding boxes instead of node itself
135                                //(*it)->_addBoundingBoxToQueue(getRenderQueue());
136                        }
137                        if (mRenderNodesContentForViz)
138                        {
139                                (*it)->_addToRenderQueue(cam, getRenderQueue(), false);
140                        }
141                }
142        }
143       
144}
145//-----------------------------------------------------------------------
146void VisibilityOctreeSceneManager::_findVisibleObjects(Camera* cam, bool onlyShadowCasters)
147{
148        //-- show visible scene nodes and octree bounding boxes from last frame
149        if (mShowVisualization)
150    {
151                ShowVisualization(cam);
152        }
153        else
154        {       
155                mVisible.clear();
156            mBoxes.clear();
157       
158                // if there is no depth pass =>
159                // we interleave identification and rendering of objects
160                // in _renderVisibibleObjects
161
162                // only shadow casters will be rendered in shadow texture pass
163                mHierarchyInterface->SetOnlyShadowCasters(onlyShadowCasters);
164        }
165}
166//-----------------------------------------------------------------------
167void VisibilityOctreeSceneManager::_renderVisibleObjects()
168{
169        // create material for depth pass
170        InitDepthPass();
171
172        // visualization: apply standard rendering
173        if (mShowVisualization)
174        {       
175                OctreeSceneManager::_renderVisibleObjects();
176                return;
177        }
178       
179
180        //-- hierarchical culling
181        // the objects of different layers (e.g., background, scene,
182        // overlay) must be identified and rendered one after another
183
184        bool leaveTransparentsInQueue = mDelayRenderTransparents && !mUseDepthPass;
185
186        // possible two cameras (one for culling, one for rendering)
187        mHierarchyInterface->InitFrame(mOctree, mCameraInProgress,
188                                                        mCullCamera ? getCamera("CullCamera") : NULL,
189                                                        leaveTransparentsInQueue);
190
191        // call initframe to reset culling manager stats
192        mVisibilityManager->GetCullingManager()->InitFrame(mVisualizeCulledNodes);
193       
194        mSkipTransparents = false;
195
196        //-- render background, in case there is one
197        clearSpecialCaseRenderQueues();
198        addSpecialCaseRenderQueue(RENDER_QUEUE_BACKGROUND);
199        addSpecialCaseRenderQueue(RENDER_QUEUE_SKIES_EARLY);
200        setSpecialCaseRenderQueueMode(SceneManager::SCRQM_INCLUDE);
201
202        SceneManager::_renderVisibleObjects();
203
204
205#ifdef GTP_VISIBILITY_MODIFIED_OGRE
206        _deleteRenderedQueueGroups(false);
207#endif
208
209        //-- render visible objects (i.e., all but overlay)
210        clearSpecialCaseRenderQueues();
211        addSpecialCaseRenderQueue(RENDER_QUEUE_SKIES_LATE);
212        addSpecialCaseRenderQueue(RENDER_QUEUE_OVERLAY);
213        setSpecialCaseRenderQueueMode(SceneManager::SCRQM_EXCLUDE);
214       
215        // transparents are skipped from hierarchical rendering
216        // => they need sorting, thus we render them afterwards
217        mSkipTransparents = mDelayRenderTransparents;
218
219        // set state for depth pass
220    mIsDepthPass = mUseDepthPass;
221       
222        /**
223        * the hierarchical culling algorithm
224          * for depth pass: will just find objects and update depth buffer
225          * for delayed rendering: will render all but transparents
226        **/
227       
228        mVisibilityManager->ApplyVisibilityCulling();
229       
230        //-- now we can savely render all remaining objects, e.g., transparents, overlay
231        mSkipTransparents = false;
232
233        clearSpecialCaseRenderQueues();
234        SceneManager::_renderVisibleObjects();
235
236        // for depth pass: add visible nodes found with the visibility culling
237        if (mUseDepthPass)
238        {
239                for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
240                {
241                        (*it)->_addToRenderQueue(mCameraInProgress, getRenderQueue(), false);
242                }
243                mIsDepthPass = false;
244    }
245        mSkipTransparents = false;
246               
247        //-- now we can render all remaining queue objects
248        // for depth pass: all
249        // for delayed rendering: transparents, overlay
250        clearSpecialCaseRenderQueues();
251        SceneManager::_renderVisibleObjects();
252       
253        WriteLog(); // write out stats
254}
255//-----------------------------------------------------------------------
256void VisibilityOctreeSceneManager::_updateSceneGraph(Camera* cam)
257{
258        mVisibilityManager->GetCullingManager()->SetHierarchyInterface(mHierarchyInterface);
259        mHierarchyInterface->SetRenderSystem(mDestRenderSystem);
260
261#ifdef GTP_VISIBILITY_MODIFIED_OGRE
262    mHierarchyInterface->SetNumOctreeNodes(mNumOctreeNodes);
263#endif
264        OctreeSceneManager::_updateSceneGraph(cam);
265}
266//-----------------------------------------------------------------------
267bool VisibilityOctreeSceneManager::setOption(const String & key, const void * val)
268{
269        if (key == "UseDepthPass")
270        {
271                mUseDepthPass = (*static_cast<const bool *>(val));
272                return true;
273        }
274        if (key == "ShowVisualization")
275        {
276                mShowVisualization = (*static_cast<const bool *>(val));
277                return true;
278        }
279        if (key == "RenderNodesForViz")
280        {
281                mRenderNodesForViz = (*static_cast<const bool *>(val));
282                return true;
283        }
284        if (key == "RenderNodesContentForViz")
285        {
286                mRenderNodesContentForViz = (*static_cast<const bool *>(val));
287                return true;
288        }
289        if (key == "SkyBoxEnabled")
290        {
291                mSkyBoxEnabled = (*static_cast<const bool *>(val));
292                return true;
293        }
294        if (key == "SkyPlaneEnabled")
295        {
296                mSkyPlaneEnabled = (*static_cast<const bool *>(val));
297                return true;
298        }
299        if (key == "SkyDomeEnabled")
300        {
301                mSkyDomeEnabled = (*static_cast<const bool *>(val));
302                return true;
303        }
304        if (key == "VisualizeCulledNodes")
305        {
306                mVisualizeCulledNodes = (*static_cast<const bool *>(val));
307                return true;
308        }
309        if (key == "DelayRenderTransparents")
310        {
311                mDelayRenderTransparents = (*static_cast<const bool *>(val));
312                return true;
313        }
314        return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
315                setOption(key, val) || OctreeSceneManager::setOption(key, val);
316}
317//-----------------------------------------------------------------------
318bool VisibilityOctreeSceneManager::getOption(const String & key, void *val)
319{
320        if (key == "NumHierarchyNodes")
321        {
322                * static_cast<unsigned int *>(val) = (unsigned int)mNumOctreeNodes;
323                return true;
324        }
325
326        return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
327                getOption(key, val) && OctreeSceneManager::getOption(key, val);
328}
329//-----------------------------------------------------------------------
330bool VisibilityOctreeSceneManager::getOptionValues(const String & key, StringVector  &refValueList)
331{
332        return OctreeSceneManager::getOptionValues( key, refValueList );
333}
334//-----------------------------------------------------------------------
335bool VisibilityOctreeSceneManager::getOptionKeys(StringVector & refKeys)
336{
337        return  VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
338                getOptionKeys (refKeys) || OctreeSceneManager::getOptionKeys(refKeys);
339}
340//-----------------------------------------------------------------------
341void VisibilityOctreeSceneManager::setVisibilityManager(GtpVisibility::VisibilityManager *visManager)
342{
343    mVisibilityManager = visManager;
344}
345//-----------------------------------------------------------------------
346GtpVisibility::VisibilityManager *VisibilityOctreeSceneManager::getVisibilityManager()
347{
348        return mVisibilityManager;
349}
350//-----------------------------------------------------------------------
351void VisibilityOctreeSceneManager::WriteLog()
352{
353        std::stringstream d;
354
355        d << "Depth pass: " << StringConverter::toString(mUseDepthPass) << ", "
356          << "Delay transparents: " << StringConverter::toString(mDelayRenderTransparents) << ", "
357          << "Use optimization: " << StringConverter::toString(mHierarchyInterface->GetUseOptimization()) << ", "
358          << "Algorithm type: " << mVisibilityManager->GetCullingManagerType() << "\n"
359          << "Hierarchy nodes: " << mNumOctreeNodes << ", "
360          << "Traversed nodes: " << mHierarchyInterface->GetNumTraversedNodes() << ", "
361          << "Rendered nodes: " << mHierarchyInterface->GetNumRenderedNodes() << ", "
362          << "Query culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumQueryCulledNodes() << ", "
363          << "Frustum culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumFrustumCulledNodes() << ", "
364      << "Queries issued: " << mVisibilityManager->GetCullingManager()->GetNumQueriesIssued() << "\n";
365          /*<< "avg. FPS: " << mCurrentViewport->getTarget()->getAverageFPS() << ", "
366          << "best FPS: " << mCurrentViewport->getTarget()->getBestFPS() << ", "
367          << "worst FPS: " << mCurrentViewport->getTarget()->getWorstFPS() << ", "
368          << "best frame time: " <<     mCurrentViewport->getTarget()->getBestFrameTime() << ", "
369          << "worst frame time: " << mCurrentViewport->getTarget()->getWorstFrameTime() << "\n";*/
370
371        LogManager::getSingleton().logMessage(d.str());
372}
373//-----------------------------------------------------------------------
374void VisibilityOctreeSceneManager::renderObjects(const RenderPriorityGroup::TransparentRenderablePassList& objs,
375            bool doLightIteration, const LightList* manualLightList)
376{
377        if (!mSkipTransparents)
378        {
379                OctreeSceneManager::renderObjects(objs, doLightIteration, manualLightList);
380        }
381}
382
383}   // namespace Ogre
Note: See TracBrowser for help on using the repository browser.