History: Ogre Wiki Tutorial Framework
Source of version: 83
- «
- »
Copy to clipboard
{DIV(class="Layout_box6")}{CUT(colsize=10%|70%)}{IMG(fileId="1506",imalign="left",link="Ogre Wiki Tutorial Framework",title="Ogre Wiki Tutorial Framework")}{IMG}
---
{DIV(class="bigBold")}((Ogre Wiki Tutorial Framework)){DIV}
This small ''framework'' is meant to be a common base for the tutorials, the various ''((Setting Up An Application))'' instructions, etc.%clear%
The idea is that if the same framework is used across all platforms/compilers, the differences and similarities will become much easier to spot.
{CUT}{DIV}
!Quick Download
__For Ogre 1.9 ("Ghadamon")__
{ATTACH(id="204",icon="1",showdesc="1")}{ATTACH}
{ATTACH(id="203",icon="1",showdesc="1")}{ATTACH}%clear%
__For Ogre 1.10 ("Xalafu")__
{ATTACH(id="207",icon="1",showdesc="1")}{ATTACH}
{ATTACH(id="206",icon="1",showdesc="1")}{ATTACH}%clear%
{TRANSCLUDE(page="infodiv")}If you're only interested in grabbing and using the Ogre Wiki Tutorial Framework, you can safely skip reading the rest of this page.
Visit the ((Setting Up An Application)) page for how to make use of the framework.
Read on to learn the details of the framework or come back later to do so.{TRANSCLUDE}
!Introduction
This page will walk you through the Ogre Wiki Tutorial Framework and try to explain the logic behind it, as well as point out the alternatives.
After reading this page you should have a fairly good understanding of how it works and why it works.
{maketoc}
!Motivation
Using a common framework for the official tutorials and the instructions for setting up your Ogre applications on various platforms / tool sets means that you can set up your environment and immediately start doing the tutorials without changing your setup.
The "Ogre AppWizard" for Visual Studio and Code::Blocks and the build your own projects using CMake is using the framework as well, providing you with a quick way to get started learning Ogre.
---
!Overview
The framework consists of two classes: ''BaseApplication'' and ''TutorialApplication''.
''BaseApplication'' is a pure virtual class because it has a pure virtual function ''createScene()''.
It cannot be instantiated on its own and is meant to be used as a base class.
''TutorialApplication'' is derived from ''BaseApplication'', and thus needs to implement the ''createScene()'' function.
---
!How It Works
''BaseApplication'' acts as five listeners in one class:
{CODE(wrap="1", colors="c++")}class BaseApplication : public Ogre::FrameListener, public Ogre::WindowEventListener, public OIS::KeyListener, public OIS::MouseListener, OgreBites::SdkTrayListener{CODE}
#The app is a ''FrameListener'' which means that it is able to execute some code each frame by means of the ''FrameListener'' callback functions: ''frameStarted()'', ''frameRenderingQueued()'' and ''frameEnded()''. See ((Basic Tutorial 4|#FrameListeners|Basic Tutorial 4)) for more on ''FrameListeners''.
#It is a ''WindowEventListener'', because it wants to be notified when the window is moved/resized and hook into the window closed event.
#The ((OIS|OIS library)) is used for input handling. Our class listens to Key events and Mouse events through the two OIS listeners (''KeyListener'' and ''MouseListener'').
#The Ogre Wiki Tutorial Framework also uses ((OgreBites)) for ((SdkTrays|GUI widgets)) and ((SdkCameraMan|camera handling)). It uses it because OgreBites - the Ogre sample framework - is included in every Ogre SDK and because it simplifies things a lot. Don't worry, though: How to replace OgreBites with your own GUI code or camera handling will be covered in the ((Tutorials|Ogre tutorials)). The ''SdkTrayListener'' is called each frame to kick off OgreBites GUI events.
{TRANSCLUDE(page="infodiv")}We will not walk you through the various listener callback functions in this guide.
It will be covered in the tutorials. Look at them, or use the API docs, the documentation for OIS or the OgreBites pages.{TRANSCLUDE}
!!Chain Of Command
# A new ''Ogre::Root'' is created.
# Resource locations are read from a config file.
# The configure dialogue is shown allowing you to choose render system, screen resolution, etc.
# Scene manager is created.
# Camera is created and set up.
# The OgreBites camera manager is hooked up with the camera.
# A viewport is created and set up.
# All resource groups are initialised.
# Scene is created.
# OIS is initialised and set up.
# Application asks to be registered as a Mouse and Keyboard listener.
# The application registers itself as a listener to window events (closed/resized).
# OgreBites SDKTrays widgets are set up, including the debug overlays.
# The application registers itself as a listener to frame events.
# The render loop starts, and continues to run until the application is quit.
!Walkthrough
As a means of walking you through the Ogre Wiki Tutorial Framework, let's see if we can create three alternative frameworks.
These serve to illustrate how the framework works, because the functionality is the same, only organised differently.
!!MinimalOgre
The biggest complaint about the Ogre Wiki Tutorial Framework could be that all the details are too hidden away. For example ''TutorialApplication'' inherits from ''BaseApplication''...it's too oblique. Why not just put everything into one class?
Alright, here's '''MinimalOgre''':
((MinimalOgre-h))
((MinimalOgre-cpp))
It's identical to the Ogre Wiki Tutorial Framework, but it's only one class, and it has only one function: ''go()''.
Take a good look at that function, because it does ''__exactly__'' the same as what the Ogre Wiki Tutorial Framework - and that's it.
Comments indicate what function of the Ogre Wiki Tutorial Framework the code relates to:
{CODE(wrap="1", colors="c++")}
bool MinimalOgre::go(void)
{
#ifdef _DEBUG
mResourcesCfg = "resources_d.cfg";
mPluginsCfg = "plugins_d.cfg";
#else
mResourcesCfg = "resources.cfg";
mPluginsCfg = "plugins.cfg";
#endif
// construct Ogre::Root
mRoot = new Ogre::Root(mPluginsCfg);
//-------------------------------------------------------------------------------------
// setup resources
// Load resource paths from config file
Ogre::ConfigFile cf;
cf.load(mResourcesCfg);
// Go through all sections & settings in the file
Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
Ogre::String secName, typeName, archName;
while (seci.hasMoreElements())
{
secName = seci.peekNextKey();
Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i)
{
typeName = i->first;
archName = i->second;
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
archName, typeName, secName);
}
}
//-------------------------------------------------------------------------------------
// configure
// Show the configuration dialog and initialise the system
// You can skip this and use root.restoreConfig() to load configuration
// settings if you were sure there are valid ones saved in ogre.cfg
if(mRoot->restoreConfig() || mRoot->showConfigDialog())
{
// If returned true, user clicked OK so initialise
// Here we choose to let the system create a default rendering window by passing 'true'
mWindow = mRoot->initialise(true, "MinimalOgre Render Window");
}
else
{
return false;
}
//-------------------------------------------------------------------------------------
// choose scenemanager
// Get the SceneManager, in this case a generic one
mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
//-------------------------------------------------------------------------------------
// initialize the OverlaySystem (changed for 1.9)
mOverlaySystem = new Ogre::OverlaySystem();
mSceneMgr->addRenderQueueListener(mOverlaySystem);
//-------------------------------------------------------------------------------------
// create camera
// Create the camera
mCamera = mSceneMgr->createCamera("PlayerCam");
// Position it at 500 in Z direction
mCamera->setPosition(Ogre::Vector3(0,0,80));
// Look back along -Z
mCamera->lookAt(Ogre::Vector3(0,0,-300));
mCamera->setNearClipDistance(5);
mCameraMan = new OgreBites::SdkCameraMan(mCamera); // create a default camera controller
//-------------------------------------------------------------------------------------
// create viewports
// Create one viewport, entire window
Ogre::Viewport* vp = mWindow->addViewport(mCamera);
vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
// Alter the camera aspect ratio to match the viewport
mCamera->setAspectRatio(
Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
//-------------------------------------------------------------------------------------
// Set default mipmap level (NB some APIs ignore this)
Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
//-------------------------------------------------------------------------------------
// Create any resource listeners (for loading screens)
//createResourceListener();
//-------------------------------------------------------------------------------------
// load resources
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
//-------------------------------------------------------------------------------------
// Create the scene
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);
//-------------------------------------------------------------------------------------
//create FrameListener
Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
OIS::ParamList pl;
size_t windowHnd = 0;
std::ostringstream windowHndStr;
mWindow->getCustomAttribute("WINDOW", &windowHnd);
windowHndStr << windowHnd;
pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
mInputManager = OIS::InputManager::createInputSystem( pl );
mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, true ));
mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject( OIS::OISMouse, true ));
mMouse->setEventCallback(this);
mKeyboard->setEventCallback(this);
//Set initial mouse clipping size
windowResized(mWindow);
//Register as a Window listener
Ogre::WindowEventUtilities::addWindowEventListener(mWindow, this);
mInputContext.mKeyboard = mKeyboard;
mInputContext.mMouse = mMouse;
mTrayMgr = new OgreBites::SdkTrayManager("InterfaceName", mWindow, mInputContext, this);
mTrayMgr->showFrameStats(OgreBites::TL_BOTTOMLEFT);
mTrayMgr->showLogo(OgreBites::TL_BOTTOMRIGHT);
mTrayMgr->hideCursor();
// create a params panel for displaying sample details
Ogre::StringVector items;
items.push_back("cam.pX");
items.push_back("cam.pY");
items.push_back("cam.pZ");
items.push_back("");
items.push_back("cam.oW");
items.push_back("cam.oX");
items.push_back("cam.oY");
items.push_back("cam.oZ");
items.push_back("");
items.push_back("Filtering");
items.push_back("Poly Mode");
mDetailsPanel = mTrayMgr->createParamsPanel(OgreBites::TL_NONE, "DetailsPanel", 200, items);
mDetailsPanel->setParamValue(9, "Bilinear");
mDetailsPanel->setParamValue(10, "Solid");
mDetailsPanel->hide();
mRoot->addFrameListener(this);
//-------------------------------------------------------------------------------------
mRoot->startRendering();
return true;
}
{CODE}
Well, it ''__does__'' actually have more than one function, but those are overridden listener functions, because Minimal Ogre is still 5 listeners rolled into one.
To keep things simple.
!!!Pros
For use in your own projects, this is probably the way to go as you don't have to worry about whether you should override the functions of the base class in your derived class, or if you should change the functions directly in the base class.
There's generally no need to be subclassing as the framework is copied from project to project.
So let's skip one additional layer of abstraction.
!!!Cons
As a Tutorial Framework, this is not the way to go.
To illustrate different aspects of Ogre, it works better if the common boiler plate code - which is going to be pretty much the same from tutorial to tutorial - is tucked away in a base class.
It's much easier to deal with changes by overriding functions, instead of telling you to modify that piece of code and then some code here and some lines there.
What's good for your own projects is bad for a tutorial framework.
Having everything in one class doesn't help illustrate certain aspects of Ogre programming. The other code '''gets in the way'''.
!!TinyOgre
'''It's still too much code! I want less!'''
((TinyOgre-h))
((TinyOgre-cpp))
Tiny Ogre doesn't inherit anything.
It's not using OIS for input, it doesn't listen to frame events or window events - it's about as scraped as an Ogre application can be:
{CODE(wrap="1", colors="c++")}bool TinyOgre::go(void)
{
#ifdef _DEBUG
mResourcesCfg = "resources_d.cfg";
mPluginsCfg = "plugins_d.cfg";
#else
mResourcesCfg = "resources.cfg";
mPluginsCfg = "plugins.cfg";
#endif
// construct Ogre::Root
mRoot = new Ogre::Root(mPluginsCfg);
//-------------------------------------------------------------------------------------
// set up resources
// Load resource paths from config file
Ogre::ConfigFile cf;
cf.load(mResourcesCfg);
// Go through all sections & settings in the file
Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
Ogre::String secName, typeName, archName;
while (seci.hasMoreElements())
{
secName = seci.peekNextKey();
Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i)
{
typeName = i->first;
archName = i->second;
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
archName, typeName, secName);
}
}
//-------------------------------------------------------------------------------------
// configure
// Show the configuration dialog and initialise the system
// You can skip this and use root.restoreConfig() to load configuration
// settings if you were sure there are valid ones saved in ogre.cfg
if(mRoot->restoreConfig() || mRoot->showConfigDialog())
{
// If returned true, user clicked OK so initialise
// Here we choose to let the system create a default rendering window by passing 'true'
mWindow = mRoot->initialise(true, "TinyOgre Render Window");
}
else
{
return false;
}
//-------------------------------------------------------------------------------------
// choose scenemanager
// Get the SceneManager, in this case a generic one
mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
//-------------------------------------------------------------------------------------
// create camera
// Create the camera
mCamera = mSceneMgr->createCamera("PlayerCam");
// Position it at 500 in Z direction
mCamera->setPosition(Ogre::Vector3(0,0,80));
// Look back along -Z
mCamera->lookAt(Ogre::Vector3(0,0,-300));
mCamera->setNearClipDistance(5);
//-------------------------------------------------------------------------------------
// create viewports
// Create one viewport, entire window
Ogre::Viewport* vp = mWindow->addViewport(mCamera);
vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
// Alter the camera aspect ratio to match the viewport
mCamera->setAspectRatio(
Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
//-------------------------------------------------------------------------------------
// Set default mipmap level (NB some APIs ignore this)
Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
//-------------------------------------------------------------------------------------
// Create any resource listeners (for loading screens)
//createResourceListener();
//-------------------------------------------------------------------------------------
// load resources
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
//-------------------------------------------------------------------------------------
// Create the scene
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);
//-------------------------------------------------------------------------------------
while(true)
{
// Pump window messages for nice behaviour
Ogre::WindowEventUtilities::messagePump();
if(mWindow->isClosed())
{
return false;
}
// Render a frame
if(!mRoot->renderOneFrame()) return false;
}
// We should never be able to reach this corner
// but return true to calm down our compiler
return true;
}
{CODE}
Please notice that you have to exit the application by closing the window '''manually''' - there's no input.
!!!Pros
This code is not going to get in your way. :)
There's simply too little of it.
As few assumptions as possible.
But: it does illustrate the minimum amount of code required to get Ogre up and rendering.
!!!Cons
It does almost nothing, so you need to hook it up with input, listeners, etc.
This is not suited for a tutorial framework as there would be too much repetition.
!!LowLevelOgre
'''I think you're hiding too much by relying on configuration files!'''
You're right: '''resources.cfg''', '''plugins.cfg''' and '''ogre.cfg''' are not really a part of Ogre.
It's an Ogre Samples convenience thing.
We can do this in code:
((LowLevelOgre-h))
((LowLevelOgre-cpp))
!!!Setup
This piece of code constructs a new Ogre::Root, telling it to not use any plugins or config files, because we'll be setting it up in code.
{CODE(wrap="1", colors="c++")}
// construct Ogre::Root : no plugins filename, no config filename, using a custom log filename
mRoot = new Ogre::Root("", "", "LowLevelOgre.log");
// A list of required plugins
Ogre::StringVector required_plugins;
required_plugins.push_back("GL RenderSystem");
required_plugins.push_back("Octree & Terrain Scene Manager");
// List of plugins to load
Ogre::StringVector plugins_toLoad;
plugins_toLoad.push_back("RenderSystem_GL");
plugins_toLoad.push_back("Plugin_OctreeSceneManager");
// Load the OpenGL RenderSystem and the Octree SceneManager plugins
for (Ogre::StringVector::iterator j = plugins_toLoad.begin(); j != plugins_toLoad.end(); j++)
{
#ifdef _DEBUG
mRoot->loadPlugin(*j + Ogre::String("_d"));
#else
mRoot->loadPlugin(*j);
#endif;
}
// Check if the required plugins are installed and ready for use
// If not: exit the application
Ogre::Root::PluginInstanceList ip = mRoot->getInstalledPlugins();
for (Ogre::StringVector::iterator j = required_plugins.begin(); j != required_plugins.end(); j++)
{
bool found = false;
// try to find the required plugin in the current installed plugins
for (Ogre::Root::PluginInstanceList::iterator k = ip.begin(); k != ip.end(); k++)
{
if ((*k)->getName() == *j)
{
found = true;
break;
}
}
if (!found) // return false because a required plugin is not available
{
return false;
}
}
{CODE}
One additional bonus feature of this code is that it checks the loaded plugins against the required list of plugins and exits if a required plugin is missing.
Here's a list of the plugin names and the render system names:
{CODE(wrap="1")}BSPSceneManager PluginName = "BSP Scene Manager"
OgreCgPlugin PluginName = "Cg Program Manager"
OgreOctreePlugin PluginName = "Octree & Terrain Scene Manager"
OctreeZone PluginName = "Octree Zone Factory"
OgreParticleFXPlugin PluginName = "ParticleFX"
PCZSceneManager PluginName = "Portal Connected Zone Scene Manager"
Direct3D10 PluginName = "D3D10 RenderSystem"
OgreD3D11Plugin PluginName = "D3D11 RenderSystem"
OgreD3D9Plugin PluginName = "D3D9 RenderSystem"
OgreGLPlugin PluginName = "GL RenderSystem"
OgreGLESPlugin PluginName = "OpenGL ES 1.x RenderSystem"
OgreD3D10RenderSystem Name = "Direct3D10 Rendering Subsystem"
Direct3D11 Name = "Direct3D11 Rendering Subsystem"
Direct3D9 Name = "Direct3D9 Rendering Subsystem"
GL Name = "OpenGL Rendering Subsystem"
GLES Name = "OpenGL ES 1.x Rendering Subsystem"
{CODE}
In later builds of OGRE the Octree plugin name has been changed in 2010 (1.7+) to no longer incorporate the Terrain element.
{CODE(wrap="1")}
OgreOctreePlugin PluginName = "Octree Scene Manager"
{CODE}
!!!Resources
This one is almost too easy to do in code:
{CODE(wrap="1", colors="c++")}
// setup resources
// Only add the minimally required resource locations to load up the Ogre head mesh
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../../media/materials/programs", "FileSystem", "General");
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../../media/materials/scripts", "FileSystem", "General");
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../../media/materials/textures", "FileSystem", "General");
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../../media/models", "FileSystem", "General");
{CODE}
!!!Configure
We have hardcoded ourselves to using the OpenGL Rendering Subsystem, with a windowed mode in 800x600 resolution with vsync turned off:
{CODE(wrap="1", colors="c++")}
// configure
// Grab the OpenGL RenderSystem, or exit
Ogre::RenderSystem* rs = mRoot->getRenderSystemByName("OpenGL Rendering Subsystem");
if(!(rs->getName() == "OpenGL Rendering Subsystem"))
{
return false; //No RenderSystem found
}
// configure our RenderSystem
rs->setConfigOption("Full Screen", "No");
rs->setConfigOption("VSync", "No");
rs->setConfigOption("Video Mode", "800 x 600 @ 32-bit");
mRoot->setRenderSystem(rs);
mWindow = mRoot->initialise(true, "LowLevelOgre Render Window");
{CODE}
!!!Pros
It's much clearer what's happening as there isn't an additional layer of logic.
Nothing is fetched from the outside.
!!!Cons
Each and every change requires you to change the code and recompile.
It's literally hard-coded.
!The Framework
Pros and Cons of the Ogre Wiki Tutorial Framework.
!!Pros
The modularity of the framework - base class, derived class - makes it obvious what is changed/customised.
It's also easier to walk through as it's neatly organised in touched (overridden) functions and un-touched functions.
!!Cons
If you're not careful, you won't see the tucked away code (much).
Hopefully, the progression of the Ogre Tutorials will make sure that you get around all corners of the small framework.
!Conclusion
You are still thinking to yourself:
'''Why are you hiding so much functionality'''?
The answer is:
Ogre is abstracting away those nitty gritty details so you don't have to worry about them.
If you really want to learn the low level basics of 3D programming, look into nitty OpenGL and/or gritty DirectX.
Honest. :)
The tutorial framework is not really hiding anything not already hidden by Ogre itself.
Sure, you can do some tricks using some lower level Ogre API, but that is something for the intermediate tutorials, in-depth articles and the snippets.
{TRANSCLUDE(page="geekbox")}
It's a good idea to study Ogre::Root!
It's a façade class, which means that everything Ogre::Root does, you can do yourself.
Ogre::Root is more or less a convenience class which does some housekeeping for you.{TRANSCLUDE}
!!The Files
{toc}
!The Downloads
!!Ogre Wiki Tutorial Framework
__For Ogre 1.9 ("Ghadamon")__
{ATTACH(id="204",icon="1",showdesc="1")}{ATTACH}
{ATTACH(id="203",icon="1",showdesc="1")}{ATTACH}%clear%
__For Ogre 1.10 ("Xalafu")__
{ATTACH(id="207",icon="1",showdesc="1")}{ATTACH}
{ATTACH(id="206",icon="1",showdesc="1")}{ATTACH}%clear%
!!Minimal Ogre
{ATTACH(id="187",icon="1",showdesc="1")}{ATTACH}%clear%
{ATTACH(id="190",icon="1",showdesc="1")}{ATTACH}
!!Tiny Ogre
{ATTACH(id="69",icon="1",showdesc="1")}{ATTACH}%clear%
{ATTACH(id="173",icon="1",showdesc="1")}{ATTACH}
!!Low Level Ogre
{ATTACH(id="67",icon="1",showdesc="1")}{ATTACH}%clear%
{ATTACH(id="126",icon="1",showdesc="1")}{ATTACH}