Revision: 47981
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=47981
Author:   aramis_acg
Date:     2012-06-15 23:54:51 +0000 (Fri, 15 Jun 2012)
Log Message:
-----------
- bf_assimp: first version of my ArmatureImporter in which the result looks at 
least halfway right.
- bf_assimp: slight refactoring, move ai matrix to blend matrix conversion to 
util.

Modified Paths:
--------------
    branches/soc-2012-bratwurst/source/blender/assimp/CMakeLists.txt
    branches/soc-2012-bratwurst/source/blender/assimp/MaterialImporter.h
    branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.cpp
    branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.h
    branches/soc-2012-bratwurst/source/blender/assimp/bassimp_internal.cpp
    branches/soc-2012-bratwurst/source/blender/assimp/bassimp_internal.h

Added Paths:
-----------
    branches/soc-2012-bratwurst/source/blender/assimp/AnimationImporter.cpp
    branches/soc-2012-bratwurst/source/blender/assimp/AnimationImporter.h
    branches/soc-2012-bratwurst/source/blender/assimp/ArmatureImporter.cpp
    branches/soc-2012-bratwurst/source/blender/assimp/ArmatureImporter.h

Added: branches/soc-2012-bratwurst/source/blender/assimp/AnimationImporter.cpp
===================================================================
Added: branches/soc-2012-bratwurst/source/blender/assimp/AnimationImporter.h
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/AnimationImporter.h       
                        (rev 0)
+++ branches/soc-2012-bratwurst/source/blender/assimp/AnimationImporter.h       
2012-06-15 23:54:51 UTC (rev 47981)
@@ -0,0 +1,67 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Alexander Gessler
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file MaterialImporter.h
+ *  \ingroup assimp
+ */
+
+#ifndef INCLUDED_ANIMATIONIMPORTER_H
+#define INCLUDED_ANIMATIONIMPORTER_H
+
+#include <string>
+#include "bassimp_shared.h"
+
+namespace bassimp {
+
+
+
+class AnimationImporter 
+{
+private:
+
+       bArmature* armature;
+       Scene* out_scene;
+       const aiAnimation* in_anim;
+       unsigned int in_anim_index;
+       const aiScene* in_scene;
+
+       const SceneImporter& scene_imp;
+       
+
+private:
+
+       void error(const char* message);
+       void verbose(const char* message);
+
+public:
+
+       AnimationImporter(const SceneImporter& scene_imp, const aiAnimation* 
in_anim, const aiScene* in_scene, Scene* out_scene, unsigned int in_anim_index);
+       ~AnimationImporter();
+
+       // run conversion
+       void convert();
+
+};
+
+}
+
+#endif

Added: branches/soc-2012-bratwurst/source/blender/assimp/ArmatureImporter.cpp
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/ArmatureImporter.cpp      
                        (rev 0)
+++ branches/soc-2012-bratwurst/source/blender/assimp/ArmatureImporter.cpp      
2012-06-15 23:54:51 UTC (rev 47981)
@@ -0,0 +1,218 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Alexander Gessler
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/assimp/MeshImporter.cpp
+ *  \ingroup assimp
+ */
+
+#include <cassert>
+#include <sstream>
+
+#include "SceneImporter.h"
+#include "ArmatureImporter.h"
+#include "bassimp_internal.h"
+#include "ED_armature.h"
+
+extern "C" {
+#      include "BKE_global.h"
+#      include "BKE_main.h"
+#      include "BKE_mesh.h"
+#      include "BKE_displist.h"
+#      include "BKE_library.h"
+#      include "BKE_material.h"
+#      include "BKE_texture.h"
+#      include "BKE_armature.h"
+#      include "BKE_depsgraph.h"
+
+#      include "MEM_guardedalloc.h"
+
+#      include "BLI_listbase.h"
+#      include "BLI_math.h"
+#      include "BLI_string.h"
+}
+
+namespace bassimp {
+
+ArmatureImporter::ArmatureImporter(const SceneImporter& scene_imp,  const 
aiScene* in_scene, Scene *out_scene)
+: out_scene(out_scene)
+, armature()
+, in_scene(in_scene)
+, scene_imp(scene_imp)
+{
+       assert(out_scene);
+       assert(in_scene);
+}
+
+
+ArmatureImporter::~ArmatureImporter()
+{
+       if (ob_armature) {
+               BKE_libblock_free(&G.main->armature,armature);
+       }
+}
+
+
+void ArmatureImporter::error(const char* message)
+{
+       scene_imp.error((message + std::string(" (armature)")).c_str());
+}
+
+
+void ArmatureImporter::verbose(const char* message)
+{
+       scene_imp.verbose((message + std::string(" (armature)")).c_str());
+}
+
+Object* ArmatureImporter::get_armature() const
+{
+       return ob_armature;
+}
+
+
+Object* ArmatureImporter::disown_armature()
+{
+       Object* m = ob_armature;
+       ob_armature = NULL;
+       return m;
+}
+
+
+void ArmatureImporter::compute_world_matrix(const aiNode* node, const 
aiMatrix4x4& parentWorld)
+{
+       // "the limit of my matrix means the limit of my world" or something 
like that.
+       const aiMatrix4x4 myWorld = parentWorld * node->mTransformation;
+       node_to_world_matrix[node] = myWorld;
+       
+       for (unsigned int i = 0; i < node->mNumChildren; ++i) {
+               compute_world_matrix(node->mChildren[i],myWorld);
+       }
+}
+
+
+void ArmatureImporter::get_world_matrix(const aiNode* node, float out[4][4])
+{
+       assert(node_to_world_matrix.find(node) != node_to_world_matrix.end());
+       copy_ai_matrix(out, node_to_world_matrix[node] );
+}
+
+
+void ArmatureImporter::convert_node(const aiNode* node, EditBone* parent, 
const aiNode* parentNode, unsigned int totchild)
+{
+       EditBone* const bone = ED_armature_edit_bone_add(armature, (char 
*)node->mName.data);
+       if(parent != NULL)
+       {
+               bone->parent = parent;
+       }
+       else {
+               // make this the active bone for the armature
+               armature->act_edbone = bone;
+       }
+
+       float obmat[4][4];
+       if(parentNode != NULL)
+       {
+               get_world_matrix(parentNode,obmat);
+       }
+       else {
+               for (unsigned int y = 0; y < 4; ++y) {
+                       for (unsigned int x = 0; x < 4; ++x)    {
+                               obmat[x][y] = (x == y ? 1.0f : 0.0f);
+                       }
+               }
+       }
+
+       float loc[3], size[3], rot[3][3], angle;
+       mat4_to_loc_rot_size(loc, rot, size, obmat);
+       mat3_to_vec_roll(rot, NULL, &angle);
+       bone->roll = angle;
+
+       // set head
+       copy_v3_v3(bone->head, obmat[3]);
+
+       // set tail, don't set it to head because 0-length bones are not allowed
+       float vec[3] = {0.0f, 0.5f, 0.0f};
+       add_v3_v3v3(bone->tail, bone->head, vec);
+
+       // set parent tail
+       if (parent && totchild == 1) {
+               copy_v3_v3(parent->tail, bone->head);
+
+               // not setting BONE_CONNECTED because this would lock child 
bone location with respect to parent
+               // bone->flag |= BONE_CONNECTED;
+
+               // XXX increase this to prevent "very" small bones?
+               const float epsilon = 0.000001f;
+
+       
+               /*// derive leaf bone length
+               float length = len_v3v3(parent->head, parent->tail);
+               if ((length < leaf_bone_length || totbone == 0) && length > 
epsilon) {
+                       leaf_bone_length = length;
+               }
+
+               // treat zero-sized bone like a leaf bone
+               if (length <= epsilon) {
+                       add_leaf_bone(parent_mat, parent, node);
+               } */
+       }
+
+       for(unsigned int i = 0; i < node->mNumChildren; ++i) {
+               convert_node(node->mChildren[i],bone,node,node->mNumChildren);
+       }
+
+       /*
+       // in second case it's not a leaf bone, but we handle it the same way
+       if (!children.getCount() || children.getCount() > 1) {
+               add_leaf_bone(mat, bone, node);
+       } */
+}
+
+
+void ArmatureImporter::convert()
+{
+       // first cache world matrices for all nodes
+       compute_world_matrix(in_scene->mRootNode,aiMatrix4x4());
+
+       // create a new object and attach a also new armature
+       ob_armature = util_add_object(out_scene, OB_ARMATURE, NULL);
+
+       armature = BKE_armature_add("armature");
+       verbose("converting armature");
+
+       bArmature* const old_armature = 
static_cast<bArmature*>(ob_armature->data);
+       ob_armature->data = armature;
+       if (--old_armature->id.us == 0) {
+               BKE_libblock_free(&G.main->armature, old_armature);
+       }
+
+       // enter armature edit mode
+       ED_armature_to_edit(ob_armature);
+
+       convert_node(in_scene->mRootNode,NULL,NULL,1);
+
+       // exit armature edit mode
+       ED_armature_from_edit(ob_armature);
+       ED_armature_edit_free(ob_armature);
+       DAG_id_tag_update(&ob_armature->id, OB_RECALC_OB | OB_RECALC_DATA);
+}
+
+}

Added: branches/soc-2012-bratwurst/source/blender/assimp/ArmatureImporter.h
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/ArmatureImporter.h        
                        (rev 0)
+++ branches/soc-2012-bratwurst/source/blender/assimp/ArmatureImporter.h        
2012-06-15 23:54:51 UTC (rev 47981)
@@ -0,0 +1,79 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Alexander Gessler
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file MaterialImporter.h
+ *  \ingroup assimp
+ */
+
+#ifndef INCLUDED_ARMATUREIMPORTER_H
+#define INCLUDED_ARMATUREIMPORTER_H
+
+#include <string>
+#include <map>
+
+#include "bassimp_shared.h"
+
+struct EditBone;
+
+namespace bassimp {
+
+class ArmatureImporter 
+{
+private:
+
+       Object* ob_armature;
+       bArmature* armature;
+       Scene* out_scene;
+       const aiScene* in_scene;
+
+       const SceneImporter& scene_imp;
+
+       std::map<const aiNode*,aiMatrix4x4> node_to_world_matrix;
+
+private:
+
+       void error(const char* message);
+       void verbose(const char* message);
+
+       void compute_world_matrix(const aiNode* node, const aiMatrix4x4& 
parentWorld);
+       void get_world_matrix(const aiNode* node, float out[4][4]);
+
+       void convert_node(const aiNode* node, EditBone* parent, const aiNode* 
node_parent, unsigned int totchild);
+
+public:
+
+       ArmatureImporter(const SceneImporter& scene_imp, const aiScene* 
in_scene, Scene* out_scene);
+       ~ArmatureImporter();
+
+       // run conversion
+       void convert();
+
+       // get converted armature, but ownership of the object stays here

@@ Diff output truncated at 10240 characters. @@
_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to