Click here to Skip to main content
Click here to Skip to main content

Managed DirectX Tutorials: Part 7 - Using Heightmaps

By , 26 May 2006
 
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.

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:

CV[y, x].Z = 0;

with:

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)

About the Author

James Gupta
Web Developer
United States United States
Member
I live in England, UK in a small town which only just got broadband...
 
In my spare time, I run a small website design, hosting and maintenance business at www.jamesgupta.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalno source codemembergokceng13 Jun '09 - 16:31 
I couldnt find any source -height map, source code vs- related to the tutorial. Your links are broken...
GeneralRe: no source codememberDevelopmentNoob14 Jul '09 - 16:15 
ya..the link is broken..x.x
pls rework it..
Generalsome ideasmemberGeorgi Petrov27 Jun '06 - 1:21 
Hi very cooll,
but do you think this will be much easy to reade these nice articles from something like e-book?

GeneralSuggestionmemberDustin Metzgar26 May '06 - 8:44 
I think that terrain mapping is one of those very tired subjects. It's a great thing for programmers to play with because it doesn't require the use of an artist. What I would suggest is trying something more interesting than just a raw terrain map. For instance, the US Geological Survey has several terrain maps covering US terrain and the formats are much more difficult to read than just the raw formats. Perhaps you could write an article showing us a program that loads a .DEM file and lets us fly around over actual terrain?
 
http://edc.usgs.gov/geodata/[^]
GeneralRe: SuggestionmemberJames Gupta26 May '06 - 9:11 

 
Wow, thanks a lot for that suggestion, that would be an interesting project.
I'll look into it, as I have been looking for a way to extend it for a while.
 





 
just-code-it.net
 
Managed DirectX & C# Tutorials
GeneralRe: SuggestionmemberDustin Metzgar20 Oct '06 - 6:07 
I was wondering if you might be interested in this. I mentioned before about reading real terrain maps for use in DirectX. I stumbled across a project called SharpMap[^] that you may want to look into. They claim to be able to read the USGS ASCII DEM file format through a third party extension. If you're at all interested in being able to read terrain maps, you might want to check out how they're doing it.
 

GeneralInteresting [modified]memberRory Plaire26 May '06 - 7:04 
Hi James,
 
Did you know this tutorial looks very close the the material here?[^]?
 
I wonder if there is a relation?
 
-rory
 
-- modified at 13:04 Friday 26th May, 2006 to include link
GeneralRe: Interesting [modified]memberJames Gupta26 May '06 - 8:04 
Of course its going to be similar, the tutorials are doing the same thing
 
Riemer's tutorial is how I learnt to do terrain myself, but since then I have expanded / customised it to my own needs.
 

Please, don't imply that I have "stolen" someones work because not only is it a dumbfounded and potentially insulting accusition, but that is clearly not the case.
Its like comparing two Hello World tutorials. This series of tutorials is clearly different, both in means and results to the tutorial you linked to.
 
just-code-it.net
 
Managed DirectX & C# Tutorials
 
-- modified at 14:08 Friday 26th May, 2006
GeneralRe: Interesting [modified]memberRory Plaire26 May '06 - 9:00 
You should cite your sources. It gives a very bad impression to have such similar work and not give credit. Especially when you espouse such values as these: "so that when I'm rich and famous you can sell them for $100 each Wink | ;) " Even though it may have been a facetious comment, it leave a bad impression - what are you really in this for, the actual contribution, or are you just trying to get your name out there? Be honest and open, and you'll get respsect if you demonstrate skill. Even if you have the skill and are cagey about it, people wonder, and geeks, in particular, are quick to judge.
GeneralRe: Interesting [modified]memberJames Gupta26 May '06 - 9:10 
"so that when I'm rich and famous you can sell them for $100 each"
 
That is *clearly* a joke, to add humour, I like humour, so if I want it in my article I will put it there, and most people would agree its a joke, and that it serves its one and only purpose, to add humour to the article.
 
What am I in this for? Maybe its because I want to make a name, or maybe its because documenting my work serves as proof to myself that i've done something with my time, maybe its because doing these tutorials actually helps me to understand what I'm doing better as I can go over it in my head.
 
The last sentence of your post is a load of rubbish. No-one else here seems to have been quick to judge, some people have given constructive criticism such as "X link is broken", "could you please explain X feature in more detail" etc.
Skill? I'll be honest I consider myself beginner - intermediate at DirectX. But I consider that an advantage when writing these articles, as mentioned before I write them mainly to get my head round it, making sure there is nothing I don't understand. As a result, I can focus on points which other people more experienced would leave out because they could solve it in an instant.
 
Citing Sources...
 
Does that mean that I have to cite that I learnt how to initialise a device from MSDN, intellisense, auto complete or wherever I learnt it from?
No
 
If i had copied and pasted the code, I would add it in a bibliography, but I havent.
That heightmap / data extraction, yes I learnt how to use file IO from Riemer's tutorials about a year ago, but since then I have changed it, adapted it etc.
 
You are being un-necessarily critical, and *that* is what really gives a bad impression.
 
just-code-it.net
 
Managed DirectX & C# Tutorials

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 26 May 2006
Article Copyright 2006 by James Gupta
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid