OGRE Wiki
Support and community documentation for Ogre3D
Ogre Forums
ogre3d.org
Log in
Username:
Password:
CapsLock is on.
Remember me (for 1 year)
Log in
Home
Tutorials
Tutorials Home
Basic Tutorials
Intermediate Tutorials
Mad Marx Tutorials
In Depth Tutorials
Older Tutorials
External Tutorials
Cookbook
Cookbook Home
CodeBank
Snippets
Experiences
Ogre Articles
Libraries
Libraries Home
Alternative Languages
Assembling A Toolset
Development Tools
OGRE Libraries
List of Libraries
Tools
Tools Home
DCC Tools
DCC Tutorials
DCC Articles
DCC Resources
Assembling a production pipeline
Development
Development Home
Roadmap
Building Ogre
Installing the Ogre SDK
Setting Up An Application
Ogre Wiki Tutorial Framework
Frequently Asked Questions
Google Summer Of Code
Help Requested
Ogre Core Articles
Community
Community Home
Projects Using Ogre
Recommended Reading
Contractors
Wiki
Immediate Wiki Tasklist
Wiki Ideas
Wiki Guidelines
Article Writing Guidelines
Wiki Styles
Wiki Page Tracker
Ogre Wiki Help
Ogre Wiki Help Overview
Help - Basic Syntax
Help - Images
Help - Pages and Structures
Help - Wiki Plugins
Toolbox
Freetags
Categories
List Pages
Structures
Trackers
Statistics
Rankings
List Galleries
Ogre Lexicon
Comments
History: OgreDotNet Intermediate Tutorial 5
View page
Source of version: 2
(current)
!!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) {maketoc} !!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. {CODE(wrap="1", colors="c#")} using System; using System.IO; using System.Drawing; using Math3D; using OgreDotNet; {CODE} 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: {CODE(wrap="1", colors="c#")} Root mRoot; RenderWindow mRenderWindow; SceneManager mSceneManager; Camera mCamera; Viewport mViewport; OgreDotNet.EventHandler mEventHandler; bool mDone; {CODE} 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. {CODE(wrap="1", colors="c#")} class HelloWorldApp : IDisposable {CODE} We'll need constructor, a destructor and Dispose methods. Since we don't use any unmanaged memory, we'll call Dispose from the destructor. {CODE(wrap="1", colors="c#")} HelloWorldApp(){} ~HelloWorldApp() { Dispose(); } public void Dispose(){} {CODE} I mentioned that we're ''still'' using the resource scripts so we need a method which does that. {CODE(wrap="1", colors="c#")} protected void SetupResources(string sFileName) { } {CODE} I also decided to use the Start method since it keeps main nice & tidy. {CODE(wrap="1", colors="c#")} public void Start() { } {CODE} 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. {CODE(wrap="1", colors="c#")} protected bool FrameStarted(FrameEvent e) { return true; } protected bool FrameEnded(FrameEvent e) { return true; } protected void KeyClicked(KeyEvent e) { } {CODE} Last, but certainly not least, we need main() ! {CODE(wrap="1", colors="c#")} static void Main(string[] args) { } {CODE} Here's what we've got so far: !!!Hello World Start Code {CODE(wrap="1", colors="c#")} 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) { } } } {CODE} !!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: {CODE(wrap="1", colors="c#")} // 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); {CODE} !!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 {CODE(wrap="1", colors="c#")} protected bool FrameStarted(FrameEvent e) { if (mRenderWindow.Closed || mDone) return false; return true; } {CODE} FrameEnded does absolutely nothing.. {CODE(wrap="1", colors="c#")} protected bool FrameEnded(FrameEvent e) { return true; } {CODE} KeyClicked simply looks for the escape key and sets the done variable. {CODE(wrap="1", colors="c#")} protected void KeyClicked(KeyEvent e) { switch (e.KeyCode) { case KeyCode.Escape: mDone = true; break; default: break; } } {CODE} !!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 {CODE(wrap="1", colors="c#")} ResourceGroupManager.getSingleton().addResourceLocation(sarchName, sLocType, secName);{CODE} The complete declaration for this function can be found in the OGRE manual: {CODE(wrap="1", colors="c#")} 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. {CODE} 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. {CODE(wrap="1", colors="c#")} 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); } } } } } {CODE} !!Starting the rendering loop Calling Root's StartRendering method kicks off the OGRE loop. {CODE(wrap="1", colors="c#")} public void Start() { mRoot.StartRendering(); } {CODE} Next, in main, we'll create our app and start it {CODE(wrap="1", colors="c#")} using(HelloWorldApp app = new HelloWorldApp()) { app.Start(); } {CODE} !!Disposing of Root Since Root implements IDisposable, we need to call mRoot.Dispose() in our Dispose method {CODE(wrap="1", colors="c#")} public void Dispose() { mRoot.Dispose(); }{CODE} !!The completed code {CODE(wrap="1", colors="c#")} 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(); } } } } {CODE}
Search by Tags
Search Wiki by Freetags
Latest Changes
IDE Eclipse
FMOD SoundManager
HDRlib
Building Ogre V2 with CMake
Ogre 2.1 FAQ
Minimal Ogre Collision
Artifex Terra
OpenMB
Advanced Mogre Framework
MogreSocks
...more
Search
Find
Advanced
Search Help
Online Users
108 online users