Embedding Axiom On A Windows Form - Axiom : A 3D Rendering Engine in C#

Embedding Axiom On A Windows Form

From Axiom

Jump to: navigation, search

Show code samples in

This example is intended to demonstrate a very basic implementation of using Axiom on a Windows Form. This example was built using the crickhollow SVN source, and assumes you have already downloaded the source and are using Visual Studio 2008


Create a new Windows Forms Application targeting the .NET 2.0 Framework


Add a PictureBox control to the form


Add a TrackBar control to the form and set the following properties:

Minimum=0

Maximum=359

TickFrequency=45


Add a Timer control to the form and set the following properties:

Interval=30


Add the following references to your project:

Axiom.dll

Axiom.Platforms.Win32.dll

Axiom.Plugins.DevILCodecs.dll

Axiom.RenderSystems.DirectX9.dll

Tao.DevIl.dll


Create a directory called 'Media' in the 'bin/Debug' folder for your project and copy the following files into it from the Axiom examples:

ogrehead.mesh from Media/Meshes/

dirt01.jpg, GreenSkin.jpg, spheremap.png, WeirdEye.png from Media/Textures/


Create a file called ogrehead.material and paste the following code into it:

 material Ogre/Earring
 {
 	technique
 	{
 		pass
 		{
 			ambient 0.5 0.5 0
 			diffuse 1 1 0  
 
 			texture_unit
 			{
 				texture spheremap.png
 				colour_op_ex add src_texture src_current
 				colour_op_multipass_fallback one one
 				env_map spherical
 			}
 		}
 	}
 }
 material Ogre/Skin
 {
 	technique
 	{
 		pass
 		{
 			ambient 0.7 0.7 0.7
 			cull_hardware none 
 
 			texture_unit
 			{
 				texture GreenSkin.jpg
 				tex_address_mode mirror
 			}
 		}
 	}
 }
 material Ogre/Tusks
 {
 	technique
 	{
 		pass
 		{
 			ambient 0.5 0.5 0.4
 			diffuse 1 1 0.8 
 
 			texture_unit
 			{
 				texture dirt01.jpg
 				colour_op_ex add src_texture src_current
 				colour_op_multipass_fallback one one
 			}
 		}
 	}
 }
 material Ogre/Eyes
 {
 	technique
 	{
 		pass
 		{
 
 			texture_unit
 			{
 				texture WeirdEye.png
 			}
 		}
 	}
 }

The full code listing for the Form class is below. Make sure you attach the event handlers to the controls (Form_Load, timer1_Tick, trackbar1_Scroll)

 		protected Axiom.Core.Root _Root = null;
 		protected Axiom.Core.SceneManager _SceneManager = null;
 		Axiom.Core.SceneNode _OgreSceneNode = null;
 
 		public Form1()
 		{
 			InitializeComponent();
 		}
 
 		private void Form1_Load(object sender, EventArgs e)
 		{
 			//Create root object
 			_Root = new Axiom.Core.Root("Axiom.log");
 
 			//Select the first available render system
 			_Root.RenderSystem = _Root.RenderSystems[0];
 
 			//Load resources from Media folder
 			Axiom.Core.ResourceGroupManager.Instance.AddResourceLocation("Media", "Folder", Axiom.Core.ResourceGroupManager.DefaultResourceGroupName);
 			Axiom.Core.ResourceGroupManager.Instance.InitializeAllResourceGroups();
 
 			//Initialize engine, but don't auto-create a window
 			_Root.Initialize(false);
 
 			//Manually create a RenderWindow and pass it a handle to the PictureBox control
 			//to render in
 			Axiom.Collections.NamedParameterList paramList = new Axiom.Collections.NamedParameterList();
 			paramList["externalWindowHandle"] = pictureBox1.Handle;
 			Axiom.Graphics.RenderWindow window = _Root.CreateRenderWindow("RenderWindow", pictureBox1.Width, pictureBox1.Height, false, paramList);
 
 			//Create generic scene manager
 			_SceneManager = _Root.CreateSceneManager(Axiom.Core.SceneType.Generic, "MainSceneManager");
 
 			//Create a camera and initialize its position
 			Axiom.Core.Camera camera = _SceneManager.CreateCamera("MainCamera");
 			camera.Position = new Axiom.Math.Vector3(60, 60, 60);
 			camera.LookAt(new Axiom.Math.Vector3(0, 0, 0));
 
 			//Set camera options
 			camera.Near = 5;
 			camera.AutoAspectRatio = true;
 
 			//Create viewport
 			Axiom.Core.Viewport viewport = window.AddViewport(camera, 0, 0, 1.0f, 1.0f, 100);
 			viewport.BackgroundColor = Axiom.Core.ColorEx.Black;
 
 			//Set default number of mipmaps for textures
 			Axiom.Core.TextureManager.Instance.DefaultMipmapCount = 5;
 
 			//Load the ogrehead.mesh from the Media folder
 			Axiom.Core.Mesh ogreMesh = Axiom.Core.MeshManager.Instance.Load("ogrehead.mesh", Axiom.Core.ResourceGroupManager.DefaultResourceGroupName);
 
 			//Create an entity and load the ogrehead.mesh into it
 			Axiom.Core.Entity entity = _SceneManager.CreateEntity("OgreEntity", ogreMesh);
 			//Create a scene node and attach the ogre entity
 			_OgreSceneNode = _SceneManager.RootSceneNode.CreateChildSceneNode("OgreSceneNode");
 			_OgreSceneNode.AttachObject(entity);
 
 			//Start the update timer
 			timer1.Enabled = true;
 		}
 
 		private void timer1_Tick(object sender, EventArgs e)
 		{
 			//Tell Axiom to render a single frame
 			_Root.RenderOneFrame();
 		}
 
 		int prevRotation = 0;
 		private void trackBar1_Scroll(object sender, EventArgs e)
 		{
 			//Rotate the ogre mesh
 			_OgreSceneNode.Rotate(Axiom.Math.Vector3.UnitY, trackBar1.Value-prevRotation);
 			prevRotation = trackBar1.Value;
 		}

    Private _Root As Axiom.Core.Root
    Private _SceneManager As Axiom.Core.SceneManager
    Private _OgreSceneNode As Axiom.Core.SceneNode
    Private prevRotation As Long

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Make sure the timer is NOT running 
        Timer1.Enabled = False
        'Create root object
        _Root = New Axiom.Core.Root("Axiom.log")

        'Select the first available render system
        _Root.RenderSystem = _Root.RenderSystems("DirectX9")

        'Load resources from Media folder
        Axiom.Core.ResourceGroupManager.Instance.AddResourceLocation("Media", "Folder", Axiom.Core.ResourceGroupManager.DefaultResourceGroupName)

        'Initialize engine, but don't auto-create a window
        _Root.Initialize(False)

        'Manually create a RenderWindow and pass it a handle to the PictureBox control
        'to render in
        Dim paramList As New Axiom.Collections.NamedParameterList
        paramList("externalWindowHandle") = PictureBox1.Handle
        Dim window As Axiom.Graphics.RenderWindow = _Root.CreateRenderWindow("RenderWindow", PictureBox1.Width, PictureBox1.Height, False, paramList)

        'Create generic scene manager
        _SceneManager = _Root.CreateSceneManager(Axiom.Core.SceneType.Generic, "MainSceneManager")

        'Create a camera and initialize its position
        Dim camera As Axiom.Core.Camera = _SceneManager.CreateCamera("MainCamera")
        camera.Position = New Axiom.Math.Vector3(60, 60, 100)
        camera.LookAt(New Axiom.Math.Vector3(0, 0, 0))

        'Set camera options
        camera.Near = 5
        camera.AutoAspectRatio = True

        'Create viewport
        Dim viewport As Axiom.Core.Viewport = window.AddViewport(camera, 0, 0, 1, 1, 100)
        viewport.BackgroundColor = Axiom.Core.ColorEx.Black
        'This is the change from the c# tutorial.. we need to move this down the list
        'so it initialize to soon
        Axiom.Core.ResourceGroupManager.Instance.InitializeAllResourceGroups()

        'Set default number of mipmaps for textures
        Axiom.Core.TextureManager.Instance.DefaultMipmapCount = 10

        'Load the ogrehead.mesh from the Media folder
        Dim ogreMesh As Axiom.Core.Mesh = Axiom.Core.MeshManager.Instance.Load("ogrehead.mesh", Axiom.Core.ResourceGroupManager.DefaultResourceGroupName)

        'Create an entity and load the ogrehead.mesh into it
        Dim entity As Axiom.Core.Entity = _SceneManager.CreateEntity("OgreEntity", ogreMesh)
        _OgreSceneNode = _SceneManager.RootSceneNode.CreateChildSceneNode("OgreSceneNode")
        _OgreSceneNode.AttachObject(entity)

        'Start the update timer
        Timer1.Enabled = True

        'Disable the button so you cant try reloading the engine after its loaded
        Button1.Enabled = False
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'Tell Axiom to render a single frame
        _Root.RenderOneFrame()
    End Sub

    Private Sub TrackBar1_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        'Rotate the ogre mesh
        _OgreSceneNode.Rotate(Axiom.Math.Vector3.UnitY, TrackBar1.Value - prevRotation)
        prevRotation = TrackBar1.Value
    End Sub

You should now have a working Axiom control embedded on your Windows Form that will allow you to rotate the ogre head model using the track bar

Views
Powered by MediaWiki GNU Free Documentation License 1.2