5,530,111 members and growing! (14,924 online)
Email Password   helpLost your password?
Multimedia » General Graphics » General     Beginner

Managed DirectX Tutorials: Part 7 - Using Heightmaps

By James Gupta

Here it really gets interesting - you will add height values to your vertices
C#, Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 26 May 2006
Updated: 26 May 2006
Views: 20,226
Bookmarked: 11 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 1.62 Rating: 3.40 out of 5
1 vote, 33.3%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
2 votes, 66.7%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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 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), wheras if it is black it is at the lowest position.
The following is an example of a heightmap:

As you can see, it is greyscale, where the height will increase as the value does (ie whiter).

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 "attatches" 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 attatched to this article.
If you have a problem with any of my samples / my topics in general please feel free to ask me.
Or you can post on my forums at http//www.just-code-it.net

History

26/05/06: Posted on CodeProject

Previous Articles

Part 1: Setting Up DirectX
Part 2: Initialising Direct3D
Part 3: Rendering Primitives
Part 4: The Transformation Pipeline
Part 7: Using Heightmaps

License

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

James Gupta


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
Occupation: Web Developer
Location: United States United States

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
Subject  Author Date 
Generalsome ideasmemberGeorgi Petrov2:21 27 Jun '06  
GeneralSuggestionmemberDustin Metzgar9:44 26 May '06  
GeneralRe: SuggestionmemberJames Gupta10:11 26 May '06  
GeneralRe: SuggestionmemberDustin Metzgar7:07 20 Oct '06  
GeneralInteresting [modified]memberRory Plaire8:04 26 May '06  
GeneralRe: Interesting [modified]memberJames Gupta9:04 26 May '06  
GeneralRe: Interesting [modified]memberRory Plaire10:00 26 May '06  
GeneralRe: Interesting [modified]memberJames Gupta10:10 26 May '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 May 2006
Editor:
Copyright 2006 by James Gupta
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project