source: trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp @ 117

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