|
|

楼主 |
发表于 2009-4-1 21:02:00
|
显示全部楼层
Re:绘制地形出错,出现一层层的块片状东西
没有啊,程序中就没有涉及到z buffer啊 会不会是顶点计算错误什么的 下面是 计算顶点和索引的代码,我看了好几遍,也应该没有错啊
bool CTerrain::ComputeVertex()
{
HRESULT hr = 0;
hr = m_pDevice->CreateVertexBuffer(
m_numVertices * sizeof(TerrainVertex),
D3DUSAGE_WRITEONLY,
0,
D3DPOOL_MANAGED,
&m_pVertexBuf,
0);
if(FAILED(hr))
{
return false;
}
int startX = -m_width / 2;
int startZ = m_depth / 2;
int endX = m_width / 2;
int endZ = -m_depth / 2;
//==compute vertex texture coordinate
float uCoordIncementSize = 1.0f / (float)m_numCellPerRow;
float vCoordIncementSize = 1.0f / (float)m_numCellPerCol;
TerrainVertex *vb = 0;
m_pVertexBuf->Lock(0, 0, (void**)&vb, 0);
int i = 0;
for(int z = startZ; z > endZ; z -= m_cellSpacing)
{
int j = 0;
for(int x = startX; x < endX; x += m_cellSpacing)
{
int index = i * m_numCellPerRow + j;
D3DXVECTOR3 normal = ComputeNormal(i, j);
vb[index] = TerrainVertex(
(float)x,
(float)m_heightMap[index],
(float)z,
normal,
uCoordIncementSize * j,
vCoordIncementSize * i,
0,
0);
j++;
}
i++;
}
m_pVertexBuf->Unlock();
const D3DVERTEXELEMENT9 elements[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 16, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
{ 0, 28, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
{ 0, 36, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
D3DDECL_END()
};
hr = m_pDevice->CreateVertexDeclaration(elements, &m_pVerDelc);
if(FAILED(hr))
{
return false;
}
return true;
}
bool CTerrain::ComputeIndex()
{
HRESULT hr;
hr = m_pDevice->CreateIndexBuffer(
m_numTriangles * 3 * sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&m_pIndexBuf,
0);
if(FAILED(hr))
{
return false;
}
WORD *ib = 0;
m_pIndexBuf->Lock(0, 0, (void**)&ib, 0);
int index = 0;
for(int i = 0; i < m_numCellPerCol; i++)
{
for(int j = 0; j < m_numCellPerRow; j++)
{
ib[index] = i * m_numCellPerRow + j;
ib[index + 1] = i * m_numCellPerRow + j + 1;
ib[index + 2] = (i + 1) * m_numCellPerRow + j;
ib[index + 3] = (i + 1) * m_numCellPerRow + j;
ib[index + 4] = i * m_numCellPerRow + j + 1;
ib[index + 5] = (i + 1) * m_numCellPerRow + j + 1;
//next quad
index += 6;
}
}
m_pIndexBuf->Unlock();
return true;
}
|
|