nuclear@0: /* nuclear@0: --------------------------------------------------------------------------- nuclear@0: Open Asset Import Library (assimp) nuclear@0: --------------------------------------------------------------------------- nuclear@0: nuclear@0: Copyright (c) 2006-2012, assimp team nuclear@0: nuclear@0: All rights reserved. nuclear@0: nuclear@0: Redistribution and use of this software in source and binary forms, nuclear@0: with or without modification, are permitted provided that the following nuclear@0: conditions are met: nuclear@0: nuclear@0: * Redistributions of source code must retain the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer. nuclear@0: nuclear@0: * Redistributions in binary form must reproduce the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer in the documentation and/or other nuclear@0: materials provided with the distribution. nuclear@0: nuclear@0: * Neither the name of the assimp team, nor the names of its nuclear@0: contributors may be used to endorse or promote products nuclear@0: derived from this software without specific prior nuclear@0: written permission of the assimp team. nuclear@0: nuclear@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS nuclear@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT nuclear@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR nuclear@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT nuclear@0: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, nuclear@0: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT nuclear@0: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, nuclear@0: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY nuclear@0: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT nuclear@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE nuclear@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. nuclear@0: --------------------------------------------------------------------------- nuclear@0: */ nuclear@0: nuclear@0: /** @file MS3DLoader.cpp nuclear@0: * @brief Implementation of the Ms3D importer class. nuclear@0: * Written against http://chumbalum.swissquake.ch/ms3d/ms3dspec.txt nuclear@0: */ nuclear@0: nuclear@0: #include "AssimpPCH.h" nuclear@0: #ifndef ASSIMP_BUILD_NO_MS3D_IMPORTER nuclear@0: nuclear@0: // internal headers nuclear@0: #include "MS3DLoader.h" nuclear@0: #include "StreamReader.h" nuclear@0: using namespace Assimp; nuclear@0: nuclear@0: static const aiImporterDesc desc = { nuclear@0: "Milkshape 3D Importer", nuclear@0: "", nuclear@0: "", nuclear@0: "http://chumbalum.swissquake.ch/", nuclear@0: aiImporterFlags_SupportBinaryFlavour, nuclear@0: 0, nuclear@0: 0, nuclear@0: 0, nuclear@0: 0, nuclear@0: "ms3d" nuclear@0: }; nuclear@0: nuclear@0: // ASSIMP_BUILD_MS3D_ONE_NODE_PER_MESH nuclear@0: // (enable old code path, which generates extra nodes per mesh while nuclear@0: // the newer code uses aiMesh::mName to express the name of the nuclear@0: // meshes (a.k.a. groups in MS3D)) nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Constructor to be privately used by Importer nuclear@0: MS3DImporter::MS3DImporter() nuclear@0: {} nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Destructor, private as well nuclear@0: MS3DImporter::~MS3DImporter() nuclear@0: {} nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Returns whether the class can handle the format of the given file. nuclear@0: bool MS3DImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const nuclear@0: { nuclear@0: // first call - simple extension check nuclear@0: const std::string extension = GetExtension(pFile); nuclear@0: if (extension == "ms3d") { nuclear@0: return true; nuclear@0: } nuclear@0: nuclear@0: // second call - check for magic identifiers nuclear@0: else if (!extension.length() || checkSig) { nuclear@0: if (!pIOHandler) { nuclear@0: return true; nuclear@0: } nuclear@0: const char* tokens[] = {"MS3D000000"}; nuclear@0: return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1); nuclear@0: } nuclear@0: return false; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: const aiImporterDesc* MS3DImporter::GetInfo () const nuclear@0: { nuclear@0: return &desc; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: void ReadColor(StreamReaderLE& stream, aiColor4D& ambient) nuclear@0: { nuclear@0: // aiColor4D is packed on gcc, implicit binding to float& fails therefore. nuclear@0: stream >> (float&)ambient.r >> (float&)ambient.g >> (float&)ambient.b >> (float&)ambient.a; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: void ReadVector(StreamReaderLE& stream, aiVector3D& pos) nuclear@0: { nuclear@0: // See note in ReadColor() nuclear@0: stream >> (float&)pos.x >> (float&)pos.y >> (float&)pos.z; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: template nuclear@0: void MS3DImporter :: ReadComments(StreamReaderLE& stream, std::vector& outp) nuclear@0: { nuclear@0: uint16_t cnt; nuclear@0: stream >> cnt; nuclear@0: nuclear@0: for(unsigned int i = 0; i < cnt; ++i) { nuclear@0: uint32_t index, clength; nuclear@0: stream >> index >> clength; nuclear@0: nuclear@0: if(index >= outp.size()) { nuclear@0: DefaultLogger::get()->warn("MS3D: Invalid index in comment section"); nuclear@0: } nuclear@0: else if (clength > stream.GetRemainingSize()) { nuclear@0: throw DeadlyImportError("MS3D: Failure reading comment, length field is out of range"); nuclear@0: } nuclear@0: else { nuclear@0: outp[index].comment = std::string(reinterpret_cast(stream.GetPtr()),clength); nuclear@0: } nuclear@0: stream.IncPtr(clength); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: template bool inrange(const T& in, const T2& lower, const T3& higher) nuclear@0: { nuclear@0: return in > lower && in <= higher; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: void MS3DImporter :: CollectChildJoints(const std::vector& joints, nuclear@0: std::vector& hadit, nuclear@0: aiNode* nd, nuclear@0: const aiMatrix4x4& absTrafo) nuclear@0: { nuclear@0: unsigned int cnt = 0; nuclear@0: for(size_t i = 0; i < joints.size(); ++i) { nuclear@0: if (!hadit[i] && !strcmp(joints[i].parentName,nd->mName.data)) { nuclear@0: ++cnt; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: nd->mChildren = new aiNode*[nd->mNumChildren = cnt]; nuclear@0: cnt = 0; nuclear@0: for(size_t i = 0; i < joints.size(); ++i) { nuclear@0: if (!hadit[i] && !strcmp(joints[i].parentName,nd->mName.data)) { nuclear@0: aiNode* ch = nd->mChildren[cnt++] = new aiNode(joints[i].name); nuclear@0: ch->mParent = nd; nuclear@0: nuclear@0: ch->mTransformation = aiMatrix4x4::Translation(joints[i].position,aiMatrix4x4()=aiMatrix4x4())* nuclear@0: // XXX actually, I don't *know* why we need the inverse here. Probably column vs. row order? nuclear@0: aiMatrix4x4().FromEulerAnglesXYZ(joints[i].rotation).Transpose(); nuclear@0: nuclear@0: const aiMatrix4x4 abs = absTrafo*ch->mTransformation; nuclear@0: for(unsigned int a = 0; a < mScene->mNumMeshes; ++a) { nuclear@0: aiMesh* const msh = mScene->mMeshes[a]; nuclear@0: for(unsigned int n = 0; n < msh->mNumBones; ++n) { nuclear@0: aiBone* const bone = msh->mBones[n]; nuclear@0: nuclear@0: if(bone->mName == ch->mName) { nuclear@0: bone->mOffsetMatrix = aiMatrix4x4(abs).Inverse(); nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: hadit[i] = true; nuclear@0: CollectChildJoints(joints,hadit,ch,abs); nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: void MS3DImporter :: CollectChildJoints(const std::vector& joints, aiNode* nd) nuclear@0: { nuclear@0: std::vector hadit(joints.size(),false); nuclear@0: aiMatrix4x4 trafo; nuclear@0: nuclear@0: CollectChildJoints(joints,hadit,nd,trafo); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Imports the given file into the given scene structure. nuclear@0: void MS3DImporter::InternReadFile( const std::string& pFile, nuclear@0: aiScene* pScene, IOSystem* pIOHandler) nuclear@0: { nuclear@0: StreamReaderLE stream(pIOHandler->Open(pFile,"rb")); nuclear@0: nuclear@0: // CanRead() should have done this already nuclear@0: char head[10]; nuclear@0: int32_t version; nuclear@0: nuclear@0: mScene = pScene; nuclear@0: nuclear@0: nuclear@0: // 1 ------------ read into temporary data structures mirroring the original file nuclear@0: nuclear@0: stream.CopyAndAdvance(head,10); nuclear@0: stream >> version; nuclear@0: if (strncmp(head,"MS3D000000",10)) { nuclear@0: throw DeadlyImportError("Not a MS3D file, magic string MS3D000000 not found: "+pFile); nuclear@0: } nuclear@0: nuclear@0: if (version != 4) { nuclear@0: throw DeadlyImportError("MS3D: Unsupported file format version, 4 was expected"); nuclear@0: } nuclear@0: nuclear@0: uint16_t verts; nuclear@0: stream >> verts; nuclear@0: nuclear@0: std::vector vertices(verts); nuclear@0: for (unsigned int i = 0; i < verts; ++i) { nuclear@0: TempVertex& v = vertices[i]; nuclear@0: nuclear@0: stream.IncPtr(1); nuclear@0: ReadVector(stream,v.pos); nuclear@0: v.bone_id[0] = stream.GetI1(); nuclear@0: v.ref_cnt = stream.GetI1(); nuclear@0: nuclear@0: v.bone_id[1] = v.bone_id[2] = v.bone_id[3] = UINT_MAX; nuclear@0: v.weights[1] = v.weights[2] = v.weights[3] = 0.f; nuclear@0: v.weights[0] = 1.f; nuclear@0: } nuclear@0: nuclear@0: uint16_t tris; nuclear@0: stream >> tris; nuclear@0: nuclear@0: std::vector triangles(tris); nuclear@0: for (unsigned int i = 0;i < tris; ++i) { nuclear@0: TempTriangle& t = triangles[i]; nuclear@0: nuclear@0: stream.IncPtr(2); nuclear@0: for (unsigned int i = 0; i < 3; ++i) { nuclear@0: t.indices[i] = stream.GetI2(); nuclear@0: } nuclear@0: nuclear@0: for (unsigned int i = 0; i < 3; ++i) { nuclear@0: ReadVector(stream,t.normals[i]); nuclear@0: } nuclear@0: nuclear@0: for (unsigned int i = 0; i < 3; ++i) { nuclear@0: stream >> (float&)(t.uv[i].x); // see note in ReadColor() nuclear@0: } nuclear@0: for (unsigned int i = 0; i < 3; ++i) { nuclear@0: stream >> (float&)(t.uv[i].y); nuclear@0: } nuclear@0: nuclear@0: t.sg = stream.GetI1(); nuclear@0: t.group = stream.GetI1(); nuclear@0: } nuclear@0: nuclear@0: uint16_t grp; nuclear@0: stream >> grp; nuclear@0: nuclear@0: bool need_default = false; nuclear@0: std::vector groups(grp); nuclear@0: for (unsigned int i = 0;i < grp; ++i) { nuclear@0: TempGroup& t = groups[i]; nuclear@0: nuclear@0: stream.IncPtr(1); nuclear@0: stream.CopyAndAdvance(t.name,32); nuclear@0: nuclear@0: t.name[32] = '\0'; nuclear@0: uint16_t num; nuclear@0: stream >> num; nuclear@0: nuclear@0: t.triangles.resize(num); nuclear@0: for (unsigned int i = 0; i < num; ++i) { nuclear@0: t.triangles[i] = stream.GetI2(); nuclear@0: } nuclear@0: t.mat = stream.GetI1(); nuclear@0: if (t.mat == UINT_MAX) { nuclear@0: need_default = true; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: uint16_t mat; nuclear@0: stream >> mat; nuclear@0: nuclear@0: std::vector materials(mat); nuclear@0: for (unsigned int i = 0;i < mat; ++i) { nuclear@0: TempMaterial& t = materials[i]; nuclear@0: nuclear@0: stream.CopyAndAdvance(t.name,32); nuclear@0: t.name[32] = '\0'; nuclear@0: nuclear@0: ReadColor(stream,t.ambient); nuclear@0: ReadColor(stream,t.diffuse); nuclear@0: ReadColor(stream,t.specular); nuclear@0: ReadColor(stream,t.emissive); nuclear@0: stream >> t.shininess >> t.transparency; nuclear@0: nuclear@0: stream.IncPtr(1); nuclear@0: nuclear@0: stream.CopyAndAdvance(t.texture,128); nuclear@0: t.texture[128] = '\0'; nuclear@0: nuclear@0: stream.CopyAndAdvance(t.alphamap,128); nuclear@0: t.alphamap[128] = '\0'; nuclear@0: } nuclear@0: nuclear@0: float animfps, currenttime; nuclear@0: uint32_t totalframes; nuclear@0: stream >> animfps >> currenttime >> totalframes; nuclear@0: nuclear@0: uint16_t joint; nuclear@0: stream >> joint; nuclear@0: nuclear@0: std::vector joints(joint); nuclear@0: for(unsigned int i = 0; i < joint; ++i) { nuclear@0: TempJoint& j = joints[i]; nuclear@0: nuclear@0: stream.IncPtr(1); nuclear@0: stream.CopyAndAdvance(j.name,32); nuclear@0: j.name[32] = '\0'; nuclear@0: nuclear@0: stream.CopyAndAdvance(j.parentName,32); nuclear@0: j.parentName[32] = '\0'; nuclear@0: nuclear@0: // DefaultLogger::get()->debug(j.name); nuclear@0: // DefaultLogger::get()->debug(j.parentName); nuclear@0: nuclear@0: ReadVector(stream,j.rotation); nuclear@0: ReadVector(stream,j.position); nuclear@0: nuclear@0: j.rotFrames.resize(stream.GetI2()); nuclear@0: j.posFrames.resize(stream.GetI2()); nuclear@0: nuclear@0: for(unsigned int a = 0; a < j.rotFrames.size(); ++a) { nuclear@0: TempKeyFrame& kf = j.rotFrames[a]; nuclear@0: stream >> kf.time; nuclear@0: ReadVector(stream,kf.value); nuclear@0: } nuclear@0: for(unsigned int a = 0; a < j.posFrames.size(); ++a) { nuclear@0: TempKeyFrame& kf = j.posFrames[a]; nuclear@0: stream >> kf.time; nuclear@0: ReadVector(stream,kf.value); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: if(stream.GetRemainingSize() > 4) { nuclear@0: uint32_t subversion; nuclear@0: stream >> subversion; nuclear@0: if (subversion == 1) { nuclear@0: ReadComments(stream,groups); nuclear@0: ReadComments(stream,materials); nuclear@0: ReadComments(stream,joints); nuclear@0: nuclear@0: // model comment - print it for we have such a nice log. nuclear@0: if (stream.GetI4()) { nuclear@0: const size_t len = static_cast(stream.GetI4()); nuclear@0: if (len > stream.GetRemainingSize()) { nuclear@0: throw DeadlyImportError("MS3D: Model comment is too long"); nuclear@0: } nuclear@0: nuclear@0: const std::string& s = std::string(reinterpret_cast(stream.GetPtr()),len); nuclear@0: DefaultLogger::get()->debug("MS3D: Model comment: " + s); nuclear@0: } nuclear@0: nuclear@0: if(stream.GetRemainingSize() > 4 && inrange((stream >> subversion,subversion),1u,3u)) { nuclear@0: for(unsigned int i = 0; i < verts; ++i) { nuclear@0: TempVertex& v = vertices[i]; nuclear@0: v.weights[3]=1.f; nuclear@0: for(unsigned int n = 0; n < 3; v.weights[3]-=v.weights[n++]) { nuclear@0: v.bone_id[n+1] = stream.GetI1(); nuclear@0: v.weights[n] = static_cast(static_cast(stream.GetI1()))/255.f; nuclear@0: } nuclear@0: stream.IncPtr((subversion-1)<<2u); nuclear@0: } nuclear@0: nuclear@0: // even further extra data is not of interest for us, at least now now. nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // 2 ------------ convert to proper aiXX data structures ----------------------------------- nuclear@0: nuclear@0: if (need_default && materials.size()) { nuclear@0: DefaultLogger::get()->warn("MS3D: Found group with no material assigned, spawning default material"); nuclear@0: // if one of the groups has no material assigned, but there are other nuclear@0: // groups with materials, a default material needs to be added ( nuclear@0: // scenepreprocessor adds a default material only if nummat==0). nuclear@0: materials.push_back(TempMaterial()); nuclear@0: TempMaterial& m = materials.back(); nuclear@0: nuclear@0: strcpy(m.name,""); nuclear@0: m.diffuse = aiColor4D(0.6f,0.6f,0.6f,1.0); nuclear@0: m.transparency = 1.f; nuclear@0: m.shininess = 0.f; nuclear@0: nuclear@0: // this is because these TempXXX struct's have no c'tors. nuclear@0: m.texture[0] = m.alphamap[0] = '\0'; nuclear@0: nuclear@0: for (unsigned int i = 0; i < groups.size(); ++i) { nuclear@0: TempGroup& g = groups[i]; nuclear@0: if (g.mat == UINT_MAX) { nuclear@0: g.mat = materials.size()-1; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // convert materials to our generic key-value dict-alike nuclear@0: if (materials.size()) { nuclear@0: pScene->mMaterials = new aiMaterial*[materials.size()]; nuclear@0: for (size_t i = 0; i < materials.size(); ++i) { nuclear@0: nuclear@0: aiMaterial* mo = new aiMaterial(); nuclear@0: pScene->mMaterials[pScene->mNumMaterials++] = mo; nuclear@0: nuclear@0: const TempMaterial& mi = materials[i]; nuclear@0: nuclear@0: aiString tmp; nuclear@0: if (0[mi.alphamap]) { nuclear@0: tmp = aiString(mi.alphamap); nuclear@0: mo->AddProperty(&tmp,AI_MATKEY_TEXTURE_OPACITY(0)); nuclear@0: } nuclear@0: if (0[mi.texture]) { nuclear@0: tmp = aiString(mi.texture); nuclear@0: mo->AddProperty(&tmp,AI_MATKEY_TEXTURE_DIFFUSE(0)); nuclear@0: } nuclear@0: if (0[mi.name]) { nuclear@0: tmp = aiString(mi.name); nuclear@0: mo->AddProperty(&tmp,AI_MATKEY_NAME); nuclear@0: } nuclear@0: nuclear@0: mo->AddProperty(&mi.ambient,1,AI_MATKEY_COLOR_AMBIENT); nuclear@0: mo->AddProperty(&mi.diffuse,1,AI_MATKEY_COLOR_DIFFUSE); nuclear@0: mo->AddProperty(&mi.specular,1,AI_MATKEY_COLOR_SPECULAR); nuclear@0: mo->AddProperty(&mi.emissive,1,AI_MATKEY_COLOR_EMISSIVE); nuclear@0: nuclear@0: mo->AddProperty(&mi.shininess,1,AI_MATKEY_SHININESS); nuclear@0: mo->AddProperty(&mi.transparency,1,AI_MATKEY_OPACITY); nuclear@0: nuclear@0: const int sm = mi.shininess>0.f?aiShadingMode_Phong:aiShadingMode_Gouraud; nuclear@0: mo->AddProperty(&sm,1,AI_MATKEY_SHADING_MODEL); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // convert groups to meshes nuclear@0: if (groups.empty()) { nuclear@0: throw DeadlyImportError("MS3D: Didn't get any group records, file is malformed"); nuclear@0: } nuclear@0: nuclear@0: pScene->mMeshes = new aiMesh*[pScene->mNumMeshes=static_cast(groups.size())](); nuclear@0: for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) { nuclear@0: nuclear@0: aiMesh* m = pScene->mMeshes[i] = new aiMesh(); nuclear@0: const TempGroup& g = groups[i]; nuclear@0: nuclear@0: if (pScene->mNumMaterials && g.mat > pScene->mNumMaterials) { nuclear@0: throw DeadlyImportError("MS3D: Encountered invalid material index, file is malformed"); nuclear@0: } // no error if no materials at all - scenepreprocessor adds one then nuclear@0: nuclear@0: m->mMaterialIndex = g.mat; nuclear@0: m->mPrimitiveTypes = aiPrimitiveType_TRIANGLE; nuclear@0: nuclear@0: m->mFaces = new aiFace[m->mNumFaces = g.triangles.size()]; nuclear@0: m->mNumVertices = m->mNumFaces*3; nuclear@0: nuclear@0: // storage for vertices - verbose format, as requested by the postprocessing pipeline nuclear@0: m->mVertices = new aiVector3D[m->mNumVertices]; nuclear@0: m->mNormals = new aiVector3D[m->mNumVertices]; nuclear@0: m->mTextureCoords[0] = new aiVector3D[m->mNumVertices]; nuclear@0: m->mNumUVComponents[0] = 2; nuclear@0: nuclear@0: typedef std::map BoneSet; nuclear@0: BoneSet mybones; nuclear@0: nuclear@0: for (unsigned int i = 0,n = 0; i < m->mNumFaces; ++i) { nuclear@0: aiFace& f = m->mFaces[i]; nuclear@0: if (g.triangles[i]>triangles.size()) { nuclear@0: throw DeadlyImportError("MS3D: Encountered invalid triangle index, file is malformed"); nuclear@0: } nuclear@0: nuclear@0: TempTriangle& t = triangles[g.triangles[i]]; nuclear@0: f.mIndices = new unsigned int[f.mNumIndices=3]; nuclear@0: nuclear@0: for (unsigned int i = 0; i < 3; ++i,++n) { nuclear@0: if (t.indices[i]>vertices.size()) { nuclear@0: throw DeadlyImportError("MS3D: Encountered invalid vertex index, file is malformed"); nuclear@0: } nuclear@0: nuclear@0: const TempVertex& v = vertices[t.indices[i]]; nuclear@0: for(unsigned int a = 0; a < 4; ++a) { nuclear@0: if (v.bone_id[a] != UINT_MAX) { nuclear@0: if (v.bone_id[a] >= joints.size()) { nuclear@0: throw DeadlyImportError("MS3D: Encountered invalid bone index, file is malformed"); nuclear@0: } nuclear@0: if (mybones.find(v.bone_id[a]) == mybones.end()) { nuclear@0: mybones[v.bone_id[a]] = 1; nuclear@0: } nuclear@0: else ++mybones[v.bone_id[a]]; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // collect vertex components nuclear@0: m->mVertices[n] = v.pos; nuclear@0: nuclear@0: m->mNormals[n] = t.normals[i]; nuclear@0: m->mTextureCoords[0][n] = aiVector3D(t.uv[i].x,1.f-t.uv[i].y,0.0); nuclear@0: f.mIndices[i] = n; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // allocate storage for bones nuclear@0: if(mybones.size()) { nuclear@0: std::vector bmap(joints.size()); nuclear@0: m->mBones = new aiBone*[mybones.size()](); nuclear@0: for(BoneSet::const_iterator it = mybones.begin(); it != mybones.end(); ++it) { nuclear@0: aiBone* const bn = m->mBones[m->mNumBones] = new aiBone(); nuclear@0: const TempJoint& jnt = joints[(*it).first]; nuclear@0: nuclear@0: bn->mName.Set(jnt.name); nuclear@0: bn->mWeights = new aiVertexWeight[(*it).second]; nuclear@0: nuclear@0: bmap[(*it).first] = m->mNumBones++; nuclear@0: } nuclear@0: nuclear@0: // .. and collect bone weights nuclear@0: for (unsigned int i = 0,n = 0; i < m->mNumFaces; ++i) { nuclear@0: TempTriangle& t = triangles[g.triangles[i]]; nuclear@0: nuclear@0: for (unsigned int i = 0; i < 3; ++i,++n) { nuclear@0: const TempVertex& v = vertices[t.indices[i]]; nuclear@0: for(unsigned int a = 0; a < 4; ++a) { nuclear@0: const unsigned int bone = v.bone_id[a]; nuclear@0: if(bone==UINT_MAX){ nuclear@0: continue; nuclear@0: } nuclear@0: nuclear@0: aiBone* const outbone = m->mBones[bmap[bone]]; nuclear@0: aiVertexWeight& outwght = outbone->mWeights[outbone->mNumWeights++]; nuclear@0: nuclear@0: outwght.mVertexId = n; nuclear@0: outwght.mWeight = v.weights[a]; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // ... add dummy nodes under a single root, each holding a reference to one nuclear@0: // mesh. If we didn't do this, we'd loose the group name. nuclear@0: aiNode* rt = pScene->mRootNode = new aiNode(""); nuclear@0: nuclear@0: #ifdef ASSIMP_BUILD_MS3D_ONE_NODE_PER_MESH nuclear@0: rt->mChildren = new aiNode*[rt->mNumChildren=pScene->mNumMeshes+(joints.size()?1:0)](); nuclear@0: nuclear@0: for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) { nuclear@0: aiNode* nd = rt->mChildren[i] = new aiNode(); nuclear@0: nuclear@0: const TempGroup& g = groups[i]; nuclear@0: nuclear@0: // we need to generate an unique name for all mesh nodes. nuclear@0: // since we want to keep the group name, a prefix is nuclear@0: // prepended. nuclear@0: nd->mName = aiString("_"); nuclear@0: nd->mName.Append(g.name); nuclear@0: nd->mParent = rt; nuclear@0: nuclear@0: nd->mMeshes = new unsigned int[nd->mNumMeshes = 1]; nuclear@0: nd->mMeshes[0] = i; nuclear@0: } nuclear@0: #else nuclear@0: rt->mMeshes = new unsigned int[pScene->mNumMeshes]; nuclear@0: for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) { nuclear@0: rt->mMeshes[rt->mNumMeshes++] = i; nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: // convert animations as well nuclear@0: if(joints.size()) { nuclear@0: #ifndef ASSIMP_BUILD_MS3D_ONE_NODE_PER_MESH nuclear@0: rt->mChildren = new aiNode*[1](); nuclear@0: rt->mNumChildren = 1; nuclear@0: nuclear@0: aiNode* jt = rt->mChildren[0] = new aiNode(); nuclear@0: #else nuclear@0: aiNode* jt = rt->mChildren[pScene->mNumMeshes] = new aiNode(); nuclear@0: #endif nuclear@0: jt->mParent = rt; nuclear@0: CollectChildJoints(joints,jt); nuclear@0: jt->mName.Set(""); nuclear@0: nuclear@0: pScene->mAnimations = new aiAnimation*[ pScene->mNumAnimations = 1 ]; nuclear@0: aiAnimation* const anim = pScene->mAnimations[0] = new aiAnimation(); nuclear@0: nuclear@0: anim->mName.Set(""); nuclear@0: nuclear@0: // carry the fps info to the user by scaling all times with it nuclear@0: anim->mTicksPerSecond = animfps; nuclear@0: nuclear@0: // leave duration at its default, so ScenePreprocessor will fill an appropriate nuclear@0: // value (the values taken from some MS3D files seem to be too unreliable nuclear@0: // to pass the validation) nuclear@0: // anim->mDuration = totalframes/animfps; nuclear@0: nuclear@0: anim->mChannels = new aiNodeAnim*[joints.size()](); nuclear@0: for(std::vector::const_iterator it = joints.begin(); it != joints.end(); ++it) { nuclear@0: if ((*it).rotFrames.empty() && (*it).posFrames.empty()) { nuclear@0: continue; nuclear@0: } nuclear@0: nuclear@0: aiNodeAnim* nd = anim->mChannels[anim->mNumChannels++] = new aiNodeAnim(); nuclear@0: nd->mNodeName.Set((*it).name); nuclear@0: nuclear@0: if ((*it).rotFrames.size()) { nuclear@0: nd->mRotationKeys = new aiQuatKey[(*it).rotFrames.size()]; nuclear@0: for(std::vector::const_iterator rot = (*it).rotFrames.begin(); rot != (*it).rotFrames.end(); ++rot) { nuclear@0: aiQuatKey& q = nd->mRotationKeys[nd->mNumRotationKeys++]; nuclear@0: nuclear@0: q.mTime = (*rot).time*animfps; nuclear@0: nuclear@0: // XXX it seems our matrix&quaternion code has faults in its conversion routines -- nuclear@0: // aiQuaternion(x,y,z) seems to besomething different as quat(matrix.fromeuler(x,y,z)). nuclear@0: q.mValue = aiQuaternion(aiMatrix3x3(aiMatrix4x4().FromEulerAnglesXYZ((*rot).value)* nuclear@0: aiMatrix4x4().FromEulerAnglesXYZ((*it).rotation)).Transpose()); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: if ((*it).posFrames.size()) { nuclear@0: nd->mPositionKeys = new aiVectorKey[(*it).posFrames.size()]; nuclear@0: nuclear@0: aiQuatKey* qu = nd->mRotationKeys; nuclear@0: for(std::vector::const_iterator pos = (*it).posFrames.begin(); pos != (*it).posFrames.end(); ++pos,++qu) { nuclear@0: aiVectorKey& v = nd->mPositionKeys[nd->mNumPositionKeys++]; nuclear@0: nuclear@0: v.mTime = (*pos).time*animfps; nuclear@0: v.mValue = (*it).position + (*pos).value; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: // fixup to pass the validation if not a single animation channel is non-trivial nuclear@0: if (!anim->mNumChannels) { nuclear@0: anim->mChannels = NULL; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: #endif