Mini 3D Terrain Viewer - C#/DirectX Example - Patrick Murris

Mini 3D Terrain Viewer - C#/DirectX Example

By Patrick Murris - last updated april 26, 2007 (1.5 beta 1 update).

I allways liked to "spin" 3D maps and terrain, and over the years, i have tried various programming languages to that effect (VRML, Director, Java)... Here is the .NET/DirectX version of my mini terrain viewer - source included.

Terrain Viewer

3D terrain from SRTM .bil

This tiny application allows you to examine a portion of 3D terrain. It uses an 8 bit greyscale elevation map (DEM) and an optional matching RGB texture - usually an aerial ortho image or a satellite picture. It can also read 16 bit .bil SRTM elevation files and produce colorized maps using 'color scales'. Version 1.4 comes with a '3D Cross Section' plugin for NASA World Wind that allows to cut terrain pieces out of the planets.

A Maps menu allows you to switch between different terrain portions or maps, described in an XML configuration file.

This program requires that you have Microsoft .NET runtime environment and DirectX installed.

This is really a simple piece of software (one class and 2000 lines of now quite messy code) and doesnt intend to compete with any serious GIS or mapping tool. I just wish it may be usefull to somebody ;)

Installation and Usage

Mt St-Helens, WA, USA
3D Cross Section from World Wind

Install

Unzip the downloaded file into your drive:\Program Files\ folder, with its directory structure preserved, so that a new 'TerrainViewer' or '3DCrossSection' directory is created inside.

To Launch the application, double click TerrainViewer.exe inside that new directory.

NASA World Wind

If you want to use it as a plugin to NASA World Wind, just copy the directory inside Program Files\NASA\World Wind 1.4\Plugins\. Once WW is loaded, Click Plugins, Load/Unload... and load the 3D Cross Section plugin. After that, a new menu option is available in the Plugins menu. It allows to select and cut out a portion of terrain and see it right away in the terrain viewer.

Usage

Mouse :

  • Mouse wheel : move closer or away from the map
  • Mouse left button and drag : move the map
  • Mouse right button and drag : tilt and rotate the map
  • Both mouse buttons and drag : change light position

Keyboard :

  • + / - : move closer or away from the map
  • Shift + / - : change camera field of view (zoom)
  • Ctrl + / - : change terrain vertical elevation factor
  • Up / down arrows : move back and forth
  • Left / right arrows : rotate the map
  • Shift left / right arrows : move sideways
  • Shift up / down arrows : change map tilt
  • F / Shift F : turns the fog on and off
  • L / Shift L : turns the light on and off (shading)
  • Ctrl L / Shift Ctrl L : compute (or remove) projected shadows
  • Enter / Space bar : turns auto rotation (slow spin) on and off

Dropping DEMs onto the viewer

You can view greyscale .png and SRTM .bil elevation files just by dropping them on the viewer window. They will be processed and rendered as a new colorized terrain. You may have to adjust the vertical scaling factor with Ctrl +/- to get appropriate proportions and use light (key L/Shift L) to have shading.

Adding new maps

DEM and texture files

To make a new map, you need two matching bitmaps for terrain elevation (DEM) and texture :


Terrain DEM and texture for Mt St-Helens, WA, USA

Both bitmaps can have different sizes (the DEM is usualy much smaler) but must have the same proportions. They can be square or rectangular, the bigger the better (keep DEM around or below 512x512 pixels for better performance though). Supported format are .jpg, .png and .bil - it is recommended to use .png for the DEM to avoid compression artefacts.

Finding and putting together such images can be very tricky if you havent tried before. I'll gather some usefull notes and links to help you get started...

Once you have the two bitmaps ready, copy them inside the TerrainViewer\maps directory.

'Geo' color scale w shading
'Grid' color scale w shading

Editing maps.xml

For each new map, you need to add a <map></map> section in the config file maps.xml, located in the application directory.

Open the file with a text editor or Notepad and add the following lines inside the <maps> section :

	<map name="My map name">
		<terrain demfile="maps/My_Map_DEM.png" 
			texturefile="maps/My_Map_Texture.jpg" altscale="1.0" />
		<sky skyfile="skys/Sky_Day1.jpg" />
	</map>

Using color 'scales'

If you do not have an image texture, you can leave the 'texturefile' attribute empty (texturefile="") or specify a color 'scale' to be used (texturefile="colors/Spectrum_1.png") to produce a 'colorized' terrain. Actual color scales are : Geo_1.png, Temp_1.png and Spectrum_1.png.

More skys and color scales can be added to the /skys and /colors directories.

Adjusting the elevation scaling factor

You will have to adjust the 'altscale' value to achieve the proper elevation to distance proportions - if the terrain seems too flat, try 2.0 or 3.0. You can do so while running the viewer : do the change in Notepad, save, and reload the current map by selecting it again in the Maps menu. You can also use Ctrl +/- from the keyboard.

Screenshots


Mont Blanc with fog, shading and shadows from NASA World Wind


Terrain 'extraction' from NASA World Wind


Slice of Europe from World Wind


Shadows over Glacier National Park, USA


Grand Canyon - from World Wind with I Cubed Landsat 7

Quotes

    "I'm doing serious re-interpretation of the neotectonics of WA with that tool! Its brilliant for tracking the features with subtle topo changes, as on the Nullarbor plain - try generating a section down there, with 20 to 30x VE, and look for the sub-metre scale fault scarps. Marvellous beast."
    Roger Hocking aka radhock, geologist and WW user, Australia

Links