• Advertisement
Sign in to follow this  

Assimp does not load data

This topic is 407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to import vertices from assimp but it does not load the data. I initialize std::vector<XMFLOAT3> positions; and after the loadModel method the positions vector is still empty. Here's my code:

main.cpp:

 

ModelMesh* model = new ModelMesh();

int numVertices = 0;
int numIndices = 0;
std::vector<XMFLOAT3> positions;
std::vector<XMFLOAT3> normals;
std::vector<XMFLOAT2> texCoords;
std::vector<unsigned int> indices;
model->LoadMesh("file.x", positions, normals,
texCoords, indices, numVertices, numIndices);
// Create vertex buffer

if (positions.size() == 0)
{
MessageBox(0, L"Vertices vector is empty.",
L"Error", MB_OK);
}
Vertex* vList = new Vertex[positions.size()];
for (size_t i = 0; i < positions.size(); i++)
{
Vertex vert;
XMFLOAT3 pos = positions[i];
vert.position = XMFLOAT3(pos.x, pos.y, pos.z);
vList[i] = vert;
}

 

ModelMesh.cpp:

bool ModelMesh::LoadMesh(const std::string& filename, std::vector<XMFLOAT3> positions, std::vector<XMFLOAT3> normals, std::vector<XMFLOAT2> texCoords, std::vector<unsigned int> indices, int numVertices, int numIndices)
{

bool ret = false;

m_pScene = m_Importer.ReadFile(filename.c_str(), aiProcess_Triangulate
| aiProcess_GenSmoothNormals
| aiProcess_FlipUVs
| aiProcess_JoinIdenticalVertices
| aiPostProcessSteps::aiProcess_FlipWindingOrder
//| aiPostProcessSteps::aiProcess_GenNormals
);

if (m_pScene)
{

ret = InitFromScene(m_pScene, filename, positions, normals, texCoords, indices, numVertices, numIndices);
}
else
{
MessageBox(0, L"Error loading the model.",
L"Error", MB_OK);
assert(m_Importer.GetErrorString());
}

return ret;
}


bool ModelMesh::InitFromScene(const aiScene* pScene, const std::string& filename, std::vector<XMFLOAT3> positions, std::vector<XMFLOAT3> normals, std::vector<XMFLOAT2> texCoords, std::vector<unsigned int> indices, int numVertices, int numIndices)
{
m_entries.resize(pScene->mNumMeshes);

numVertices = 0;
numIndices = 0;

// Count the number of vertices and indices
for (size_t i = 0; i < m_entries.size(); i++) {
m_entries[i].MaterialIndex = pScene->mMeshes[i]->mMaterialIndex;
m_entries[i].NumIndices = pScene->mMeshes[i]->mNumFaces * 3;
m_entries[i].BaseVertex = numVertices;
m_entries[i].BaseIndex = numIndices;

numVertices += pScene->mMeshes[i]->mNumVertices;
numIndices += m_entries[i].NumIndices;
}

// Reserve space in the vectors for the vertex attributes and indices
positions.reserve(numVertices);
normals.reserve(numVertices);
texCoords.reserve(numVertices);
indices.reserve(numIndices);

// Initialize the meshes in the scene one by one
for (size_t i = 0; i < m_entries.size(); i++) {
const aiMesh* paiMesh = pScene->mMeshes[i];
InitMesh(i, paiMesh, positions, normals, texCoords, indices, numVertices, numIndices);
}

return true;
}


void ModelMesh::InitMesh(int meshIndex,
const aiMesh* paiMesh,
std::vector<XMFLOAT3>& positions,
std::vector<XMFLOAT3>& normals,
std::vector<XMFLOAT2>& texCoords,
std::vector<unsigned int>& indices,
int numVertices,
int numIndices)
{
const aiVector3D Zero3D(0.0f, 0.0f, 0.0f);

// Populate the vertex attribute vectors
for (unsigned int i = 0; i < paiMesh->mNumVertices; i++) {
const aiVector3D* pPos = &(paiMesh->mVertices[i]);
const aiVector3D* pNormal = &(paiMesh->mNormals[i]);
const aiVector3D* pTexCoord = paiMesh->HasTextureCoords(0) ? &(paiMesh->mTextureCoords[0][i]) : &Zero3D;

positions.push_back(XMFLOAT3(pPos->x, pPos->y, pPos->z));
normals.push_back(XMFLOAT3(pNormal->x, pNormal->y, pNormal->z));
texCoords.push_back(XMFLOAT2(pTexCoord->x, pTexCoord->y));
}

// Populate the index buffer
for (unsigned int i = 0; i < paiMesh->mNumFaces; i++) {
const aiFace& face = paiMesh->mFaces[i];
assert(face.mNumIndices == 3);
indices.push_back(face.mIndices[0]);
indices.push_back(face.mIndices[1]);
indices.push_back(face.mIndices[2]);
}
}

 

I don't understand why it returns me the `positions` variable still empty. Any idea why?

 

Share this post


Link to post
Share on other sites
Advertisement

You pass in the positions vector into LoadMesh by value. (And the normals, texcoords, etc.) This means a copy of it (probably) gets filled up during the load process, but it gets thrown away once LoadMesh returns.

Share this post


Link to post
Share on other sites

You pass in the positions vector into LoadMesh by value. (And the normals, texcoords, etc.) This means a copy of it (probably) gets filled up during the load process, but it gets thrown away once LoadMesh returns.

Do you suggest me to pass in the pointer to the positions vector into LoadMesh then? Is it a good option?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement