Update M3DImporter.cpp

Fix some review finding:
- Add missing brackets to make code more readable
- fix scope of variables
pull/2973/head
Kim Kulling 2020-01-30 10:00:43 +01:00 committed by GitHub
parent 605336832f
commit a2ef0b5bd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 75 additions and 60 deletions

View File

@ -109,7 +109,9 @@ using namespace std;
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Default constructor // Default constructor
M3DImporter::M3DImporter() : M3DImporter::M3DImporter() :
mScene(nullptr) {} mScene(nullptr) {
// empty
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Returns true, if file is a binary or ASCII Model 3D file. // Returns true, if file is a binary or ASCII Model 3D file.
@ -126,13 +128,7 @@ bool M3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool c
if (!pIOHandler) { if (!pIOHandler) {
return true; return true;
} }
/*
* don't use CheckMagicToken because that checks with swapped bytes too, leading to false
* positives. This magic is not uint32_t, but char[4], so memcmp is the best way
const char* tokens[] = {"3DMO", "3dmo"};
return CheckMagicToken(pIOHandler,pFile,tokens,2,0,4);
*/
std::unique_ptr<IOStream> pStream(pIOHandler->Open(pFile, "rb")); std::unique_ptr<IOStream> pStream(pIOHandler->Open(pFile, "rb"));
unsigned char data[4]; unsigned char data[4];
if (4 != pStream->Read(data, 1, 4)) { if (4 != pStream->Read(data, 1, 4)) {
@ -144,6 +140,7 @@ bool M3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool c
#endif #endif
; ;
} }
return false; return false;
} }
@ -257,19 +254,21 @@ void M3DImporter::importMaterials(const M3DWrapper &m3d) {
aiMaterial *mat = new aiMaterial; aiMaterial *mat = new aiMaterial;
name.Set(std::string(m->name)); name.Set(std::string(m->name));
mat->AddProperty(&name, AI_MATKEY_NAME); mat->AddProperty(&name, AI_MATKEY_NAME);
for (j = 0; j < m->numprop; j++) { for (j = 0; j < m->numprop; ++j) {
// look up property type // look up property type
// 0 - 127 scalar values, // 0 - 127 scalar values,
// 128 - 255 the same properties but for texture maps // 128 - 255 the same properties but for texture maps
k = 256; k = 256;
for (l = 0; l < sizeof(m3d_propertytypes) / sizeof(m3d_propertytypes[0]); l++) for (l = 0; l < sizeof(m3d_propertytypes) / sizeof(m3d_propertytypes[0]); ++l) {
if (m->prop[j].type == m3d_propertytypes[l].id || if (m->prop[j].type == m3d_propertytypes[l].id ||
m->prop[j].type == m3d_propertytypes[l].id + 128) { m->prop[j].type == m3d_propertytypes[l].id + 128) {
k = l; k = l;
break; break;
} }
// should never happen, but be safe than sorry // should never happen, but be safe than sorry
if (k == 256) continue; if (k == 256) {
continue;
}
// scalar properties // scalar properties
if (m->prop[j].type < 128 && aiProps[k].pKey) { if (m->prop[j].type < 128 && aiProps[k].pKey) {
@ -293,6 +292,7 @@ void M3DImporter::importMaterials(const M3DWrapper &m3d) {
} }
mat->AddProperty(&n, 1, aiProps[k].pKey, aiProps[k].type, aiProps[k].index); mat->AddProperty(&n, 1, aiProps[k].pKey, aiProps[k].type, aiProps[k].index);
break; break;
}
} }
} }
// texture map properties // texture map properties
@ -314,7 +314,11 @@ void M3DImporter::importMaterials(const M3DWrapper &m3d) {
// import textures, this is the simplest of all // import textures, this is the simplest of all
void M3DImporter::importTextures(const M3DWrapper &m3d) { void M3DImporter::importTextures(const M3DWrapper &m3d) {
unsigned int i; unsigned int i;
const char *formatHint[] = { "rgba0800", "rgba0808", "rgba8880", "rgba8888" }; static const char *formatHint[] = {
"rgba0800",
"rgba0808",
"rgba8880",
"rgba8888" };
m3dtx_t *t; m3dtx_t *t;
ai_assert(mScene != nullptr); ai_assert(mScene != nullptr);
@ -323,8 +327,9 @@ void M3DImporter::importTextures(const M3DWrapper &m3d) {
mScene->mNumTextures = m3d->numtexture; mScene->mNumTextures = m3d->numtexture;
ASSIMP_LOG_DEBUG_F("M3D: importTextures ", mScene->mNumTextures); ASSIMP_LOG_DEBUG_F("M3D: importTextures ", mScene->mNumTextures);
if (!m3d->numtexture || !m3d->texture) if (!m3d->numtexture || !m3d->texture) {
return; return;
}
mScene->mTextures = new aiTexture *[m3d->numtexture]; mScene->mTextures = new aiTexture *[m3d->numtexture];
for (i = 0; i < m3d->numtexture; i++) { for (i = 0; i < m3d->numtexture; i++) {
@ -391,8 +396,9 @@ void M3DImporter::importMeshes(const M3DWrapper &m3d) {
ASSIMP_LOG_DEBUG_F("M3D: importMeshes ", m3d->numface); ASSIMP_LOG_DEBUG_F("M3D: importMeshes ", m3d->numface);
if (!m3d->numface || !m3d->face || !m3d->numvertex || !m3d->vertex) if (!m3d->numface || !m3d->face || !m3d->numvertex || !m3d->vertex) {
return; return;
}
for (i = 0; i < m3d->numface; i++) { for (i = 0; i < m3d->numface; i++) {
// we must switch mesh if material changes // we must switch mesh if material changes
@ -476,12 +482,12 @@ void M3DImporter::importMeshes(const M3DWrapper &m3d) {
} }
delete meshes; delete meshes;
if (faces) delete faces; delete faces;
if (vertices) delete vertices; delete vertices;
if (normals) delete normals; delete normals;
if (texcoords) delete texcoords; delete texcoords;
if (colors) delete colors; delete colors;
if (vertexids) delete vertexids; delete vertexids;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -495,11 +501,15 @@ void M3DImporter::importBones(const M3DWrapper &m3d, unsigned int parentid, aiNo
ASSIMP_LOG_DEBUG_F("M3D: importBones ", m3d->numbone, " parentid ", (int)parentid); ASSIMP_LOG_DEBUG_F("M3D: importBones ", m3d->numbone, " parentid ", (int)parentid);
if (!m3d->numbone || !m3d->bone) if (!m3d->numbone || !m3d->bone) {
return; return;
}
for (n = 0, i = parentid + 1; i < m3d->numbone; i++) for (n = 0, i = parentid + 1; i < m3d->numbone; i++) {
if (m3d->bone[i].parent == parentid) n++; if (m3d->bone[i].parent == parentid) {
n++;
}
}
pParent->mChildren = new aiNode *[n]; pParent->mChildren = new aiNode *[n];
for (i = parentid + 1; i < m3d->numbone; i++) { for (i = parentid + 1; i < m3d->numbone; i++) {
@ -532,8 +542,9 @@ void M3DImporter::importAnimations(const M3DWrapper &m3d) {
ASSIMP_LOG_DEBUG_F("M3D: importAnimations ", mScene->mNumAnimations); ASSIMP_LOG_DEBUG_F("M3D: importAnimations ", mScene->mNumAnimations);
if (!m3d->numaction || !m3d->action || !m3d->numbone || !m3d->bone || !m3d->vertex) if (!m3d->numaction || !m3d->action || !m3d->numbone || !m3d->bone || !m3d->vertex) {
return; return;
}
mScene->mAnimations = new aiAnimation *[m3d->numaction]; mScene->mAnimations = new aiAnimation *[m3d->numaction];
for (i = 0; i < m3d->numaction; i++) { for (i = 0; i < m3d->numaction; i++) {
@ -643,16 +654,17 @@ void M3DImporter::convertPose(const M3DWrapper &m3d, aiMatrix4x4 *m, unsigned in
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// find a node by name // find a node by name
aiNode *M3DImporter::findNode(aiNode *pNode, aiString name) { aiNode *M3DImporter::findNode(aiNode *pNode, aiString name) {
unsigned int i;
ai_assert(pNode != nullptr); ai_assert(pNode != nullptr);
ai_assert(mScene != nullptr); ai_assert(mScene != nullptr);
if (pNode->mName == name) if (pNode->mName == name) {
return pNode; return pNode;
for (i = 0; i < pNode->mNumChildren; i++) { }
for ( unsigned int i = 0; i < pNode->mNumChildren; i++) {
aiNode *pChild = findNode(pNode->mChildren[i], name); aiNode *pChild = findNode(pNode->mChildren[i], name);
if (pChild) return pChild; if (pChild) {
return pChild;
}
} }
return nullptr; return nullptr;
} }
@ -714,7 +726,8 @@ void M3DImporter::populateMesh(const M3DWrapper &m3d, aiMesh *pMesh, std::vector
// this is complicated, because M3D stores a list of bone id / weight pairs per // this is complicated, because M3D stores a list of bone id / weight pairs per
// vertex but assimp uses lists of local vertex id/weight pairs per local bone list // vertex but assimp uses lists of local vertex id/weight pairs per local bone list
pMesh->mNumBones = m3d->numbone; pMesh->mNumBones = m3d->numbone;
/* we need aiBone with mOffsetMatrix for bones without weights as well */
// we need aiBone with mOffsetMatrix for bones without weights as well
if (pMesh->mNumBones && m3d->numbone && m3d->bone) { if (pMesh->mNumBones && m3d->numbone && m3d->bone) {
pMesh->mBones = new aiBone *[pMesh->mNumBones]; pMesh->mBones = new aiBone *[pMesh->mNumBones];
for (unsigned int i = 0; i < m3d->numbone; i++) { for (unsigned int i = 0; i < m3d->numbone; i++) {
@ -730,15 +743,16 @@ void M3DImporter::populateMesh(const M3DWrapper &m3d, aiMesh *pMesh, std::vector
pMesh->mBones[i]->mOffsetMatrix = aiMatrix4x4(); pMesh->mBones[i]->mOffsetMatrix = aiMatrix4x4();
} }
if (vertexids->size() && m3d->numvertex && m3d->vertex && m3d->numskin && m3d->skin) { if (vertexids->size() && m3d->numvertex && m3d->vertex && m3d->numskin && m3d->skin) {
unsigned int i, j;
// first count how many vertices we have per bone // first count how many vertices we have per bone
for (i = 0; i < vertexids->size(); i++) { for (unsigned int i = 0; i < vertexids->size(); i++) {
if(vertexids->at(i) >= m3d->numvertex) continue; if (vertexids->at(i) >= m3d->numvertex) {
continue;
}
unsigned int s = m3d->vertex[vertexids->at(i)].skinid; unsigned int s = m3d->vertex[vertexids->at(i)].skinid;
if (s != M3D_UNDEF && s != M3D_INDEXMAX) { if (s != M3D_UNDEF && s != M3D_INDEXMAX) {
for (unsigned int k = 0; k < M3D_NUMBONE && m3d->skin[s].weight[k] > 0.0; k++) { for (unsigned int k = 0; k < M3D_NUMBONE && m3d->skin[s].weight[k] > 0.0; k++) {
aiString name = aiString(std::string(m3d->bone[m3d->skin[s].boneid[k]].name)); aiString name = aiString(std::string(m3d->bone[m3d->skin[s].boneid[k]].name));
for (j = 0; j < pMesh->mNumBones; j++) { for (unsigned int j = 0; j < pMesh->mNumBones; j++) {
if (pMesh->mBones[j]->mName == name) { if (pMesh->mBones[j]->mName == name) {
pMesh->mBones[j]->mNumWeights++; pMesh->mBones[j]->mNumWeights++;
break; break;
@ -747,6 +761,7 @@ void M3DImporter::populateMesh(const M3DWrapper &m3d, aiMesh *pMesh, std::vector
} }
} }
} }
// allocate mWeights // allocate mWeights
for (j = 0; j < pMesh->mNumBones; j++) { for (j = 0; j < pMesh->mNumBones; j++) {
aiBone *pBone = pMesh->mBones[j]; aiBone *pBone = pMesh->mBones[j];