Search code examples
c++meshdirectx-10

How to load another mesh in Directx 10


I have managed to load a mesh in DirectX 10 and I am now trying to load another mesh but whenever I add a new mesh the original mesh disappears. How can I display two meshes at the same time? This is a section of the code I have tried so far (I can post more code if needed):

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

Rendering the mesh:

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

Solution

  • The code is doing exactly what you asked it to do:

    // 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 ) );
    

    If you want both meshes loaded at the same time, you need to use two different instances of whatever g_Mesh is--which I'm guessing is the SDKmesh.cpp / SDKMesh.h helper stuff from those old samples.

    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.