Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 167 vs 168 for /trunk/Source Code/Scenes/Scene.cpp

Diff revisions: vs.
  @@ -7,8 +7,9 @@
7 7 #include "Core/Math/Headers/Transform.h"
8 8
9 9 #include "Utility/Headers/XMLParser.h"
10 - #include "Rendering/Headers/Frustum.h"
11 10 #include "Managers/Headers/AIManager.h"
11 + #include "Managers/Headers/CameraManager.h"
12 + #include "Rendering/Camera/Headers/FreeFlyCamera.h"
12 13
13 14 #include "Environment/Sky/Headers/Sky.h"
14 15 #include "Environment/Terrain/Headers/Terrain.h"
  @@ -24,23 +25,29 @@
24 25 #include "Dynamics/Physics/Headers/PhysicsSceneInterface.h"
25 26
26 27 Scene::Scene() : Resource(),
27 - _GFX(GFX_DEVICE),
28 - _FBSpeedFactor(1.0f),
29 - _LRSpeedFactor(5.0f),
30 - _loadComplete(false),
31 - _paramHandler(ParamHandler::getInstance()),
32 - _currentSelection(NULL),
33 - _physicsInterface(NULL),
34 - _sceneGraph(New SceneGraph()),
35 - _sceneState(New SceneState()),
36 - _sceneRenderState(New SceneRenderState())
28 + _GFX(GFX_DEVICE),
29 + _FBSpeedFactor(1.0f),
30 + _LRSpeedFactor(5.0f),
31 + _loadComplete(false),
32 + _paramHandler(ParamHandler::getInstance()),
33 + _currentSelection(NULL),
34 + _physicsInterface(NULL),
35 + _cameraMgr(NULL),
36 + _sceneGraph(New SceneGraph())
37 37 {
38 + _mousePressed[OIS::MB_Left] = false;
39 + _mousePressed[OIS::MB_Right] = false;
40 + _mousePressed[OIS::MB_Middle] = false;
41 + _mousePressed[OIS::MB_Button3] = false;
42 + _mousePressed[OIS::MB_Button4] = false;
43 + _mousePressed[OIS::MB_Button5] = false;
44 + _mousePressed[OIS::MB_Button6] = false;
45 + _mousePressed[OIS::MB_Button7] = false;
38 46 }
39 47
40 - Scene::~Scene() {
41 - SAFE_DELETE(_sceneState);
42 - SAFE_DELETE(_sceneRenderState);
43 - };
48 + Scene::~Scene()
49 + {
50 + }
44 51
45 52 bool Scene::idle(){ //Called when application is idle
46 53 if(_sceneGraph){
  @@ -75,6 +82,10 @@
75 82 return true;
76 83 }
77 84
85 + void Scene::updateCameras(){
86 + renderState().updateCamera(_cameraMgr->getActiveCamera());
87 + }
88 +
78 89 void Scene::postRender(){
79 90
80 91 }
  @@ -180,6 +191,20 @@
180 191 LightManager::getInstance().addLight(lightItem);
181 192 }
182 193
194 + Camera* Scene::addDefaultCamera(){
195 + PRINT_FN(Locale::get("SCENE_ADD_DEFAULT_CAMERA"), getName().c_str());
196 + Camera* camera = New FreeFlyCamera();
197 + // force all lights to update on camera change (to keep them still actually)
198 + camera->addUpdateListener(DELEGATE_BIND(&LightManager::update, DELEGATE_REF(LightManager::getInstance())));
199 + camera->setMoveSpeedFactor(_paramHandler.getParam<F32>("options.cameraSpeed.move"));
200 + camera->setTurnSpeedFactor(_paramHandler.getParam<F32>("options.cameraSpeed.turn"));
201 + camera->setFixedYawAxis(true);
202 + //As soon as a camera is added to the camera manager, the manager is responsible for cleaning it up
203 + _cameraMgr->addNewCamera("defaultCamera", camera);
204 + _cameraMgr->setActiveCamera("defaultCamera");
205 + return camera;
206 + }
207 +
183 208 Light* Scene::addDefaultLight(){
184 209 std::stringstream ss; ss << LightManager::getInstance().getLights().size();
185 210 ResourceDescriptor defaultLight("Default directional light "+ss.str());
  @@ -193,8 +218,8 @@
193 218 return l;
194 219 }
195 220 ///Add skies
196 - Sky* Scene::addDefaultSky(){
197 221 #pragma message("ToDo: load skyboxes from XML")
222 + Sky* Scene::addDefaultSky(){
198 223 Sky* tempSky = New Sky("Default Sky");
199 224 _skiesSGN.push_back(_sceneGraph->getRoot()->addNode(tempSky));
200 225 return tempSky;
  @@ -215,19 +240,20 @@
215 240 }
216 241
217 242 bool Scene::preLoad() {
218 - ParamHandler& par = ParamHandler::getInstance();
219 - _GFX.enableFog(_sceneState->getFogDesc()._fogMode,
220 - _sceneState->getFogDesc()._fogDensity,
221 - _sceneState->getFogDesc()._fogColor,
222 - _sceneState->getFogDesc()._fogStartDist,
223 - _sceneState->getFogDesc()._fogEndDist);
224 - _sceneRenderState->shadowMapResolutionFactor() = par.getParam<U8>("rendering.shadowResolutionFactor");
243 + _GFX.enableFog(_sceneState.getFogDesc()._fogMode,
244 + _sceneState.getFogDesc()._fogDensity,
245 + _sceneState.getFogDesc()._fogColor,
246 + _sceneState.getFogDesc()._fogStartDist,
247 + _sceneState.getFogDesc()._fogEndDist);
248 + renderState().shadowMapResolutionFactor(_paramHandler.getParam<U8>("rendering.shadowResolutionFactor"));
225 249 return true;
226 250 }
227 251
228 - bool Scene::load(const std::string& name){
252 + bool Scene::load(const std::string& name, CameraManager* const cameraMgr){
253 + _cameraMgr = cameraMgr;
254 + addDefaultCamera();
229 255 SceneGraphNode* root = _sceneGraph->getRoot();
230 - ///Add terrain from XML
256 + //Add terrain from XML
231 257 if(!_terrainInfoArray.empty()){
232 258 for(U8 i = 0; i < _terrainInfoArray.size(); i++){
233 259 ResourceDescriptor terrain(_terrainInfoArray[i]->getVariable("terrainName"));
  @@ -241,20 +267,19 @@
241 267 temp->initializeVegetation(_terrainInfoArray[i],terrainTemp);
242 268 }
243 269 }
244 - ///Camera position is overriden in the scene's XML configuration file
270 + //Camera position is overriden in the scene's XML configuration file
245 271 if(ParamHandler::getInstance().getParam<bool>("options.cameraStartPositionOverride")){
246 - renderState()->getCamera()->setEye(vec3<F32>(_paramHandler.getParam<F32>("options.cameraStartPosition.x"),
272 + renderState().getCamera().setEye(vec3<F32>(_paramHandler.getParam<F32>("options.cameraStartPosition.x"),
247 273 _paramHandler.getParam<F32>("options.cameraStartPosition.y"),
248 274 _paramHandler.getParam<F32>("options.cameraStartPosition.z")));
249 275 vec2<F32> camOrientation(_paramHandler.getParam<F32>("options.cameraStartOrientation.xOffsetDegrees"),
250 276 _paramHandler.getParam<F32>("options.cameraStartOrientation.yOffsetDegrees"));
251 - renderState()->getCamera()->setAngleX(RADIANS(camOrientation.x));
252 - renderState()->getCamera()->setAngleY(RADIANS(camOrientation.y));
277 + renderState().getCamera().setGlobalRotation(camOrientation.y/*yaw*/,camOrientation.x/*pitch*/);
253 278 }else{
254 - renderState()->getCamera()->setEye(vec3<F32>(0,50,0));
279 + renderState().getCamera().setEye(vec3<F32>(0,50,0));
255 280 }
256 281
257 - ///Create an AI thread, but start it only if needed
282 + //Create an AI thread, but start it only if needed
258 283 Kernel* kernel = Application::getInstance().getKernel();
259 284 _aiTask.reset(New Task(kernel->getThreadPool(),10,false,false,DELEGATE_BIND(&AIManager::tick, DELEGATE_REF(AIManager::getInstance()))));
260 285 _loadComplete = true;
  @@ -321,211 +346,8 @@
321 346 LightManager::getInstance().clear();
322 347 }
323 348
324 - void Scene::clearTasks(){
325 - PRINT_FN(Locale::get("STOP_SCENE_TASKS"));
326 - removeTasks();
327 - }
328 -
329 - void Scene::onMouseClickDown(const OIS::MouseEvent& key,OIS::MouseButtonID button){
330 - }
331 -
332 - void Scene::onMouseClickUp(const OIS::MouseEvent& key,OIS::MouseButtonID button){
333 - }
334 -
335 - static F32 speedFactor = 0.25f;
336 - void Scene::onKeyDown(const OIS::KeyEvent& key){
337 - switch(key.key){
338 - case OIS::KC_LEFT :
339 - state()->_angleLR = -(speedFactor/2.5);
340 - break;
341 - case OIS::KC_RIGHT :
342 - state()->_angleLR = speedFactor/2.5;
343 - break;
344 - case OIS::KC_UP :
345 - state()->_angleUD = -(speedFactor/5);
346 - break;
347 - case OIS::KC_DOWN :
348 - state()->_angleUD = speedFactor/5;
349 - break;
350 - case OIS::KC_END:
351 - deleteSelection();
352 - break;
353 - case OIS::KC_F2:{
354 - D_PRINT_FN(Locale::get("TOGGLE_SCENE_SKELETONS"));
355 - renderState()->toggleSkeletons();
356 - }break;
357 - case OIS::KC_B:{
358 - D_PRINT_FN(Locale::get("TOGGLE_SCENE_BOUNDING_BOXES"));
359 - renderState()->toggleBoundingBoxes();
360 - }break;
361 - case OIS::KC_ADD:
362 - if (speedFactor < 10) speedFactor += 0.1f;
363 - break;
364 - case OIS::KC_SUBTRACT:
365 - if (speedFactor > 0.1f) speedFactor -= 0.1f;
366 - break;
367 - case OIS::KC_F8:
368 - ParamHandler::getInstance().setParam("postProcessing.enablePostFX",!ParamHandler::getInstance().getParam<bool>("postProcessing.enablePostFX"));
369 - break;
370 - case OIS::KC_F10:
371 - LightManager::getInstance().togglePreviewShadowMaps();
372 - break;
373 - case OIS::KC_F12:
374 - GFX_DEVICE.Screenshot("screenshot_",vec4<F32>(0,0,renderState()->_cachedResolution.x,renderState()->_cachedResolution.y));
375 - break;
376 - default:
377 - break;
378 - }
379 - }
380 -
381 - void Scene::onKeyUp(const OIS::KeyEvent& key){
382 - switch( key.key ){
383 - case OIS::KC_LEFT:
384 - case OIS::KC_RIGHT:
385 - state()->_angleLR = 0.0f;
386 - break;
387 - case OIS::KC_UP:
388 - case OIS::KC_DOWN:
389 - state()->_angleUD = 0.0f;
390 - break;
391 - case OIS::KC_F3:
392 - _paramHandler.setParam("postProcessing.enableDepthOfField", !_paramHandler.getParam<bool>("postProcessing.enableDepthOfField"));
393 - break;
394 - case OIS::KC_F4:
395 - _paramHandler.setParam("postProcessing.enableBloom", !_paramHandler.getParam<bool>("postProcessing.enableBloom"));
396 - break;
397 - case OIS::KC_F5:
398 - GFX_DEVICE.drawDebugAxis(!GFX_DEVICE.drawDebugAxis());
399 - break;
400 - default:
401 - break;
402 - }
403 - }
404 -
405 - void Scene::onJoystickMoveAxis(const OIS::JoyStickEvent& key,I8 axis,I32 deadZone){
406 - if(key.device->getID() != InputInterface::JOY_1) return;
407 - if(axis == 1){
408 - if(key.state.mAxes[axis].abs > deadZone)
409 - state()->_angleLR = speedFactor/5;
410 - else if(key.state.mAxes[axis].abs < -deadZone)
411 - state()->_angleLR = -(speedFactor/5);
412 - else
413 - state()->_angleLR = 0;
414 - }else if(axis == 0){
415 - if(key.state.mAxes[axis].abs > deadZone)
416 - state()->_angleUD = speedFactor/5;
417 - else if(key.state.mAxes[axis].abs < -deadZone)
418 - state()->_angleUD = -(speedFactor/5);
419 - else
420 - state()->_angleUD = 0;
421 - }else if(axis == 2){
422 - if(key.state.mAxes[axis].abs < -deadZone)
423 - state()->_moveFB = 0.25f;
424 - else if(key.state.mAxes[axis].abs > deadZone)
425 - state()->_moveFB = -0.25f;
426 - else
427 - state()->_moveFB = 0;
428 - }else if(axis == 3){
429 - if(key.state.mAxes[axis].abs < -deadZone)
430 - state()->_moveLR = 0.25f;
431 - else if(key.state.mAxes[axis].abs > deadZone)
432 - state()->_moveLR = -0.25f;
433 - else
434 - state()->_moveLR = 0;
435 - }
436 - }
437 -
438 349 void Scene::updateSceneState(const U32 sceneTime){
439 - _sceneGraph->sceneUpdate(sceneTime);
440 - }
441 -
442 - void Scene::findSelection(const vec3<F32>& camOrigin, U32 x, U32 y){
443 - F32 value_fov = 0.7853f; //this is 45 degrees converted to radians
444 - F32 value_aspect = _paramHandler.getParam<F32>("runtime.aspectRatio");
445 - F32 half_resolution_width = renderState()->_cachedResolution.width / 2.0f;
446 - F32 half_resolution_height = renderState()->_cachedResolution.height / 2.0f;
447 -
448 - F32 modifier_x, modifier_y;
449 - //mathematical handling of the difference between
450 - //your mouse position and the 'center' of the window
451 -
452 - vec3<F32> point;
453 - //the untransformed ray will be put here
454 -
455 - F32 point_dist = _paramHandler.getParam<F32>("runtime.zFar");
456 - //it'll be put this far on the Z plane
457 -
458 - vec3<F32> camera_origin;
459 - //this is where the camera sits, in 3dspace
460 -
461 - vec3<F32> point_xformed;
462 - //this is the transformed point
463 -
464 - vec3<F32> final_point;
465 - vec4<F32> color(0.0, 1.0, 0.0, 1.0);
466 -
467 - //These lines are the biggest part of this function.
468 - //This is where the mouse position is turned into a mathematical
469 - //'relative' of 3d space. The conversion to an actual point
470 - modifier_x = (F32)tan( value_fov * 0.5f )
471 - * (( 1.0f - x / half_resolution_width ) * ( value_aspect ) );
472 - modifier_y = (F32)tan( value_fov * 0.5f )
473 - * -( 1.0f - y / half_resolution_height );
474 -
475 - //These 3 take our modifier_x/y values and our 'casting' distance
476 - //to throw out a point in space that lies on the point_dist plane.
477 - //If we were using completely untransformed, untranslated space,
478 - //this would be fine - but we're not :)
479 - point.x = modifier_x * point_dist;
480 - point.y = modifier_y * point_dist;
481 - point.z = point_dist;
482 -
483 - //Next we make an openGL call to grab our MODELVIEW_MATRIX -
484 - //This is the matrix that rasters 3d points to 2d space - which is
485 - //kinda what we're doing, in reverse
486 - mat4<GLfloat> temp;
487 - GFX_DEVICE.getMatrix(MV_MATRIX,temp);
488 - //Some folks would then invert the matrix - I invert the results.
489 -
490 - //First, to get the camera_origin, we transform the 12, 13, 14
491 - //slots of our pulledMatrix - this gets us the actual viewing
492 - //position we are 'sitting' at when the function is called
493 - camera_origin.x = -(
494 - temp.mat[0] * temp.mat[12] +
495 - temp.mat[1] * temp.mat[13] +
496 - temp.mat[2] * temp.mat[14]);
497 - camera_origin.y = -(
498 - temp.mat[4] * temp.mat[12] +
499 - temp.mat[5] * temp.mat[13] +
500 - temp.mat[6] * temp.mat[14]);
501 - camera_origin.z = -(
502 - temp.mat[8] * temp.mat[12] +
503 - temp.mat[9] * temp.mat[13] +
504 - temp.mat[10] * temp.mat[14]);
505 -
506 - //Second, we transform the position we generated earlier - the '3d'
507 - //mouse position - by our viewing matrix.
508 - point_xformed.x = -(
509 - temp.mat[0] * point[0] +
510 - temp.mat[1] * point[1] +
511 - temp.mat[2] * point[2]);
512 - point_xformed.y = -(
513 - temp.mat[4] * point[0] +
514 - temp.mat[5] * point[1] +
515 - temp.mat[6] * point[2]);
516 - point_xformed.z = -(
517 - temp.mat[8] * point[0] +
518 - temp.mat[9] * point[1] +
519 - temp.mat[10] * point[2]);
520 -
521 - final_point = point_xformed + camera_origin;
522 -
523 - vec3<F32> origin(camOrigin);
524 - vec3<F32> dir = origin.direction(final_point);
525 -
526 - Ray r(origin,dir);
527 - _currentSelection = _sceneGraph->Intersect(r,Frustum::getInstance().getZPlanes().x,
528 - Frustum::getInstance().getZPlanes().y/2.0f);
350 + _sceneGraph->sceneUpdate(sceneTime, _sceneState);
529 351 }
530 352
531 353 void Scene::deleteSelection(){
  @@ -534,7 +356,12 @@
534 356 }
535 357 }
536 358
537 - void Scene::removeTasks(){
359 + void Scene::addTask(Task_ptr taskItem) {
360 + _tasks.push_back(taskItem);
361 + }
362 +
363 + void Scene::clearTasks(){
364 + PRINT_FN(Locale::get("STOP_SCENE_TASKS"));
538 365 for_each(Task_ptr& task, _tasks){
539 366 task->stopTask();
540 367 }
  @@ -560,8 +387,4 @@
560 387 return;
561 388 }
562 389 }
563 - }
564 -
565 - void Scene::addTask(Task_ptr taskItem) {
566 - _tasks.push_back(taskItem);
567 390 }