# Need Help with Terrains!

## Recommended Posts

Hello, I'm looking into the algo's that it takes to load a terrain into my project. I have been looking through info for a couple of days and cant really seem to get it together here. So far heres what I have: Create a heightmap with a paintprogram or 3rd party software. How would I load something like this into my project using DirectX? Create a planer model or mesh using a modeling program. How would I load something like this into my project using DirectX? Make a function that generates the data to create the vertices, then apply a texture to it? I have been attempting to step through the DX tutorials, but as usual, cant get the code to work. I'll keep at it though. I used Darkbasic for about 5 months and created a nice little demo pretty easily. I stepped away from Darkbasic due to the fact that I wanted more control over implementing low level functions so that I could do certain things the way I wanted to (and I didnt have to pay for any upgraded features, lol). But it seems I may have gotten in way over my head here. Thanks for any help.

##### Share on other sites
Quote:
 Original post by Hilbily001Hello

Hi!
Quote:
 I'm looking into the algo's that it takes to load a terrain into my project. I have been looking through info for a couple of days and cant really seem to get it together here.

I'm not sure what you're looking for here, maybe you could be more specific?
Quote:
 Create a heightmap with a paintprogram or 3rd party software. How would I load something like this into my project using DirectX?

The most straightforward way: create a grayscale image as a heightmap. Because it contains only shades of gray, the RGB components for a pixel's color will all be the same: white = RGB(255, 255, 255), black = RGB(0, 0, 0), light gray = RGB(196, 196, 196), etc.
You can use one of the components as an indication of height, black is low terrain, white is high terrain. The only thing you'll have to do is itterate through the pixels of the image, and use their height value to set the "height" of the vertices that make up the grid. In pseudo code:
img = LoadImage("heightmap.bmp")vert = array // array of verticessize = 10  // size of a terrain tilei = 0for (z = 0; z < img.height; z++){  for (x = 0; x < img.width; x++)  {    // get height from image    y = img.ColorAt(x, y)    // create 6 vertices to make up a quad:    // x----x    // |   /|    // |  / |    // | /  |    // |/   |    // x----x    // triangle 1    vert[i++] = vertex(x,        y, z)          // upper left    vert[i++] = vertex(x,        y, z + size)   // bottom left    vert[i++] = vertex(x + size, y, z)          // upper right    // triangle 2    vert[i++] = vertex(x + size, y, z)          // upper right    vert[i++] = vertex(x,        y, z + size)   // bottom left    vert[i++] = vertex(x + size, y, z + size)   // bottom right  }}

Not only is this pseudo code, it is also highly unoptimized and incomplete code. In practice, you could use triangle strips instead of lists, index the vertices etc, but I left it out for simplicity.
Quote:
 Create a planer model or mesh using a modeling program. How would I load something like this into my project using DirectX?

This depends entirely on the format of you model. DirectX has build in support of X files, for formats like 3DS, ASE etc you'll need to write your own parser. Google for sample code and tutorials, there are plenty out there.
Quote:
 Make a function that generates the data to create the vertices, then apply a texture to it?

Yep, also possible. For procedural terrains (plain random values will not yield realistic results), look into Perlin noise algorithms for example. But since you had trouble setting up a terrain already, I would try one of the other options first.

##### Share on other sites
I learnt to load terrains into Direct X by studying other peoples code. Ogre 3D has good terrain example tutorial. There are also others if you google that I found a while ago.

I like to load landscape meshes but heightmaps are very good. I use a program called Freeworld 3D to create the terrains and export them. You can write a loader for your direct x program to load the terrains from the files freeworld generates or convert them to x files and load them that way. I don't like .x file format though but thats personal opinion only.

Freeworld Examples are a few terrains i created and imported into ogre

##### Share on other sites
Keep at it and you'll succeed.

However, Seeing that you came from using Dark Basic which is specifically tailored to making games, using DirectX is difficult to get going for beginners. I would recommend looking into SDL as you can get something on the screen with < 30 lines of code.

Checkout The Robot Frog" web page. He presents some algorithms for generating heightmaps and he also has the code for generating these aswell.

##### Share on other sites
I have a series on creating textured terrains from heightmaps on my site, http://www.just-code-it.net.

Hope they're useful - find them in the Tutorials -> Heightmapped Terrain section.

James

##### Share on other sites
Wow, great replies there. Thank you!

I will spend a few weeks (lol) digesting this info.

WanMaster, I was looking for sample code that shows how to load a heightmap into a DX enable object. Sorry for being unclear there. That psuedo code answered my question perfectly. Thank you very much.

Mordt, I too sampled Freeworld and your right, it is a very nice program, havent tried out the exporting yet though. Personally I like the .x file format, for non landscape models anyways. It worked great for DarkBasic and the animations ran flawlessly. It didnt take much code to get a model loaded and run through animations with it.

Adam, I appreciate the recommendation, but after looking into the SDL api, I chose not to pursue that avenue. There are a few limitations to it and I would rather smack my head against DX for now. I used DX with VB, so I have limited exposure to it though in a slight different degree.

Xpyder, thank you for the time spent on that tutorial, That was what I was looking for, I will spend some time with it and see if I can translate it. Thank you.

On a side note, I have downloaded and purchased like 3 or 4 different landscape creators back when I used DarkBasic. I agree with those that suggest creating a landscape with 3rd party software then export to heightmap and implement in project. Thanks for the advice. I will keep the post updated with progress.

##### Share on other sites
Xpyder,

Thanks again for the tutorial. As usual, with me, it took me hours just to get to the first part. Finally I reread it for the 100th time and realized youre using visual C# express, not visual C++ express. I couldnt figure out the references and creating the DXEngine class and stuff.

After installing Visual C# express, I was able to complete the first tutorial "Part 1) Setting Up & Understanding DirectX" so I moved on to the next one "Part 2) Basic Graphics" and right away I got lost... again (I apologize). You discribe the initialization function then go to actually coding the following

public Device device;
public PresentParameters presentParams;"

The problem I have is, where did we create a window class, is this another standalone class library?

So I downloaded the last tutorial sample to see what files are where and I didnt see any reference to DXEngine or to a window class.

Again I apologize, but I got lost pretty quick here, Im not too savy with the C++ or C# IDE.

##### Share on other sites
Sorry I should have made this more obvious.

There should be a part in the base section which tells you to add a new windows form, although I ported these from a previous engine that i made with a different structure.

I will change it when i get back home, and inform you.

James

*PS: If you translate it, I would appreciate it a lot if you could send me the converted C++ code, not your whole engine, but just the tutorial parts.

If you cant then I understand.

**EDIT**

there is a part on the second Base tutorial explaining how to create your form class:

Now, we need to get to the second part of this tutorial: Creating a DirectX-specialised form.
Delete the form's "designer" file.

It is near the end and has a code sample.

Hope this is what you were looking for

[Edited by - Xpyder on May 24, 2006 11:56:50 AM]

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627682
• Total Posts
2978614

• 13
• 12
• 10
• 12
• 22