...
- Use Ogre AppWizard to create a new OGRE project
in the createScene method of your OgreApp
Code Block language cpp title Creating the ogrehead and the ambient light linenumbers true void Refactoring101::createScene(void) { Ogre::Entity* ogreHead = mSceneMgr->createEntity("Head", "ogrehead.mesh"); Ogre::SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); headNode->attachObject(ogreHead); // Set ambient light mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5)); // Create a light Ogre::Light* l = mSceneMgr->createLight("MainLight"); l->setPosition(20,80,50); }
- Read the code : to display a 3D object, there needs to be an Entity and a Node
- A node can contain multiple entities.
- Entity cannot have the same name.
- Node can be unmaned, but it will be hard to retrieve.
- Node with same name is also not allowed.
- What we need is to put the ogrehead code into our custom class.
- Add file "HeadEntity.h" and "HeadEntity.cpp"
- The header file should be in <your project>\include and the cpp should be in <your project>\src (if you find error include cannot find "HeadEntity.h", it's probably because your .h file is outside include folder in the project root)
- Let's assume we only create one entity for each node for manipulation this time.
- Code into both files:
header
Code Block language cpp title HeadEntity.h linenumbers true // Include used Ogre libraries #include <OgreEntity.h> #include <OgreSceneManager.h> #ifndef _HeadEntity_h_ #define _HeadEntity_h_ class HeadEntity{ public: HeadEntity(); ~HeadEntity(); protected: Ogre::Entity* mHeadEntity; Ogre::SceneNode* mHeadNode; }; #endif //_HeadEntity_h_
cpp
Code Block language cpp title HeadEntity.cpp linenumbers true #include "HeadEntity.h" HeadEntity::HeadEntity(){ } HeadEntity::~HeadEntity(){ }
- Now what we want to refactoring into class are:
The code that create the Ogrehead
Code Block language cpp title Need refactor linenumbers true Ogre::Entity* ogreHead = mSceneMgr->createEntity("Head", "ogrehead.mesh"); Ogre::SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); headNode->attachObject(ogreHead);
- We see that the "mSceneMgr" is needed so, we overload the constructor and hide the default constructor
header
Code Block language cpp title HeadEntity.h linenumbers true // Include used Ogre libraries #include <OgreEntity.h> #include <OgreSceneManager.h> #ifndef _HeadEntity_h_ #define _HeadEntity_h_ class HeadEntity{ public: HeadEntity(Ogre::SceneManager* SceneMgr); ~HeadEntity(); protected: HeadEntity(); Ogre::SceneManager* mSceneMgr; Ogre::Entity* mHeadEntity; Ogre::SceneNode* mHeadNode; }; #endif //_HeadEntity_h_
cpp
Code Block language cpp linenumbers true #include "HeadEntity.h" HeadEntity::HeadEntity(Ogre::SceneManager* SceneMgr):mSceneMgr(SceneMgr){ // Exactly the same sequence of create Scene in OgreApp mHeadEntity = mSceneMgr->createEntity("Head", "ogrehead.mesh"); mHeadNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); mHeadNode->attachObject(mHeadEntity); } HeadEntity::HeadEntity(){ } HeadEntity::~HeadEntity(){ }
Modify your OgreApp so it uses your class instead of creating objects separately
Code Block language cpp linenumbers true #include "Refactoring101.h" #include "HeadEntity.h" //------------------------------------------------------------------------------------- Refactoring101::Refactoring101(void) { } //------------------------------------------------------------------------------------- Refactoring101::~Refactoring101(void) { } //------------------------------------------------------------------------------------- void Refactoring101::createScene(void) { // Replace this with new object from class you just created HeadEntity *OgreHead = new HeadEntity(mSceneMgr); // Set ambient light mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5)); // Create a light Ogre::Light* l = mSceneMgr->createLight("MainLight"); l->setPosition(20,80,50); }