简体   繁体   English

DirectX 9 Terrain生成C ++

[英]DirectX 9 Terrain genereration C++

I am having trouble generating a flat terrain with quads, I think the quads are located correctly but the indices are incorrect.. Can someone please have a look see and tell me what i am doing wrong or how i can fix it? 我在生成带有四边形的平坦地形时遇到麻烦,我认为四边形的位置正确,但是索引不正确。.有人可以看看我是否做错了或者如何解决? Basically the terrain is displayed but totally wrong. 基本上显示了地形,但完全错误。 I THINK it is the indices positioning, please help me it would be highly appreciated. 我认为这是指数的定位,请帮助我,我们将不胜感激。

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);
}

Your outer-most loop does not make sense. 您最外层的循环没有意义。 Instead, maintain two indices for the vertex buffer and the index buffer like so: 而是为顶点缓冲区和索引缓冲区维护两个索引,如下所示:

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;
    }
}

I assume that you're aware that the terrain will stretch from 0 to j + 1 in both x and z direction. 我假设您知道地形会在xz方向上从0延伸到j + 1

If it's just a plane, you might also consider using a single quad. 如果只是飞机,您可能还会考虑使用单个四边形。 If you need multiple triangles, you might consider converting the triangle list to a triangle strip. 如果需要多个三角形,则可以考虑将三角形列表转换为三角形带。 If you want to stick to the triangle list, you should at least consider removing the duplicate vertices (every vertex in the terrain's center exists four times). 如果要坚持使用三角形列表,则至少应考虑删除重复的顶点(地形中心的每个顶点都存在四次)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM