Embedding Axiom On A Windows Form

From Axiom

Jump to: navigation, search

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


You should now have a form design similar to this:

http://cwadev.net/files/EmbeddedAxiomWinFormLayout.png


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


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