PDA

View Full Version : Terrain Rendering Using Vertex Arrays?



Heaven
03-21-2001, 08:15 AM
Could somebody help me out with theory here? I'd sure appreciate it.

Map is say, 1024x1024, an array of floats representing z values loaded from a greyscale bitmap at runtime:

float z[1024][1024]; // scaled to 0.0f - 32.0f;

I want to display 128x128 cells at a time, so ignoring displacement within the big map, to build my viewable chunk each frame I'd populate a vertex and a color array right?

float vertices[128*128*18],
color[128*128*18]; // 18 because I'm drawing 2 tris per cell to form a quad at 3 verts per tri

for (float x=0.0f;x<128.0f;x+=1.0f)
{
for (float y=0.0f;y<128.0f;y+=1.0f)
{
float h1=z[(int)x][(int)y],
h2=z[(int)x][(int)y+1],
h3=z[(int)x+1][(int)y],
h4=z[(int)x+1][(int)y+1];

float v1[3]={x,y,h1]}, c1=h1*8.0f,
v2[3]={x,y-1.0f,h2}, c2=h2*8.0f,
v3[3]={x+1.0f,y,h3}, c3=h3*8.0f,
v4[3]={x+1.0f,y-1.0f,h4}, c4=h4*8.0f;

int idx=(int)x*18+(int)y*2304; // 2304 is 128 * 18

vertices[idx+0]=v1[0]; color[idx+0]=c1;
vertices[idx+1]=v1[1]; color[idx+1]=c1;
vertices[idx+2]=v1[2]; color[idx+2]=c1;

vertices[idx+3]=v2[0]; color[idx+3]=c2;
vertices[idx+4]=v2[1]; color[idx+4]=c2;
vertices[idx+5]=v2[2]; color[idx+5]=c2;

vertices[idx+6]=v3[0]; color[idx+6]=c3;
vertices[idx+7]=v3[1]; color[idx+7]=c3;
vertices[idx+8]=v3[2]; color[idx+8]=c3;

vertices[idx+9]=v3[0]; color[idx+9]=c3;
vertices[idx+10]=v3[1]; color[idx+10]=c3;
vertices[idx+11]=v3[2]; color[idx+11]=c3;

vertices[idx+12]=v2[0]; color[idx+12]=c2;
vertices[idx+13]=v2[1]; color[idx+13]=c2;
vertices[idx+14]=v2[2]; color[idx+14]=c2;

vertices[idx+15]=v4[0]; color[idx+15]=c4;
vertices[idx+16]=v4[1]; color[idx+16]=c4;
vertices[idx+17]=v4[2]; color[idx+17]=c4;
}
}

Then, after doing a glEnableClientState for both GL_VERTEX_ARRAY and GL_COLOR_ARRAY I do the following:

glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, color);

Finally, to draw the chunk I do a:

glBegin(GL_TRIANGLES);

glDrawArrays(GL_TRIANGLES,0,128*128*18);

glEnd();

This is what I'm trying to switch over to coming from manually drawing all the tris myself, and I'm in the process of understanding the * arrays. It seems pretty handy so far, but I was wondering if I'm going about it the wrong way or if there's a quicker way of doing it.

I would definitely expect some savings over manual, since I would be making 6 function calls (vertex+color each for 3 verts) per triangle, or 12 function calls per map cell/quad. That would be 196,608 function calls for 128x128 cells as opposed to my 1 function call to glDrawArrays. Right?

Thanks for any comments.

Care,
Heaven
Heaven@knology.net

Deiussum
03-21-2001, 09:51 AM
First, let me say that I didn't look at the whole code. (Sorry short on time.) Generally speaking using arrays will probably be faster than doing individual calls. The one problem I did see with your code is that you put glDrawArrays in between a glBegin/glEnd... this is a no no. http://www.opengl.org/discussion_boards/ubb/smile.gif You don't need, and shouldn't use a glBegin/glEnd with glDrawArrays/glDrawElements. In fact calling glDrawArrays/glDrawElements within a glBegin/glEnd pair will generate an error.