1 | #include <OgreNoMemoryMacros.h>
|
---|
2 | #include <CEGUI/CEGUI.h>
|
---|
3 | #include <../CEGUIRenderer/include/OgreCEGUIRenderer.h>
|
---|
4 | #include <../CEGUIRenderer/include/OgreCEGUIResourceProvider.h>
|
---|
5 | #include <../CEGUIRenderer/include/OgreCEGUITexture.h>
|
---|
6 | #include <OgreMemoryMacros.h>
|
---|
7 | #include <OgreIteratorWrappers.h>
|
---|
8 | #include <Ogre.h>
|
---|
9 | #include <direct.h> // for getcwd
|
---|
10 | //#include "OgreReferenceAppLayer.h"
|
---|
11 | //#include "OgreRefAppWorld.h"
|
---|
12 | #include "TestCullingTerrainApplication.h"
|
---|
13 | #include "TerrainFrameListener.h"
|
---|
14 | //#include "OgreVisibilityOctreeSceneManager.h"
|
---|
15 |
|
---|
16 | #include "IVReader.h"
|
---|
17 |
|
---|
18 | #define WIN32_LEAN_AND_MEAN
|
---|
19 | #include <windows.h>
|
---|
20 |
|
---|
21 | const static bool USE_STATIC_GEOMETRY = false;
|
---|
22 | bool TestCullingTerrainApplication::msShowHillyTerrain = false;
|
---|
23 | //bool TestCullingTerrainApplication::msShowHillyTerrain = true;
|
---|
24 |
|
---|
25 | /******************************************************/
|
---|
26 | /* EntityState implementation */
|
---|
27 | /******************************************************/
|
---|
28 |
|
---|
29 |
|
---|
30 | Vector3 EntityState::msMinPos = Vector3::ZERO;
|
---|
31 | Vector3 EntityState::msMaxPos = Vector3::ZERO;
|
---|
32 |
|
---|
33 | EntityState::EntityState(Entity *ent, State entityState, Real speed):
|
---|
34 | mEntity(ent), mState(entityState), mAnimationSpeed(speed)
|
---|
35 | {
|
---|
36 | switch(entityState)
|
---|
37 | {
|
---|
38 | case MOVING:
|
---|
39 | mAnimationState = mEntity->getAnimationState("Walk");
|
---|
40 | break;
|
---|
41 | case WAITING:
|
---|
42 | mAnimationState = mEntity->getAnimationState("Idle");
|
---|
43 | break;
|
---|
44 | case STOP:
|
---|
45 | mAnimationState = NULL;
|
---|
46 | break;
|
---|
47 | default:
|
---|
48 | break;
|
---|
49 | }
|
---|
50 | // enable animation state
|
---|
51 | if (mAnimationState)
|
---|
52 | {
|
---|
53 | mAnimationState->setLoop(true);
|
---|
54 | mAnimationState->setEnabled(true);
|
---|
55 | }
|
---|
56 |
|
---|
57 | mTimeElapsed = Math::RangeRandom(1, 5);
|
---|
58 | }
|
---|
59 | //-----------------------------------------------------------------------
|
---|
60 | EntityState::~EntityState()
|
---|
61 | {
|
---|
62 | mAnimationState = NULL;
|
---|
63 | mEntity = NULL;
|
---|
64 | }
|
---|
65 | //-----------------------------------------------------------------------
|
---|
66 | Entity *EntityState::GetEntity()
|
---|
67 | {
|
---|
68 | return mEntity;
|
---|
69 | }
|
---|
70 | //-----------------------------------------------------------------------
|
---|
71 | EntityState::State EntityState::GetState()
|
---|
72 | {
|
---|
73 | return mState;
|
---|
74 | }
|
---|
75 | //-----------------------------------------------------------------------
|
---|
76 | void EntityState::update(Real timeSinceLastFrame)
|
---|
77 | {
|
---|
78 | mTimeElapsed -= timeSinceLastFrame * mAnimationSpeed;
|
---|
79 |
|
---|
80 | if (!mEntity || !mAnimationState)
|
---|
81 | return;
|
---|
82 |
|
---|
83 | if (mState == MOVING) // toggle between moving (longer) and waiting (short)
|
---|
84 | {
|
---|
85 | SceneNode *parent = mEntity->getParentSceneNode();
|
---|
86 |
|
---|
87 | if (!parent)
|
---|
88 | return;
|
---|
89 |
|
---|
90 | if (mTimeElapsed <= 0) // toggle animation state
|
---|
91 | {
|
---|
92 | if (mAnimationState->getAnimationName() == "Idle")
|
---|
93 | {
|
---|
94 | SetAnimationState("Walk", true);
|
---|
95 |
|
---|
96 | mTimeElapsed = walk_duration; // walk for mTimeElapsed units
|
---|
97 |
|
---|
98 | // choose random rotation
|
---|
99 | Radian rnd = Radian(Math::UnitRandom() * Math::PI * rotate_factor);
|
---|
100 |
|
---|
101 | //mEntity->getParentSceneNode()->rotate();
|
---|
102 | parent->yaw(rnd);
|
---|
103 | }
|
---|
104 | else
|
---|
105 | {
|
---|
106 | SetAnimationState("Idle", true);
|
---|
107 | mTimeElapsed = wait_duration; // wait for mTimeElapsed seconds
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (mAnimationState->getAnimationName() == "Walk") // move forward
|
---|
112 | {
|
---|
113 | // store old position, just in case we get out of bounds
|
---|
114 | Vector3 oldPos = parent->getPosition();
|
---|
115 | parent->translate(parent->getLocalAxes(), Vector3(move_factor * mAnimationSpeed, 0, 0));
|
---|
116 |
|
---|
117 | // HACK: if out of bounds => reset to old position and set animationstate to idle
|
---|
118 | if (OutOfBounds(parent))
|
---|
119 | {
|
---|
120 | parent->setPosition(oldPos);
|
---|
121 | SetAnimationState("Idle", true);
|
---|
122 |
|
---|
123 | mTimeElapsed = wait_duration;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | // add time to drive animation
|
---|
129 | mAnimationState->addTime(timeSinceLastFrame * mAnimationSpeed);
|
---|
130 | }
|
---|
131 | //-----------------------------------------------------------------------
|
---|
132 | void EntityState::SetAnimationState(String stateStr, bool loop)
|
---|
133 | {
|
---|
134 | if (!mEntity)
|
---|
135 | return;
|
---|
136 |
|
---|
137 | mAnimationState = mEntity->getAnimationState(stateStr);
|
---|
138 | mAnimationState->setLoop(loop);
|
---|
139 | mAnimationState->setEnabled(true);
|
---|
140 | }
|
---|
141 | //-----------------------------------------------------------------------
|
---|
142 | bool EntityState::OutOfBounds(SceneNode *node)
|
---|
143 | {
|
---|
144 | Vector3 pos = node->getPosition();
|
---|
145 |
|
---|
146 | if ((pos > msMinPos) && (pos < msMaxPos))
|
---|
147 | return false;
|
---|
148 |
|
---|
149 | return true;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 |
|
---|
154 | /*********************************************************/
|
---|
155 | /* TestCullingTerrainApplication implementation */
|
---|
156 | /*********************************************************/
|
---|
157 |
|
---|
158 |
|
---|
159 | TestCullingTerrainApplication::TestCullingTerrainApplication():
|
---|
160 | mTerrainContentGenerator(NULL),
|
---|
161 | mRayQueryExecutor(NULL),
|
---|
162 | mIVReader(NULL)
|
---|
163 | {
|
---|
164 | }
|
---|
165 | //-----------------------------------------------------------------------
|
---|
166 | TestCullingTerrainApplication::~TestCullingTerrainApplication()
|
---|
167 | {
|
---|
168 | OGRE_DELETE(mTerrainContentGenerator);
|
---|
169 | OGRE_DELETE(mRayQueryExecutor);
|
---|
170 | OGRE_DELETE(mIVReader);
|
---|
171 |
|
---|
172 | deleteEntityStates();
|
---|
173 | }
|
---|
174 | //-----------------------------------------------------------------------
|
---|
175 | void TestCullingTerrainApplication::deleteEntityStates()
|
---|
176 | {
|
---|
177 | for (int i = 0; i < (int)mEntityStates.size(); ++i)
|
---|
178 | {
|
---|
179 | OGRE_DELETE(mEntityStates[i]);
|
---|
180 | }
|
---|
181 |
|
---|
182 | mEntityStates.clear();
|
---|
183 | }
|
---|
184 | //-----------------------------------------------------------------------
|
---|
185 | void TestCullingTerrainApplication::createCamera()
|
---|
186 | {
|
---|
187 | // create the camera
|
---|
188 | mCamera = mSceneMgr->createCamera("PlayerCam");
|
---|
189 |
|
---|
190 | /** set a nice viewpoint
|
---|
191 | * we use a camera node here and apply all transformations on it instead
|
---|
192 | * of applying all transformations directly to the camera
|
---|
193 | * because then the camera is displayed correctly in the visualization
|
---|
194 | */
|
---|
195 | mCamNode = mSceneMgr->getRootSceneNode()->
|
---|
196 | createChildSceneNode("CamNode1", Vector3(707, 5000, 528));
|
---|
197 |
|
---|
198 | mCamNode->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
|
---|
199 | mCamNode->attachObject(mCamera);
|
---|
200 |
|
---|
201 | //-- create visualization camera
|
---|
202 | mVizCamera = mSceneMgr->createCamera("VizCam");
|
---|
203 | mVizCamera->setPosition(mCamNode->getPosition());
|
---|
204 |
|
---|
205 | mVizCamera->setNearClipDistance(1);
|
---|
206 | mCamera->setNearClipDistance(1);
|
---|
207 |
|
---|
208 | // infinite far plane?
|
---|
209 | if (mRoot->getRenderSystem()->getCapabilities()->hasCapability(RSC_INFINITE_FAR_PLANE))
|
---|
210 | {
|
---|
211 | mVizCamera->setFarClipDistance(0);
|
---|
212 | mCamera->setFarClipDistance(0);
|
---|
213 | }
|
---|
214 | else
|
---|
215 | {
|
---|
216 | mVizCamera->setFarClipDistance(20000);
|
---|
217 | mCamera->setFarClipDistance(20000);
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | //-----------------------------------------------------------------------
|
---|
222 | bool TestCullingTerrainApplication::setup()
|
---|
223 | {
|
---|
224 | bool carryOn = ExampleApplication::setup();
|
---|
225 |
|
---|
226 | if (carryOn)
|
---|
227 | createRenderTargetListener();
|
---|
228 |
|
---|
229 | return carryOn;
|
---|
230 | }
|
---|
231 | //-----------------------------------------------------------------------
|
---|
232 | void TestCullingTerrainApplication::createRenderTargetListener()
|
---|
233 | {
|
---|
234 | mWindow->addListener(new VisualizationRenderTargetListener(mSceneMgr));
|
---|
235 | }
|
---|
236 | //-----------------------------------------------------------------------
|
---|
237 | void TestCullingTerrainApplication::LoadWorldGeometry()
|
---|
238 | {
|
---|
239 | mIVReader = new IVReader();
|
---|
240 |
|
---|
241 | // _MAX_PATH is the maximum length allowed for a path
|
---|
242 | char currentPath[_MAX_PATH];
|
---|
243 | // use the function to get the path
|
---|
244 | getcwd(currentPath, _MAX_PATH);
|
---|
245 |
|
---|
246 |
|
---|
247 | // $$matt TODO; use relatrive path
|
---|
248 | std::stringstream d;
|
---|
249 | d << "current path: " << currentPath;
|
---|
250 | Ogre::LogManager::getSingleton().logMessage(d.str());
|
---|
251 |
|
---|
252 | String path = "E:/svn/gametools/OGRE/trunk/resources/";
|
---|
253 |
|
---|
254 |
|
---|
255 | /* SceneNode *node1 = mSceneMgr->getRootSceneNode()->createChildSceneNode("Node1");
|
---|
256 |
|
---|
257 | SceneNode *node2 = mSceneMgr->getRootSceneNode()->createChildSceneNode("Node2");
|
---|
258 | SceneNode *node3 = mSceneMgr->getRootSceneNode()->createChildSceneNode("Node3");
|
---|
259 | SceneNode *node4 = mSceneMgr->getRootSceneNode()->createChildSceneNode("Node4");
|
---|
260 | SceneNode *node5 = mSceneMgr->getRootSceneNode()->createChildSceneNode("Node5");
|
---|
261 | SceneNode *node6 = mSceneMgr->getRootSceneNode()->createChildSceneNode("Node6");
|
---|
262 | SceneNode *node7 = mSceneMgr->getRootSceneNode()->createChildSceneNode("Node7");
|
---|
263 |
|
---|
264 |
|
---|
265 |
|
---|
266 | node2->translate(Vector3(-400, 0, 750));
|
---|
267 | node3->scale(Vector3(100, 100 ,100));
|
---|
268 | node3->pitch(Radian(3 * Math::HALF_PI));
|
---|
269 | node4->translate(Vector3(-100, 0, 0));
|
---|
270 | node5->translate(Vector3(0, 300, 0));
|
---|
271 |
|
---|
272 | node6->scale(Vector3(100, 100 ,100));
|
---|
273 | node6->pitch(Radian(3 * Math::HALF_PI));
|
---|
274 | node6->roll(Radian(Math::HALF_PI));
|
---|
275 | node6->translate(Vector3(0, -100, 0));
|
---|
276 |
|
---|
277 | node7->scale(Vector3(100, 100 ,100));
|
---|
278 | node7->pitch(Radian(3 * Math::HALF_PI));
|
---|
279 | node7->roll(Radian(Math::HALF_PI));
|
---|
280 | node7->translate(Vector3(100, 0, 0));
|
---|
281 |
|
---|
282 | */
|
---|
283 |
|
---|
284 | Log *log = LogManager::getSingleton().createLog("IVLog.log");
|
---|
285 | mIVReader->setLog(log);
|
---|
286 |
|
---|
287 |
|
---|
288 |
|
---|
289 | #if 0
|
---|
290 | //String fileName = path + "media\\simple_geom.wrl";
|
---|
291 | String fileName = path + "media\\city.wrl";
|
---|
292 | //String fileName = path + "media\\house_test1.iv";
|
---|
293 | mCamNode->setPosition(Vector3(-6, 0.5, 10));
|
---|
294 | //mCamNode->setPosition(Vector3(412, 7.4, -667));
|
---|
295 | mCamNode->setOrientation(Quaternion(-0.95, 0, 0.3, 0));
|
---|
296 |
|
---|
297 | if (0 && mIVReader->loadFile(fileName.c_str()))
|
---|
298 | {
|
---|
299 | mIVReader->buildTree(mSceneMgr, node1);
|
---|
300 | }
|
---|
301 |
|
---|
302 | String fileName = path + "media\\house_test1.iv";
|
---|
303 |
|
---|
304 | if (0 && mIVReader->loadFile(fileName.c_str()))
|
---|
305 | {
|
---|
306 | mIVReader->buildTree(mSceneMgr, node1);
|
---|
307 | mIVReader->buildTree(mSceneMgr, node2);
|
---|
308 | }
|
---|
309 |
|
---|
310 | if (0 && mIVReader->loadFile("media\\anhk.wrl"))
|
---|
311 | {
|
---|
312 | mIVReader->buildTree(mSceneMgr, node3);
|
---|
313 | }
|
---|
314 | if (0 && mIVReader->loadFile("media\\ant.wrl"))
|
---|
315 | {
|
---|
316 | mIVReader->buildTree(mSceneMgr, node4);
|
---|
317 | }
|
---|
318 | if (0 && mIVReader->loadFile("media\\zepplin0.wrl"))
|
---|
319 | {
|
---|
320 | mIVReader->buildTree(mSceneMgr, node5);
|
---|
321 | }
|
---|
322 | if (0 && mIVReader->loadFile("media\\warbird.wrl"))
|
---|
323 | {
|
---|
324 | mIVReader->buildTree(mSceneMgr, node6);
|
---|
325 | }
|
---|
326 | if (0 && mIVReader->loadFile("media\\z_wing.wrl"))
|
---|
327 | {
|
---|
328 | mIVReader->buildTree(mSceneMgr, node7);
|
---|
329 | }
|
---|
330 |
|
---|
331 | #endif
|
---|
332 |
|
---|
333 | #if 1
|
---|
334 | //-- load vienna model
|
---|
335 |
|
---|
336 | SceneNode *roofsNode = NULL;
|
---|
337 | SceneNode *viennaNode = NULL;
|
---|
338 | SceneNode *roadsNode = NULL;
|
---|
339 | SceneNode *planeNode = NULL;
|
---|
340 | SceneNode *wallsNode = NULL;
|
---|
341 |
|
---|
342 | viennaNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("Vienna");
|
---|
343 |
|
---|
344 | mCamNode->setPosition(Vector3(830, 300, -540));
|
---|
345 | mCamNode->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
|
---|
346 |
|
---|
347 | //viennaNode->translate(Vector3(-300, -300, 0));
|
---|
348 |
|
---|
349 | String fileName = path + "media\\city1500_flat_1.iv";
|
---|
350 |
|
---|
351 | if (0 && mIVReader->loadFile(fileName.c_str()))
|
---|
352 | {
|
---|
353 | wallsNode = viennaNode->createChildSceneNode("Walls");
|
---|
354 | mIVReader->buildTree(mSceneMgr, wallsNode);
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 | fileName = path + "media\\roofs_1500.iv";
|
---|
359 | if (1 && mIVReader->loadFile(fileName.c_str()))
|
---|
360 | {
|
---|
361 | roofsNode = viennaNode->createChildSceneNode("Roofs");
|
---|
362 | mIVReader->buildTree(mSceneMgr, roofsNode);
|
---|
363 | }
|
---|
364 |
|
---|
365 | fileName = path + "media\\CityRoads60.iv";
|
---|
366 | if (0 && mIVReader->loadFile(fileName.c_str()))
|
---|
367 | {
|
---|
368 | roadsNode = viennaNode->createChildSceneNode("Roads");
|
---|
369 | mIVReader->buildTree(mSceneMgr, roadsNode);
|
---|
370 | }
|
---|
371 |
|
---|
372 | fileName = path + "media\\CityPlane60.iv";
|
---|
373 | if (0 && mIVReader->loadFile(fileName.c_str()))
|
---|
374 | {
|
---|
375 | planeNode = viennaNode->createChildSceneNode("Plane");
|
---|
376 | mIVReader->buildTree(mSceneMgr, planeNode);
|
---|
377 | }
|
---|
378 |
|
---|
379 | #endif
|
---|
380 |
|
---|
381 | mIVReader->collapse();
|
---|
382 |
|
---|
383 | //-- bake into static geometry
|
---|
384 | if (USE_STATIC_GEOMETRY)
|
---|
385 | {
|
---|
386 | BakeSceneIntoStaticGeometry("staticVienna", "Vienna");
|
---|
387 | }
|
---|
388 | }
|
---|
389 | //--------------------------------------------------------
|
---|
390 | void TestCullingTerrainApplication::BakeSceneIntoStaticGeometry(const String &staticGeomName,
|
---|
391 | const String &nodeName)
|
---|
392 | {
|
---|
393 | #if OGRE_103
|
---|
394 | // note: different static geom for roofs, roads, ..., becazse they have same material
|
---|
395 | StaticGeometry *staticGeom = mSceneMgr->createStaticGeometry(staticGeomName);
|
---|
396 |
|
---|
397 | // note: looping over entities here. why does scene node not work?
|
---|
398 | SceneManager::EntityIterator it = mSceneMgr->getEntityIterator();
|
---|
399 | while (it.hasMoreElements())
|
---|
400 | {
|
---|
401 | Entity *ent = it.getNext();
|
---|
402 | ent->setVisible(false);
|
---|
403 | staticGeom->addEntity(ent, ent->getParentSceneNode()->getPosition());
|
---|
404 | }
|
---|
405 |
|
---|
406 | staticGeom->setRegionDimensions(Vector3(100,100,100));
|
---|
407 | staticGeom->build();
|
---|
408 |
|
---|
409 | // cleanup node
|
---|
410 | //wallsNode->detachAllObjects();
|
---|
411 |
|
---|
412 | //roofsNode->detachAllObjects();
|
---|
413 | //roadsNode->detachAllObjects();
|
---|
414 | // planeNode->detachAllObjects();
|
---|
415 |
|
---|
416 | //viennaNode->removeChild("Walls");
|
---|
417 | mSceneMgr->destroySceneNode(nodeName);
|
---|
418 | #endif
|
---|
419 | }
|
---|
420 | //--------------------------------------------------------
|
---|
421 | void TestCullingTerrainApplication::createScene()
|
---|
422 | {
|
---|
423 | if (!msShowHillyTerrain)
|
---|
424 | LoadWorldGeometry();
|
---|
425 |
|
---|
426 | // Set ambient light
|
---|
427 | mAmbientLight = ColourValue(0.5, 0.5, 0.5);
|
---|
428 | mSceneMgr->setAmbientLight(mAmbientLight);
|
---|
429 |
|
---|
430 | //-- create light
|
---|
431 | mSunLight = mSceneMgr->createLight("SunLight");
|
---|
432 | mSunLight->setType(Light::LT_DIRECTIONAL);
|
---|
433 | //mSunLight->setType(Light::LT_SPOTLIGHT);
|
---|
434 | //mSunLight->setSpotlightRange(Degree(30), Degree(50));
|
---|
435 |
|
---|
436 | mSunLight->setPosition(707, 2000, 500);
|
---|
437 | mSunLight->setCastShadows(true);
|
---|
438 |
|
---|
439 | // set light angle not too small over the surface, otherwise shadows textures will be broken
|
---|
440 | Vector3 dir(0.5, 1, 0.5);
|
---|
441 | dir.normalise();
|
---|
442 | mSunLight->setDirection(dir);
|
---|
443 | //mSunLight->setDirection(Vector3::NEGATIVE_UNIT_Y);
|
---|
444 | mSunLight->setDiffuseColour(1, 1, 1);
|
---|
445 | mSunLight->setSpecularColour(1, 1, 1);
|
---|
446 |
|
---|
447 | // -- Fog
|
---|
448 | // NB it's VERY important to set this before calling setWorldGeometry
|
---|
449 | // because the vertex program picked will be different
|
---|
450 | ColourValue fadeColour(0.93, 0.86, 0.76);
|
---|
451 | mWindow->getViewport(0)->setBackgroundColour(fadeColour);
|
---|
452 | //mSceneMgr->setFog( FOG_LINEAR, fadeColour, .001, 500, 1000);
|
---|
453 |
|
---|
454 | // Create a skybox
|
---|
455 | mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox", 5000, true);
|
---|
456 |
|
---|
457 | if (msShowHillyTerrain)
|
---|
458 | {
|
---|
459 | std::string terrain_cfg("terrain.cfg");
|
---|
460 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
|
---|
461 | terrain_cfg = mResourcePath + terrain_cfg;
|
---|
462 | #endif
|
---|
463 | mSceneMgr->setWorldGeometry(terrain_cfg);
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 | //-- CEGUI setup
|
---|
468 | setupGui();
|
---|
469 |
|
---|
470 | // occluder plane to test visibility
|
---|
471 | if (0)
|
---|
472 | {
|
---|
473 | Plane plane; plane.normal = Vector3::UNIT_Y; plane.d = -60;
|
---|
474 | MeshManager::getSingleton().createPlane("Myplane",
|
---|
475 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane,
|
---|
476 | 5000,5000,100,100,true,1,5,5,Vector3::UNIT_Z);
|
---|
477 | Entity* pPlaneEnt = mSceneMgr->createEntity( "plane", "Myplane" );
|
---|
478 | pPlaneEnt->setMaterialName("Examples/Rockwall");
|
---|
479 | pPlaneEnt->setCastShadows(true);
|
---|
480 | mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(pPlaneEnt);
|
---|
481 | }
|
---|
482 |
|
---|
483 | // Warning: In GL since we can't go higher than the window res
|
---|
484 | mSceneMgr->setShadowTextureSettings(1024, 2);
|
---|
485 |
|
---|
486 | mSceneMgr->setShadowColour(ColourValue(0.5, 0.5, 0.5));
|
---|
487 |
|
---|
488 |
|
---|
489 | //-- terrain content setup
|
---|
490 |
|
---|
491 | // HACK: necessary to call once before the content creation for
|
---|
492 | // terrain initialisation
|
---|
493 | mSceneMgr->_renderScene(mCamera, mWindow->getViewport(0), true);
|
---|
494 |
|
---|
495 | // ray query executor: needed to clamp to terrain
|
---|
496 | mRayQueryExecutor = new RayQueryExecutor(mSceneMgr);
|
---|
497 |
|
---|
498 | mTerrainMinPos = EntityState::msMinPos = Vector3(0, 0, 0);
|
---|
499 | mTerrainMaxPos = EntityState::msMaxPos = Vector3(5000, 5000, 5000);
|
---|
500 |
|
---|
501 | mTerrainContentGenerator = new TerrainContentGenerator(mSceneMgr);
|
---|
502 |
|
---|
503 |
|
---|
504 | // if no objects in file, we generate new objects
|
---|
505 | if (msShowHillyTerrain && !mTerrainContentGenerator->LoadObjects("objects.out"))
|
---|
506 | {
|
---|
507 | // the objects are generated randomly distributed over the terrain
|
---|
508 | if (1) generateScene(500, 0); // create robots
|
---|
509 | if (0) generateScene(100, 1); // create trees
|
---|
510 | if (0) generateScene(100, 2); // create ninjas
|
---|
511 | }
|
---|
512 |
|
---|
513 | // HACK: load the view cells from the disc
|
---|
514 | //(dynamic_cast<VisibilityOctreeSceneManager *>(mSceneMgr))->LoadViewCells("");
|
---|
515 |
|
---|
516 | //std::string filename = "vienna_simple-21-04-avs2-viewCells.xml";
|
---|
517 | //std::string filename = "vienna_simple-21-04-kdcasm-viewCells.xml";
|
---|
518 | std::string filename = "vienna_simple-21-04-avs-viewCells.xml";
|
---|
519 |
|
---|
520 | mSceneMgr->setOption("LoadViewCells", filename.c_str());
|
---|
521 | }
|
---|
522 | //-----------------------------------------------------------------------
|
---|
523 | void TestCullingTerrainApplication::generateScene(int num, int objectType)
|
---|
524 | {
|
---|
525 | float val = TerrainFrameListener::msObjectScales[objectType];
|
---|
526 | Vector3 scale(val, val, val);
|
---|
527 | const float maxHeight = 75;
|
---|
528 |
|
---|
529 | // In order to provide much occlusion,
|
---|
530 | // height is restricted to maxHeight => no objects are created on peaks
|
---|
531 | mTerrainContentGenerator->SetMinPos(Vector3(mTerrainMinPos));
|
---|
532 | mTerrainContentGenerator->SetMaxPos(Vector3(mTerrainMaxPos.x, maxHeight, mTerrainMaxPos.z));
|
---|
533 |
|
---|
534 | //std::stringstream d; d << "objscale: " << scale[0];
|
---|
535 | //Ogre::LogManager::getSingleton().logMessage(d.str());
|
---|
536 |
|
---|
537 | mTerrainContentGenerator->SetScale(scale);
|
---|
538 | mTerrainContentGenerator->SetOffset(TerrainFrameListener::msObjectTerrainOffsets[objectType]);
|
---|
539 | mTerrainContentGenerator->GenerateScene(num, TerrainFrameListener::msObjectCaptions[objectType]);
|
---|
540 |
|
---|
541 | if (objectType != 0) // from our objects, only robot has animation phases
|
---|
542 | return;
|
---|
543 |
|
---|
544 | EntityList *entList = mTerrainContentGenerator->GetGeneratedEntities();
|
---|
545 |
|
---|
546 | //-- add animation state for new robots (located at the end of the list)
|
---|
547 | for (int i = (int)entList->size() - num; i < (int)entList->size(); ++i)
|
---|
548 | {
|
---|
549 | mEntityStates.push_back(new EntityState((*entList)[i],
|
---|
550 | EntityState::WAITING, Math::RangeRandom(0.5, 1.5)));
|
---|
551 | }
|
---|
552 |
|
---|
553 | // release limitations on height => it is possible for the user to put single
|
---|
554 | // objects on peaks of the terrain (will be only few, not relevant for occlusion)
|
---|
555 | mTerrainContentGenerator->SetMaxPos(mTerrainMaxPos);
|
---|
556 | }
|
---|
557 | //-----------------------------------------------------------------------
|
---|
558 | void TestCullingTerrainApplication::updateAnimations(Real timeSinceLastFrame)
|
---|
559 | {
|
---|
560 | for (int i = 0; i < (int)mEntityStates.size(); ++i)
|
---|
561 | {
|
---|
562 | SceneNode *sn = mEntityStates[i]->GetEntity()->getParentSceneNode();
|
---|
563 |
|
---|
564 | mEntityStates[i]->update(timeSinceLastFrame);
|
---|
565 |
|
---|
566 | if (mEntityStates[i]->GetState() == EntityState::MOVING)
|
---|
567 | {
|
---|
568 | Clamp2Terrain(sn, 0); //sn->setNodeVisible(false);
|
---|
569 | }
|
---|
570 | }
|
---|
571 | }
|
---|
572 | //-----------------------------------------------------------------------
|
---|
573 | EntityStates &TestCullingTerrainApplication::getEntityStates()
|
---|
574 | {
|
---|
575 | return mEntityStates;
|
---|
576 | }
|
---|
577 | //-----------------------------------------------------------------------
|
---|
578 | void TestCullingTerrainApplication::setupGui()
|
---|
579 | {
|
---|
580 | #if OGRE103
|
---|
581 | mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY,
|
---|
582 | false, 3000, ST_EXTERIOR_CLOSE);
|
---|
583 | #else
|
---|
584 | mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY,
|
---|
585 | false, 3000, mSceneMgr);
|
---|
586 | #endif
|
---|
587 | mGUISystem = new CEGUI::System(mGUIRenderer);
|
---|
588 |
|
---|
589 | // Mouse
|
---|
590 | CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLook.scheme");
|
---|
591 | CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
|
---|
592 | mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook",
|
---|
593 | (CEGUI::utf8*)"MouseArrow");
|
---|
594 |
|
---|
595 | CEGUI::MouseCursor::getSingleton().hide();
|
---|
596 | }
|
---|
597 | //-----------------------------------------------------------------------
|
---|
598 | void TestCullingTerrainApplication::createFrameListener()
|
---|
599 | {
|
---|
600 | mTerrainFrameListener = new TerrainFrameListener(mWindow, mCamera, mSceneMgr,
|
---|
601 | mGUIRenderer, mTerrainContentGenerator, mVizCamera, mCamNode, mSunLight, this);
|
---|
602 |
|
---|
603 | mRoot->addFrameListener(mTerrainFrameListener);
|
---|
604 | }
|
---|
605 | //-----------------------------------------------------------------------
|
---|
606 | void TestCullingTerrainApplication::chooseSceneManager()
|
---|
607 | {
|
---|
608 | #ifdef OGRE_103
|
---|
609 | if (msShowHillyTerrain)
|
---|
610 | {
|
---|
611 | // Terrain scene manager
|
---|
612 | mSceneMgr = mRoot->getSceneManager(ST_EXTERIOR_CLOSE);
|
---|
613 | }
|
---|
614 | else
|
---|
615 | { // octree scene manager
|
---|
616 | mSceneMgr = mRoot->getSceneManager(ST_GENERIC);
|
---|
617 | }
|
---|
618 | #else
|
---|
619 | if (msShowHillyTerrain)
|
---|
620 | {
|
---|
621 | //mSceneMgr = mRoot->createSceneManager("VisibilityTerrainSceneManager");
|
---|
622 | mSceneMgr = mRoot->createSceneManager("TerrainSceneManager");
|
---|
623 | }
|
---|
624 | else
|
---|
625 | {
|
---|
626 | mSceneMgr = mRoot->createSceneManager("VisibilityOctreeSceneManager");
|
---|
627 | //mSceneMgr = mRoot->createSceneManager("OctreeSceneManager");
|
---|
628 | }
|
---|
629 |
|
---|
630 | #endif
|
---|
631 | }
|
---|
632 | //-----------------------------------------------------------------------
|
---|
633 | bool TestCullingTerrainApplication::Clamp2Terrain(SceneNode *node, int terrainOffs)
|
---|
634 | {
|
---|
635 | // clamp scene node to terrain
|
---|
636 | Vector3 pos = node->getPosition();
|
---|
637 | Vector3 queryResult;
|
---|
638 |
|
---|
639 | if (mRayQueryExecutor->executeRayQuery(&queryResult,
|
---|
640 | Vector3(pos.x, MAX_HEIGHT, pos.z), Vector3::NEGATIVE_UNIT_Y))
|
---|
641 | {
|
---|
642 | node->setPosition(pos.x, queryResult.y + terrainOffs, pos.z);
|
---|
643 | return true;
|
---|
644 | }
|
---|
645 |
|
---|
646 | return false;
|
---|
647 | }
|
---|
648 |
|
---|
649 | //-----------------------------------------------------------------------
|
---|
650 | bool TestCullingTerrainApplication::Clamp2FloorPlane()
|
---|
651 | {
|
---|
652 | // clamp to floor plane
|
---|
653 | RaySceneQuery *raySceneQuery = mSceneMgr->createRayQuery(
|
---|
654 | Ray(mCamNode->getPosition(), Vector3::NEGATIVE_UNIT_Y));
|
---|
655 |
|
---|
656 | RaySceneQueryResult& qryResult = raySceneQuery->execute();
|
---|
657 |
|
---|
658 | RaySceneQueryResult::iterator rit = qryResult.begin();
|
---|
659 |
|
---|
660 | while (rit != qryResult.end() && rit->movable)
|
---|
661 | {
|
---|
662 | if (rit->movable->getName() != "PlayerCam")
|
---|
663 | {
|
---|
664 | mCamNode->setPosition(mCamNode->getPosition().x,
|
---|
665 | rit->movable->getWorldBoundingBox().getCenter().y + 2,
|
---|
666 | mCamNode->getPosition().z);
|
---|
667 |
|
---|
668 | /*std::stringstream d;
|
---|
669 | d << "World: " << it->movable->getWorldBoundingBox().getCenter().y <<
|
---|
670 | ", Object: " << it->movable->getBoundingBox().getCenter().y <<
|
---|
671 | ", Camera: " << mCamera->getDerivedPosition();
|
---|
672 |
|
---|
673 | LogManager::getSingleton().logMessage(d.str());*/
|
---|
674 |
|
---|
675 | return true;
|
---|
676 | }
|
---|
677 |
|
---|
678 | ++ rit;
|
---|
679 | }
|
---|
680 |
|
---|
681 | OGRE_DELETE(raySceneQuery);
|
---|
682 | return false;
|
---|
683 | }
|
---|
684 |
|
---|
685 |
|
---|
686 |
|
---|
687 | /**********************************************************************/
|
---|
688 | /* VisualizationRenderTargetListener implementation */
|
---|
689 | /**********************************************************************/
|
---|
690 |
|
---|
691 |
|
---|
692 | //-----------------------------------------------------------------------
|
---|
693 | VisualizationRenderTargetListener::VisualizationRenderTargetListener(SceneManager *sceneMgr)
|
---|
694 | :RenderTargetListener(), mSceneMgr(sceneMgr)
|
---|
695 | {
|
---|
696 | }
|
---|
697 | //-----------------------------------------------------------------------
|
---|
698 | void VisualizationRenderTargetListener::preViewportUpdate(const RenderTargetViewportEvent &evt)
|
---|
699 | {
|
---|
700 | // visualization viewport
|
---|
701 | const bool showViz = evt.source->getZOrder() == VIZ_VIEWPORT_Z_ORDER;
|
---|
702 | const bool nShowViz = !showViz;
|
---|
703 |
|
---|
704 | mSavedShadowTechnique = mSceneMgr->getShadowTechnique();
|
---|
705 | mSavedAmbientLight = mSceneMgr->getAmbientLight();
|
---|
706 |
|
---|
707 | // -- ambient light must be full for visualization, shadows disabled
|
---|
708 | if (showViz)
|
---|
709 | {
|
---|
710 | mSceneMgr->setAmbientLight(ColourValue(1, 1, 1));
|
---|
711 | mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE);
|
---|
712 | }
|
---|
713 |
|
---|
714 | mSceneMgr->setOption("PrepareVisualization", &showViz);
|
---|
715 | mSceneMgr->setOption("SkyBoxEnabled", &nShowViz);
|
---|
716 | //mSceneMgr->setOption("SkyPlaneEnabled", &showViz);
|
---|
717 |
|
---|
718 | RenderTargetListener::preViewportUpdate(evt);
|
---|
719 | }
|
---|
720 | //-----------------------------------------------------------------------
|
---|
721 | void VisualizationRenderTargetListener::postRenderTargetUpdate(const RenderTargetEvent &evt)
|
---|
722 | {
|
---|
723 | // reset values
|
---|
724 | mSceneMgr->setShadowTechnique(mSavedShadowTechnique);
|
---|
725 | mSceneMgr->setAmbientLight(mSavedAmbientLight);
|
---|
726 |
|
---|
727 | RenderTargetListener::postRenderTargetUpdate(evt);
|
---|
728 | }
|
---|
729 | //-----------------------------------------------------------------------
|
---|
730 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
|
---|
731 | {
|
---|
732 | // Create application object
|
---|
733 | TestCullingTerrainApplication app;
|
---|
734 |
|
---|
735 | try
|
---|
736 | {
|
---|
737 | app.go();
|
---|
738 | }
|
---|
739 | catch( Ogre::Exception& e )
|
---|
740 | {
|
---|
741 | MessageBox( NULL, e.getFullDescription().c_str(),
|
---|
742 | "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
|
---|
743 | }
|
---|
744 |
|
---|
745 | return 0;
|
---|
746 | }
|
---|