Archived

This topic is now archived and is closed to further replies.

best way of creating indices for terrain?

This topic is 5335 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

Hi all, I''m converting my terrain engine over to use index buffers. Now, one problem I am thinking is the best way to create the index buffer (the indices), as I have a terrain of 256x256 quads I don''t wanna be doing the indices by hand, anyone done this before ? Kind regards, Steve As I get older, my mind becomes more cluttered.

Share this post


Link to post
Share on other sites
I did this recently...I just made all the vertices in left-right top-bottom order then filled in the indicies with this:


  
short *indicies = new short[triVertCount];
short *current_index = indicies;
for (x = 0; x < (nSizeX-1); x++)
{
for (y = 0; y < (nSizeY-1); y++)
{
current_index[0] = (nSizeX*(y))+(x);
current_index[1] = (nSizeX*(y+1))+(x);
current_index[2] = (nSizeX*(y))+(x+1);

current_index[3] = (nSizeX*(y+1))+(x+1);
current_index[4] = (nSizeX*(y))+(x+1);
current_index[5] = (nSizeX*(y+1))+(x);
current_index += 6;
}
}


It just winds the triangles based on the current quad...got it first try after drawing it on a legal pad :-P

Brian J

Share this post


Link to post
Share on other sites
Thanks!

That is just what I wanted. I was trying to see with my hand coded indices if there was a pattern!

Steve

Share this post


Link to post
Share on other sites
Here's how I did it:


          
nCenter = AddTransformedVertex ( NW_X + nRadius, NW_Y + nRadius,m_HeightMap->GetHeight ( NW_X + nRadius, NW_Y + nRadius ) );
nNE = AddTransformedVertex (NW_X + nQuadWidth, NW_Y,m_HeightMap->GetHeight ( NW_X + nQuadWidth, NW_Y ) );
nNW = AddTransformedVertex (NW_X, NW_Y, m_HeightMap->GetHeight ( NW_X, NW_Y ) );
nSW = AddTransformedVertex (NW_X, NW_Y + nQuadWidth ,m_HeightMap->GetHeight ( NW_X, NW_Y + nQuadWidth ) );
nSE = AddTransformedVertex (NW_X + nQuadWidth, NW_Y + nQuadWidth ,m_HeightMap->GetHeight ( NW_X + nQuadWidth, NW_Y +nQuadWidth ) );

// ***** North Vertex *****

m_Indices[m_IndexCount++] = nCenter;
m_Indices[m_IndexCount++] = nNW;
m_Indices[m_IndexCount++] = nNE;

// ***** West Vertex *****

m_Indices[m_IndexCount++] = nCenter;
m_Indices[m_IndexCount++] = nSW;
m_Indices[m_IndexCount++] = nNW;

... etc..



The function, AddTransformedVertex() looks like:


        
UINT32 CTerrain::AddTransformedVertex ( float x, float y, float z )
{
TransformVertex ( &m_Vertexes[m_VertexCount], x, y, z );
return m_VertexCount++;
}

void CTerrain::TransformVertex ( D3DVERTEX *vertex, float x, float y, float z )
{
float map_size = m_HeightMap->GetMapSize();

vertex->x = x;
vertex->z = y;
vertex->y = (z/HEIGHTSCALE);
vertex->tu = x/(map_size/5);
vertex->tv = y/(map_size/5);
}


TransformVertex() just assigns the x,y,z,tu,tv coords to the appropriate vertex (m_Vertexes).

Full source @ cvs.bpopp.net

[edited by - bpopp on May 5, 2003 6:05:10 PM]

Share this post


Link to post
Share on other sites