|
|
|
|
Initialisation of our Project |
Hi there! Glad you made it to this second series of the DirectX for C# Tutorials. We’re going to cover some new DirectX features, put them together into one project, and end up with a real flight simulator! Again, the main goal is to show you the principles of DirectX, so don’t expect the real-life flight physics, such as gravity, coriolis and others. This would add too much maths, and draw our attention away from the DirectX part. If you have finished this series, you can always improve the math sections of it (and send it over to me ;) ).
The only purpose of this first chapter is to set up our starting code. The code is based on the Camera chapter of the first series of C# tutorials and contains nothing new. The aims of this code are:
- Opening a window - Linking to the Device - Creating a Z buffer and clearing it - Positioning the camera
So open a new C# Windows Application project and copy-paste the code below into it. Make sure to add references to Microsoft.DirectX, Microsoft.Direct3D and Microsoft.Direct3DX by clicking Project -> Add References, or you will be facing errors like ‘The type or namespace name 'DirectX' does not exist in the namespace’. Also make sure you use the same version number of both references, or your project will not compile.
Compiling and running the code should give you an empty window, cleared to a color of your choice by DirectX:

Click here to go to the forum on this chapter!
We’re ready to start our second project.
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; using D3D = Microsoft.DirectX.Direct3D; namespace DirectX_Tutorial { public class WinForm : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; private D3D.Device device; public WinForm() { InitializeComponent(); this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); } public void InitializeDevice() { PresentParameters presentParams = new PresentParameters(); presentParams.Windowed = true; presentParams.SwapEffect = SwapEffect.Discard; presentParams.AutoDepthStencilFormat = DepthFormat.D16; presentParams.EnableAutoDepthStencil = true; device = new D3D.Device(0, D3D.DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); device.RenderState.Lighting = false; } protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0f, 0); device.BeginScene(); device.EndScene(); device.Present(); this.Invalidate(); } private void SetUpCamera() { device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 0.3f, 500f); device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 30), new Vector3(0, 0, 0), new Vector3(0, 1, 0)); } protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(500, 500); this.Text = "Riemer's DirectX Tutorial using C# -- Season 2"; } static void Main() { using (WinForm our_directx_form = new WinForm()) { our_directx_form.InitializeDevice(); our_directx_form.SetUpCamera(); Application.Run(our_directx_form); } } } }
- Website design & XNA + DirectX code : Riemer Grootjans - ©2003 - 2008 Riemer Grootjans
|
|
|
|
|