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.

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:

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.

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.

HelloWorldApp(){}
       ~HelloWorldApp()
       {  
          Dispose();
       }
       public void Dispose(){}

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

protected void SetupResources(string sFileName)
        {
        }

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

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.

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() !

static void Main(string[] args)
        {   
        }

Here's what we've got so far:

Hello World Start Code

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:

// 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

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

FrameEnded does absolutely nothing..

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

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

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 detail about this. For now, lets just wave our hands at it wait until the part 2 to examine what's actually going on.

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.

public void Start()
        {
            mRoot.StartRendering();
        }

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

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

Disposing of Root

 

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

public void Dispose()
        {
            mRoot.Dispose();
        }

The completed code

 

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();
            }
        }
    }
 }

 


Contributors to this page: jacmoe111451 points  and OgreWikiBot .
Page last modified on Thursday 31 of December, 2009 05:39:39 GMT by jacmoe111451 points .


The content on this page is licensed under the terms of the Creative Commons Attribution-ShareAlike License.
As an exception, any source code contributed within the content is released into the Public Domain.