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

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