source: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreOcclusionCullingSceneManager.cpp @ 1193

Revision 1193, 36.5 KB checked in by mattausch, 18 years ago (diff)

bug fixed

RevLine 
[720]1#include "OgreOcclusionCullingSceneManager.h"
[675]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#include <OgreEntity.h>
10#include <OgreSubEntity.h>
[897]11#include <OgreMaterialManager.h>
[863]12#include <OgreIteratorWrappers.h>
[925]13#include <OgreHeightmapTerrainPageSource.h>
[828]14#include "VspBspTree.h"
[863]15#include "Containers.h"
16#include "ViewCellsManager.h"
[903]17#include <OgreConfigFile.h>
18#include "OgreTypeConverter.h"
19#include "OgreMeshInstance.h"
20#include "common.h"
[931]21#include "OgreBoundingBoxConverter.h"
[675]22
[868]23// normal terrain rendering
24const static NORMAL_RENDER_HACK = false;
25
[675]26namespace Ogre {
27
28//-----------------------------------------------------------------------
[897]29OcclusionCullingSceneManager::OcclusionCullingSceneManager(const String& name,
30                                                        GtpVisibility::VisibilityManager *visManager):
31TerrainSceneManager(name),
[675]32mVisibilityManager(visManager),
33mShowVisualization(false),
34mRenderNodesForViz(false),
35mRenderNodesContentForViz(false),
36mVisualizeCulledNodes(false),
37mLeavePassesInQueue(0),
38mDelayRenderTransparents(true),
39mUseDepthPass(false),
40mIsDepthPassPhase(false),
41mUseItemBuffer(false),
[897]42//mUseItemBuffer(true),
[675]43mIsItemBufferPhase(false),
44mCurrentEntityId(1),
45mEnableDepthWrite(true),
46mSkipTransparents(false),
47mRenderTransparentsForItemBuffer(true),
[925]48//mExecuteVertexProgramForAllPasses(true),
49mExecuteVertexProgramForAllPasses(false),
[903]50mIsHierarchicalCulling(false),
51mViewCellsLoaded(false),
52mUseViewCells(false),
53mUseVisibilityFilter(false),
54mCurrentViewCell(NULL),
55mElementaryViewCell(NULL)
[675]56{
57        mHierarchyInterface = new OctreeHierarchyInterface(this, mDestRenderSystem);
58       
[868]59        if (0)
60        {
[925]61                mDisplayNodes = true;
62                mShowBoundingBoxes = true;
63                mShowBoxes = true;
[868]64        }
65
[675]66        // TODO: set maxdepth to reasonable value
67        mMaxDepth = 50;
68}
69//-----------------------------------------------------------------------
[720]70void OcclusionCullingSceneManager::InitDepthPass()
[675]71{
72        MaterialPtr depthMat = MaterialManager::getSingleton().getByName("Visibility/DepthPass");
73
74        if (depthMat.isNull())
75    {
76                depthMat = MaterialManager::getSingleton().create(
77                "Visibility/DepthPass",
78                ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
79
80        mDepthPass = depthMat->getTechnique(0)->getPass(0);
81                mDepthPass->setColourWriteEnabled(false);
82                mDepthPass->setDepthWriteEnabled(true);
83                mDepthPass->setLightingEnabled(false);
84        }
85        else
86        {
87                mDepthPass = depthMat->getTechnique(0)->getPass(0);
88        }
89}
90//-----------------------------------------------------------------------
[720]91OcclusionCullingSceneManager::~OcclusionCullingSceneManager()
[675]92{
93        OGRE_DELETE(mHierarchyInterface);
[903]94        CLEAR_CONTAINER(mObjects);
95        OGRE_DELETE(mCurrentViewCell);
[675]96}
97//-----------------------------------------------------------------------
[720]98void OcclusionCullingSceneManager::InitItemBufferPass()
[675]99{
100        MaterialPtr itemBufferMat = MaterialManager::getSingleton().
101                getByName("Visibility/ItemBufferPass");
102
103        if (itemBufferMat.isNull())
104    {
105                // Init
106                itemBufferMat = MaterialManager::getSingleton().create("Visibility/ItemBufferPass",
107                ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
108
109                mItemBufferPass = itemBufferMat->getTechnique(0)->getPass(0);
110                mItemBufferPass->setColourWriteEnabled(true);
111                mItemBufferPass->setDepthWriteEnabled(true);
112                mItemBufferPass->setLightingEnabled(true);
113                //mItemBufferPass->setLightingEnabled(false);
114        }
115        else
116        {
117                mItemBufferPass = itemBufferMat->getTechnique(0)->getPass(0);
118        }
119        //mItemBufferPass->setAmbient(1, 1, 0);
120}
[897]121//-------------------------------------------------------------------------
[925]122#if 1
[897]123void OcclusionCullingSceneManager::setWorldGeometry( DataStreamPtr& stream, const String& typeName )
124{
125    // Clear out any existing world resources (if not default)
126    if (ResourceGroupManager::getSingleton().getWorldResourceGroupName() !=
127        ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)
128    {
129        ResourceGroupManager::getSingleton().clearResourceGroup(
130            ResourceGroupManager::getSingleton().getWorldResourceGroupName());
131    }
132        destroyLevelIndexes();
133    mTerrainPages.clear();
134    // Load the configuration
[938]135    loadConfig(stream);
[897]136        initLevelIndexes();
137
138    // Resize the octree, allow for 1 page for now
139    float max_x = mOptions.scale.x * mOptions.pageSize;
140    float max_y = mOptions.scale.y;
141    float max_z = mOptions.scale.z * mOptions.pageSize;
142
143        float maxAxis = std::max(max_x, max_y);
144        maxAxis = std::max(maxAxis, max_z);
145        resize( AxisAlignedBox( 0, 0, 0, maxAxis, maxAxis, maxAxis ) );
146   
147    setupTerrainMaterial();
148
149    setupTerrainPages();
150
151 }
[925]152#endif
[675]153//-----------------------------------------------------------------------
[720]154void OcclusionCullingSceneManager::PrepareVisualization(Camera *cam)
[675]155{
156        // add player camera for visualization purpose
157        try
158        {
159                Camera *c;
160                if ((c = getCamera("PlayerCam")) != NULL)
161                {
162                        getRenderQueue()->addRenderable(c);
163                }   
164    }
165    catch (...)
166    {
167        // ignore
168    }
[868]169        // add bounding boxes of rendered objects
[944]170        if (0)
[675]171        for (BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it)
172        {
173                getRenderQueue()->addRenderable(*it);
174        }
[868]175       
[675]176        if (mRenderNodesForViz || mRenderNodesContentForViz)
177        {
178                // HACK: change node material so it is better suited for visualization
179                MaterialPtr nodeMat = MaterialManager::getSingleton().getByName("Core/NodeMaterial");
180                nodeMat->setAmbient(1, 1, 0);
181                nodeMat->setLightingEnabled(true);
182                nodeMat->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();
183
184                for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
185                {
186                        if (mRenderNodesForViz)
187                        {
188                                // render the leaf nodes
[925]189                                if ((*it)->numAttachedObjects() &&
190                                        !(*it)->numChildren() &&
191                                        ((*it)->getAttachedObject(0)->getMovableType() == "Entity") &&
192                                        (*it)->getAttachedObject(0)->isVisible())
[675]193                                {
194                                        getRenderQueue()->addRenderable((*it));
195                                }
196
197                                // addbounding boxes instead of node itself
198                                //(*it)->_addBoundingBoxToQueue(getRenderQueue());
199                        }
[903]200                        // add renderables itself
[675]201                        if (mRenderNodesContentForViz)
202                        {
203                                (*it)->_addToRenderQueue(cam, getRenderQueue(), false);
204                        }
205                }
206        }       
207}
208//-----------------------------------------------------------------------
[897]209const Pass *OcclusionCullingSceneManager::_setPass(Pass* pass)
[675]210{
[868]211        if (NORMAL_RENDER_HACK)
212        {
[897]213                return SceneManager::_setPass(pass);
[868]214        }
215
[675]216        // TODO: setting vertex program is not efficient
217        //Pass *usedPass = ((mIsDepthPassPhase && !pass->hasVertexProgram()) ? mDepthPass : pass);
218       
219        // set depth fill pass if we currently do not make an aabb occlusion query
[868]220        const bool useDepthPass =
221                (mIsDepthPassPhase && !mHierarchyInterface->IsBoundingBoxQuery());
222
223        Pass *usedPass = useDepthPass ? mDepthPass : pass;
[675]224               
[925]225
226        const IlluminationRenderStage savedStage = mIlluminationStage;
[675]227       
228        // set illumination stage to NONE so no shadow material is used
229        // for depth pass or for occlusion query
230        if (mIsDepthPassPhase || mHierarchyInterface->IsBoundingBoxQuery())
231        {
232                mIlluminationStage = IRS_NONE;
233        }
234       
235        // --- set vertex program of current pass in order to set correct depth
[868]236        if (mExecuteVertexProgramForAllPasses &&
237                mIsDepthPassPhase &&
238                pass->hasVertexProgram())
[675]239        {
240                // add vertex program of current pass to depth pass
241                mDepthPass->setVertexProgram(pass->getVertexProgramName());
242
243                if (mDepthPass->hasVertexProgram())
244                {
245                        const GpuProgramPtr& prg = mDepthPass->getVertexProgram();
246                        // Load this program if not done already
247                        if (!prg->isLoaded())
248                                prg->load();
249                        // Copy params
250                        mDepthPass->setVertexProgramParameters(pass->getVertexProgramParameters());
251                }
252        }
[868]253        else if (mDepthPass->hasVertexProgram()) // reset vertex program
[675]254        {
255                mDepthPass->setVertexProgram("");
256        }
257       
[868]258        // save old depth write: needed for item buffer
259        const bool IsDepthWrite = usedPass->getDepthWriteEnabled();
[675]260
261        // global option which enables / disables depth writes
262        if (!mEnableDepthWrite)
263        {
264                usedPass->setDepthWriteEnabled(false);
265        }
266        //else if (mIsItemBufferPass) {usedPass = mItemBufferPass;}
267       
[868]268
269        //-- set actual pass here
[897]270        const Pass *result = SceneManager::_setPass(usedPass);
[675]271
[868]272
[675]273        // reset depth write
274        if (!mEnableDepthWrite)
275        {
276                usedPass->setDepthWriteEnabled(IsDepthWrite);
277        }
278
279        // reset illumination stage
280        mIlluminationStage = savedStage;
281
282        return result;
283}
284//-----------------------------------------------------------------------
[897]285void OcclusionCullingSceneManager::_findVisibleObjects(Camera* cam,
286                                                                                                                bool onlyShadowCasters)
[675]287{
[868]288        if (NORMAL_RENDER_HACK)
289        {
290                OctreeSceneManager::_findVisibleObjects(cam, onlyShadowCasters);
291                return;
292        }
293
[903]294
[675]295        //-- show visible scene nodes and octree bounding boxes from last frame
296        if (mShowVisualization)
297    {
298                PrepareVisualization(cam);
299        }
300        else
301        {       
302                // for hierarchical culling, we interleave identification
303                // and rendering of objects in _renderVisibibleObjects
304
305                // for the shadow pass we use only standard rendering
306                // because of low occlusion
307                if (mShadowTechnique == SHADOWTYPE_TEXTURE_MODULATIVE &&
308                        mIlluminationStage == IRS_RENDER_TO_TEXTURE)
309                {
[868]310                        OctreeSceneManager::_findVisibleObjects(cam, onlyShadowCasters);
[675]311                }
[903]312
[675]313                // only shadow casters will be rendered in shadow texture pass
[868]314                if (0) mHierarchyInterface->SetOnlyShadowCasters(onlyShadowCasters);
[903]315
316
317                //-- apply view cell pvs
318                updatePvs(cam);
[675]319        }
320       
321       
322        // -- delete lists stored for visualization
323        mVisible.clear();
324        mBoxes.clear();
325}
326//-----------------------------------------------------------------------
[720]327void OcclusionCullingSceneManager::_renderVisibleObjects()
[675]328{
[868]329        if (NORMAL_RENDER_HACK)
330        {
331                OctreeSceneManager::_renderVisibleObjects();
[675]332
[868]333                return;
334        }
335
[675]336        InitDepthPass();          // create material for depth pass
337        InitItemBufferPass(); // create material for item buffer pass
338
339        // save ambient light to reset later
340        ColourValue savedAmbient = mAmbientLight;
341
342        //-- apply standard rendering for some modes (e.g., visualization, shadow pass)
343
344        if (mShowVisualization ||
345           (mShadowTechnique == SHADOWTYPE_TEXTURE_MODULATIVE &&
346            mIlluminationStage == IRS_RENDER_TO_TEXTURE))
347        {       
348                IlluminationRenderStage savedStage = mIlluminationStage;
349       
350                if (mShowVisualization)
[868]351                {
[675]352                        // disable illumination stage to prevent rendering shadows
353                        mIlluminationStage = IRS_NONE;
[868]354                }
[675]355
356                // standard rendering for shadow maps because of performance
357                TerrainSceneManager::_renderVisibleObjects();
358
359                mIlluminationStage = savedStage;
360        }
361        else //-- the hierarchical culling algorithm
362        {
[868]363                // this is also called in TerrainSceneManager: really
364                // nexessary?
365                mDestRenderSystem -> setLightingEnabled(false);
366
[675]367                // don't render backgrounds for item buffer
368                if (mUseItemBuffer)
369                {
370                        clearSpecialCaseRenderQueues();
371                        getRenderQueue()->clear();
372                }
373
374                //-- hierarchical culling
375                // the objects of different layers (e.g., background, scene,
376                // overlay) must be identified and rendered one after another
377
378                //-- render all early skies
379                clearSpecialCaseRenderQueues();
380                addSpecialCaseRenderQueue(RENDER_QUEUE_BACKGROUND);
381                addSpecialCaseRenderQueue(RENDER_QUEUE_SKIES_EARLY);
382                setSpecialCaseRenderQueueMode(SceneManager::SCRQM_INCLUDE);
383
384                TerrainSceneManager::_renderVisibleObjects();
385
386#ifdef GTP_VISIBILITY_MODIFIED_OGRE
387                // delete previously rendered content
388                _deleteRenderedQueueGroups();
389#endif
390
391                //-- prepare queue for visible objects (i.e., all but overlay and skies late)
392                clearSpecialCaseRenderQueues();
393                addSpecialCaseRenderQueue(RENDER_QUEUE_SKIES_LATE);
394                addSpecialCaseRenderQueue(RENDER_QUEUE_OVERLAY);
[897]395       
[675]396                // exclude this queues from hierarchical rendering
397                setSpecialCaseRenderQueueMode(SceneManager::SCRQM_EXCLUDE);
398
[903]399
[675]400                // set all necessary parameters for
401                // hierarchical visibility culling and rendering
402                InitVisibilityCulling(mCameraInProgress);
403
[897]404
[675]405                /**
406                * the hierarchical culling algorithm
407                * for depth pass: we just find objects and update depth buffer
408                * for "delayed" rendering: we render some passes afterwards
409                * e.g., transparents, because they need front-to-back sorting
410                **/
411               
412                mVisibilityManager->ApplyVisibilityCulling();
413
[868]414                // delete remaining renderables from queue:
415                // all which are not in mLeavePassesInQueue)
[675]416#ifdef GTP_VISIBILITY_MODIFIED_OGRE
417                _deleteRenderedQueueGroups(mLeavePassesInQueue);
418#endif
419
420                //-- reset parameters
421                mIsDepthPassPhase = false;
422                mIsItemBufferPhase = false;
423                mSkipTransparents = false;
[868]424                mIsHierarchicalCulling = false;
425               
[675]426                mLeavePassesInQueue = 0;
[897]427               
[868]428#if 1   
[675]429                // add visible nodes found by the visibility culling algorithm
430                if (mUseDepthPass)
431                {
[868]432                        NodeList::const_iterator it, it_end = mVisible.end();
433
434                        //getRenderQueue()->clear();
435                        for (it = mVisible.begin(); it != it_end; ++ it)
[675]436                        {
437                                (*it)->_addToRenderQueue(mCameraInProgress, getRenderQueue(), false);
438                        }
439                }
[868]440#endif 
[675]441                //-- now we can render all remaining queue objects
[868]442                //-- used for depth pass, transparents, overlay
[675]443                clearSpecialCaseRenderQueues();
444
445                TerrainSceneManager::_renderVisibleObjects();
[868]446        } // hierarchical culling
[675]447               
448        // HACK: set the new render level index, important to avoid cracks
449        // in terrain caused by LOD
450        TerrainRenderable::NextRenderLevelIndex();
451       
452        // reset ambient light
453        setAmbientLight(savedAmbient);
454
455        getRenderQueue()->clear(); // finally clear render queue
[868]456        if (0) OGRE_DELETE(mRenderQueue); // HACK: should rather only be cleared ...
457        if (0) WriteLog(); // write out stats
[675]458}
459
460//-----------------------------------------------------------------------
[720]461void OcclusionCullingSceneManager::_updateSceneGraph(Camera* cam)
[675]462{
[868]463        if (NORMAL_RENDER_HACK)
464        {
465                OctreeSceneManager::_updateSceneGraph(cam);
466                return;
467        }
468
[675]469        mVisibilityManager->GetCullingManager()->SetHierarchyInterface(mHierarchyInterface);
470        mHierarchyInterface->SetRenderSystem(mDestRenderSystem);
471
472        TerrainSceneManager::_updateSceneGraph(cam);
473}
474//-----------------------------------------------------------------------
[720]475bool OcclusionCullingSceneManager::setOption(const String & key, const void * val)
[675]476{
477        if (key == "UseDepthPass")
478        {
479                mUseDepthPass = (*static_cast<const bool *>(val));
480                return true;
481        }
482        if (key == "PrepareVisualization")
483        {
484                mShowVisualization = (*static_cast<const bool *>(val));
485                return true;
486        }
487        if (key == "RenderNodesForViz")
488        {
489                mRenderNodesForViz = (*static_cast<const bool *>(val));
490                return true;
491        }
492        if (key == "RenderNodesContentForViz")
493        {
494                mRenderNodesContentForViz = (*static_cast<const bool *>(val));
495                return true;
496        }
497        if (key == "SkyBoxEnabled")
498        {
499                mSkyBoxEnabled = (*static_cast<const bool *>(val));
500                return true;
501        }
502        if (key == "SkyPlaneEnabled")
503        {
504                mSkyPlaneEnabled = (*static_cast<const bool *>(val));
505                return true;
506        }
507        if (key == "SkyDomeEnabled")
508        {
509                mSkyDomeEnabled = (*static_cast<const bool *>(val));
510                return true;
511        }
512        if (key == "VisualizeCulledNodes")
513        {
514                mVisualizeCulledNodes = (*static_cast<const bool *>(val));
515                return true;
516        }
517        if (key == "DelayRenderTransparents")
518        {
519                mDelayRenderTransparents = (*static_cast<const bool *>(val));
520                return true;
521        }
522
523        if (key == "DepthWrite")
524        {
525                mEnableDepthWrite = (*static_cast<const bool *>(val));
526                return true;
527        }
528        if (key == "UseItemBuffer")
529        {
530                mUseItemBuffer = (*static_cast<const bool *>(val));
531                return true;
532        }
533        if (key == "ExecuteVertexProgramForAllPasses")
534        {
535                mExecuteVertexProgramForAllPasses  = (*static_cast<const bool *>(val));
536                return true;
537        }
538        if (key == "RenderTransparentsForItemBuffer")
539        {
540                mRenderTransparentsForItemBuffer  = (*static_cast<const bool *>(val));
541                return true;
542        }
543        if (key == "NodeVizScale")
544        {
545                OctreeNode::setVizScale(*static_cast<const float *>(val));
546                return true;
547        }
[903]548        if (key == "LoadViewCells")
549        {
550                if (!mViewCellsLoaded)
551                {
[938]552                        String filename(static_cast<const char *>(val));
553                        mViewCellsLoaded = LoadViewCells(filename);     
[903]554                }
[675]555
[938]556                return mViewCellsLoaded;
[903]557        }
558        if (key == "UseViewCells")
559        {
[938]560                if (mViewCellsLoaded)
561                {
562                        mUseViewCells = *static_cast<const bool *>(val);
[903]563
[938]564                        // reset view cell
565                        OGRE_DELETE(mCurrentViewCell);
566
567                        if (mUseViewCells)
568                                mCurrentViewCell = mViewCellsManager->GenerateViewCell();
569
570                        mElementaryViewCell = NULL;
571
572                        // if using view cells, all objects are set to false initially
573                        SetObjectsVisible(!mUseViewCells);
574                }
575
[903]576                return true;
577        }
578        if (key == "UseVisibilityFilter")
[675]579        {
[903]580                mUseVisibilityFilter = *static_cast<const bool *>(val);
581                // set null =>recomputation of the pvs
582        mElementaryViewCell = NULL;
583                return true;
584        }
[675]585
586        return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
587                setOption(key, val) || TerrainSceneManager::setOption(key, val);
588}
589//-----------------------------------------------------------------------
[720]590bool OcclusionCullingSceneManager::getOption(const String & key, void *val)
[675]591{
592        if (key == "NumHierarchyNodes")
593        {
594                * static_cast<unsigned int *>(val) = (unsigned int)mNumOctants;
595                return true;
596        }
[925]597        if (key == "VisibilityManager")
598        {
599                * static_cast<GtpVisibility::VisibilityManager **>(val) =
600                        (GtpVisibility::VisibilityManager *)mVisibilityManager;
601                return true;
602        }
603        if (key == "HierarchInterface")
604        {
605                * static_cast<GtpVisibility::HierarchyInterface **>(val) =
606                        (GtpVisibility::HierarchyInterface *)mHierarchyInterface;
607                return true;
608        }
609
[675]610        return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
611                getOption(key, val) && TerrainSceneManager::getOption(key, val);
612}
613//-----------------------------------------------------------------------
[720]614bool OcclusionCullingSceneManager::getOptionValues(const String & key,
[675]615                                                                                                        StringVector &refValueList)
616{
617        return TerrainSceneManager::getOptionValues( key, refValueList);
618}
619//-----------------------------------------------------------------------
[720]620bool OcclusionCullingSceneManager::getOptionKeys(StringVector & refKeys)
[675]621{
622        return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
623                getOptionKeys(refKeys) || TerrainSceneManager::getOptionKeys(refKeys);
624}
625//-----------------------------------------------------------------------
[720]626void OcclusionCullingSceneManager::setVisibilityManager(GtpVisibility::
[675]627                                                                                                                 VisibilityManager *visManager)
628{
629        mVisibilityManager = visManager;
630}
631//-----------------------------------------------------------------------
[720]632GtpVisibility::VisibilityManager *OcclusionCullingSceneManager::getVisibilityManager( void )
[675]633{
634        return mVisibilityManager;
635}
636//-----------------------------------------------------------------------
[720]637void OcclusionCullingSceneManager::WriteLog()
[675]638{
639        std::stringstream d;
640
641        d << "Depth pass: " << StringConverter::toString(mUseDepthPass) << ", "
642          << "Delay transparents: " << StringConverter::toString(mDelayRenderTransparents) << ", "
643          << "Use optimization: " << StringConverter::toString(mHierarchyInterface->GetTestGeometryForVisibleLeaves()) << ", "
644          << "Algorithm type: " << mVisibilityManager->GetCullingManagerType() << ", "
645          << "Hierarchy nodes: " << mNumOctants << ", "
646          << "Traversed nodes: " << mHierarchyInterface->GetNumTraversedNodes() << ", "
647          << "Rendered nodes: " << mHierarchyInterface->GetNumRenderedNodes() << ", "
648          << "Query culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumQueryCulledNodes() << ", "
649          << "Frustum culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumFrustumCulledNodes() << ", "
[868]650      << "Queries issued: " << mVisibilityManager->GetCullingManager()->GetNumQueriesIssued() << ", "
651          << "Found objects: " << (int)mVisible.size() << "\n";
[675]652
653        LogManager::getSingleton().logMessage(d.str());
654}
655//-----------------------------------------------------------------------
[925]656void OcclusionCullingSceneManager::renderBasicQueueGroupObjects(RenderQueueGroup* pGroup,
657                                                                                                                                QueuedRenderableCollection::OrganisationMode om)
[675]658{
[925]659    // Basic render loop
660    // Iterate through priorities
661    RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
662
663    while (groupIt.hasMoreElements())
664    {
665        RenderPriorityGroup* pPriorityGrp = groupIt.getNext();
666
667        // Sort the queue first
668        pPriorityGrp->sort(mCameraInProgress);
669
670        // Do solids
671        renderObjects(pPriorityGrp->getSolidsBasic(), om, true);
672
673                // for correct rendering, transparents must be rendered after hierarchical culling
674                // => do nothing
675
676        // Do transparents (always descending)
677                if (NORMAL_RENDER_HACK || !mSkipTransparents)
678                {
679                        renderObjects(pPriorityGrp->getTransparents(),
680                        QueuedRenderableCollection::OM_SORT_DESCENDING, true);
681                }
682
683
684    }// for each priority
[675]685}
[925]686
[675]687//-----------------------------------------------------------------------
[720]688bool OcclusionCullingSceneManager::validatePassForRendering(Pass* pass)
[675]689{
[868]690        if (NORMAL_RENDER_HACK)
691        {
692                return SceneManager::validatePassForRendering(pass);
693        }
694
[675]695        // skip all but first pass if we are doing the depth pass
[868]696        if ((mIsDepthPassPhase || mIsItemBufferPhase) && (pass->getIndex() > 0))
[675]697        {
698                return false;
699        }
[868]700        // all but first pass
701        /*else if ((!mIsDepthPassPhase || mIsItemBufferPhase) && (pass->getIndex() != 0))
702        {
703                return false;
704        }*/
[675]705
706        return SceneManager::validatePassForRendering(pass);
707}
708//-----------------------------------------------------------------------
[897]709void OcclusionCullingSceneManager::_renderQueueGroupObjects(RenderQueueGroup* pGroup,
710                                                                                                                        QueuedRenderableCollection::OrganisationMode om)
[675]711{
[868]712        if (NORMAL_RENDER_HACK || !mIsItemBufferPhase)
[675]713        {
[897]714                TerrainSceneManager::_renderQueueGroupObjects(pGroup, om);
[675]715                return;
716        }
[897]717#ifdef  ITEM_BUFFER
718        //-- item buffer
[868]719        //-- item buffer: render objects using false colors
[675]720
721    // Iterate through priorities
722    RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
723
724        while (groupIt.hasMoreElements())
725    {
726                RenderItemBuffer(groupIt.getNext());
727        }
[897]728#endif // ITEM_BUFFER
[675]729}
[897]730#ifdef ITEM_BUFFER
[675]731//-----------------------------------------------------------------------
[720]732void OcclusionCullingSceneManager::RenderItemBuffer(RenderPriorityGroup* pGroup)
[675]733{
734        // Do solids
[897]735        QueuedRenderableCollection solidObjs = pGroup->getSolidsBasic();//msz
[675]736
737        // ----- SOLIDS LOOP -----
738        RenderPriorityGroup::SolidRenderablePassMap::const_iterator ipass, ipassend;
739        ipassend = solidObjs.end();
740
741        for (ipass = solidObjs.begin(); ipass != ipassend; ++ipass)
742        {
743                // Fast bypass if this group is now empty
744                if (ipass->second->empty())
745                        continue;
746
[868]747                // Render only first pass of renderable as false color
[675]748                if (ipass->first->getIndex() > 0)
749                        continue;
750
751                RenderPriorityGroup::RenderableList* rendList = ipass->second;
752               
753                RenderPriorityGroup::RenderableList::const_iterator irend, irendend;
754                irendend = rendList->end();
755                       
756                for (irend = rendList->begin(); irend != irendend; ++irend)
757                {
[868]758                        if (0)
759                        {
760                        std::stringstream d; d << "itembuffer, pass name: " <<
761                                ipass->first->getParent()->getParent()->getName();
762                               
763                        LogManager::getSingleton().logMessage(d.str());
764                        }
[675]765                       
766                        RenderSingleObjectForItemBuffer(*irend, ipass->first);
767                }
768        }
769
[868]770        //-- TRANSPARENT LOOP: must be handled differently from solids
[675]771
772        // transparents are treated either as solids or completely discarded
773        if (mRenderTransparentsForItemBuffer)
774        {
[897]775                QueuedRenderableCollection transpObjs = pGroup->getTransparents(); //msz
[675]776                RenderPriorityGroup::TransparentRenderablePassList::const_iterator
777                        itrans, itransend;
778
779                itransend = transpObjs.end();
780                for (itrans = transpObjs.begin(); itrans != itransend; ++itrans)
781                {
782                        // like for solids, render only first pass
783                        if (itrans->pass->getIndex() == 0)
784                        {       
785                                RenderSingleObjectForItemBuffer(itrans->renderable, itrans->pass);
786                        }
787                }
788        }
789}
790//-----------------------------------------------------------------------
[720]791void OcclusionCullingSceneManager::RenderSingleObjectForItemBuffer(Renderable *rend, Pass *pass)
[675]792{
793        static LightList nullLightList;
794       
795        int col[4];
796       
797        // -- create color code out of object id
798        col[0] = (rend->getId() >> 16) & 255;
799        col[1] = (rend->getId() >> 8) & 255;
800        col[2] = rend->getId() & 255;
801//      col[3] = 255;
802
803        //mDestRenderSystem->setColour(col[0], col[1], col[2], col[3]);
804   
805        mItemBufferPass->setAmbient(ColourValue(col[0] / 255.0f,
806                                                                                    col[1] / 255.0f,
807                                                                                        col[2] / 255.0f, 1));
808
809        // set vertex program of current pass
810        if (mExecuteVertexProgramForAllPasses && pass->hasVertexProgram())
811        {
812                mItemBufferPass->setVertexProgram(pass->getVertexProgramName());
813
814                if (mItemBufferPass->hasVertexProgram())
815                {
816                        const GpuProgramPtr& prg = mItemBufferPass->getVertexProgram();
817                        // Load this program if not done already
818                        if (!prg->isLoaded())
819                                prg->load();
820                        // Copy params
821                        mItemBufferPass->setVertexProgramParameters(pass->getVertexProgramParameters());
822                }
823        }
824        else if (mItemBufferPass->hasVertexProgram())
825        {
826                mItemBufferPass->setVertexProgram("");
827        }
828
[897]829        const Pass *usedPass = _setPass(mItemBufferPass);
[675]830
831
[903]832        // render a single object, this will set up auto params if required
[675]833        renderSingleObject(rend, usedPass, false, &nullLightList);
834}
[897]835#endif // ITEM_BUFFER
[675]836//-----------------------------------------------------------------------
[720]837GtpVisibility::VisibilityManager *OcclusionCullingSceneManager::GetVisibilityManager()
[675]838{
839        return mVisibilityManager;
840}
841//-----------------------------------------------------------------------
[720]842void OcclusionCullingSceneManager::InitVisibilityCulling(Camera *cam)
[675]843{
844        // reset culling manager stats
845        mVisibilityManager->GetCullingManager()->InitFrame(mVisualizeCulledNodes);
846
847        // set depth pass flag before rendering
848        mIsDepthPassPhase = mUseDepthPass;
849
[868]850        mIsHierarchicalCulling = true; // during hierarchical culling
851
[675]852        // item buffer needs full ambient lighting to use item colors as unique id
853        if (mUseItemBuffer)
854        {
855                mIsItemBufferPhase = true;
856                setAmbientLight(ColourValue(1,1,1,1));
857        }
858
859
860        // set passes which are stored in render queue
861        // for rendering AFTER hierarchical culling, i.e., passes which need
862        // a special rendering order
[868]863       
[675]864        mLeavePassesInQueue = 0;
865
[897]866        // if we have the depth pass or use an item buffer, no passes are left in the queue
867        if (1 && !mUseDepthPass && !mUseItemBuffer)
[675]868        {
869                if (mShadowTechnique == SHADOWTYPE_STENCIL_ADDITIVE)
870                {
871                        // TODO: remove this pass because it should be processed during hierarchical culling
872                        //mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_NOSHADOW;
873
874                        mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_DECAL;
875                        mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_DIFFUSE_SPECULAR;
876                        mLeavePassesInQueue |= RenderPriorityGroup::TRANSPARENT_PASSES;
877
[903]878                        // just render ambient passes
[897]879                        /*** msz: no more IRS_AMBIENT, see OgreSceneManager.h ***/
880                        // mIlluminationStage = IRS_AMBIENT;
[925]881                        //getRenderQueue()->setSplitPassesByLightingType(true);
[675]882                }
883       
884                if (mShadowTechnique == SHADOWTYPE_STENCIL_MODULATIVE)
885                {
886                        mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_NOSHADOW;
887                        mLeavePassesInQueue |= RenderPriorityGroup::TRANSPARENT_PASSES;
888                }
889       
890                // transparents should be rendered after hierarchical culling to
891                // provide front-to-back ordering
892                if (mDelayRenderTransparents)
893                {
894                        mLeavePassesInQueue |= RenderPriorityGroup::TRANSPARENT_PASSES;
895                }
896        }
897
[868]898        // skip rendering transparents during the hierarchical culling
[675]899        // (because they will be rendered afterwards)
[868]900        mSkipTransparents =
901                (mIsDepthPassPhase || (mLeavePassesInQueue & RenderPriorityGroup::TRANSPARENT_PASSES));
[675]902
903        // -- initialise interface for rendering traversal of the hierarchy
904        mHierarchyInterface->SetHierarchyRoot(mOctree);
905       
906        // possible two cameras (one for culling, one for rendering)
907        mHierarchyInterface->InitTraversal(mCameraInProgress,
908                                                                           mCullCamera ? getCamera("CullCamera") : NULL,
909                                                                           mLeavePassesInQueue);
910               
911}
912//-----------------------------------------------------------------------
[720]913OctreeHierarchyInterface *OcclusionCullingSceneManager::GetHierarchyInterface()
[675]914{
915        return mHierarchyInterface;
916}
917//-----------------------------------------------------------------------
[720]918void OcclusionCullingSceneManager::endFrame()
[675]919{
920        TerrainRenderable::ResetRenderLevelIndex();
921}
922//-----------------------------------------------------------------------
[720]923Entity* OcclusionCullingSceneManager::createEntity(const String& entityName,
[675]924                                                                                                        const String& meshName)
925{
926        Entity *ent = SceneManager::createEntity(entityName, meshName);
927
928        for (int i = 0; i < (int)ent->getNumSubEntities(); ++i)
929        {
930                ent->getSubEntity(i)->setId(mCurrentEntityId);
931        }
932
933        // increase counter of entity id values
934        ++ mCurrentEntityId;
935
936        return ent;
937}
938//-----------------------------------------------------------------------
[897]939void OcclusionCullingSceneManager::renderAdditiveStencilShadowedQueueGroupObjects(
940        RenderQueueGroup* pGroup, QueuedRenderableCollection::OrganisationMode om)
[675]941{
942        // only render solid passes during hierarchical culling
943        if (mIsHierarchicalCulling)
944        {
945                RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
946            LightList lightList;
947
948                while (groupIt.hasMoreElements())
949                {
950                        RenderPriorityGroup* pPriorityGrp = groupIt.getNext();
951
952                        // Sort the queue first
953                        pPriorityGrp->sort(mCameraInProgress);
954
955                        // Clear light list
956                        lightList.clear();
957
958                        // Render all the ambient passes first, no light iteration, no lights
[897]959                        /*** msz: no more IRS_AMBIENT, see OgreSceneManager.h ***/
960                        // mIlluminationStage = IRS_AMBIENT;
[675]961
[897]962                        OctreeSceneManager::renderObjects(pPriorityGrp->getSolidsBasic(), om, false, &lightList);
[675]963                        // Also render any objects which have receive shadows disabled
[897]964                        OctreeSceneManager::renderObjects(pPriorityGrp->getSolidsNoShadowReceive(), om, true);
[903]965#if 0           
966                        std::stringstream d;
967                        d << " solid size: " << (int)pPriorityGrp->_getSolidPasses().size()
968                                << " solid no shadow size: " << (int)pPriorityGrp->_getSolidPassesNoShadow().size()
969                                << "difspec size: " << (int)pPriorityGrp->_getSolidPassesDiffuseSpecular().size()
970                                << " decal size: " << (int)pPriorityGrp->_getSolidPassesDecal().size();
971                        LogManager::getSingleton().logMessage(d.str());
972#endif
[675]973                }
974        }
[903]975        else // render the rest of the passes
[675]976        {
[897]977                OctreeSceneManager::renderAdditiveStencilShadowedQueueGroupObjects(pGroup, om);
[675]978        }
979}
980//-----------------------------------------------------------------------
[897]981void OcclusionCullingSceneManager::renderModulativeStencilShadowedQueueGroupObjects(
982        RenderQueueGroup* pGroup, QueuedRenderableCollection::OrganisationMode om)
[675]983{
984   if (mIsHierarchicalCulling)
985   {
986           // Iterate through priorities
987           RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
988
989           while (groupIt.hasMoreElements())
990           {
991                   RenderPriorityGroup* pPriorityGrp = groupIt.getNext();
992
993                   // Sort the queue first
994                   pPriorityGrp->sort(mCameraInProgress);
995
996                   // Do (shadowable) solids
[897]997                   OctreeSceneManager::renderObjects(pPriorityGrp->getSolidsBasic(), om, true);
[675]998           }
999   }
1000   else
1001   {
[931]1002           OctreeSceneManager::renderModulativeStencilShadowedQueueGroupObjects(pGroup, om);
[675]1003   }
1004}
[903]1005//-------------------------------------------------------------------------
1006void OcclusionCullingSceneManager::SetObjectsVisible(const bool visible)
1007{
1008        GtpVisibilityPreprocessor::ObjectContainer::iterator it, it_end = mObjects.end();
[828]1009
[903]1010        for (it = mObjects.begin(); it != it_end; ++ it)
1011        {
1012                OgreMeshInstance *omi = static_cast<OgreMeshInstance *>(*it);
1013                Entity *ent = omi->GetMesh();
[944]1014               
[903]1015                ent->setVisible(visible);
1016        }
1017}
1018//-----------------------------------------------------------------------
[938]1019bool OcclusionCullingSceneManager::LoadViewCells(const String &filename)
[863]1020{
[938]1021        // objects are set to invisible initially
[903]1022        SetObjectsVisible(false);
[938]1023       
[931]1024        const string bboxesFilename = mVisibilityManager->GetVisibilityEnvironment()->getViewCellsFileName();
[903]1025
[938]1026        // converter between view cell ids and Ogre entites
[931]1027        GtpVisibilityPreprocessor::IndexedBoundingBoxContainer iboxes;
1028        OgreBoundingBoxConverter bconverter(this);
1029
[863]1030        // load the view cells assigning the found objects to the pvss
[903]1031        mViewCellsManager =
[1004]1032                GtpVisibilityPreprocessor::ViewCellsManager::LoadViewCells(filename, &mObjects, false, &bconverter);
[903]1033
[938]1034        return (mViewCellsManager != NULL);
[863]1035}
[903]1036//-------------------------------------------------------------------------
1037void OcclusionCullingSceneManager::applyViewCellPvs(GtpVisibilityPreprocessor::ViewCell *vc,
1038                                                                                                        const bool load)
1039{       // NOTE: should not happen, rather apply view cell representing unbounded space then
1040        if (!vc)
1041        {       
1042                // set everything visible for savety
1043                SetObjectsVisible(true);
[944]1044
[903]1045                return;
1046        }
[944]1047               
[903]1048        GtpVisibilityPreprocessor::ObjectPvsMap::const_iterator oit,
1049                        oit_end = vc->GetPvs().mEntries.end();
1050
1051        //-- PVS of view cell
1052        for (oit = vc->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
1053        {
1054                if (!(*oit).first) continue;
1055
[1146]1056                OgreMeshInstance *omi = dynamic_cast<OgreMeshInstance *>((*oit).first);
[903]1057                omi->GetMesh()->setVisible(load);
[944]1058                //GtpVisibilityPreprocessor::Debug << "assigned id " << omi->GetId() << endl;
[863]1059        }
1060}
[903]1061//-------------------------------------------------------------------------
1062void OcclusionCullingSceneManager::updatePvs(Camera *cam)
1063{
[938]1064        if (!(mViewCellsLoaded && mUseViewCells))
1065                return;
[863]1066
[938]1067        const GtpVisibilityPreprocessor::Vector3 viewPoint =
1068                OgreTypeConverter::ConvertFromOgre(cam->getDerivedPosition());
[903]1069
[938]1070        GtpVisibilityPreprocessor::ViewCell *newElementary =
1071                mViewCellsManager->GetViewCell(viewPoint);
[903]1072
[938]1073        // elementary view cell did not change => apply same pvs
1074        if (mElementaryViewCell == newElementary)
1075                return;
[903]1076
[938]1077        mElementaryViewCell = newElementary;
[944]1078        //LogManager::getSingleton().logMessage("unloading");
[938]1079        //-- unload old pvs
1080        applyViewCellPvs(mCurrentViewCell, false);
[903]1081
1082
[938]1083        //-- the new view cell
[903]1084               
[938]1085        GtpVisibilityPreprocessor::ViewCell *viewCell;
[903]1086
1087               
[938]1088        if (mUseVisibilityFilter)
1089        {
1090                //-- compute new filtered cell
1091                GtpVisibilityPreprocessor::PrVs prvs;
1092                mViewCellsManager->GetPrVS(viewPoint, prvs, 5);
1093                viewCell = prvs.mViewCell;
1094        }
1095        else
1096        {
1097                viewCell = newElementary;
1098        }
[944]1099        //LogManager::getSingleton().logMessage("loading");
[938]1100        //-- load new pvs
[944]1101        applyViewCellPvs(viewCell, true);
[903]1102
[938]1103        // store pvs
1104        mCurrentViewCell->SetPvs(viewCell->GetPvs());
[903]1105
[938]1106        // delete merge tree of filtered view cell
1107        if (mUseVisibilityFilter)
1108                mViewCellsManager->DeleteLocalMergeTree(viewCell);
[903]1109}
[955]1110#if 0
[951]1111//-------------------------------------------------------------------------
1112void OcclusionCullingSceneManager::TestVisible(SceneNode *node)
1113{
1114        // first test for scene node, then for octant (part of the hierarchy)
1115        if (!node->mVisibleChildren)
1116                node->setVisible(false);
[903]1117
[951]1118        node->getOctant()->mVisibleChildren --;
1119}
1120//-------------------------------------------------------------------------
1121void OcclusionCullingSceneManager::TestVisible(Octree *octant)
1122{
1123        // first test for scene node, then for octant (part of the hierarchy)
1124        if (!octant->mVisibleChildren)
1125                octant->setVisible(false);
1126}
[903]1127
[951]1128//-------------------------------------------------------------------------
1129void OcclusionCullingSceneManager::UpdateVisibility(Entity *ent)
1130{
1131        if (!ent->isVisible())
1132        {
1133                bool visible = TestVisible(ent->getParentNode());
1134               
1135                if (!visible)
1136                        visible = TestVisible(octant->getParentNode());
[903]1137
[951]1138                if (!visible)
1139                        mHierarchyInterface->pullupVisibility();
1140        }
1141}
[955]1142#endif
[903]1143//-----------------------------------------------------------------------
1144const String OcclusionCullingSceneManagerFactory::FACTORY_TYPE_NAME = "OcclusionCullingSceneManager";
1145//-----------------------------------------------------------------------
1146void OcclusionCullingSceneManagerFactory::initMetaData(void) const
1147{
1148        mMetaData.typeName = FACTORY_TYPE_NAME;
1149        mMetaData.description = "Scene manager organising the scene on the basis of an octree with advanced occlusion culling (TM).";
1150        mMetaData.sceneTypeMask = 0xFFFF; // support all types
1151        mMetaData.worldGeometrySupported = false;
1152}
1153//-----------------------------------------------------------------------
[925]1154SceneManager *OcclusionCullingSceneManagerFactory::createInstance(
1155                const String& instanceName)
[903]1156{
[925]1157        OcclusionCullingSceneManager* tsm = new OcclusionCullingSceneManager(instanceName, visManager);
1158       
1159        // Create & register default sources (one per manager)
1160        HeightmapTerrainPageSource* ps = new HeightmapTerrainPageSource();
1161        mTerrainPageSources.push_back(ps);
1162        tsm->registerPageSource("Heightmap", ps);
1163
1164        return tsm;
[903]1165}
1166//-----------------------------------------------------------------------
1167void OcclusionCullingSceneManagerFactory::destroyInstance(SceneManager* instance)
1168{
1169        delete instance;
1170}
1171
[675]1172} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.