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 "OgreReferenceAppLayer.h"
|
---|
17 | //#include "OgreRefAppWorld.h"
|
---|
18 | #include "TestCullingTerrainApplication.h"
|
---|
19 |
|
---|
20 | #define WIN32_LEAN_AND_MEAN
|
---|
21 | #include <windows.h>
|
---|
22 |
|
---|
23 | #define VIZ_VIEWPORT_Z_ORDER 10
|
---|
24 |
|
---|
25 | const char* frame_outfile = "frame.out";
|
---|
26 | const char* objects_outfile = "objects.out";
|
---|
27 |
|
---|
28 | /*******************************************************/
|
---|
29 | /* TestCullingTerrainApplication implementation */
|
---|
30 | /*******************************************************/
|
---|
31 |
|
---|
32 | //-----------------------------------------------------------------------
|
---|
33 | TestCullingTerrainApplication::~TestCullingTerrainApplication()
|
---|
34 | {
|
---|
35 | if(mTerrainContentGenerator)
|
---|
36 | delete mTerrainContentGenerator;
|
---|
37 | //if(mRenderTargetListener) delete mRenderTargetListener;
|
---|
38 | }
|
---|
39 | //-----------------------------------------------------------------------
|
---|
40 | void TestCullingTerrainApplication::createCamera()
|
---|
41 | {
|
---|
42 | // create the camera
|
---|
43 | mCamera = mSceneMgr->createCamera("PlayerCam");
|
---|
44 |
|
---|
45 | /** set a nice viewpoint
|
---|
46 | * we use a camera node here and apply all transformations on it instead
|
---|
47 | * of applying all transformations directly to the camera
|
---|
48 | * because then the camera is displayed correctly in the visualization
|
---|
49 | */
|
---|
50 | mCamNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(
|
---|
51 | "CamNode1", Vector3(707, 5000, 528));
|
---|
52 | //mCamera->setPosition(707, 5000, 528);
|
---|
53 | mCamNode->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
|
---|
54 |
|
---|
55 | mCamNode->attachObject(mCamera);
|
---|
56 |
|
---|
57 | //-- create visualization camera
|
---|
58 | mVizCamera = mSceneMgr->createCamera("VizCam");
|
---|
59 | mVizCamera->setPosition(mCamNode->getPosition());
|
---|
60 |
|
---|
61 | mVizCamera->setNearClipDistance(1);
|
---|
62 | mCamera->setNearClipDistance(1);
|
---|
63 |
|
---|
64 | // infinite far plane?
|
---|
65 | if (mRoot->getRenderSystem()->getCapabilities()->hasCapability(RSC_INFINITE_FAR_PLANE))
|
---|
66 | {
|
---|
67 | mVizCamera->setFarClipDistance(0);
|
---|
68 | mCamera->setFarClipDistance(0);
|
---|
69 | }
|
---|
70 | else
|
---|
71 | {
|
---|
72 | mVizCamera->setFarClipDistance(20000);
|
---|
73 | mCamera->setFarClipDistance(20000);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | //-----------------------------------------------------------------------
|
---|
78 | bool TestCullingTerrainApplication::setup()
|
---|
79 | {
|
---|
80 | bool result = ExampleApplication::setup();
|
---|
81 |
|
---|
82 | createRenderTargetListener();
|
---|
83 |
|
---|
84 | return result;
|
---|
85 | }
|
---|
86 | //-----------------------------------------------------------------------
|
---|
87 | void TestCullingTerrainApplication::createRenderTargetListener()
|
---|
88 | {
|
---|
89 | mWindow->addListener(new VisualizationRenderTargetListener(mSceneMgr));
|
---|
90 | }
|
---|
91 | //-----------------------------------------------------------------------
|
---|
92 | void TestCullingTerrainApplication::createScene()
|
---|
93 | {
|
---|
94 | // Set ambient light
|
---|
95 | mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
|
---|
96 |
|
---|
97 | // Create a light
|
---|
98 | Light* l = mSceneMgr->createLight("MainLight");
|
---|
99 | // Accept default settings: point light, white diffuse, just set position
|
---|
100 | // NB I could attach the light to a SceneNode if I wanted it to move automatically with
|
---|
101 | // other objects, but I don't
|
---|
102 | l->setPosition(20,80,50);
|
---|
103 |
|
---|
104 | // --Fog
|
---|
105 | // NB it's VERY important to set this before calling setWorldGeometry
|
---|
106 | // because the vertex program picked will be different
|
---|
107 | ColourValue fadeColour(0.93, 0.86, 0.76);
|
---|
108 | mWindow->getViewport(0)->setBackgroundColour(fadeColour);
|
---|
109 | //mSceneMgr->setFog( FOG_LINEAR, fadeColour, .001, 500, 1000);
|
---|
110 |
|
---|
111 | // Create a skybox
|
---|
112 | mSceneMgr->setSkyBox(true, "Examples/SpaceSkyBox", 5000, false);
|
---|
113 |
|
---|
114 | std::string terrain_cfg("terrain.cfg");
|
---|
115 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
|
---|
116 | terrain_cfg = mResourcePath + terrain_cfg;
|
---|
117 | #endif
|
---|
118 | mSceneMgr->setWorldGeometry(terrain_cfg);
|
---|
119 |
|
---|
120 | //-- CEGUI setup
|
---|
121 | setupGui();
|
---|
122 |
|
---|
123 | //-- terrain content setup
|
---|
124 |
|
---|
125 | // HACK: necessary to call once before the content creation for
|
---|
126 | // terrain initialisation
|
---|
127 | mSceneMgr->_renderScene(mCamera, mWindow->getViewport(0), true);
|
---|
128 |
|
---|
129 | mTerrainContentGenerator = new TerrainContentGenerator(mSceneMgr);
|
---|
130 |
|
---|
131 | // if no objects file generate yourself
|
---|
132 | if (!mTerrainContentGenerator->LoadObjects("objects.out"))
|
---|
133 | {
|
---|
134 | // the objects are generated on the whole terrain
|
---|
135 | // height is restricted to 50, so no objects appear on peaks
|
---|
136 | // => there is much occlusion
|
---|
137 | mTerrainContentGenerator->SetMaxPos(Vector3(3000.0f, 50.0f, 3000.0f));
|
---|
138 | mTerrainContentGenerator->GenerateScene(1500, "robot");
|
---|
139 | // mTerrainContentGenerator->GenerateScene(500, "ninja");
|
---|
140 | }
|
---|
141 |
|
---|
142 | // no limitations needed anymore: the user can set
|
---|
143 | // objects also on peaks of terrain
|
---|
144 | mTerrainContentGenerator->SetMaxPos(Vector3(3000.0f, 5000.0f, 3000.0f));
|
---|
145 | }
|
---|
146 | //-----------------------------------------------------------------------
|
---|
147 | void TestCullingTerrainApplication::setupGui()
|
---|
148 | {
|
---|
149 | mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY,
|
---|
150 | false, 3000, ST_EXTERIOR_CLOSE);
|
---|
151 | mGUISystem = new CEGUI::System(mGUIRenderer);
|
---|
152 |
|
---|
153 | // Mouse
|
---|
154 | CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLook.scheme");
|
---|
155 | CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
|
---|
156 | mGUISystem->setDefaultMouseCursor(
|
---|
157 | (CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
|
---|
158 |
|
---|
159 | CEGUI::MouseCursor::getSingleton().show();
|
---|
160 | }
|
---|
161 | //-----------------------------------------------------------------------
|
---|
162 | void TestCullingTerrainApplication::createFrameListener()
|
---|
163 | {
|
---|
164 | mFrameListener= new MouseQueryListener(mWindow, mCamera, mSceneMgr,
|
---|
165 | mGUIRenderer, mTerrainContentGenerator, mVizCamera, mCamNode);
|
---|
166 | mFrameListener->showDebugOverlay(true);
|
---|
167 | mRoot->addFrameListener(mFrameListener);
|
---|
168 | }
|
---|
169 | //-----------------------------------------------------------------------
|
---|
170 | void TestCullingTerrainApplication::chooseSceneManager()
|
---|
171 | {
|
---|
172 | mSceneMgr = mRoot->getSceneManager(ST_EXTERIOR_CLOSE);
|
---|
173 | }
|
---|
174 | /***********************************************/
|
---|
175 | /* MouseQueryListener implementation */
|
---|
176 | /***********************************************/
|
---|
177 | //-----------------------------------------------------------------------
|
---|
178 | MouseQueryListener::MouseQueryListener(RenderWindow* win, Camera* cam,
|
---|
179 | SceneManager *sceneManager,
|
---|
180 | CEGUI::Renderer *renderer,
|
---|
181 | TerrainContentGenerator *sceneGenerator,
|
---|
182 | Camera *vizCamera,
|
---|
183 | SceneNode *camNode):
|
---|
184 | ExampleFrameListener(win, cam, false, true),
|
---|
185 | mGUIRenderer(renderer),
|
---|
186 | mShutdownRequested(false),
|
---|
187 | mLMouseDown(false),
|
---|
188 | mRMouseDown(false),
|
---|
189 | mSceneMgr(sceneManager),
|
---|
190 | mCurrentObject(NULL),
|
---|
191 | mTerrainContentGenerator(sceneGenerator),
|
---|
192 | mVisibilityThreshold(0),
|
---|
193 | mCurrentAlgorithm(GtpVisibility::VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING),
|
---|
194 | mShowOctree(false),
|
---|
195 | mUseVisibilityCulling(true),
|
---|
196 | mUseOptimization(true),
|
---|
197 | mVizCamera(vizCamera),
|
---|
198 | mShowVisualization(false),
|
---|
199 | mRenderNodesForViz(false),
|
---|
200 | mVizCameraHeight(Real(2500.0)),
|
---|
201 | mCamNode(camNode),
|
---|
202 | mCullCamera(false),
|
---|
203 | mAppState(WALKTHROUGH),
|
---|
204 | mCurrentFrame(0),
|
---|
205 | mRecord(false),
|
---|
206 | mTimeElapsed(0)
|
---|
207 | {
|
---|
208 | // Reduce move speed
|
---|
209 | mMoveSpeed = 50;
|
---|
210 | mRotateSpeed *= 2;
|
---|
211 |
|
---|
212 | // Register this so that we get mouse events.
|
---|
213 | mEventProcessor->addMouseListener(this);
|
---|
214 | mEventProcessor->addMouseMotionListener(this);
|
---|
215 | mEventProcessor->addKeyListener(this);
|
---|
216 |
|
---|
217 | mRayQueryExecutor = new RayQueryExecutor(mSceneMgr);
|
---|
218 |
|
---|
219 | // show overlay
|
---|
220 | Overlay* pOver = OverlayManager::getSingleton().getByName("Example/VisibilityDemoOverlay");
|
---|
221 |
|
---|
222 | mAlgorithmInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/AlgorithmInfo");
|
---|
223 | mThresholdInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/ThresholdInfo");
|
---|
224 |
|
---|
225 | mFrustumCulledNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/FrustumCulledNodesInfo");
|
---|
226 | mQueryCulledNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/QueryCulledNodesInfo");
|
---|
227 | mTraversedNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/TraversedNodesInfo");
|
---|
228 | mHierarchyNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/HierarchyNodesInfo");
|
---|
229 | mRenderedNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/RenderedNodesInfo");
|
---|
230 | mObjectsInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/ObjectsInfo");
|
---|
231 | mUseOptimizationInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/UseOptimizationInfo");
|
---|
232 | mQueriesIssuedInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/QueriesIssuedInfo");
|
---|
233 |
|
---|
234 | mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
|
---|
235 | mThresholdInfo->setCaption(": 0");
|
---|
236 | mFrustumCulledNodesInfo->setCaption(": 0");
|
---|
237 | mQueryCulledNodesInfo->setCaption(": 0");
|
---|
238 | mTraversedNodesInfo->setCaption(": 0");
|
---|
239 | mHierarchyNodesInfo->setCaption(": 0");
|
---|
240 | mRenderedNodesInfo->setCaption(": 0");
|
---|
241 | mObjectsInfo->setCaption(": 0");
|
---|
242 | mUseOptimizationInfo->setCaption(": true");
|
---|
243 | mQueriesIssuedInfo->setCaption(": 0");
|
---|
244 |
|
---|
245 | setAlgorithm(mCurrentAlgorithm);
|
---|
246 |
|
---|
247 | mSceneMgr->setOption("UseOptimization", &mUseOptimization);
|
---|
248 | mSceneMgr->setOption("UseVisibilityCulling", &mUseVisibilityCulling);
|
---|
249 | mSceneMgr->setOption("ShowOctree", &mShowOctree);
|
---|
250 | mSceneMgr->setOption("CullCamera", &mCullCamera);
|
---|
251 | mSceneMgr->setOption("ShowVisualization", &mShowVisualization);
|
---|
252 | mSceneMgr->setOption("RenderNodesForViz", &mRenderNodesForViz);
|
---|
253 |
|
---|
254 | pOver->show();
|
---|
255 | }
|
---|
256 | //-----------------------------------------------------------------------
|
---|
257 | MouseQueryListener::~MouseQueryListener()
|
---|
258 | {
|
---|
259 | delete mRayQueryExecutor;
|
---|
260 | }
|
---|
261 | //-----------------------------------------------------------------------
|
---|
262 | void MouseQueryListener::mouseMoved(MouseEvent *e)
|
---|
263 | {
|
---|
264 | // Update CEGUI with the mouse motion
|
---|
265 | CEGUI::System::getSingleton().injectMouseMove(e->getRelX() *
|
---|
266 | mGUIRenderer->getWidth(), e->getRelY() * mGUIRenderer->getHeight());
|
---|
267 | }
|
---|
268 | //-----------------------------------------------------------------------
|
---|
269 | void MouseQueryListener::mousePressed(MouseEvent* e)
|
---|
270 | {
|
---|
271 | // Left mouse button down
|
---|
272 | if (e->getButtonID() & InputEvent::BUTTON0_MASK)
|
---|
273 | {
|
---|
274 | CEGUI::MouseCursor::getSingleton().hide();
|
---|
275 |
|
---|
276 | // Setup the ray scene query
|
---|
277 | Ray mouseRay = mCamera->getCameraToViewportRay(e->getX(), e->getY());
|
---|
278 |
|
---|
279 | Vector3 queryResult;
|
---|
280 |
|
---|
281 | // Get results, create a node/entity on the position
|
---|
282 | mCurrentObject = mTerrainContentGenerator->GenerateSceneObject(
|
---|
283 | mouseRay.getOrigin(), Vector3(0,0,0), "robot.mesh");
|
---|
284 |
|
---|
285 | mLMouseDown = true;
|
---|
286 | }
|
---|
287 | // Right mouse button down
|
---|
288 | else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
|
---|
289 | {
|
---|
290 | CEGUI::MouseCursor::getSingleton().hide();
|
---|
291 | mRMouseDown = true;
|
---|
292 | }
|
---|
293 | } // mousePressed
|
---|
294 | //-----------------------------------------------------------------------
|
---|
295 | void MouseQueryListener::mouseReleased(MouseEvent* e)
|
---|
296 | {
|
---|
297 | // Left mouse button up
|
---|
298 | if (e->getButtonID() & InputEvent::BUTTON0_MASK)
|
---|
299 | {
|
---|
300 | CEGUI::MouseCursor::getSingleton().show();
|
---|
301 | mLMouseDown = false;
|
---|
302 | }
|
---|
303 | // Right mouse button up
|
---|
304 | else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
|
---|
305 | {
|
---|
306 | CEGUI::MouseCursor::getSingleton().show();
|
---|
307 | mRMouseDown = false;
|
---|
308 | }
|
---|
309 | }
|
---|
310 | //-----------------------------------------------------------------------
|
---|
311 | void MouseQueryListener::mouseDragged(MouseEvent *e)
|
---|
312 | {
|
---|
313 | // If we are dragging the left mouse button.
|
---|
314 | if (mLMouseDown)
|
---|
315 | {
|
---|
316 | Vector3 queryResult;
|
---|
317 | Ray mouseRay = mCamera->getCameraToViewportRay(e->getX(), e->getY());
|
---|
318 |
|
---|
319 | if (mRayQueryExecutor->executeRayQuery(&queryResult, mouseRay))
|
---|
320 | {
|
---|
321 | if (mCurrentObject)
|
---|
322 | {
|
---|
323 | mCurrentObject->setPosition(queryResult);
|
---|
324 | }
|
---|
325 | }
|
---|
326 | }
|
---|
327 | // If we are dragging the right mouse button.
|
---|
328 | if (mRMouseDown)
|
---|
329 | {
|
---|
330 | //mCamera->yaw(-e->getRelX() * mRotateSpeed);
|
---|
331 | //mCamera->pitch(-e->getRelY() * mRotateSpeed);
|
---|
332 | mCamNode->yaw(-e->getRelX() * mRotateSpeed);
|
---|
333 | mCamNode->pitch(-e->getRelY() * mRotateSpeed);
|
---|
334 | }
|
---|
335 | }
|
---|
336 | //-----------------------------------------------------------------------
|
---|
337 | bool MouseQueryListener::frameStarted(const FrameEvent &evt)
|
---|
338 | {
|
---|
339 | switch (mAppState)
|
---|
340 | {
|
---|
341 | case REPLAY:
|
---|
342 | setCurrentFrameInfo(evt.timeSinceLastFrame);
|
---|
343 | break;
|
---|
344 | case WALKTHROUGH:
|
---|
345 | if (mRecord)
|
---|
346 | {
|
---|
347 | addFrameInfo(mCamNode, evt.timeSinceLastFrame);
|
---|
348 | // print recording message
|
---|
349 | mWindow->setDebugText("Recording frame " +
|
---|
350 | StringConverter::toString(mFrameInfo.size() - 1));
|
---|
351 | }
|
---|
352 |
|
---|
353 | Clamp2Terrain();
|
---|
354 | break;
|
---|
355 | default:
|
---|
356 | break;
|
---|
357 | };
|
---|
358 |
|
---|
359 | if (mShowVisualization)
|
---|
360 | {
|
---|
361 | // important for visualization => draw octree bounding boxes
|
---|
362 | mSceneMgr->setOption("ShowOctree", &mShowVisualization);
|
---|
363 | // also render geometry?
|
---|
364 | mSceneMgr->setOption("RenderNodesForViz", &mRenderNodesForViz);
|
---|
365 |
|
---|
366 | // -- setup visualization camera
|
---|
367 | mVizCamera->setPosition(0, 0, 0);
|
---|
368 | mVizCamera->setOrientation(Quaternion::IDENTITY);
|
---|
369 |
|
---|
370 | Vector3 camPos = mCamNode->getPosition();
|
---|
371 | mVizCamera->setPosition(camPos.x, mVizCameraHeight, camPos.z);
|
---|
372 |
|
---|
373 | // point down -Z axis
|
---|
374 | mVizCamera->pitch(Radian(Degree(270.0)));
|
---|
375 |
|
---|
376 | // rotation arounnd X axis
|
---|
377 | mVizCamera->yaw(Math::ATan2(-mCamera->getDerivedDirection().x,
|
---|
378 | -mCamera->getDerivedDirection().z));
|
---|
379 |
|
---|
380 | // move by a constant so view plane is on bottom of viewport
|
---|
381 | mVizCamera->moveRelative(Vector3(0, 800, 0));
|
---|
382 | }
|
---|
383 |
|
---|
384 | return ExampleFrameListener::frameStarted(evt);
|
---|
385 | }
|
---|
386 | //-----------------------------------------------------------------------
|
---|
387 | void MouseQueryListener::Clamp2Terrain()
|
---|
388 | {
|
---|
389 | // clamp to terrain
|
---|
390 | Vector3 camPos = mCamNode->getPosition();
|
---|
391 | Vector3 queryResult;
|
---|
392 |
|
---|
393 | if (mRayQueryExecutor->executeRayQuery(&queryResult,
|
---|
394 | Vector3(camPos.x, 5000.0f, camPos.z), Vector3::NEGATIVE_UNIT_Y))
|
---|
395 | {
|
---|
396 | mCamNode->setPosition(camPos.x, queryResult.y + 10, camPos.z);
|
---|
397 | }
|
---|
398 | }
|
---|
399 | //-----------------------------------------------------------------------
|
---|
400 | bool MouseQueryListener::frameEnded(const FrameEvent& evt)
|
---|
401 | {
|
---|
402 | if (mShutdownRequested)
|
---|
403 | return false;
|
---|
404 |
|
---|
405 | if (timeDelay >= 0)
|
---|
406 | timeDelay -= evt.timeSinceLastFrame;
|
---|
407 |
|
---|
408 | KEY_PRESSED(KC_SPACE, 0.3, nextAlgorithm());
|
---|
409 |
|
---|
410 | KEY_PRESSED(KC_O, 0.3, toggleUseOptimization());
|
---|
411 | KEY_PRESSED(KC_T, 0.3, toggleShowOctree());
|
---|
412 | KEY_PRESSED(KC_C, 0.3, toggleUseVisibilityCulling());
|
---|
413 | KEY_PRESSED(KC_1, 0.3, toggleShowViz());
|
---|
414 | KEY_PRESSED(KC_2, 0.3, toggleRenderNodesForViz());
|
---|
415 |
|
---|
416 | KEY_PRESSED(KC_SUBTRACT, 0, changeThreshold(-10));
|
---|
417 | KEY_PRESSED(KC_ADD, 0, changeThreshold(10));
|
---|
418 |
|
---|
419 | KEY_PRESSED(KC_3, 0, zoomVizCamera(50));
|
---|
420 | KEY_PRESSED(KC_4, 0, zoomVizCamera(-50));
|
---|
421 |
|
---|
422 | KEY_PRESSED(KC_F1, 0.3, nextAppState());
|
---|
423 | KEY_PRESSED(KC_F2, 0.3, toggleRecord());
|
---|
424 | KEY_PRESSED(KC_F3, 0.3, mTerrainContentGenerator->WriteObjects(objects_outfile));
|
---|
425 | //KEY_PRESSED(KC_F3, 0.3, writeFrames());
|
---|
426 | //KEY_PRESSED(KC_F4, 0.3, loadFrames());
|
---|
427 |
|
---|
428 | updateStats();
|
---|
429 |
|
---|
430 | //return ExampleFrameListener::frameStarted(evt) && ExampleFrameListener::frameEnded(evt);
|
---|
431 | return ExampleFrameListener::frameEnded(evt);
|
---|
432 | }
|
---|
433 | //-----------------------------------------------------------------------
|
---|
434 | void MouseQueryListener::moveCamera()
|
---|
435 | {
|
---|
436 | mCamNode->yaw(mRotX);
|
---|
437 | mCamNode->pitch(mRotY);
|
---|
438 | mCamNode->translate(mCamNode->getLocalAxes(), mTranslateVector);
|
---|
439 | }
|
---|
440 | //-----------------------------------------------------------------------
|
---|
441 | void MouseQueryListener::writeFrames()
|
---|
442 | {
|
---|
443 | std::ofstream ofstr(frame_outfile);
|
---|
444 |
|
---|
445 | std::vector<frame_info>::const_iterator it, it_end;
|
---|
446 |
|
---|
447 | it_end = mFrameInfo.end();
|
---|
448 | for(it = mFrameInfo.begin(); it < it_end; ++it)
|
---|
449 | {
|
---|
450 | ofstr << StringConverter::toString((*it).position) << " "
|
---|
451 | << StringConverter::toString((*it).orientation) << " "
|
---|
452 | << StringConverter::toString((*it).timeElapsed) << "\n";
|
---|
453 | }
|
---|
454 | ofstr.close();
|
---|
455 | }
|
---|
456 | //-----------------------------------------------------------------------
|
---|
457 | void MouseQueryListener::loadFrames()
|
---|
458 | {
|
---|
459 | std::ifstream ifstr(frame_outfile);
|
---|
460 | char line[256];
|
---|
461 | frame_info info;
|
---|
462 |
|
---|
463 | // reset current values
|
---|
464 | mFrameInfo.clear();
|
---|
465 | mCurrentFrame = 0;
|
---|
466 |
|
---|
467 | while (!ifstr.eof())
|
---|
468 | {
|
---|
469 | ifstr.getline(line, 256);
|
---|
470 | sscanf(line, "%f %f %f %f %f %f %f %f", &info.position.x, &info.position.y, &info.position.z,
|
---|
471 | &info.orientation.w, &info.orientation.x, &info.orientation.y, &info.orientation.z,
|
---|
472 | &info.timeElapsed);
|
---|
473 |
|
---|
474 | mFrameInfo.push_back(info);
|
---|
475 |
|
---|
476 | /*
|
---|
477 | std::stringstream d;
|
---|
478 | d << StringConverter::toString(info.position) << " " << StringConverter::toString(info.orientation);
|
---|
479 | LogManager::getSingleton().logMessage(d.str());
|
---|
480 | */
|
---|
481 | }
|
---|
482 | ifstr.close();
|
---|
483 | }
|
---|
484 | //-----------------------------------------------------------------------
|
---|
485 | void MouseQueryListener::setAppState(int state)
|
---|
486 | {
|
---|
487 | mAppState = state;
|
---|
488 | }
|
---|
489 | //-----------------------------------------------------------------------
|
---|
490 | void MouseQueryListener::nextAppState()
|
---|
491 | {
|
---|
492 | mCurrentFrame = 0;
|
---|
493 |
|
---|
494 | // if last state was replay state
|
---|
495 | if (mAppState == REPLAY)
|
---|
496 | {
|
---|
497 | // reset debug text and write frame info to file
|
---|
498 | mWindow->setDebugText("");
|
---|
499 | writeFrames();
|
---|
500 |
|
---|
501 | //-- write out stats
|
---|
502 | std::stringstream d;
|
---|
503 | d << "Algorithm: " << mCurrentAlgorithmCaptions[mCurrentAlgorithm] << "\n"
|
---|
504 | << "avg. FPS: " << mWindow->getAverageFPS() << "\n"
|
---|
505 | << "best FPS: " << mWindow->getBestFPS() << "\n"
|
---|
506 | << "worst FPS: " << mWindow->getWorstFPS() << "\n"
|
---|
507 | << "best frame time: " << mWindow->getBestFrameTime() << "\n"
|
---|
508 | << "worst frame time: " << mWindow->getWorstFrameTime();
|
---|
509 |
|
---|
510 | LogManager::getSingleton().logMessage(d.str());
|
---|
511 | }
|
---|
512 |
|
---|
513 | //-- set the next státe
|
---|
514 | mAppState = (mAppState + 1) % STATE_NUM;
|
---|
515 |
|
---|
516 | // replay recorded walkthrough
|
---|
517 | if (mAppState == REPLAY)
|
---|
518 | {
|
---|
519 | // no recording during replay
|
---|
520 | mRecord = false;
|
---|
521 |
|
---|
522 | // load recorded walkthrough
|
---|
523 | if (mFrameInfo.size() == 0)
|
---|
524 | {
|
---|
525 | loadFrames();
|
---|
526 | }
|
---|
527 |
|
---|
528 | // if there are no recorded frames => set next state
|
---|
529 | if (mFrameInfo.size() == 0)
|
---|
530 | {
|
---|
531 | nextAppState();
|
---|
532 | }
|
---|
533 | else
|
---|
534 | {
|
---|
535 | mWindow->setDebugText("Replay");
|
---|
536 |
|
---|
537 | // reset, because we measure fps stats during walkthrough
|
---|
538 | mWindow->resetStatistics();
|
---|
539 |
|
---|
540 | //-- initialise frame data
|
---|
541 | mTimeElapsed = 0;
|
---|
542 |
|
---|
543 | mCamNode->setPosition(mFrameInfo[0].position);
|
---|
544 | mCamNode->setOrientation(mFrameInfo[0].orientation);
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 | }
|
---|
549 | //-----------------------------------------------------------------------
|
---|
550 | void MouseQueryListener::toggleRecord()
|
---|
551 | {
|
---|
552 | mRecord = !mRecord;
|
---|
553 |
|
---|
554 | // clear previous camera path
|
---|
555 | if (mRecord)
|
---|
556 | mFrameInfo.clear();
|
---|
557 | else
|
---|
558 | mWindow->setDebugText("");
|
---|
559 | }
|
---|
560 | //-----------------------------------------------------------------------
|
---|
561 | void MouseQueryListener::changeThreshold(int incr)
|
---|
562 | {
|
---|
563 | mVisibilityThreshold += incr;
|
---|
564 | if(mVisibilityThreshold < 0) mVisibilityThreshold = 0;
|
---|
565 |
|
---|
566 | char str[100]; sprintf(str,": %d", mVisibilityThreshold);
|
---|
567 |
|
---|
568 | mSceneMgr->setOption("Threshold", &mVisibilityThreshold);
|
---|
569 | mThresholdInfo->setCaption(str);
|
---|
570 | }
|
---|
571 | //-----------------------------------------------------------------------
|
---|
572 | bool MouseQueryListener::processUnbufferedKeyInput(const FrameEvent& evt)
|
---|
573 | {
|
---|
574 | if (mInputDevice->isKeyDown(KC_RIGHT))
|
---|
575 | {
|
---|
576 | mCamNode->yaw(-mRotScale);
|
---|
577 | return true;
|
---|
578 | }
|
---|
579 | if (mInputDevice->isKeyDown(KC_LEFT))
|
---|
580 | {
|
---|
581 | mCamNode->yaw(mRotScale);
|
---|
582 | return true;
|
---|
583 | }
|
---|
584 |
|
---|
585 | return ExampleFrameListener::processUnbufferedKeyInput(evt);
|
---|
586 | }
|
---|
587 | //-----------------------------------------------------------------------
|
---|
588 | void MouseQueryListener::zoomVizCamera(int zoom)
|
---|
589 | {
|
---|
590 | mVizCameraHeight += zoom;
|
---|
591 | if(mVizCameraHeight < 0) mVizCameraHeight = 0;
|
---|
592 | }
|
---|
593 | //-----------------------------------------------------------------------
|
---|
594 | void MouseQueryListener::nextAlgorithm()
|
---|
595 | {
|
---|
596 | mCurrentAlgorithm = (mCurrentAlgorithm + 1) %
|
---|
597 | GtpVisibility::VisibilityEnvironment::NUM_CULLING_MANAGERS,
|
---|
598 |
|
---|
599 | setAlgorithm(mCurrentAlgorithm);
|
---|
600 | }
|
---|
601 | //-----------------------------------------------------------------------
|
---|
602 | void MouseQueryListener::setAlgorithm(int algorithm)
|
---|
603 | {
|
---|
604 | mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
|
---|
605 | mSceneMgr->setOption("Algorithm", &mCurrentAlgorithm);
|
---|
606 | }
|
---|
607 | //-----------------------------------------------------------------------
|
---|
608 | void MouseQueryListener::updateStats()
|
---|
609 | {
|
---|
610 | unsigned int opt = 0;
|
---|
611 | char str[100];
|
---|
612 |
|
---|
613 | mSceneMgr->getOption("NumFrustumCulledNodes", &opt); sprintf(str,": %d", opt);
|
---|
614 | mFrustumCulledNodesInfo->setCaption(str);
|
---|
615 |
|
---|
616 | mSceneMgr->getOption("NumQueriesIssued", &opt); sprintf(str,": %d", opt);
|
---|
617 | mQueriesIssuedInfo->setCaption(str);
|
---|
618 |
|
---|
619 | mSceneMgr->getOption("NumQueryCulledNodes", &opt); sprintf(str,": %d", opt);
|
---|
620 | mQueryCulledNodesInfo->setCaption(str);
|
---|
621 |
|
---|
622 | mSceneMgr->getOption("NumTraversedNodes", &opt); sprintf(str,": %d", opt);
|
---|
623 | mTraversedNodesInfo->setCaption(str);
|
---|
624 |
|
---|
625 | mSceneMgr->getOption("NumHierarchyNodes", &opt); sprintf(str,": %d", opt);
|
---|
626 | mHierarchyNodesInfo->setCaption(str);
|
---|
627 |
|
---|
628 | mSceneMgr->getOption("NumRenderedNodes", &opt); sprintf(str,": %d", opt);
|
---|
629 | mRenderedNodesInfo->setCaption(str);
|
---|
630 |
|
---|
631 | sprintf(str,": %d", mTerrainContentGenerator->GetObjectCount());
|
---|
632 | mObjectsInfo->setCaption(str);
|
---|
633 | }
|
---|
634 | //-----------------------------------------------------------------------
|
---|
635 | void MouseQueryListener::toggleUseOptimization()
|
---|
636 | {
|
---|
637 | mUseOptimization = !mUseOptimization;
|
---|
638 |
|
---|
639 | mSceneMgr->setOption("UseOptimization", &mUseOptimization);
|
---|
640 |
|
---|
641 | if (mUseOptimization)
|
---|
642 | mUseOptimizationInfo->setCaption(": true");
|
---|
643 | else
|
---|
644 | mUseOptimizationInfo->setCaption(": false");
|
---|
645 | }
|
---|
646 | //-----------------------------------------------------------------------
|
---|
647 | void MouseQueryListener::toggleShowOctree()
|
---|
648 | {
|
---|
649 | mShowOctree = !mShowOctree;
|
---|
650 |
|
---|
651 | mSceneMgr->setOption("ShowOctree", &mShowOctree);
|
---|
652 | }
|
---|
653 | //-----------------------------------------------------------------------
|
---|
654 | void MouseQueryListener::toggleUseVisibilityCulling()
|
---|
655 | {
|
---|
656 | mUseVisibilityCulling = !mUseVisibilityCulling;
|
---|
657 |
|
---|
658 | mSceneMgr->setOption("UseVisibilityCulling", &mUseVisibilityCulling);
|
---|
659 | }
|
---|
660 | //-----------------------------------------------------------------------
|
---|
661 | void MouseQueryListener::toggleShowViz()
|
---|
662 | {
|
---|
663 | mShowVisualization = !mShowVisualization;
|
---|
664 |
|
---|
665 | // create viewport with priority VIZ_VIEWPORT_Z_ORDER:
|
---|
666 | // will be rendered over standard viewport
|
---|
667 | if (mShowVisualization)
|
---|
668 | {
|
---|
669 | Viewport *vizvp = mWindow->addViewport(mVizCamera,
|
---|
670 | VIZ_VIEWPORT_Z_ORDER, 0.6, 0.6, 0.4, 0.4);
|
---|
671 |
|
---|
672 | vizvp->setBackgroundColour(ColourValue(0.0, 0.3, 0.2, 1));
|
---|
673 |
|
---|
674 | vizvp->setOverlaysEnabled(false);
|
---|
675 | // Alter the camera aspect ratio to match the viewport
|
---|
676 | mVizCamera->setAspectRatio(Real(vizvp->getActualWidth()) /
|
---|
677 | Real(vizvp->getActualHeight()));
|
---|
678 | //vizvp->setClearEveryFrame(false);
|
---|
679 |
|
---|
680 | // Create a skyplane (for visualization background)
|
---|
681 | /*
|
---|
682 | Plane plane;
|
---|
683 | plane.d = -1000;
|
---|
684 | plane.normal = Vector3::UNIT_Y;
|
---|
685 | mSceneMgr->setSkyPlane(true, plane, "Examples/TransparentTest", 4000, 75, false);
|
---|
686 | */
|
---|
687 |
|
---|
688 | }
|
---|
689 | else
|
---|
690 | {
|
---|
691 | mWindow->removeViewport(VIZ_VIEWPORT_Z_ORDER);
|
---|
692 | // if octree was enabled for visualization purpose
|
---|
693 | mSceneMgr->setOption("ShowOctree", &mShowOctree);
|
---|
694 | }
|
---|
695 | }
|
---|
696 | //-----------------------------------------------------------------------
|
---|
697 | void MouseQueryListener::toggleRenderNodesForViz()
|
---|
698 | {
|
---|
699 | mRenderNodesForViz = !mRenderNodesForViz;
|
---|
700 |
|
---|
701 | mSceneMgr->setOption("RenderNodesForViz", &mRenderNodesForViz);
|
---|
702 | }
|
---|
703 | //-----------------------------------------------------------------------
|
---|
704 | void MouseQueryListener::keyPressed(KeyEvent* e)
|
---|
705 | {
|
---|
706 | if(e->getKey() == KC_ESCAPE)
|
---|
707 | {
|
---|
708 | mShutdownRequested = true;
|
---|
709 | e->consume();
|
---|
710 | return;
|
---|
711 | }
|
---|
712 |
|
---|
713 | CEGUI::System::getSingleton().injectKeyDown(e->getKey());
|
---|
714 | CEGUI::System::getSingleton().injectChar(e->getKeyChar());
|
---|
715 | e->consume();
|
---|
716 | }
|
---|
717 | //-----------------------------------------------------------------------
|
---|
718 | void MouseQueryListener::keyReleased(KeyEvent* e)
|
---|
719 | {
|
---|
720 | CEGUI::System::getSingleton().injectKeyUp(e->getKey());
|
---|
721 | e->consume();
|
---|
722 | }
|
---|
723 | //-----------------------------------------------------------------------
|
---|
724 | void MouseQueryListener::keyClicked(KeyEvent* e)
|
---|
725 | {
|
---|
726 | // Do nothing
|
---|
727 | e->consume();
|
---|
728 | }
|
---|
729 | //-----------------------------------------------------------------------
|
---|
730 | void MouseQueryListener::addFrameInfo(SceneNode *camNode, Real timeElapsed)
|
---|
731 | {
|
---|
732 | frame_info info;
|
---|
733 | info.orientation = mCamNode->getOrientation();
|
---|
734 | info.position = mCamNode->getPosition();
|
---|
735 | info.timeElapsed = timeElapsed;
|
---|
736 |
|
---|
737 | mFrameInfo.push_back(info);
|
---|
738 | }
|
---|
739 | //-----------------------------------------------------------------------
|
---|
740 | void MouseQueryListener::setCurrentFrameInfo(Real timeElapsed)
|
---|
741 | {
|
---|
742 | //-- find right frame relative to elapsed frame time
|
---|
743 | mTimeElapsed -= timeElapsed;
|
---|
744 |
|
---|
745 | while ((mTimeElapsed <= 0) && (mCurrentFrame < (int)mFrameInfo.size() - 1))
|
---|
746 | {
|
---|
747 | mTimeElapsed += mFrameInfo[mCurrentFrame ++].timeElapsed;
|
---|
748 | /*std::stringstream d;
|
---|
749 | d << StringConverter::toString(mTimeElapsed) << " "
|
---|
750 | << StringConverter::toString(timeElapsed) << " "
|
---|
751 | << StringConverter::toString(mFrameInfo[mCurrentFrame - 1].timeElapsed)
|
---|
752 | << " " << mCurrentFrame;
|
---|
753 | LogManager::getSingleton().logMessage(d.str());*/
|
---|
754 | }
|
---|
755 |
|
---|
756 | frame_info new_frame = mFrameInfo[mCurrentFrame];
|
---|
757 | frame_info old_frame = mFrameInfo[mCurrentFrame - 1];
|
---|
758 |
|
---|
759 | //-- interpolate frames
|
---|
760 | Real factor = 1;
|
---|
761 |
|
---|
762 | if (old_frame.timeElapsed > 0)
|
---|
763 | factor = mTimeElapsed / old_frame.timeElapsed;
|
---|
764 |
|
---|
765 | Vector3 camPos = old_frame.position + factor * (new_frame.position - old_frame.position);
|
---|
766 | Quaternion camOrienation = Quaternion::Slerp(factor, old_frame.orientation,
|
---|
767 | new_frame.orientation, true);
|
---|
768 |
|
---|
769 | mCamNode->setPosition(camPos);
|
---|
770 | mCamNode->setOrientation(camOrienation);
|
---|
771 | /*
|
---|
772 | std::stringstream d;
|
---|
773 | d << "old pos: " << StringConverter::toString(old_frame.position) << "\n"
|
---|
774 | << "interp pos: " << StringConverter::toString(camPos) << "\n"
|
---|
775 | << " new pos: " << StringConverter::toString(new_frame.position);
|
---|
776 | LogManager::getSingleton().logMessage(d.str());*/
|
---|
777 |
|
---|
778 | // stop replay after a full walkthrough
|
---|
779 | if (mCurrentFrame == (int)mFrameInfo.size() - 1)
|
---|
780 | {
|
---|
781 | nextAppState();
|
---|
782 | }
|
---|
783 | }
|
---|
784 |
|
---|
785 | /**************************************************************/
|
---|
786 | /* VisualizationRenderTargetListener implementation */
|
---|
787 | /**************************************************************/
|
---|
788 | //-----------------------------------------------------------------------
|
---|
789 | VisualizationRenderTargetListener::VisualizationRenderTargetListener(SceneManager *sceneMgr)
|
---|
790 | :RenderTargetListener(), mSceneMgr(sceneMgr)
|
---|
791 | {
|
---|
792 | }
|
---|
793 | //-----------------------------------------------------------------------
|
---|
794 | void VisualizationRenderTargetListener::preViewportUpdate(const RenderTargetViewportEvent &evt)
|
---|
795 | {
|
---|
796 | const bool showViz = evt.source->getZOrder() == VIZ_VIEWPORT_Z_ORDER; // visualization viewport
|
---|
797 | const bool nShowViz = !showViz;
|
---|
798 |
|
---|
799 | mSceneMgr->setOption("ShowVisualization", &showViz);
|
---|
800 | mSceneMgr->setOption("SkyBoxEnabled", &nShowViz);
|
---|
801 | //mSceneMgr->setOption("SkyPlaneEnabled", &showViz);
|
---|
802 |
|
---|
803 | RenderTargetListener::preViewportUpdate(evt);
|
---|
804 | }
|
---|
805 | //-----------------------------------------------------------------------
|
---|
806 | void VisualizationRenderTargetListener::postRenderTargetUpdate(const RenderTargetEvent &evt)
|
---|
807 | {
|
---|
808 | RenderTargetListener::postRenderTargetUpdate(evt);
|
---|
809 | }
|
---|
810 | //-----------------------------------------------------------------------
|
---|
811 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
|
---|
812 | {
|
---|
813 | // Create application object
|
---|
814 | TestCullingTerrainApplication app;
|
---|
815 |
|
---|
816 | try
|
---|
817 | {
|
---|
818 | app.go();
|
---|
819 | }
|
---|
820 | catch( Ogre::Exception& e )
|
---|
821 | {
|
---|
822 | MessageBox( NULL, e.getFullDescription().c_str(),
|
---|
823 | "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
|
---|
824 | }
|
---|
825 |
|
---|
826 | return 0;
|
---|
827 | } |
---|