سؤال

I have custom model file format that I am reading from to create a model in DX. I use

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

to create the mesh, then lock, fill, unlock the index buffer, vertex buffer, and attribute buffer in turn.

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

My problem is that the model is overlapping with itself:

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

I have CCW backface culling enabled:

d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

I also have z-buffer enabled, but I'm pretty sure that's only between two meshes, not between a mesh and itself.

I've spent the last day and a half trying to Google for a solution, but I couldn't find anything. Any help or links to help would be greatly appreciated.

هل كانت مفيدة؟

المحلول

It turns out I hadn't actually turned on Z-buffering, because I needed to turn it on in the d3d presentation parameters:

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

Once I did that and added a

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

to the render loop, it renders correctly.

Wow, so glad I figured this out. I hope this helps others in their explorations of DX

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top