OGRE教程六代码注释(第二版)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* Basic Ogre life cycle: 2 * 1. Create Root Object. 3 * 2. Define the resources that Ogre will use. 4 * 3. Choose and setup the RenderSystem. 5 * 4. Create the RenderWindow. 6 * 5. Initialise the resources that you are going to use. 7 * 6. Create a scene using those resources. 8 * 7. Set up any third party libraries and plugins. 9 * 8. Create any number of frame listeners. 10 * 9. Start the render loop. 11 * 12 * 以下是按步骤实现的部分代码。 13 */ 14 15 Ogre::String mPluginsCfg = "plugins_d.cfg"; 16 Ogre::Root mRoot = new Ogre::Root(mPluginsCfg); 17 18 Ogre::String mResourcesCfg = "resources_d.cfg"; 19 Ogre::ConfigFile cf; 20 cf.load(mResourcesCfg); 21 22 // 你会在弹出的配置对话框中选择渲染系统,是否全屏等。 23 if(!(mRoot->restoreConfig() || mRoot->showConfigDialog()) 24 {return false;} 25 26 // 你可以用 WIN32 的 API 自己创建窗口,不过还是用 OGRE 封装的比较好。 27 Ogre::RenderWindow *mWindow = mRoot->initialise(true, "Game Render Window"); 28 29 // 两层 for 循环,即用所谓的迭代器完成资源文件的定位,然后添加进 OGRE 资源管理器,最后初始化 30 Ogre::ConfigFile::Interator seci = cf.getSectionInterator(); 31 Ogre::ConfigFile::SettingsMultiMap *settings; 32 Ogre::ConfigFile::SettingsMultiMap::Interator *i; 33 Ogre::String secName, typeName, archName; 34 while(!seci.hasMoreElement()) 35 { 36 secName = seci.peekNextKey(); 37 settings = seci.getNext(); 38 for(i=settings.begin(); i !=settings->end(); ++i) 39 { 40 typeName = i->first; 41 archName = i->second; 42 Ogre::ResourceManager::getSingleton().addResourceLocation(archName, typeName, secName); 43 } 44 } 45 Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5); 46 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); 47 48 // 创建场景、相机、观察点、怪物头像、场景节点、灯光,完成最简单场景的布置。 49 Ogre::SceneManager *mSceneMgr = mRoot->createSceneManager("defaultSceneManager"); 50 mSceneMgr->setAmbientLight(Ogre::Colour(0.5,0.5,0.5)); 51 Ogre::Camera *mCamera = mSceneMgr->createCamera("defaultCamera"); 52 mCamera->setPosition(Ogre::Vector3(0,0,80)); 53 mCamera->lookAt(Ogre::Vector3(0,0,-300)); 54 mCamera->setNearClipDistance(5); 55 mCamera->setFarClipDistance(1000); 56 Ogre::Viewport *mViewport = mWindow->addViewport("defaultViewport"); 57 mViewport->setBackgroundColour(Ogre::Colour(0,0,0)); 58 mCamera->setAspectRatio(Ogre::Real(mViewport->getActualWidth()) / Ogre::Real(mViewport->getActualHeight)); 59 Ogre::Entity *ogreHead = mSceneManager->createEntity("ogreHead", "ogrehead.mesh"); 60 Ogre::SceneNode *ogreHeadNode = mSceneManager->getRootSceneNode().createChildSceneNode("ogreHeadNode"); 61 ogreHeadNode->setPosition(Ogre::Vector3(0,0,0)); 62 ogreHeadNode->attachObject(ogreHead); 63 Ogre::Light *mLight = mSceneMgr->createLight("mainLight"); 64 mLight->setPosition(Ogre::Vector3(20,100,50)); 65 66 // 使用第三方插件 - OIS 67 Ogre::LogManager::getSingletonPtr()->logMessage("*** Initialise OIS ***"); 68 OIS::ParamList mParamList; 69 size_t windowHnd = 0; 70 std::ostringstream windowHndStr; 71 mWindow->getCustomAttribute("WINDOW", &windowHnd); 72 windowHndStr << windowHnd; 73 mParamList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); 74 OIS::InputManager *mInputManager = OIS::InputManger::createInputSystem(mParamList); 75 OIS::Mouse *mMouse = static_cast<OIS::Mouse *>(mInputManger->createInputObject(OIS::OISMouse, false)); 76 OIS::Keyboard *mKeyboard = static_cast<OIS::Keyboard *>(mInputManger->createInputObject(OIS::OISKeyboard, false)); 77 78 // 让本程序(this)接收消息,消息处理程序需要单独写。 79 mRoot->addFrameListener(this); 80 81 // startRendering 是 Root 对象一个简单实现的方法 - 在无限循环中 renderOneFrame()。 82 mRoot->startRendering(); 83