0
\$\begingroup\$

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

//

\$\endgroup\$
3
  • \$\begingroup\$ Probably solved. Will confirm later. \$\endgroup\$
    – Loryan55
    Commented Jul 26, 2013 at 16:37
  • \$\begingroup\$ If you solve it can you please post your solution. I have had this issue in the past \$\endgroup\$
    – CobaltHex
    Commented Jul 27, 2013 at 1:19
  • \$\begingroup\$ You can check it, but i still don't understand what i did to solve it. :( Looks like really import flags or something, maybe i forgot triangulation (lol). \$\endgroup\$
    – Loryan55
    Commented Jul 27, 2013 at 19:56

1 Answer 1

4
\$\begingroup\$

Well i dont know how, but i have solved my problems and for now i can load and render my model (except i need to work on shaders now:P ). Probably my error was in import pre/post-process flags of assimp. Now they are:

    unsigned int processFlags =
aiProcess_CalcTangentSpace         | // calculate tangents and bitangents if possible
aiProcess_JoinIdenticalVertices    | // join identical vertices/ optimize indexing
//aiProcess_ValidateDataStructure  | // perform a full validation of the loader's output
aiProcess_Triangulate              | // Ensure all verticies are triangulated (each 3 vertices are triangle)
aiProcess_ConvertToLeftHanded      | // convert everything to D3D left handed space (by default right-handed, for OpenGL)
aiProcess_SortByPType              | // ?
aiProcess_ImproveCacheLocality     | // improve the cache locality of the output vertices
aiProcess_RemoveRedundantMaterials | // remove redundant materials
aiProcess_FindDegenerates          | // remove degenerated polygons from the import
aiProcess_FindInvalidData          | // detect invalid model data, such as invalid normal vectors
aiProcess_GenUVCoords              | // convert spherical, cylindrical, box and planar mapping to proper UVs
aiProcess_TransformUVCoords        | // preprocess UV transformations (scaling, translation ...)
aiProcess_FindInstances            | // search for instanced meshes and remove them by references to one master
aiProcess_LimitBoneWeights         | // limit bone weights to 4 per vertex
aiProcess_OptimizeMeshes           | // join small meshes, if possible;
aiProcess_SplitByBoneCount         | // split meshes with too many bones. Necessary for our (limited) hardware skinning shader
0;

Mesh (vertices, indices and normals) load code, with D3D11 buffers initialization:

// D3D buffer resources:
D3D11_BUFFER_DESC       buffDesc;   ZeroMemory( &buffDesc, sizeof(D3D11_BUFFER_DESC) );
D3D11_SUBRESOURCE_DATA  buffData;   ZeroMemory( &buffData, sizeof(D3D11_SUBRESOURCE_DATA) );
CDevice* pDevice = pModelOf->GetDevice();

// Create vertex array:
m_uVertexCount = mesh.mNumVertices;
sVertex * vertices = new sVertex[m_uVertexCount];
for(UINT v = 0; v < m_uVertexCount; v++)
{
    sVertex point;
    point.Position = AiToVector4(mesh.mVertices[v]);
    //point.Color = VectorSet4(1,0,0,1);
    point.Normal = AiToVector4(mesh.mNormals[v]);
    vertices[v] = point;
}

// Create D3D vertex buffer:
buffDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
buffDesc.Usage = D3D11_USAGE_DEFAULT;
buffDesc.ByteWidth = sizeof(sVertex) * m_uVertexCount;
buffData.pSysMem = vertices;
if( FAILED(pDevice->GetDevice()->CreateBuffer(&buffDesc, &buffData, &m_pVertexBuffer)) )
    throw new CException(ERR_DX, "Create vertex buffer failed.");

// Create index array:
m_uIndexCount = mesh.mNumFaces * 3;
UINT* indices = new UINT[m_uIndexCount];
for (UINT face = 0; face < mesh.mNumFaces; face++)
    for (UINT idx = 0; idx < 3; idx++)
        indices[face*3+idx] = mesh.mFaces[face].mIndices[idx];

// Create D3D index buffer:
buffDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
buffDesc.Usage = D3D11_USAGE_DEFAULT;
buffDesc.ByteWidth = sizeof(UINT) * m_uIndexCount;
buffData.pSysMem = indices;
if( FAILED(pDevice->GetDevice()->CreateBuffer(&buffDesc, &buffData, &m_pIndexBuffer)) )
    throw new CException(ERR_DX, "Create index buffer failed.");
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .