不使用SampleBrowser创建Ogre应用的框架

本文介绍了Ogre3D图形渲染引擎中BaseApplication框架的使用方法及其实现细节,该框架简化了应用程序的搭建过程,避免了复杂的监听器编写工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ogre 1.7引入了SampleBrowser,可以浏览30多个例子,无需单个编译单个浏览,可以说是一大进步。但通常我们创建的应用程序只有一个例子,不需要使用SampleBrowser;而且带有SampleBrowser的程序结构比较混乱,其实也是没有必要的。而如果采用Ogre 1.6.x版本的ExampleApplication,风格相对1.7.x来说又比较老,本身ExampleApplication结构并不好,Application和Listener分割严重,基于它写程序并不方便,貌似只有自己重新写程序框架一条路了。

其实不然,我们看中级教程的时候会发现,每个教程的例子都是基于BaseApplication的,那这个BaseApplication是什么东西呢?可以看官方的介绍。我们写程序基于这个框架就容易多了。只需继承BaseApplication,无需再操心Listener的编写,因为它已经为我们写好了。我们也不用再为了是否改写ExampleApplication而纠结,因为我们可以随意改写BaseApplication了,哈哈!由此可见,其他教材都是浮云,官网才是王道啊!

下面看看它的代码吧,详细的可从这里找。

BaseApplication.h

  1. /* 
  2. ----------------------------------------------------------------------------- 
  3. Filename:    BaseApplication.h 
  4. ----------------------------------------------------------------------------- 
  5.  
  6. This source file is part of the 
  7.    ___                 __    __ _ _    _  
  8.   /___\__ _ _ __ ___  / / /\ \ (_) | _(_) 
  9.  //  // _` | '__/ _ \ \ \/  \/ / | |/ / | 
  10. / \_// (_| | | |  __/  \  /\  /| |   <| | 
  11. \___/ \__, |_|  \___|   \/  \/ |_|_|\_\_| 
  12.       |___/                               
  13.       Tutorial Framework 
  14.       http://www.ogre3d.org/tikiwiki/ 
  15. ----------------------------------------------------------------------------- 
  16. */  
  17. #ifndef __BaseApplication_h_   
  18. #define __BaseApplication_h_   
  19.   
  20. #include <OgreCamera.h>   
  21. #include <OgreEntity.h>   
  22. #include <OgreLogManager.h>   
  23. #include <OgreRoot.h>   
  24. #include <OgreViewport.h>   
  25. #include <OgreSceneManager.h>   
  26. #include <OgreRenderWindow.h>   
  27. #include <OgreConfigFile.h>   
  28.   
  29. #include <OISEvents.h>   
  30. #include <OISInputManager.h>   
  31. #include <OISKeyboard.h>   
  32. #include <OISMouse.h>   
  33.   
  34. #include <SdkTrays.h>   
  35. #include <SdkCameraMan.h>   
  36.   
  37. class BaseApplication : public Ogre::FrameListener, public Ogre::WindowEventListener, public OIS::KeyListener, public OIS::MouseListener, OgreBites::SdkTrayListener  
  38. {  
  39. public:  
  40.     BaseApplication(void);  
  41.     virtual ~BaseApplication(void);  
  42.   
  43.     virtual void go(void);  
  44.   
  45. protected:  
  46.     virtual bool setup();  
  47.     virtual bool configure(void);  
  48.     virtual void chooseSceneManager(void);  
  49.     virtual void createCamera(void);  
  50.     virtual void createFrameListener(void);  
  51.     virtual void createScene(void) = 0; // Override me!   
  52.     virtual void destroyScene(void);  
  53.     virtual void createViewports(void);  
  54.     virtual void setupResources(void);  
  55.     virtual void createResourceListener(void);  
  56.     virtual void loadResources(void);  
  57.   
  58.     // Ogre::FrameListener   
  59.     virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt);  
  60.   
  61.     // OIS::KeyListener   
  62.     virtual bool keyPressed( const OIS::KeyEvent &arg );  
  63.     virtual bool keyReleased( const OIS::KeyEvent &arg );  
  64.     // OIS::MouseListener   
  65.     virtual bool mouseMoved( const OIS::MouseEvent &arg );  
  66.     virtual bool mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id );  
  67.     virtual bool mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id );  
  68.   
  69.     // Ogre::WindowEventListener   
  70.     //Adjust mouse clipping area   
  71.     virtual void windowResized(Ogre::RenderWindow* rw);  
  72.     //Unattach OIS before window shutdown (very important under Linux)   
  73.     virtual void windowClosed(Ogre::RenderWindow* rw);  
  74.   
  75.     Ogre::Root *mRoot;  
  76.     Ogre::Camera* mCamera;  
  77.     Ogre::SceneManager* mSceneMgr;  
  78.     Ogre::RenderWindow* mWindow;  
  79.     Ogre::String mResourcesCfg;  
  80.     Ogre::String mPluginsCfg;  
  81.   
  82.     // OgreBites   
  83.     OgreBites::SdkTrayManager* mTrayMgr;  
  84.     OgreBites::SdkCameraMan* mCameraMan;       // basic camera controller   
  85.     OgreBites::ParamsPanel* mDetailsPanel;     // sample details panel   
  86.     bool mCursorWasVisible;                    // was cursor visible before dialog appeared   
  87.     bool mShutDown;  
  88.   
  89.     //OIS Input devices   
  90.     OIS::InputManager* mInputManager;  
  91.     OIS::Mouse*    mMouse;  
  92.     OIS::Keyboard* mKeyboard;  
  93. };  
  94.   
  95. #endif // #ifndef __BaseApplication_h_  
 

BaseApplication.cpp

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值