Setting up a simple Mogre Application in F#

Game engine panorama is pretty crowded. There are open source ones, commercial ones and so on. Ogre is properly defined as an “Open Source 3D Graphics Engine”, which means that it is at a lower level than a fully-fledged Game Engine. You can use Ogre for 3D rendering and assemble your homemade Game Engine picking up other useful library, for example OpenAL for the sound processing and Nvidia PhysiX for the Physics. This way has been followed, for example by NeoAxis Engine.

Mogre  (quoting from the official site) ” is an advanced .NET wrapper for OGRE. It can be used by C#, Visual Basic etc. […]  The pure graphic rendering speed is similar to Ogre C++ applications, because the calculations are done by the same Ogre core library (written in C++) and the wrapped interactions are quite fast”. Since premises seemes encouranging, I’ve given Mogre a try, intrigued by the possibility to use F# for game developement (or at least for same experiments).

Installation

The installation is a piece of cake, thanks to an installer downloadable from the official site. Once installation is completed, you should have a directory labeled “MogreSDK” inside your C folder (or whatever). The next step is to build all the bundled examples (they are for 90% the same of the original Ogre package) clicking on a command script inside the SDK folder (BuildSamples.cmd for the lazy people). Now you can start the “SampleBrowser” program and test this stuff, for example “Mogre in Windows Forms”:

Cool, it worked!

Setting up a simple F# Project

Setting up an F# Project is not incredibly difficult (is pretty straightforward) thanks to a bunch of libraries called “Mogre Wiki Tutorial Framework” especially gathered to simplify the initial Mogre setup. As we can read on the Wiki this “Mini Framework” is not the ideal environment to work with, but only an aid to get started quickly with Mogre without struggling too much. As scaffolder we’ll use the Visual C# project downloadable from here (it targets .NET 4.0). Let’s see what it contains:

The “bin” directory contains all the required .dll for make our example works, and the “app.config” file is important as well, because it allows mixing assemblies compiled against different .NET versions. So let’s create another project, this file an F# solution (as Application type, leave “F# Application”), and then copy folders “bin” and “Media” and “app.config” in the newly created project. This is how the project will look like (ignore the obj directory, obviously will be generated after a successful compile):

Now the funniest part, write a program that do something useful.

Retrieve, Reuse, Revise, Retain

Before taking a leap inside the actual program, remember to add the required references for your project: you will need at least three of them: “Mogre”, “Mogre.TutorialFramework” and “MOIS”. You will found the dlls inside the bin directory. When you are ready, this is what’s inside my Program.fs:

open Mogre
open Mogre.TutorialFramework
open System

type SimpleApp() =
    inherit BaseApplication()

    override this.CreateScene() =
        this.mSceneMgr.AmbientLight <- new ColourValue(1.0f, 1.0f, 1.0f)
        let ent = this.mSceneMgr.CreateEntity("Head", "ogrehead.mesh")
        let node = this.mSceneMgr.RootSceneNode.CreateChildSceneNode("HeadNode")
        node.AttachObject(ent)

let app = new SimpleApp()
app.Go()

I’m not getting through it, because it’s a mere porting from the equivalent C# code. Once again, F# succintness and .NET thight intregration allows us to write compact and elegant code. One last subtle thing: I’ve encountered an error running my project, related to the mix of assemblies targetting different .NET version. Do you remeber the app.config file? There is a line which solves exactly this issue, but you have to copy it inside the executable directory in order to make all work. You can elegantly solve the problem with a post build rule (Project properties -> Post build event):

copy $(ProjectDir)app.config $(TargetDir)$(TargetFileName).config

And now you’ll (hopefully) be able to run your code!

You can download the full project on GitHub here.

Cheers,

Alfredo