Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Pre-requisite:

  1. OGRE3D Base Environment Setup (Windows)
  2. Object-Oriented Programming

Objectives:

  1. Refactor the ogre head and pack it into an object

Descriptions:

  1. Use Ogre AppWizard to create a new OGRE project
  2. in the createScene method of your OgreApp

    Code Block
    languagecpp
    titleCreating the ogrehead and the ambient light
    linenumberstrue
     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);
    }
  3. Read the code : to display a 3D object, there needs to be an Entity and a Node
    1. A node can contain multiple entities.
    2. Entity cannot have the same name.
    3. Node can be unmaned, but it will be hard to retrieve.
    4. Node with same name is also not allowed.
  4. What we need is to put the ogrehead code into our custom class.
    1. Add file "HeadEntity.h" and "HeadEntity.cpp"
    2. 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)
    3. Let's assume we only create one entity for each node for manipulation this time.
  5. Code into both files:
    1. header

      Code Block
      languagecpp
      titleHeadEntity.h
      linenumberstrue
      // 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_
    2. cpp

      Code Block
      languagecpp
      titleHeadEntity.cpp
      linenumberstrue
      #include "HeadEntity.h"
      HeadEntity::HeadEntity(){
      }
      HeadEntity::~HeadEntity(){
      }
  6. Now what we want to refactoring into class are:
    1. The code that create the Ogrehead

      Code Block
      languagecpp
      titleNeed refactor
      linenumberstrue
          Ogre::Entity* ogreHead = mSceneMgr->createEntity("Head", "ogrehead.mesh");
          Ogre::SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
          headNode->attachObject(ogreHead);