Base.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #include "include/Base.h"
  2. //-------------------------------------------------------------------------------------
  3. Base::Base(void)
  4. :mRoot(0),
  5. mCamera(0),
  6. mSceneMgr(0),
  7. mWindow(0),
  8. mResourcesCfg(Ogre::StringUtil::BLANK),
  9. mPluginsCfg(Ogre::StringUtil::BLANK),
  10. mTrayMgr(0),
  11. mCameraMan(0),
  12. mDetailsPanel(0),
  13. mCursorWasVisible(false),
  14. mShutDown(false),
  15. mInputManager(0),
  16. mMouse(0),
  17. mKeyboard(0)
  18. {
  19. }
  20. //-------------------------------------------------------------------------------------
  21. Base::~Base(void)
  22. {
  23. if (mTrayMgr) delete mTrayMgr;
  24. if (mCameraMan) delete mCameraMan;
  25. //Remove ourself as a Window listener
  26. Ogre::WindowEventUtilities::removeWindowEventListener(mWindow, this);
  27. windowClosed(mWindow);
  28. delete mRoot;
  29. }
  30. //-------------------------------------------------------------------------------------
  31. bool Base::configure(void)
  32. {
  33. // Show the configuration dialog and initialise the system
  34. // You can skip this and use root.restoreConfig() to load configuration
  35. // settings if you were sure there are valid ones saved in ogre.cfg
  36. if(mRoot->showConfigDialog())
  37. {
  38. // If returned true, user clicked OK so initialise
  39. // Here we choose to let the system create a default rendering window by passing 'true'
  40. mWindow = mRoot->initialise(true, "Codria");
  41. return true;
  42. }
  43. else
  44. {
  45. return false;
  46. }
  47. }
  48. //-------------------------------------------------------------------------------------
  49. void Base::chooseSceneManager(void)
  50. {
  51. // Get the SceneManager, in this case a generic one
  52. mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
  53. }
  54. //-------------------------------------------------------------------------------------
  55. void Base::createCamera(void)
  56. {
  57. // Create the camera
  58. mCamera = mSceneMgr->createCamera("PlayerCam");
  59. // Position it at 500 in Z direction
  60. mCamera->setPosition(Ogre::Vector3(0,0,80));
  61. // Look back along -Z
  62. mCamera->lookAt(Ogre::Vector3(0,0,-300));
  63. mCamera->setNearClipDistance(5);
  64. mCameraMan = new OgreBites::SdkCameraMan(mCamera); // create a default camera controller
  65. }
  66. //-------------------------------------------------------------------------------------
  67. void Base::createFrameListener(void)
  68. {
  69. Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
  70. OIS::ParamList pl;
  71. size_t windowHnd = 0;
  72. std::ostringstream windowHndStr;
  73. mWindow->getCustomAttribute("WINDOW", &windowHnd);
  74. windowHndStr << windowHnd;
  75. pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
  76. mInputManager = OIS::InputManager::createInputSystem( pl );
  77. mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, true ));
  78. mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject( OIS::OISMouse, true ));
  79. mMouse->setEventCallback(this);
  80. mKeyboard->setEventCallback(this);
  81. //Set initial mouse clipping size
  82. windowResized(mWindow);
  83. //Register as a Window listener
  84. Ogre::WindowEventUtilities::addWindowEventListener(mWindow, this);
  85. mTrayMgr = new OgreBites::SdkTrayManager("InterfaceName", mWindow, mMouse, this);
  86. mTrayMgr->showFrameStats(OgreBites::TL_BOTTOMLEFT);
  87. mTrayMgr->showLogo(OgreBites::TL_BOTTOMRIGHT);
  88. //mTrayMgr->hideCursor();
  89. // create a params panel for displaying sample details
  90. Ogre::StringVector items;
  91. items.push_back("cam.pX");
  92. items.push_back("cam.pY");
  93. items.push_back("cam.pZ");
  94. items.push_back("");
  95. items.push_back("cam.oW");
  96. items.push_back("cam.oX");
  97. items.push_back("cam.oY");
  98. items.push_back("cam.oZ");
  99. items.push_back("");
  100. items.push_back("Filtering");
  101. items.push_back("Poly Mode");
  102. mDetailsPanel = mTrayMgr->createParamsPanel(OgreBites::TL_NONE, "DetailsPanel", 200, items);
  103. mDetailsPanel->setParamValue(9, "Bilinear");
  104. mDetailsPanel->setParamValue(10, "Solid");
  105. mDetailsPanel->hide();
  106. mRoot->addFrameListener(this);
  107. }
  108. //-------------------------------------------------------------------------------------
  109. void Base::destroyScene(void)
  110. {
  111. }
  112. //-------------------------------------------------------------------------------------
  113. void Base::createViewports(void)
  114. {
  115. // Create one viewport, entire window
  116. Ogre::Viewport* vp = mWindow->addViewport(mCamera);
  117. vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
  118. // Alter the camera aspect ratio to match the viewport
  119. mCamera->setAspectRatio(
  120. Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
  121. }
  122. //-------------------------------------------------------------------------------------
  123. void Base::setupResources(void)
  124. {
  125. // Load resource paths from config file
  126. Ogre::ConfigFile cf;
  127. cf.load(mResourcesCfg);
  128. // Go through all sections & settings in the file
  129. Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
  130. Ogre::String secName, typeName, archName;
  131. while (seci.hasMoreElements())
  132. {
  133. secName = seci.peekNextKey();
  134. Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
  135. Ogre::ConfigFile::SettingsMultiMap::iterator i;
  136. for (i = settings->begin(); i != settings->end(); ++i)
  137. {
  138. typeName = i->first;
  139. archName = i->second;
  140. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  141. archName, typeName, secName);
  142. }
  143. }
  144. }
  145. //-------------------------------------------------------------------------------------
  146. void Base::createResourceListener(void)
  147. {
  148. }
  149. //-------------------------------------------------------------------------------------
  150. void Base::loadResources(void)
  151. {
  152. Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
  153. }
  154. //-------------------------------------------------------------------------------------
  155. void Base::go(void)
  156. {
  157. #ifdef _DEBUG
  158. mResourcesCfg = "resources_d.cfg";
  159. mPluginsCfg = "plugins_d.cfg";
  160. #else
  161. mResourcesCfg = "resources.cfg";
  162. mPluginsCfg = "plugins.cfg";
  163. #endif
  164. if (!setup())
  165. return;
  166. mRoot->startRendering();
  167. // clean up
  168. destroyScene();
  169. }
  170. //-------------------------------------------------------------------------------------
  171. bool Base::setup(void)
  172. {
  173. mRoot = new Ogre::Root(mPluginsCfg);
  174. setupResources();
  175. bool carryOn = configure();
  176. if (!carryOn) return false;
  177. chooseSceneManager();
  178. createCamera();
  179. createViewports();
  180. // Set default mipmap level (NB some APIs ignore this)
  181. Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
  182. // Create any resource listeners (for loading screens)
  183. createResourceListener();
  184. // Load resources
  185. loadResources();
  186. // Create the scene
  187. createScene();
  188. createFrameListener();
  189. return true;
  190. };
  191. //-------------------------------------------------------------------------------------
  192. bool Base::frameRenderingQueued(const Ogre::FrameEvent& evt)
  193. {
  194. if(mWindow->isClosed())
  195. return false;
  196. if(mShutDown)
  197. return false;
  198. //Need to capture/update each device
  199. mKeyboard->capture();
  200. mMouse->capture();
  201. mTrayMgr->frameRenderingQueued(evt);
  202. if (!mTrayMgr->isDialogVisible())
  203. {
  204. mCameraMan->frameRenderingQueued(evt); // if dialog isn't up, then update the camera
  205. if (mDetailsPanel->isVisible()) // if details panel is visible, then update its contents
  206. {
  207. mDetailsPanel->setParamValue(0, Ogre::StringConverter::toString(mCamera->getDerivedPosition().x));
  208. mDetailsPanel->setParamValue(1, Ogre::StringConverter::toString(mCamera->getDerivedPosition().y));
  209. mDetailsPanel->setParamValue(2, Ogre::StringConverter::toString(mCamera->getDerivedPosition().z));
  210. mDetailsPanel->setParamValue(4, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().w));
  211. mDetailsPanel->setParamValue(5, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().x));
  212. mDetailsPanel->setParamValue(6, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().y));
  213. mDetailsPanel->setParamValue(7, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().z));
  214. }
  215. }
  216. return true;
  217. }
  218. //-------------------------------------------------------------------------------------
  219. bool Base::keyPressed( const OIS::KeyEvent &arg )
  220. {
  221. if (mTrayMgr->isDialogVisible()) return true; // don't process any more keys if dialog is up
  222. if (arg.key == OIS::KC_F) // toggle visibility of advanced frame stats
  223. {
  224. mTrayMgr->toggleAdvancedFrameStats();
  225. }
  226. else if (arg.key == OIS::KC_G) // toggle visibility of even rarer debugging details
  227. {
  228. if (mDetailsPanel->getTrayLocation() == OgreBites::TL_NONE)
  229. {
  230. mTrayMgr->moveWidgetToTray(mDetailsPanel, OgreBites::TL_TOPRIGHT, 0);
  231. mDetailsPanel->show();
  232. }
  233. else
  234. {
  235. mTrayMgr->removeWidgetFromTray(mDetailsPanel);
  236. mDetailsPanel->hide();
  237. }
  238. }
  239. else if (arg.key == OIS::KC_T) // cycle polygon rendering mode
  240. {
  241. Ogre::String newVal;
  242. Ogre::TextureFilterOptions tfo;
  243. unsigned int aniso;
  244. switch (mDetailsPanel->getParamValue(9).asUTF8()[0])
  245. {
  246. case 'B':
  247. newVal = "Trilinear";
  248. tfo = Ogre::TFO_TRILINEAR;
  249. aniso = 1;
  250. break;
  251. case 'T':
  252. newVal = "Anisotropic";
  253. tfo = Ogre::TFO_ANISOTROPIC;
  254. aniso = 8;
  255. break;
  256. case 'A':
  257. newVal = "None";
  258. tfo = Ogre::TFO_NONE;
  259. aniso = 1;
  260. break;
  261. default:
  262. newVal = "Bilinear";
  263. tfo = Ogre::TFO_BILINEAR;
  264. aniso = 1;
  265. }
  266. Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(tfo);
  267. Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(aniso);
  268. mDetailsPanel->setParamValue(9, newVal);
  269. }
  270. else if (arg.key == OIS::KC_R) // cycle polygon rendering mode
  271. {
  272. Ogre::String newVal;
  273. Ogre::PolygonMode pm;
  274. switch (mCamera->getPolygonMode())
  275. {
  276. case Ogre::PM_SOLID:
  277. newVal = "Wireframe";
  278. pm = Ogre::PM_WIREFRAME;
  279. break;
  280. case Ogre::PM_WIREFRAME:
  281. newVal = "Points";
  282. pm = Ogre::PM_POINTS;
  283. break;
  284. default:
  285. newVal = "Solid";
  286. pm = Ogre::PM_SOLID;
  287. }
  288. mCamera->setPolygonMode(pm);
  289. mDetailsPanel->setParamValue(10, newVal);
  290. }
  291. else if(arg.key == OIS::KC_F5) // refresh all textures
  292. {
  293. Ogre::TextureManager::getSingleton().reloadAll();
  294. }
  295. else if (arg.key == OIS::KC_SYSRQ) // take a screenshot
  296. {
  297. mWindow->writeContentsToTimestampedFile("screenshot", ".jpg");
  298. }
  299. else if (arg.key == OIS::KC_ESCAPE)
  300. {
  301. mShutDown = true;
  302. }
  303. mCameraMan->injectKeyDown(arg);
  304. return true;
  305. }
  306. bool Base::keyReleased( const OIS::KeyEvent &arg )
  307. {
  308. mCameraMan->injectKeyUp(arg);
  309. return true;
  310. }
  311. bool Base::mouseMoved( const OIS::MouseEvent &arg )
  312. {
  313. if (mTrayMgr->injectMouseMove(arg)) return true;
  314. mCameraMan->injectMouseMove(arg);
  315. return true;
  316. }
  317. bool Base::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
  318. {
  319. if (mTrayMgr->injectMouseDown(arg, id)) return true;
  320. mCameraMan->injectMouseDown(arg, id);
  321. return true;
  322. }
  323. bool Base::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
  324. {
  325. if (mTrayMgr->injectMouseUp(arg, id)) return true;
  326. mCameraMan->injectMouseUp(arg, id);
  327. return true;
  328. }
  329. //Adjust mouse clipping area
  330. void Base::windowResized(Ogre::RenderWindow* rw)
  331. {
  332. unsigned int width, height, depth;
  333. int left, top;
  334. rw->getMetrics(width, height, depth, left, top);
  335. const OIS::MouseState &ms = mMouse->getMouseState();
  336. ms.width = width;
  337. ms.height = height;
  338. }
  339. //Unattach OIS before window shutdown (very important under Linux)
  340. void Base::windowClosed(Ogre::RenderWindow* rw)
  341. {
  342. //Only close for window that created OIS (the main window in these demos)
  343. if( rw == mWindow )
  344. {
  345. if( mInputManager )
  346. {
  347. mInputManager->destroyInputObject( mMouse );
  348. mInputManager->destroyInputObject( mKeyboard );
  349. OIS::InputManager::destroyInputSystem(mInputManager);
  350. mInputManager = 0;
  351. }
  352. }
  353. }