I have managed to fumble my way to some degree of success with DirectX... only real problem I have now is that my render meshes take on the aspect ratio of the windows form object... ?
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Timers;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace Model
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
private Device device = null;
private Mesh NeutronMesh;
private Material NMaterial;
private float NMass = (float)1.67495438;
private float NRad = (float)1.11328405736;
private Mesh ProtonMesh;
private Material PMaterial;
private float PMass = (float)1.67264858;
private float PRad = (float)1.11277296101;
private Mesh ElectronMesh;
private Material EMaterial;
private float EMass = (float).00091095344;
private float ERad = (float).0908734583548;
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
InitializeDevice();
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
this.Render();
this.Invalidate();
}
protected override void Dispose(bool disposing)
{
base.Dispose( disposing );
}
private void InitializeDevice()
{
Caps caps = Manager.GetDeviceCaps( Manager.Adapters.Default.Adapter,
DeviceType.Hardware );
CreateFlags flags;
if( caps.DeviceCaps.SupportsHardwareTransformAndLight )
flags = CreateFlags.HardwareVertexProcessing;
else
flags = CreateFlags.SoftwareVertexProcessing;
PresentParameters pp = new PresentParameters();
pp.BackBufferFormat = Format.Unknown;
pp.SwapEffect = SwapEffect.Discard;
pp.Windowed = true;
pp.EnableAutoDepthStencil = true;
pp.AutoDepthStencilFormat = DepthFormat.D16;
pp.PresentationInterval = PresentInterval.Default;
device = new Device( 0, DeviceType.Hardware, this, flags, pp );
device.VertexFormat = CustomVertex.PositionNormalColored.Format;
device.DeviceReset += new System.EventHandler( this.OnResetDevice );
OnResetDevice( device, null );
}
public void OnResetDevice(object sender, EventArgs e)
{
Device device = (Device)sender;
device.Transform.Projection = Matrix.PerspectiveFovLH( Geometry.DegreeToRadian( 45.0f ), (float)this.ClientSize.Width / this.ClientSize.Height, 0.1f, 100.0f );
device.RenderState.Lighting = true;
device.RenderState.Ambient = Color.White;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Diffuse = Color.White;
device.Lights[0].Direction = new Vector3(1, -1, 1);
device.Lights[0].Enabled = true;
device.RenderState.CullMode = Cull.None;
device.RenderState.FillMode = FillMode.Solid;
NeutronMesh = Mesh.Sphere( device, NRad, 64, 64 );
NMaterial = new Material();
NMaterial.Ambient = Color.Green;
NMaterial.Diffuse = Color.Green;
NMaterial.Specular = Color.Green;
NMaterial.SpecularSharpness = 100;
ProtonMesh = Mesh.Sphere( device, PRad, 64, 64 );
PMaterial = new Material();
PMaterial.Ambient = Color.DarkRed;
PMaterial.Diffuse = Color.DarkRed;
PMaterial.Specular = Color.DarkRed;
PMaterial.SpecularSharpness = 100;
ElectronMesh = Mesh.Sphere( device, ERad, 64, 64 );
EMaterial = new Material();
EMaterial.Ambient = Color.White;
EMaterial.Diffuse = Color.White;
EMaterial.Specular = Color.White;
EMaterial.SpecularSharpness = 100;
}
private void SetupMatrices()
{
device.Transform.World = Matrix.Identity;
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, -30), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1, 1, 100);
}
private void Render()
{
SetupMatrices();
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DimGray, 1, 0 );
device.BeginScene();
device.Transform.World = Matrix.Translation(0, 0, 0);
device.Material = NMaterial;
NeutronMesh.DrawSubset(0);
device.Transform.World = Matrix.Translation(5,5,-10);
device.Material = PMaterial;
ProtonMesh.DrawSubset(0);
device.Transform.World = Matrix.Translation(7,7,7);
device.Material = EMaterial;
ElectronMesh.DrawSubset(0);
device.EndScene();
device.SetStreamSource(0, NeutronMesh.VertexBuffer, 0);
device.VertexFormat = CustomVertex.PositionColored.Format;
device.Present();
}
}
}