Collada exporter update
[blender.git] / source / blender / collada / AnimationExporter.h
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 #ifndef __BC_ANIMATION_EXPORTER_H__
18 #define __BC_ANIMATION_EXPORTER_H__
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <math.h>
23
24 #include "BCAnimationCurve.h"
25
26 extern "C" {
27 #include "DNA_scene_types.h"
28 #include "DNA_object_types.h"
29 #include "DNA_anim_types.h"
30 #include "DNA_action_types.h"
31 #include "DNA_curve_types.h"
32 #include "DNA_light_types.h"
33 #include "DNA_camera_types.h"
34 #include "DNA_armature_types.h"
35 #include "DNA_material_types.h"
36 #include "DNA_constraint_types.h"
37 #include "DNA_scene_types.h"
38
39 #include "BLI_math.h"
40 #include "BLI_string.h"
41 #include "BLI_listbase.h"
42 #include "BLI_utildefines.h"
43
44 #include "BKE_fcurve.h"
45 #include "BKE_animsys.h"
46 #include "BKE_scene.h"
47 #include "BKE_action.h"  // pose functions
48 #include "BKE_armature.h"
49 #include "BKE_object.h"
50 #include "BKE_constraint.h"
51 #include "BIK_api.h"
52 #include "ED_object.h"
53 }
54
55 #include "MEM_guardedalloc.h"
56
57 #include "RNA_access.h"
58
59 #include "COLLADASWSource.h"
60 #include "COLLADASWInstanceGeometry.h"
61 #include "COLLADASWInputList.h"
62 #include "COLLADASWPrimitves.h"
63 #include "COLLADASWVertices.h"
64 #include "COLLADASWLibraryAnimations.h"
65 #include "COLLADASWParamTemplate.h"
66 #include "COLLADASWParamBase.h"
67 #include "COLLADASWSampler.h"
68 #include "COLLADASWConstants.h"
69 #include "COLLADASWBaseInputElement.h"
70
71 #include "EffectExporter.h"
72 #include "BCAnimationSampler.h"
73 #include "collada_internal.h"
74
75 #include "IK_solver.h"
76
77 #include <vector>
78 #include <map>
79 #include <algorithm>  // std::find
80
81 struct Depsgraph;
82
83 typedef enum BC_animation_source_type {
84   BC_SOURCE_TYPE_VALUE,
85   BC_SOURCE_TYPE_ANGLE,
86   BC_SOURCE_TYPE_TIMEFRAME,
87 } BC_animation_source_type;
88
89 typedef enum BC_global_rotation_type {
90   BC_NO_ROTATION,
91   BC_OBJECT_ROTATION,
92   BC_DATA_ROTATION
93 } BC_global_rotation_type;
94
95 class AnimationExporter : COLLADASW::LibraryAnimations {
96  private:
97   COLLADASW::StreamWriter *sw;
98   BCExportSettings &export_settings;
99
100   BC_global_rotation_type get_global_rotation_type(Object *ob);
101
102  public:
103   AnimationExporter(COLLADASW::StreamWriter *sw, BCExportSettings &export_settings)
104       : COLLADASW::LibraryAnimations(sw), sw(sw), export_settings(export_settings)
105   {
106   }
107
108   bool exportAnimations();
109
110   // called for each exported object
111   void operator()(Object *ob);
112
113  protected:
114   void export_object_constraint_animation(Object *ob);
115
116   void export_morph_animation(Object *ob);
117
118   void write_bone_animation_matrix(Object *ob_arm, Bone *bone);
119
120   void write_bone_animation(Object *ob_arm, Bone *bone);
121
122   void sample_and_write_bone_animation(Object *ob_arm, Bone *bone, int transform_type);
123
124   void sample_and_write_bone_animation_matrix(Object *ob_arm, Bone *bone);
125
126   void sample_animation(float *v,
127                         std::vector<float> &frames,
128                         int type,
129                         Bone *bone,
130                         Object *ob_arm,
131                         bPoseChannel *pChan);
132
133   void sample_animation(std::vector<float[4][4]> &mats,
134                         std::vector<float> &frames,
135                         Bone *bone,
136                         Object *ob_arm,
137                         bPoseChannel *pChan);
138
139   // dae_bone_animation -> add_bone_animation
140   // (blend this into dae_bone_animation)
141   void dae_bone_animation(std::vector<float> &fra,
142                           float *v,
143                           int tm_type,
144                           int axis,
145                           std::string ob_name,
146                           std::string bone_name);
147
148   void dae_baked_animation(std::vector<float> &fra, Object *ob_arm, Bone *bone);
149
150   void dae_baked_object_animation(std::vector<float> &fra, Object *ob);
151
152   float convert_time(float frame);
153
154   float convert_angle(float angle);
155
156   std::vector<std::vector<std::string>> anim_meta;
157
158   /* Main entry point into Animation export (called for each exported object) */
159   void exportAnimation(Object *ob, BCAnimationSampler &sampler);
160
161   /* export animation as separate trans/rot/scale curves */
162   void export_curve_animation_set(Object *ob, BCAnimationSampler &sampler, bool export_tm_curves);
163
164   /* export one single curve */
165   void export_curve_animation(Object *ob, BCAnimationCurve &curve);
166
167   /* export animation as matrix data */
168   void export_matrix_animation(Object *ob, BCAnimationSampler &sampler);
169
170   /* step through the bone hierarchy */
171   void export_bone_animations_recursive(Object *ob_arm, Bone *bone, BCAnimationSampler &sampler);
172
173   /* Export for one bone */
174   void export_bone_animation(Object *ob, Bone *bone, BCFrames &frames, BCMatrixSampleMap &outmats);
175
176   /* call to the low level collada exporter */
177   void export_collada_curve_animation(std::string id,
178                                       std::string name,
179                                       std::string target,
180                                       std::string axis,
181                                       BCAnimationCurve &curve,
182                                       BC_global_rotation_type global_rotation_type);
183
184   /* call to the low level collada exporter */
185   void export_collada_matrix_animation(std::string id,
186                                        std::string name,
187                                        std::string target,
188                                        BCFrames &frames,
189                                        BCMatrixSampleMap &outmats,
190                                        BC_global_rotation_type global_rotation_type,
191                                        Matrix &parentinv);
192
193   BCAnimationCurve *get_modified_export_curve(Object *ob,
194                                               BCAnimationCurve &curve,
195                                               BCAnimationCurveMap &curves);
196
197   /* Helper functions */
198   void openAnimationWithClip(std::string id, std::string name);
199   bool open_animation_container(bool has_container, Object *ob);
200   void close_animation_container(bool has_container);
201
202   /* Input and Output sources (single valued) */
203   std::string collada_source_from_values(BC_animation_source_type tm_channel,
204                                          COLLADASW::InputSemantic::Semantics semantic,
205                                          std::vector<float> &values,
206                                          const std::string &anim_id,
207                                          const std::string axis_name);
208
209   /* Output sources (matrix data) */
210   std::string collada_source_from_values(BCMatrixSampleMap &samples,
211                                          const std::string &anim_id,
212                                          BC_global_rotation_type global_rotation_type,
213                                          Matrix &parentinv);
214
215   /* Interpolation sources */
216   std::string collada_linear_interpolation_source(int tot, const std::string &anim_id);
217
218   /* source ID = animation_name + semantic_suffix */
219
220   std::string get_semantic_suffix(COLLADASW::InputSemantic::Semantics semantic);
221
222   void add_source_parameters(COLLADASW::SourceBase::ParameterNameList &param,
223                              COLLADASW::InputSemantic::Semantics semantic,
224                              bool is_rot,
225                              const std::string axis,
226                              bool transform);
227
228   int get_point_in_curve(BCBezTriple &bezt,
229                          COLLADASW::InputSemantic::Semantics semantic,
230                          bool is_angle,
231                          float *values);
232   int get_point_in_curve(const BCAnimationCurve &curve,
233                          float sample_frame,
234                          COLLADASW::InputSemantic::Semantics semantic,
235                          bool is_angle,
236                          float *values);
237
238   std::string collada_tangent_from_curve(COLLADASW::InputSemantic::Semantics semantic,
239                                          BCAnimationCurve &curve,
240                                          const std::string &anim_id,
241                                          const std::string axis_name);
242
243   std::string collada_interpolation_source(const BCAnimationCurve &curve,
244                                            const std::string &anim_id,
245                                            std::string axis_name,
246                                            bool *has_tangents);
247
248   std::string get_axis_name(std::string channel, int id);
249   const std::string get_collada_name(std::string channel_target) const;
250   std::string get_collada_sid(const BCAnimationCurve &curve, const std::string axis_name);
251
252   /* ===================================== */
253   /* Currently unused or not (yet?) needed */
254   /* ===================================== */
255
256   bool is_bone_deform_group(Bone *bone);
257
258 #if 0
259   BC_animation_transform_type _get_transform_type(const std::string path);
260   void get_eul_source_for_quat(std::vector<float> &cache, Object *ob);
261 #endif
262
263 #ifdef WITH_MORPH_ANIMATION
264   void export_morph_animation(Object *ob, BCAnimationSampler &sampler);
265 #endif
266 };
267
268 #endif