source: trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp @ 87

Revision 87, 15.0 KB checked in by mattausch, 20 years ago (diff)
Line 
1/**
2    \file
3        TestCullingTerrainApplication.cpp
4    \brief
5        Tests the visibility culling algorithm
6*/
7
8#include <OgreNoMemoryMacros.h>
9#include <CEGUI/CEGUI.h>
10#include <../CEGUIRenderer/include/OgreCEGUIRenderer.h>
11#include <../CEGUIRenderer/include/OgreCEGUIResourceProvider.h>
12#include <../CEGUIRenderer/include/OgreCEGUITexture.h>
13#include <OgreMemoryMacros.h>
14
15#include "Ogre.h"
16#include "TestCullingTerrainApplication.h"
17
18#define WIN32_LEAN_AND_MEAN
19#include "windows.h"
20
21
22/*******************************************************/
23/*     TestCullingTerrainApplication implementation    */
24/*******************************************************/
25TestCullingTerrainApplication::~TestCullingTerrainApplication()
26{
27        delete mTerrainContentGenerator;
28}
29//-----------------------------------------------------------------------
30void TestCullingTerrainApplication::createCamera( void )
31{
32        // Create the camera
33        mCamera = mSceneMgr->createCamera("PlayerCam");
34
35        // Position it at 500 in Z direction
36        mCamera->setPosition(Vector3(128,25,128));
37
38        // Look back along -Z
39    mCamera->lookAt(Vector3(0,0,-300));
40    mCamera->setNearClipDistance(1);
41    mCamera->setFarClipDistance(1000);
42}
43//-----------------------------------------------------------------------
44void TestCullingTerrainApplication::createScene()
45{
46        // Set ambient light
47        mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
48       
49        // Create a light
50        Light* l = mSceneMgr->createLight("MainLight");
51        // Accept default settings: point light, white diffuse, just set position
52        // NB I could attach the light to a SceneNode if I wanted it to move automatically with
53        //  other objects, but I don't
54        l->setPosition(20,80,50);
55
56        // --Fog
57        // NB it's VERY important to set this before calling setWorldGeometry
58        // because the vertex program picked will be different
59        ColourValue fadeColour(0.93, 0.86, 0.76);
60        mWindow->getViewport(0)->setBackgroundColour(fadeColour);
61        //mSceneMgr->setFog( FOG_LINEAR, fadeColour, .001, 500, 1000);
62
63        // Create a skybox
64    //mSceneMgr->setSkyBox(true, "Examples/SpaceSkyBox", 500, false);
65        //mSceneMgr->setSkyDome( true, "Examples/CloudySky", 5, 8, 500, false );
66
67        /*      // Define the required skyplane
68        Plane plane;
69        // 5000 world units from the camera
70        plane.d = 5000;
71        // Above the camera, facing down
72        plane.normal = -Vector3::UNIT_Y;
73        // Create the plane 10000 units wide, tile the texture 3 times
74    mSceneMgr->setSkyPlane(true, plane, "Examples/SpaceSkyPlane",10000,3);*/
75
76        std::string terrain_cfg("terrain.cfg");
77#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
78        terrain_cfg = mResourcePath + terrain_cfg;
79#endif
80        mSceneMgr->setWorldGeometry(terrain_cfg);
81        // Infinite far plane?
82        if (mRoot->getRenderSystem()->getCapabilities()->hasCapability(RSC_INFINITE_FAR_PLANE))
83        {
84                mCamera->setFarClipDistance(0);
85        }
86
87        // Set a nice viewpoint
88        mCamera->setPosition(707, 2500, 528);
89        mCamera->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
90       
91        // CEGUI setup
92        setupGui();
93        // HACK: necessary to call once before the content creation for
94        // terrain initialisation
95        mSceneMgr->_renderScene(mCamera, mWindow->getViewport(0), true);
96
97        mTerrainContentGenerator = new TerrainContentGenerator(mSceneMgr);
98//      mTerrainContentGenerator->GenerateScene(500, "ninja.mesh");
99        mTerrainContentGenerator->GenerateScene(1000, "robot.mesh");
100
101        // no limitations needed anymore: the user can set
102        // objects also on peaks of terrain
103        mTerrainContentGenerator->SetMaxHeight(5000);
104}
105//-----------------------------------------------------------------------
106void TestCullingTerrainApplication::setupGui()
107{
108         mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY,
109                 false, 3000, ST_EXTERIOR_CLOSE);
110     mGUISystem = new CEGUI::System(mGUIRenderer);
111
112         // Mouse
113     CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLook.scheme");
114     CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
115         mGUISystem->setDefaultMouseCursor(
116                (CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
117
118         CEGUI::MouseCursor::getSingleton().show();
119}
120//-----------------------------------------------------------------------
121void TestCullingTerrainApplication::createFrameListener()
122{
123        mFrameListener= new MouseQueryListener(mWindow, mCamera, mSceneMgr,
124                mGUIRenderer, mTerrainContentGenerator);
125        mFrameListener->showDebugOverlay(true);
126        mRoot->addFrameListener(mFrameListener);
127}
128//-----------------------------------------------------------------------
129void TestCullingTerrainApplication::chooseSceneManager()
130{
131        mSceneMgr = mRoot->getSceneManager(ST_EXTERIOR_CLOSE);
132}
133/***********************************************/
134/*      MouseQueryListener implementation      */
135/***********************************************/
136//-----------------------------------------------------------------------
137MouseQueryListener::MouseQueryListener(RenderWindow* win, Camera* cam,
138                                                                           SceneManager *sceneManager,
139                                                                           CEGUI::Renderer *renderer,
140                                                                           TerrainContentGenerator *sceneGenerator):
141ExampleFrameListener(win, cam, false, true),
142mGUIRenderer(renderer),
143mShutdownRequested(false),
144mUseOptimization(false),
145mLMouseDown(false),
146mRMouseDown(false),
147mSceneMgr(sceneManager),
148mCurrentObject(NULL),
149mTerrainContentGenerator(sceneGenerator),
150mVisibilityThreshold(0),
151mCurrentAlgorithm(GtpVisibility::VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING),
152mShowOctree(true),
153mUseCulling(false)
154{
155        // Reduce move speed
156        mMoveSpeed = 50;
157        mRotateSpeed *= 2;
158   
159        // Register this so that we get mouse events.
160        mEventProcessor->addMouseListener(this);
161        mEventProcessor->addMouseMotionListener(this);
162        mEventProcessor->addKeyListener(this);
163
164        mRayQueryExecutor = new RayQueryExecutor(mSceneMgr);
165       
166        // show overlay
167        Overlay* pOver = OverlayManager::getSingleton().getByName("Example/VisibilityDemoOverlay");
168
169        mAlgorithmInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/AlgorithmInfo");
170        mThresholdInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/ThresholdInfo");
171       
172        mFrustumCulledNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/FrustumCulledNodesInfo");
173        mQueryCulledNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/QueryCulledNodesInfo");
174    mTraversedNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/TraversedNodesInfo");
175        mHierarchyNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/HierarchyNodesInfo");
176        mRenderedNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/RenderedNodesInfo");
177        mObjectsInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/ObjectsInfo");
178        mUseOptimizationInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/UseOptimizationInfo");
179        mQueriesIssuedInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/QueriesIssuedInfo");
180       
181        mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
182        mThresholdInfo->setCaption(": 0");
183        mFrustumCulledNodesInfo->setCaption(": 0");
184        mQueryCulledNodesInfo->setCaption(": 0");
185        mTraversedNodesInfo->setCaption(": 0");
186        mHierarchyNodesInfo->setCaption(": 0");
187        mRenderedNodesInfo->setCaption(": 0");
188        mObjectsInfo->setCaption(": 0");
189        mUseOptimizationInfo->setCaption(": true");
190        mQueriesIssuedInfo->setCaption(": 0");
191
192        setAlgorithm(mCurrentAlgorithm);
193        toggleUseOptimization();
194        toggleShowOctree();
195        toggleUseCulling();
196
197    pOver->show();
198}
199//-----------------------------------------------------------------------
200MouseQueryListener::~MouseQueryListener( )
201{
202        delete mRayQueryExecutor;
203}
204//-----------------------------------------------------------------------
205void MouseQueryListener::mouseMoved (MouseEvent *e)
206{
207        // Update CEGUI with the mouse motion
208    CEGUI::System::getSingleton().injectMouseMove(e->getRelX() * mGUIRenderer->getWidth(),
209                e->getRelY() * mGUIRenderer->getHeight());
210}
211//-----------------------------------------------------------------------
212void MouseQueryListener::mousePressed(MouseEvent* e)
213{
214     // Left mouse button down
215     if (e->getButtonID() & InputEvent::BUTTON0_MASK)
216     {
217                 CEGUI::MouseCursor::getSingleton().hide();
218
219                 // Setup the ray scene query
220         Ray mouseRay = mCamera->getCameraToViewportRay(e->getX(), e->getY());
221   
222                 Vector3 queryResult;
223                 
224                 // Get results, create a node/entity on the position
225                 mCurrentObject = mTerrainContentGenerator->GenerateSceneObject(
226                         mouseRay.getOrigin(), mouseRay.getDirection(), "robot.mesh");
227               
228         mLMouseDown = true;
229     }
230     // Right mouse button down
231     else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
232     {
233         CEGUI::MouseCursor::getSingleton().hide();
234         mRMouseDown = true;
235     } // else if
236} // mousePressed
237
238 //-----------------------------------------------------------------------
239void MouseQueryListener::mouseReleased(MouseEvent* e)
240{
241    // Left mouse button up
242    if (e->getButtonID() & InputEvent::BUTTON0_MASK)
243    {
244                CEGUI::MouseCursor::getSingleton().show();
245        mLMouseDown = false;
246    }
247    // Right mouse button up
248    else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
249    {
250        CEGUI::MouseCursor::getSingleton().show();
251        mRMouseDown = false;
252    }
253}
254//-----------------------------------------------------------------------
255void MouseQueryListener::mouseDragged(MouseEvent *e)
256 {
257         // If we are dragging the left mouse button.           
258         if (mLMouseDown)
259     {
260                 Vector3 queryResult;
261                 Ray mouseRay = mCamera->getCameraToViewportRay(e->getX(), e->getY());
262
263                 if(mRayQueryExecutor->executeRayQuery(&queryResult, mouseRay))
264                 {
265                         if(mCurrentObject)
266                                 mCurrentObject->setPosition(queryResult);
267                 }
268     }
269         // If we are dragging the right mouse button.
270         if (mRMouseDown)
271         {
272                 mCamera->yaw( -e->getRelX() * mRotateSpeed );
273                 mCamera->pitch( -e->getRelY() * mRotateSpeed );
274         }
275}
276//-----------------------------------------------------------------------
277bool MouseQueryListener::frameStarted(const FrameEvent &evt)
278{
279        // clamp to terrain
280        Vector3 camPos = mCamera->getPosition();
281        Vector3 queryResult;
282
283    if(mRayQueryExecutor->executeRayQuery(&queryResult,
284                Vector3(camPos.x, 5000.0f, camPos.z), Vector3::NEGATIVE_UNIT_Y))
285        {
286                mCamera->setPosition(mCamera->getPosition().x, queryResult.y + 10,
287                        mCamera->getPosition().z);
288        }
289       
290        return ExampleFrameListener::frameStarted(evt);
291}
292//-----------------------------------------------------------------------
293bool MouseQueryListener::frameEnded(const FrameEvent& evt)
294{
295        if (mShutdownRequested)
296                return false;
297
298    if (timeDelay >= 0)
299        timeDelay -= evt.timeSinceLastFrame;
300
301    KEY_PRESSED(KC_SPACE, 0.3, nextAlgorithm());
302
303        KEY_PRESSED(KC_SUBTRACT, 0, changeThreshold(-10));
304        KEY_PRESSED(KC_ADD, 0, changeThreshold(10));
305        KEY_PRESSED(KC_O, 0.3, toggleUseOptimization());
306        KEY_PRESSED(KC_S, 0.3, toggleShowOctree());
307        KEY_PRESSED(KC_C, 0.3, toggleUseCulling());
308             
309        updateStats();
310
311    return ExampleFrameListener::frameStarted(evt) && ExampleFrameListener::frameEnded(evt);       
312}
313//-----------------------------------------------------------------------
314void MouseQueryListener::changeThreshold(int incr)
315{
316        mVisibilityThreshold += incr;
317        if(mVisibilityThreshold < 0) mVisibilityThreshold = 0;
318       
319        char str[100]; sprintf(str,": %d", mVisibilityThreshold);
320
321        mSceneMgr->setOption("Threshold", &mVisibilityThreshold);
322        mThresholdInfo->setCaption(str);
323}
324//-----------------------------------------------------------------------
325void MouseQueryListener::nextAlgorithm()
326{
327        mCurrentAlgorithm = ++mCurrentAlgorithm %
328                GtpVisibility::VisibilityEnvironment::NUM_CULLING_MANAGERS,
329
330        setAlgorithm(mCurrentAlgorithm);
331}
332//-----------------------------------------------------------------------
333void MouseQueryListener::setAlgorithm(int algorithm)
334{
335        mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
336        mSceneMgr->setOption("Algorithm", &mCurrentAlgorithm);
337}
338//-----------------------------------------------------------------------
339void MouseQueryListener::updateStats()
340{
341        unsigned int opt = 0;
342        char str[100];
343       
344        mSceneMgr->getOption("NumFrustumCulledNodes", &opt); sprintf(str,": %d", opt);
345        mFrustumCulledNodesInfo->setCaption(str);
346       
347        mSceneMgr->getOption("NumQueriesIssued", &opt); sprintf(str,": %d", opt);
348        mQueriesIssuedInfo->setCaption(str);
349       
350        mSceneMgr->getOption("NumQueryCulledNodes", &opt); sprintf(str,": %d", opt);
351        mQueryCulledNodesInfo->setCaption(str);
352       
353        mSceneMgr->getOption("NumTraversedNodes", &opt); sprintf(str,": %d", opt);
354        mTraversedNodesInfo->setCaption(str);
355
356        mSceneMgr->getOption("NumHierarchyNodes", &opt); sprintf(str,": %d", opt);
357        mHierarchyNodesInfo->setCaption(str);
358
359        mSceneMgr->getOption("NumRenderedNodes", &opt); sprintf(str,": %d", opt);
360        mRenderedNodesInfo->setCaption(str);
361
362        sprintf(str,": %d", mTerrainContentGenerator->GetObjectCount());
363        mObjectsInfo->setCaption(str);
364}
365//-----------------------------------------------------------------------
366void MouseQueryListener::toggleUseOptimization()
367{
368        mUseOptimization = !mUseOptimization;
369
370        mSceneMgr->setOption("UseOptimization", &mUseOptimization);
371
372        if(mUseOptimization)
373                mUseOptimizationInfo->setCaption(": true");
374        else
375                mUseOptimizationInfo->setCaption(": false");
376}
377//-----------------------------------------------------------------------
378void MouseQueryListener::toggleShowOctree()
379{
380        mShowOctree = !mShowOctree;
381
382        mSceneMgr->setOption("ShowOctree", &mShowOctree);
383}
384//-----------------------------------------------------------------------
385void MouseQueryListener::toggleUseCulling()
386{
387        mUseCulling = !mUseCulling;
388
389        mSceneMgr->setOption("UseCulling", &mUseCulling);
390}
391//-----------------------------------------------------------------------
392void MouseQueryListener::keyPressed(KeyEvent* e)
393{
394        if(e->getKey() == KC_ESCAPE)
395    {
396                mShutdownRequested = true;
397                e->consume();
398                return;
399        }
400
401        CEGUI::System::getSingleton().injectKeyDown(e->getKey());
402        CEGUI::System::getSingleton().injectChar(e->getKeyChar());
403        e->consume();
404}
405//-----------------------------------------------------------------------
406void MouseQueryListener::keyReleased(KeyEvent* e)
407{
408        CEGUI::System::getSingleton().injectKeyUp(e->getKey());
409        e->consume();
410}
411//-----------------------------------------------------------------------
412void MouseQueryListener::keyClicked(KeyEvent* e)
413{
414        // Do nothing
415        e->consume();
416}
417//-----------------------------------------------------------------------
418INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
419{
420    // Create application object
421    TestCullingTerrainApplication app;
422
423        try
424        {
425        app.go();
426    }
427        catch( Ogre::Exception& e )
428        {
429        MessageBox( NULL, e.getFullDescription().c_str(),
430                        "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
431    }   
432
433    return 0;
434}
Note: See TracBrowser for help on using the repository browser.