请教一个Directx9 3D问题

苦逼热狗

路边通讯社社长
VIP
注册
2002-10-12
消息
47,114
荣誉分数
2,376
声望点数
393
我在尝试用directx9来做skeletal animation
3d model来自MilkShape3D


// A structure for our custom vertex type
typedef struct
{
D3DXVECTOR3 p;
D3DXVECTOR3 n;
D3DXVECTOR2 uv;
} D3DVertex_t;

std::vector<D3DVertex_t> m_vertices;
std::vector<word> m_indices;

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)

当我尝试创建一个自己的mesh,把material跟texture读入以后,试图显示出来3d模型
代码:
HRESULT InitGeometry()
{
    g_dwNumMaterials = MilkShape3D->GetNumMaterials();

    int numVerts, i, j,k;
    numVerts = MilkShape3D->GetNumVertices();

    DWORD dwNumFaces = MilkShape3D->GetNumTriangles();
    DWORD dwNumVertices = dwNumFaces * 3;
    m_vertices.resize(dwNumVertices);
    m_indices.resize(dwNumVertices);
    dwNumVertices = 0;
    
    for (i = 0; i < MilkShape3D->GetNumGroups(); i++)
    {
        ms3d_group_t *pGroup = NULL;
        MilkShape3D->GetGroupAt(i, &pGroup);

        for (j = 0; j < pGroup->numtriangles; j++)
        {
            ms3d_triangle_t *pTriangle = NULL;
            MilkShape3D->GetTriangleAt(pGroup->triangleIndices[j], &pTriangle);

            for (k = 0; k < 3; k++)
            {
                int nVertexIndex = pTriangle->vertexIndices[k];
                ms3d_vertex_t *pVertex = NULL;
                MilkShape3D->GetVertexAt(nVertexIndex, &pVertex);

                m_indices[dwNumVertices] = nVertexIndex;
                m_vertices[dwNumVertices].p[0] = pVertex->vertex[0];
                m_vertices[dwNumVertices].p[1] = pVertex->vertex[1];
                m_vertices[dwNumVertices].p[2] = pVertex->vertex[2];
                m_vertices[dwNumVertices].n[0] = pTriangle->vertexNormals[k][0];
                m_vertices[dwNumVertices].n[1] = pTriangle->vertexNormals[k][1];
                m_vertices[dwNumVertices].n[2] = pTriangle->vertexNormals[k][2];
                m_vertices[dwNumVertices].uv[0] = pTriangle->s[k];
                m_vertices[dwNumVertices].uv[1] = pTriangle->t[k];
                dwNumVertices++;
            }
        }
    }
    //Create Empty Mesh
    D3DXCreateMeshFVF(dwNumFaces, m_vertices.size(),D3DXMESH_DYNAMIC,D3DFVF_VERTEX,g_pd3dDevice,&g_pMesh);
    
    //Copy vertices
    VOID* pVertices;
    g_pMesh->LockVertexBuffer( 0,(void**)&pVertices );
    memcpy( pVertices, &m_vertices[0], sizeof(D3DVertex_t) * m_vertices.size() );
    g_pMesh->UnlockVertexBuffer();

    //copy indices
    WORD* pIndices = NULL;
    g_pMesh->LockIndexBuffer(0, (void**)&pIndices);
    memcpy( pIndices, &m_indices[0], sizeof(word) * m_indices.size() );
    g_pMesh->UnlockIndexBuffer();

    // We need to extract the material properties and texture names from the 
    g_pMeshMaterials = new D3DMATERIAL9[g_dwNumMaterials];
    if( g_pMeshMaterials == NULL )
        return E_OUTOFMEMORY;
    g_pMeshTextures  = new LPDIRECT3DTEXTURE9[g_dwNumMaterials];
    if( g_pMeshTextures == NULL )
        return E_OUTOFMEMORY;

    for( DWORD i=0; i<g_dwNumMaterials; i++ )
    {
        
        ms3d_material_t *pMaterial = NULL;
        MilkShape3D->GetMaterialAt(i, &pMaterial);

        // Copy the material
        g_pMeshMaterials[i].Ambient = pMaterial->ambient;
        g_pMeshMaterials[i].Diffuse = pMaterial->diffuse;
        g_pMeshMaterials[i].Emissive = pMaterial->emissive;
        g_pMeshMaterials[i].Specular = pMaterial->specular;
        g_pMeshMaterials[i].Power = pMaterial->shininess;

        g_pMeshTextures[i] = NULL;

        if( pMaterial->texture != NULL && lstrlenA(pMaterial->texture) > 0 )
        {
            
            char ab[133];
            sprintf_s(ab, "%s\n", pMaterial->texture);
            MessageBox(hWnd , ab, "test", S_OK);
            // Create the texture
            if( FAILED( D3DXCreateTextureFromFileA( g_pd3dDevice, 
                                                pMaterial->texture, 
                                                &g_pMeshTextures[i] ) ) )
            {
                // If texture is not in current folder, try parent folder
                const CHAR* strPrefix = "..\\";
                CHAR strTexture[MAX_PATH];
                StringCchCopyA( strTexture, MAX_PATH, strPrefix );
                StringCchCatA( strTexture, MAX_PATH, pMaterial->texture );
                // If texture is not in current folder, try parent folder
                if( FAILED( D3DXCreateTextureFromFileA( g_pd3dDevice, 
                                                    strTexture, 
                                                    &g_pMeshTextures[i] ) ) )
                {
                    MessageBox(NULL, "Could not find texture map", "Meshes.exe", MB_OK);
                }
            }
        }
    }

    return S_OK;
}
我首先建立了一个空的pMesh
然后把vertices跟indice数据考入pMesh的对应buff
再读取material跟texture,但是输出的结果却是花屏
 

附件

  • untitled.JPG
    untitled.JPG
    83.8 KB · 查看: 68
后退
顶部