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

Revision 868, 28.2 KB checked in by mattausch, 18 years ago (diff)

fitting objects to bb, debug

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