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 private:
91  template <typename T>
92  struct ChannelInfo
93  {
94  bool using_global_key_times = false;
95  UT_Array<fpreal32> key_times;
97  bool time_dependent = false;
98  GLTF_Interpolation interpolation = INTERP_NONE;
99  };
100 
101  struct TransformAnimationInfo
102  {
103  GLTF_Handle node_handle = GLTF_INVALID_IDX;
104  ChannelInfo<UT_Vector3F> translation;
105  ChannelInfo<UT_QuaternionF> rotation;
106  ChannelInfo<UT_Vector3F> scale;
107  };
108 
109  struct GeoAnimationInfo
110  {
111  GLTF_Handle node_handle = GLTF_INVALID_IDX;
112  GU_ConstDetailHandle cooked_gdh;
113  GLTF_RefinerHandle refiner;
114  bool added_first_frame = false;
115  bool using_global_sample_times = true;
116  UT_Array<fpreal32> sample_times;
117  };
118 
119  // Returns true iff the transform is animated
120  bool updateTransformInfo(OBJ_Node *node);
121  bool updateTransformInfoFromMatrix(OBJ_Node *node);
122 
123  // Helper function to fill the info struct
124  void addTRS(
125  TransformAnimationInfo &info,
126  UT_Vector3F &curr_t,
127  UT_Vector3F &curr_euler,
128  UT_Vector3F &curr_s,
129  UT_Vector3F &prev_euler,
130  exint count);
131 
132  // Determines if transform info can be extracted from parm channels instead
133  // of xform matrices
134  bool canUseTRS(OBJ_Node *node);
135 
136  template <int tuple_size, typename T>
137  void updateChannels(
138  ChannelInfo<T> &info,
139  const PRM_Parm *parm,
140  const int thread,
141  bool is_rotation = false);
142 
143  // Helper functions for adding animation channels
144  void addChannel(
145  GLTF_ExportRoot &root,
147  GLTF_Handle out_index,
148  GLTF_Handle node_handle,
149  GLTF_Interpolation interpolation,
150  GLTF_Handle in_override = GLTF_INVALID_IDX);
151  void addTransformChannels(
152  GLTF_ExportRoot &root,
153  const TransformAnimationInfo &info,
154  GLTF_Handle times_index = GLTF_INVALID_IDX);
155 
157  // To keep ordering consistent
158  UT_Array<OBJ_Node *> myNodes;
159 
161  UT_Array<SOP_Node *> myGeos;
162 
163  // The input array for all animation samples
164  UT_Array<fpreal32> myTimeSamples;
165 
166  GLTF_Handle myAnimation;
167  GLTF_Handle myTimesAccessor;
168 
169  GLTF_BaseErrorManager* myErrorManager;
170 };
171 
172 } // namespace GLTF_NAMESPACE
173 
174 #endif // __GLTF_ANIMATOR_H__
GLenum GLenum GLenum GLenum GLenum scale
Definition: glew.h:14163
GT_API const UT_StringHolder time
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
uint32 GLTF_Handle
Definition: GLTF_Types.h:48
int64 exint
Definition: SYS_Types.h:125
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1601
#define GLTF_NAMESPACE
Definition: GLTF_API.h:42
GLint GLsizei count
Definition: glcorearb.h:404
#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
fpreal64 fpreal
Definition: SYS_Types.h:277
GLuint index
Definition: glcorearb.h:785
#define GLTFZ_API
Definition: GLTFZ_API.h:37
MatType rotation(const Quat< typename MatType::value_type > &q, typename MatType::value_type eps=static_cast< typename MatType::value_type >(1.0e-8))
Return the rotation matrix specified by the given quaternion.
Definition: Mat.h:194