Sign in to follow this  
Neil Kerkin

[.net] Managed DirectX Control

Recommended Posts

Neil Kerkin    164
I am trying to create a general managed DirectX control using C# Express 2005 similar to this OpenGL COM control. This is what I have so far (sourced mainly from Tom Millers excellent Managed DirectX book [wink]). This works great when placed on a form, however if more then one control is added, only one control displays properly. I imagine because of how the device member variable is created for each object. I have read that multiple viewports can be created using the swap-chain but I am unsure how to implement the swap chain in this fashion. My guess would have been to create a static device and backbuffer and then create a surface member for each instance and set the render target in the OnPaint method. But I couldn't get this to work. Also, I am envisioning that each viewport will have a different set of vertices to render and I am wondering how costly this context switch will be using managed code. Anyway any input would be greatly appreciated.
using System;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace CTWindows
{
	public partial class CT3DGraph : UserControl
	{
		private Device device = null;
		private PresentParameters presentParams = null;

		private CustomVertex.PositionColored[] verts;

		public CT3DGraph()
		{
			InitializeComponent();
			this.InitialiseGraphics();
			
			this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
		}

		#region Initialise
		public void InitialiseGraphics()
		{
			// Set our presentation parameters
			presentParams = new PresentParameters();
			presentParams.Windowed = true;
			presentParams.SwapEffect = SwapEffect.Discard;
			presentParams.DeviceWindowHandle = this.Handle;

			//create our device
			device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

			//initialise data
			initialiseObjects();
		}

		private void initialiseObjects()
		{
			verts = new CustomVertex.PositionColored[3];
			verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
			verts[0].Color = System.Drawing.Color.Aqua.ToArgb();
			verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
			verts[1].Color = System.Drawing.Color.Black.ToArgb();
			verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
			verts[2].Color = System.Drawing.Color.Purple.ToArgb();
		}
		#endregion

		#region Render 
		protected override void OnPaint(PaintEventArgs e)
		{
			device.Clear(ClearFlags.Target, System.Drawing.Color.CornflowerBlue, 1.0f, 0);

			this.SetupCamera();

			device.BeginScene();
			drawObjects();
			device.EndScene();
			
			device.Present();
			this.Invalidate();
		}		

		private void SetupCamera()
		{
			device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4,
				 this.Width / this.Height, 1.0f, 100.0f);
			device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 5.0f), new Vector3(),
				 new Vector3(0, 1, 0));

			device.RenderState.Lighting = false;
			device.Transform.World = Matrix.RotationZ((System.Environment.TickCount / 450.0f) / (float)Math.PI);
		}

		private void drawObjects()
		{
			device.VertexFormat = CustomVertex.PositionColored.Format;
			device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts);
		}
		#endregion

	}
}


Share this post


Link to post
Share on other sites
Neil Kerkin    164
Thanks Mykre, The Inner Realm control seems to be based around one control per form (though I could be wrong)


[STAThread]
static int Main() {

CmlForm form = new CmlForm();
//Application.Run(form);
form.viewer.RunLoop(form);
return 0;
}


I've avoided the MSDN Forums you mentioned thus far as there does not seem to be much activity on those boards.

Share this post


Link to post
Share on other sites
Neil Kerkin    164
OK so I've checked out the MDX Library which seems to be a lot closer to what I want to do, however even with this control (modified to work with the latest DX SDK) you seem to only be able to render to one target at a time. The community on this site seems dead also.


Surely someone, somewhere has created a control of this nature using Manaed DirectX.


I'll simplify my question. Doesn't anyone know whether what I am trying to do is actually possible? ie create a generic DirectX control.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this