Drawing terrain as triangles (from heightmap)

Started by
23 comments, last by Funkymunky 17 years, 4 months ago
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.
Advertisement
Please move to opengl(I mean. Drawing in opengl)
Quads are just made up of two triangles.

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

Dave
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.
fread(hm, 1, nSize, f);

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.
Try this tutorial :
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=34
Sincerely,Arto RuotsalainenDawn Bringer 3D - Tips & Tricks
Quote:Original post by Arex
Try this tutorial :
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=34


Read post #1,

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



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



I need code for Triangles.

Thanks.
Quote:Original post by jett
I need code for Triangles.


Quads are 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
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.
Sincerely,Arto RuotsalainenDawn Bringer 3D - Tips & Tricks
in reply to post #7.

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,

you saw my loading code.

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

in reply to post #8

Triangles are eaiser for the video card to wrap textures onto.
But with triangles, not trianglestrips.
That would be great.

This topic is closed to new replies.

Advertisement