如何在 Directx 10 中加载另一个网格

How to load another mesh in Directx 10

我已经成功地在 DirectX 10 中加载了一个网格,现在我正在尝试加载另一个网格,但是每当我添加一个新的网格时,原始网格就会消失。如何同时显示两个网格?这是我到目前为止尝试过的代码的一部分(如果需要,我可以 post 更多代码):

const D3D10_INPUT_ELEMENT_DESC layout[] =
{
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
    { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
    { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 },
};
UINT numElements = sizeof( layout ) / sizeof( layout[0] );

// Create the input layout
D3D10_PASS_DESC PassDesc;
g_pTechnique->GetPassByIndex( 0 )->GetDesc( &PassDesc );
V_RETURN( pd3dDevice->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature,
                                         PassDesc.IAInputSignatureSize, &g_pVertexLayout ) );

// Set the input layout
pd3dDevice->IASetInputLayout( g_pVertexLayout );

// Load the mesh
V_RETURN( g_Mesh.Create( pd3dDevice, L"Tiger\tiger.sdkmesh", true ) );
V_RETURN( g_Mesh.Create( pd3dDevice, L"Wing\wing.sdkmesh", true ) );


// Initialize the world matrices
D3DXMatrixIdentity( &g_World );

渲染网格:

//
// Render the mesh
//
UINT Strides[1];
UINT Offsets[1];
ID3D10Buffer* pVB[1];
pVB[0] = g_Mesh.GetVB10( 0, 0 );
Strides[0] = ( UINT )g_Mesh.GetVertexStride( 0, 0 );
Offsets[0] = 0;
pd3dDevice->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets );
pd3dDevice->IASetIndexBuffer( g_Mesh.GetIB10( 0 ), g_Mesh.GetIBFormat10( 0 ), 0 );

D3D10_TECHNIQUE_DESC techDesc;
g_pTechnique->GetDesc( &techDesc );
SDKMESH_SUBSET* pSubset = NULL;
ID3D10ShaderResourceView* pDiffuseRV = NULL;
D3D10_PRIMITIVE_TOPOLOGY PrimType;

for( UINT p = 0; p < techDesc.Passes; ++p )
{
    for( UINT subset = 0; subset < g_Mesh.GetNumSubsets( 0 ); ++subset )
    {
        pSubset = g_Mesh.GetSubset( 0, subset );

        PrimType = g_Mesh.GetPrimitiveType10( ( SDKMESH_PRIMITIVE_TYPE )pSubset->PrimitiveType );
        pd3dDevice->IASetPrimitiveTopology( PrimType );

        pDiffuseRV = g_Mesh.GetMaterial( pSubset->MaterialID )->pDiffuseRV10;
        g_ptxDiffuseVariable->SetResource( pDiffuseRV );

        g_pTechnique->GetPassByIndex( p )->Apply( 0 );
        pd3dDevice->DrawIndexed( ( UINT )pSubset->IndexCount, 0, ( UINT )pSubset->VertexStart );
    }
}

代码完全按照您的要求执行:

// Load the mesh
V_RETURN( g_Mesh.Create( pd3dDevice, L"Tiger\tiger.sdkmesh", true ) );
V_RETURN( g_Mesh.Create( pd3dDevice, L"Wing\wing.sdkmesh", true ) );

如果你想同时加载两个网格,你需要使用 g_Mesh 的两个不同实例——我猜是 SDKmesh.cpp / SDKMesh.h 那些旧样本的帮手。

I don't know why you'd be using DirectX 10. There's no value in using it at all as every single supported platform that supports DirectX 10 also supports DirectX 11, which is far better supported and supports a broader range of hardware. TL;DR: Use DirectX 11, not DirectX 10

You are making use of the deprecated D3DX10 utility library from the legacy DirectX SDK, which requires using the old DXSETUP deployment. You should consider instead using the open source replacements for this functionality--which again assumes you are using DirectX 11 instead of the older 10.x API. For example instead of using Effects for Direct3D 10, you can use Effects for Direct3D 11.