I use Assimp library to import vertex and index data from model file.
I don't know how to correctly create index buffer from mFaces array of aiMesh. Or something wrong, because i my model is incorrect.
I have already experienced successful mesh rendering, if verticies and indicies are defined manually in code (x5 vertices triangle and indicies).
Here is my code.
// Loading TRIANGULATED vertex/index buffers:
// Load verticies:
m_uVertexCount = pMesh->mNumVertices;
CBasicEffect::sVertex * verticies = new CBasicEffect::sVertex[m_uVertexCount];
for(UINT i = 0; i < m_uVertexCount; i++)
{
CBasicEffect::sVertex v;
v.Pos = Vector3FromAi(pMesh->mVertices[i]);
v.Color = XMFLOAT4(1,1,0,1);//test
verticies[i] = v;
}
D3D11_BUFFER_DESC bd;
ZeroMemory( &bd, sizeof(bd) );
D3D11_SUBRESOURCE_DATA InitData;
// Create vertex buffer:
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof( CBasicEffect::sVertex ) * m_uVertexCount;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
InitData.pSysMem = verticies;
if( FAILED(pModelOf->GetDevice()->GetDevice()->CreateBuffer(&bd, &InitData, &m_pVertexBuffer)) )
;//...
// Load indicies:
m_uIndexCount = pMesh->mNumFaces * 3;
UINT* indices = new UINT[m_uIndexCount];
for (UINT face = 0; face < pMesh->mNumFaces; face++)
for (UINT idx = 0; idx < 3; idx++)
*indices++ = pMesh->mFaces[face].mIndices[idx];
// Create index buffer:
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof( UINT ) * m_uIndexCount;
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
bd.CPUAccessFlags = 0;
InitData.pSysMem = &indices;
if( FAILED(pModelOf->GetDevice()->GetDevice()->CreateBuffer(&bd, &InitData, &m_pIndexBuffer)) )
;//...
UPDATE: Now i see my model. But it is loaded incorrecty: https://i.sstatic.net/Z9liR.jpg (originally it is scene with 5 primitives).
My render code:
// <Clean render target view and depth stencil view>
ID3D11Buffer* pVertexBuffer = pMesh->GetVerticiesBuffer();
ID3D11Buffer* pIndexBuffer = pMesh->GetIndexesBuffer();
// Set buffers and topology mode
UINT stride = sizeof( sVertex ), offset = 0;
m_pDevice->GetContext()->IASetVertexBuffers( 0, 1, &pVertexBuffer, &stride, &offset );
m_pDevice->GetContext()->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
m_pDevice->GetContext()->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
// Shaders:
m_pDevice->GetContext()->IASetInputLayout( m_pVertexLayout );
m_pDevice->GetContext()->VSSetShader( m_pVertexShader, NULL, 0 );
m_pDevice->GetContext()->VSSetConstantBuffers( 0, 1, &m_pConstantBuffer );
m_pDevice->GetContext()->PSSetShader( m_pPixelShader, NULL, 0 );
//m_pDevice->GetContext()->PSSetConstantBuffers( 0, 1, &m_pConstantBuffer );
// Draw:
//m_pDevice->GetContext()->Draw( pMesh->GetVertexCount(), 0 );
m_pDevice->GetContext()->DrawIndexed( pMesh->GetIndexCount(), 0, 0 );
//