Directx D3DX9自定义网格在渲染期间与自身重叠

Directx D3DX9自定义网格在渲染期间与自身重叠,directx,overlap,mesh,culling,Directx,Overlap,Mesh,Culling,我有一个自定义的模型文件格式,我从中读取该格式以在DX中创建模型。我用 DWORD dwFVF = ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 ); D3DXCreateMeshFVF(numIndices/3, numVertices, D3DXMESH_MANAGED, dwFVF, *d3ddev, mesh); 要创建网格,请依次锁定、填充、解锁索引缓冲区、顶点缓冲区和属性缓冲区 void createMeshFromSkn(ifstream*

我有一个自定义的模型文件格式,我从中读取该格式以在DX中创建模型。我用

DWORD dwFVF = ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 );
D3DXCreateMeshFVF(numIndices/3, numVertices, D3DXMESH_MANAGED, dwFVF, *d3ddev, mesh);
要创建网格,请依次锁定、填充、解锁索引缓冲区、顶点缓冲区和属性缓冲区

void createMeshFromSkn(ifstream* fHandle, LPDIRECT3DDEVICE9 * d3ddev, LPD3DXMESH * mesh)
{
        // Start reading the file
        int magic = readInt(fHandle);
        short version = readShort(fHandle);
        short numObjects = readShort(fHandle);

        SKNMaterial *materialHeaders;

        if (version > 0)
        {
                // Read in the material headers
                int numMaterialHeaders = readInt(fHandle);
                fHandle->seekg((16 + MATERIAL_NAME_SIZE) * numMaterialHeaders, ios::cur);

                // Read in model data.
                int numIndices = readInt(fHandle);
                int numVertices = readInt(fHandle);

                // Create the mesh
                DWORD dwFVF = ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 );
                D3DXCreateMeshFVF(numIndices/3, numVertices, D3DXMESH_MANAGED, dwFVF, *d3ddev, mesh);

                // Read in the index buffer
                WORD* indexBuffer = 0;
                (*mesh)->LockIndexBuffer(0, (void**)&indexBuffer);

                for (int i = 0; i < numIndices; i++)
                {
                        indexBuffer[i] = readShort(fHandle);
                }

                (*mesh)->UnlockIndexBuffer();

                // Read in the vertexBuffer
                D3DVERTEX* vertexBuffer;
                (*mesh)->LockVertexBuffer( 0, (void**)&vertexBuffer);

                for (int i = 0; i < numVertices; ++i)
                {
                        ((D3DVERTEX*)vertexBuffer)[i].position.x = readFloat(fHandle);
                        ((D3DVERTEX*)vertexBuffer)[i].position.y = readFloat(fHandle);
                        ((D3DVERTEX*)vertexBuffer)[i].position.z = readFloat(fHandle);


                        for (unsigned int j = 0; j < BONE_INDEX_SIZE; ++j)
                        {
                                int bone = (int) readByte(fHandle);
                                //data->vertices[i].boneIndex[j] = bone;
                        }

                        //////////////////////////////////////////////////////////////////////////
                        //
                        // Need to fix this to work with bones
                        //
                        //////////////////////////////////////////////////////////////////////////
                        D3DXVECTOR4 weight;

                        weight.x = readFloat(fHandle);
                        weight.y = readFloat(fHandle);
                        weight.z = readFloat(fHandle);
                        weight.w = readFloat(fHandle);

                        ((D3DVERTEX*)vertexBuffer)[i].normal.x = readFloat(fHandle);
                        ((D3DVERTEX*)vertexBuffer)[i].normal.y = readFloat(fHandle);
                        ((D3DVERTEX*)vertexBuffer)[i].normal.z = readFloat(fHandle);

                        ((D3DVERTEX*)vertexBuffer)[i].tu = readFloat(fHandle);
                        ((D3DVERTEX*)vertexBuffer)[i].tv = readFloat(fHandle);
                }

                (*mesh)->UnlockVertexBuffer();

                DWORD *pAttribBuf;
                HRESULT hRslt = (*mesh)->LockAttributeBuffer(0, &pAttribBuf);
                if(hRslt != D3D_OK)
                        return; // Add error handling

                unsigned int numFaces = (*mesh)->GetNumFaces();
                for(unsigned int i=0; i<numFaces; i++)
                        pAttribBuf[i]= 0;

                hRslt = (*mesh)->UnlockAttributeBuffer();
                if(hRslt != D3D_OK)
                        return; // Add error handling

                DWORD *m_pAdjacencyBuffer;
                m_pAdjacencyBuffer = new DWORD[3 * (*mesh)->GetNumFaces()];
                (*mesh)->GenerateAdjacency(0.0f, m_pAdjacencyBuffer);

                (*mesh)->OptimizeInplace(D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE, m_pAdjacencyBuffer, NULL, NULL, NULL);

        }

        return;
}
我还启用了z缓冲区,但我很确定这只是两个网格之间的缓冲区,而不是网格和网格本身之间的缓冲区

我花了一天半的时间在谷歌上寻找解决方案,但什么也找不到。任何帮助或帮助链接都将不胜感激。

事实证明,我实际上没有打开Z缓冲,因为我需要在d3d演示参数中打开它:

d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
有一次,我这样做了,并添加了一个

d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
对于渲染循环,它将正确渲染

哇,很高兴我发现了这一点。我希望这能帮助其他人探索DX

d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);