GraphicsInteractionManager.cpp

Go to the documentation of this file.
00001 /*
00002  * GraphicsInteractionManager.cpp
00003  *
00004  *  Created on: Dec 20, 2011
00005  *      Author: sushil
00006  ----------------------------------------------------------------------------
00007 Filename:    GraphicsInteractionManager.cpp originally  named BaseApplication.cpp
00008 -----------------------------------------------------------------------------
00009 
00010 This source file is part of the
00011    ___                 __    __ _ _    _
00012   /___\__ _ _ __ ___  / / /\ \ (_) | _(_)
00013  //  // _` | '__/ _ \ \ \/  \/ / | |/ / |
00014 / \_// (_| | | |  __/  \  /\  /| |   <| |
00015 \___/ \__, |_|  \___|   \/  \/ |_|_|\_\_|
00016       |___/
00017       Tutorial Framework
00018       http://www.ogre3d.org/tikiwiki/
00019 -----------------------------------------------------------------------------
00020 */
00021 
00022 #include <OgreMeshManager.h>
00023 #include <OgreEntity.h>
00024 #include <OgreSceneNode.h>
00025 
00026 #include <engine.h>
00027 #include <GraphicsInteractionManager.h>
00028 #include <uiMgr.h>
00029 
00030 #include <map>
00031 #include "DEBUG.h"
00032 
00033 using namespace OgreGFX;
00034 
00035 //-------------------------------------------------------------------------------------
00036 GraphicsInteractionManager::GraphicsInteractionManager(FastEcslent::Engine* eng):
00037         mRoot(0),
00038         mSceneMgr(0),
00039         mWindow(0),
00040         mCamera(0),
00041         mResourcesCfg(Ogre::StringUtil::BLANK),
00042         mPluginsCfg(Ogre::StringUtil::BLANK),
00043         mCursorWasVisible(false),
00044         mShutDown(false)
00045 {
00046         this->engine = eng;
00047 
00048 }
00049 
00050 //-------------------------------------------------------------------------------------
00051 GraphicsInteractionManager::~GraphicsInteractionManager(void)
00052 {
00053     //if (mTrayMgr) delete mTrayMgr;
00054     //if (mCameraMan) delete mCameraMan;
00055 
00056     //Remove ourself as a Window listener
00057     Ogre::WindowEventUtilities::removeWindowEventListener(mWindow, this);
00058     windowClosed(mWindow);
00059     delete mRoot;
00060 }
00061 
00062 //-------------------------------------------------------------------------------------
00063 bool GraphicsInteractionManager::configure(void)
00064 {
00065     // Show the configuration dialog and initialise the system
00066     // You can skip this and use root.restoreConfig() to load configuration
00067     // settings if you were sure there are valid ones saved in ogre.cfg
00068         if (!(mRoot->restoreConfig() || mRoot->showConfigDialog())){
00069                 return false;
00070         }
00071         mWindow = mRoot->initialise(true, "FastEcslent::GraphicsInteractionManager's Render Window");
00072         return true;
00073 
00074 }
00075 //-------------------------------------------------------------------------------------
00076 void GraphicsInteractionManager::chooseSceneManager(void)
00077 {
00078     // Get the SceneManager, in this case a generic one
00079         //mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
00080         mSceneMgr = mRoot->createSceneManager("DefaultSceneManager");
00081 }
00082 //-------------------------------------------------------------------------------------
00083 void GraphicsInteractionManager::createCamera(void)
00084 {
00085     // Create the camera
00086     mCamera = mSceneMgr->createCamera("PlayerCam");
00087 
00088     // Position it at 500 in Z direction
00089     //mCamera->setPosition(Ogre::Vector3(100,500,100));
00090     // Look back along -Z
00091     //mCamera->lookAt(Ogre::Vector3(0,0,0));
00092     mCamera->setNearClipDistance(5);
00093 
00094     //mCameraMan = new OgreBites::SdkCameraMan(mCamera);   // create a default camera controller
00095 }
00096 //-------------------------------------------------------------------------------------
00097 //void GraphicsInteractionManager::createFrameListener(void)
00098 void GraphicsInteractionManager::createUIMgr(void)
00099 {
00100         uiMgr = new OgreGFX::UIMgr(this);
00101         mRoot->addFrameListener(uiMgr);
00102 }
00103 
00104 void GraphicsInteractionManager::createWidgetMgr(void)
00105 {
00106         widgetMgr = new OgreGFX::WidgetMgr(this);
00107         mRoot->addFrameListener(widgetMgr);
00108 }
00109 
00110 /*void GraphicsInteractionManager::createInputSystem(void)
00111 {
00112         inputSystem = new FastEcslent::InputSystem(this, uiMgr->keyboard, uiMgr->mouse);
00113         mRoot->addFrameListener(inputSystem);
00114 }*/
00115 
00116 
00117 void GraphicsInteractionManager::createFrameListener(void) {
00118         mRoot->addFrameListener(this);
00119 }
00120 
00121 
00122 //-------------------------------------------------------------------------------------
00123 void GraphicsInteractionManager::destroyScene(void)
00124 {
00125 }
00126 //-------------------------------------------------------------------------------------
00127 void GraphicsInteractionManager::createViewports(void)
00128 {
00129     // Create one viewport, entire window
00130     Ogre::Viewport* vp = mWindow->addViewport(mCamera);
00131 
00132     vp->setVisibilityMask(OgreGFX::PLAYER_CAMERA_MASK);
00133     //vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
00134     vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
00135 
00136     // Alter the camera aspect ratio to match the viewport
00137     mCamera->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
00138 }
00139 //-------------------------------------------------------------------------------------
00140 void GraphicsInteractionManager::setupResources(void)
00141 {
00142     // Load resource paths from config file
00143     Ogre::ConfigFile cf;
00144     cf.load(mResourcesCfg);
00145 
00146     // Go through all sections & settings in the file
00147     Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
00148 
00149     Ogre::String secName, typeName, archName;
00150     while (seci.hasMoreElements())  {
00151         secName = seci.peekNextKey();
00152         Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
00153         Ogre::ConfigFile::SettingsMultiMap::iterator i;
00154         for (i = settings->begin(); i != settings->end(); ++i){
00155             typeName = i->first;
00156             archName = i->second;
00157             Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
00158                 archName, typeName, secName);
00159         }
00160     }
00161 }
00162 //-------------------------------------------------------------------------------------
00163 void GraphicsInteractionManager::createResourceListener(void)
00164 {
00165 
00166 }
00167 //-------------------------------------------------------------------------------------
00168 void GraphicsInteractionManager::loadResources(void)
00169 {
00170     Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
00171 }
00172 //-------------------------------------------------------------------------------------
00173 void GraphicsInteractionManager::go(void)
00174 {
00175 #ifdef _DEBUG
00176     mResourcesCfg = "config/resources_d.cfg";
00177     mPluginsCfg = "config/plugins_d.cfg";
00178 #else
00179     mResourcesCfg = "config/resources.cfg";
00180     mPluginsCfg = "config/plugins.cfg";
00181 #endif
00182 
00183     if (!setup())
00184         return;
00185 
00186     mRoot->startRendering();
00187 
00188     // clean up
00189     destroyScene();
00190 }
00191 //-------------------------------------------------------------------------------------
00192 void GraphicsInteractionManager::initManagers(void) {
00193 
00194                 uiMgr->initialize();
00195                 widgetMgr->initialize();
00196 }
00197 
00198 bool GraphicsInteractionManager::setup(void)
00199 {
00200     mRoot = new Ogre::Root(mPluginsCfg);
00201     setupResources();
00202     bool carryOn = configure();
00203     if (!carryOn) return false;
00204     chooseSceneManager();
00205     createCamera();
00206     createViewports();
00207 
00208     // Set default mipmap level (NB some APIs ignore this)
00209     Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
00210     // Create any resource listeners (for loading screens)
00211     createResourceListener();
00212     // Load resources
00213     loadResources();
00214     // Create the scene
00215     createScene();
00216     createUIMgr();
00217     createWidgetMgr();
00218 //    createInputSystem();
00219     createFrameListener();
00220     initManagers();
00221 
00222     return true;
00223 };
00224 //-------------------------------------------------------------------------------------
00225 
00226 void GraphicsInteractionManager::handleBornEnts(){
00227         if (this->engine->entityMgr->nEnts > this->nGFXNodes) {
00228                 for (int i = nGFXNodes; i < this->engine->entityMgr->nEnts; i++){
00229                                 makeNode(this->engine->entityMgr->ents[i]);
00230                 }
00231                 this->nGFXNodes = engine->entityMgr->nEnts;
00232         }
00233 
00234 }
00235 
00236 
00237 void GraphicsInteractionManager::handleDevelopingEnts(){
00238 
00239 }
00240 
00241 
00242 bool GraphicsInteractionManager::frameRenderingQueued(const Ogre::FrameEvent& evt){
00243     if(mWindow->isClosed())
00244         return false;
00245     mShutDown = uiMgr->shutDown;
00246     if(mShutDown){
00247         if(uiMgr) {
00248                 uiMgr->kill();
00249                 delete uiMgr;
00250                 boost::mutex::scoped_lock scoped_lock(engine->quitLock);
00251                 engine->quit = true;
00252         }
00253         return false;
00254     }
00255     handleDevelopingEnts(); // create nodes
00256     handleBornEnts();       // move   nodes
00257     copySimState(); //handleLivingEnts
00258 
00259 
00260     return true;
00261 }
00262 
00263 //Adjust mouse clipping area
00264 void GraphicsInteractionManager::windowResized(Ogre::RenderWindow* rw){
00265 }
00266 
00267 //Unattach OIS before window shutdown (very important under Linux)
00268 void GraphicsInteractionManager::windowClosed(Ogre::RenderWindow* rw){
00269     //Only close for window that created OIS (the main window in these demos)
00270     if( rw == mWindow ) {
00271         if(uiMgr) {
00272                 uiMgr->kill();
00273         }
00274     }
00275 }
00276 
00277 
00278 
00279 void GraphicsInteractionManager::makeInitialEntityNodes(){
00280         this->nGFXNodes = engine->entityMgr->nEnts;
00281         DEBUG(std::cout << "GFX: Number of ents: " << nGFXNodes << std::endl;)
00282         for (int i = 0; i < nGFXNodes; i++){
00283                 makeNode(engine->entityMgr->ents[i]);
00284         }
00285 
00286 }
00287 
00288 
00289 void GraphicsInteractionManager::makeNode(FastEcslent::Entity *feEnt){
00290         DEBUG(std::cout << "Name: " << feEnt->uiname << std::endl;)
00291         Ogre::Entity* ent = mSceneMgr->createEntity(feEnt->uiname, feEnt->meshName);
00292 
00293         ent->setVisibilityFlags(OgreGFX::ENT_VISIBILITY_FLAG);
00294 //              if (engine->entityMgr->ents[i]->entityClass == FastEcslent::STATIC){
00295 //                      ent->setVisibilityFlags(OgreGFX::STATIC_VISIBILITY_FLAG);
00296 //              }
00297         DEBUG(std::cout << "GFX creating: " << feEnt->uiname << std::endl;)
00298 
00299         Ogre::SceneNode* sceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(feEnt->pos);
00300         std::stringstream ss;
00301         ss << feEnt->uiname << "_mini" ;
00302         Ogre::Entity* minimapEnt = mSceneMgr->createEntity(ss.str(), "cube.mesh" );
00303         minimapEnt->setVisibilityFlags(OgreGFX::MINIMAP_VISIBILITY_FLAG);
00304         Ogre::SceneNode* minimapNode = sceneNode->createChildSceneNode();
00305         minimapNode->attachObject(minimapEnt);
00306         minimapNode->setScale(2.0, 2.0, 2.0);
00307         int id = feEnt->entityId.id;
00308         gfxNodes[id].node = sceneNode;
00309         gfxNodes[id].node->attachObject(ent);
00310         gfxNodes[id].node->resetOrientation();
00311         gfxNodes[id].node->yaw(Ogre::Radian(feEnt->heading));
00312         gfxNodes[id].id = id;
00313         gfxNodes[id].selectable = feEnt->selectable;
00314         sceneNodeToEntIdMap[sceneNode] = id;
00315 }
00316 
00317 
00318 void GraphicsInteractionManager::createScene(){
00319 
00320         //this->mShutDown = false;
00321         Ogre::Entity*         mOceanSurfaceEnt;
00322         makeInitialEntityNodes();
00323 
00324         //mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));
00325         mSceneMgr->setAmbientLight(Ogre::ColourValue(0.8, 0.8, 0.8));
00326         Ogre::Light* l1 = mSceneMgr->createLight("LightOne");
00327         l1->setPosition(20, 80, 50);
00328 
00329         //mSceneMgr->setSkyBox(true, "SkyBox", 1000);
00330         mSceneMgr->setSkyDome(true, "Examples/CloudySky", 10, 8);
00331         //mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox", 50000, false);
00332 
00333 
00334     // Define a plane mesh that will be used for the ocean surface
00335         //Ogre::Plane oceanSurface;
00336     oceanSurface.normal = Ogre::Vector3::UNIT_Y;
00337     oceanSurface.d = 0;
00338     Ogre::MeshManager::getSingleton().createPlane("OceanSurface",
00339         Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
00340         oceanSurface,
00341         10000, 10000, 50, 50, true, 1, 1, 1, Ogre::Vector3::UNIT_Z);
00342 
00343     mOceanSurfaceEnt = mSceneMgr->createEntity( "OceanSurface", "OceanSurface" );
00344     mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(mOceanSurfaceEnt);
00345     mOceanSurfaceEnt->setCastShadows(false);
00346     mOceanSurfaceEnt->setMaterialName("OceanHLSL_GLSL");
00347     //mOceanSurfaceEnt->setMaterialName("OceanCg");
00348     //mOceanSurfaceEnt->setMaterialName("Ocean2_Cg");
00349 }
00350 
00351 
00352 
00353 void GraphicsInteractionManager::copySimState(){
00354 
00355         for(int i = 0; i < nGFXNodes; i++){
00356                 DEBUG(std::cout << engine->entityMgr->ents[i]->pos.x << ", " << engine->entityMgr->ents[0]->pos.y << ", " <<  engine->entityMgr->ents[0]->pos.z << std::endl;)
00357                 gfxNodes[i].node->setPosition(engine->entityMgr->ents[i]->pos);//.x, engine->entityMgr->ents[0]->pos.y, engine->entityMgr->ents[0]->pos.z);
00358                 gfxNodes[i].node->resetOrientation();
00359                 gfxNodes[i].node->yaw(Ogre::Radian(engine->entityMgr->ents[i]->heading));
00360         }
00361 }
00362 
00363 

Generated on Fri Dec 13 14:54:17 2013 for FastECSLent by  doxygen 1.5.4