Adding support for Matrix Transformation export
[blender.git] / source / blender / collada / ControllerExporter.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, Sukhitha Jayathilake
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  */
23
24 /** \file blender/collada/ControllerExporter.cpp
25  *  \ingroup collada
26  */
27
28 #include "COLLADASWBaseInputElement.h"
29 #include "COLLADASWInstanceController.h"
30 #include "COLLADASWPrimitves.h"
31 #include "COLLADASWSource.h"
32
33 #include "DNA_action_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_modifier_types.h"
36
37 #include "BKE_action.h"
38 #include "BKE_armature.h"
39
40 extern "C" {
41 #include "BKE_main.h"
42 #include "BKE_mesh.h"
43 #include "BKE_global.h"
44 #include "BKE_library.h"
45 #include "BKE_idprop.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 "ControllerExporter.h"
55 #include "SceneExporter.h"
56
57 #include "collada_utils.h"
58
59 // XXX exporter writes wrong data for shared armatures.  A separate
60 // controller should be written for each armature-mesh binding how do
61 // we make controller ids then?
62 ControllerExporter::ControllerExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings) : COLLADASW::LibraryControllers(sw), export_settings(export_settings) {
63 }
64
65 bool ControllerExporter::is_skinned_mesh(Object *ob)
66 {
67         return bc_get_assigned_armature(ob) != NULL;
68 }
69
70
71 void ControllerExporter::write_bone_URLs(COLLADASW::InstanceController &ins, Object *ob_arm, Bone *bone)
72 {
73         if (bc_is_root_bone(bone, this->export_settings->deform_bones_only))
74                 ins.addSkeleton(COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_joint_id(bone, ob_arm)));
75         else {
76                 for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
77                         write_bone_URLs(ins, ob_arm, child);
78                 }
79         }
80 }
81
82 bool ControllerExporter::add_instance_controller(Object *ob)
83 {
84         Object *ob_arm = bc_get_assigned_armature(ob);
85         bArmature *arm = (bArmature *)ob_arm->data;
86
87         const std::string& controller_id = get_controller_id(ob_arm, ob);
88
89         COLLADASW::InstanceController ins(mSW);
90         ins.setUrl(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, controller_id));
91
92         Mesh *me = (Mesh *)ob->data;
93         if (!me->dvert) return false;
94
95         // write root bone URLs
96         Bone *bone;
97         for (bone = (Bone *)arm->bonebase.first; bone; bone = bone->next) {
98                 write_bone_URLs(ins, ob_arm, bone);
99         }
100
101         InstanceWriter::add_material_bindings(ins.getBindMaterial(),
102                     ob, 
103                         this->export_settings->active_uv_only,
104                         this->export_settings->export_texture_type);
105                 
106         ins.add();
107         return true;
108 }
109
110 void ControllerExporter::export_controllers(Scene *sce)
111 {
112         scene = sce;
113
114         openLibrary();
115
116         GeometryFunctor gf;
117         gf.forEachMeshObjectInExportSet<ControllerExporter>(sce, *this, this->export_settings->export_set);
118
119         closeLibrary();
120 }
121
122 void ControllerExporter::operator()(Object *ob)
123 {
124         Object *ob_arm = bc_get_assigned_armature(ob);
125         Key *key = BKE_key_from_object(ob);
126
127         if (ob_arm) {
128                 export_skin_controller(ob, ob_arm);
129         }
130         if (key && this->export_settings->include_shapekeys) {
131                 export_morph_controller(ob, key);
132         }
133 }
134 #if 0
135
136 bool ArmatureExporter::already_written(Object *ob_arm)
137 {
138         return std::find(written_armatures.begin(), written_armatures.end(), ob_arm) != written_armatures.end();
139 }
140
141 void ArmatureExporter::wrote(Object *ob_arm)
142 {
143         written_armatures.push_back(ob_arm);
144 }
145
146 void ArmatureExporter::find_objects_using_armature(Object *ob_arm, std::vector<Object *>& objects, Scene *sce)
147 {
148         objects.clear();
149
150         Base *base = (Base *) sce->base.first;
151         while (base) {
152                 Object *ob = base->object;
153                 
154                 if (ob->type == OB_MESH && get_assigned_armature(ob) == ob_arm) {
155                         objects.push_back(ob);
156                 }
157
158                 base = base->next;
159         }
160 }
161 #endif
162
163 std::string ControllerExporter::get_controller_id(Object *ob_arm, Object *ob)
164 {
165         return translate_id(id_name(ob_arm)) + "_" + translate_id(id_name(ob)) + SKIN_CONTROLLER_ID_SUFFIX;
166 }
167
168 std::string ControllerExporter::get_controller_id(Key *key, Object *ob)
169 {
170         return translate_id(id_name(ob)) + MORPH_CONTROLLER_ID_SUFFIX;
171 }
172
173 // ob should be of type OB_MESH
174 // both args are required
175 void ControllerExporter::export_skin_controller(Object *ob, Object *ob_arm)
176 {
177         // joint names
178         // joint inverse bind matrices
179         // vertex weights
180
181         // input:
182         // joint names: ob -> vertex group names
183         // vertex group weights: me->dvert -> groups -> index, weight
184
185 #if 0
186         me->dvert :
187
188         typedef struct MDeformVert {
189                 struct MDeformWeight *dw;
190                 int totweight;
191                 int flag;   // flag only in use for weightpaint now
192         } MDeformVert;
193
194         typedef struct MDeformWeight {
195                 int def_nr;
196                 float weight;
197         } MDeformWeight;
198 #endif
199
200         bool use_instantiation = this->export_settings->use_object_instantiation;
201         Mesh *me;
202
203         me = bc_get_mesh_copy(scene,
204                                 ob,
205                                 this->export_settings->export_mesh_type,
206                                 this->export_settings->apply_modifiers,
207                                 this->export_settings->triangulate);
208         
209         if (!me->dvert) return;
210
211         std::string controller_name = id_name(ob_arm);
212         std::string controller_id = get_controller_id(ob_arm, ob);
213
214         openSkin(controller_id, controller_name,
215                  COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_geometry_id(ob, use_instantiation)));
216
217         add_bind_shape_mat(ob);
218
219         std::string joints_source_id = add_joints_source(ob_arm, &ob->defbase, controller_id);
220         std::string inv_bind_mat_source_id = add_inv_bind_mats_source(ob_arm, &ob->defbase, controller_id);
221
222         std::list<int> vcounts;
223         std::list<int> joints;
224         std::list<float> weights;
225
226         {
227                 int i, j;
228
229                 // def group index -> joint index
230                 std::vector<int> joint_index_by_def_index;
231                 bDeformGroup *def;
232
233                 for (def = (bDeformGroup *)ob->defbase.first, i = 0, j = 0; def; def = def->next, i++) {
234                         if (is_bone_defgroup(ob_arm, def))
235                                 joint_index_by_def_index.push_back(j++);
236                         else
237                                 joint_index_by_def_index.push_back(-1);
238                 }
239
240                 int oob_counter = 0;
241                 for (i = 0; i < me->totvert; i++) {
242                         MDeformVert *vert = &me->dvert[i];
243                         std::map<int, float> jw;
244
245                         // We're normalizing the weights later
246                         float sumw = 0.0f;
247
248                         for (j = 0; j < vert->totweight; j++) {
249                                 int idx = vert->dw[j].def_nr;
250                                 if (idx >= joint_index_by_def_index.size()) {
251                                         // XXX: Maybe better find out where and 
252                                         //      why the Out Of Bound indexes get created ?
253                                         oob_counter += 1;
254                                 }
255                                 else {
256                                         if (idx >= 0) {
257                                                 int joint_index = joint_index_by_def_index[idx];
258                                                 if (joint_index != -1 && vert->dw[j].weight > 0.0f) {
259                                                         jw[joint_index] += vert->dw[j].weight;
260                                                         sumw += vert->dw[j].weight;
261                                                 }
262                                         }
263                                 }
264                         }
265
266                         if (sumw > 0.0f) {
267                                 float invsumw = 1.0f / sumw;
268                                 vcounts.push_back(jw.size());
269                                 for (std::map<int, float>::iterator m = jw.begin(); m != jw.end(); ++m) {
270                                         joints.push_back((*m).first);
271                                         weights.push_back(invsumw * (*m).second);
272                                 }
273                         }
274                         else {
275                                 vcounts.push_back(0);
276 #if 0
277                                 vcounts.push_back(1);
278                                 joints.push_back(-1);
279                                 weights.push_back(1.0f);
280 #endif
281                         }
282                 }
283
284                 if (oob_counter > 0) {
285                         fprintf(stderr, "Ignored %d Vertex weights which use index to non existing VGroup %zu.\n",
286                                 oob_counter, joint_index_by_def_index.size());
287                 }
288         }
289
290         std::string weights_source_id = add_weights_source(me, controller_id, weights);
291         add_joints_element(&ob->defbase, joints_source_id, inv_bind_mat_source_id);
292         add_vertex_weights_element(weights_source_id, joints_source_id, vcounts, joints);
293
294         BKE_libblock_free_us(G.main, me);
295
296         closeSkin();
297         closeController();
298 }
299
300 void ControllerExporter::export_morph_controller(Object *ob, Key *key)
301 {
302         bool use_instantiation = this->export_settings->use_object_instantiation;
303         Mesh *me;
304
305         me = bc_get_mesh_copy(scene,
306                                 ob,
307                                 this->export_settings->export_mesh_type,
308                                 this->export_settings->apply_modifiers,
309                                 this->export_settings->triangulate);
310
311         std::string controller_name = id_name(ob) + "-morph";
312         std::string controller_id = get_controller_id(key, ob);
313
314         openMorph(controller_id, controller_name,
315                  COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_geometry_id(ob, use_instantiation)));
316
317         std::string targets_id = add_morph_targets(key, ob);
318         std::string morph_weights_id = add_morph_weights(key, ob);
319         
320         COLLADASW::TargetsElement targets(mSW);
321
322         COLLADASW::InputList &input = targets.getInputList();
323
324         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::MORPH_TARGET, // constant declared in COLLADASWInputList.h
325                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, targets_id)));
326         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::MORPH_WEIGHT,
327                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, morph_weights_id)));
328         targets.add();
329
330         BKE_libblock_free_us(G.main, me);
331
332
333         //support for animations
334         //can also try the base element and param alternative
335         add_weight_extras(key);
336         closeMorph();
337         closeController();
338 }
339
340 std::string ControllerExporter::add_morph_targets(Key *key, Object *ob)
341 {
342         std::string source_id = translate_id(id_name(ob)) + TARGETS_SOURCE_ID_SUFFIX;
343
344         COLLADASW::IdRefSource source(mSW);
345         source.setId(source_id);
346         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
347         source.setAccessorCount(key->totkey - 1);
348         source.setAccessorStride(1);
349
350         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
351         param.push_back("IDREF");
352
353         source.prepareToAppendValues();
354
355         KeyBlock *kb = (KeyBlock *)key->block.first;
356         //skip the basis
357         kb = kb->next;
358         for (; kb; kb = kb->next) {
359                 std::string geom_id = get_geometry_id(ob, false) + "_morph_" + translate_id(kb->name);
360                 source.appendValues(geom_id);
361
362         }
363
364         source.finish();
365
366         return source_id;
367 }
368
369 std::string ControllerExporter::add_morph_weights(Key *key, Object *ob)
370 {
371         std::string source_id = translate_id(id_name(ob)) + WEIGHTS_SOURCE_ID_SUFFIX;
372
373         COLLADASW::FloatSourceF source(mSW);
374         source.setId(source_id);
375         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
376         source.setAccessorCount(key->totkey - 1);
377         source.setAccessorStride(1);
378
379         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
380         param.push_back("MORPH_WEIGHT");
381         
382         source.prepareToAppendValues();
383
384         KeyBlock *kb = (KeyBlock *)key->block.first;
385         //skip the basis
386         kb = kb->next;
387         for (; kb; kb = kb->next) {
388                 float weight = kb->curval;
389                 source.appendValues(weight);
390         }
391         source.finish();
392
393         return source_id;
394 }
395
396 //Added to implemente support for animations.
397 void ControllerExporter::add_weight_extras(Key *key)
398 {
399         // can also try the base element and param alternative
400         COLLADASW::BaseExtraTechnique extra;
401         
402         KeyBlock * kb = (KeyBlock *)key->block.first;
403         //skip the basis
404         kb = kb->next;
405         for (; kb; kb = kb->next) {
406                 // XXX why is the weight not used here and set to 0.0?
407                 // float weight = kb->curval;
408                 extra.addExtraTechniqueParameter ("KHR", "morph_weights" , 0.000, "MORPH_WEIGHT_TO_TARGET");
409         }
410 }
411
412
413
414 void ControllerExporter::add_joints_element(ListBase *defbase,
415                                           const std::string& joints_source_id, const std::string& inv_bind_mat_source_id)
416 {
417         COLLADASW::JointsElement joints(mSW);
418         COLLADASW::InputList &input = joints.getInputList();
419
420         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::JOINT, // constant declared in COLLADASWInputList.h
421                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, joints_source_id)));
422         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::BINDMATRIX,
423                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, inv_bind_mat_source_id)));
424         joints.add();
425 }
426
427 void ControllerExporter::add_bind_shape_mat(Object *ob)
428 {
429         double bind_mat[4][4];
430
431         converter.mat4_to_dae_double(bind_mat, ob->obmat);
432
433         addBindShapeTransform(bind_mat);
434 }
435
436 std::string ControllerExporter::add_joints_source(Object *ob_arm, ListBase *defbase, const std::string& controller_id)
437 {
438         std::string source_id = controller_id + JOINTS_SOURCE_ID_SUFFIX;
439
440         int totjoint = 0;
441         bDeformGroup *def;
442         for (def = (bDeformGroup *)defbase->first; def; def = def->next) {
443                 if (is_bone_defgroup(ob_arm, def))
444                         totjoint++;
445         }
446
447         COLLADASW::NameSource source(mSW);
448         source.setId(source_id);
449         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
450         source.setAccessorCount(totjoint);
451         source.setAccessorStride(1);
452         
453         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
454         param.push_back("JOINT");
455
456         source.prepareToAppendValues();
457
458         for (def = (bDeformGroup *)defbase->first; def; def = def->next) {
459                 Bone *bone = get_bone_from_defgroup(ob_arm, def);
460                 if (bone)
461                         source.appendValues(get_joint_sid(bone, ob_arm));
462         }
463
464         source.finish();
465
466         return source_id;
467 }
468
469
470 std::string ControllerExporter::add_inv_bind_mats_source(Object *ob_arm, ListBase *defbase, const std::string& controller_id)
471 {
472         std::string source_id = controller_id + BIND_POSES_SOURCE_ID_SUFFIX;
473
474         int totjoint = 0;
475         for (bDeformGroup *def = (bDeformGroup *)defbase->first; def; def = def->next) {
476                 if (is_bone_defgroup(ob_arm, def))
477                         totjoint++;
478         }
479
480         COLLADASW::FloatSourceF source(mSW);
481         source.setId(source_id);
482         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
483         source.setAccessorCount(totjoint); //BLI_listbase_count(defbase));
484         source.setAccessorStride(16);
485         
486         source.setParameterTypeName(&COLLADASW::CSWC::CSW_VALUE_TYPE_FLOAT4x4);
487         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
488         param.push_back("TRANSFORM");
489
490         source.prepareToAppendValues();
491
492         bPose *pose = ob_arm->pose;
493         bArmature *arm = (bArmature *)ob_arm->data;
494
495         int flag = arm->flag;
496
497         // put armature in rest position
498         if (!(arm->flag & ARM_RESTPOS)) {
499                 arm->flag |= ARM_RESTPOS;
500                 BKE_pose_where_is(scene, ob_arm);
501         }
502
503         for (bDeformGroup *def = (bDeformGroup *)defbase->first; def; def = def->next) {
504                 if (is_bone_defgroup(ob_arm, def)) {
505                         bPoseChannel *pchan = BKE_pose_channel_find_name(pose, def->name);
506
507                         float mat[4][4];
508                         float world[4][4];
509                         float inv_bind_mat[4][4];
510
511                         float bind_mat[4][4]; /* derived from bone->arm_mat */
512
513                         bool has_bindmat = bc_get_property_matrix(pchan->bone, "bind_mat", bind_mat);
514                         
515                         if (!has_bindmat) {
516
517                                 /* Have no bind matrix stored, try old style <= Blender 2.78 */
518
519                                 bc_create_restpose_mat(this->export_settings, pchan->bone, bind_mat, pchan->bone->arm_mat, true);
520
521                                 // SL/OPEN_SIM COMPATIBILITY
522                                 if (export_settings->open_sim) {
523
524                                         float loc[3];
525                                         float rot[3] = { 0, 0, 0 };
526                                         float scale[3];
527                                         bc_decompose(bind_mat, loc, NULL, NULL, scale);
528
529                                         // Only translations, no rotation vs armature
530                                         loc_eulO_size_to_mat4(bind_mat, loc, rot, scale, 6);
531                                 }
532                         }
533
534                         // make world-space matrix (bind_mat is armature-space)
535                         mul_m4_m4m4(world, ob_arm->obmat, bind_mat);
536
537                         invert_m4_m4(mat, world);
538                         converter.mat4_to_dae(inv_bind_mat, mat);
539                         if (this->export_settings->limit_precision)
540                                 bc_sanitize_mat(inv_bind_mat, 6);
541                         source.appendValues(inv_bind_mat);
542                 }
543         }
544
545         // back from rest positon
546         if (!(flag & ARM_RESTPOS)) {
547                 arm->flag = flag;
548                 BKE_pose_where_is(scene, ob_arm);
549         }
550
551         source.finish();
552
553         return source_id;
554 }
555
556 Bone *ControllerExporter::get_bone_from_defgroup(Object *ob_arm, bDeformGroup *def)
557 {
558         bPoseChannel *pchan = BKE_pose_channel_find_name(ob_arm->pose, def->name);
559         return pchan ? pchan->bone : NULL;
560 }
561
562 bool ControllerExporter::is_bone_defgroup(Object *ob_arm, bDeformGroup *def)
563 {
564         return get_bone_from_defgroup(ob_arm, def) != NULL;
565 }
566
567 std::string ControllerExporter::add_weights_source(Mesh *me, const std::string& controller_id, const std::list<float>& weights)
568 {
569         std::string source_id = controller_id + WEIGHTS_SOURCE_ID_SUFFIX;
570
571         COLLADASW::FloatSourceF source(mSW);
572         source.setId(source_id);
573         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
574         source.setAccessorCount(weights.size());
575         source.setAccessorStride(1);
576         
577         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
578         param.push_back("WEIGHT");
579
580         source.prepareToAppendValues();
581
582         for (std::list<float>::const_iterator i = weights.begin(); i != weights.end(); ++i) {
583                 source.appendValues(*i);
584         }
585
586         source.finish();
587
588         return source_id;
589 }
590
591 void ControllerExporter::add_vertex_weights_element(const std::string& weights_source_id, const std::string& joints_source_id,
592                                                   const std::list<int>& vcounts,
593                                                   const std::list<int>& joints)
594 {
595         COLLADASW::VertexWeightsElement weightselem(mSW);
596         COLLADASW::InputList &input = weightselem.getInputList();
597
598         int offset = 0;
599         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::JOINT, // constant declared in COLLADASWInputList.h
600                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, joints_source_id), offset++));
601         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::WEIGHT,
602                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, weights_source_id), offset++));
603
604         weightselem.setCount(vcounts.size());
605
606         // write number of deformers per vertex
607         COLLADASW::PrimitivesBase::VCountList vcountlist;
608
609         vcountlist.resize(vcounts.size());
610         std::copy(vcounts.begin(), vcounts.end(), vcountlist.begin());
611
612         weightselem.prepareToAppendVCountValues();
613         weightselem.appendVertexCount(vcountlist);
614
615         weightselem.CloseVCountAndOpenVElement();
616
617         // write deformer index - weight index pairs
618         int weight_index = 0;
619         for (std::list<int>::const_iterator i = joints.begin(); i != joints.end(); ++i) {
620                 weightselem.appendValues(*i, weight_index++);
621         }
622
623         weightselem.finish();
624 }