Original Post
I''ve been having problems rendering my terrain. I''m using a binary tree, but have simplified it to just drawing a grid, to try to solve my problem. I''ve tried all sorts of things, but have had no luck. Ingame nothing shows up. However when looking towards the point (0,0,0), I get a significant slowdown.
So what I think is happening is that all the vertices are getting stuck at (0,0,0). This code just generates the terrain and makes a vertex and index buffer.
Yes I know the terrain would be flat.
Now this bit of code sends it to be rendered.
And finally here is the declaration of my vertex struct.
I''m sure it must be a very simple mistake I''ve made, that I''ll only ever make this once. I''m sorry to post so much code, but this has been bugging me for the past few days.
int gw = 50;
NumVertices = (gw+1) * (gw+1);
TERRAINVERTEX *g_Vertices = new TERRAINVERTEX[NumVertices];
int i, j;
for(i = 0; i < (gw+1); i++)
{
for(j = 0; j < (gw+1); j++)
{
g_Vertices[i*(gw+1) + j].x = i*100.0f;
g_Vertices[i*(gw+1) + j].y = 100;
g_Vertices[i*(gw+1) + j].z = j*100.0f;
g_Vertices[i*(gw+1) + j].tu = ((float)i)/((float)gw);
g_Vertices[i*(gw+1) + j].tv = ((float)j)/((float)gw);
}
}
NumFaces = gw * gw * 2;
WORD *pIndices = new WORD[NumFaces*3];
for(i = 0; i < gw; i++)
{
for(j = 0; j < gw; j++)
{
pIndices[i*6*gw + j*6 + 0] = (i+0) * (gw+1) + (j+0);
pIndices[i*6*gw + j*6 + 1] = (i+1) * (gw+1) + (j+0);
pIndices[i*6*gw + j*6 + 2] = (i+0) * (gw+1) + (j+1);
pIndices[i*6*gw + j*6 + 3] = (i+0) * (gw+1) + (j+1);
pIndices[i*6*gw + j*6 + 4] = (i+1) * (gw+1) + (j+0);
pIndices[i*6*gw + j*6 + 5] = (i+1) * (gw+1) + (j+1);
}
}
g_pD3DDevice->CreateVertexBuffer( NumVertices*sizeof(TERRAINVERTEX),
0, D3DFVF_TERRAINVERTEX,
D3DPOOL_DEFAULT, &terrainVB, NULL );
VOID* pVertices;
terrainVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 );
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
terrainVB->Unlock();
WORD *pwIndices;
g_pD3DDevice->CreateIndexBuffer(NumFaces * 3 * sizeof(WORD), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &terrainIB, NULL);
terrainIB->Lock(0, NumFaces * 3 * sizeof(WORD), (void**) &pwIndices, D3DLOCK_DISCARD);
memcpy( pwIndices, pIndices, sizeof(pIndices) );
terrainIB->Unlock();
delete [] pIndices;
delete [] g_Vertices;
g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1); g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE); g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE); g_pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP, D3DTOP_DISABLE); g_pD3DDevice->SetSamplerState(0,D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); g_pD3DDevice->SetSamplerState(0,D3DSAMP_MINFILTER, D3DTEXF_LINEAR); g_pD3DDevice->SetSamplerState(0,D3DSAMP_MIPFILTER, D3DTEXF_LINEAR); g_pD3DDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR ); g_pD3DDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_MIRROR ); g_pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); g_pD3DDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME ); g_pD3DDevice->SetStreamSource( 0, terrainVB, 0, sizeof(TERRAINVERTEX)); g_pD3DDevice->SetFVF( D3DFVF_TERRAINVERTEX ); g_pD3DDevice->SetTexture( 0, terrainTexture ); g_pD3DDevice->SetIndices(terrainIB); g_pD3DDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, NumVertices, 0, NumFaces ); g_pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW ); g_pD3DDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID ); g_pD3DDevice->SetTexture(0,NULL); g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
struct TERRAINVERTEX
{
FLOAT x, y, z;
FLOAT tu, tv;
};
#define D3DFVF_TERRAINVERTEX (D3DFVF_XYZ|D3DFVF_TEX1)