HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GLTF_Animator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020
3  * Side Effects Software Inc. All rights reserved.
4  *
5  * Redistribution and use of Houdini Development Kit samples in source and
6  * binary forms, with or without modification, are permitted provided that the
7  * following conditions are met:
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. The name of Side Effects Software may not be used to endorse or
11  * promote products derived from this software without specific prior
12  * written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY SIDE EFFECTS SOFTWARE `AS IS' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17  * NO EVENT SHALL SIDE EFFECTS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
20  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef __GLTF_ANIMATOR_H__
27 #define __GLTF_ANIMATOR_H__
28 
29 #include "GLTFZ_API.h"
30 
31 #include <GLTF/GLTF_Types.h>
32 #include <GLTFZ/GLTF_ExportRoot.h>
33 #include <GLTFZ/GLTF_Refiner.h>
34 #include <GU/GU_DetailHandle.h>
35 #include <OBJ/OBJ_Node.h>
36 #include <SOP/SOP_Node.h>
37 #include <UT/UT_Array.h>
38 #include <UT/UT_Map.h>
39 
40 namespace GLTF_NAMESPACE
41 {
42 
43 // Helper class for exporting GLTF animations
45 {
46 public:
47  /*
48  struct AnimatorSettings
49  {
50  fpreal tstart, tend;
51  };
52  */
53  // For when the settings struct is used in the near future
54  // GLTF_Animator(AnimatorSettings &settings);
55 
56  ~GLTF_Animator() = default;
57  GLTF_Animator(GLTF_BaseErrorManager* error_manager);
58 
59  void registerNode(OBJ_Node *node, GLTF_Handle index);
60 
61  void registerGeo(
62  SOP_Node *sop,
63  GU_ConstDetailHandle& cooked_gdh,
64  const GLTF_Handle node_handle,
65  const GLTF_RefinerHandle &refiner);
66 
67  void registerGeo(
68  SOP_Node* sop,
69  const GLTF_Handle node_handle,
70  const GLTF_RefinerHandle& refiner);
71 
72  bool addKeyFrame(fpreal time);
73 
74  // Outputs stored information (OBJ and SOP animations) to the ExportRoot
75  void outputToRoot(GLTF_ExportRoot &root);
76 
77  // Applies transform animations to the node
78  // There must be one transform in xforms per time sample
79  // Time samples are added beforehand with addKeyFrame()
80  void animateNodeTransform(
81  GLTF_ExportRoot &root,
82  GLTF_Handle node_handle,
83  const UT_Array<UT_Matrix4D> &xforms);
84 
85  void animateMorphTargetWeights(
86  GLTF_ExportRoot &root,
87  GLTF_Handle node_handle,
88  GLTF_Handle weights_handle);
89 
90  void setAnimationName(const UT_StringHolder& anim_name);
91 
92 private:
93  template <typename T>
94  struct ChannelInfo
95  {
96  bool using_global_key_times = false;
97  UT_Array<fpreal32> key_times;
99  bool time_dependent = false;
100  GLTF_Interpolation interpolation = INTERP_NONE;
101  };
102 
103  struct TransformAnimationInfo
104  {
105  GLTF_Handle node_handle = GLTF_INVALID_IDX;
106  ChannelInfo<UT_Vector3F> translation;
107  ChannelInfo<UT_QuaternionF> rotation;
108  ChannelInfo<UT_Vector3F> scale;
109  };
110 
111  struct GeoAnimationInfo
112  {
113  GLTF_Handle node_handle = GLTF_INVALID_IDX;
114  GU_ConstDetailHandle cooked_gdh;
115  GLTF_RefinerHandle refiner;
116  bool added_first_frame = false;
117  bool using_global_sample_times = true;
118  UT_Array<fpreal32> sample_times;
119  };
120 
121  // Returns true iff the transform is animated
122  bool updateTransformInfo(OBJ_Node *node);
123  bool updateTransformInfoFromMatrix(OBJ_Node *node);
124 
125  // Helper function to fill the info struct
126  void addTRS(
127  TransformAnimationInfo &info,
128  UT_Vector3F &curr_t,
129  UT_Vector3F &curr_euler,
130  UT_Vector3F &curr_s,
131  UT_Vector3F &prev_euler,
132  exint count);
133 
134  // Determines if transform info can be extracted from parm channels instead
135  // of xform matrices
136  bool canUseTRS(OBJ_Node *node);
137 
138  template <int tuple_size, typename T>
139  void updateChannels(
140  ChannelInfo<T> &info,
141  const PRM_Parm *parm,
142  const int thread,
143  bool is_rotation = false);
144 
145  // Helper functions for adding animation channels
146  void addChannel(
147  GLTF_ExportRoot &root,
149  GLTF_Handle out_index,
150  GLTF_Handle node_handle,
151  GLTF_Interpolation interpolation,
152  GLTF_Handle in_override = GLTF_INVALID_IDX);
153  void addTransformChannels(
154  GLTF_ExportRoot &root,
155  const TransformAnimationInfo &info,
156  GLTF_Handle times_index = GLTF_INVALID_IDX);
157 
159  // To keep ordering consistent
161 
163  UT_Array<SOP_Node *> myGeos;
164 
165  // The input array for all animation samples
166  UT_Array<fpreal32> myTimeSamples;
167 
168  GLTF_Handle myAnimation;
169  GLTF_Handle myTimesAccessor;
170 
171  GLTF_BaseErrorManager* myErrorManager;
172 
173  UT_StringHolder myAnimationName;
174 };
175 
176 } // namespace GLTF_NAMESPACE
177 
178 #endif // __GLTF_ANIMATOR_H__
myNodes
Definition: UT_RTreeImpl.h:708
GT_API const UT_StringHolder time
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
uint32 GLTF_Handle
Definition: GLTF_Types.h:48
int64 exint
Definition: SYS_Types.h:125
GA_API const UT_StringHolder scale
#define GLTF_NAMESPACE
Definition: GLTF_API.h:42
SIM_API const UT_StringHolder rotation
#define GLTF_INVALID_IDX
Definition: GLTF_Types.h:44
**Note that the tasks the is the thread number *for the or if it s being executed by a non pool thread(this *can happen in cases where the whole pool is occupied and the calling *thread contributes to running the work load).**Thread pool.Have fun
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
fpreal64 fpreal
Definition: SYS_Types.h:277
GLuint index
Definition: glcorearb.h:786
#define GLTFZ_API
Definition: GLTFZ_API.h:37
GLint GLsizei count
Definition: glcorearb.h:405