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 
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 GraphicsInteractionManager::~GraphicsInteractionManager(void)
00051 {
00052     //if (mTrayMgr) delete mTrayMgr;
00053     //if (mCameraMan) delete mCameraMan;
00054 
00055     //Remove ourself as a Window listener
00056     Ogre::WindowEventUtilities::removeWindowEventListener(mWindow, this);
00057     windowClosed(mWindow);
00058     delete mRoot;
00059 }
00060 
00061 //-------------------------------------------------------------------------------------
00062 bool GraphicsInteractionManager::configure(void)
00063 {
00064     // Show the configuration dialog and initialise the system
00065     // You can skip this and use root.restoreConfig() to load configuration
00066     // settings if you were sure there are valid ones saved in ogre.cfg
00067         if (!(mRoot->restoreConfig() || mRoot->showConfigDialog())){
00068                 return false;
00069         }
00070         mWindow = mRoot->initialise(true, "FastEcslent::GraphicsInteractionManager's Render Window");
00071         return true;
00072 
00073 }
00074 //-------------------------------------------------------------------------------------
00075 void GraphicsInteractionManager::chooseSceneManager(void)
00076 {
00077     // Get the SceneManager, in this case a generic one
00078         //mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
00079         mSceneMgr = mRoot->createSceneManager("DefaultSceneManager");
00080 }
00081 //-------------------------------------------------------------------------------------
00082 void GraphicsInteractionManager::createCamera(void)
00083 {
00084     // Create the camera
00085     mCamera = mSceneMgr->createCamera("PlayerCam");
00086 
00087     // Position it at 500 in Z direction
00088     //mCamera->setPosition(Ogre::Vector3(100,500,100));
00089     // Look back along -Z
00090     //mCamera->lookAt(Ogre::Vector3(0,0,0));
00091     mCamera->setNearClipDistance(5);
00092 
00093     //mCameraMan = new OgreBites::SdkCameraMan(mCamera);   // create a default camera controller
00094 }
00095 //-------------------------------------------------------------------------------------
00096 //void GraphicsInteractionManager::createFrameListener(void)
00097 void GraphicsInteractionManager::createUIMgr(void)
00098 {
00099         uiMgr = new OgreGFX::UIMgr(this);
00100 }
00101 
00102 void GraphicsInteractionManager::createWidgetMgr(void)
00103 {
00104         widgetMgr = new OgreGFX::WidgetMgr(this);
00105         mRoot->addFrameListener(widgetMgr);
00106 }
00107 
00108 /*void GraphicsInteractionManager::createInputSystem(void)
00109 {
00110         inputSystem = new FastEcslent::InputSystem(this, uiMgr->keyboard, uiMgr->mouse);
00111         mRoot->addFrameListener(inputSystem);
00112 }*/
00113 
00114 
00115 void GraphicsInteractionManager::loadGameGFX(void) {
00116         setupGame();
00117         mRoot->addFrameListener(this);
00118 }
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::initGFXManagers(void) {
00193         uiMgr->initialize();
00194         widgetMgr->initialize();
00195         mRoot->addFrameListener(uiMgr);
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     createUIMgr();
00215 
00216     //create Lobby Manager
00217     createLobbyMgr();
00218 
00219     //Load Lobby GUI
00220     loadLobbyGFX();
00221 
00222     return true;
00223 };
00224 
00225 bool GraphicsInteractionManager::setupGame(void)
00226 {
00227         createScene();
00228     createWidgetMgr();
00229 
00230     initGFXManagers();
00231 
00232     return true;
00233 };
00234 //-------------------------------------------------------------------------------------
00235 
00236 void GraphicsInteractionManager::handleBornEnts(){
00237         if (this->engine->entityMgr->nEnts > this->nGFXNodes) {
00238                 for (int i = nGFXNodes; i < this->engine->entityMgr->nEnts; i++){
00239                                 makeNode(this->engine->entityMgr->ents[i]);
00240                 }
00241                 this->nGFXNodes = engine->entityMgr->nEnts;
00242         }
00243 }
00244 
00245 
00246 void GraphicsInteractionManager::handleDevelopingEnts(){
00247 
00248 }
00249 
00250 
00251 bool GraphicsInteractionManager::frameRenderingQueued(const Ogre::FrameEvent& evt){
00252     if(mWindow->isClosed())
00253         return false;
00254     mShutDown = uiMgr->shutDown;
00255     if(mShutDown){
00256         if(uiMgr) {
00257                 uiMgr->kill();
00258                 delete uiMgr;
00259                 boost::mutex::scoped_lock scoped_lock(engine->quitLock);
00260                 engine->quit = true;
00261         }
00262         return false;
00263     }
00264 
00265     handleDevelopingEnts(); // create nodes
00266     handleBornEnts();       // move   nodes
00267     copySimState(); //handleLivingEnts
00268 
00269     return true;
00270 }
00271 
00272 //Adjust mouse clipping area
00273 void GraphicsInteractionManager::windowResized(Ogre::RenderWindow* rw){
00274            //check input
00275            unsigned int width, height, depth;
00276            int left, top;
00277            rw->getMetrics(width, height, depth, left, top);
00278 
00279            const OIS::MouseState &ms = uiMgr->mouse->getMouseState();
00280            ms.width = width;
00281            ms.height = height;
00282 }
00283 
00284 //Unattach OIS before window shutdown (very important under Linux)
00285 void GraphicsInteractionManager::windowClosed(Ogre::RenderWindow* rw){
00286     //Only close for window that created OIS (the main window in these demos)
00287     if( rw == mWindow ) {
00288         if(uiMgr) {
00289                 uiMgr->kill();
00290         }
00291     }
00292 }
00293 
00294 
00295 
00296 void GraphicsInteractionManager::makeInitialEntityNodes(){
00297         this->nGFXNodes = engine->entityMgr->nEnts;
00298         std::cout << "GFX: Number of ents: " << nGFXNodes << std::endl;
00299         for (int i = 0; i < nGFXNodes; i++){
00300                 makeNode(engine->entityMgr->ents[i]);
00301         }
00302 }
00303 
00304 
00305 void GraphicsInteractionManager::makeNode(FastEcslent::Entity *feEnt){
00306         std::cout << "Name: " << feEnt->uiname << std::endl;
00307         Ogre::Entity* ent = mSceneMgr->createEntity(feEnt->uiname, feEnt->meshName);
00308 
00309         ent->setVisibilityFlags(OgreGFX::ENT_VISIBILITY_FLAG);
00310 //              if (engine->entityMgr->ents[i]->entityClass == FastEcslent::STATIC){
00311 //                      ent->setVisibilityFlags(OgreGFX::STATIC_VISIBILITY_FLAG);
00312 //              }
00313         std::cout << "GFX creating: " << feEnt->uiname << std::endl;
00314 
00315         Ogre::SceneNode* sceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(feEnt->pos);
00316         std::stringstream ss;
00317         ss << feEnt->uiname << "_mini" ;
00318         Ogre::Entity* minimapEnt = mSceneMgr->createEntity(ss.str(), "cube.mesh" );
00319         minimapEnt->setVisibilityFlags(OgreGFX::MINIMAP_VISIBILITY_FLAG);
00320         Ogre::SceneNode* minimapNode = sceneNode->createChildSceneNode();
00321         minimapNode->attachObject(minimapEnt);
00322         minimapNode->setScale(2.0, 2.0, 2.0);
00323         int id = feEnt->entityId.id;
00324         gfxNodes[id].node = sceneNode;
00325         gfxNodes[id].node->attachObject(ent);
00326         gfxNodes[id].node->resetOrientation();
00327         gfxNodes[id].node->yaw(Ogre::Radian(feEnt->heading));
00328         gfxNodes[id].id = id;
00329         gfxNodes[id].selectable = feEnt->selectable;
00330         sceneNodeToEntIdMap[sceneNode] = id;
00331 }
00332 
00333 
00334 void GraphicsInteractionManager::createScene(){
00335 
00336         //this->mShutDown = false;
00337         Ogre::Entity*         mOceanSurfaceEnt;
00338         makeInitialEntityNodes();
00339 
00340         //mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));
00341         mSceneMgr->setAmbientLight(Ogre::ColourValue(0.8, 0.8, 0.8));
00342         Ogre::Light* l1 = mSceneMgr->createLight("LightOne");
00343         l1->setPosition(20, 80, 50);
00344 
00345         //mSceneMgr->setSkyBox(true, "SkyBox", 1000);
00346         mSceneMgr->setSkyDome(true, "Examples/CloudySky", 10, 8);
00347         //mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox", 50000, false);
00348 
00349 
00350     // Define a plane mesh that will be used for the ocean surface
00351         //Ogre::Plane oceanSurface;
00352     oceanSurface.normal = Ogre::Vector3::UNIT_Y;
00353     oceanSurface.d = 0;
00354     Ogre::MeshManager::getSingleton().createPlane("OceanSurface",
00355         Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
00356         oceanSurface,
00357         10000, 10000, 50, 50, true, 1, 1, 1, Ogre::Vector3::UNIT_Z);
00358 
00359     mOceanSurfaceEnt = mSceneMgr->createEntity( "OceanSurface", "OceanSurface" );
00360     mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(mOceanSurfaceEnt);
00361     mOceanSurfaceEnt->setCastShadows(false);
00362     mOceanSurfaceEnt->setMaterialName("OceanHLSL_GLSL");
00363     //mOceanSurfaceEnt->setMaterialName("OceanCg");
00364     //mOceanSurfaceEnt->setMaterialName("Ocean2_Cg");
00365 }
00366 
00367 
00368 
00369 void GraphicsInteractionManager::copySimState(){
00370 
00371         for(int i = 0; i < nGFXNodes; i++){
00372                 //std::cout << engine->entityMgr->ents[i]->pos.x << ", " << engine->entityMgr->ents[0]->pos.y << ", " <<  engine->entityMgr->ents[0]->pos.z << std::endl;
00373                 gfxNodes[i].node->setPosition(engine->entityMgr->ents[i]->pos);//.x, engine->entityMgr->ents[0]->pos.y, engine->entityMgr->ents[0]->pos.z);
00374                 gfxNodes[i].node->resetOrientation();
00375                 gfxNodes[i].node->yaw(Ogre::Radian(engine->entityMgr->ents[i]->heading));
00376         }
00377 }
00378 
00379 void GraphicsInteractionManager::createLobbyMgr()
00380 {
00381         lobbyMgr = new LobbyMgr(engine, this, uiMgr->mouse, uiMgr->keyboard);
00382 }
00383 
00384 void GraphicsInteractionManager::loadLobbyGFX(void) {
00385         mRoot->addFrameListener(lobbyMgr);
00386 }

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