Click here to Skip to main content
15,065,328 members
Articles / Desktop Programming / WPF
Posted 9 Sep 2008


68 bookmarked

Blend the OGRE Graphics Engine into your WPF projects

Rate me:
Please Sign up or sign in to vote.
4.85/5 (30 votes)
9 Sep 2008CPOL8 min read
Blend a First Class Gaming Graphics Engine into your WPF application

I have uploaded the demo project in 2 parts. (Sorry :-( OGRE is quite large and I couldn't upload 1 file.)
Extract both of the zip files into the same place before running the demo.
e.g. in winzip click "extract" -> "c:\temp" (same process for both)
Your directory structure should look like this once completed doing so:
..\OgreInWpf\Release (goto this folder to run the demos)

With This You Can... the 10 second overview

  • Blend a First Class Gaming Graphics Engine into a First Class Presentation System.
  • Spill your coffee over your keyboard as you see just how cool your UI look and feel can be.



  • Visual Studio 2008
  • .Net framework 3.5 (Service Pack 1)
  • A reasonable graphics card. This project might not run on all computers. I have only tested it on NVidia cards.


The Viewport3D control in the WPF is pretty useful, but it lacks the cool tweakiness of a proper graphics engine, e.g:

  • performance critical features like scene paging and "level of detail" meshes allowing large meshes to be displayed using different configurations; a hi-polygon version when near to the camera for when high definition is important, and with a low-polygon version when far away from the camera.
  • GPU (Graphics Processor) scripts for DirectX Pixel Shaders to achieve scene blurring, or to simulate night vision goggles. Now, I'm not talking about the GPU code itself, because the new 3.5 SP1 framework has got GPU Bitmap effects, I'm talking about making it easy and maintainable to apply these kinky features by having a scripting resource mechanism so you don't even have to write a line of code to include fancy first rate materials features in your scene. (Well one line ;-) This allows you to change the object's materials easily and without any re-compiling. No mess, no fuss.
  • Proper package resource management for your textures, scripts, GPU Shaders, animations etc.
  • Plenty of other stuff which you'll need, even before you get to the physics, sound, AI and actual gaming side...

As you can see there really is a lot of technology that goes into a modern game and animation effects. Now, graphics aside what about all this behaving itself on your desktop, reading office documents, allowing a user to work in an interface that's funky, themable, supports data binding, web services, swanky smoke effects, etc...

Missing the good old days of Pac-man and DOS ?. It all seemed so much simpler back then. I know I sometimes do.... um, miss DOS, not seem simple. Though I know people that might argue that point.

Not convinced...ok, ok, say your boss comes to you and says, "Frank", cause that's your know, or Bob.
"Frank" he says, "we need to write an application for the 'Smokey Cheese Company', that has, ok, get this, a swift little smoke effect on the main screen, see, bellowing smoke into the air -", (I can do this you say to yourself, you know, graphics isn't too hard) ...", the boss continues, " - over their scrolling logo and displays a list of stuff from a web service where the user can click - "
Ok now it's getting a bit harder all of a sudden because graphics engines don't have very good UI controls (the boss still hasn't stopped talking by the way)
- "..blah blah, by Friday!", the boss finishes.
Unfortunately Bob is out for tea.

The Fluffy Bunny Of Doom syndrome.

Each a do-able task in their own right?. Integration people, that's what I'm talking about.

Enter Stage (left): The OgreImage Class

Ogre loading

Sweet particle effects

Realtime TV compositor effect

The OgreImage class is just like any of the other WPF ImageSource classes. An image source can be used as a brush for a background, for text, or displayed in an Image control and the OgreImage is no different, except that it has all the functionality of a First Class Graphics Engine. It has been based on the new D3DImage ( class which is part of the Framework 3.5 SP 1 update.

I have, in the simplest way implemented the process of hosting the OGRE engine in the DirectX context of the D3DImage class. That is to say, you will still need to interact with the OGRE library via the MOGRE library as any other .NET OGRE application, but won't have to deal with screen re-sizing and DirectX device loss/management issues. One thing I have implemented is asynchronous loading of the OGRE resources and have included a progress event system to allow for the a progress indicator implementation (see example). I've done this because OGRE Engine can take a while to start-up.

There is currently no implementation of a control similar to the Viewport3D, but I'm hoping to expand on this project and to xaml-fy it more to allow for the entire scene to be constructed in xaml.

How To Use It

Exactly like you might have displayed a bitmap in the Image control you could do the following:

The ImageSource

<Image x:Name="RenterTargetControl" 
        <OgreLib:OgreImage x:Name="_ogreImage"

This would include the OgreImage class in your page displayed via the Image control. The events of interest are:

  • The ResourceLoadItemProgress event which is called for every resource loaded. Here you can get the name of the resource and a scalar (0 -> 1) indicating the progress of the entire loading process.

    private void OgreImage_ResourceLoadItemProgress
    	(object sender, ResourceLoadEventArgs e)
        _progressName.Text = e.Name;
        // scale the progress bar
        _progressScale.ScaleX = e.Progress;
  • The Initialised event. This event is called just after all the resources have been loaded (and before the first frame is rendered). In this event the scene can be constructed.

    Please refer to the Ogre wiki on how to use Ogre SceneManager itself.

    void _image_InitScene(object sender, RoutedEventArgs e)
        // start the scene fade in animation
        var sb = (Storyboard)this.Resources["StartEngine"];
        // get the Ogre scene manager
        SceneManager sceneMgr = _ogreImage.SceneManager;
        // create a light
        Light l = sceneMgr.CreateLight("MainLight");
        // Accept default settings: point light, white diffuse, just set position
        // NB I could attach the light to a SceneNode 
        // if I wanted it to move automatically with
        // other objects.
        l.Position = new Vector3(20F, 80F, 50F);
        // load the "ogre head mesh" resource.
        _ogreMesh = sceneMgr.CreateEntity
    		("ogre", "ogrehead.mesh");
        // create a node for the "ogre head mesh"
        _ogreNode = sceneMgr.RootSceneNode.CreateChildSceneNode
        // Create shared node for the particle effects
        _fountainNode = sceneMgr.RootSceneNode.CreateChildSceneNode();
        // Set nonvisible timeout
        ParticleSystem.DefaultNonVisibleUpdateTimeout = 5;
        // update the partical systems
        _cb_Click(null, null);
  • The PreRender event. This event is called before the frame is rendered. Here you can update or change any of the scene nodes.

    void _image_PreRender(object sender, System.EventArgs e)
        // if the viewport has changed reload the special effects
        if (_ogreImage.Camera.Viewport != _viewport)
            _viewport = _ogreImage.Camera.Viewport;
            _cbCompositor_Click(_cbBloom, null);
            _cbCompositor_Click(_cbGlass, null);
            _cbCompositor_Click(_cbOldTV, null);
        // rotate the "ogre head"
        _ogreNode.Rotate(new Vector3(0, 1, 0), new Degree(0.1f));

Resizing the Ogre Viewport

Also, to get the Ogre viewport to resize itself when the Image size changes you need to manually resize the OgreImage image on the size event of the Image.

private void RenterTargetControl_SizeChanged
	(object sender, SizeChangedEventArgs e)
    if (_ogreImage == null) return;

    _ogreImage.ViewportSize = e.NewSize;

To keep the app responsive during a window drag exercise I've made the OgreImage class delay its processing of the resize. This will make the image wait for a couple of milliseconds (200 by default) until the size has stabilized. Have a look at the ResizeRenderTargetDelay property.

Starting it all. "Graphics, you haz dem."

One more thing to do is to start the engine. Currently this is done manually by calling the InitOgreAsync method. The reason why the engine can't start automatically is because it requires a valid windows handle to initialize itself and this isn't available while the page xaml is busy loading. I'm sure it won't be too difficult to come up with a clever solution, but I'll let you guys do that. ;-)

The control can be initialized on the window's Loaded event.

 private void Window_Loaded(object sender, RoutedEventArgs e)
    _ogreImage = (OgreImage)RenterTargetControl.Source;

Binding to the OGRE Library and Media

The OGRE library is quite a monster to get setup for a project. I have only included the essential DLLs and media in this article's download because the full MOGRE setup is quite large. If you would like to develop using MOGRE you will need to download the full SDK which is a hefty 40 meg.

The executables in this project have been compiled into the "Release" folder of the article's download where all the OGRE/MOGRE "Release" DLLs and resource config files are. They are quite large so there's only one instance of them. That is to say, all the VS projects have been configured to have their build output go directly into the Release folder.

If you'd like to re-compile the MogreWpf.Interop.dll (a c++ project) you'll need to set the MOGRE_HOME environment variable in windows to include the "<..>\MogreSDK" path....once you've downloaded the MOGRE SDK. Also compile it in "Release" mode to match the Ogre and Mogre DLLs for this project's download.

Points of Interest

  • The WPF now supports GPU Bitmap effects (a-series-on-gpu-based-effects-for-wpf)
    This article doesn't use them, it just might interest you to know that it's now possible to do funky stuff in the WPF.
  • Ogre is a pretty good object oriented graphics library. If you like graphics programming you should have a look at it. It is mature library, is fun to use and has a good Texture Material and Particle Effects scripting language.


And there you have it. A 3D graphics engine blended into your WPF window. Ooooooh,....shiny!

Ok, this wouldn't be the best way to create a 2000 "frames per second" game. It would be best to leave the OGRE Engine to use its native windowing system, but it will keep your boss loving you at your day job. ;-)

Note: Before you make all kinds of promises to 'said' boss, read the OGRE and MOGRE licenses first, and also I'm not sure the implementation of the OgreImage is 100% reliable at all times. I have had some funny crashes, though it seems pretty stable at the moment after my last code tweak'ins (fluffy bunnies aside). However it should give you a good place to start your "next level" graphics journey.


  • 2008-09-09 - Initial publication


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Leslie Godwin
Technical Lead FNB Connect
South Africa South Africa
iOS Technical Lead at FNB

Computers are really sweet. Aren't they?
Yup they are...

I've always loved writing tools and components...never been very interested in playing games....always wanted to be able to write them though.

And, yes. I'm still pretty annoyed they discontinued the Amiga computer.

Comments and Discussions

QuestionMogre 1.7 building Pin
hayden62123-Jan-13 22:18
Memberhayden62123-Jan-13 22:18 
GeneralMy vote of 5 Pin
hayden62122-Jan-13 17:12
Memberhayden62122-Jan-13 17:12 
QuestionThis example on .net 4.5 Pin
AlexanderPD21-Dec-12 0:03
MemberAlexanderPD21-Dec-12 0:03 
Suggestion-- A proposed Headquarters for WPF+Mogre development -- Pin
JaredThirsk7-May-12 19:35
MemberJaredThirsk7-May-12 19:35 
QuestionExcellent article! Just what I was looking for Pin
andyb197928-Mar-12 4:27
Memberandyb197928-Mar-12 4:27 
QuestionNew version of OGRE Pin
uj_mipt9-Sep-11 16:35
Memberuj_mipt9-Sep-11 16:35 
AnswerRe: New version of OGRE Pin
Member 865786627-Feb-12 5:36
MemberMember 865786627-Feb-12 5:36 
GeneralRe: New version of OGRE Pin
Leslie Godwin28-Feb-12 4:20
MemberLeslie Godwin28-Feb-12 4:20 
GeneralRe: New version of OGRE Pin
andyb197928-Mar-12 5:26
Memberandyb197928-Mar-12 5:26 
AnswerRe: New version of OGRE Pin
NorekCZ13-Apr-12 6:42
MemberNorekCZ13-Apr-12 6:42 
QuestionAntialiasing Pin
oidamoh20-Jun-11 7:32
Memberoidamoh20-Jun-11 7:32 
Questionhow to use wpf transfer the sence of ogre(C++) Pin
qackyqqr19-Oct-10 16:40
Memberqackyqqr19-Oct-10 16:40 
AnswerRe: how to use wpf transfer the sence of ogre(C++) Pin
Leslie Godwin19-Oct-10 18:32
MemberLeslie Godwin19-Oct-10 18:32 
AnswerRe: how to use wpf transfer the sence of ogre(C++) Pin
Leslie Godwin19-Oct-10 18:55
MemberLeslie Godwin19-Oct-10 18:55 
QuestionHow to output to JPG image? Pin
Gabriyel25-Aug-10 18:01
MemberGabriyel25-Aug-10 18:01 
AnswerRe: How to output to JPG image? Pin
Leslie Godwin26-Aug-10 23:28
MemberLeslie Godwin26-Aug-10 23:28 
AnswerRe: How to output to JPG image? Pin
JaredThirsk18-Mar-11 0:30
MemberJaredThirsk18-Mar-11 0:30 
QuestionSystem.MissingMethodException [modified] Pin
Thor Jacobsen24-Aug-10 5:48
MemberThor Jacobsen24-Aug-10 5:48 
QuestionMultiple viewports at once Pin
Pegazyz15-Mar-10 18:43
MemberPegazyz15-Mar-10 18:43 
AnswerRe: Multiple viewports at once Pin
Pegazyz25-Mar-10 7:02
MemberPegazyz25-Mar-10 7:02 
GeneralRe: Multiple viewports at once Pin
Leslie Godwin19-Oct-10 18:31
MemberLeslie Godwin19-Oct-10 18:31 
GeneralRe: Multiple viewports at once Pin
JaredThirsk18-Nov-10 8:05
MemberJaredThirsk18-Nov-10 8:05 
GeneralRe: Multiple viewports at once Pin
JaredThirsk21-Mar-11 5:00
MemberJaredThirsk21-Mar-11 5:00 
GeneralWPF Pin
TusharCool10-Jan-10 20:28
MemberTusharCool10-Jan-10 20:28 
GeneralRe: WPF Pin
Leslie Godwin26-Aug-10 22:27
MemberLeslie Godwin26-Aug-10 22:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.