[.net] Managed DirectX Control

Started by
2 comments, last by Neil Kerkin 18 years, 9 months ago
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

	}
}


Advertisement

You might want to have a look at the following post, it has a referance to an application that is using Managed DirectX inside a User Control. It might help you to get yours going.
Inner Realm - Managed DirectX in a User Control

Also You might be able to check here for more information,
MSDN Forums - Windows Game Development
Mykre - BlogVirtual Realm :- XNA News and Resources from Down Under** For those Interested in an Australian XNA User Group Contact me though my site.
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.

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.

This topic is closed to new replies.

Advertisement