WinForm Multiple viewports VB.NET - Axiom : A 3D Rendering Engine in C#

WinForm Multiple viewports VB.NET

From Axiom

Jump to: navigation, search

This example is to show you how you can use Multiple windows/viewports on one windows form and also loading a mesh and using a trackbar control to rotate the mesh (same as my Embedding Axiom On A Windows Form VB.NET) tutorial

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 i have used sizes... 460, 650


Add 2 PictureBox controls to the form i have used sizes... 443, 268 one under the other

Add a Button 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

NOTE You must add ->existing item and browse to your Axiom samples folder and add devIL and tao.devIL DLLs to your project


Create a directory called 'Media' in the 'bin/Debug' folder and 3 folders inside the Media folder called... Materials, Textures and Meshes for your project and copy the following files into it from the Axiom examples:

ogrehead.mesh from Media/Meshes/ into Media/Meshes/ in your project folder you just created

dirt01.jpg, GreenSkin.jpg, spheremap.png, WeirdEye.png from Media/Textures/ into Media/Textures/ in your project folder you just created


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


and put this into Media/Materials in your project folder you just created

The full code listing for the Form class is below.

		Public Class Form1
   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 folders
       Axiom.Core.ResourceGroupManager.Instance.AddResourceLocation("Media\Meshes", "Folder", Axiom.Core.ResourceGroupManager.DefaultResourceGroupName)
       Axiom.Core.ResourceGroupManager.Instance.AddResourceLocation("Media\Materials", "Folder", Axiom.Core.ResourceGroupManager.DefaultResourceGroupName)
       Axiom.Core.ResourceGroupManager.Instance.AddResourceLocation("Media\Textures", "Folder", Axiom.Core.ResourceGroupManager.DefaultResourceGroupName)
       'Initialize engine, but don't auto-create a window
       _Root.Initialize(False)
       'Manually create RenderWindow1 and pass it a handle to the PictureBox control
       'to render in
       Dim paramList As New Axiom.Collections.NamedParameterList
       paramList("externalWindowHandle") = PictureBox1.Handle
       Dim window1 As Axiom.Graphics.RenderWindow = _Root.CreateRenderWindow("RenderWindow1", PictureBox1.Width, PictureBox1.Height, False, paramList)
       'Manually create RenderWindow2 and pass it a handle to the PictureBox control
       'to render in
       Dim paramList2 As New Axiom.Collections.NamedParameterList
       paramList2("externalWindowHandle") = PictureBox2.Handle
       Dim window2 As Axiom.Graphics.RenderWindow = _Root.CreateRenderWindow("RenderWindow2", PictureBox2.Width, PictureBox2.Height, False, paramList2)
       'Create generic scene manager
       _SceneManager = _Root.CreateSceneManager(Axiom.Core.SceneType.Generic, "MainSceneManager")
       'Create a camera and initialize its position
       Dim camera1 As Axiom.Core.Camera = _SceneManager.CreateCamera("1stCamera")
       camera1.Position = New Axiom.Math.Vector3(60, 60, 100)
       camera1.LookAt(New Axiom.Math.Vector3(0, 0, 0))
       'Set camera options
       camera1.Near = 5
       camera1.AutoAspectRatio = True
       'Create viewport
       Dim viewport1 As Axiom.Core.Viewport = window1.AddViewport(camera1, 0, 0, 1, 1, 100)
       viewport1.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
       Dim camera2 As Axiom.Core.Camera = _SceneManager.CreateCamera("2ndCamera")
       camera2.Position = New Axiom.Math.Vector3(60, 60, 100)
       camera2.LookAt(New Axiom.Math.Vector3(0, 0, 0))
       'Set camera options
       camera2.Near = 5
       camera2.AutoAspectRatio = True
       Dim viewport2 As Axiom.Core.Viewport = window2.AddViewport(camera2, 0, 0, 1, 1, 101)
       viewport2.BackgroundColor = Axiom.Core.ColorEx.Blue
       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

End Class

You should now have Multiple window/viewports on your windows form

PLEASE NOTE: You must create camera1 then the viewport1 and then camera 2 and then viewport2 and so on when adding extra viewports!!! you can NOT add camera 1 and camera2 then the viewports!!!

THANKS for reading TADS

a winform with 4 viewports created the same way... [[1]]

Views
Powered by MediaWiki GNU Free Documentation License 1.2