AnimEngine
GltfScenePopulator.hpp
1 #pragma once
2 #ifndef KJY_ANIM_ENGINE_GLTF_SCENE_POPULATOR_HPP_
3 #define KJY_ANIM_ENGINE_GLTF_SCENE_POPULATOR_HPP_
4 
5 #include "GltfCommon.h"
6 #include "world/Entities/TypicalEntities.inl"
7 #include <world/Scene.hpp>
8 
9 /* TinyGltf objects don't contain that much so duplicating them shouldn't
10  * involve to much overhead */
11 template<typename T>
12 struct AugmentedGltfObject : public T{
13  AugmentedGltfObject(int index, const T& t) : original_index(index), T(t) {}
14  int original_index = -1;
15 };
16 
18 
19 
21  std::string owner;
22  int target_node;
23  std::string path;
24  PoseKeyframeSequence sequence;
25 };
26 
28  std::string name;
29  std::vector<GltfPoseKeyframeSequence> channels;
30  bool start_enabled = true;
31  bool should_loop = false;
32 };
33 
34 class ExtrasExtension;
35 
36 /* TODO list:
37  * 1. Support multiple scenes and non-default scenes.
38  * 6. Shape keys
39  */
41  public:
42  GltfScenePopulator(Scene* scene, const tinygltf::Model& model, const std::string& source_name = std::string());
43 
44  void populate();
45 
46  void addExtension(ExtrasExtension* aExtension) {mExtensions.push_back(aExtension);}
47 
48  static bool node_has_pose(const tinygltf::Node& node);
49  static Pose get_node_pose(const tinygltf::Node& node);
50 
51  const std::map<int, Entity*>& getGltfnodesToEntites() const {return(gltfnodes_to_entites);}
52  const std::map<int, MeshEntity*>& getGltfmeshToMeshes() const {return(gltfmesh_to_meshes);}
53  const std::map<int, Skeleton*>& getGltfskinsToSkeletons() const {return(gltfskins_to_skeletons);}
54  const std::map<int, Bone*>& getGltfnodeToBones() const {return(gltfnode_to_bones);}
55  const std::map<int, GltfMetallicRoughness*>& getGltfmaterialsToMaterials() const {return(gltfmaterials_to_materials);}
56  const std::map<int, SimpleTexture2D>& getGltftexturesToTextures() const {return(gltftextures_to_textures);}
57  const std::unordered_map<std::string, GltfAnimation>& getGltfanimToAnimations() const {return(gltfanim_to_animations);}
58  const std::unordered_map<std::string, GltfAnimation>& getSkeletonIsolatedAnimations() const {return(skeleton_isolated_animations);}
59 
60  /* Get the root entity of all content loaded from glTF file */
61  EmptyEntity* sceneRoot();
62 
63  protected:
64  friend class ExtrasExtension;
65 
66  Entity* traverse_node(int node_index, Entity* parent = nullptr);
67  BaseCameraEntity* process_camera_entity(const AugNode& node);
68  SkinnedMeshEntity* process_skinned_mesh_entity(const AugNode& node);
69  SolidMeshEntity* process_mesh_entity(const AugNode& node);
70  Skeleton* construct_skeleton_from_skin(int skin_index);
71  void process_animation(const tinygltf::Animation& animation);
72  void populate_scene_animators();
73  void process_pose_animation_channel(const tinygltf::AnimationChannel& channel, const tinygltf::AnimationSampler& sampler, GltfPoseKeyframeSequence& output_sequence);
74  GltfPoseKeyframeSequence process_animation_channel(const tinygltf::AnimationChannel& channel, const tinygltf::AnimationSampler& sampler);
75  GltfMetallicRoughness* process_metallic_roughness_material(int material_index);
76  SimpleTexture2D process_texture(int texture_index);
77  void isolate_bone_animations(const GltfAnimation& animation, const std::vector<Bone*>& touched_bones);
78  Skeleton* find_bone_skeleton(Bone* bone);
79 
80  void printInfo(const std::string& info);
81  void printWarning(const std::string& info);
82 
83  std::vector<ExtrasExtension*> mExtensions;
84 
85  Scene* _scene = nullptr;
86  EmptyEntity* scene_root = nullptr;
87  const tinygltf::Model& _asset;
88  const std::string _source_identifier;
89 
90  std::map<int, Entity*> gltfnodes_to_entites;
91  std::map<int, MeshEntity*> gltfmesh_to_meshes;
92  std::map<int, Skeleton*> gltfskins_to_skeletons;
93  std::map<int, Bone*> gltfnode_to_bones;
94  std::map<int, GltfMetallicRoughness*> gltfmaterials_to_materials;
95  std::map<int, SimpleTexture2D> gltftextures_to_textures;
96  std::unordered_map<std::string, GltfAnimation> gltfanim_to_animations;
97  std::unordered_map<std::string, GltfAnimation> skeleton_isolated_animations;
98 
99  std::map<int, BaseCameraEntity*> gltfcam_to_camera_entity; // TODO
100  // std::map<int, Light*> gltflight_to_lights; // TODO
101  // std::map<int, GltfMetallicRoughness> converted_materials; // TODO
102  // std::map<int, SimpleTexture2D> gltftex_to_textures; // TODO
103 };
104 
105 enum class GltfLoadStep {
106  TRAVERSE_NODE = 0,
107  PROCESS_MESH_ENT,
108  PROCESS_SKIN_MESH_ENT,
109  PROCESS_CAMERA_ENT,
110  PROCESS_ANIM,
111  PROCESS_PBRMAT,
112  PROCESS_TEX,
113  _GLTF_LOAD_STEP_TOTAL
114 };
115 
116 enum class ExtensionResultHint{
117  CONTINUE = 0,
118  FINISH_NOW,
119  DISCARD
120 };
121 
123 {
124  public:
125  virtual ExtensionResultHint preProcessSceneCallback(GltfScenePopulator& aPopulator, const tinygltf::Scene& aSceneIn, Scene* aSceneOut) {return ExtensionResultHint::CONTINUE;}
126  virtual ExtensionResultHint postProcessSceneCallback(GltfScenePopulator& aPopulator, const tinygltf::Scene& aSceneIn, Scene* aSceneOut) {return ExtensionResultHint::CONTINUE;}
127 
128  virtual ExtensionResultHint preTraverseNodeCallback(GltfScenePopulator& aPopulator, const AugNode& aNode, Entity* aParent, Entity*& aResult) {return ExtensionResultHint::CONTINUE;}
129  virtual ExtensionResultHint postTraverseNodeCallback(GltfScenePopulator& aPopulator, const AugNode& aNode, Entity* aParent, Entity*& aResult) {return ExtensionResultHint::CONTINUE;}
130 
131  virtual ExtensionResultHint preProcessMeshEntityCallback(GltfScenePopulator& aPopulator, const AugNode& aNode, SolidMeshEntity*& aMesh) {return ExtensionResultHint::CONTINUE;}
132  virtual ExtensionResultHint postProcessMeshEntityCallback(GltfScenePopulator& aPopulator, const AugNode& aNode, SolidMeshEntity*& aMesh) {return ExtensionResultHint::CONTINUE;}
133 
134  virtual ExtensionResultHint preProcessSkinMeshEntityCallback(GltfScenePopulator& aPopulator, const AugNode& aNode, SkinnedMeshEntity*& aSkin) {return ExtensionResultHint::CONTINUE;}
135  virtual ExtensionResultHint postProcessSkinMeshEntityCallback(GltfScenePopulator& aPopulator, const AugNode& aNode, SkinnedMeshEntity*& aSkin) {return ExtensionResultHint::CONTINUE;}
136 
137  virtual ExtensionResultHint preProcessCameraEntityCallback(GltfScenePopulator& aPopulator, const AugNode& aNode, LookAtCamera*& aCamera) {return ExtensionResultHint::CONTINUE;}
138  virtual ExtensionResultHint postProcessCameraEntityCallback(GltfScenePopulator& aPopulator, const AugNode& aNode, LookAtCamera*& aCamera) {return ExtensionResultHint::CONTINUE;}
139 
140  virtual ExtensionResultHint preProcessAnimationCallback(GltfScenePopulator& aPopulator, const tinygltf::Animation& aAnimIn, std::vector<Bone*>& aTouchedBones, GltfAnimation& aAnimResult) {return ExtensionResultHint::CONTINUE;}
141  virtual ExtensionResultHint postProcessAnimationCallback(GltfScenePopulator& aPopulator, const tinygltf::Animation& aAnimIn, std::vector<Bone*>& aTouchedBones, GltfAnimation& aAnimResult) {return ExtensionResultHint::CONTINUE;}
142 
143  virtual ExtensionResultHint preProcessPbrMaterialCallback(GltfScenePopulator& aPopulator, const tinygltf::Material& aMaterialIn, GltfMetallicRoughness*& aMaterialOut) {return ExtensionResultHint::CONTINUE;}
144  virtual ExtensionResultHint postProcessPbrMaterialCallback(GltfScenePopulator& aPopulator, const tinygltf::Material& aMaterialIn, GltfMetallicRoughness*& aMaterialOut) {return ExtensionResultHint::CONTINUE;}
145 
146  virtual ExtensionResultHint preProcessTextureCallback(GltfScenePopulator& aPopulator, const tinygltf::Texture& aTexture, const tinygltf::Image& aImage, SimpleTexture2D& aTextureOut) {return ExtensionResultHint::CONTINUE;}
147  virtual ExtensionResultHint postProcessTextureCallback(GltfScenePopulator& aPopulator, const tinygltf::Texture& aTexture, const tinygltf::Image& aImage, SimpleTexture2D& aTextureOut) {return ExtensionResultHint::CONTINUE;}
148 
149  virtual ExtensionResultHint preProcessPopulateAnimatorsCallback(GltfScenePopulator& aPopulator, std::unordered_map<std::string, GltfAnimation>& aAnimationsIn, Scene* aSceneOut) {return ExtensionResultHint::CONTINUE;}
150  virtual ExtensionResultHint postProcessPopulateAnimatorsCallback(GltfScenePopulator& aPopulator, std::unordered_map<std::string, GltfAnimation>& aAnimationsIn, Scene* aSceneOut) {return ExtensionResultHint::CONTINUE;}
151 
152  protected:
153  static const tinygltf::Model& getAsset(const GltfScenePopulator& aPopulator) {
154  return(aPopulator._asset);
155  }
156 };
157 
158 
159 #endif
Definition: GltfScenePopulator.hpp:40
Definition: GltfScenePopulator.hpp:27
Definition: Skeleton.hpp:15
Definition: GltfScenePopulator.hpp:122
Definition: SkinnedMeshEntity.hpp:55
Definition: Texture.hpp:60
Definition: SolidMeshEntity.hpp:31
Definition: GltfScenePopulator.hpp:12
Definition: CameraEntities.hpp:21
Definition: EntitiesBase.hpp:74
Definition: GltfScenePopulator.hpp:20
Definition: EntitiesBase.hpp:18
Definition: CameraEntities.hpp:63
Definition: Scene.hpp:12
Definition: Pose.hpp:11
Definition: Material.hpp:101
Definition: KeyframeAnimation.hpp:153
Definition: Skeleton.hpp:72