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

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
13// normal terrain rendering
14const static NORMAL_RENDER_HACK = false;
15
16namespace Ogre {
17
18//-----------------------------------------------------------------------
19VisibilityTerrainSceneManager::VisibilityTerrainSceneManager(
20        GtpVisibility::VisibilityManager *visManager):
21TerrainSceneManager(),
22mVisibilityManager(visManager),
23mShowVisualization(false),
24mRenderNodesForViz(false),
25mRenderNodesContentForViz(false),
26mVisualizeCulledNodes(false),
27mLeavePassesInQueue(0),
28mDelayRenderTransparents(true),
29mUseDepthPass(false),
30mIsDepthPassPhase(false),
31mUseItemBuffer(false),
32mIsItemBufferPhase(false),
33mCurrentEntityId(1),
34mEnableDepthWrite(true),
35mSkipTransparents(false),
36mRenderTransparentsForItemBuffer(true),
37mExecuteVertexProgramForAllPasses(false),
38mIsHierarchicalCulling(false)
39{
40        mHierarchyInterface = new OctreeHierarchyInterface(this, mDestRenderSystem);
41       
42        if (0)
43        {
44                mDisplayNodes = true;
45                mShowBoundingBoxes = true;
46                mShowBoxes = true;
47        }
48
49        // TODO: set maxdepth to reasonable value
50        mMaxDepth = 50;
51}
52//-----------------------------------------------------------------------
53void VisibilityTerrainSceneManager::InitDepthPass()
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);
62
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//-----------------------------------------------------------------------
74VisibilityTerrainSceneManager::~VisibilityTerrainSceneManager()
75{
76        OGRE_DELETE(mHierarchyInterface);
77}
78//-----------------------------------------------------------------------
79void VisibilityTerrainSceneManager::InitItemBufferPass()
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);
94                //mItemBufferPass->setLightingEnabled(false);
95        }
96        else
97        {
98                mItemBufferPass = itemBufferMat->getTechnique(0)->getPass(0);
99        }
100        //mItemBufferPass->setAmbient(1, 1, 0);
101}
102//-----------------------------------------------------------------------
103void VisibilityTerrainSceneManager::PrepareVisualization(Camera *cam)
104{
105        // add player camera for visualization purpose
106        try
107        {
108                Camera *c;
109                if ((c = getCamera("PlayerCam")) != NULL)
110                {
111                        getRenderQueue()->addRenderable(c);
112                }   
113    }
114    catch (...)
115    {
116        // ignore
117    }
118        // add bounding boxes of rendered objects
119        for (BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it)
120        {
121                getRenderQueue()->addRenderable(*it);
122        }
123       
124        if (mRenderNodesForViz || mRenderNodesContentForViz)
125        {
126                // HACK: change node material so it is better suited for visualization
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
132                for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
133                {
134                        if (mRenderNodesForViz)
135                        {
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                                }
142
143                                // addbounding boxes instead of node itself
144                                //(*it)->_addBoundingBoxToQueue(getRenderQueue());
145                        }
146                        if (mRenderNodesContentForViz)
147                        {
148                                (*it)->_addToRenderQueue(cam, getRenderQueue(), false);
149                        }
150                }
151        }       
152}
153//-----------------------------------------------------------------------
154Pass *VisibilityTerrainSceneManager::setPass(Pass* pass)
155{
156        if (NORMAL_RENDER_HACK)
157        {
158                return SceneManager::setPass(pass);
159        }
160
161        // TODO: setting vertex program is not efficient
162        //Pass *usedPass = ((mIsDepthPassPhase && !pass->hasVertexProgram()) ? mDepthPass : pass);
163       
164        // set depth fill pass if we currently do not make an aabb occlusion query
165        const bool useDepthPass =
166                (mIsDepthPassPhase && !mHierarchyInterface->IsBoundingBoxQuery());
167
168        Pass *usedPass = useDepthPass ? mDepthPass : pass;
169       
170
171        const IlluminationRenderStage savedStage = mIlluminationStage;
172       
173        // set illumination stage to NONE so no shadow material is used
174        // for depth pass or for occlusion query
175        if (mIsDepthPassPhase || mHierarchyInterface->IsBoundingBoxQuery())
176        {
177                mIlluminationStage = IRS_NONE;
178        }
179       
180        // --- set vertex program of current pass in order to set correct depth
181        if (mExecuteVertexProgramForAllPasses &&
182                mIsDepthPassPhase &&
183                pass->hasVertexProgram())
184        {
185                // add vertex program of current pass to depth pass
186                mDepthPass->setVertexProgram(pass->getVertexProgramName());
187
188                if (mDepthPass->hasVertexProgram())
189                {
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());
196                }
197        }
198        else if (mDepthPass->hasVertexProgram()) // reset vertex program
199        {
200                mDepthPass->setVertexProgram("");
201        }
202       
203        // save old depth write: needed for item buffer
204        const bool IsDepthWrite = usedPass->getDepthWriteEnabled();
205
206        // global option which enables / disables depth writes
207        if (!mEnableDepthWrite)
208        {
209                usedPass->setDepthWriteEnabled(false);
210        }
211       
212
213        //-- set actual pass here
214        Pass *result = SceneManager::setPass(usedPass);
215
216
217        // reset depth write
218        if (!mEnableDepthWrite)
219        {
220                usedPass->setDepthWriteEnabled(IsDepthWrite);
221        }
222
223        // reset illumination stage
224        mIlluminationStage = savedStage;
225
226        return result;
227}
228//-----------------------------------------------------------------------
229void VisibilityTerrainSceneManager::_findVisibleObjects(Camera* cam,
230                                                                                                                bool onlyShadowCasters)
231{
232        if (NORMAL_RENDER_HACK)
233        {
234                TerrainSceneManager::_findVisibleObjects(cam, onlyShadowCasters);
235                return;
236        }
237
238        //-- show visible scene nodes and octree bounding boxes from last frame
239        if (mShowVisualization)
240    {
241                PrepareVisualization(cam);
242        }
243        else
244        {       
245                // for hierarchical culling, we interleave identification
246                // and rendering of objects in _renderVisibibleObjects
247
248                // for the shadow pass we use only standard rendering
249                // because of low occlusion
250                if (mShadowTechnique == SHADOWTYPE_TEXTURE_MODULATIVE &&
251                        mIlluminationStage == IRS_RENDER_TO_TEXTURE)
252                {
253                        TerrainSceneManager::_findVisibleObjects(cam, onlyShadowCasters);
254                }
255
256                // only shadow casters will be rendered in shadow texture pass
257                if (0) mHierarchyInterface->SetOnlyShadowCasters(onlyShadowCasters);
258        }
259       
260       
261        // -- delete lists stored for visualization
262        mVisible.clear();
263        mBoxes.clear();
264}
265//-----------------------------------------------------------------------
266void VisibilityTerrainSceneManager::_renderVisibleObjects()
267{
268        if (NORMAL_RENDER_HACK)
269        {
270                TerrainSceneManager::_renderVisibleObjects();
271
272                return;
273        }
274
275        InitDepthPass();          // create material for depth pass
276        InitItemBufferPass(); // create material for item buffer pass
277
278        // save ambient light to reset later
279        ColourValue savedAmbient = mAmbientLight;
280
281        //-- apply standard rendering for some modes (e.g., visualization, shadow pass)
282
283        if (mShowVisualization ||
284           (mShadowTechnique == SHADOWTYPE_TEXTURE_MODULATIVE &&
285            mIlluminationStage == IRS_RENDER_TO_TEXTURE))
286        {       
287                IlluminationRenderStage savedStage = mIlluminationStage;
288       
289                if (mShowVisualization)
290                {
291                        // disable illumination stage to prevent rendering shadows
292                        mIlluminationStage = IRS_NONE;
293                }
294
295                // standard rendering for shadow maps because of performance
296                TerrainSceneManager::_renderVisibleObjects();
297
298                mIlluminationStage = savedStage;
299        }
300        else //-- the hierarchical culling algorithm
301        {
302                // this is also called in TerrainSceneManager: really
303                // nexessary?
304                mDestRenderSystem -> setLightingEnabled(false);
305
306                // don't render backgrounds for item buffer
307                if (mUseItemBuffer)
308                {
309                        clearSpecialCaseRenderQueues();
310                        getRenderQueue()->clear();
311                }
312
313                //-- hierarchical culling
314                // the objects of different layers (e.g., background, scene,
315                // overlay) must be identified and rendered one after another
316
317                //-- render all early skies
318                clearSpecialCaseRenderQueues();
319                addSpecialCaseRenderQueue(RENDER_QUEUE_BACKGROUND);
320                addSpecialCaseRenderQueue(RENDER_QUEUE_SKIES_EARLY);
321                setSpecialCaseRenderQueueMode(SceneManager::SCRQM_INCLUDE);
322
323                OctreeSceneManager::_renderVisibleObjects();
324                /////////////////////////////////////////////////
325
326#ifdef GTP_VISIBILITY_MODIFIED_OGRE
327                // delete previously rendered content
328                _deleteRenderedQueueGroups();
329#endif
330
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);
335       
336                // exclude this queues from hierarchical rendering
337                setSpecialCaseRenderQueueMode(SceneManager::SCRQM_EXCLUDE);
338
339                // set all necessary parameters for
340                // hierarchical visibility culling and rendering
341                InitVisibilityCulling(mCameraInProgress);
342
343                /**
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
348                **/
349               
350                mVisibilityManager->ApplyVisibilityCulling();
351
352                // delete remaining renderables from queue:
353                // all which are not in mLeavePassesInQueue)
354#ifdef GTP_VISIBILITY_MODIFIED_OGRE
355                _deleteRenderedQueueGroups(mLeavePassesInQueue);
356#endif
357
358                //-- reset parameters
359                mIsDepthPassPhase = false;
360                mIsItemBufferPhase = false;
361                mSkipTransparents = false;
362                mLeavePassesInQueue = 0;
363               
364#if 1   
365                // add visible nodes found by the visibility culling algorithm
366                if (mUseDepthPass)
367                {
368                        NodeList::const_iterator it, it_end = mVisible.end();
369
370                        //getRenderQueue()->clear();
371                        for (it = mVisible.begin(); it != it_end; ++ it)
372                        {
373                                (*it)->_addToRenderQueue(mCameraInProgress, getRenderQueue(), false);
374                        }
375                }
376#endif 
377                //-- now we can render all remaining queue objects
378                //-- used for depth pass, transparents, overlay
379                clearSpecialCaseRenderQueues();
380       
381                OctreeSceneManager::_renderVisibleObjects();
382       
383        } // hierarchical culling
384               
385        // HACK: set the new render level index, important to avoid cracks
386        // in terrain caused by LOD
387        TerrainRenderable::NextRenderLevelIndex();
388       
389        // reset ambient light
390        setAmbientLight(savedAmbient);
391
392        getRenderQueue()->clear(); // finally clear render queue
393        if (1) OGRE_DELETE(mRenderQueue); // HACK: should rather only be cleared ...
394
395        if (0) WriteLog(); // write out stats
396}
397
398//-----------------------------------------------------------------------
399void VisibilityTerrainSceneManager::_updateSceneGraph(Camera* cam)
400{
401        if (NORMAL_RENDER_HACK)
402        {
403                TerrainSceneManager::_updateSceneGraph(cam);
404                return;
405        }
406
407        mVisibilityManager->GetCullingManager()->SetHierarchyInterface(mHierarchyInterface);
408        mHierarchyInterface->SetRenderSystem(mDestRenderSystem);
409
410        TerrainSceneManager::_updateSceneGraph(cam);
411}
412//-----------------------------------------------------------------------
413bool VisibilityTerrainSceneManager::setOption(const String & key, const void * val)
414{
415        if (key == "UseDepthPass")
416        {
417                mUseDepthPass = (*static_cast<const bool *>(val));
418                return true;
419        }
420        if (key == "PrepareVisualization")
421        {
422                mShowVisualization = (*static_cast<const bool *>(val));
423                return true;
424        }
425        if (key == "RenderNodesForViz")
426        {
427                mRenderNodesForViz = (*static_cast<const bool *>(val));
428                return true;
429        }
430        if (key == "RenderNodesContentForViz")
431        {
432                mRenderNodesContentForViz = (*static_cast<const bool *>(val));
433                return true;
434        }
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        }
450        if (key == "VisualizeCulledNodes")
451        {
452                mVisualizeCulledNodes = (*static_cast<const bool *>(val));
453                return true;
454        }
455        if (key == "DelayRenderTransparents")
456        {
457                mDelayRenderTransparents = (*static_cast<const bool *>(val));
458                return true;
459        }
460
461        if (key == "DepthWrite")
462        {
463                mEnableDepthWrite = (*static_cast<const bool *>(val));
464                return true;
465        }
466        if (key == "UseItemBuffer")
467        {
468                mUseItemBuffer = (*static_cast<const bool *>(val));
469                return true;
470        }
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        }
481        if (key == "NodeVizScale")
482        {
483                OctreeNode::setVizScale(*static_cast<const float *>(val));
484                return true;
485        }
486
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        }
502        return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
503                setOption(key, val) || TerrainSceneManager::setOption(key, val);
504}
505//-----------------------------------------------------------------------
506bool VisibilityTerrainSceneManager::getOption(const String & key, void *val)
507{
508        if (key == "NumHierarchyNodes")
509        {
510                * static_cast<unsigned int *>(val) = (unsigned int)mNumOctants;
511                return true;
512        }
513       
514        return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
515                getOption(key, val) && TerrainSceneManager::getOption(key, val);
516}
517//-----------------------------------------------------------------------
518bool VisibilityTerrainSceneManager::getOptionValues(const String & key,
519                                                                                                        StringVector &refValueList)
520{
521        return TerrainSceneManager::getOptionValues( key, refValueList);
522}
523//-----------------------------------------------------------------------
524bool VisibilityTerrainSceneManager::getOptionKeys(StringVector & refKeys)
525{
526        return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
527                getOptionKeys(refKeys) || TerrainSceneManager::getOptionKeys(refKeys);
528}
529//-----------------------------------------------------------------------
530void VisibilityTerrainSceneManager::setVisibilityManager(GtpVisibility::
531                                                                                                                 VisibilityManager *visManager)
532{
533        mVisibilityManager = visManager;
534}
535//-----------------------------------------------------------------------
536GtpVisibility::VisibilityManager *VisibilityTerrainSceneManager::getVisibilityManager()
537{
538        return mVisibilityManager;
539}
540//-----------------------------------------------------------------------
541void VisibilityTerrainSceneManager::WriteLog()
542{
543        std::stringstream d;
544
545        d << "Depth pass: " << StringConverter::toString(mUseDepthPass) << ", "
546          << "Delay transparents: " << StringConverter::toString(mDelayRenderTransparents) << ", "
547          << "Use optimization: " << StringConverter::toString(mHierarchyInterface->GetTestGeometryForVisibleLeaves()) << ", "
548          << "Algorithm type: " << mVisibilityManager->GetCullingManagerType() << ", "
549          << "Hierarchy nodes: " << mNumOctants << ", "
550          << "Traversed nodes: " << mHierarchyInterface->GetNumTraversedNodes() << ", "
551          << "Rendered nodes: " << mHierarchyInterface->GetNumRenderedNodes() << ", "
552          << "Query culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumQueryCulledNodes() << ", "
553          << "Frustum culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumFrustumCulledNodes() << ", "
554      << "Queries issued: " << mVisibilityManager->GetCullingManager()->GetNumQueriesIssued() << ", "
555          << "Found objects: " << (int)mVisible.size() << "\n";
556
557        LogManager::getSingleton().logMessage(d.str());
558}
559//-----------------------------------------------------------------------
560void VisibilityTerrainSceneManager::renderObjects(
561                                                const RenderPriorityGroup::TransparentRenderablePassList& objs,
562                        bool doLightIteration,
563                                                const LightList* manualLightList)
564{
565        // for correct rendering, transparents must be rendered after hierarchical culling
566        // => do nothing
567        if (NORMAL_RENDER_HACK || !mSkipTransparents)
568        {
569                SceneManager::renderObjects(objs, doLightIteration, manualLightList);
570        }
571}
572//-----------------------------------------------------------------------
573bool VisibilityTerrainSceneManager::validatePassForRendering(Pass* pass)
574{       
575        if (NORMAL_RENDER_HACK)
576        {
577                return SceneManager::validatePassForRendering(pass);
578        }
579
580        // skip all but first pass if we are doing the depth pass
581        if ((mIsDepthPassPhase || mIsItemBufferPhase) && (pass->getIndex() > 0))
582        {
583                return false;
584        }
585        // all but first pass
586        /*else if ((!mIsDepthPassPhase || mIsItemBufferPhase) && (pass->getIndex() != 0))
587        {
588                return false;
589        }*/
590
591        return SceneManager::validatePassForRendering(pass);
592}
593//-----------------------------------------------------------------------
594void VisibilityTerrainSceneManager::renderQueueGroupObjects(RenderQueueGroup* pGroup)
595{
596        if (NORMAL_RENDER_HACK || !mIsItemBufferPhase)
597        {
598                OctreeSceneManager::renderQueueGroupObjects(pGroup);
599                return;
600        }
601
602        //-- item buffer: render objects using false colors
603
604    // Iterate through priorities
605    RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
606
607        while (groupIt.hasMoreElements())
608    {
609                RenderItemBuffer(groupIt.getNext());
610        }
611}
612//-----------------------------------------------------------------------
613void VisibilityTerrainSceneManager::RenderItemBuffer(RenderPriorityGroup* pGroup)
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
628                // Render only first pass of renderable as false color
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                {
639                        if (0)
640                        {
641                                std::stringstream d; d << "itembuffer, pass name: " <<
642                                ipass->first->getParent()->getParent()->getName();
643                               
644                                LogManager::getSingleton().logMessage(d.str());
645                        }
646                       
647                        RenderSingleObjectForItemBuffer(*irend, ipass->first);
648                }
649        }
650
651        //-- TRANSPARENT LOOP: must be handled differently from solids
652
653        // transparents are treated either as solids or completely discarded
654        if (mRenderTransparentsForItemBuffer)
655        {
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                        }
669                }
670        }
671}
672//-----------------------------------------------------------------------
673void VisibilityTerrainSceneManager::RenderSingleObjectForItemBuffer(Renderable *rend, Pass *pass)
674{
675        static LightList nullLightList;
676       
677        int col[4];
678       
679        // -- create color code out of object id
680        col[0] = (rend->getId() >> 16) & 255;
681        col[1] = (rend->getId() >> 8) & 255;
682        col[2] = rend->getId() & 255;
683//      col[3] = 255;
684
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));
690
691        // set vertex program of current pass
692        if (mExecuteVertexProgramForAllPasses && pass->hasVertexProgram())
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
713
714        // render a single object, this will set up auto params if required
715        renderSingleObject(rend, usedPass, false, &nullLightList);
716}
717//-----------------------------------------------------------------------
718GtpVisibility::VisibilityManager *VisibilityTerrainSceneManager::GetVisibilityManager()
719{
720        return mVisibilityManager;
721}
722//-----------------------------------------------------------------------
723void VisibilityTerrainSceneManager::InitVisibilityCulling(Camera *cam)
724{
725        // reset culling manager stats
726        mVisibilityManager->GetCullingManager()->InitFrame(mVisualizeCulledNodes);
727
728        // set depth pass flag before rendering
729        mIsDepthPassPhase = mUseDepthPass;
730
731        mIsHierarchicalCulling = true; // during hierarchical culling
732
733        // item buffer needs full ambient lighting to use item colors as unique id
734        if (mUseItemBuffer)
735        {
736                mIsItemBufferPhase = true;
737                setAmbientLight(ColourValue(1,1,1,1));
738        }
739
740
741        // set passes which are stored in render queue
742        // for rendering AFTER hierarchical culling, i.e., passes which need
743        // a special rendering order
744       
745        mLeavePassesInQueue = 0;
746
747        if (!mUseDepthPass && !mUseItemBuffer)
748        {
749                if (mShadowTechnique == SHADOWTYPE_STENCIL_ADDITIVE)
750                {
751                        // TODO: remove this pass because it should be processed during hierarchical culling
752                        //mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_NOSHADOW;
753
754                        mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_DECAL;
755                        mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_DIFFUSE_SPECULAR;
756                        mLeavePassesInQueue |= RenderPriorityGroup::TRANSPARENT_PASSES;
757
758                        // just render ambient stuff
759                        mIlluminationStage = IRS_AMBIENT;
760                        getRenderQueue()->setSplitPassesByLightingType(true);
761                }
762       
763                if (mShadowTechnique == SHADOWTYPE_STENCIL_MODULATIVE)
764                {
765                        mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_NOSHADOW;
766                        mLeavePassesInQueue |= RenderPriorityGroup::TRANSPARENT_PASSES;
767                }
768       
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                }
775        }
776
777        // skip rendering transparents during the hierarchical culling
778        // (because they will be rendered afterwards)
779        mSkipTransparents =
780                (mIsDepthPassPhase || (mLeavePassesInQueue & RenderPriorityGroup::TRANSPARENT_PASSES));
781
782        // -- initialise interface for rendering traversal of the hierarchy
783        mHierarchyInterface->SetHierarchyRoot(mOctree);
784       
785        // possible two cameras (one for culling, one for rendering)
786        mHierarchyInterface->InitTraversal(mCameraInProgress,
787                                                                           mCullCamera ? getCamera("CullCamera") : NULL,
788                                                                           mLeavePassesInQueue);
789               
790        //std::stringstream d; d << "leave passes in queue: " << mLeavePassesInQueue;LogManager::getSingleton().logMessage(d.str());
791}
792//-----------------------------------------------------------------------
793OctreeHierarchyInterface *VisibilityTerrainSceneManager::GetHierarchyInterface()
794{
795        return mHierarchyInterface;
796}
797//-----------------------------------------------------------------------
798void VisibilityTerrainSceneManager::endFrame()
799{
800        TerrainRenderable::ResetRenderLevelIndex();
801}
802//-----------------------------------------------------------------------
803Entity* VisibilityTerrainSceneManager::createEntity(const String& entityName,
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}
818//-----------------------------------------------------------------------
819void VisibilityTerrainSceneManager::renderAdditiveStencilShadowedQueueGroupObjects(
820        RenderQueueGroup* pGroup)
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        }
846        else // render the rest of the passes
847        {
848                OctreeSceneManager::renderAdditiveStencilShadowedQueueGroupObjects(pGroup);
849        }
850}
851//-----------------------------------------------------------------------
852void VisibilityTerrainSceneManager::renderModulativeStencilShadowedQueueGroupObjects(
853        RenderQueueGroup* pGroup)
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}
876
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
911} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.