|
|
在做一个大地形的引擎,发现当顶点数大于257×257时,顶点不能全部渲染出来,后面的顶点没被渲染,不知是否VertexBuffer空间有上限,请指教。下面是用到的代码片断:
生成VertexBuffer和IndexBuffer:
if(FAILED(m_device->CreateVertexBuffer(m_iNumVertices*sizeof(TERRAINVERTEX), D3DUSAGE_WRITEONLY,
D3DFVF_TERRAINVERTEX, D3DPOOL_MANAGED, &m_pVB, NULL) ) )
{
m_pVB = NULL;
MessageBox(NULL, "can not create the vertex buffer", "D3DEngine",MB_OK);
: ostQuitMessage(0);
}
if(FAILED(m_device->CreateIndexBuffer(
/*m_iNumCellsPerRow * m_iNumCellsPerCol * 2 * 3 * sizeof(DWORD)*/25000*sizeof(DWORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX32,
D3DPOOL_MANAGED,
&m_pIB,
0)))
{
m_pIB = NULL;
MessageBox(NULL, "can not create the index buffer", "D3DEngine",MB_OK);
::PostQuitMessage(0);
}
初始化VertexBuffer:
void CQuadTree::InitVertexBuf() //初始化顶点缓冲,渲染时只改变索引缓冲加快效率
{
TERRAINVERTEX * VertexBuf = 0;
float texspace = 1.0 / m_iNumCellsPerRow;
float x, z;
if(m_pVB == NULL) return;
m_pVB->Lock(0, 0, (void **) &VertexBuf, 0);
for(int i = 0; i < m_iNumVertsPerRow; i++)
for(int j = 0; j < m_iNumVertsPerCol; j++)
{
ArrtoWorld(i, j, x, z);
VertexBuf[i * m_iNumVertsPerRow +j] = TERRAINVERTEX(x, GetHeightEntry(i, j),
z, j * texspace, i * texspace);
}
m_pVB->Unlock();
}
然后根据四叉树动态填充IndexBuffer,最后渲染代码为:
D3DXMATRIX mat;
D3DXMatrixIdentity(&mat);
m_device->SetTransform(D3DTS_WORLD, &mat);
m_device->SetStreamSource(0, m_pVB, 0, sizeof(TERRAINVERTEX));
m_device->SetFVF(D3DFVF_TERRAINVERTEX);
m_device->SetIndices(m_pIB);
m_device->SetTexture(0, m_tex);
// turn off lighting since we're lighting it ourselves
m_device->SetRenderState(D3DRS_LIGHTING, false);
m_device->DrawIndexedPrimitive(
D3DPT_TRIANGLELIST,
0,
0,
m_IndexBuf.size(),
0,
m_IndexBuf.size()/3);
m_device->SetRenderState(D3DRS_LIGHTING, true);
m_IndexBuf.clear(); |
|