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

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