Skip to main content

Introduction

Well, we've gotten far enough to look at the guts of ExampleApplication and rip out the important parts and build a "hello world" application using OgreDotNet. This is the first of 2 "hello world" applications. In the first part, we'll setup a basic application which still uses the OGRE Config Dialog and Resource scripts. In the second part, we'll get rid of the config dialog and customize our resource script (hopefully).

On second thought... Part 1 isn't really a traditional hello world app, since its sort of bloated, but it's still a good starting point for creating your own application framework (which should be one of your goals)

Getting Started

OK. Let's get started. Since OGRE is a fairly complex API, our starting point is fairly large. We'll need System, System.IO, System.Drawing, Math3D and OgreDotNet.

Copy to clipboard
using System; using System.IO; using System.Drawing; using Math3D; using OgreDotNet;

There are a few objects which are needed in OGRE no matter what. If you are not familiar with any of them, take a look at the OGRE documentation... I chose to use the same variable names as the example application... Anyway, here they are:

Copy to clipboard
Root mRoot; RenderWindow mRenderWindow; SceneManager mSceneManager; Camera mCamera; Viewport mViewport; OgreDotNet.EventHandler mEventHandler; bool mDone;

I'm not 100% sure mDone is needed, but having a common variable that we can use in our delegates makes things easier. mEventHandler isn't necessary, but without it, we have no way to close the application. (unless you want a really lame ODN application that starts up then closes)

Next, since Root implements IDisposable, we'll do that too since it makes sense.

Copy to clipboard
class HelloWorldApp : IDisposable

We'll need constructor, a destructor and Dispose methods. Since we don't use any unmanaged memory, we'll call Dispose from the destructor.

Copy to clipboard
HelloWorldApp(){} ~HelloWorldApp() { Dispose(); } public void Dispose(){}

I mentioned that we're still using the resource scripts so we need a method which does that.

Copy to clipboard
protected void SetupResources(string sFileName) { }

I also decided to use the Start method since it keeps main nice & tidy.

Copy to clipboard
public void Start() { }

The next three methods are the ones we need for event handling. remember that returning false in either Frame* method kills the app. KeyClicked is here since we want a way to close our app when its running fullscreen.

Copy to clipboard
protected bool FrameStarted(FrameEvent e) { return true; } protected bool FrameEnded(FrameEvent e) { return true; } protected void KeyClicked(KeyEvent e) { }

Last, but certainly not least, we need main() !

Copy to clipboard
static void Main(string[] args) { }

Here's what we've got so far:

Hello World Start Code

Copy to clipboard
using System; using System.IO; using System.Drawing; using Math3D; using OgreDotNet; namespace OgreDotNetTutorial { class HelloWorldApp : IDisposable { //Declare & pre-initialize the required elements public Root mRoot = null; public RenderWindow mRenderWindow = null; public SceneManager mSceneManager = null; public Camera mCamera = null; public Viewport mViewport = null; public OgreDotNet.EventHandler mEventHandler = null; public bool mDone = false; //Constructor HelloWorldApp() { } public void Dispose() { } ~HelloWorldApp() { Dispose(); } protected void SetupResources(string sFileName) { } public void Start() { } protected bool FrameStarted(FrameEvent e) { return true; } protected bool FrameEnded(FrameEvent e) { return true; } protected void KeyClicked(KeyEvent e) { } static void Main(string[] args) { } } }

Initializing the required objects



We've already declared all the required objects and it makes sense to use the constructor to initialize them.. Find the constructor and lets go to work:

Copy to clipboard
// Create the Root object; mRoot = new Root(); //Setup OGRE resources (call the function which parses the resource script.) SetupResources("resources.cfg"); //Show the config Dialog if (!mRoot.ShowConfigDialog()) { mRoot.Dispose(); return; } //Initialize the RenderWindow mRenderWindow = mRoot.Initialise(true, "Hello OGRE.NET"); //Initialize the SceneManager mSceneManager = mRoot.CreateSceneManager((ushort)SceneType.Generic, "HelloOgreDontNetSceneManager"); mSceneManager.SetAmbientLight(Color.White); //Initialize the Camera mCamera = mSceneManager.CreateCamera("HelloCamera"); mCamera.SetPosition(0, 0, -200); mCamera.LookAt = new Vector3(0, 0, 0); mCamera.NearClipDistance = 5; //Initialize the Viewport mViewport = mRenderWindow.AddViewport(mCamera); mViewport.BackgroundColor = Color.Black; mCamera.AspectRatio = (float)mViewport.ActualWidth / (float)mViewport.ActualHeight; //Do some manager setup(); ResourceGroupManager.getSingleton().initialiseAllResourceGroups(); MaterialManager.Instance.SetDefaultTextureFiltering(TextureFilterOptions.TfoBilinear); MaterialManager.Instance.SetDefaultAnisotropy(1); TextureManager.Instance.SetDefaultNumMipmaps(5); //Create Eventhandler mEventHandler = new OgreDotNet.EventHandler(mRoot, mRenderWindow); mEventHandler.SubscribeEvents(); mEventHandler.FrameStarted += new FrameEventDelegate(FrameStarted); mEventHandler.FrameEnded += new FrameEventDelegate(FrameEnded); mEventHandler.KeyClicked += new KeyEventDelegate(KeyClicked); //Create the Scene Entity ent; SceneNode node; ent = mSceneManager.CreateEntity("Robot", "robot.mesh"); node = mSceneManager.GetRootSceneNode().CreateChildSceneNode("RobotNode"); node.AttachObject(ent); node.SetPosition(0, 0, 0);

A minimal set of event handlers



There's really only 3 functions which we need here and they are all really basic.
FrameStarted checks if the user wants the window closed

Copy to clipboard
protected bool FrameStarted(FrameEvent e) { if (mRenderWindow.Closed || mDone) return false; return true; }

FrameEnded does absolutely nothing..

Copy to clipboard
protected bool FrameEnded(FrameEvent e) { return true; }

KeyClicked simply looks for the escape key and sets the done variable.

Copy to clipboard
protected void KeyClicked(KeyEvent e) { switch (e.KeyCode) { case KeyCode.Escape: mDone = true; break; default: break; } }

Parsing resources.cfg



I'm not going to go into too much detail about this. This function is basically a specialized file parser.
The most interesting line of code here is

Copy to clipboard
ResourceGroupManager.getSingleton().addResourceLocation(sarchName, sLocType, secName);

The complete declaration for this function can be found in the OGRE manual:

Copy to clipboard
void addResourceLocation (const String &name, const String &locType, const String &resGroup=DEFAULT_RESOURCE_GROUP_NAME, bool recursive=false) Method to add a resource location to for a given resource group.

The basic flow of SetupResources is simple: it opens the resource file, then iterates over it line by line. The first few lines of the while loop clean up any comments (which begin with #) on the current line. Next it checks if the line indicates the start of a new group of resources. Resource groups are put into brackets like this General. If it finds a resource group, it sets the secName variable and continues to the next line in the resources file. Otherwise, it looks for an = sign and saves the strings on both sides of the equal sign and calls the addResourceLocation function using the group, type and name strings.

Copy to clipboard
protected void SetupResources(string sFileName) { //Initialiser.SetupResources(sFileName); using (StreamReader sr = new StreamReader(sFileName)) { string secName = "", sLocType, sarchName; string line; while ((line = sr.ReadLine()) != null) { int x = line.IndexOf("#"); if (x > -1) line = line.Substring(0, x); line = line.Trim(); if (line.Length > 0) { if (line[0] == '[') { secName = line.Substring(1, line.Length - 2); } else if (secName.Length > 0) { x = line.IndexOf("="); if (x <= 0) throw new Exception("Invalid line in resource file " + sFileName); sLocType = line.Substring(0, x); sarchName = line.Substring(x + 1); ResourceGroupManager.getSingleton().addResourceLocation(sarchName, sLocType, secName); } } } } }

Starting the rendering loop



Calling Root's StartRendering method kicks off the OGRE loop.

Copy to clipboard
public void Start() { mRoot.StartRendering(); }

Next, in main, we'll create our app and start it

Copy to clipboard
using(HelloWorldApp app = new HelloWorldApp()) { app.Start(); }

Disposing of Root



Since Root implements IDisposable, we need to call mRoot.Dispose() in our Dispose method

Copy to clipboard
public void Dispose() { mRoot.Dispose(); }

The completed code


Copy to clipboard
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Drawing; using Math3D; using OgreDotNet; namespace OgreDotNetTutorial { class HelloWorldApp : IDisposable { //Declare & pre-initialize the required elements public Root mRoot = null; public RenderWindow mRenderWindow = null; public SceneManager mSceneManager = null; public Camera mCamera = null; public Viewport mViewport = null; public OgreDotNet.EventHandler mEventHandler = null; public bool mDone = false; //Constructor HelloWorldApp() { // Create the Root object; mRoot = new Root(); //Setup OGRE resources SetupResources("resources.cfg"); //Show the config Dialog if (!mRoot.ShowConfigDialog()) { mRoot.Dispose(); return; } //Initialize the RenderWindow mRenderWindow = mRoot.Initialise(true, "Hello OGRE.NET"); //Initialize the SceneManager mSceneManager = mRoot.CreateSceneManager((ushort)SceneType.Generic, "HelloOgreDontNetSceneManager"); mSceneManager.SetAmbientLight(Color.White); //Initialize the Camera mCamera = mSceneManager.CreateCamera("HelloCamera"); mCamera.SetPosition(0, 0, -200); mCamera.LookAt = new Vector3(0, 0, 0); mCamera.NearClipDistance = 5; //Initialize the Viewport mViewport = mRenderWindow.AddViewport(mCamera); mViewport.BackgroundColor = Color.Black; mCamera.AspectRatio = (float)mViewport.ActualWidth / (float)mViewport.ActualHeight; //Do some "manager" setup. ResourceGroupManager.getSingleton().initialiseAllResourceGroups(); MaterialManager.Instance.SetDefaultTextureFiltering(TextureFilterOptions.TfoBilinear); MaterialManager.Instance.SetDefaultAnisotropy(1); TextureManager.Instance.SetDefaultNumMipmaps(5); //Create and Setup the event handler mEventHandler = new OgreDotNet.EventHandler(mRoot, mRenderWindow); mEventHandler.SubscribeEvents(); mEventHandler.FrameStarted += new FrameEventDelegate(FrameStarted); mEventHandler.FrameEnded += new FrameEventDelegate(FrameEnded); mEventHandler.KeyClicked += new KeyEventDelegate(KeyClicked); //Create the Scene Entity ent; SceneNode node; ent = mSceneManager.CreateEntity("Robot", "robot.mesh"); node = mSceneManager.GetRootSceneNode().CreateChildSceneNode("RobotNode"); node.AttachObject(ent); node.SetPosition(0, 0, 0); } public void Dispose() { mRoot.Dispose(); } ~HelloWorldApp() { Dispose(); } protected void SetupResources(string sFileName) { //Initialiser.SetupResources(sFileName); using (StreamReader sr = new StreamReader(sFileName)) { string secName = "", sLocType, sarchName; string line; while ((line = sr.ReadLine()) != null) { int x = line.IndexOf("#"); if (x > -1) line = line.Substring(0, x); line = line.Trim(); if (line.Length > 0) { if (line[0] == '[') { secName = line.Substring(1, line.Length - 2); } else if (secName.Length > 0) { x = line.IndexOf("="); if (x <= 0) throw new Exception("Invalid line in resource file " + sFileName); sLocType = line.Substring(0, x); sarchName = line.Substring(x + 1); ResourceGroupManager.getSingleton().addResourceLocation(sarchName, sLocType, secName); } } } } } public void Start() { mRoot.StartRendering(); } protected bool FrameStarted(FrameEvent e) { if (mRenderWindow.Closed || mDone) return false; return true; } protected bool FrameEnded(FrameEvent e) { return true; } protected void KeyClicked(KeyEvent e) { switch (e.KeyCode) { case KeyCode.Escape: mDone = true; break; default: break; } } static void Main(string[] args) { using(HelloWorldApp app = new HelloWorldApp()) { app.Start(); } } } }