简体   繁体   English

DirectX11 每个三角形使用缓冲区中的第一个顶点

[英]DirectX11 each triangle using first vertex in the buffer

I'm currently having a problem with my DirectX 11 voxel renderer where whenever a triangle is rendered, the first vertex in the buffer is being used rather than one of the correct vertices.我目前的 DirectX 11 体素渲染器出现问题,每当渲染三角形时,都会使用缓冲区中的第一个顶点,而不是使用正确的顶点之一。

I have a working version of the same mesh generation algorithm in Unity, and after comparing the Indices and Vertices, they are exactly the same so I don't believe the problem is with the algorithm.我在 Unity 中有一个相同网格生成算法的工作版本,在比较了索引和顶点之后,它们完全相同,所以我不认为问题出在算法上。

The reason I know the first vertex in the buffer is being used, is because the first vertex created for each chunk is at the chunk's origin.我知道缓冲区中的第一个顶点正在使用的原因是因为为每个块创建的第一个顶点位于块的原点。 The world position of each chunk GameObject is (0, 0, 0), and the chunk's position is just added as an offset to each vertex when constructing each chunk's mesh.每个chunk GameObject的世界position为(0, 0, 0),而该chunk的position只是在构建每个chunk的mesh时作为每个顶点的偏移量添加。

As you can see in the image below, the triangles are stretching to the origin of the chunk (The first vertex).正如您在下图中看到的,三角形正在拉伸到块的原点(第一个顶点)。 The mountainous shape of the voxel terrain can still be seen, so some vertices are being rendered in the correct position.仍然可以看到体素地形的山地形状,因此一些顶点正在正确的 position 中渲染。 I just do not understand why it appears that every third index gets set to 0 (I'm not sure if this is actually what is happening, it just looks like what I imagine that would look like).我只是不明白为什么似乎每三个索引都设置为 0(我不确定这是否真的发生了,它看起来就像我想象的那样)。

三角形拉伸的图像

Here is the code I am using for creating the vertex and index buffers for each chunk.这是我用于为每个块创建顶点和索引缓冲区的代码。 m_vertices and m_indices are both vectors, which are identical to the working Unity version when I have compared them. m_vertices 和 m_indices 都是向量,当我比较它们时,它们与工作的 Unity 版本相同。

    VoxelMesh mesh;
    DirectX::VertexPositionNormalTexture* verticesArray = new DirectX::VertexPositionNormalTexture[m_vertices.size()];
    for (unsigned int i = 0; i < m_vertices.size(); i++) {
        verticesArray[i].position = m_vertices[i];
        verticesArray[i].normal = m_normals[i];
        verticesArray[i].textureCoordinate = m_uvs[i];
    }

    //Create vertex buffer
    ID3D11Buffer* vertexBuffer;
    D3D11_BUFFER_DESC bd;
    ZeroMemory(&bd, sizeof(bd));
    bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = sizeof(DirectX::VertexPositionNormalTexture) * m_vertices.size();
    bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    bd.CPUAccessFlags = 0;

    D3D11_SUBRESOURCE_DATA InitData;
    ZeroMemory(&InitData, sizeof(InitData));
    InitData.pSysMem = verticesArray;

    device->CreateBuffer(&bd, &InitData, &vertexBuffer);

    mesh.m_VertexBuffer = vertexBuffer;
    mesh.m_VBOffset = 0;
    mesh.m_VBStride = sizeof(DirectX::VertexPositionNormalTexture);


    //Create index buffer
    unsigned int* indicesArray = new unsigned int[m_indices.size()];
    for (unsigned int i = 0; i < m_indices.size(); i++) {
        indicesArray[i] = m_indices[i];
    }

    ID3D11Buffer* indexBuffer;

    D3D11_BUFFER_DESC bd1;
    ZeroMemory(&bd1, sizeof(bd1));
    bd1.Usage = D3D11_USAGE_DEFAULT;
    bd1.ByteWidth = sizeof(WORD) * m_indices.size();
    bd1.BindFlags = D3D11_BIND_INDEX_BUFFER;
    bd1.CPUAccessFlags = 0;

    ZeroMemory(&InitData, sizeof(InitData));
    InitData.pSysMem = indicesArray;
    device->CreateBuffer(&bd1, &InitData, &indexBuffer);

    mesh.m_IndexCount = m_indices.size();
    mesh.m_IndexBuffer = indexBuffer;

    delete[] indicesArray;
    delete[] verticesArray;

VoxelMesh struct:体素网格结构:

struct VoxelMesh {
    ID3D11Buffer* m_VertexBuffer;
    ID3D11Buffer* m_IndexBuffer;
    UINT m_VBStride;
    UINT m_VBOffset;
    UINT m_IndexCount;
};

Your index buffer is declared as an R16_UINT, but the buffer you're providing is using 32 bit indices.您的索引缓冲区被声明为 R16_UINT,但您提供的缓冲区使用 32 位索引。 Either change indicesArray to unsigned short* , or change your buffer to be R32_UINT.要么将indicesArray更改为unsigned short* ,要么将缓冲区更改为 R32_UINT。

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

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