Tutorial 1, DirectX using C#
DirectX using C#
Tutorial 1
The Blue Window
- Create a new project in C#
- Add reference to Microsoft.DirectX
- Add reference to Microsoft.DirectX.Direct3D
- Add reference to Microsoft.DirectX.Direct3DX
Under Form1 we will have a nested class 'CosmicX' which will be our framework.
using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; using D3D = Microsoft.DirectX.Direct3D;
1. Set form style to opaque
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
Try skipping this line and you will come to know about its importance...
2. Turn on lightening
this.device.RenderState.Lighting = true;
3. Our Render Call Back
public void RenderX() { this.device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0f, 0); this.device.BeginScene(); this.device.RenderState.Ambient = Color.DarkGray;
//We'll render objects here
this.device.EndScene(); this.device.Present(); this.form.Invalidate(); }
- Clear the screen and paint it blue
- Render objects
- Invalidate form [back to point 1.]
Output
Complete Code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; using D3D = Microsoft.DirectX.Direct3D; namespace XBasic { public partial class Form1 : Form { class cosmicX { #region -FIELDS- D3D.Device device; Form form; float aspectRatio; #endregion #region -Properties- public D3D.Device Device { get { return device; } set { device = value; } } #endregion #region -Methods- public cosmicX(Form f) { this.form = f; D3D.PresentParameters presentParams = new D3D.PresentParameters(); presentParams.Windowed = true; presentParams.SwapEffect = SwapEffect.Discard; presentParams.AutoDepthStencilFormat = DepthFormat.D16; presentParams.EnableAutoDepthStencil = true; this.Device = new D3D.Device(0, D3D.DeviceType.Hardware, f, CreateFlags.SoftwareVertexProcessing, presentParams); calculateAspectRatio(); } void calculateAspectRatio() { this.device.RenderState.Lighting = true; this.device.Lights[0].Type = LightType.Directional; this.device.Lights[0].Diffuse = Color.White; this.device.Lights[0].Direction = new Vector3(1, 1, -1); this.device.Lights[0].Update(); this.device.Lights[0].Enabled = true; this.device.Lights[1].Type = LightType.Directional; this.device.Lights[1].Diffuse = Color.White; this.device.Lights[1].Direction = new Vector3(-1, -1, -1); this.device.Lights[1].Update(); this.device.Lights[1].Enabled = true; this.aspectRatio = (float)form.Width / (float)form.Height; this.device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.aspectRatio, 0.3f, 500f); } public void RenderX() { this.device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0f, 0); this.device.BeginScene(); this.device.RenderState.Ambient = Color.DarkGray; this.device.EndScene(); this.device.Present(); this.form.Invalidate(); } #endregion } cosmicX X; public Form1() { InitializeComponent(); SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); X = new cosmicX(this); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); X.RenderX(); } } }
Comments
Post a Comment