Jump to content
• Advertisement

Public Group

Drawing terrain as triangles (from heightmap)

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

If you intended to correct an error in the post then please contact us.

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 this post

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

Share this post

Share on other sites
Quads are just made up of two triangles.

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

Dave

Share this post

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.
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.

Share this post

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

Share this post

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

Read post #1,

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 this post

Share on other sites
I need code for Triangles.

Thanks.

Share this post

Share on other sites
Quote:
 Original post by jettI 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

Share this post

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 this post

Share on other sites
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.

Share this post

Share on other sites

• Advertisement
• Advertisement

• Popular Contributors

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5
• Advertisement

• 10
• 14
• 30
• 13
• 11
• Forum Statistics

• Total Topics
631782
• Total Posts
3002338
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!