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

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