Click here to Skip to main content
11,920,772 members (60,882 online)
Click here to Skip to main content
Add your own
alternative version


160 bookmarked

3D Terrain Visualisation in Managed DirectX 9 and C#

, 18 Sep 2005
Rate this:
Please Sign up or sign in to vote.
In this project I demonstrate how to write a simple 3D rendering application in a relatively small amount of code.

Point, Wireframe and Solid (Textured) Mode


GIS (Geographical Information System) is a computer support system that represents data using maps. It helps people access, display and analyse data that has geographic content and meaning. For those not familiar with GIS, it used to be a niche IT market dominated by the traditional GIS and CAD companies such as Intergraph, Bentley, MapInfo, Autodesk and ESRI. Nowadays global IT giants such as Microsoft, Google and Oracle are competing for their share of the pie through products such as Virtual Earth, Google Earth and Oracle Spatial. NASA has also recently released a free, open source GIS viewer application called World Wind.

In this article, I will demonstrate how to build a standalone 3D terrain visualisation tool from scratch using C# and Managed DirectX 9.0c. The application will allow the user to rotate the point of view using the arrow keys and to change the rendering mode to (P) Point, (W) Wire frame and (S) Solid.


I recently completed a GIS system implementation for a local City Council. During that project I developed a proof of concept application to demonstrate the technical feasibility of 3D visualisation using the available spot heights and aerial photography textures. The aim of this article is to share my knowledge and experience with all developers interested in GIS and .NET.


Before we start, I would like to specify the software requirements for this project:

  • Visual Studio .NET IDE (I used 2005 beta 2)
  • Managed DirectX 9.0c SDK (I used August 2005 update)
  • .NET framework (I used v2.0 but v1.1 will work as well)

3D rendering concepts

First of all I will need to explain the general 3D programming concepts behind my code. Unfortunately, entire books are written on this topic and I won't be able to give a full explanation for every single line in my code, but instead will attempt to present the most important ideas behind 3D visualisation.

In order to generate any 3D terrain model, you will need some grid based data with X, Y and Z values for each grid point. A very important consideration is how the Z value is stored, as DirectX uses left-handed coordinate system while OpenGL uses right-handed coordinate system (to learn about different coordinate systems, please search the Internet). I have chosen a grid size of 79x88 simply because that is how my source data is stored, but you can change this to any arbitrary grid size. Likewise, my data uses 20m resolution which means that the real distance between two adjacent points is 20 meters.

Point Mode

Once you read in all the points you will need to generate a "mesh". The mesh is an array of triangles constructed from the points you loaded in the previous step. All rendering in 3D is based on triangles and arrays of triangles.

Wireframe Mode

Even the meanest video cards on today's market have limited rendering capability in terms of how many triangles can be drawn per second. Therefore, the less work your video card needs to perform the faster your application runs. This is where optimisation algorithms come into play, such as ROAM or PLOD (the latter is built-in DirectX 9). These and other similar algorithms are aimed at reducing the level of detail and number of triangles located furthest from the view point. The other way to look at this is to say that we are reducing the level of detail where it matters least, while we are preserving the highest possible level of detail where it matters most. We won't be using these algorithms here, however you should be aware of what they are and what they are used for.

Finally, textures are used to provide a more realistic look of the scene. Textures use their own coordinate system, with top left representing 0,0 and bottom right representing 1,1. Any texture point within this range (0,0 - 1,1) is referred to as a "texel".

Textured Mode

As an exercise left to the reader, further enhancements could include the SkyBox, Lighting, Shading or even Physics engine with collision detection etc. Managed DirectX also includes support for DirectPlay and DirectSound with advanced networking and sound APIs. Using your imagination, the sky is the limit!

Using the code

I have built a sample WinForm application using the Visual Studio 2005 IDE. You will need to have Managed DirectX 9.0c SDK installed on your PC for the project to compile and run correctly (I used the August 2005 update). However you can use a much smaller DirectX 9.0c redistributable if you wish to distribute your code to users who don't have Managed DirectX SDK installed on their PC.

OK, let's dive into the code.

First of all, we will import the necessary libraries:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX.DirectInput;

Then, we will declare our grid width and height, screen and keyboard devices, VertexBuffer and IndexBuffer, Texture, Vertex and Triangle structs and a few other variables used throughout the project:

private int GRID_WIDTH = 79;     // grid width
private int GRID_HEIGHT = 88;    // grid height
private Microsoft.DirectX.Direct3D.Device device = null;  // device object
private Microsoft.DirectX.DirectInput.Device keyb = null; // keyboard
private float angleZ = 0f;       // POV Z
private float angleX = 0f;       // POV X
private float[,] heightData;     // array storing our height data
private int[] indices;           // indices array
private IndexBuffer ib = null; 
private VertexBuffer vb = null;
private Texture tex = null;
//Points (Vertices)
public struct dVertex
  public float x;
  public float y;
  public float z;
//Created Triangles, vv# are the vertex pointers
public struct dTriangle
  public long vv0;
  public long vv1;
  public long vv2;
private System.ComponentModel.Container components = null;

Now we're ready to initialise our Direct3D device object:

// define parameters for our Device object
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.EnableAutoDepthStencil = true;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
// declare the Device object
device = new Microsoft.DirectX.Direct3D.Device(0, 
             Microsoft.DirectX.Direct3D.DeviceType.Hardware, this, 
             CreateFlags.SoftwareVertexProcessing, presentParams);
device.RenderState.FillMode = FillMode.Solid;
device.RenderState.CullMode = Cull.None;
// Hook the device reset event
device.DeviceReset += new EventHandler(this.OnDeviceReset);
this.OnDeviceReset(device, null);
this.Resize += new EventHandler(this.OnResize);

As you can see we have wired up the OnDeviceReset event which fires up every time the user resizes the application window. Our points are stored in a VertexBuffer:

// create VertexBuffer to store the points
vb = new VertexBuffer(typeof(CustomVertex.PositionTextured), 
         GRID_WIDTH * GRID_HEIGHT, device, Usage.Dynamic | Usage.WriteOnly, 
         CustomVertex.PositionTextured.Format, Pool.Default);
vb.Created += new EventHandler(this.OnVertexBufferCreate);
OnVertexBufferCreate(vb, null);

Then, we need to instantiate an IndexBuffer, from which our triangular mesh is constructed. IndexBuffer stores an ordered list into the Vertex data:

ib = new IndexBuffer(typeof(int), (GRID_WIDTH - 1) * 
     (GRID_HEIGHT - 1) * 6, device, Usage.WriteOnly, Pool.Default);
ib.Created += new EventHandler(this.OnIndexBufferCreate);
OnIndexBufferCreate(ib, null);

Also pay attention to InitialiseIndices() and LoadHeightData() functions in the source code attached, where we load and "triangulate" our data. Next, we initialise the keyboard device:

public void InitialiseKeyboard()
  keyb = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
  keyb.SetCooperativeLevel(this, CooperativeLevelFlags.Background |

Then, we position our camera:

private void CameraPositioning()
  device.Transform.Projection = 
     this.Width/this.Height, 1f, 350f);
  device.Transform.View = 
     Matrix.LookAtLH(new Vector3(0, -70, -35), new Vector3(0, -5, 0), 
     new Vector3(0, 1, 0));
  device.RenderState.Lighting = false;
  device.RenderState.CullMode = Cull.None;

Almost done, only a few steps left. Now we will override the OnPaint event and provide our own event handling code:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.LightBlue , 1.0f, 0);
  // set the camera position
  // draw the scene     
  device.SetTexture(0, tex);
  device.VertexFormat = CustomVertex.PositionTextured.Format;
  device.SetStreamSource(0, vb, 0);
  device.Indices = ib;
  device.Transform.World = 
         Matrix.Translation(-GRID_WIDTH/2, -GRID_HEIGHT/2, 0) *   
  device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, GRID_WIDTH * 
                               GRID_HEIGHT, 0, indices.Length/3);

Finally, we need to write our main procedure and we're done:

static void Main() 
  using (WinForm directx_form = new WinForm())

Now compile and run. You should get the results as shown in the pictures at the top. Use the arrow keys on your keyboard to drive the application, and press P, W and S to switch between different rendering modes: Point, Wire frame and Solid. Cool, huh?

Points to note

One thing you'll quickly learn to appreciate is how time consuming 3D programming can be. Even the smallest detail or effect you wish to implement could take many painful days; however, the entire experience is very rewarding once you overcome the hurdles. My suggestion to everyone is to use the Internet in the first instance and search about the problem you're trying to solve. The chances are, someone has already done what you're trying to do, and better still, the problem may have been documented and solved. If you're lucky, a step-by-step tutorial or code snippets may be readily available showing you how to solve your problem.


  • For those people who have some spare cash in their wallets, there is an excellent book available for Managed DirectX programming in C#:
    • Managed DirectX 9 - Graphics and Game Programming, by Tom Miller.
  • Also, as a starting point there is an excellent DirectX 9 tutorial using C# available here.
  • Another great site with lots of practical examples in DX9 and C# is available here.


  • 19th September 2005: First release.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Igor Stjepanovic
Founder GIS People
Australia Australia
I have filled variety of roles ranging from Junior Software Engineer to GIS Team Leader. My background is in real-time spatial solutions, including mobile data capture and advanced road network data modelling.

In my projects I have utilised and integrated technologies such as GPS, Oracle Spatial, FME, MapInfo and ESRI suite of tools.

I'm proficient in all aspects of the software development lifecycle and I'm holding a degree in Computer Science from Queensland University of Technology.

You may also be interested in...

Comments and Discussions

QuestionSuper-impose vector data Pin
Member 370746116-Apr-15 23:06
memberMember 370746116-Apr-15 23:06 
QuestionC++ support Pin
Member 1155085224-Mar-15 2:27
memberMember 1155085224-Mar-15 2:27 
QuestionCompilation with Visual Studio 2010 Pin
Gagnon Claude29-Jun-11 17:36
memberGagnon Claude29-Jun-11 17:36 
AnswerRe: Compilation with Visual Studio 2010 Pin
schubert0125-Aug-11 18:49
memberschubert0125-Aug-11 18:49 
GeneralRe: Compilation with Visual Studio 2010 [modified] Pin
Igor Stjepanovic27-Aug-11 23:21
memberIgor Stjepanovic27-Aug-11 23:21 
Generalsize limitation on overlay bitmap Pin
slavo.salma13-May-09 15:11
memberslavo.salma13-May-09 15:11 
GeneralRe: size limitation on overlay bitmap Pin
Igor Stjepanovic13-Aug-09 16:32
memberIgor Stjepanovic13-Aug-09 16:32 
QuestionFullScreen? Is the Windows.Form neccessary? Pin
Member 427848522-Oct-08 22:00
memberMember 427848522-Oct-08 22:00 
GeneralManaged DirectX Pin
newspicy16-Sep-08 3:30
membernewspicy16-Sep-08 3:30 
QuestionBlue screen Pin
Taher Hassan25-Nov-07 13:37
memberTaher Hassan25-Nov-07 13:37 
General3d games in directx Pin
Rick8124-Sep-07 8:58
memberRick8124-Sep-07 8:58 
Generalgrid size Pin
jtby230-Aug-07 13:21
memberjtby230-Aug-07 13:21 
GeneralRe: grid size Pin
Igor Stjepanovic13-Aug-09 16:37
memberIgor Stjepanovic13-Aug-09 16:37 
GeneralZ-Buffer Pin
sithira7731-Jul-07 0:27
membersithira7731-Jul-07 0:27 
GeneralRe: Z-Buffer Pin
Igor Stjepanovic15-Aug-07 16:00
memberIgor Stjepanovic15-Aug-07 16:00 
Generalfloat.Parse Pin
gajatko1-Jul-07 3:38
membergajatko1-Jul-07 3:38 
GeneralRe: float.Parse Pin
gajatko2-Jul-07 1:02
membergajatko2-Jul-07 1:02 
GeneralData type Pin
muzzaukuk24-Nov-06 12:06
membermuzzaukuk24-Nov-06 12:06 
GeneralVirus Pin
Murray Roke11-Jul-06 2:11
memberMurray Roke11-Jul-06 2:11 
Hi Igor,
AVG found a virus in your project zip file.

GeneralRe: Virus Pin
Igor Stjepanovic11-Jul-06 2:13
memberIgor Stjepanovic11-Jul-06 2:13 
GeneralPlease take a minute to vote Pin
Igor Stjepanovic9-Jul-06 23:44
memberIgor Stjepanovic9-Jul-06 23:44 
GeneralRe: Please take a minute to vote Pin
giloutho10-Jul-06 2:10
membergiloutho10-Jul-06 2:10 
GeneralRe: Please take a minute to vote Pin
Darren Ashenden19-Feb-07 15:31
memberDarren Ashenden19-Feb-07 15:31 
GeneralProblems in localized Windows [modified] Pin
ivandasch20-Jun-06 9:59
memberivandasch20-Jun-06 9:59 
GeneralRe: Problems in localized Windows Pin
giloutho9-Jul-06 1:59
membergiloutho9-Jul-06 1:59 
GeneralZoom In/Out .. Pin
sahiljain2223-May-06 21:06
membersahiljain2223-May-06 21:06 
GeneralRe: Zoom In/Out .. Pin
Igor Stjepanovic28-May-06 19:53
memberIgor Stjepanovic28-May-06 19:53 
GeneralRe: Zoom In/Out .. Pin
Igor Stjepanovic4-Jun-06 21:52
memberIgor Stjepanovic4-Jun-06 21:52 
GeneralRe: Zoom In/Out .. Pin
Andrew Phillips8-Aug-07 20:55
memberAndrew Phillips8-Aug-07 20:55 
QuestionHow to add a 3D point object to viewer? Pin
Phil J B14-May-06 18:31
memberPhil J B14-May-06 18:31 
AnswerRe: How to add a 3D point object to viewer? Pin
Igor Stjepanovic16-May-06 14:09
memberIgor Stjepanovic16-May-06 14:09 
GeneralRe: How to add a 3D point object to viewer? Pin
Phil J B16-May-06 15:19
memberPhil J B16-May-06 15:19 
AnswerRe: Sorry, but i need your help Pin
Igor Stjepanovic8-May-06 2:11
memberIgor Stjepanovic8-May-06 2:11 
QuestionRe: Sorry, but i need your help Pin
cloogey5-Oct-06 12:58
membercloogey5-Oct-06 12:58 
GeneralGot a blue window Pin
GrayGryphon22-Feb-06 19:49
memberGrayGryphon22-Feb-06 19:49 
GeneralRe: Got a blue window Pin
GrayGryphon23-Feb-06 16:29
memberGrayGryphon23-Feb-06 16:29 
GeneralRe: Got a blue window Pin
Igor Stjepanovic24-Feb-06 16:07
memberIgor Stjepanovic24-Feb-06 16:07 
QuestionCamera Movement? Pin
bthalken6-Jan-06 10:08
memberbthalken6-Jan-06 10:08 
AnswerRe: Camera Movement? Pin
Igor Stjepanovic26-Jan-06 12:37
memberIgor Stjepanovic26-Jan-06 12:37 
QuestionUseing procedural texturing with GIS-data Pin
Hans Ekstrom7-Dec-05 5:59
memberHans Ekstrom7-Dec-05 5:59 
AnswerRe: Useing procedural texturing with GIS-data Pin
Igor Stjepanovic8-Dec-05 11:54
memberIgor Stjepanovic8-Dec-05 11:54 
GeneralNon-gridded data Pin
Morten on GIS6-Dec-05 23:11
memberMorten on GIS6-Dec-05 23:11 
GeneralRe: Non-gridded data Pin
Igor Stjepanovic6-Dec-05 23:46
memberIgor Stjepanovic6-Dec-05 23:46 
GeneralRe: Non-gridded data Pin
Morten on GIS7-Dec-05 0:15
memberMorten on GIS7-Dec-05 0:15 
GeneralPower of two Pin
Morten on GIS6-Dec-05 22:40
memberMorten on GIS6-Dec-05 22:40 
GeneralRe: Power of two Pin
Igor Stjepanovic6-Dec-05 23:37
memberIgor Stjepanovic6-Dec-05 23:37 
GeneralError in OnVertexBufferCreate... Pin
bukagija24-Nov-05 5:56
memberbukagija24-Nov-05 5:56 
GeneralRe: Error in OnVertexBufferCreate... Pin
bukagija24-Nov-05 6:14
memberbukagija24-Nov-05 6:14 
GeneralRe: Error in OnVertexBufferCreate... Pin
Igor Stjepanovic25-Nov-05 1:27
memberIgor Stjepanovic25-Nov-05 1:27 
GeneralGood think, I need Help in Managed DirectX Pin
walid10018-Nov-05 15:54
memberwalid10018-Nov-05 15:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151120.1 | Last Updated 18 Sep 2005
Article Copyright 2005 by Igor Stjepanovic
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid