71d7d14a1123161debedc211fcea11f4d9e72488
[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 class AnimationExporter : COLLADASW::LibraryAnimations {
90  private:
91   BlenderContext &blender_context;
92   COLLADASW::StreamWriter *sw;
93   const ExportSettings *export_settings;
94
95  public:
96   AnimationExporter(BlenderContext &blender_context,
97                     COLLADASW::StreamWriter *sw,
98                     const ExportSettings *export_settings)
99       : COLLADASW::LibraryAnimations(sw),
100         blender_context(blender_context),
101         sw(sw),
102         export_settings(export_settings)
103   {
104   }
105
106   bool exportAnimations();
107
108   // called for each exported object
109   void operator()(Object *ob);
110
111  protected:
112   void export_object_constraint_animation(Object *ob);
113
114   void export_morph_animation(Object *ob);
115
116   void write_bone_animation_matrix(Object *ob_arm, Bone *bone);
117
118   void write_bone_animation(Object *ob_arm, Bone *bone);
119
120   void sample_and_write_bone_animation(Object *ob_arm, Bone *bone, int transform_type);
121
122   void sample_and_write_bone_animation_matrix(Object *ob_arm, Bone *bone);
123
124   void sample_animation(float *v,
125                         std::vector<float> &frames,
126                         int type,
127                         Bone *bone,
128                         Object *ob_arm,
129                         bPoseChannel *pChan);
130
131   void sample_animation(std::vector<float[4][4]> &mats,
132                         std::vector<float> &frames,
133                         Bone *bone,
134                         Object *ob_arm,
135                         bPoseChannel *pChan);
136
137   // dae_bone_animation -> add_bone_animation
138   // (blend this into dae_bone_animation)
139   void dae_bone_animation(std::vector<float> &fra,
140                           float *v,
141                           int tm_type,
142                           int axis,
143                           std::string ob_name,
144                           std::string bone_name);
145
146   void dae_baked_animation(std::vector<float> &fra, Object *ob_arm, Bone *bone);
147
148   void dae_baked_object_animation(std::vector<float> &fra, Object *ob);
149
150   float convert_time(float frame);
151
152   float convert_angle(float angle);
153
154   std::vector<std::vector<std::string>> anim_meta;
155
156   /* Main entry point into Animation export (called for each exported object) */
157   void exportAnimation(Object *ob, BCAnimationSampler &sampler);
158
159   /* export animation as separate trans/rot/scale curves */
160   void export_curve_animation_set(Object *ob, BCAnimationSampler &sampler, bool export_tm_curves);
161
162   /* export one single curve */
163   void export_curve_animation(Object *ob, BCAnimationCurve &curve);
164
165   /* export animation as matrix data */
166   void export_matrix_animation(Object *ob, BCAnimationSampler &sampler);
167
168   /* step through the bone hierarchy */
169   void export_bone_animations_recursive(Object *ob_arm, Bone *bone, BCAnimationSampler &sampler);
170
171   /* Export for one bone */
172   void export_bone_animation(Object *ob, Bone *bone, BCFrames &frames, BCMatrixSampleMap &outmats);
173
174   /* call to the low level collada exporter */
175   void export_collada_curve_animation(std::string id,
176                                       std::string name,
177                                       std::string target,
178                                       std::string axis,
179                                       BCAnimationCurve &curve);
180
181   /* call to the low level collada exporter */
182   void export_collada_matrix_animation(std::string id,
183                                        std::string name,
184                                        std::string target,
185                                        BCFrames &frames,
186                                        BCMatrixSampleMap &outmats);
187
188   BCAnimationCurve *get_modified_export_curve(Object *ob,
189                                               BCAnimationCurve &curve,
190                                               BCAnimationCurveMap &curves);
191
192   /* Helper functions */
193   void openAnimationWithClip(std::string id, std::string name);
194   bool open_animation_container(bool has_container, Object *ob);
195   void close_animation_container(bool has_container);
196
197   /* Input and Output sources (single valued) */
198   std::string collada_source_from_values(BC_animation_source_type tm_channel,
199                                          COLLADASW::InputSemantic::Semantics semantic,
200                                          std::vector<float> &values,
201                                          const std::string &anim_id,
202                                          const std::string axis_name);
203
204   /* Output sources (matrix data) */
205   std::string collada_source_from_values(BCMatrixSampleMap &samples, const std::string &anim_id);
206
207   /* Interpolation sources */
208   std::string collada_linear_interpolation_source(int tot, const std::string &anim_id);
209
210   /* source ID = animation_name + semantic_suffix */
211
212   std::string get_semantic_suffix(COLLADASW::InputSemantic::Semantics semantic);
213
214   void add_source_parameters(COLLADASW::SourceBase::ParameterNameList &param,
215                              COLLADASW::InputSemantic::Semantics semantic,
216                              bool is_rot,
217                              const std::string axis,
218                              bool transform);
219
220   int get_point_in_curve(BCBezTriple &bezt,
221                          COLLADASW::InputSemantic::Semantics semantic,
222                          bool is_angle,
223                          float *values);
224   int get_point_in_curve(const BCAnimationCurve &curve,
225                          float sample_frame,
226                          COLLADASW::InputSemantic::Semantics semantic,
227                          bool is_angle,
228                          float *values);
229
230   std::string collada_tangent_from_curve(COLLADASW::InputSemantic::Semantics semantic,
231                                          BCAnimationCurve &curve,
232                                          const std::string &anim_id,
233                                          const std::string axis_name);
234
235   std::string collada_interpolation_source(const BCAnimationCurve &curve,
236                                            const std::string &anim_id,
237                                            std::string axis_name,
238                                            bool *has_tangents);
239
240   std::string get_axis_name(std::string channel, int id);
241   const std::string get_collada_name(std::string channel_target) const;
242   std::string get_collada_sid(const BCAnimationCurve &curve, const std::string axis_name);
243
244   /* ===================================== */
245   /* Currently unused or not (yet?) needed */
246   /* ===================================== */
247
248   bool is_bone_deform_group(Bone *bone);
249
250 #if 0
251   BC_animation_transform_type _get_transform_type(const std::string path);
252   void get_eul_source_for_quat(std::vector<float> &cache, Object *ob);
253 #endif
254
255 #ifdef WITH_MORPH_ANIMATION
256   void export_morph_animation(Object *ob, BCAnimationSampler &sampler);
257 #endif
258 };
259
260 #endif