The terrain 256 X 256 verticies split up into blocks of 16 X 16. Here's the block setup:
int CTerrain::SetupTerrainBlocks(void)
{
unsigned int currentVertex, numBlockPolys;
numBlockPolys = BLOCK_SIZE - 1; // Always 1 less polygon for n vertices
// Loop across each terrain block
for (int z = 0; z < mapheight / numBlockPolys; z++)
for (int x = 0; x < mapwidth / numBlockPolys; x++)
{
mapblocks[x][z] = new TerrainBlock;
int index = 0;
// Setup the indexing info for this map block
for (int z2 = 0; z2 < BLOCK_SIZE -1; z2++)
for (int x2 = 0; x2 < BLOCK_SIZE; x2++)
{
// Move to the correct block
currentVertex = z * numBlockPolys * mapwidth + x * numBlockPolys;
// Move to the correct offset within the block
currentVertex += z2 * mapwidth + x2;
mapblocks[x][z]->indexArray[0][index++] = currentVertex + mapwidth;
mapblocks[x][z]->indexArray[0][index++] = currentVertex;
}
}
return(OK);
}
Here's the rendering code:
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(3, GL_FLOAT, 0, terrain); glTexCoordPointer(2, GL_FLOAT, 0, texcoords); glColorPointer(3, GL_FLOAT, 0, colors); for (int z = 0; z < mapheight / (BLOCK_SIZE - 1); z++) for (int x = 0; x < mapwidth / (BLOCK_SIZE -1); x++) DrawTerrainBlock(mapblocks[x][z]); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY);And here's the DrawTerrainBlock() function:
int CTerrain::DrawTerrainBlock(TerrainBlock *block)
{
// Do frustum culling!
if (!CheckBlockVisibility(block))
return(OK);
int mipmaplevel = 0;
int mipmapHeight, mipmapWidth;
// Set the size of the terrain block depending on the mipmap level
mipmapHeight = (BLOCK_SIZE -1) / pow(2, mipmaplevel) + 1;
mipmapWidth = mipmapHeight;
glEnable(GL_CULL_FACE);
glPolygonMode(GL_FRONT, GL_LINE);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
unsigned int tex = GetEngine()->GetGraphics()->GetTextureManager()->GetSurface(TEX_GRASS_1);
glBindTexture(GL_TEXTURE_2D, tex);
for (int z = 0; z < mipmapHeight - 1; z++)
glDrawElements(GL_TRIANGLE_STRIP, mipmapWidth * 2, GL_UNSIGNED_INT,
&block->indexArray[mipmaplevel][z * mipmapWidth * 2]);
glPolygonMode(GL_FRONT, GL_FILL);
return(0);
}
I'd REALLY appreciate any insight into this.
EDIT: Changed some of the code to make it more human friendly. BTW, why wont the source tags STAY PUT during each edit? It gets rid of all but the first opening tag and the last closing tag!
Edit some more: I think there are a few bugs in the Edit thingy. It deleted some of my code when I tried to edit!
[edited by - BradDaBug on May 31, 2003 12:52:11 AM]