• 12
• 11
• 9
• 10
• 13

# Drawing terrain as triangles (from heightmap)

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

## Recommended Posts

I've been experimenting with terrain rendering for my game, but it always turns out wrong. Any help?. I've seen nehes tutorials. But they use quads, which aren't good for terrain. Thanks.

##### Share on other sites
Please move to opengl(I mean. Drawing in opengl)

##### Share on other sites

Can you paste a screenshot so we can see what is wrong?

Dave

##### Share on other sites
Well, it just doesnt draw anything, and when it does, it looks wacky.

And, Quads are harder for the videocard to do textures with.
Its easier to do two triangles.

I'm using .RAW files made with Gimp.

void LoadRawFile(char filename[], int nSize, BYTE *hm)
{

FILE *f = NULL;
f = fopen(filename, "rb");

if(f == NULL)
{
char tmperr[40];
sprintf(tmperr, "Error: Cannot load raw file '%s'.", filename);
l.LogMessage(tmperr);
return;
}

// Read the bytes from the file. 1 byte at a time
// and send them to the pointer to the byte-array.

if(ferror(f))
{
char tmperr[40];
sprintf(tmperr, "Error: Failed to read raw '%s'.", filename);
l.LogMessage(tmperr);
fclose(f);
return;
}

if(!hm)
{
char tmperr[40];
sprintf(tmperr, "Error: Heightmap '%s', is invalid.", filename);
l.LogMessage(tmperr);
fclose(f);
return;
}

char tmpstr[40];
sprintf(tmpstr, "Heightmap '%s', loaded at (0x%X).", filename, &hm);
l.LogMessage(tmpstr);

fclose(f);

return;
}

But how to draw? I know im not doing it right, cuase its not there.

Im a super beginner at opengl.

##### Share on other sites
Try this tutorial :
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=34

##### Share on other sites
Quote:
 Original post by ArexTry this tutorial :http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=34

Quote:
 Original post by JettI've seen nehes tutorials. But they use quad

Quote:
 Original post by Jett2 Triangles are easier than a quad for the videocard.

##### Share on other sites
I need code for Triangles.

Thanks.

##### Share on other sites
Quote:
 Original post by jettI need code for Triangles.

But I digress. I used triangle strips in my terrain engine way back in the day. The algorithm is something like this:

1     2     3     4     5*-----*-----*-----*-----*|\    |\    |\    |\    || \   | \   | \   | \   ||  \  |  \  |  \  |  \  ||   \ |   \ |   \ |   \ ||    \|    \|    \|    \|*-----*-----*-----*-----*6     7     8     9     10*-----*-----*-----*-----*|\    |\    |\    |\    || \   | \   | \   | \   ||  \  |  \  |  \  |  \  ||   \ |   \ |   \ |   \ ||    \|    \|    \|    \|*-----*-----*-----*-----*11    12    13    14    15

You draw this first strip from left to right (you can look up the syntax for triangle strips). But the index ordering is something like:
1, 2, 7, 1, 7, 6, 2, 3, 8, 2, 8, 7, etc

The next strip you draw from right to left (again you can look up the syntax). But the ordering is something like:
9, 10, 15, 9, 15, 14, 8, 9, 14, 8, 14, 13

I don't remember openGL winding or if that's nessarily the correct index ordering but hopefully you get the idea.

-me

##### Share on other sites
Use the same code, it is using triangles, but that is an easier way to do quads in opengl, already indexed. You just need to create 2 more vertices which are those vertices that overlap with the other triangle vertices. So basically the quad system is faster, because if you want to draw triangles without indexing then you will have 2 extra vertices. :)

So, duplicate the bottom right and top left vertices for example. And then set it to render triangles.

##### Share on other sites

OK, I have an 512X512 heightmap, i try to draw it like,
GLBegin(GL_TRIANGLES);glVertex3f(x, heightmap[x + (y * 512)], y);glVertex3f(x+1, heightmap[x + (y * 512)], y);glVertex3f(x, heightmap[x + (y * 512)], y+1);glVertex3f(x+1, heightmap[x + (y * 512)], y+1);dont remember the restGLEnd();

i have

BYTE heightmap[512*512];

and,

So, if someone could put what you said in code?