OGRE Hello World!

In this tutorial, I will teach you how to create an empty window with OGRE graphics engine and C/C++ language. Unlike any other tutorials that you can find on the internet, this tutorial does not rely on ExampleApplication.h and therefore you will be able to learn OGRE completely from scratch.

At the end of this tutorial, you will be able to create an empty window similar to this:

It’s actually not that complicated to code from scratch, just take a look at the full source code below and you will realize that it’s actually pretty simple to understand even for beginners.

#include <Ogre.h>
using namespace Ogre;

int main()
{
    Root *root = new Root("plugins.cfg", "mygame.cfg", "mygame.log");
    root->showConfigDialog();

    root->initialise(true, "My Game");
    RenderWindow *window = root->getAutoCreatedWindow();

    SceneManager *smgr = root->createSceneManager(ST_GENERIC, "SceneManager");

    Camera *cam = smgr->createCamera("MainCamera");

    Viewport *vp = window->addViewport(cam);
    vp->setBackgroundColour(ColourValue(0.3, 0.6, 0.9));

    root->startRendering();

    return 0;
}

Pretty simple isn’t it? I will now explain what the code does part-by-part. First of all, you have to include OGRE’s main header file “Ogre.h” into your project so that you can access its classes and functions for later use:

#include <Ogre.h>
using namespace Ogre;

You can also predefine its namespaces (but not a must) to make your code shorter. For example, instead of calling Ogre::Root *root = new Ogre::Root(), we can use Root *root = new Root() instead, which is much shorter and clean.

After that, create a Root class at the beginning of your main function, like so:

Root *root = new Root("plugins.cfg", "mygame.cfg", "mygame.log");

As you can see, Root requires 3 parameters during initialization: the first one being the config file that defines what plugins to load before starting the program; the second parameter is your game’s rendering config file that defines the resolution, aspect ratio, color depth, and so on which will also be loaded before your game is launched; and the last parameter is the name of the log file that will be generated whenever an error happens and it will tell you what problem had occurred.

After that we will be calling:

root->showConfigDialog();

which displays a config dialog before launching your game. The dialog window looks like this:

It lets the players change display settings before the game starts, which is quite handy especially for debugging purposes during development stages.

Next, we will initialize the Root class and define the window title by doing so:

root->initialise(true, "My Game");
RenderWindow *window = root->getAutoCreatedWindow();

We will then call root->getAutoCreatedWindow() to return the newly created render window and stores it to the a pointer called window. Please note that Ogre uses UK English, therefore you should use initialise instead of initialize.

Next, we will create a scene manager that manages your game’s scene graph (i.e. all the 3D objects in your scene) and save it to a pointer called smgr:

SceneManager *smgr = root->createSceneManager(ST_GENERIC, "SceneManager");

There are two parameters for creating a scene manager. The first parameter defines the type of scene graph you’re about to create. There are multiple types of scene managers you can choose from, such as:

  1. ST_GENERIC – Generic scene manager
  2. ST_EXTERIOR_CLOSE – Old terrain scene manager
  3. ST_EXTERIOR_FAR – Nature scene manager
  4. ST_EXTERIOR_REAL_FAR – Paging scene manager
  5. ST_INTERIOR – BSP scene manager

which each of them serves a very different and specific purpose. In this example, we use ST_GENERIC which is the generic type that works well in most cases. The second parameter simply lets you set a name for your scene manager, in this case, we called it “SceneManager”.

Next, we have the code below:

Camera *cam = smgr->createCamera("MainCamera");

Viewport *vp = window->addViewport(cam);
vp->setBackgroundColour(ColourValue(0.3, 0.6, 0.9));

which simply does the following, in ascending order:

  1. Create a camera and call it “MainCamera”.
  2. Ask the rendering window to link the newly created camera to its viewport.
  3. Set a background color for the viewport which will be used to flush the back buffer.

After everything has been setup correctly, we will now render the scene by calling startRendering() which is quite self-explanatory:

root->startRendering();

and viola! You have now successfully created an empty rendering window that runs on OGRE. That’s all for this tutorial, have fun!

One thought to “OGRE Hello World!”

  1. Hi, i believe that i saw you visited my website so i got here to go back the
    prefer?.I’m trying to find issues to enhance my site!I guess its adequate to make use of a few of your ideas!!

Leave a Reply

Your email address will not be published. Required fields are marked *