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

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