Here is where I create the vertex and index buffers for the terrain:
HRESULT CTerrain::CreateTerrain(int xVerts, int zVerts, float scale)
{
int numVertices = xVerts * zVerts;
int numIndices = xVerts * zVerts * 2;
// Create the vertex buffer.
if( FAILED( d3dDevice->CreateVertexBuffer( numVertices*sizeof(TerrainVertex), D3DUSAGE_WRITEONLY,
D3DFVF_TerrainVertex, D3DPOOL_MANAGED, &pVB, NULL ) ) )
{
return E_FAIL;
}
// Create the index buffer
if( FAILED( d3dDevice->CreateIndexBuffer( numIndices*sizeof(short), D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16, D3DPOOL_MANAGED, &pIB, NULL ) ) )
{
return E_FAIL;
}
int currentVertex;
int index = 0;
TerrainVertex *pVertexData = new TerrainVertex[numVertices];
for(int z=0; z<zVerts; z++)
{
for(int x=0; x<xVerts; x++)
{
currentVertex = z * zVerts + x;
pVertexData[currentVertex].x = (float)x*scale;
pVertexData[currentVertex].y = 0.0f;
pVertexData[currentVertex].z = (float)z*scale;
pVertexData[currentVertex].color = 0xFFFFFFFF;
}
}
numStrips = zVerts - 1;
short *pIndexData = new short[numIndices];
for(z=0; z<numStrips; z++)
{
for(int x=0; x<xVerts; x++)
{
currentVertex = z * zVerts + x;
pIndexData[index++] = currentVertex + zVerts;
pIndexData[index++] = currentVertex;
}
}
// Fill the vertex buffer.
VOID* pVertices;
if( FAILED( pVB->Lock( 0, sizeof(pVertexData), (void**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, pVertexData, numVertices*sizeof(TerrainVertex) );
pVB->Unlock();
// Fill the index buffer
VOID* pIndices;
if( FAILED( pIB->Lock( 0, sizeof(pIndexData), (void**)&pIndices, 0 ) ) )
return E_FAIL;
memcpy( pIndices, pIndexData, numIndices*sizeof(short) );
pIB->Unlock();
delete[] pVertexData;
delete[] pIndexData;
return S_OK;
}
and here is where I render them
d3dDevice->SetStreamSource( 0, pVB, 0, sizeof(TerrainVertex) );
d3dDevice->SetIndices(pIB);
d3dDevice->SetFVF( D3DFVF_TerrainVertex );
for(int i=0; i<numStrips; i++)
d3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLESTRIP, 0, 0, 10, 0, 8);
i can't seem to figure what parameters to give to the drawindexedprimitive function. im just not sure on the formula i need to give for the 1st,2nd, and 4th parameters. please help because this has really been driving me crazy.
[edited by - adamxiii on August 10, 2003 12:29:48 AM]