source: GTP/trunk/App/Demos/Vis/HillyTerrain/OGRE/TestCullingTerrainApplication.cpp @ 2455

Revision 2455, 23.6 KB checked in by mattausch, 17 years ago (diff)
Line 
1#include <OgreNoMemoryMacros.h>
2#include <CEGUI/CEGUI.h>
3#include <../CEGUIRenderer/include/OgreCEGUIRenderer.h>
4#include <../CEGUIRenderer/include/OgreCEGUIResourceProvider.h>
5#include <../CEGUIRenderer/include/OgreCEGUITexture.h>
6#include <OgreMemoryMacros.h>
7#include <OgreIteratorWrappers.h>
8#include <Ogre.h>
9#include "TestCullingTerrainApplication.h"
10#include "TerrainFrameListener.h"
11
12#if COLLADA
13#include "OgreColladaPrerequisites.h"
14#include "OgreColladaDocument.h"
15#include "OgreColladaScene.h"
16#endif
17
18#include "IVReader.h"
19
20#define WIN32_LEAN_AND_MEAN
21#include <windows.h>
22
23const static bool USE_STATIC_GEOMETRY = false;
24bool TestCullingTerrainApplication::msShowHillyTerrain = false;
25
26
27
28/*************************************************************/
29/*                EntityState implementation                 */
30/*************************************************************/
31
32
33Vector3 EntityState::msMinPos = Vector3::ZERO;
34Vector3 EntityState::msMaxPos = Vector3::ZERO;
35
36EntityState::EntityState(Entity *ent, State entityState, Real speed):
37mEntity(ent), mState(entityState), mAnimationSpeed(speed)
38{
39        switch(entityState)
40        {
41        case MOVING:
42                mAnimationState = mEntity->getAnimationState("Walk");
43                break;
44        case WAITING:
45                mAnimationState = mEntity->getAnimationState("Idle");
46                break;
47        case STOP:
48                mAnimationState = NULL;
49                break;
50        default:
51                break;
52        }
53        // enable animation state
54        if (mAnimationState)
55        {
56                mAnimationState->setLoop(true);
57                mAnimationState->setEnabled(true);
58        }
59
60        mTimeElapsed = Math::RangeRandom(1, 5);
61}
62//-----------------------------------------------------------------------
63EntityState::~EntityState()
64{
65        mAnimationState = NULL;
66        mEntity = NULL;
67}
68//-----------------------------------------------------------------------
69Entity *EntityState::GetEntity()
70{
71        return mEntity;
72}
73//-----------------------------------------------------------------------
74EntityState::State EntityState::GetState()
75{
76        return mState;
77}
78//-----------------------------------------------------------------------
79void EntityState::update(Real timeSinceLastFrame)
80{
81        mTimeElapsed -= timeSinceLastFrame * mAnimationSpeed;
82
83        if (!mEntity || !mAnimationState)
84                return;
85       
86        if (mState == MOVING) // toggle between moving (longer) and waiting (short)
87        {
88                SceneNode *parent = mEntity->getParentSceneNode();
89               
90                if (!parent)
91                        return;
92
93                if (mTimeElapsed <= 0) // toggle animation state
94                {
95                        if (mAnimationState->getAnimationName() == "Idle")
96                        {
97                                SetAnimationState("Walk", true);
98                               
99                                mTimeElapsed = walk_duration; // walk for mTimeElapsed units
100
101                                // choose random rotation
102                                Radian rnd = Radian(Math::UnitRandom() * Math::PI * rotate_factor);
103
104                                //mEntity->getParentSceneNode()->rotate();
105                                parent->yaw(rnd);                                               
106                        }
107                        else
108                        {
109                                SetAnimationState("Idle", true);
110                                mTimeElapsed = wait_duration; // wait for mTimeElapsed seconds
111                        }
112                }
113
114                if (mAnimationState->getAnimationName() == "Walk") // move forward
115                {
116                        // store old position, just in case we get out of bounds
117                        Vector3 oldPos = parent->getPosition();
118                        parent->translate(parent->getLocalAxes(), Vector3(move_factor * mAnimationSpeed, 0, 0));
119
120                        // HACK: if out of bounds => reset to old position and set animationstate to idle
121                        if (OutOfBounds(parent))
122                        {
123                                parent->setPosition(oldPos);
124                                SetAnimationState("Idle", true);
125                               
126                                mTimeElapsed = wait_duration;
127                        }
128                }
129        }
130               
131        // add time to drive animation
132        mAnimationState->addTime(timeSinceLastFrame * mAnimationSpeed);
133}
134//-----------------------------------------------------------------------
135void EntityState::SetAnimationState(String stateStr, bool loop)
136{
137        if (!mEntity)
138                return;
139
140        mAnimationState = mEntity->getAnimationState(stateStr);
141        mAnimationState->setLoop(loop);
142        mAnimationState->setEnabled(true);
143}
144//-----------------------------------------------------------------------
145bool EntityState::OutOfBounds(SceneNode *node)
146{
147        Vector3 pos = node->getPosition();
148
149        if ((pos > msMinPos) && (pos < msMaxPos))
150                return false;
151
152        return true;
153}
154
155
156
157/********************************************************************/
158/*            TestCullingTerrainApplication implementation          */
159/********************************************************************/
160
161
162TestCullingTerrainApplication::TestCullingTerrainApplication():
163mTerrainContentGenerator(NULL),
164mRayQueryExecutor(NULL),
165mIVReader(NULL),
166mFilename("terrain"),
167mViewCellsFilename(""),
168mAlgorithm(GtpVisibility::VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING)
169{
170}
171//-------------------------------------------------------------------------
172void TestCullingTerrainApplication::loadConfig(const String& filename)
173{
174        // Set up the options
175        ConfigFile config;
176        String val;
177
178        config.load(filename);
179
180        std::stringstream d; d << "reading the config file from: " << filename;
181        LogManager::getSingleton().logMessage(d.str());
182
183        val = config.getSetting("Scene");
184
185    if (!val.empty())
186        {
187                 mFilename = val.c_str();
188                 d << "\nloading scene from file: " << mFilename;
189                 LogManager::getSingleton().logMessage(d.str());
190        }
191}
192//-----------------------------------------------------------------------
193TestCullingTerrainApplication::~TestCullingTerrainApplication()
194{
195        OGRE_DELETE(mTerrainContentGenerator);
196        OGRE_DELETE(mRayQueryExecutor);
197        OGRE_DELETE(mIVReader);
198
199        deleteEntityStates();   
200}
201//-----------------------------------------------------------------------
202void TestCullingTerrainApplication::deleteEntityStates()
203{
204        for (int i = 0; i < (int)mEntityStates.size(); ++i)
205        {
206                OGRE_DELETE(mEntityStates[i]);
207        }
208
209        mEntityStates.clear();
210}
211//-----------------------------------------------------------------------
212void TestCullingTerrainApplication::createCamera()
213{
214        // create the camera
215        mCamera = mSceneMgr->createCamera("PlayerCam");
216       
217        /** set a nice viewpoint
218        *       we use a camera node here and apply all transformations on it instead
219        *       of applying all transformations directly to the camera
220        *       because then the camera is displayed correctly in the visualization
221        */
222        // hack: vienna view point
223        Vector3 viewPoint(830, 300, -540);
224       
225        mCamNode = mSceneMgr->getRootSceneNode()->
226                createChildSceneNode("CamNode1", viewPoint);
227       
228        mCamNode->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
229        mCamNode->attachObject(mCamera);
230       
231        ///////////////////
232        //-- create visualization camera
233
234        mVizCamera = mSceneMgr->createCamera("VizCam");
235        mVizCamera->setPosition(mCamNode->getPosition());
236
237        mVizCamera->setNearClipDistance(1);
238        mCamera->setNearClipDistance(1);
239
240        // infinite far plane?
241        if (mRoot->getRenderSystem()->getCapabilities()->hasCapability(RSC_INFINITE_FAR_PLANE))
242        {
243                mVizCamera->setFarClipDistance(0);
244                mCamera->setFarClipDistance(0);
245        }
246        else
247        {
248                 mVizCamera->setFarClipDistance(20000);
249                 mCamera->setFarClipDistance(20000);
250        }       
251}
252
253//-----------------------------------------------------------------------
254bool TestCullingTerrainApplication::setup()
255{
256        bool carryOn = ExampleApplication::setup();
257
258        if (carryOn)
259                createRenderTargetListener();
260
261        return carryOn;
262}
263//-----------------------------------------------------------------------
264void TestCullingTerrainApplication::createRenderTargetListener()
265{
266        mWindow->addListener(new VisualizationRenderTargetListener(mSceneMgr));
267}
268//-----------------------------------------------------------------------
269bool TestCullingTerrainApplication::LoadSceneCollada(const String &filename,
270                                                                                                         SceneNode *root,
271                                                                                                         const int index)
272{
273#if COLLADA
274        // Collada
275        ColladaDocument *daeDoc = new ColladaDocument(mSceneMgr);
276
277        String daeName = filename;
278        //if (daeName.empty())
279        //      daeName = "City_1500.dae";
280
281        // default directory
282        //daeName.insert(0, "../../media/models/collada/City");
283        LogManager::getSingleton().logMessage("ColladaDocument - import started");
284
285        // full import
286        if (daeDoc->doImport(daeName))
287        {
288                LogManager::getSingleton().logMessage("loading collada");
289                /**
290                 * build up scene
291                 * if you only want a specific part of the scene graph, you must fetch a scene node
292                 * by its unique node name and give it as parameter
293                 * e.g.
294                 * ColladaSceneNode *box = mScene->getNode("Box2");
295                 * if (box != NULL) box->createOgreInstance(NULL);
296                 */
297                daeDoc->getScene()->createOgreInstance(NULL);
298
299                // everything is loaded, we do not need the collada document anymore
300                delete daeDoc;
301
302                return true;
303        }
304        else
305        {
306                LogManager::getSingleton().logMessage("ColladaDocument - import failed");
307                return false;
308        }
309#endif
310        return true;
311}
312//-----------------------------------------------------------------------
313bool TestCullingTerrainApplication::LoadSceneIV(const String &filename,
314                                                                                                SceneNode *root,
315                                                                                                const int index)
316{
317        mIVReader = new IVReader();
318
319        Timer *timer = PlatformManager::getSingleton().createTimer();
320        timer->reset();
321
322        if (1)
323        {
324                std::string logFilename = "IVLog" + Ogre::StringConverter().toString(index) + ".log";
325               
326                Log *log = LogManager::getSingleton().createLog(logFilename);
327                mIVReader->setLog(log);
328        }
329       
330        //viennaNode->translate(Vector3(-300, -300, 0));
331
332        if (mIVReader->loadFile(filename.c_str()))
333        {
334                SceneNode *node = root->createChildSceneNode("IVSceneNode" + index);
335
336                mIVReader->buildTree(mSceneMgr, node);
337               
338                mIVReader->collapse();
339                OGRE_DELETE(mIVReader);
340
341                std::stringstream d;
342                d << "loaded " << filename << " in " << timer->getMilliseconds() * 1e-3 << " secs";
343                LogManager::getSingleton().logMessage(d.str());
344               
345                PlatformManager::getSingleton().destroyTimer(timer);
346
347                //-- bake into static geometry
348                if (USE_STATIC_GEOMETRY)
349                {
350                        BakeSceneIntoStaticGeometry("staticVienna", "Vienna");
351                }
352       
353                return true;
354        }
355
356        return false;
357}
358//--------------------------------------------------------
359void TestCullingTerrainApplication::BakeSceneIntoStaticGeometry(const String &staticGeomName,
360                                                                                                                                const String &nodeName)
361{
362#if OGRE_103
363        // note: different static geom for roofs, roads, ..., becazse they have same material
364        StaticGeometry *staticGeom = mSceneMgr->createStaticGeometry(staticGeomName);
365
366        // note: looping over entities here. why does scene node not work?
367        SceneManager::EntityIterator it = mSceneMgr->getEntityIterator();
368        while (it.hasMoreElements())
369        {
370                Entity *ent = it.getNext();
371                ent->setVisible(false);
372                staticGeom->addEntity(ent, ent->getParentSceneNode()->getPosition());
373        }
374
375        staticGeom->setRegionDimensions(Vector3(100,100,100));
376        staticGeom->build();
377
378        // cleanup node
379        //wallsNode->detachAllObjects();
380       
381        //roofsNode->detachAllObjects();
382        //roadsNode->detachAllObjects();
383        //planeNode->detachAllObjects();
384       
385        //viennaNode->removeChild("Walls");
386        mSceneMgr->destroySceneNode(nodeName);
387#endif
388}
389//--------------------------------------------------------
390void TestCullingTerrainApplication::createScene()
391{
392        /////////
393        //-- load parameters & scene
394        //loadConfig("terrainCulling.cfg");
395       
396        /////////////////////////////////////
397
398        // Set ambient light
399        mAmbientLight = ColourValue(0.5, 0.5, 0.5);
400        mSceneMgr->setAmbientLight(mAmbientLight);
401       
402        //-- create light
403        mSunLight = mSceneMgr->createLight("SunLight");
404        mSunLight->setType(Light::LT_DIRECTIONAL);
405        //mSunLight->setType(Light::LT_SPOTLIGHT);
406        //mSunLight->setSpotlightRange(Degree(30), Degree(50));
407
408    mSunLight->setPosition(707, 2000, 500);
409        mSunLight->setCastShadows(true);
410
411        // set light angle not too small over the surface,
412        // otherwise shadows textures will be broken
413        Vector3 dir(0.5, 1, 0.5);
414        dir.normalise();
415        mSunLight->setDirection(dir);
416        //mSunLight->setDirection(Vector3::NEGATIVE_UNIT_Y);
417        mSunLight->setDiffuseColour(1, 1, 1);
418        mSunLight->setSpecularColour(1, 1, 1);
419
420        //-- Fog
421
422        // NB it's VERY important to set this before calling setWorldGeometry
423        // because the vertex program picked will be different
424        ColourValue fadeColour(0.93, 0.86, 0.76);
425        mWindow->getViewport(0)->setBackgroundColour(fadeColour);
426        //mSceneMgr->setFog(FOG_LINEAR, fadeColour, .001, 500, 1000);
427       
428        // Create a skybox
429        mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox", 50000, true);
430       
431        // terrain creation
432        std::string terrain_cfg("terrainCulling.cfg");
433       
434        mSceneMgr->setWorldGeometry(terrain_cfg);
435
436        // was terrain loaded?
437        mSceneMgr->getOption("ShowTerrain", &msShowHillyTerrain);
438        msShowHillyTerrain=true;
439
440               
441        // hack view point for terrain
442        if (msShowHillyTerrain)
443        {
444                Vector3 viewPoint(707, 5000, 528);
445                mCamNode->setPosition(viewPoint);
446        }
447
448       
449        //-- CEGUI setup
450        setupGui();
451
452        // Warning: In GL since we can't go higher than the window res
453        mSceneMgr->setShadowTextureSettings(1024, 2);
454        mSceneMgr->setShadowColour(ColourValue(0.5, 0.5, 0.5));
455
456
457        //////////////
458        //-- terrain content setup
459
460        // HACK: necessary to call once here for terrain initialisation
461        mSceneMgr->_renderScene(mCamera, mWindow->getViewport(0), true);
462
463        // ray query executor: needed to clamp to terrain
464        mRayQueryExecutor = new RayQueryExecutor(mSceneMgr);
465
466        mTerrainMinPos = EntityState::msMinPos = Vector3(0, 0, 0);
467        mTerrainMaxPos = EntityState::msMaxPos = Vector3(5000, 5000, 5000);
468
469        mTerrainContentGenerator = new TerrainContentGenerator(mSceneMgr);
470               
471        // if no objects in file, we generate new objects
472        if (msShowHillyTerrain && !mTerrainContentGenerator->LoadObjects("objects.out"))
473        {
474                // the objects are generated randomly distributed over the terrain
475                if (1) generateScene(500, 0); // create robots
476                if (0) generateScene(300, 1); // create ninjas
477                if (0) generateScene(1000, 2); // create trees
478        }
479
480        int maxDepth = 30;
481       
482        mSceneMgr->setOption("BiHierarchyMaxDepth", &maxDepth);
483       
484        int mode = 1;
485       
486        mSceneMgr->setOption("EnhancedVisibility", &mode);
487        mSceneMgr->setOption("RebuildBiHierarchy", NULL);
488        mSceneMgr->setOption("RebuildKdTree", NULL);
489}
490//-----------------------------------------------------------------------
491void  TestCullingTerrainApplication::generateScene(int num, int objectType)
492{
493        const float val = TerrainFrameListener::msObjectScales[objectType];
494        Vector3 scale(val, val, val);
495        const float maxHeight = 75;
496       
497        // In order to provide much occlusion,
498        // height is restricted to maxHeight => no objects are created on peaks
499        mTerrainContentGenerator->SetMinPos(Vector3(mTerrainMinPos));
500        mTerrainContentGenerator->SetMaxPos(Vector3(mTerrainMaxPos.x, maxHeight, mTerrainMaxPos.z));
501       
502        mTerrainContentGenerator->SetScale(scale);
503        mTerrainContentGenerator->SetOffset(TerrainFrameListener::msObjectTerrainOffsets[objectType]);
504        mTerrainContentGenerator->GenerateScene(num, TerrainFrameListener::msObjectCaptions[objectType]);
505
506        if (objectType != 0) // from our objects, only robot has animation phases
507                return;
508
509        EntityList *entList = mTerrainContentGenerator->GetGeneratedEntities();
510
511        /////////////
512        //-- add animation state for new robots (located at the end of the list)
513       
514        for (size_t i = entList->size() - (unsigned int)num; i < entList->size(); ++ i)
515        {
516                mEntityStates.push_back(new EntityState((*entList)[i],
517                        EntityState::WAITING, Math::RangeRandom(0.5, 1.5)));
518        }
519
520        // release limitations on height => it is possible for the user to put single
521        // objects on peaks of the terrain (will be only few, not relevant for occlusion)
522        mTerrainContentGenerator->SetMaxPos(mTerrainMaxPos);
523}
524//-----------------------------------------------------------------------
525void TestCullingTerrainApplication::updateAnimations(Real timeSinceLastFrame)
526{
527        for (int i = 0; i < (int)mEntityStates.size(); ++i)
528        {
529                SceneNode *sn = mEntityStates[i]->GetEntity()->getParentSceneNode();
530
531                mEntityStates[i]->update(timeSinceLastFrame);
532
533                if (mEntityStates[i]->GetState() == EntityState::MOVING)
534                {
535                        Clamp2Terrain(sn, 0); //sn->setNodeVisible(false);
536                }
537        }
538}
539//-----------------------------------------------------------------------
540EntityStates  &TestCullingTerrainApplication::getEntityStates()
541{
542        return mEntityStates;
543}
544//-----------------------------------------------------------------------
545void TestCullingTerrainApplication::setupGui()
546{
547#if OGRE103
548         mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY,
549                 false, 3000, ST_EXTERIOR_CLOSE);
550#else
551          mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY,
552                 false, 3000, mSceneMgr);
553#endif
554     mGUISystem = new CEGUI::System(mGUIRenderer);
555
556         // Mouse
557     CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLook.scheme");
558     CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
559         mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook",
560                                                                           (CEGUI::utf8*)"MouseArrow");
561
562         CEGUI::MouseCursor::getSingleton().hide();
563}
564//-----------------------------------------------------------------------
565void TestCullingTerrainApplication::createFrameListener()
566{
567        mTerrainFrameListener =
568                new TerrainFrameListener(mWindow,
569                                                                 mCamera,
570                                                                 mSceneMgr,
571                                                                 mGUIRenderer,
572                                                                 mTerrainContentGenerator,
573                                                                 mVizCamera,
574                                                                 mCamNode,
575                                                                 mSunLight,
576                                                                 this);
577
578        mTerrainFrameListener->setPriority(10);
579       
580        mRoot->addFrameListener(mTerrainFrameListener);
581}
582//-----------------------------------------------------------------------
583void TestCullingTerrainApplication::chooseSceneManager()
584{
585#ifdef OGRE_103
586        if (msShowHillyTerrain)
587        {
588                // Terrain scene manager
589                mSceneMgr = mRoot->getSceneManager(ST_EXTERIOR_CLOSE);
590        }
591        else
592        {       // octree scene manager
593                mSceneMgr = mRoot->getSceneManager(ST_GENERIC);
594        }
595#else
596
597        mSceneMgr = mRoot->createSceneManager("OcclusionCullingSceneManager");
598       
599        //mSceneMgr = mRoot->createSceneManager("TerrainSceneManager");
600        //mSceneMgr = mRoot->createSceneManager("OctreeSceneManager");
601        //mSceneMgr = mRoot->createSceneManager("BiHierarchySceneManager");
602        //mSceneMgr = mRoot->createSceneManager("BiTreeTerrainSceneManager");
603       
604        bool useEnhancedVisibility = true;
605        mSceneMgr->setOption("EnhancedVisibility", &useEnhancedVisibility);
606#endif
607}
608//-----------------------------------------------------------------------
609bool TestCullingTerrainApplication::Clamp2Terrain(SceneNode *node, int terrainOffs)
610{
611        // clamp scene node to terrain
612        Vector3 pos = node->getPosition();
613        Vector3 queryResult;
614
615        if (mRayQueryExecutor->executeRayQuery(&queryResult,
616                        Vector3(pos.x, MAX_HEIGHT, pos.z), Vector3::NEGATIVE_UNIT_Y))
617        {
618        node->setPosition(pos.x, queryResult.y + terrainOffs, pos.z);
619                return true;
620        }
621
622        return false;
623}
624
625//-----------------------------------------------------------------------
626bool TestCullingTerrainApplication::Clamp2FloorPlane(const float dist)
627{
628    // clamp to floor plane
629        RaySceneQuery *raySceneQuery = mSceneMgr->createRayQuery(
630                Ray(mCamNode->getPosition(), Vector3::NEGATIVE_UNIT_Y));
631
632        RaySceneQueryResult& qryResult = raySceneQuery->execute();
633   
634        RaySceneQueryResult::iterator rit = qryResult.begin();
635        bool success = false;
636       
637        float yVal = 0;
638        float minVal = 999999999999;
639
640        while (rit != qryResult.end() && rit->movable)
641        {
642                if (rit->movable->getName() != "PlayerCam")
643                {
644                        // place on the ground object
645                        yVal = rit->movable->getWorldBoundingBox().getCenter().y;
646                                if (yVal < minVal)
647                                        minVal = yVal;
648
649                        //std::stringstream d; d << "dist: " << dist << endl;
650                        //Ogre::LogManager()
651                        success = true;
652                }
653
654                ++ rit;
655        }
656   
657        // place on the ground object
658        if (success)
659                        mCamNode->setPosition(
660                                mCamNode->getPosition().x,
661                                minVal + dist,
662                                mCamNode->getPosition().z);
663
664        OGRE_DELETE(raySceneQuery);
665        return success;
666}
667
668//-----------------------------------------------------------------------
669// splits strings containing multiple file names
670static int SplitFilenames(const std::string str, std::vector<std::string> &filenames)
671{
672        int pos = 0;
673
674        while(1)
675        {
676                int npos = (int)str.find(';', pos);
677               
678                if (npos < 0 || npos - pos < 1)
679                        break;
680                filenames.push_back(std::string(str, pos, npos - pos));
681                pos = npos + 1;
682        }
683       
684        filenames.push_back(std::string(str, pos, str.size() - pos));
685        return (int)filenames.size();
686}
687//-----------------------------------------------------------------------
688bool TestCullingTerrainApplication::LoadScene(const String &filename)
689{
690        using namespace std;
691        // use leaf nodes of the original spatial hierarchy as occludees
692        vector<string> filenames;
693        int files = SplitFilenames(filename, filenames);
694       
695        std::stringstream d;
696        d << "number of input files: " << files << "\n";
697        LogManager::getSingleton().logMessage(d.str());
698
699        bool result = false;
700        vector<string>::const_iterator fit, fit_end = filenames.end();
701        int i = 0;
702
703        for (fit = filenames.begin(); fit != fit_end; ++ fit, ++ i)
704        {
705                const string fn = *fit;
706
707                if (strstr(fn.c_str(), ".iv") || strstr(fn.c_str(), ".wrl"))
708                {
709                        // load iv files
710                        if (!LoadSceneIV(fn, mSceneMgr->getRootSceneNode(), i))
711                        {
712                                // terrain hack
713                                //msShowHillyTerrain = true;
714                                LogManager::getSingleton().logMessage("error loading scene => load terrain");
715                        }
716                }
717                else if (strstr(filename.c_str(), ".dae"))
718                {
719                        // load collada files
720                        LoadSceneCollada(fn, mSceneMgr->getRootSceneNode(), i);         
721                }
722                else //if (filename == "terrain")
723                {
724                        // terrain hack
725                        msShowHillyTerrain = true;
726                        LogManager::getSingleton().logMessage("loading terrain");
727                }
728       
729                result = true;
730        }
731       
732        return result;
733}
734//-----------------------------------------------------------------------
735bool TestCullingTerrainApplication::LoadViewCells(const String &filename)
736{
737        // if not already loaded,
738        // the scene manager will load the view cells
739        return mSceneMgr->setOption("UseViewCells", filename.c_str());
740}
741
742
743/**********************************************************************/
744/*           VisualizationRenderTargetListener implementation         */
745/**********************************************************************/
746
747
748//-----------------------------------------------------------------------
749VisualizationRenderTargetListener::VisualizationRenderTargetListener(SceneManager *sceneMgr)
750:RenderTargetListener(), mSceneMgr(sceneMgr)
751{
752}
753//-----------------------------------------------------------------------
754void VisualizationRenderTargetListener::preViewportUpdate(const RenderTargetViewportEvent &evt)
755{
756        // visualization viewport
757        const bool showViz = evt.source->getZOrder() == VIZ_VIEWPORT_Z_ORDER;
758        const bool nShowViz = !showViz;
759
760        mSavedShadowTechnique = mSceneMgr->getShadowTechnique();
761        mSavedAmbientLight = mSceneMgr->getAmbientLight();
762
763        // -- ambient light must be full for visualization, shadows disabled
764    if (showViz)
765        {
766                mSceneMgr->setAmbientLight(ColourValue(1, 1, 1));
767                mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE);
768        }
769       
770    mSceneMgr->setOption("PrepareVisualization", &showViz);
771        mSceneMgr->setOption("SkyBoxEnabled", &nShowViz);
772       
773        RenderTargetListener::preViewportUpdate(evt);
774}
775//-----------------------------------------------------------------------
776void VisualizationRenderTargetListener::postRenderTargetUpdate(const RenderTargetEvent &evt)
777{
778        // reset values
779        mSceneMgr->setShadowTechnique(mSavedShadowTechnique);
780        mSceneMgr->setAmbientLight(mSavedAmbientLight);
781       
782        RenderTargetListener::postRenderTargetUpdate(evt);
783}
784//-----------------------------------------------------------------------
785INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
786{
787    // Create application object
788    TestCullingTerrainApplication app;
789
790        try
791        {
792        app.go();
793    }
794        catch( Ogre::Exception& e )
795        {
796        MessageBox( NULL, e.getFullDescription().c_str(),
797                        "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
798    }   
799
800    return 0;
801}
Note: See TracBrowser for help on using the repository browser.