#include "stdafx.h"
#include "DaeLoader.h"
-#include <LibQtGeoViewerCore/SceneMain.h>
-
-#include "../FileUtil.h"
+#include "AssimpReader.h"
-#include <assimp/Importer.hpp>
-#include <assimp/scene.h>
-#include <assimp/postprocess.h>
-
-#include "AssimpUtil.h"
+#include <LibQtGeoViewerCore/SceneMain.h>
-void CreateBoneTree(BoneMap& bone_map, Bone* b, aiNode* n)
+GeomObject* DaeLoader::LoadGeom(SceneMain& scene, const std::string& filename)
{
- ae::ConvertMat(b->m_NodeTrans, n->mTransformation);
-
- for (int i = 0; i < n->mNumChildren; ++i)
- {
- aiNode* nc = n->mChildren[i];
- Bone* bc = bone_map.Bones[nc->mName.C_Str()];
- b->m_Children.push_back(bc);
- CreateBoneTree(bone_map, bc, nc);
- }
+ AssimpReader reader(geom::GeomFileFormat::Collada);
+ return reader.LoadGeom(scene, filename);
}
-
-bool DaeLoader::Load(SceneMain& scene_main, const std::string& filename)
+bool DaeLoader::Load(SceneMain& scene, const std::string& filename)
{
- Assimp::Importer importer;
- const aiScene* scene = importer.ReadFile(filename, 0);
- if(!scene)
+ GeomObject* geom = LoadGeom(scene, filename);
+ if (geom == NULL)
return false;
- BoneMap bone_map;
-
- bool HasAnimation = (scene->mNumAnimations > 0);
-
- for (int i = 0; i < scene->mNumMeshes; ++i)
- {
- aiMesh* mesh = scene->mMeshes[i];
- GeomObject* geom = scene_main.CreateNewGeometry();
- MeshBuf* mbuf = geom->CreateNewMeshBuf();
-
- lib_geo::BaseMesh& mesh_dst = mbuf->m_Mesh;
-
- mesh_dst.m_Verts.resize(mesh->mNumVertices);
- for (int j = 0; j < mesh->mNumVertices; ++j)
- {
- const aiVector3D& v = mesh->mVertices[j];
- mesh_dst.m_Verts[j].set(v.x, v.y, v.z);
- }
-
- mesh_dst.m_Faces.resize(mesh->mNumFaces);
- for (int j = 0; j < mesh->mNumFaces; ++j)
- {
- const aiFace& sf = mesh->mFaces[j];
- lib_geo::BaseFace& df = mesh_dst.m_Faces[j];
-
- df.m_VertIds.resize(sf.mNumIndices);
- for(int k = 0; k < sf.mNumIndices; ++k)
- {
- df.m_VertIds[k] = sf.mIndices[k];
- }
- }
-
- geom->m_BoneAnimation.m_Bones.resize(mesh->mNumBones);
- for (int j = 0; j < mesh->mNumBones; ++j)
- {
- const aiBone* s_bone = mesh->mBones[j];
- Bone& d_bone = geom->m_BoneAnimation.m_Bones[j];
- d_bone.m_Name = s_bone->mName.C_Str();
-
- d_bone.m_Weights.resize(s_bone->mNumWeights);
- for (int k = 0; k < s_bone->mNumWeights; ++k)
- {
- BoneWeight& bw = d_bone.m_Weights[k];
- aiVertexWeight& vw = s_bone->mWeights[k];
- bw.m_Vid = vw.mVertexId;
- bw.m_Weight = vw.mWeight;
- }
-
- ae::ConvertMat(d_bone.m_Offset, s_bone->mOffsetMatrix);
-
- bone_map.Bones[d_bone.m_Name] = &d_bone;
- }
-
- mesh_dst.CreateNormalsEachVerts();
- mesh_dst.UpdateNormal();
-
- geom->m_Name = mesh->mName.C_Str();
- if (geom->m_Name.empty())
- {
- std::ostringstream oss;
- oss << FileUtil::GetFileTitle(filename) << "-" << i;
- geom->m_Name = oss.str();
- }
-
- if (HasAnimation)
- geom->m_BoneAnimation.m_SrcVertPos = mesh_dst.m_Verts;
-
- const aiNode* root = scene->mRootNode;
- for (int j = 0; j < root->mNumChildren; ++j)
- {
- aiNode* n = root->mChildren[j];
- for (int k = 0; k < n->mNumChildren; ++k)
- {
- aiNode* cn = n->mChildren[k];
- Bone* b_root = bone_map.Bones[cn->mName.C_Str()];
-
- BoneRoot rb;
- rb.m_Bone = b_root;
- ae::ConvertMat(rb.m_Transform, cn->mTransformation);
- geom->m_BoneAnimation.m_RootNodes.push_back(rb);
-
- CreateBoneTree(bone_map, b_root, n->mChildren[k]);
- }
- }
-
- geom->InitializeBufferCommon();
- }
-
- scene_main.UpdateTransform();
-
- if (HasAnimation)
- AssimpUtil::CreateBoneMap(scene, bone_map);
+ scene.m_Objects.push_back(geom);
- scene_main.ReportDoneEditGeometry();
+ scene.RefreshObjectIndex();
+ scene.UpdateTransform();
+ scene.ReportDoneEditGeometry();
return true;
}