Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Blend the OGRE Graphics Engine into your WPF projects

, 9 Sep 2008
Blend a First Class Gaming Graphics Engine into your WPF application
OgreInWpf_Part1.zip
OgreInWpf
DemoApps
DemoApps.suo
OrgreHead
OgreHead.csproj.user
Properties
Settings.settings
Release
OgreHead.vshost.exe.manifest
SmokeyCheeseCompany
bin
Release
Properties
Settings.settings
Libraries
MogreWpf.Interop
app.ico
MogreWpf.Interop.suo
MogreWpf.Interop.vcproj.FRED-COMPUTER.Fred.user
MogreWpf.Interop.vcproj.LESLIEG.Fred.user
OgreLib
Properties
Release
Media
DeferredShadingMedia
COPYING
deferred.glsl.program
deferred.hlsl.program
deferred.material
deferreddemo.material
DeferredShading
material
glsl
nm_notex_ps.glsl
nm_ps.glsl
nm_vs.glsl
notex_ps.glsl
ps.glsl
vs.glsl
hlsl
nm_notex_ps.hlsl
nm_ps.hlsl
nm_vs.hlsl
notex_ps.hlsl
ps.hlsl
vs.hlsl
post
glsl
Ambient_ps.glsl
GlobalLight_ps.glsl
LightMaterial_ps.glsl
LightMaterial_vs.glsl
ShowColour_ps.glsl
ShowDS_ps.glsl
ShowNormal_ps.glsl
SinglePass_ps.glsl
vs.glsl
hlsl
Ambient_ps.hlsl
GlobalLight_ps.hlsl
LightMaterial_ps.hlsl
LightMaterial_vs.hlsl
ShowColour_ps.hlsl
ShowDS_ps.hlsl
ShowNormal_ps.hlsl
SinglePass_ps.hlsl
vs.hlsl
deferred_post_debug.glsl.program
deferred_post_debug.hlsl.program
deferred_post_debug.material
deferred_post_minilight.glsl.program
deferred_post_minilight.hlsl.program
deferred_post_minilight.material
deferred_post_multipass.glsl.program
deferred_post_multipass.hlsl.program
deferred_post_multipass.material
deferred_post_onepass.glsl.program
deferred_post_onepass.hlsl.program
deferred_post_onepass.material
fonts
bluebold.ttf
bluecond.ttf
bluehigh.ttf
sample.fontdef
solo5.ttf
gui
ogregui.layout
materials
programs
AmbientOneTexture.glsl
Bloom_ps20.hlsl
Bloom_vs11.hlsl
Blur0_ps20.hlsl
Blur0_vs.glsl
Blur0_vs11.hlsl
Blur1_ps20.hlsl
Blur1_vs.glsl
Blur1_vs11.hlsl
Blur_ps.glsl
Combine_fp.cg
crowdVp.glsl
DepthShadowmap.hlsl
DepthShadowmapCasterFp.glsl
DepthShadowmapCasterVp.glsl
DepthShadowmapNormalMapReceiverFp.glsl
DepthShadowmapNormalMapReceiverVp.glsl
DepthShadowmapReceiverFp.glsl
DepthShadowmapReceiverVp.glsl
DOF_ps.cg
Example_Basic.cg
Example_Basic.hlsl
Example_BumpMapping.cg
Example_CelShading.cg
Example_Fresnel.cg
Example_Projection.cg
GlassFP.cg
Grass.cg
GrayScale.cg
hdr.cg
hdr.hlsl
hdr_bloom.glsl
hdr_downscale2x2luminence.glsl
hdr_downscale3x3.glsl
hdr_downscale3x3brightpass.glsl
hdr_finalToneMapping.glsl
hdr_tonemap_util.glsl
HeatVision.cg
instancing.cg
instancingVp.glsl
InvertFP.cg
LaplaceFP.cg
Ocean2GLSL.frag
Ocean2GLSL.vert
Ocean2HLSL_Cg.frag
Ocean2HLSL_Cg.vert
oceanGLSL.frag
oceanGLSL.vert
oceanHLSL_Cg.frag
oceanHLSL_Cg.vert
OffsetMapping.cg
OffsetMapping.hlsl
OffsetMappingFp.glsl
OffsetMappingVp.glsl
OldMovieFP.cg
OldTV.cg
PosterizeFP.cg
SharpenEdgesFP.cg
skinningTwoWeightsVp.glsl
StdQuad_vp.cg
StdQuad_vp.glsl
TilingFP.cg
varianceshadowcasterfp.cg
varianceshadowcastervp.cg
varianceshadowreceiverfp.cg
varianceshadowreceivervp.cg
scripts
Bloom.material
Example-DynTex.material
Example.material
Examples-Advanced.material
Examples.compositor
Examples.program
Glass.material
Ogre.material
OldMovie.material
OldTV.material
smoke.material
StdQuad_vp.program
textures
aureola.png
basic_droplet.png
cel_shading_diffuse.png
cel_shading_edge.png
cel_shading_specular.png
Dirt.jpg
dirt01.jpg
droplet.png
flare.png
flaretrail.png
GreenSkin.jpg
NoiseVolume.dds
Random3D.dds
smoke.png
smokecolors.png
spheremap.png
WaterNormal1.tga
WeirdEye.png
models
ogrehead.mesh
overlays
Compositor.overlay
DP3.overlay
Example-CubeMapping.overlay
Example-DynTex.overlay
Example-Water.overlay
Shadows.overlay
packs
OgreCore.zip
particle
emitted_emitter.particle
Example-Water.particle
Example.particle
smoke.particle
Release
D3DX9_37.dll
OgreInWpf_Part2.zip
cg.dll
Mogre.dll
MogreWpf.Interop.dll
ogre.cfg
OgreHead.exe
OgreHead.vshost.exe
OgreHead.vshost.exe.manifest
OgreLib.dll
OgreMain.dll
plugins.cfg
Plugin_BSPSceneManager.dll
Plugin_CgProgramManager.dll
Plugin_OctreeSceneManager.dll
Plugin_ParticleFX.dll
RenderSystem_Direct3D9.dll
resources.cfg
SmokeyCheeseCompany.exe
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using Mogre;

using OgreLib;

namespace OgreHead
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private SceneNode _ogreNode;
        private Entity _ogreMesh;
        private SceneNode _fountainNode;
        private Viewport _viewport;

        public Window1()
        {
            App.Current.Exit += Current_Exit;

            InitializeComponent();
        }

        void Current_Exit(object sender, ExitEventArgs e)
        {
            RenterTargetControl.Source = null;

            _ogreImage.Dispose();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _ogreImage.InitOgreAsync();
        }

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

            _ogreImage.ViewportSize = e.NewSize;
        }

        void _image_InitScene(object sender, RoutedEventArgs e)
        {
 	    // start the scene fade in animation
            var sb = (Storyboard)this.Resources["StartEngine"];
            sb.Begin(this);

            // get the Ogre scene manager
            SceneManager sceneMgr = _ogreImage.SceneManager;

            // ceate 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("ogreNode");
            _ogreNode.AttachObject(_ogreMesh);

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

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

            // rotote the "ogre head"
            _ogreNode.Rotate(new Vector3(0, 1, 0), new Degree(0.1f));
        }

        private void RenterTargetControl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            MessageBox.Show("Clicked");
        }

        private void _cb_Click(object sender, RoutedEventArgs e)
        {
            SceneManager sceneMgr = _ogreImage.SceneManager;
            if (sceneMgr == null) return;

            _fountainNode.RemoveAndDestroyAllChildren();
            sceneMgr.DestroyAllParticleSystems();

            SceneNode fNode;

            if (_cbFireworks.IsChecked == true) //isChecked is nullable
            {
                ParticleSystem pSys1 = sceneMgr.CreateParticleSystem("fireworks", "Examples/Fireworks");

                _fountainNode.CreateChildSceneNode().AttachObject(pSys1);
            }

            if (_cbPurpleFountain.IsChecked == true)
            {
                // fountain 1
                ParticleSystem pSys2 = sceneMgr.CreateParticleSystem("fountain1",
                                                                     "Examples/PurpleFountain");

                // Point the fountain at an angle
                fNode = _fountainNode.CreateChildSceneNode();
                fNode.Translate(200, -100, 0);
                fNode.Rotate(Vector3.UNIT_Z, new Degree(20));
                fNode.AttachObject(pSys2);

                // fountain 2
                ParticleSystem pSys3 = sceneMgr.CreateParticleSystem("fountain2",
                    "Examples/PurpleFountain");
                // Point the fountain at an angle
                fNode = _fountainNode.CreateChildSceneNode();
                fNode.Translate(-200, -100, 0);
                fNode.Rotate(Vector3.UNIT_Z, new Degree(-20));
                fNode.AttachObject(pSys3);
            }

            if (_cbRain.IsChecked == true)
            {
                // Create a rainstorm
                ParticleSystem pSys4 = sceneMgr.CreateParticleSystem("rain",
                                                                     "Examples/Rain");
                TextureHelper.CalculateAllAlphas(pSys4);

                SceneNode rNode = sceneMgr.RootSceneNode.CreateChildSceneNode();
                rNode.Translate(0, 1000, 0);
                rNode.AttachObject(pSys4);
                // Fast-forward the rain so it looks more natural
                pSys4.FastForward(5);
            }

            if (_cbAureola.IsChecked == true)
            {
                // Aureola around Ogre perpendicular to the ground
                ParticleSystem pSys5 = sceneMgr.CreateParticleSystem("aureola",
                                                                     "Examples/Aureola");
                sceneMgr.RootSceneNode.CreateChildSceneNode().AttachObject(pSys5);
            }
        }

        private void _cbOgreMaterials_Click(object sender, RoutedEventArgs e)
        {
            SceneManager sceneMgr = _ogreImage.SceneManager;
            if (sceneMgr == null) return;

            const uint CUSTOM_SHININESS = 1;
            const uint CUSTOM_DIFFUSE = 2;
            const uint CUSTOM_SPECULAR = 3;

            if (_cbOgreMaterials.IsChecked == true)
            {
                // Set common material, but define custom parameters to change colours
                // See Example-Advanced.material for how these are finally bound to GPU parameters
                SubEntity sub;

                // eyes
                sub = _ogreMesh.GetSubEntity(0);
                sub.MaterialName = ("Examples/CelShading");
                sub.SetCustomParameter(CUSTOM_SHININESS, new Vector4(35.0f, 0.0f, 0.0f, 0.0f));
                sub.SetCustomParameter(CUSTOM_DIFFUSE, new Vector4(1.0f, 0.3f, 0.3f, 1.0f));
                sub.SetCustomParameter(CUSTOM_SPECULAR, new Vector4(1.0f, 0.6f, 0.6f, 1.0f));

                // skin
                sub = _ogreMesh.GetSubEntity(1);
                sub.MaterialName = ("Examples/CelShading");
                sub.SetCustomParameter(CUSTOM_SHININESS, new Vector4(10.0f, 0.0f, 0.0f, 0.0f));
                sub.SetCustomParameter(CUSTOM_DIFFUSE, new Vector4(0.0f, 0.5f, 0.0f, 1.0f));
                sub.SetCustomParameter(CUSTOM_SPECULAR, new Vector4(0.3f, 0.5f, 0.3f, 1.0f));

                // earring
                sub = _ogreMesh.GetSubEntity(2);
                sub.MaterialName = ("Examples/CelShading");
                sub.SetCustomParameter(CUSTOM_SHININESS, new Vector4(25.0f, 0.0f, 0.0f, 0.0f));
                sub.SetCustomParameter(CUSTOM_DIFFUSE, new Vector4(1.0f, 1.0f, 0.0f, 1.0f));
                sub.SetCustomParameter(CUSTOM_SPECULAR, new Vector4(1.0f, 1.0f, 0.7f, 1.0f));

                // teeth
                sub = _ogreMesh.GetSubEntity(3);
                sub.MaterialName = ("Examples/CelShading");
                sub.SetCustomParameter(CUSTOM_SHININESS, new Vector4(20.0f, 0.0f, 0.0f, 0.0f));
                sub.SetCustomParameter(CUSTOM_DIFFUSE, new Vector4(1.0f, 1.0f, 0.7f, 1.0f));
                sub.SetCustomParameter(CUSTOM_SPECULAR, new Vector4(1.0f, 1.0f, 1.0f, 1.0f));
            }
            else
            {
                sceneMgr.DestroyEntity(_ogreMesh);
                _ogreMesh = sceneMgr.CreateEntity("ogre", "ogrehead.mesh");
                _ogreNode.AttachObject(_ogreMesh);
            }
        }

        private void _cbCompositor_Click(object sender, RoutedEventArgs e)
        {
            var checkBox = (CheckBox)sender;

            string name = null;
            if (checkBox == _cbBloom)
                name = "Bloom";
            if (checkBox == _cbGlass)
                name = "Glass";
            if (checkBox == _cbOldTV)
                name = "Old TV";

            if (name != null)
            {
                if (checkBox.IsChecked == true)
                {
                    CompositorInstance instance = CompositorManager.Singleton.AddCompositor(_viewport, name, 0);
                    if (instance != null)
                        instance. Enabled = true;
                }
                else
                {
                    CompositorManager.Singleton.RemoveCompositor(_viewport, name);
                }
            }
        }

        private void OgreImage_ResourceLoadItemProgress(object sender, ResourceLoadEventArgs e)
        {
            _progressName.Text = e.Name;

            // scale the progress bar
            _progressScale.ScaleX = e.Progress;
        }

        private void StartEngine_Completed(object sender, EventArgs e)
        {
            _loading.Visibility = Visibility.Collapsed;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

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.
Follow on   Twitter   Google+

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 9 Sep 2008
Article Copyright 2008 by Leslie Godwin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid