#### Archived

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

# DirectX Landscaping

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

## Recommended Posts

I am trying to build a sort of landscape engine.. this is my first attempt and i dont know exactly how i should set it up. I know there are meshes and i believe this would work well.. right? I think this is much more effecient than making everything out of seperate triangles.. but im not sure.. Also with a mesh can you blend textures together.. such as blending one texture into another in a single triangle or blending one texture directly on top of another triangle. I am doing this stuff with dx8 right now.. An just need some pointers about how about would be a good way to do this.. For now i will play with learning how textures work.. thanks, Russ

##### Share on other sites
This is such a huge topic. No doubt it''s the reason for all your replies

If you''re just starting, you might want to use a very simple method: a triangle mesh. I can''t tell you how to do this in DX because I use OpenGL. But basically it goes like this:

-on paper draw a large square.
-break the square up into an equal amount of rows and columns.
-break each smaller square once into a triangle.

There is your triangle mesh. How do you define the vertices in 3D space? Pretend your map is laying flat on the XZ axis in whatever quadrant you prefer. Each column line on your map is a constant offset from 0 in the X direction. Each row line is a constant offset from 0 in the Z direction.

How you put that information into a mesh object will depend on your API.

Now what if you want to raise any of those vertices (which you will want to do sooner or later)? A simple way is to use a height map, which exists to specify the Y value of a vertex. The easiest (but least efficient) way to add this AND define your triangle mesh at the same time is to use a vertex array. If you go back to that map you drew and labeled each column line going from 0 to n, and each row line from 0 to n, then you have an array. If you define an array that size of type vertices then you''ve got an awfully simple way for creating terrain because you''re specifying the x,y and z value for each triangle point. It''s grossly inefficient... but very simple to use and understand.

You can then initialize the array like this:

for r:=0 to gsize-1 do begin
for c:=0 to gsize-1 do begin
y:=random(5);
MakeVector(mapv[r,c],c*tsize,y,r*tsize);
end;
end;

gsize is the number of columns/rows on your map (assuming it''s square), and tsize is the size of the triangle you want. Is that the easiest terrain code or what?!

If you want to get fancy and use a bitmap to specify the Y value of each vertex instead of a random value you can do something like this (depending on your language and API):

for r:=0 to gsize-1 do begin
for c:=0 to gsize-1 do begin
///Color indicates height of a vertex.
col:=image1.Canvas.Pixels[c,r]; //get color of pixel on map
blue:= (col and $00ff0000) shr 16; green:= (col and$0000ff00) shr 8;
red:= (col and \$000000ff);
y:=(blue+green+red)/3; //or any other formula you want
MakeVector(mapv[r,c],c*tsize,y,r*tsize);

There you go. One simple hand-made terrain map. Now how you render it is up to you and depends on your language/API. But it''s just a matter of looping through each array element... which is a vertex.

##### Share on other sites
hi,

i managed to easily load the pixel values from a bitmap:
int x, y;
int i[256][256];

for (y=0;y<256;y++)
for (x=0;x<256;x++)
i[x][y] = getpixel((BITMAP*)data[map].dat,x,y);

Now i just gotta figure out how to render it using vc++6 and allegro. I''ll probably never learn how to do this if i just start out with openGL or directX.

Can i actually accomplish this with the above routine? Or is there more to it?

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013597
×