最简单的Ogre系列之一——Ogre框架程序(不使用ExampleListener/Application)

本文介绍了一个基于Ogre 1.6.5版本的极简程序示例,该程序不依赖额外资源文件,适用于初学者快速上手Ogre框架。文章详细讲解了如何创建一个最小化的Ogre应用程序,包括配置文件、窗口初始化、场景管理器设置等内容。

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

原文:http://blog.youkuaiyun.com/zhuxiaoyang2000/article/details/6324080

本文参考Ogre官网上的MinimalApplication写了一个最简单的Ogre程序,不包含任何资源文件,当然了,也没有任何功能: )。本文写作的初衷是:

(1)ExampleListener/Application包含的内容过于丰富,不便于在此基础上写出简单的程序应用,如2D程序。它需要的资源文件和相应的配置文件对于简单的程序显得臃肿。

(2)现有的MinimalApplication混淆了Listener和Application,而且将所有文件写到了一个程序里面,文档结构不清晰。

(3)更重要的是通过自己写这样一个程序,能够了解Ogre建立的详细过程,有助于理解Ogre的框架。

闲言少叙,进入正题。建立一个Win32程序,程序包括三个文件,分别是SimpleListener.h,SimpleApplication.h和main.cpp。该程序使用的是Ogre 1.6.5版本,1.7.x版本可能需要修改相应的include文件。

程序需要包含库OgreMain_d.lib、OIS_d.lib(Debug模式),或OgreMain.lib、OIS.lib(Release模式);需要包含dll文件OgreMain_d.dll、OIS_d.dll、RenderSystem_Direct3D9_d.dll(Debug模式),或OgreMain.dll、OIS.dll、RenderSystem_Direct3D9.dll(Release模式)。

程序需要建立一个空文件resources.cfg。

程序需要建立一个文件Plugins.cfg,其内容为

[c-sharp]  view plain copy
  1. # Defines plugins to load  
  2. # Define plugin folder  
  3. PluginFolder=.  
  4. # Define plugins  
  5. Plugin=RenderSystem_Direct3D9_d  

下面是SimpleListener.h的内容。

[cpp]  view plain copy
  1. /* 
  2.   ====================================================================== 
  3.    SimpleListener.h --- protoype to show off the simple ogre listener. 
  4.   ---------------------------------------------------------------------- 
  5.    Author : Zhu Xiaoyang (xiaoyang.zhu@ia.ac.cn) 
  6.    Creation Date : Apr 14 2011 
  7.    Description: 
  8.    This is a mini Ogre Listener, including SimpleFrameListener, 
  9.    SimpleKeyListener and SimpleMouseListener.  
  10.   ======================================================================= 
  11. */  
  12. #ifndef __SimpleListener_H__  
  13. #define __SimpleListener_H__  
  14. #include "Ogre.h"  
  15. #include "OgreFrameListener.h"  
  16. #include <OIS/OIS.h>  
  17. using namespace Ogre;  
  18. /** 
  19. ---------------------------------------------------------------------- 
  20.     class SimpleFrameListener 
  21. ---------------------------------------------------------------------- 
  22. */  
  23. class SimpleFrameListener : public FrameListener  
  24. {  
  25. public:  
  26.     SimpleFrameListener(OIS::Keyboard* keyboard, OIS::Mouse* mouse)  
  27.     {  
  28.         mKeyboard = keyboard;  
  29.         mMouse    = mouse;  
  30.     }  
  31.     //This gets called before the next frame is being rendered.  
  32.     bool frameStarted(const FrameEvent& evt)  
  33.     {  
  34.         //update the input devices  
  35.         mKeyboard->capture();  
  36.         mMouse->capture();  
  37.         //exit if key KC_ESCAPE pressed  
  38.         if( mKeyboard->isKeyDown( OIS::KC_ESCAPE ) )  
  39.             return false;  
  40.         else   
  41.             return true;  
  42.     }  
  43.     //This gets called at the end of a frame  
  44.     bool frameEnded(const FrameEvent& evt)  
  45.     {  
  46.         return true;  
  47.     }  
  48. private:  
  49.     OIS::Keyboard* mKeyboard;  
  50.     OIS::Mouse*    mMouse;  
  51. };  
  52. /** 
  53. ---------------------------------------------------------------------- 
  54.     class SimpleKeyListener 
  55. ---------------------------------------------------------------------- 
  56. */  
  57. class SimpleKeyListener : public OIS::KeyListener  
  58. {  
  59. public:  
  60.     bool keyPressed    (const OIS::KeyEvent& e)   { return true; }  
  61.     bool keyReleased   (const OIS::KeyEvent& e)   { return true; }  
  62. };  
  63. /** 
  64. ---------------------------------------------------------------------- 
  65.     class SimpleMouseListener 
  66. ---------------------------------------------------------------------- 
  67. */  
  68. class SimpleMouseListener : public OIS::MouseListener  
  69. {  
  70. public:  
  71.     bool mouseMoved    (const OIS::MouseEvent& e) { return true; }  
  72.     bool mousePressed  (const OIS::MouseEvent& e, OIS::MouseButtonID id) { return true; }  
  73.     bool mouseReleased (const OIS::MouseEvent& e, OIS::MouseButtonID id) { return true; }  
  74. };  
  75. #endif //__SimpleListener_H__   

下面是SimpleApplication.h的内容。

[cpp]  view plain copy
  1. /* 
  2.   ========================================================================== 
  3.    SimpleApplication.h --- protoype to show off the simple ogre application. 
  4.   -------------------------------------------------------------------------- 
  5.    Author : Zhu Xiaoyang (xiaoyang.zhu@ia.ac.cn) 
  6.    Creation Date : Apr 14 2011 
  7.    Description: 
  8.    This is a mini Ogre Application.  It does noting. 
  9.   ========================================================================== 
  10. */  
  11. #ifndef __SimpleApplication_H__  
  12. #define __SimpleApplication_H__  
  13. #include "Ogre.h"  
  14. #include "SimpleListener.h"  
  15. using namespace Ogre;  
  16. /** 
  17. ---------------------------------------------------------------------- 
  18.     class SimpleApplication 
  19. ---------------------------------------------------------------------- 
  20. */  
  21. class SimpleApplication  
  22. {  
  23. public:  
  24.     SimpleApplication()  
  25.     {  
  26.         mRoot = 0;  
  27.     }  
  28.     ~SimpleApplication()  
  29.     {  
  30.         if(mRoot)  
  31.             OGRE_DELETE mRoot;  
  32.     }  
  33.     //start example  
  34.     void go()   
  35.     {  
  36.         if( !setup() )  
  37.             return;  
  38.         /** 
  39.         ---------------------------------------------------------------------- 
  40.         8 start rendering 
  41.         @ blocks until a frame listener returns false.  
  42.           eg. from pressing escape in this example. 
  43.         ---------------------------------------------------------------------- 
  44.         */  
  45.         mRoot->startRendering();  
  46.         /** 
  47.         ---------------------------------------------------------------------- 
  48.         9 clean up 
  49.         ---------------------------------------------------------------------- 
  50.         */  
  51.         destroyScene();  
  52.     }  
  53.     //setup Ogre  
  54.     bool setup()  
  55.     {  
  56.         /** 
  57.         ---------------------------------------------------------------------- 
  58.         1 Enter ogre 
  59.         ---------------------------------------------------------------------- 
  60.         */  
  61.         mRoot = new Root;  
  62.         /** 
  63.         ---------------------------------------------------------------------- 
  64.         2 Configure resource paths 
  65.         @ Load resource paths from config file 
  66.           File format is: 
  67.           [ResourceGroupName] 
  68.           ArchiveType=Path 
  69.           .. repeat 
  70.           For example: 
  71.           [General] 
  72.           FileSystem=media/ 
  73.           zip=packages/level1.zip 
  74.         ---------------------------------------------------------------------- 
  75.         */  
  76.         ConfigFile cf;  
  77.         cf.load("resources.cfg");  
  78.         //Go through all sections & settings in the file  
  79.         ConfigFile::SectionIterator seci = cf.getSectionIterator();  
  80.         String secName, typeName, archName;  
  81.         while ( seci.hasMoreElements() )  
  82.         {  
  83.             secName = seci.peekNextKey();  
  84.             ConfigFile::SettingsMultiMap  *settings = seci.getNext();  
  85.             ConfigFile::SettingsMultiMap::iterator i;  
  86.             for (i = settings->begin(); i != settings->end(); ++i)  
  87.             {  
  88.                 typeName = i->first;  
  89.                 archName = i->second;  
  90.                 ResourceGroupManager::getSingleton().addResourceLocation(  
  91.                     archName, typeName, secName);  
  92.             }  
  93.         }  
  94.         /** 
  95.         ---------------------------------------------------------------------- 
  96.         3 Configures the application and creates the window 
  97.         ---------------------------------------------------------------------- 
  98.         */  
  99.         if ( !mRoot->showConfigDialog() )  
  100.         {  
  101.             //Ogre  
  102.             delete mRoot;  
  103.             return false//Exit the application on cancel  
  104.         }  
  105.         mWindow = mRoot->initialise(true"Simple Ogre App");  
  106.         ResourceGroupManager::getSingleton().initialiseAllResourceGroups();  
  107.         /** 
  108.         ---------------------------------------------------------------------- 
  109.         4 Create the SceneManager 
  110.         @ SceneManager Type 
  111.           ST_GENERIC = octree 
  112.           ST_EXTERIOR_CLOSE = simple terrain 
  113.           ST_EXTERIOR_FAR = nature terrain (depreciated) 
  114.           ST_EXTERIOR_REAL_FAR = paging landscape 
  115.           ST_INTERIOR = Quake3 BSP 
  116.         ---------------------------------------------------------------------- 
  117.         */  
  118.         mSceneMgr = mRoot->createSceneManager(ST_GENERIC);  
  119.         /** 
  120.         ---------------------------------------------------------------------- 
  121.         5 Create the camera 
  122.         ---------------------------------------------------------------------- 
  123.         */  
  124.         mCamera = mSceneMgr->createCamera("SimpleCamera");  
  125.         /** 
  126.         ---------------------------------------------------------------------- 
  127.         6 Create one viewport, entire window 
  128.         ---------------------------------------------------------------------- 
  129.         */  
  130.         Viewport* viewport = mWindow->addViewport(mCamera);  
  131.         /** 
  132.         ---------------------------------------------------------------------- 
  133.         7 Add OIS input handling 
  134.         ---------------------------------------------------------------------- 
  135.         */  
  136.         OIS::ParamList pl;  
  137.         size_t windowHnd = 0;  
  138.         std::ostringstream windowHndStr;  
  139.         //tell OIS about the Ogre window  
  140.         mWindow->getCustomAttribute("WINDOW", &windowHnd);  
  141.         windowHndStr<<windowHnd;  
  142.         pl.insert( std::make_pair( std::string("WINDOW"), windowHndStr.str() ) );  
  143.         //setup the manager, keyboard and mouse to handle input  
  144.         inputManager = OIS::InputManager::createInputSystem( pl );  
  145.         keyboard = static_cast<OIS::Keyboard*>(inputManager->createInputObject( OIS::OISKeyboard, true ) );  
  146.         mouse    = static_cast<OIS::Mouse*>(inputManager->createInputObject( OIS::OISMouse,    true ) );  
  147.         //tell OIS about the window's dimensions  
  148.         unsigned int width, height, depth;  
  149.         int top, left;  
  150.         mWindow->getMetrics(width, height, depth, left, top);  
  151.         const OIS::MouseState &ms = mouse->getMouseState();  
  152.         ms.width  = width;  
  153.         ms.height = height;  
  154.         //everything is set up, now we listen for input and frames (replaces while loops)  
  155.         //key events  
  156.         keyListener = new SimpleKeyListener();  
  157.         keyboard->setEventCallback(keyListener);  
  158.         //mouse events  
  159.         mouseListener = new SimpleMouseListener();  
  160.         mouse->setEventCallback(mouseListener);  
  161.         //render events  
  162.         mFrameListener = new SimpleFrameListener(keyboard, mouse);  
  163.         mRoot->addFrameListener(mFrameListener);  
  164.         return true;  
  165.     }  
  166.     //clean Ogre  
  167.     void destroyScene()  
  168.     {  
  169.         //OIS  
  170.         inputManager->destroyInputObject(mouse);             mouse = 0;  
  171.         inputManager->destroyInputObject(keyboard);          keyboard = 0;  
  172.         OIS::InputManager::destroyInputSystem(inputManager); inputManager = 0;  
  173.         //listeners  
  174.         delete mFrameListener;  
  175.         delete mouseListener;  
  176.         delete keyListener;  
  177.     }  
  178. private:  
  179.     Root* mRoot;  
  180.     Camera* mCamera;  
  181.     SceneManager* mSceneMgr;  
  182.     RenderWindow* mWindow;  
  183.     //OIS   
  184.     OIS::InputManager* inputManager;  
  185.     OIS::Keyboard* keyboard;  
  186.     OIS::Mouse* mouse;  
  187.     //Listener  
  188.     SimpleKeyListener* keyListener;  
  189.     SimpleMouseListener* mouseListener;  
  190.     SimpleFrameListener* mFrameListener;  
  191. };  
  192. #endif //__SimpleApplication_H__  

 

下面是main.cpp的内容。

 

[cpp]  view plain copy
  1. #include "SimpleApplication.h"  
  2. #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32  
  3. #define WIN32_LEAN_AND_MEAN  
  4. #include "windows.h"  
  5. #endif  
  6. #ifdef __cplusplus  
  7. extern "C" {  
  8. #endif  
  9. #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32  
  10. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCELPSTR strCmdLine, INT )  
  11. #else  
  12. int main(int argc, char **argv)  
  13. #endif  
  14. {  
  15.     // Create application object  
  16.     SimpleApplication app;  
  17.     srand(time(0));  
  18.     try {  
  19.         app.go();  
  20.     } catch( Exception& e ) {  
  21. #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32  
  22.         MessageBoxA( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);  
  23. #else  
  24.         std::cerr << "An exception has occured: " << e.getFullDescription();  
  25. #endif  
  26.     }  
  27.     return 0;  
  28. }  
  29. #ifdef __cplusplus  
  30. }  
  31. #endif  

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值