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

Revision 675, 26.5 KB checked in by mattausch, 18 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#include <OgreEntity.h>
10#include <OgreSubEntity.h>
11
12
13namespace Ogre {
14
15//-----------------------------------------------------------------------
16VisibilityTerrainSceneManager::VisibilityTerrainSceneManager(
17                                                        GtpVisibility::VisibilityManager *visManager):
18mVisibilityManager(visManager),
19mShowVisualization(false),
20mRenderNodesForViz(false),
21mRenderNodesContentForViz(false),
22mVisualizeCulledNodes(false),
23mLeavePassesInQueue(0),
24mDelayRenderTransparents(true),
25mUseDepthPass(false),
26mIsDepthPassPhase(false),
27mUseItemBuffer(false),
28//mUseItemBuffer(true),
29mIsItemBufferPhase(false),
30mCurrentEntityId(1),
31mEnableDepthWrite(true),
32mSkipTransparents(false),
33mRenderTransparentsForItemBuffer(true),
34mExecuteVertexProgramForAllPasses(true),
35mIsHierarchicalCulling(false)
36{
37        mHierarchyInterface = new OctreeHierarchyInterface(this, mDestRenderSystem);
38       
39        //mDisplayNodes = true;
40        //mShowBoundingBoxes = true;
41        //mShowBoxes = true;
42
43        // TODO: set maxdepth to reasonable value
44        mMaxDepth = 50;
45}
46//-----------------------------------------------------------------------
47void VisibilityTerrainSceneManager::InitDepthPass()
48{
49        MaterialPtr depthMat = MaterialManager::getSingleton().getByName("Visibility/DepthPass");
50
51        if (depthMat.isNull())
52    {
53                depthMat = MaterialManager::getSingleton().create(
54                "Visibility/DepthPass",
55                ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
56
57        mDepthPass = depthMat->getTechnique(0)->getPass(0);
58                mDepthPass->setColourWriteEnabled(false);
59                mDepthPass->setDepthWriteEnabled(true);
60                mDepthPass->setLightingEnabled(false);
61        }
62        else
63        {
64                mDepthPass = depthMat->getTechnique(0)->getPass(0);
65        }
66}
67//-----------------------------------------------------------------------
68VisibilityTerrainSceneManager::~VisibilityTerrainSceneManager()
69{
70        OGRE_DELETE(mHierarchyInterface);
71}
72//-----------------------------------------------------------------------
73void VisibilityTerrainSceneManager::InitItemBufferPass()
74{
75        MaterialPtr itemBufferMat = MaterialManager::getSingleton().
76                getByName("Visibility/ItemBufferPass");
77
78        if (itemBufferMat.isNull())
79    {
80                // Init
81                itemBufferMat = MaterialManager::getSingleton().create("Visibility/ItemBufferPass",
82                ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
83
84                mItemBufferPass = itemBufferMat->getTechnique(0)->getPass(0);
85                mItemBufferPass->setColourWriteEnabled(true);
86                mItemBufferPass->setDepthWriteEnabled(true);
87                mItemBufferPass->setLightingEnabled(true);
88                //mItemBufferPass->setLightingEnabled(false);
89        }
90        else
91        {
92                mItemBufferPass = itemBufferMat->getTechnique(0)->getPass(0);
93        }
94        //mItemBufferPass->setAmbient(1, 1, 0);
95}
96//-------------------------------------------------------------------------
97void VisibilityTerrainSceneManager::setWorldGeometry( const String& filename )
98{
99    // Clear out any existing world resources (if not default)
100    if (ResourceGroupManager::getSingleton().getWorldResourceGroupName() !=
101        ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)
102    {
103        ResourceGroupManager::getSingleton().clearResourceGroup(
104            ResourceGroupManager::getSingleton().getWorldResourceGroupName());
105    }
106    mTerrainPages.clear();
107    // Load the configuration
108    loadConfig(filename);
109
110    // Resize the octree, allow for 1 page for now
111    float max_x = mOptions.scale.x * mOptions.pageSize;
112    float max_y = mOptions.scale.y;
113    float max_z = mOptions.scale.z * mOptions.pageSize;
114
115        float maxAxis = std::max(max_x, max_y);
116        maxAxis = std::max(maxAxis, max_z);
117        resize( AxisAlignedBox( 0, 0, 0, maxAxis, maxAxis, maxAxis ) );
118   
119    setupTerrainMaterial();
120
121    setupTerrainPages();
122
123 }
124
125//-----------------------------------------------------------------------
126void VisibilityTerrainSceneManager::PrepareVisualization(Camera *cam)
127{
128        // add player camera for visualization purpose
129        try
130        {
131                Camera *c;
132                if ((c = getCamera("PlayerCam")) != NULL)
133                {
134                        getRenderQueue()->addRenderable(c);
135                }   
136    }
137    catch (...)
138    {
139        // ignore
140    }
141        for (BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it)
142        {
143                getRenderQueue()->addRenderable(*it);
144        }
145        if (mRenderNodesForViz || mRenderNodesContentForViz)
146        {
147                // HACK: change node material so it is better suited for visualization
148                MaterialPtr nodeMat = MaterialManager::getSingleton().getByName("Core/NodeMaterial");
149                nodeMat->setAmbient(1, 1, 0);
150                nodeMat->setLightingEnabled(true);
151                nodeMat->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();
152
153                for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
154                {
155                        if (mRenderNodesForViz)
156                        {
157                                // render the leaf nodes
158                                if (((*it)->numAttachedObjects() > 0) && ((*it)->numChildren() == 0) &&
159                                         (*it)->getAttachedObject(0)->getMovableType() == "Entity")
160                                {
161                                        getRenderQueue()->addRenderable((*it));
162                                }
163
164                                // addbounding boxes instead of node itself
165                                //(*it)->_addBoundingBoxToQueue(getRenderQueue());
166                        }
167                        if (mRenderNodesContentForViz)
168                        {
169                                (*it)->_addToRenderQueue(cam, getRenderQueue(), false);
170                        }
171                }
172        }       
173}
174//-----------------------------------------------------------------------
175Pass *VisibilityTerrainSceneManager::setPass(Pass* pass)
176{
177        // TODO: setting vertex program is not efficient
178        //Pass *usedPass = ((mIsDepthPassPhase && !pass->hasVertexProgram()) ? mDepthPass : pass);
179       
180        // set depth fill pass if we currently do not make an aabb occlusion query
181        Pass *usedPass = (mIsDepthPassPhase && !mHierarchyInterface->IsBoundingBoxQuery() ?
182                                          mDepthPass : pass);
183               
184        IlluminationRenderStage savedStage = mIlluminationStage;
185       
186        // set illumination stage to NONE so no shadow material is used
187        // for depth pass or for occlusion query
188        if (mIsDepthPassPhase || mHierarchyInterface->IsBoundingBoxQuery())
189        {
190                mIlluminationStage = IRS_NONE;
191        }
192       
193        // --- set vertex program of current pass in order to set correct depth
194        if (mExecuteVertexProgramForAllPasses && mIsDepthPassPhase && pass->hasVertexProgram())
195        {
196                // add vertex program of current pass to depth pass
197                mDepthPass->setVertexProgram(pass->getVertexProgramName());
198
199                if (mDepthPass->hasVertexProgram())
200                {
201                        const GpuProgramPtr& prg = mDepthPass->getVertexProgram();
202                        // Load this program if not done already
203                        if (!prg->isLoaded())
204                                prg->load();
205                        // Copy params
206                        mDepthPass->setVertexProgramParameters(pass->getVertexProgramParameters());
207                }
208        }
209        else if (mDepthPass->hasVertexProgram())
210        {
211                mDepthPass->setVertexProgram("");
212        }
213       
214
215        bool IsDepthWrite = usedPass->getDepthWriteEnabled();
216
217        // global option which enables / disables depth writes
218        if (!mEnableDepthWrite)
219        {
220                usedPass->setDepthWriteEnabled(false);
221        }
222        //else if (mIsItemBufferPass) {usedPass = mItemBufferPass;}
223       
224        Pass *result = SceneManager::setPass(usedPass);
225
226        // reset depth write
227        if (!mEnableDepthWrite)
228        {
229                usedPass->setDepthWriteEnabled(IsDepthWrite);
230        }
231
232        // reset illumination stage
233        mIlluminationStage = savedStage;
234
235        return result;
236}
237//-----------------------------------------------------------------------
238void VisibilityTerrainSceneManager::_findVisibleObjects(Camera* cam,
239                                                                                                                bool onlyShadowCasters)
240{
241        //-- show visible scene nodes and octree bounding boxes from last frame
242        if (mShowVisualization)
243    {
244                PrepareVisualization(cam);
245        }
246        else
247        {       
248                // for hierarchical culling, we interleave identification
249                // and rendering of objects in _renderVisibibleObjects
250
251                // for the shadow pass we use only standard rendering
252                // because of low occlusion
253                if (mShadowTechnique == SHADOWTYPE_TEXTURE_MODULATIVE &&
254                        mIlluminationStage == IRS_RENDER_TO_TEXTURE)
255                {
256                        TerrainSceneManager::_findVisibleObjects(cam, onlyShadowCasters);
257                }
258                // only shadow casters will be rendered in shadow texture pass
259                // mHierarchyInterface->SetOnlyShadowCasters(onlyShadowCasters);
260        }
261       
262       
263        // -- delete lists stored for visualization
264        mVisible.clear();
265        mBoxes.clear();
266}
267//-----------------------------------------------------------------------
268void VisibilityTerrainSceneManager::_renderVisibleObjects()
269{
270
271        InitDepthPass();          // create material for depth pass
272        InitItemBufferPass(); // create material for item buffer pass
273
274        // save ambient light to reset later
275        ColourValue savedAmbient = mAmbientLight;
276
277        //-- apply standard rendering for some modes (e.g., visualization, shadow pass)
278
279        if (mShowVisualization ||
280           (mShadowTechnique == SHADOWTYPE_TEXTURE_MODULATIVE &&
281            mIlluminationStage == IRS_RENDER_TO_TEXTURE))
282        {       
283                IlluminationRenderStage savedStage = mIlluminationStage;
284       
285                if (mShowVisualization)
286                        // disable illumination stage to prevent rendering shadows
287                        mIlluminationStage = IRS_NONE;
288
289                // standard rendering for shadow maps because of performance
290                TerrainSceneManager::_renderVisibleObjects();
291
292                mIlluminationStage = savedStage;
293        }
294        else //-- the hierarchical culling algorithm
295        {
296                // don't render backgrounds for item buffer
297                if (mUseItemBuffer)
298                {
299                        clearSpecialCaseRenderQueues();
300                        getRenderQueue()->clear();
301                }
302
303                //-- hierarchical culling
304                // the objects of different layers (e.g., background, scene,
305                // overlay) must be identified and rendered one after another
306
307                //-- render all early skies
308                clearSpecialCaseRenderQueues();
309                addSpecialCaseRenderQueue(RENDER_QUEUE_BACKGROUND);
310                addSpecialCaseRenderQueue(RENDER_QUEUE_SKIES_EARLY);
311                setSpecialCaseRenderQueueMode(SceneManager::SCRQM_INCLUDE);
312
313                TerrainSceneManager::_renderVisibleObjects();
314
315#ifdef GTP_VISIBILITY_MODIFIED_OGRE
316                // delete previously rendered content
317                _deleteRenderedQueueGroups();
318#endif
319
320                //-- prepare queue for visible objects (i.e., all but overlay and skies late)
321                clearSpecialCaseRenderQueues();
322                addSpecialCaseRenderQueue(RENDER_QUEUE_SKIES_LATE);
323                addSpecialCaseRenderQueue(RENDER_QUEUE_OVERLAY);
324       
325                // exclude this queues from hierarchical rendering
326                setSpecialCaseRenderQueueMode(SceneManager::SCRQM_EXCLUDE);
327
328                // set all necessary parameters for
329                // hierarchical visibility culling and rendering
330                InitVisibilityCulling(mCameraInProgress);
331
332                /**
333                * the hierarchical culling algorithm
334                * for depth pass: we just find objects and update depth buffer
335                * for "delayed" rendering: we render some passes afterwards
336                * e.g., transparents, because they need front-to-back sorting
337                **/
338               
339                mVisibilityManager->ApplyVisibilityCulling();
340
341                // delete remaining renderables from queue (all not in mLeavePassesInQueue)
342#ifdef GTP_VISIBILITY_MODIFIED_OGRE
343                _deleteRenderedQueueGroups(mLeavePassesInQueue);
344#endif
345
346                //-- reset parameters
347                mIsDepthPassPhase = false;
348                mIsItemBufferPhase = false;
349                mSkipTransparents = false;
350                mLeavePassesInQueue = 0;
351               
352
353                // add visible nodes found by the visibility culling algorithm
354                if (mUseDepthPass)
355                {
356                        for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++ it)
357                        {
358                                (*it)->_addToRenderQueue(mCameraInProgress, getRenderQueue(), false);
359                        }
360                }
361               
362                //-- now we can render all remaining queue objects
363                // used for depth pass, transparents, overlay
364                clearSpecialCaseRenderQueues();
365
366                TerrainSceneManager::_renderVisibleObjects();
367        }
368               
369        // HACK: set the new render level index, important to avoid cracks
370        // in terrain caused by LOD
371        TerrainRenderable::NextRenderLevelIndex();
372       
373        // reset ambient light
374        setAmbientLight(savedAmbient);
375
376        getRenderQueue()->clear(); // finally clear render queue
377        OGRE_DELETE(mRenderQueue); // HACK: should be cleared before...
378        //WriteLog(); // write out stats
379
380}
381
382//-----------------------------------------------------------------------
383void VisibilityTerrainSceneManager::_updateSceneGraph(Camera* cam)
384{
385        mVisibilityManager->GetCullingManager()->SetHierarchyInterface(mHierarchyInterface);
386        mHierarchyInterface->SetRenderSystem(mDestRenderSystem);
387
388        TerrainSceneManager::_updateSceneGraph(cam);
389}
390//-----------------------------------------------------------------------
391bool VisibilityTerrainSceneManager::setOption(const String & key, const void * val)
392{
393        if (key == "UseDepthPass")
394        {
395                mUseDepthPass = (*static_cast<const bool *>(val));
396                return true;
397        }
398        if (key == "PrepareVisualization")
399        {
400                mShowVisualization = (*static_cast<const bool *>(val));
401                return true;
402        }
403        if (key == "RenderNodesForViz")
404        {
405                mRenderNodesForViz = (*static_cast<const bool *>(val));
406                return true;
407        }
408        if (key == "RenderNodesContentForViz")
409        {
410                mRenderNodesContentForViz = (*static_cast<const bool *>(val));
411                return true;
412        }
413        if (key == "SkyBoxEnabled")
414        {
415                mSkyBoxEnabled = (*static_cast<const bool *>(val));
416                return true;
417        }
418        if (key == "SkyPlaneEnabled")
419        {
420                mSkyPlaneEnabled = (*static_cast<const bool *>(val));
421                return true;
422        }
423        if (key == "SkyDomeEnabled")
424        {
425                mSkyDomeEnabled = (*static_cast<const bool *>(val));
426                return true;
427        }
428        if (key == "VisualizeCulledNodes")
429        {
430                mVisualizeCulledNodes = (*static_cast<const bool *>(val));
431                return true;
432        }
433        if (key == "DelayRenderTransparents")
434        {
435                mDelayRenderTransparents = (*static_cast<const bool *>(val));
436                return true;
437        }
438
439        if (key == "DepthWrite")
440        {
441                mEnableDepthWrite = (*static_cast<const bool *>(val));
442                return true;
443        }
444        if (key == "UseItemBuffer")
445        {
446                mUseItemBuffer = (*static_cast<const bool *>(val));
447                return true;
448        }
449        if (key == "ExecuteVertexProgramForAllPasses")
450        {
451                mExecuteVertexProgramForAllPasses  = (*static_cast<const bool *>(val));
452                return true;
453        }
454        if (key == "RenderTransparentsForItemBuffer")
455        {
456                mRenderTransparentsForItemBuffer  = (*static_cast<const bool *>(val));
457                return true;
458        }
459        if (key == "NodeVizScale")
460        {
461                OctreeNode::setVizScale(*static_cast<const float *>(val));
462                return true;
463        }
464
465        if (key == "UseArbQueries")
466        {
467                bool useArbQueries = (*static_cast<const bool *>(val));
468
469                if (useArbQueries)
470                {
471                        mHierarchyInterface->DeleteQueries();
472                        mDestRenderSystem->setConfigOption("ArbQueries", "Yes");
473                }
474                else
475                {
476                        mHierarchyInterface->DeleteQueries();
477                        mDestRenderSystem->setConfigOption("ArbQueries", "No");
478                }
479        }
480        return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
481                setOption(key, val) || TerrainSceneManager::setOption(key, val);
482}
483//-----------------------------------------------------------------------
484bool VisibilityTerrainSceneManager::getOption(const String & key, void *val)
485{
486        if (key == "NumHierarchyNodes")
487        {
488                * static_cast<unsigned int *>(val) = (unsigned int)mNumOctants;
489                return true;
490        }
491       
492        return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
493                getOption(key, val) && TerrainSceneManager::getOption(key, val);
494}
495//-----------------------------------------------------------------------
496bool VisibilityTerrainSceneManager::getOptionValues(const String & key,
497                                                                                                        StringVector &refValueList)
498{
499        return TerrainSceneManager::getOptionValues( key, refValueList);
500}
501//-----------------------------------------------------------------------
502bool VisibilityTerrainSceneManager::getOptionKeys(StringVector & refKeys)
503{
504        return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
505                getOptionKeys(refKeys) || TerrainSceneManager::getOptionKeys(refKeys);
506}
507//-----------------------------------------------------------------------
508void VisibilityTerrainSceneManager::setVisibilityManager(GtpVisibility::
509                                                                                                                 VisibilityManager *visManager)
510{
511        mVisibilityManager = visManager;
512}
513//-----------------------------------------------------------------------
514GtpVisibility::VisibilityManager *VisibilityTerrainSceneManager::getVisibilityManager( void )
515{
516        return mVisibilityManager;
517}
518//-----------------------------------------------------------------------
519void VisibilityTerrainSceneManager::WriteLog()
520{
521        std::stringstream d;
522
523        d << "Depth pass: " << StringConverter::toString(mUseDepthPass) << ", "
524          << "Delay transparents: " << StringConverter::toString(mDelayRenderTransparents) << ", "
525          << "Use optimization: " << StringConverter::toString(mHierarchyInterface->GetTestGeometryForVisibleLeaves()) << ", "
526          << "Algorithm type: " << mVisibilityManager->GetCullingManagerType() << ", "
527          << "Hierarchy nodes: " << mNumOctants << ", "
528          << "Traversed nodes: " << mHierarchyInterface->GetNumTraversedNodes() << ", "
529          << "Rendered nodes: " << mHierarchyInterface->GetNumRenderedNodes() << ", "
530          << "Query culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumQueryCulledNodes() << ", "
531          << "Frustum culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumFrustumCulledNodes() << ", "
532      << "Queries issued: " << mVisibilityManager->GetCullingManager()->GetNumQueriesIssued() << "\n";
533
534        LogManager::getSingleton().logMessage(d.str());
535}
536//-----------------------------------------------------------------------
537void VisibilityTerrainSceneManager::renderObjects(
538        const RenderPriorityGroup::TransparentRenderablePassList& objs,
539    bool doLightIteration, const LightList* manualLightList)
540{
541        // for correct rendering, transparents must be rendered after hierarchical culling
542        if (!mSkipTransparents)
543        {
544                OctreeSceneManager::renderObjects(objs, doLightIteration, manualLightList);
545        }
546}
547//-----------------------------------------------------------------------
548bool VisibilityTerrainSceneManager::validatePassForRendering(Pass* pass)
549{
550        // skip all but first pass if we are doing the depth pass
551        if ((mIsDepthPassPhase || mIsItemBufferPhase) && pass->getIndex() > 0)
552        {
553                return false;
554        }
555
556        return SceneManager::validatePassForRendering(pass);
557}
558//-----------------------------------------------------------------------
559void VisibilityTerrainSceneManager::renderQueueGroupObjects(RenderQueueGroup* pGroup)
560{
561        if (!mIsItemBufferPhase)
562        {
563                TerrainSceneManager::renderQueueGroupObjects(pGroup);
564                return;
565        }
566
567        //-- item buffer
568
569    // Iterate through priorities
570    RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
571
572        while (groupIt.hasMoreElements())
573    {
574                RenderItemBuffer(groupIt.getNext());
575        }
576}
577//-----------------------------------------------------------------------
578void VisibilityTerrainSceneManager::RenderItemBuffer(RenderPriorityGroup* pGroup)
579{
580        // Do solids
581        RenderPriorityGroup::SolidRenderablePassMap solidObjs = pGroup->_getSolidPasses();
582
583        // ----- SOLIDS LOOP -----
584        RenderPriorityGroup::SolidRenderablePassMap::const_iterator ipass, ipassend;
585        ipassend = solidObjs.end();
586
587        for (ipass = solidObjs.begin(); ipass != ipassend; ++ipass)
588        {
589                // Fast bypass if this group is now empty
590                if (ipass->second->empty())
591                        continue;
592
593                // Render only first pass
594                if (ipass->first->getIndex() > 0)
595                        continue;
596
597                RenderPriorityGroup::RenderableList* rendList = ipass->second;
598               
599                RenderPriorityGroup::RenderableList::const_iterator irend, irendend;
600                irendend = rendList->end();
601                       
602                for (irend = rendList->begin(); irend != irendend; ++irend)
603                {
604                        std::stringstream d; d << "itembuffer, pass name: " <<
605                                ipass->first->getParent()->getParent()->getName();
606                               
607                        LogManager::getSingleton().logMessage(d.str());
608                       
609                        RenderSingleObjectForItemBuffer(*irend, ipass->first);
610                }
611        }
612
613        // -- TRANSPARENT LOOP: must be handled differently
614
615        // transparents are treated either as solids or completely discarded
616        if (mRenderTransparentsForItemBuffer)
617        {
618                RenderPriorityGroup::TransparentRenderablePassList transpObjs =
619                        pGroup->_getTransparentPasses();
620                RenderPriorityGroup::TransparentRenderablePassList::const_iterator
621                        itrans, itransend;
622
623                itransend = transpObjs.end();
624                for (itrans = transpObjs.begin(); itrans != itransend; ++itrans)
625                {
626                        // like for solids, render only first pass
627                        if (itrans->pass->getIndex() == 0)
628                        {       
629                                RenderSingleObjectForItemBuffer(itrans->renderable, itrans->pass);
630                        }
631                }
632        }
633}
634//-----------------------------------------------------------------------
635void VisibilityTerrainSceneManager::RenderSingleObjectForItemBuffer(Renderable *rend, Pass *pass)
636{
637        static LightList nullLightList;
638       
639        int col[4];
640       
641        // -- create color code out of object id
642        col[0] = (rend->getId() >> 16) & 255;
643        col[1] = (rend->getId() >> 8) & 255;
644        col[2] = rend->getId() & 255;
645//      col[3] = 255;
646
647        //mDestRenderSystem->setColour(col[0], col[1], col[2], col[3]);
648   
649        mItemBufferPass->setAmbient(ColourValue(col[0] / 255.0f,
650                                                                                    col[1] / 255.0f,
651                                                                                        col[2] / 255.0f, 1));
652
653        // set vertex program of current pass
654        if (mExecuteVertexProgramForAllPasses && pass->hasVertexProgram())
655        {
656                mItemBufferPass->setVertexProgram(pass->getVertexProgramName());
657
658                if (mItemBufferPass->hasVertexProgram())
659                {
660                        const GpuProgramPtr& prg = mItemBufferPass->getVertexProgram();
661                        // Load this program if not done already
662                        if (!prg->isLoaded())
663                                prg->load();
664                        // Copy params
665                        mItemBufferPass->setVertexProgramParameters(pass->getVertexProgramParameters());
666                }
667        }
668        else if (mItemBufferPass->hasVertexProgram())
669        {
670                mItemBufferPass->setVertexProgram("");
671        }
672
673        Pass *usedPass = setPass(mItemBufferPass);
674
675
676        // Render a single object, this will set up auto params if required
677        renderSingleObject(rend, usedPass, false, &nullLightList);
678}
679//-----------------------------------------------------------------------
680GtpVisibility::VisibilityManager *VisibilityTerrainSceneManager::GetVisibilityManager()
681{
682        return mVisibilityManager;
683}
684//-----------------------------------------------------------------------
685void VisibilityTerrainSceneManager::InitVisibilityCulling(Camera *cam)
686{
687        // reset culling manager stats
688        mVisibilityManager->GetCullingManager()->InitFrame(mVisualizeCulledNodes);
689
690        // set depth pass flag before rendering
691        mIsDepthPassPhase = mUseDepthPass;
692
693        // item buffer needs full ambient lighting to use item colors as unique id
694        if (mUseItemBuffer)
695        {
696                mIsItemBufferPhase = true;
697                setAmbientLight(ColourValue(1,1,1,1));
698        }
699
700
701        // set passes which are stored in render queue
702        // for rendering AFTER hierarchical culling, i.e., passes which need
703        // a special rendering order
704        mLeavePassesInQueue = 0;
705
706        if (!mUseDepthPass && !mUseItemBuffer)
707        {
708                if (mShadowTechnique == SHADOWTYPE_STENCIL_ADDITIVE)
709                {
710                        // TODO: remove this pass because it should be processed during hierarchical culling
711                        //mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_NOSHADOW;
712
713                        mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_DECAL;
714                        mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_DIFFUSE_SPECULAR;
715                        mLeavePassesInQueue |= RenderPriorityGroup::TRANSPARENT_PASSES;
716
717                        // just render ambient stuff
718                        mIlluminationStage = IRS_AMBIENT;
719                }
720       
721                if (mShadowTechnique == SHADOWTYPE_STENCIL_MODULATIVE)
722                {
723                        mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_NOSHADOW;
724                        mLeavePassesInQueue |= RenderPriorityGroup::TRANSPARENT_PASSES;
725                }
726       
727                // transparents should be rendered after hierarchical culling to
728                // provide front-to-back ordering
729                if (mDelayRenderTransparents)
730                {
731                        mLeavePassesInQueue |= RenderPriorityGroup::TRANSPARENT_PASSES;
732                }
733        }
734
735        // skip rendering transparents in the hierarchical culling
736        // (because they will be rendered afterwards)
737        mSkipTransparents = mUseDepthPass ||
738                (mLeavePassesInQueue & RenderPriorityGroup::TRANSPARENT_PASSES);
739
740        // -- initialise interface for rendering traversal of the hierarchy
741        mHierarchyInterface->SetHierarchyRoot(mOctree);
742       
743        // possible two cameras (one for culling, one for rendering)
744        mHierarchyInterface->InitTraversal(mCameraInProgress,
745                                                                           mCullCamera ? getCamera("CullCamera") : NULL,
746                                                                           mLeavePassesInQueue);
747               
748        //std::stringstream d; d << "leave passes in queue: " << mLeavePassesInQueue;LogManager::getSingleton().logMessage(d.str());
749}
750//-----------------------------------------------------------------------
751OctreeHierarchyInterface *VisibilityTerrainSceneManager::GetHierarchyInterface()
752{
753        return mHierarchyInterface;
754}
755//-----------------------------------------------------------------------
756void VisibilityTerrainSceneManager::endFrame()
757{
758        TerrainRenderable::ResetRenderLevelIndex();
759}
760//-----------------------------------------------------------------------
761Entity* VisibilityTerrainSceneManager::createEntity(const String& entityName,
762                                                                                                        const String& meshName)
763{
764        Entity *ent = SceneManager::createEntity(entityName, meshName);
765
766        for (int i = 0; i < (int)ent->getNumSubEntities(); ++i)
767        {
768                ent->getSubEntity(i)->setId(mCurrentEntityId);
769        }
770
771        // increase counter of entity id values
772        ++ mCurrentEntityId;
773
774        return ent;
775}
776//-----------------------------------------------------------------------
777void VisibilityTerrainSceneManager::renderAdditiveStencilShadowedQueueGroupObjects(RenderQueueGroup* pGroup)
778{
779        // only render solid passes during hierarchical culling
780        if (mIsHierarchicalCulling)
781        {
782                RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
783            LightList lightList;
784
785                while (groupIt.hasMoreElements())
786                {
787                        RenderPriorityGroup* pPriorityGrp = groupIt.getNext();
788
789                        // Sort the queue first
790                        pPriorityGrp->sort(mCameraInProgress);
791
792                        // Clear light list
793                        lightList.clear();
794
795                        // Render all the ambient passes first, no light iteration, no lights
796                        mIlluminationStage = IRS_AMBIENT;
797
798                        OctreeSceneManager::renderObjects(pPriorityGrp->_getSolidPasses(), false, &lightList);
799                        // Also render any objects which have receive shadows disabled
800                        OctreeSceneManager::renderObjects(pPriorityGrp->_getSolidPassesNoShadow(), true);
801                }
802        }
803        else
804        {
805                OctreeSceneManager::renderAdditiveStencilShadowedQueueGroupObjects(pGroup);
806        }
807}
808//-----------------------------------------------------------------------
809void VisibilityTerrainSceneManager::renderModulativeStencilShadowedQueueGroupObjects(RenderQueueGroup* pGroup)
810{
811   if (mIsHierarchicalCulling)
812   {
813           // Iterate through priorities
814           RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
815
816           while (groupIt.hasMoreElements())
817           {
818                   RenderPriorityGroup* pPriorityGrp = groupIt.getNext();
819
820                   // Sort the queue first
821                   pPriorityGrp->sort(mCameraInProgress);
822
823                   // Do (shadowable) solids
824                   OctreeSceneManager::renderObjects(pPriorityGrp->_getSolidPasses(), true);
825           }
826   }
827   else
828   {
829           SceneManager::renderModulativeStencilShadowedQueueGroupObjects(pGroup);
830   }
831}
832} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.