2

我有自定义模型文件格式,我正在从中读取以在 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;
}

我的问题是模型与自身重叠:

http://imageshack.us/a/img210/2732/20121018181019896.png

我启用了逆时针背面剔除:

d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

我也启用了 z-buffer,但我很确定这只是在两个网格之间,而不是在一个网格和它本身之间。

我花了最后一天半的时间尝试谷歌寻求解决方案,但我找不到任何东西。任何帮助或帮助链接将不胜感激。

4

1 回答 1

0

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

d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

一旦我这样做并添加了一个

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

到渲染循环,它会正确渲染。

哇,很高兴我知道了这一点。我希望这有助于其他人探索 DX

于 2012-10-20T04:53:59.350 回答