Using the previously applied code we are now going to create a class to separate the Ogre code from the main function.
MyApplication
, which has two private pointers, one to a Ogre 3D SceneManager
and the other to the Root
class:class MyApplication { private: Ogre::SceneManager* _sceneManager; Ogre::Root* _root;
public:
loadResources()
function, which loads the resources.cfg
configuration file:void loadResources() { Ogre::ConfigFile cf; cf.load(«resources_d.cfg»);
Ogre::ConfigFile::SectionIterator sectionIter = cf.getSectionIterator(); Ogre::String sectionName, typeName, dataname; while (sectionIter.hasMoreElements()) {
sectionName = sectionIter.peekNextKey(); Ogre::ConfigFile::SettingsMultiMap *settings = sectionIter.getNext(); Ogre::ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i) { typeName = i->first; dataname = i->second; Ogre::ResourceGroupManager::getSingleton().addResourceLocation( dataname, typeName, sectionName); } } Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); }
startup()
function, which creates an Ogre 3D root
class instance using the plugins.cfg:
int startup() { _root = new Ogre::Root(«plugins_d.cfg»);
-1
to close the application:if(!_root->showConfigDialog()) { return -1; }
RenderWindow
and the SceneManager:
Ogre::RenderWindow* window = _root->initialise(true,"Ogre3D Beginners Guide"); _sceneManager = root->createSceneManager(Ogre::ST_GENERIC);
camera
and a viewport:
Ogre::Camera* camera = _sceneManager->createCamera("Camera"); camera->setPosition(Ogre::Vector3(0,0,50)); camera->lookAt(Ogre::Vector3(0,0,0)); camera->setNearClipDistance(5); Ogre::Viewport* viewport = window->addViewport(camera); viewport->setBackgroundColour(Ogre::ColourValue(0.0,0.0,0.0)); camera->setAspectRatio(Ogre::Real(viewport->getActualWidth())/ Ogre::Real(viewport->getActualHeight()));
loadResources(); createScene(); _root->startRendering(); return 0;
createScene()
function, which contains the code for creating the SceneNode
and the Entity:
void createScene() { Ogre::Entity* ent = _sceneManager->createEntity(«Sinbad.mesh»); _sceneManager->getRootSceneNode()->attachObject(ent); }
MyApplication() { _sceneManager = NULL; _root = NULL; }
root
instance when our application instance is destroyed, so implement a destructor which does this:~MyApplication() { delete _root; }
int main (void) { MyApplication app; app.startup(); return 0; }
We refactored our starting codebase so that different functionalities are better organized. We also added a destructor so our created instances would be deleted when our application is closed. One problem is that our destructor won't be called; because startup()
never returns, there is no way to close our application. We need to add a FrameListener
to tell Ogre 3D to stop rendering.
3.138.106.233