Fixed what kimkulling broke

pull/2973/head
bzt 2020-01-31 14:20:23 +01:00
parent a2ef0b5bd5
commit e434c63c31
1 changed files with 70 additions and 67 deletions

View File

@ -111,7 +111,7 @@ using namespace std;
M3DImporter::M3DImporter() :
mScene(nullptr) {
// empty
}
}
// ------------------------------------------------------------------------------------------------
// Returns true, if file is a binary or ASCII Model 3D file.
@ -128,7 +128,13 @@ bool M3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool c
if (!pIOHandler) {
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"));
unsigned char data[4];
if (4 != pStream->Read(data, 1, 4)) {
@ -140,7 +146,6 @@ bool M3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool c
#endif
;
}
return false;
}
@ -254,21 +259,19 @@ void M3DImporter::importMaterials(const M3DWrapper &m3d) {
aiMaterial *mat = new aiMaterial;
name.Set(std::string(m->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
// 0 - 127 scalar values,
// 128 - 255 the same properties but for texture maps
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 ||
m->prop[j].type == m3d_propertytypes[l].id + 128) {
k = l;
break;
}
// should never happen, but be safe than sorry
if (k == 256) {
continue;
}
if (k == 256) continue;
// scalar properties
if (m->prop[j].type < 128 && aiProps[k].pKey) {
@ -292,7 +295,6 @@ void M3DImporter::importMaterials(const M3DWrapper &m3d) {
}
mat->AddProperty(&n, 1, aiProps[k].pKey, aiProps[k].type, aiProps[k].index);
break;
}
}
}
// texture map properties
@ -314,11 +316,12 @@ void M3DImporter::importMaterials(const M3DWrapper &m3d) {
// import textures, this is the simplest of all
void M3DImporter::importTextures(const M3DWrapper &m3d) {
unsigned int i;
static const char *formatHint[] = {
const char *formatHint[] = {
"rgba0800",
"rgba0808",
"rgba8880",
"rgba8888" };
"rgba8888"
};
m3dtx_t *t;
ai_assert(mScene != nullptr);
@ -482,12 +485,12 @@ void M3DImporter::importMeshes(const M3DWrapper &m3d) {
}
delete meshes;
delete faces;
delete vertices;
delete normals;
delete texcoords;
delete colors;
delete vertexids;
if (faces) delete faces;
if (vertices) delete vertices;
if (normals) delete normals;
if (texcoords) delete texcoords;
if (colors) delete colors;
if (vertexids) delete vertexids;
}
// ------------------------------------------------------------------------------------------------
@ -660,7 +663,8 @@ aiNode *M3DImporter::findNode(aiNode *pNode, aiString name) {
if (pNode->mName == name) {
return pNode;
}
for ( unsigned int i = 0; i < pNode->mNumChildren; i++) {
for (unsigned int i = 0; i < pNode->mNumChildren; i++) {
aiNode *pChild = findNode(pNode->mChildren[i], name);
if (pChild) {
return pChild;
@ -726,7 +730,6 @@ void M3DImporter::populateMesh(const M3DWrapper &m3d, aiMesh *pMesh, std::vector
// 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
pMesh->mNumBones = m3d->numbone;
// we need aiBone with mOffsetMatrix for bones without weights as well
if (pMesh->mNumBones && m3d->numbone && m3d->bone) {
pMesh->mBones = new aiBone *[pMesh->mNumBones];
@ -743,16 +746,17 @@ void M3DImporter::populateMesh(const M3DWrapper &m3d, aiMesh *pMesh, std::vector
pMesh->mBones[i]->mOffsetMatrix = aiMatrix4x4();
}
if (vertexids->size() && m3d->numvertex && m3d->vertex && m3d->numskin && m3d->skin) {
unsigned int i, j;
// first count how many vertices we have per bone
for (unsigned int i = 0; i < vertexids->size(); i++) {
if (vertexids->at(i) >= m3d->numvertex) {
for (i = 0; i < vertexids->size(); i++) {
if(vertexids->at(i) >= m3d->numvertex) {
continue;
}
unsigned int s = m3d->vertex[vertexids->at(i)].skinid;
if (s != M3D_UNDEF && s != M3D_INDEXMAX) {
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));
for (unsigned int j = 0; j < pMesh->mNumBones; j++) {
for (j = 0; j < pMesh->mNumBones; j++) {
if (pMesh->mBones[j]->mName == name) {
pMesh->mBones[j]->mNumWeights++;
break;
@ -761,7 +765,6 @@ void M3DImporter::populateMesh(const M3DWrapper &m3d, aiMesh *pMesh, std::vector
}
}
}
// allocate mWeights
for (j = 0; j < pMesh->mNumBones; j++) {
aiBone *pBone = pMesh->mBones[j];