Terrain Question...

Started by
8 comments, last by Orb 20 years, 6 months ago
I following a terrain tutorial right now, and I am having trouble with rendering it. The tutorial I am following, using allegroGL, to calculate the height in a heightmap. They use the function readpixel, to return the pixel level in a heightmap, and then it stores those values in a 2d array, like so:

for(x = 0; x < AREA_SIZE; x++)
    {
        for(y = 0; y < AREA_SIZE; y++)
        {
            h = (float) getpixel(height_bitmap, x, y);      // [0..256]

            h /=4.0;                                        // scale down by factor 

           height_field[(int)x][(int)y] = h;
        }
    }
 
In my demo, I don't want to use getpixel, so I used the usual method of loading heightmaps:

HeightmapFile = fopen(filename, "rb");
fread(HeightmapData, 1, SIZE_X * SIZE_Y, HeightmapFile);
fclose(HeightmapFile);
 
Now the problem is, I am wanting to render the information from my 1 dimensional array, using triangle strips, like the tutorial does. First, I have to convert the single dimension array, to a 2 dimension array, for ease of access. This is my code for exchanging the data.

for(int p=0; p<SIZE_X; p++)
	{
		for(int q=0; q<SIZE_Y; q++)
		{
			HeightmapArray[p][q] = HeightmapData[p + (q*512)];
		}
	}
 
Now the code for rendering:

glBegin(GL_TRIANGLE_STRIP);
    for(x = 0; x < SIZE_X-Step; x+=Step)
    {
        for(z = 0; z < SIZE_Y-Step; z+=Step)
        {
			glColor3f(0, Height(HeightmapData, x, z)/256.0, 0);
            glVertex3f(Scale*(x+Step), Scale*HeightmapArray[x+Step][z], Scale*z);
            glVertex3f(Scale*x, Scale*HeightmapArray[x][z], Scale*z);
        }
    }

    glEnd();
 
I tried this method and it doesn't work. It has a general outline of the terrain, but there are blocky areas, and areas with lines crossing all over the terrain. Any help is GREATLY appreciated. [edited by - Orb on October 8, 2003 6:09:41 PM]
Advertisement
*bump*

And if anyone needs a screenshot so that I can show you what is rendering, I will get one.
Post a screenshot.
You might also want to put a (float) before all your scale multiplications, otherwise yor terain might look blocky.

Height Map Editor | Eternal Lands | Fast User Directory
Look at the heightmap tutorial on www.gametutorials.com , you''ll find the correct way to render it there (yours is wrong).

thats not the right way to render it with a triangle strip.

[ My Site ]
''I wish life was not so short,'' he thought. ''Languages take such a time, and so do all the things one wants to know about.'' - J.R.R Tolkien
/*ilici*/
Don''t use GL_TRIANGLE_STRIP I wish I would have used GL_QUADS or GL_TRIANGLES instead. strips are a pain. I don''t know for sure but I don''t think you can render strips in sections? If someone else can verify this please tell me so I can learn how. I am tossing around the idea of a tilemap for the terrain textures and using VBO''s I don''t know how this would work with GL_TRIANGLE_STRIP that is why I suggest using GL_QUADS or GL_TRIANGLES. I don''t think their is no huge performance increase form using strips vs. triangles anyway.
I'm pretty sure tri strips are a lot faster b/c there are going to be less vertices to render

Tri-strips == 2 calls to Vertex per four verts after the initial two
quad == 4 calls to Vertex per quad, no matter where you are

Even with vertex arrays, less vertices is better.

[edited by - EvilSwan on October 10, 2003 5:48:56 PM]
there are also quad strips, GL_QUAD_STRIPS i think. 2 calls to vertex per quad after the initial 4 vertices called.
quote:Original post by EvilSwan
I''m pretty sure tri strips are a lot faster b/c there are going to be less vertices to render

Tri-strips == 2 calls to Vertex per four verts after the initial two
quad == 4 calls to Vertex per quad, no matter where you are

Even with vertex arrays, less vertices is better.

*cough* indexed triangle lists *cough*

They are a little slower...but unless you are extremely tight on performance or need that little extra memory the indices would take up then lists are easier to work with.
____________________________________________________________AAAAA: American Association Against Adobe AcrobatYou know you hate PDFs...
I checked out gametutorials, but their tutorials use quads. I don''t want to use quads, because I''m going to be working with LOD, and that usually requires triangulation, which I don''t believe you can do with quads.

I want to use triangle strips, but I don''t understand why I can''t use the method I''m using now. But I tried using the float thing, but the terrain still looks blocky. Yes, I do have smooth shade on too.

Thanks.
Nevermind, I just had to do a little experimenting myself. I wrote up a new way to render the heightmap using triangle strips, and it works. Now I can focus on the LOD implementation.

Thanks for the help.

This topic is closed to new replies.

Advertisement