XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   
My XNA Book
      
       Go to section on this site

Additional info


Latest Forum posts

 Account settings
  Posted by: Anonymous
  When: 07/05/2014 at 09:48:39

 forced subtitle
  Posted by: Applefly
  When: 07/05/2014 at 06:00:48

 convert DVD into PMS
  Posted by: Applefly
  When: 07/05/2014 at 05:55:25

 DVD to Digital Copy easily
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:52:29

 DVD on Xbox 360/Xbox One Console
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:51:47

 Extract .Srt Subtitles
  Posted by: Applefly
  When: 04/05/2014 at 03:54:38

 Encode Movie collection
  Posted by: Applefly
  When: 04/05/2014 at 03:52:41

 Convert DVD to WMV
  Posted by: Applefly
  When: 29/04/2014 at 05:53:50

 rip DVDs into digital files
  Posted by: Applefly
  When: 29/04/2014 at 05:51:20

 iTunes movies/music to Kindle Fire
  Posted by: ciciyu80
  When: 29/04/2014 at 05:10:20


Ads

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:




DirectX Tutorial 1 - Starting code

If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !



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);
             }
         }
     }
 }


Google
 
Webwww.riemers.net


If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !



- Website design & XNA + DirectX code : Riemer Grootjans -
©2003 - 2011 Riemer Grootjans
Translations

This site in English
This site in Korean
This site in Czech

Microsoft MVP Award



2007 - 2011 MVP Award
DirectX - XNA

Contents

News
Home
Forum
XNA 2.0 Recipes Book (8)
XNA 3.0 Recipes Book (8)
Downloads
Extra Reading (3)
Matrices: geometrical
Matrix Mathematics
Homogenous matrices
Community Projects (1)
Tutorials (160)
XNA 4.0 using C# (89)
DirectX using C# (54)
Series 1:Terrain (14)
Series 2: Flightsim (19)
Starting code
Textures
The floorplan
Creating the 3D City
Meshloading from file
Ambient light
Action
Flight kinematics
Collision detection
Skybox
Texture filtering
Adding targets
Point sprites
Alpha blending
DirectSound
Sounds in 3D
Playing MP3 files
Displaying text
Going fullscreen
Series 3: HLSL (19)
Short Tuts (2)
DirectX using C++ (15)
DirectX using VB (2)
-- Expand all --


Thank you!

Support this site --
any amount is welcome !

Stay up-to-date

I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!