source: trunk/VUT/obsolete/OcclusionCullingSceneManager/TestCullingTerrain/TestCullingTerrainApplication.cpp @ 55

Revision 55, 15.1 KB checked in by mattausch, 20 years ago (diff)

added TerrainContentGenerator?

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