Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Sixten

A real Newbe Question about Height maps

This topic is 5729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well i have been checking around on the net for some good tutorials on height maps.. and i didnt find any.. so i thought i shoudl try for my self.. well i got on problem.... i dont know how to load the bmp file and then go threw every pixel to check the color of it... i know its really newbe.. but i really dont know how to do it... i just want to know how i open the bmp and how i go threw it to check the gray scale... lol Thx.... i dont like Copy past.... but if u guys can give me some hints it whould be cool...

Share this post


Link to post
Share on other sites
Advertisement
I have also been writing a terrain engine. The best references I have found are...

http://www.gamasutra.com/features/20000228/ulrich_01.htm

and

http://longbowdigitalarts.com//seumas/progbintri.html

The basics are pretty straight forward. It''s when you start implementing LOD algorithms things get a little more complicated. Having said that, as a VB programmer I have managed to write a binary triangle tree terrain tessellation routine in C# and DX in a couple of weeks.

Good Luck

Raeldor

Share this post


Link to post
Share on other sites
Props to you Raeldor, i recently tackled my own terrain engine.(well, i really just de-spaghettified my previous one) will check your links and thanks for the info.

oh yeah, anyone know what the data that a grayscale returns is?
i know its a long, and im guessing it is a whole number between 0 and 255, can anyone verify that?


[edited by - Dreddnafious Maelstrom on December 6, 2002 2:26:24 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Dreddnafious Maelstrom
oh yeah, anyone know what the data that a grayscale returns is?
i know its a long, and im guessing it is a whole number between 0 and 255, can anyone verify that?
[edited by - Dreddnafious Maelstrom on December 6, 2002 2:26:24 PM]

It would be a char between 0 (black) and 255 (white).

Share this post


Link to post
Share on other sites
In grayscale, each intensity is represented by a byte, whose value is between 0 and 255. If you use the grayscale bitmap as a height map, then each value represents a height in your terrain.

The best way is to store your grayscale bitmap as a raw file, in which case all you have to read is the raw data.

Meaning if you have a bitmap that is 256x256 that represents a height map, when you save it as a raw file, all you get is 256x256 bytes stored in the file. Makes reading much easier, and you don''t have to worry about reading headers and so on.

Photoshop makes working with grayscale bitmaps extremely easy.

After you read the height map you would have an array of height values. Say:

unsigned char heightMap[256][256]; // [y][x]

Now those are your height values at a single point in your terrain.

Since your terrain is made up of trianges, and not points, you have to decide on the grid/cell size.

_____
| |
| |
|_____|

The square above represents a single cell, which has dimensions 10x10.

Now that you know the dimensions of each cell in your world, you can go ahead and define the vertices for the triangles in the terrain.

Note: The smaller the cell size, the more detailed the terrain is, but the slower it is, and the more memory it requires to store larger terrains. Hope that made some sense. Not likely

Before we define the vertices we have to remember that since the terrain is in 3D, the values in our height map will represent the "y" values of our vertices, whereas the indecies to our height map will represent the x and z values.

Note: Remember that we will be connecting values between two height map values (heights) in order to construct a continues terrain.

We basically connect the triangles between 4 height values.

Imagine a grid like this:


v1 v2
_____
| /| /|
|/_v6_| v5
| /| /|
|/_|/_|v3
v4

Let''s get the theory into action and see what we get:



  
for( int z = 0; z < 255; z++ )
{

for( int x = 0; x < 255; x++ )
{

SetVertex(v1, x*10, heighMap[z][x]*10, z*10);
SetVertex(v2, x*10, heightMap[z+1][x]*10, z*10+10);
SetVertex(v3, x*10+10, heightMap[z][x+1]*10, z*10);
SetVertex(v4, x*10+10, heightMap[z+1][x+1]*10, z*10+10);
SetVertex(v5, x*10+10, heightMap[z][x+1]*10, z*10);
SetVertex(v6, x*10, heightMap[z+1][x]*10, z*10+10);

}

}


Values in heightMap[][] will be between 0..255. What we are doing here is basically defining all the vertices that define our terrain. This means in order to connect those, we have to define the neighbor vertices as well.

Remember that a lot of the vertices will be shared between neighbors so one way to optimize is to get rid of the shared vertices, and define a face type that will keep track of the indices into the vertex list. This will reduce the memory required.

In order to render a cell, we have to split it into 2 triangles. Those 2 triangles have vertices v1, v2, v3, and v4, v5, v6, respectively.

Now just render those triangles and we are in business.

One thing I forgot to mention that you might be wondering about is the color of the triangles. The simplest method is to use the height map to define the color of the triangle you are rendering. Basically, the height map values are the intensities that you want to use to render the triangle.

This is the simplest way to render a terrain from a height map.

Hope this helps somewhat. If you have questions ask away.

Share this post


Link to post
Share on other sites
Excellent explanation, Gladiator, great code examples.. I was actually visualizing the heightmap points as vertices on the terrain, instead of squares.

is this a right approach?

Share this post


Link to post
Share on other sites
WARNING
Don''t read this unless you''re really interested in heightmaps and are really really bored. It got quite long. It''s mostly my personal theories. I really should make a tutorial... I''m getting started right now. Oh no, the warning got too long too.. argh...
WARNING

As a comment about heightmaps, don''t use them. Your terrain will be a lot more cool if you make it yourself manually. Sounds like a bitch? It is.

However when you place out your buildings, trees and roads and stuff you will want to edit the terrain yourself anyway. I used heightmaps for a while but in the i end i just made a sin-generated terrain as a base and edited manually using an arrow placed in the sharing point of four quads. Then i use the keyboard to move this point up and down, This gives a great result. About the same technique is used for applying textures and rotating them. This editor function is made rather easily.

Now you can save your height and texture info in the same file and you don''t have to risk users manipulating your heightmap (at least not as easily). You''ll also have 100% control. If you also apply a "copy/paste height" function you can also easily level terrain, good when placing roads and buildings.

However, a heightmap could be a good base for loading a "default terrain" that you''ll want to edit later. Another tip is to use a four channel image (ALPHA+R+G+B). Each of theese channels can be used for different things. Green could be ground, red trees and blue water. To access all points and colors you''ll hacve to load it onto a surface, lock the memory and read the pixels. (I guess there''s an easier way but I don''t know it).

Anyhow, the point is (or "the vertex is") make a VERY simple leveleditor and life will be a lot happier. Making the editor will be almost as easy as reading an imagefile. Just place and arrow on a vertex, move it up and down and regenerate.

.Pangis

Share this post


Link to post
Share on other sites
Well i have been working on this by my self for a few days... and i have gotten this far.....

i use this code to load teh height map in as a texture


  
D3DXCreateTextureFromFileEx( m_pD3DDevice, "hello.bmp",
D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_A8R8G8B8,
D3DPOOL_MANAGED, D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR,
D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR, 0, NULL, NULL, &m_pCustomNormalMap );


m_pCustomNormalMap->GetLevelDesc( 0, &d3dsd );
m_pCustomNormalMap->LockRect( 0, &d3dlr, 0, 0 );
pPixel = (DWORD*)d3dlr.pBits;


so with the GetLevelDesc i get the Height and i get the Width...
Then i use the LockRect to get the size of each pixel.. at least i think it dose that if im thinking right.... because u dont look at the color u look at the size of the byte in the bmp file right... not the color...

Then i just tried something funny and it turned out liek this

  
color01 = pPixel[d3dlr.Pitch/sizeof(DWORD)];

fHeight01 = (FLOAT)((color01&0x00ff0000)>>16)/255.0f;

i got it from a prog that was useing light maps... so i thought it whould be about the same... but i get some way of numbers... the color01 = -16777216
The fHeight01 = 0
and pPixel = 14908800

when the height map is totaly black... and the when its white

the color01 = 0
The fHeight01 = -1
and pPixel = 14908800

So what am i doing wrong???? Why are the number so strange???? Can any one help me???? i got code allrdy to aply the height but i just cant get the right height from the height map... i dont know what im doing wrong???

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!