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