|
|
void ShowTerrain(int nRow,int nCol,int nSize)
{
const int col = 5;
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
typedef struct
{
float x,y,z;
float rhw;
DWORD color;
}cellData2;
static cellData2 cellData[col][col];
CMyPos beginPos = {300,300};
int startY = 0;
for(startY = 0 ; startY < nCol;++startY)
{
for (int startX = 0 ; startX < nRow ;++startX)
{
cellData[startY][startX].x = beginPos.x + startX * nSize;
cellData[startY][startX].y = beginPos.y + startY * nSize;
cellData[startY][startX].z = 0;
cellData[startY][startX].rhw = 1;
cellData[startY][startX].color = 0;
}
}
static int indexData[(col - 1) * (col - 1) * 6];
int count = 0;
int n = 0;
for(startY = 0;startY < (nCol -1 ) * (nRow - 1) * 6;startY += 6)
{
indexData[startY] = n;
indexData[startY + 1] = n + 1;
indexData[startY + 2] = (count + 1) * nCol + n;
indexData[startY + 3] = n + 1;
indexData[startY + 4] = (count + 1) * nCol + n + 1;
indexData[startY + 5] = (count + 1) * nCol + n;
++n;
if(n%nCol==0)
++count;
}
LPDIRECT3DVERTEXBUFFER8 vb;
g_D3DDevice->CreateVertexBuffer(3 * sizeof(cellData2),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&vb);
void* vertex;
vb->Lock(0,sizeof(cellData),(BYTE**)&vertex,0);
memcpy(vertex,cellData,sizeof(cellData));
vb->Unlock();
static LPDIRECT3DINDEXBUFFER8 ibs;
//为什么下边一行创建的时候总是报错啊
g_D3DDevice->CreateIndexBuffer( sizeof ( indexData ),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_DEFAULT,&ibs );
VOID* pIndices;
if( FAILED( ibs->Lock( 0, // Fill from start of the buffer.
sizeof(indexData), // Size of the data to load.
(BYTE**)&pIndices, // Returned index data.
0 ) ) ) // Send default flags to the lock.
return;
memcpy( pIndices, indexData, sizeof(indexData) );
ibs->Unlock();
g_D3DDevice->SetStreamSource(0,vb,sizeof(cellData2));
g_D3DDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
g_D3DDevice->SetIndices( ibs, 0 );
g_D3DDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0,nCol * nRow, 0, sizeof(indexData)/(sizeof(int) *3));
}abc |
|