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

@ -110,8 +110,8 @@ using namespace std;
// Default constructor // Default constructor
M3DImporter::M3DImporter() : M3DImporter::M3DImporter() :
mScene(nullptr) { mScene(nullptr) {
// empty // 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.
@ -128,8 +128,14 @@ 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
std::unique_ptr<IOStream> pStream(pIOHandler->Open(pFile, "rb")); 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]; unsigned char data[4];
if (4 != pStream->Read(data, 1, 4)) { if (4 != pStream->Read(data, 1, 4)) {
return false; return false;
@ -140,8 +146,7 @@ bool M3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool c
#endif #endif
; ;
} }
return false;
return false;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -247,53 +252,50 @@ void M3DImporter::importMaterials(const M3DWrapper &m3d) {
if (!m3d->nummaterial || !m3d->material) { if (!m3d->nummaterial || !m3d->material) {
return; return;
} }
for (i = 0; i < m3d->nummaterial; i++) { for (i = 0; i < m3d->nummaterial; i++) {
m = &m3d->material[i]; m = &m3d->material[i];
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) { if (k == 256) continue;
continue;
}
// scalar properties // scalar properties
if (m->prop[j].type < 128 && aiProps[k].pKey) { if (m->prop[j].type < 128 && aiProps[k].pKey) {
switch (m3d_propertytypes[k].format) { switch (m3d_propertytypes[k].format) {
case m3dpf_color: case m3dpf_color:
c = mkColor(m->prop[j].value.color); c = mkColor(m->prop[j].value.color);
mat->AddProperty(&c, 1, aiProps[k].pKey, aiProps[k].type, aiProps[k].index); mat->AddProperty(&c, 1, aiProps[k].pKey, aiProps[k].type, aiProps[k].index);
break; break;
case m3dpf_float: case m3dpf_float:
f = m->prop[j].value.fnum; f = m->prop[j].value.fnum;
mat->AddProperty(&f, 1, aiProps[k].pKey, aiProps[k].type, aiProps[k].index); mat->AddProperty(&f, 1, aiProps[k].pKey, aiProps[k].type, aiProps[k].index);
break; break;
default: default:
n = m->prop[j].value.num; n = m->prop[j].value.num;
if (m->prop[j].type == m3dp_il) { if (m->prop[j].type == m3dp_il) {
switch (n) { switch (n) {
case 0: n = aiShadingMode_NoShading; break; case 0: n = aiShadingMode_NoShading; break;
case 2: n = aiShadingMode_Phong; break; case 2: n = aiShadingMode_Phong; break;
default: n = aiShadingMode_Gouraud; break; default: n = aiShadingMode_Gouraud; break;
} }
} }
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
if (m->prop[j].type >= 128 && aiTxProps[k].pKey && if (m->prop[j].type >= 128 && aiTxProps[k].pKey &&
@ -314,11 +316,12 @@ 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;
static const char *formatHint[] = { const char *formatHint[] = {
"rgba0800", "rgba0800",
"rgba0808", "rgba0808",
"rgba8880", "rgba8880",
"rgba8888" }; "rgba8888"
};
m3dtx_t *t; m3dtx_t *t;
ai_assert(mScene != nullptr); ai_assert(mScene != nullptr);
@ -329,7 +332,7 @@ void M3DImporter::importTextures(const M3DWrapper &m3d) {
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++) {
@ -398,7 +401,7 @@ void M3DImporter::importMeshes(const M3DWrapper &m3d) {
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
@ -482,12 +485,12 @@ void M3DImporter::importMeshes(const M3DWrapper &m3d) {
} }
delete meshes; delete meshes;
delete faces; if (faces) delete faces;
delete vertices; if (vertices) delete vertices;
delete normals; if (normals) delete normals;
delete texcoords; if (texcoords) delete texcoords;
delete colors; if (colors) delete colors;
delete vertexids; if (vertexids) delete vertexids;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -503,13 +506,13 @@ void M3DImporter::importBones(const M3DWrapper &m3d, unsigned int parentid, aiNo
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) { if (m3d->bone[i].parent == parentid) {
n++; 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++) {
@ -544,7 +547,7 @@ void M3DImporter::importAnimations(const M3DWrapper &m3d) {
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++) {
@ -659,12 +662,13 @@ aiNode *M3DImporter::findNode(aiNode *pNode, aiString name) {
if (pNode->mName == name) { if (pNode->mName == name) {
return pNode; 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); aiNode *pChild = findNode(pNode->mChildren[i], name);
if (pChild) { if (pChild) {
return pChild; return pChild;
} }
} }
return nullptr; return nullptr;
} }
@ -726,8 +730,7 @@ 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++) {
@ -743,16 +746,17 @@ 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 (unsigned int i = 0; i < vertexids->size(); i++) { for (i = 0; i < vertexids->size(); i++) {
if (vertexids->at(i) >= m3d->numvertex) { if(vertexids->at(i) >= m3d->numvertex) {
continue; 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 (unsigned int j = 0; j < pMesh->mNumBones; j++) { for (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;
@ -761,8 +765,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];
if (pBone->mNumWeights) { if (pBone->mNumWeights) {