fix for struct definition building with msvc2008 and some style cleanup.
[blender-staging.git] / source / blender / collada / ArmatureExporter.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
19  *                 Nathan Letwory
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  */
23
24 /** \file blender/collada/ArmatureExporter.cpp
25  *  \ingroup collada
26  */
27
28
29 #include "COLLADASWBaseInputElement.h"
30 #include "COLLADASWInstanceController.h"
31 #include "COLLADASWPrimitves.h"
32 #include "COLLADASWSource.h"
33
34 #include "DNA_action_types.h"
35 #include "DNA_meshdata_types.h"
36 #include "DNA_modifier_types.h"
37
38 #include "BKE_action.h"
39 #include "BKE_armature.h"
40
41 extern "C" {
42 #include "BKE_main.h"
43 #include "BKE_mesh.h"
44 #include "BKE_global.h"
45 #include "BKE_library.h"
46 }
47
48 #include "ED_armature.h"
49
50 #include "BLI_listbase.h"
51
52 #include "GeometryExporter.h"
53 #include "ArmatureExporter.h"
54 #include "SceneExporter.h"
55
56 #include "collada_utils.h"
57
58 // XXX exporter writes wrong data for shared armatures.  A separate
59 // controller should be written for each armature-mesh binding how do
60 // we make controller ids then?
61 ArmatureExporter::ArmatureExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings) : COLLADASW::LibraryControllers(sw), export_settings(export_settings) {
62 }
63
64 // write bone nodes
65 void ArmatureExporter::add_armature_bones(Object *ob_arm, Scene *sce,
66                                           SceneExporter *se,
67                                           std::list<Object *>& child_objects)
68 {
69         // write bone nodes
70         bArmature *arm = (bArmature *)ob_arm->data;
71         for (Bone *bone = (Bone *)arm->bonebase.first; bone; bone = bone->next) {
72                 // start from root bones
73                 if (!bone->parent)
74                         add_bone_node(bone, ob_arm, sce, se, child_objects);
75         }
76 }
77
78 void ArmatureExporter::write_bone_URLs(COLLADASW::InstanceController &ins, Object *ob_arm, Bone *bone)
79 {
80         if (bc_is_root_bone(bone, this->export_settings->deform_bones_only))
81                 ins.addSkeleton(COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_joint_id(bone, ob_arm)));
82         else {
83                 for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
84                         write_bone_URLs(ins, ob_arm, child);
85                 }
86         }
87 }
88
89 bool ArmatureExporter::add_instance_controller(Object *ob)
90 {
91         Object *ob_arm = bc_get_assigned_armature(ob);
92         bArmature *arm = (bArmature *)ob_arm->data;
93
94         const std::string& controller_id = get_controller_id(ob_arm, ob);
95
96         COLLADASW::InstanceController ins(mSW);
97         ins.setUrl(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, controller_id));
98
99         Mesh *me = (Mesh *)ob->data;
100         if (!me->dvert) return false;
101
102         // write root bone URLs
103         Bone *bone;
104         for (bone = (Bone *)arm->bonebase.first; bone; bone = bone->next) {
105                 write_bone_URLs(ins, ob_arm, bone);
106         }
107
108         InstanceWriter::add_material_bindings(ins.getBindMaterial(), ob, this->export_settings->active_uv_only);
109                 
110         ins.add();
111         return true;
112 }
113
114 #if 0
115 void ArmatureExporter::operator()(Object *ob)
116 {
117         Object *ob_arm = bc_get_assigned_armature(ob);
118
119 }
120
121 bool ArmatureExporter::already_written(Object *ob_arm)
122 {
123         return std::find(written_armatures.begin(), written_armatures.end(), ob_arm) != written_armatures.end();
124 }
125
126 void ArmatureExporter::wrote(Object *ob_arm)
127 {
128         written_armatures.push_back(ob_arm);
129 }
130
131 void ArmatureExporter::find_objects_using_armature(Object *ob_arm, std::vector<Object *>& objects, Scene *sce)
132 {
133         objects.clear();
134
135         Base *base = (Base *) sce->base.first;
136         while (base) {
137                 Object *ob = base->object;
138                 
139                 if (ob->type == OB_MESH && get_assigned_armature(ob) == ob_arm) {
140                         objects.push_back(ob);
141                 }
142
143                 base = base->next;
144         }
145 }
146 #endif
147
148 std::string ArmatureExporter::get_joint_sid(Bone *bone, Object *ob_arm)
149 {
150         return get_joint_id(bone, ob_arm);
151 }
152
153 // parent_mat is armature-space
154 void ArmatureExporter::add_bone_node(Bone *bone, Object *ob_arm, Scene *sce,
155                                      SceneExporter *se,
156                                      std::list<Object *>& child_objects)
157 {
158         if (!(this->export_settings->deform_bones_only && bone->flag & BONE_NO_DEFORM)) {
159                 std::string node_id = get_joint_id(bone, ob_arm);
160                 std::string node_name = std::string(bone->name);
161                 std::string node_sid = get_joint_sid(bone, ob_arm);
162
163                 COLLADASW::Node node(mSW);
164
165                 node.setType(COLLADASW::Node::JOINT);
166                 node.setNodeId(node_id);
167                 node.setNodeName(node_name);
168                 node.setNodeSid(node_sid);
169
170 #if 0 
171                 if (bone->childbase.first == NULL || BLI_countlist(&(bone->childbase)) >= 2) {
172                         add_blender_leaf_bone( bone, ob_arm, node);
173                 }
174                 else {
175 #endif
176                         node.start();
177
178                         add_bone_transform(ob_arm, bone, node);
179
180                         // Write nodes of childobjects, remove written objects from list
181                         std::list<Object *>::iterator i = child_objects.begin();
182
183                         while (i != child_objects.end()) {
184                                 if ((*i)->partype == PARBONE && (0 == strcmp((*i)->parsubstr, bone->name))) {
185                                         float backup_parinv[4][4];
186                                         copy_m4_m4(backup_parinv, (*i)->parentinv);
187
188                                         // crude, temporary change to parentinv
189                                         // so transform gets exported correctly.
190
191                                         // Add bone tail- translation... don't know why
192                                         // bone parenting is against the tail of a bone
193                                         // and not it's head, seems arbitrary.
194                                         (*i)->parentinv[3][1] += bone->length;
195
196                                         // SECOND_LIFE_COMPATIBILITY
197                                         // TODO: when such objects are animated as
198                                         // single matrix the tweak must be applied
199                                         // to the result.
200                                         if (export_settings->second_life) {
201                                                 // tweak objects parentinverse to match compatibility
202                                                 float temp[4][4];
203
204                                                 copy_m4_m4(temp, bone->arm_mat);
205                                                 temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
206
207                                                 mult_m4_m4m4((*i)->parentinv, temp, (*i)->parentinv);
208                                         }
209
210                                         se->writeNodes(*i, sce);
211
212                                         copy_m4_m4((*i)->parentinv, backup_parinv);
213                                         child_objects.erase(i++);
214                                 }
215                                 else i++;
216                         }
217
218                         for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
219                                 add_bone_node(child, ob_arm, sce, se, child_objects);
220                         }
221                         node.end();
222                 }
223                 else {
224                         for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
225                                 add_bone_node(child, ob_arm, sce, se, child_objects);
226                         }
227                 }
228 }
229
230 //#if 1
231 void ArmatureExporter::add_blender_leaf_bone(Bone *bone, Object *ob_arm, COLLADASW::Node& node)
232 {
233         node.start();
234         
235         add_bone_transform(ob_arm, bone, node);
236         
237         node.addExtraTechniqueParameter("blender", "tip_x", bone->tail[0]);
238         node.addExtraTechniqueParameter("blender", "tip_y", bone->tail[1]);
239         node.addExtraTechniqueParameter("blender", "tip_z", bone->tail[2]);
240         
241         /*for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
242                 add_bone_node(child, ob_arm, sce, se, child_objects);
243         }*/
244         node.end();
245         
246 }
247 //#endif
248
249 void ArmatureExporter::add_bone_transform(Object *ob_arm, Bone *bone, COLLADASW::Node& node)
250 {
251         //bPoseChannel *pchan = BKE_pose_channel_find_name(ob_arm->pose, bone->name);
252
253         float mat[4][4];
254
255         if (bone->parent) {
256                 // get bone-space matrix from parent pose
257                 /*bPoseChannel *parchan = BKE_pose_channel_find_name(ob_arm->pose, bone->parent->name);
258                 float invpar[4][4];
259                 invert_m4_m4(invpar, parchan->pose_mat);
260                 mult_m4_m4m4(mat, invpar, pchan->pose_mat);*/
261                 
262                 float invpar[4][4];
263                 invert_m4_m4(invpar, bone->parent->arm_mat);
264                 mult_m4_m4m4(mat, invpar, bone->arm_mat);
265
266         }
267         else {
268                 
269                 //copy_m4_m4(mat, pchan->pose_mat);
270                 //pose mat is object space
271                 //New change: export bone->arm_mat
272                 copy_m4_m4(mat, bone->arm_mat);
273         }
274
275         // SECOND_LIFE_COMPATIBILITY
276         if (export_settings->second_life) {
277                 // Remove rotations vs armature from transform
278                 // parent_rest_rot * mat * irest_rot
279                 float temp[4][4];
280                 copy_m4_m4(temp, bone->arm_mat);
281                 temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
282                 invert_m4(temp);
283
284                 mult_m4_m4m4(mat, mat, temp);
285
286                 if (bone->parent) {
287                         copy_m4_m4(temp, bone->parent->arm_mat);
288                         temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
289
290                         mult_m4_m4m4(mat, temp, mat);
291                 }
292         }
293
294         TransformWriter::add_node_transform(node, mat, NULL);
295 }
296
297 std::string ArmatureExporter::get_controller_id(Object *ob_arm, Object *ob)
298 {
299         return translate_id(id_name(ob_arm)) + "_" + translate_id(id_name(ob)) + SKIN_CONTROLLER_ID_SUFFIX;
300 }