Advertisement

Terrain rendering question

Started by July 25, 2000 08:48 AM
13 comments, last by dinotrack 24 years, 3 months ago
I wrote some code that reads the height value(Y) from a heightmap tga file. The heightmap is 256x256. It stores the values in an array. But I cannot figure out how to calculate and store the X and Z values. I assumed these would be stored in a 256x256 array also, and they would run from 0 through 255, but I don''t know for sure. I need to draw the terrain as GL_TRIANLES(or GL_TRIANGLE_STRIP would be better, but I want to keep it simple for now). But I don''t know how I can calculate the triangles from the arrays of vertices and draw them. So can someone please help me out. Thanks, Jonathan
If u want even spacing for the x and y, u could do something like this(plot vertexs 0.5f apart):-

for(int x=0;x<256;x++)
for(int z=0;z<256;z++)
glVertex3f(x*0.5f, height[x][z], z*0.5f);

if that makes any sense.

Should work I think.
Advertisement
This is a section of code without variables. with this code you can display the all the terrain (no eficient).




^ .....
| .....
z .....
x-->


for(x=0; x<255; x++) {
x1 = x;
x2 = x + 1;
z = 0; // if you are in another position you need this
z1 = z;
z2 = z + 1;

glBegin(GL_TRIANGLE_STRIP);
glVertex3f(x2, y[x+1][z], z1);
glVertex3f(x1, y[x,z] ,z1);
glVertex3f(x2, y[x+1][z+1], z2);
glVertex3f(x1, y[x][z+1], z2);

for(z=2; z<255; z++) {
z1 = z;
glVertex3f(x2, y[x+1][z], z1);
glVertex3f(x1, y[x][z], z1);
}
glEnd();
}

The real code is:

void DrawTerrain(float pos_x, float pos_z, int min_x, int min_z, int max_x, int max_z)
{
register int x, z ;


glDisable(GL_NORMALIZE);

pos_x = pos_x/mapscale;
pos_z = pos_z/mapscale;

for(x=pos_x+min_x; x x1 = x*mapscale;
x2 = x*mapscale + mapscale;
z = pos_z+min_z;
z1 = z*mapscale;
z2 = z*mapscale + mapscale;


glBegin(GL_TRIANGLE_STRIP);

glNormal3f(n[INDEX(x+1,z)].x1,n[INDEX(x+1,z)].y1,n[INDEX(x+1,z)].z1);
glColor3f(c[INDEX(x+1,z)].r, c[INDEX(x+1,z)].g, c[INDEX(x+1,z)].b);
glTexCoord2f((x+1)/tetureres, (z)/tetureres);
glVertex3f(x2, y[INDEX(x+1,z)], z1);


glColor3f(c[INDEX(x,z)].r, c[INDEX(x,z)].g, c[INDEX(x,z)].b);
glTexCoord2f((x)/tetureres, (z)/tetureres);
glVertex3f(x1, y[INDEX(x,z)], z1);

glColor3f(c[INDEX(x+1,z+1)].r, c[INDEX(x+1,z+1)].g, c[INDEX(x+1,z+1)].b);
glTexCoord2f((x+1)/tetureres, (z+1)/tetureres);
glVertex3f(x2, y[INDEX(x+1,z+1)], z2);

glNormal3f(n[INDEX(x,z+1)].x2,n[INDEX(x,z+1)].y2,n[INDEX(x,z+1)].z2);
glColor3f(c[INDEX(x,z+1)].r, c[INDEX(x,z+1)].g, c[INDEX(x,z+1)].b);
glTexCoord2f((x)/tetureres, (z+1)/tetureres);
glVertex3f(x1, y[INDEX(x,z+1)], z2);


for(z=(pos_z+min_z)+2; z z1 = z*mapscale;

glNormal3f(n[INDEX(x+1,z)].x1,n[INDEX(x+1,z)].y1,n[INDEX(x+1,z)].z1);
glColor3f(c[INDEX(x+1,z)].r, c[INDEX(x+1,z)].g, c[INDEX(x+1,z)].b);
glTexCoord2f((x+1)/tetureres, (z)/tetureres);
glVertex3f(x2, y[INDEX(x+1,z)], z1);

glNormal3f(n[INDEX(x,z)].x2,n[INDEX(x,z)].y2,n[INDEX(x,z)].z2);
glColor3f(c[INDEX(x,z)].r, c[INDEX(x,z)].g, c[INDEX(x,z)].b);
glTexCoord2f((x)/tetureres, (z)/tetureres);
glVertex3f(x1, y[INDEX(x,z)], z1);

}

glEnd();
}

}
I think I can figure it out if I can arrange the data differently.

The heightdata, the Y value, is in a huge 256x256 array.

How would I put this in a 2-dimensional [256][256] array?

I''ve been trying to figure it out, but I just cant work out how to make a loop that will do it.
do what seaman said.

alternativly

float height[256][256];

for(int x=0;x<256;x++)
{for(int z=0;z<256;z++)
glVertex3f(x*0.5f, height[x][z], z*0.5f);

sorry bout posting this
(is there a way i can use a tab key as a tab key when i type in stuff)

Edited by - zedzeek on July 25, 2000 6:32:23 PM
do what seaman said.

alternativly

    float height[256][256];VERTEX landsclape[256][256];for(int x=0;x<256;x++){ for(int z=0;z<256;z++)  {  landscape = VERTEX(x*0.5f, height[x][z], z*0.5f);  }}    

Advertisement
My height data is in just a 1-dimensional array, that is heightdata[65536].

But, I don''t know how I can put these values into a 2d array.

Any suggestions? I can''t figure out a loop that can work.
change height[x][y] to height[(x << 8) + y].

this totally depends on how you stored the original (1-dimensional) array, ie. ordered by rows or columns first.

normally one would create multiple rows of columns, where y addresses a row and x a column. then you would get: (height[y][x] ->) height[(y << 8] + x].

Hope that makes everything clear.
I think you're looking for this..

        for(x=0; x<256; x++){   for(y=0; y<256; y++)   {      NewArray[x][y] = OriginalArray[(x*256)+y]   }}        


That should do it.. I tried to do it with as few outside parameters as possible.. therefore it's prone to errors.. But, it should work.. let me know if that's what you wanted..


bosco()




--
leader of the free world .. or something ..

Edited by - bosco on July 25, 2000 11:53:57 PM
--leader of the free world .. or something ..
Yes, that is exactly what I''m looking for.


I still may need help figuring out how to draw the triangles though.

This topic is closed to new replies.

Advertisement