Terrain mesh building

Started by
7 comments, last by Iceshadow 23 years, 9 months ago
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
-=[Iceshadow ]=--=[ ilnelson@cswnet.com ]=-
Advertisement
here''s your height map:

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.

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
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949

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.

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...
-----------------------------1. "Diplomacy is the art of saying 'Nice doggie!'... till you can find a rock." 2. "Always remember you're unique, just like everyone else." 3. "If we don't succeed, we run the risk of failure."-Dan Quayle4. If life gives you sour grapes, squash them and make wine!
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 ?
''Once a smurf, always a smurf''
I assume he''s referencing vertices in a vertex pool, hence the use of only one value.
http://www.inmytree.com/~roz/
One thing i don''t understand Frag_Daddy...

why multiply by the width?

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


??





- Iceshadow
-=[Iceshadow ]=--=[ ilnelson@cswnet.com ]=-
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.

This topic is closed to new replies.

Advertisement