#### Archived

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

# Terrain mesh building

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

## Recommended Posts

Hey all. I got a simple dot field created from a height map, but now how do i connect all these points to create a triangle mesh? I can''t seem to get any of my ideas working. I am doing this in OpenGL, if that helps.. Thanks. - Iceshadow

##### Share on other sites

1 2 3
4 5 6
7 8 9

triangle 1: 1 - 2 - 4
triangle 2: 4 - 2 - 5

Here I defined the triangles for the upper left 4 points. Do the same thing for all points in the heightfield, and u''ll be all set.

##### Share on other sites
Yup, in the above diagram you''d draw a line from 5 to 1,2,3,4,6,7,8,9. And the same for all points, except remember that point 1 to 5 is the same a 5 to 1 and 2 to 5 is the same as 5 to 2 and so forth, so you have to have some sort of system, so you don''t draw any line twice... hmmm, maybe only draw lines when point2 has a greater value than point1? I''m not sure but the idea is to draw line''s from every point...
See ya,
Ben

##### Share on other sites

I was just where you were awhile ago, so maybe I can help. First, what I suggest, is to sit down with a piece of paper and draw triangles for each dot. Then look for patterns If you can get it, great, otherwise, I''ll tell you...right now.

The first thing you need to realize is that you''re going to have to write code for making two types of triangles, kind of. You''ll have the front facing, and the back facing (if this doesn''t make sense, don''t worry--you''ll see what I mean). Observe.

You''ll have a triangle like this:
/\
/_\
And a triangle like this:
__
\ /
\/

Now, in order to make the mesh, you make a series of for loops, and count the triangles that you''ll need, like so.

int Count = 0;

for(int i = 0; i < Height - 1; i++)
{
for(int j = 0; j < Width - 1; j++)
{
Count++; //2 triangles for every pass.
Count++;
}
}

Then, you allocate enough memory for this Count many triangles, via malloc,new, or whatever. You then actually make the triangles, like this:

in Cnt = 0; //used to track which face we''re on.

//Now we initialize those triangles.
for(i = 0; i < Height - 1; i++)
{
for(int j = 0; j < Width - 1; j++)
{
Faces[Cnt].Verts[0] = (i * Width) + j;
Faces[Cnt].Verts[1] = (i * Width) + j + Width;
Faces[Cnt].Verts[2] = (i * Width) + j + 1;
Cnt++;

Faces[Cnt].Verts[0] = (i * Width) + j + 1;
Faces[Cnt].Verts[1] = (i * Width) + j + Width;
Faces[Cnt].Verts[2] = (i * Width) + j + Width + 1;
Cnt++;
}
}
A thing to note would be that the Face class contains integers for which vertices belong to it. You can easily use pointers or whatever, or take the vertices that you made in your dot thing, and set them to it. So, the code works, but it looks ugly (that is, it sucks without tabs and colors--I wasn''t refering to the code, because I code so beatifully...). I hope that it helps you out.

##### Share on other sites

Damn it! My name didn''t show up above... Anyways, I meant the triangles to look nicer, but it appears that this message board doesn''t have the straight up line (you know, the one that you use for OR in C++), so don''t pay attention to those. Instead, I''ll explain it. In the first, the hypotenuse is supposed to be on the right hand side, and on the second, it''s on the left hand side...

##### Share on other sites
A question for you Frag Daddy - wot does that triangle function u wrote there do exactly?

each triangle (or face i suppose) has 3 vertices which u have specified as vert 0,1,2. for each vertice u have given each only one value. doesnt a vertice contain 3 values - an x,y and z component ??

are u perhaps just describing each triangle in world coordinates ?

##### Share on other sites
I assume he''s referencing vertices in a vertex pool, hence the use of only one value.

##### Share on other sites
One thing i don''t understand Frag_Daddy...

why multiply by the width?

ex:Faces[Cnt].Verts[0] = (i * Width) + j;

??

##### Share on other sites
This by far is the easiest part of rendering your terrain. Suppose your grid is 256 by 256, and you want to render triangles out of it. your points would range from 0..255 in both directions.
so we have a loop like this :
for (y=0; y<255; y++)
for (x=0; x<255; x++)
{
// We will use rectangular indexing coordinates,
// so you know which coordinates I am referring to
draw3triangle( height[x,y], height[x+1,y], height[x+1,y+1] );
draw3triangle( height[x,y], height[x+1,y+1], height[x,y+1] );
}

That is all, if you still don''t understand, then I don''t know how else to put it.

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 12
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
633349
• Total Posts
3011464
• ### Who's Online (See full list)

There are no registered users currently online

×