Click here to Skip to main content
15,879,535 members
Articles / Web Development / ASP.NET

Managed DirectX Tutorials: Part 7 - Using Heightmaps

Rate me:
Please Sign up or sign in to vote.
3.40/5 (3 votes)
26 May 2006CPOL3 min read 61.2K   25   10
Here it really gets interesting - you will add height values to your vertices
Sample Image - MDXTut_7.png

What Are Heightmaps and How To Read Them

In this (rather short) tutorial we will learn about heightmaps, and use them to add different height levels to our vertices to create a realistic-looking terrain.

What is a Heightmap?

A heightmap is a file used by game programmers to read height data.
In form, they are an image file, usually with as many pixels as the amount of vertices in the game world.

They only have greyscale colour. The program loads this heightmap into memory, and tests the colour value of each pixel. If it is pure white, (255), then the height of the vertex at that pixel location will be the highest (this is usually 255 scaled down by a factor), whereas if it is black it is at the lowest position.

For simplicity, heightmaps are often saved in the .RAW file format.
This is an image format which just stores the pixel data, without any compression or anything else.

These can be opened in image editors such as Adobe Photoshop Elements, and work in a similar way to bmp.

How will we apply it to our terrain?

To apply a heightmap to our terrain, we will simply modify the SetGrid() method we created in the last tutorial to accept a 2D array of points (which we will apply to the Z-axis) and then create a new method to load the height data into it.

C#
public int[,] heightFromFile(string pFile, int WIDTH, int HEIGHT)
{ 
int[,] HeightData = new int[WIDTH, HEIGHT];
FileStream fs = new FileStream(pFile, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
int Currheight = new int();for (int i = 0; i < HEIGHT; i++)
for (int y = 0; y < WIDTH; y++)
{
Currheight = (int)(r.ReadByte()/2);
HeightData[WIDTH - 1 - y, HEIGHT - 1 - i] = Currheight;
}
r.Close();
return (HeightData);
}

The first four lines of this method are just setting it up.
FileStream opens a link between the program and a particular file (in this case, the one passed through parameter pFile), and BinaryReader "attaches" itself to this stream so it can literally read the values of the heightmap.

We then loop through the file, reading the current byte value (ReadByte) and dividing it by two. This gives us a value out of 255, and then the division is simply to scale it down (we do not want our terrain to be able to be that high).
We then assign this value to the appropriate HeightData element.
Finally, we close the stream between the file and return the HeightData array, now full to the program.

Next, change the SetGrid() method so that it accepts a 2D int array called heightData, and replace the line:

C#
CV[y, x].Z = 0;

with:

C#
CV[y, x].Z = heightData[y, x]; 

Finally, we can use the heightData member of the Form class in the previous sample, and load it using our new methods.
If everything went well, you will have heightmapped terrain when you run the sample. :)
Next, we will learn to move around this terrain.

Feedback

I am always open for answering questions, whether through MSN (jamespraveen@aol.com), email (james@magclan.cwhnetworks.com) or through the message board attached to this article.
If you have a problem with any of my samples / my topics in general, please feel free to ask me.

History

  • 26/05/06: Posted on CodeProject

Previous Articles

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Synap
United Kingdom United Kingdom
Founder & CEO of Synap, an online education platform that uses machine learning to help people learn more in less time.

Software developer, main languages currently are Objective-C, MySQL and Javascript, though I got started on C++, C# and PHP.

Comments and Discussions

 
Generalno source code Pin
gokceng13-Jun-09 16:31
gokceng13-Jun-09 16:31 
GeneralRe: no source code Pin
DevelopmentNoob14-Jul-09 16:15
DevelopmentNoob14-Jul-09 16:15 
Generalsome ideas Pin
Georgi Petrov27-Jun-06 1:21
Georgi Petrov27-Jun-06 1:21 
GeneralSuggestion Pin
Dustin Metzgar26-May-06 8:44
Dustin Metzgar26-May-06 8:44 
GeneralRe: Suggestion Pin
James Gupta26-May-06 9:11
professionalJames Gupta26-May-06 9:11 
GeneralRe: Suggestion Pin
Dustin Metzgar20-Oct-06 6:07
Dustin Metzgar20-Oct-06 6:07 
GeneralInteresting [modified] Pin
codekaizen26-May-06 7:04
codekaizen26-May-06 7:04 
GeneralRe: Interesting [modified] Pin
James Gupta26-May-06 8:04
professionalJames Gupta26-May-06 8:04 
GeneralRe: Interesting [modified] Pin
codekaizen26-May-06 9:00
codekaizen26-May-06 9:00 
GeneralRe: Interesting [modified] Pin
James Gupta26-May-06 9:10
professionalJames Gupta26-May-06 9:10 

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.