DirectX 9 Terrain生成C ++

时间:2015-04-05 13:49:04

标签: c++ directx terrain

我在生成带有四边形的平坦地形时遇到问题,我认为四边形位置正确但索引不正确..有人可以看看并告诉我我做错了什么或我如何解决它?基本上显示地形但完全错误。我认为这是指数的定位,请帮助我,我将非常感激。

FOR LOOP:

    for(int i = 0; i < NUM_VERTS; i += 4){
    for(int x = 0; x < j; x ++){
        for(int z = 0; z < j; z ++){
            verts[i] = D3DVertex::VertexPositionNormalTexture(x, -1.0f, z+1, n, 1.0f, g, 0.0f, 0.0f);
            verts[i+1] = D3DVertex::VertexPositionNormalTexture(x+1, -1.0f, z+1, n, 1.0f, g, 1.0f, 0.0f);
            verts[i+2] = D3DVertex::VertexPositionNormalTexture(x, -1.0f, z, n, 1.0f, g, 0.0f, 1.0f);
            verts[i+2] = D3DVertex::VertexPositionNormalTexture(x+1, -1.0f, z, n, 1.0f, g, 0.0f, 1.0f);
            indices[i] = i;
            indices[i+1] = i+1;
            indices[i+2] = i+2;
            indices[i+3] = i+2;
            indices[i+4] = i+1;
            indices[i+5] = i+3;
        }
    }
    //MessageBox(NULL, L"Test", NULL, NULL);
}

1 个答案:

答案 0 :(得分:1)

你最外面的循环没有意义。相反,为顶点缓冲区和索引缓冲区维护两个索引,如下所示:

int iVertex = 0;
int iIndex = 0;
for(int x = 0; x < j; x ++){
    for(int z = 0; z < j; z ++){
        verts[iVertex  ] = D3DVertex::VertexPositionNormalTexture(x, -1.0f, z+1, n, 1.0f, g, 0.0f, 0.0f);
        verts[iVertex+1] = D3DVertex::VertexPositionNormalTexture(x+1, -1.0f, z+1, n, 1.0f, g, 1.0f, 0.0f);
        verts[iVertex+2] = D3DVertex::VertexPositionNormalTexture(x, -1.0f, z, n, 1.0f, g, 0.0f, 1.0f);
        verts[iVertex+3] = D3DVertex::VertexPositionNormalTexture(x+1, -1.0f, z, n, 1.0f, g, 0.0f, 1.0f);
        indices[iIndex  ] = iVertex;
        indices[iIndex+1] = iVertex+1;
        indices[iIndex+2] = iVertex+2;
        indices[iIndex+3] = iVertex+2;
        indices[iIndex+4] = iVertex+1;
        indices[iIndex+5] = iVertex+3;

        iVertex += 4;
        iIndex += 6;
    }
}

我认为你知道地形会在0j + 1方向从x延伸到z

如果它只是一个平面,您也可以考虑使用单个四边形。如果需要多个三角形,可以考虑将三角形列表转换为三角形条带。如果你想坚持三角形列表,你至少应该考虑删除重复的顶点(地形中心的每个顶点都存在四次)。