Making a grid

Started by
3 comments, last by adamxiii 20 years, 8 months ago
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]
Advertisement

Check the SDK Docs for function parameters.
okis this is how i did the same thing to generate a terrian for a RTS style game. it might help ya, by the way this code generates a mesh of connected diamonds not squares or triangles... diamonds are easier to tile up together and scroll....

struct CUSTOMVERTEX{	D3DXVECTOR3 position; // The position        D3DCOLOR    color;    // The color        FLOAT       tu, tv;   // The texture coordinates};const int MAP_VER_TILES=41;const int MAP_HOR_TILES=24;// MAP_VER_TILES * MAP_HOR_TILES = 984LPDIRECT3DVERTEXBUFFER9 pLandMesh[984]; LPDIRECT3DVERTEXBUFFER9 g_pUIVB;


   HRESULT CreateTiles(){		int i;	int j;	for (j=0;j<MAP_VER_TILES;j++)		for(i=0;i<MAP_HOR_TILES;i++)		{			// Create the vertex buffer.			if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),                                                  0 , D3DFVF_CUSTOMVERTEX,                                                  D3DPOOL_DEFAULT, &pLandMesh[j*MAP_HOR_TILES+i], NULL ) ) )			{				return E_FAIL;			}			CUSTOMVERTEX* pVertices;			pLandMesh[j*MAP_HOR_TILES+i]->Lock( 0, 0, (void**)&pVertices, 0);								// Tiles of size 64*32			float OffsetX = (j%2==0)?(i*64.0f):(i*64.0f - 32.0f);			float OffsetY = -j*16.0f;			pVertices[0].color =	0xffffffff;			pVertices[0].tu =0.0f;			pVertices[0].tv =0.0f;			pVertices[1].color =	0xffffffff;			pVertices[1].tu =1.0f;			pVertices[1].tv =0.0f;			pVertices[2].color =	0xffffffff;			pVertices[2].tu =0.0f;			pVertices[2].tv =1.0f;			pVertices[3].color =	0xffffffff;			pVertices[3].tu =1.0f;			pVertices[3].tv =1.0f;			pLandMesh[j*MAP_HOR_TILES+i]->Unlock();								}		return S_OK;}


  HRESULT DrawLandMesh(){ for (j=0;j<MAP_VER_TILES;j++) {    for(i=0;i<MAP_HOR_TILES;i++)   {        g_pd3dDevice->SetTexture( 0, "What ever texture ya wanna apply" );                g_pd3dDevice->SetStreamSource( 0, pLandMesh[j*MAP_HOR_TILES+i], 0, sizeof(CUSTOMVERTEX) );        g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );   } }}


[edited by - Persian_Gamer on August 11, 2003 3:44:01 AM]

[edited by - Persian_Gamer on August 11, 2003 3:45:36 AM]
I guess my real question is why wont this work:

for(int i=0; i<numStrips; i++)    d3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, 0,0, numVertices, i*numXVertices, numPrimitives );


it draws a few strips right but then it all goes to hell.

is the 1st and 2nd parameter supposed to be 0?

[edited by - adamxiii on August 11, 2003 8:51:05 AM]
I fixed my problem. It turns out that the 3rd parameter of the DrawIndexedPrimitive function is supposed to be the total number of vertices in the vertexbuffer not the total vertices used for the call.

The documentation was not very clear about that.

Thanks for the help though

This topic is closed to new replies.

Advertisement