Jim's Blog Ramblings about novels, comics, programming, and other geek topics

28Feb/088

How to create a heightmap and skydome using Terragen for XNA

Google AdSense

In this example, I'm going to use the Generated Geometry Sample provided by the XNA Creators Club as the code base, so we can just focus on creating heightmaps and the skydome.

Terragen is a great scenery generator and best of all, it's free for personal (non-commercial) use. However, the unregistered version has some limitations such as the image dimensions that you can generate. There's a great gallery of Terragen graphics showcasing it's abilities. Terragen also has numerous plug-ins for other 3D software packages such as Blender, 3D Studio MAX, LightWave 3D, Maya, etc.

The heightmap used by the sample is named "terrain.bmp" and it's dimensions are 257x257. The sky image used by the sample is named "sky.bmp" and it's dimensions are 2048x1024. Both of these images are found in the root Content folder.

Generating the heightmap

When Terragen opens, you will find the Landscape dialog at the foreground. To generate a random terrain, click the Generate button. On the Terrain Genesis dialog, you can play with the various options for a bit. Finally, click the Generate Terrain button and then the Close button.

To preview the terrain, click the Render Preview button on the Rendering Control dialog. Without a texture applied, you probably won't see much.

Exporting the heightmap

To change the file dimensions, click on the Size button of the Landscape dialog (located in the Terrain panel). You can increase the size to 513x513 with the unregistered version of Terragen. Registering the software will allow you to export to higher resolutions.

You can export the surface to a RAW image format and then convert it to an image format such as BMP, JPG, etc. that is more friendly with the XNA environment. Alternatively, you can download the FEO (For Export Only) plug-in that enables you to save your terrains to BMP, DXF, RIB, and OBJ formats (once you follow the install instructions for FEO, you can click on Accessories and Export to BMP).

Once you get the image into a BMP format (or other XNA compatible format), just copy the file into your Content folder. We'll get back to that after we finish the skybox.

Generating the skydome

Now that we have the heightmap finished, we can move on to the skydome. In this sample, the skydome is just one image that wraps around the terrain. We can create this by taking four images (North, East, South, West) from the center of our scene and stitching them together.

First, we need to setup our scene.

Uncheck the Fixed Height Above Surface for camera and target (2 checkboxes) in the Camera panel.

Find a good spot to use as the center point. I chose the center of my surface (x=128, y=128 terrain units). I played around a bit with the altitude (z) until I wasn't below the surface.

Increase your detail slider to the max (rightmost) in the Image panel.

We need to apply some texture to the surface so our image will look nice. Click the Open button in the Surface Map panel of the Landscape dialog. Terragen comes with a handful of surface maps that you can use (you'll may need to browse to your Terragen install directory). I'll chose GrassAndSnow2.srf for my surface.

Exporting the skydome

Set your Image Width and Height to 960 and 960 (the best resolution you can export with unregistered is 1280x960).

The next few steps will take the longest time. You'll need to change your camera settings, render an image, and save the image for each of our four directions.

Alternatively, you can download the script at the end of this article to automate the process. It will still take a while to render each of the images, but at least you can walk away for a few minutes.

View Name Camera Heading Camera Pitch Camera Bank
North 0 0 0
East 90 0 0
South 180 0 0
West 270 0 0

Change the camera orientation to the above parameters, render the image, and then export the image. Once all 4 images are done, stitch the images together in a clockwise manner (N->E->S->W or S->W->N->E, etc.).

You may need to reduce the combined width of the image to 2048 to run on your PC.

Testing the scene

Open the GeneratedGeometryWindows solution.

Note: If you overwrote the files from the sample using the same filenames, then you can skip this part and just start the sample application to view your work. You will need to rebuild the solution, so that the XNB files are updated via the pipeline otherwise, it will load the old XNB files.

Adding the files to the project

In the Solution Explorer, make sure you enable the Show All Files option by clicking the button above the solution tree. Expand the Content folder (you may need to refresh the file list).

Select the terrain heightmap image from the previous steps, right click, and choose Include In Project. View the heightmap image properties and change the Content Processor to TerrainProcessor.

Select the sky image from the previous steps, right click, and choose Include In Project. View the sky image properties and change the Content Processor to SkyProcessor

Make code changes

Update the code in the function called GeneratedGeometry.LoadContent. The model loaded for the terrain object should be the filename that you created (do not include the file extension). The sky file loaded for the sky object should be the skybox image that you created (do not include the file extension).

Run the application

Build your solution and then click Start Debugging (or just press F5) to see your newly created heightmap and skydome. You may also want to turn off the Fog effects drawn on the terrain (in the GeneratedGeometry.DrawTerrain function).

Notes on the Generated Geometry sample

The sample code uses a tiled texture for the terrain surfaces, so unless you replace "rocks.bmp" with a more fitting texture, your landscape scene might look a bit out of place.

Skybox Script for Terragen

Copy the following script into Notepad and save as "SkyboxScript.tgs". Execute this script in the above steps. The below script will generate 4 images in your C:temp directory by changing the camera orientation to the same parameters as listed in the table above. The frend (frame render) command renders the image and saves to your temp folder. The images will be named skydome0001, skydome0002, skydome0003, and skydome0004. So just stitch them together in the same order.

initanim "C:tempskydome", 1 

;_north
Zoom 1.0
CamH 0
CamP 0
CamB 0
frend 

;_east
Zoom 1.0
CamH 90
CamP 0
CamB 0
frend 

;_south
Zoom 1.0
CamH 180
CamP 0
CamB 0
frend 

;_west
Zoom 1.0
CamH 270
CamP 0
CamB 0
frend
 

Update 1 (3/4/2008): Some of my friends remarked that this post was useless without pics. With 3G of upload space remaining here, I might as well upload a few.

Scene example - generated from Terragen, default parameters
Terragen Scene Render

Heightmap example - generated from Terragen, default parameters
Terragen Heightmap Example

Skydome image example - generated from Terragen, default parameters using script above
Terragen Skydome Landscape render

James Welch

James Welch is a software engineer in Vermont working for a large information technology company and specializing in .NET. Additionally, he holds a Master’s Degree in Software Engineering and a Bachelor of Science Degree in Computer Science. Jim also enjoys local craft beer, comic books, and science-fiction and fantasy novels, games, and movies.

Twitter Google+ 

Comments (8) Trackbacks (0)
  1. Script didnt save any images when I ran it. Even created the temp folder and gave “eveyrone” full access to it.

  2. Are you using Vista or having any security settings, etc. issues?

    Also, check the root of your C drive to make sure the files weren’t put there.

  3. Hi there!

    Jim, thanks so much for your post, it’s simple to follow and works perfectly. It has helped me a lot!! o/

    Just a note about your script, you missed the slashs in it, and because of that, it actually saves the images where the script is saved. So, the path in first line of it have to be changed to: “C:tempskydome”. Hope it helps! see ya!

  4. How do i sitch the 4 images? Any app to do that?

  5. Hello,
    I just wondered whether the picture above is made in XNA or in Terragen. Thanks in advance for your answer.

  6. Thanks for the post. Have you tried using World Machine? If so, which do you prefer: free version of World Machine or free version of Terragen?


Leave a Reply

No trackbacks yet.