source: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreVisibilityTerrainSceneManager.cpp @ 925

Revision 925, 30.9 KB checked in by mattausch, 18 years ago (diff)

update for ogre 1.2
OcclusionCullingSceneManager? is the only scenemanager in the solution now

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