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

Revision 1003, 25.8 KB checked in by mattausch, 18 years ago (diff)

added collada importer

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