will martin
will martin

Reputation: 81

Assimp, BGFX and C++ trouble matching vertices to indices

I have been trying to make a generic model class in c++ that can use any underlying data type, defined by it's BGFX layout. To do this I have been using assimp to import the model data. Unfortunately I have been finding that the indices and the vertices appear to be mismatched, and I've tracked the problem down to the function that processes the indices and vertices. This issue persists even when I am only importing the positions and there is no offset between them, and so I am unsure as to where I am going wrong when importing. The mesh class I am importing the data to copies data based on a void* pointer to the source, an offset in bytes to copy into within the mesh buffer, and the size of the data to copy in bytes.

The problematic function:

void ProcessVerticesAndIndices(const aiMesh* mesh, SGModel::Mesh& meshtmp)
{
using namespace bgfx;
using namespace SGModel;

//Preallocate memory
meshtmp.reserveVerticesData(mesh->mNumVertices * meshtmp.layout().getSize(1));
meshtmp.reserveIndicesData(mesh->mNumFaces * TRIANGULATED_VERTEX_COUNT * sizeof(Indice));

size_t vertex_size = meshtmp.layout().getSize(1);
for (size_t i = 0; i < mesh->mNumVertices; ++i)
{
    //Process vertices
    if (mesh->HasPositions())
    {
        //Type sizes are the size of the primitive type of the attribute (e.g. sizeof(float), sizeof(int32_t) etc)
        uint8_t pos_type_size = GetBGFXAttributeTypeSize(GetBGFXAttributeType(Attrib::Position));
        meshtmp.copyVertices(&mesh->mVertices[i].x, i * vertex_size, pos_type_size);
        meshtmp.copyVertices(&mesh->mVertices[i].y, i * vertex_size + pos_type_size, pos_type_size);
        meshtmp.copyVertices(&mesh->mVertices[i].z, i * vertex_size + 2 * pos_type_size, pos_type_size);
    }
}

size_t indices_index = 0;
for(size_t i = 0; i < mesh->mNumFaces; ++i)
{
    auto face = mesh->mFaces[i];
    for (size_t j = 0; j < face.mNumIndices; ++j)
    {
        meshtmp.copyIndices(&face.mIndices[j], indices_index * sizeof(Indice), sizeof(Indice));
        indices_index++;
    }
}

}

The console print of the cube vertices, and indices:

VERTICES (in buffer order):
 Index: 0
 POS: 4 4 4
 Index: 1
 POS: -4 4 4
 Index: 2
 POS: -4 -4 4
 Index: 3
 POS: 4 -4 4
 Index: 4
 POS: 4 -4 -4
 Index: 5
 POS: 4 -4 4
 Index: 6
 POS: -4 -4 4
 Index: 7
 POS: -4 -4 -4
 Index: 8
 POS: -4 -4 -4
 Index: 9
 POS: -4 -4 4
 Index: 10
 POS: -4 4 4
 Index: 11
 POS: -4 4 -4
 Index: 12
 POS: -4 4 -4
 Index: 13
 POS: 4 4 -4
 Index: 14
 POS: 4 -4 -4
 Index: 15
 POS: -4 -4 -4
 Index: 16
 POS: 4 4 -4
 Index: 17
 POS: 4 4 4
 Index: 18
 POS: 4 -4 4
 Index: 19
 POS: 4 -4 -4
 Index: 20
 POS: -4 4 -4
 Index: 21
 POS: -4 4 4
 Index: 22
 POS: 4 4 4
 Index: 23
 POS: 4 4 -4
 INDICES (in buffer order):
 0
 1
 2
 0
 2
 3
 4
 5
 6
 4
 6
 7
 8
 9
 10
 8
 10
 11
 12
 13
 14
 12
 14
 15
 16
 17
 18
 16
 18
 19
 20
 21
 22
 20
 22
 23

The render output (should be a black cube): The render output (should be a black cube)

Upvotes: 0

Views: 213

Answers (1)

KimKulling
KimKulling

Reputation: 2843

For me, this output and the code looks correct. You are storing all the indices in the right way so far.

For me it seems that you have an issue in your render-code. Do you have configure the right index type or vertex type? SOme more input from that side would be really useful.

Upvotes: 0

Related Questions