Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
Model.cpp
Go to the documentation of this file.
1 #include "Engine/Model/Model.hpp"
2 #include "Engine/Mesh/Mesh.hpp"
3 #include "Engine/engine.hpp"
4 #include <ThirdParty/EASTL-master/include/EASTL/string.h>
5 
6 namespace Engine
7 {
8  Model::Model(const aiScene* scene, eastl::string name)
9  {
10  if (name == "")
11  name = name + std::to_string(Engine::GetEngine().lock()->GetResourceManager().lock()->loadedModels_.size()).c_str();
12  this->name = name;
13  this->speed = 1.f;
14  this->paused = true;
15  this->time = 0.f;
16  this->looping = true;
17  this->currentAnimation = -1;
18  }
19 
20  eastl::vector<eastl::shared_ptr<Mesh>>& Model::GetModelMeshes()
21  {
22  return meshes;
23  }
24 
25  eastl::string Model::GetName()
26  {
27  return name;
28  }
29 
30  void Model::SetSkeleton(eastl::shared_ptr<Skeleton> skeleton)
31  {
32  this->skeleton = skeleton;
33  }
34 
35  eastl::shared_ptr<Skeleton> Model::GetSkeleton() const
36  {
37  return skeleton;
38  }
39 
40  eastl::vector<eastl::string> Model::GetAnimations()
41  {
42  if (skeleton != nullptr)
43  return skeleton->GetAnimations();
44  else
45  return eastl::vector<eastl::string>();
46  }
47 
48  void Model::SetAnimation(eastl::string animation, bool resetTime)
49  {
50  if (skeleton != nullptr) {
51  size_t index = skeleton->GetAnimationIndex(animation);
52  if (index != -1) {
53  currentAnimation = index;
54  currentAnimationName = animation;
55 
56  if (resetTime) {
57  this->time = 0.f;
58  }
59  }
60 
61  }
62  }
63 
65  {
66  currentAnimation = -1;
68  }
69 
71  {
72  this->time = time;
73  }
74 
75  void Model::Update(float deltaTime)
76  {
77 
78  }
79 
80  void Model::UpdateAnimation(float deltaTime)
81  {
82  if (!paused&&skeleton != nullptr && currentAnimation != -1) {
83  this->time += deltaTime * speed;
84  if (this->time > skeleton->GetAnimationDuration(currentAnimation)) {
85  if (looping)
86  this->time = fmodf(this->time, skeleton->GetAnimationDuration(currentAnimation));
87  else
88  this->time = skeleton->GetAnimationDuration(currentAnimation);
89  }
90  }
91  }
92 
94  {
95  return this->time;
96  }
97 
99  {
100  if (skeleton != nullptr && currentAnimation != -1) {
101  return skeleton->GetAnimationDuration(currentAnimation);
102  }
103  return 0.0f;
104  }
105 
107  {
108  this->paused = paused;
109  }
110 
111  bool Model::IsPaused() const
112  {
113  return this->paused;
114  }
115 
116  bool Model::HasAnimations() const
117  {
118  if (skeleton == nullptr)
119  return false;
120  return skeleton->HasAnimations();
121  }
122 
124  {
125  this->looping = looping;
126  }
127 
128  bool Model::IsLooping() const
129  {
130  return this->looping;
131  }
132 
133  void Model::SetSpeed(float speed)
134  {
135  this->speed = speed;
136  }
137 
138  float Model::GetSpeed() const
139  {
140  return this->speed;
141  }
142 
143  eastl::string Model::GetCurrentAnimationName() const
144  {
145  return currentAnimationName;
146  }
147 
149  {
150  return currentAnimation;
151  }
152 
153  eastl::shared_ptr<Material> Model::GetMeshMaterial(eastl::shared_ptr<Mesh> mesh)
154  {
155  if (meshMaterialMap.count(mesh) > 0)
156  return meshMaterialMap[mesh];
157  else
158  return nullptr;
159  }
160 
161  void Model::SetMeshMaterial(eastl::shared_ptr<Mesh> mesh, eastl::shared_ptr<Material> material)
162  {
163  meshMaterialMap[mesh] = material;
164  }
165 
167  {
168  if (name != other.name) return false;
169 
170  size_t thisMeshCount = meshes.size();
171  size_t otherMeshCount = other.meshes.size();
172 
173  if (thisMeshCount != otherMeshCount) return false;
174 
175  for (size_t i = 0; i < thisMeshCount; ++i)
176  {
177  if (meshes[i] != other.meshes[i]) return false;
178  }
179 
180  for each (eastl::pair<eastl::shared_ptr<Mesh>, eastl::shared_ptr<Material>> mat in meshMaterialMap)
181  {
182  if (other.meshMaterialMap[mat.first] != mat.second) return false;
183  }
184 
185  if (other.skeleton != skeleton) return false;
186 
187  return true;
188  }
189 
191  {
192  if (name != other.name) return true;
193 
194  size_t thisMeshCount = meshes.size();
195  size_t otherMeshCount = other.meshes.size();
196 
197  if (thisMeshCount != otherMeshCount) return true;
198 
199  for (size_t i = 0; i < thisMeshCount; ++i)
200  {
201  if (meshes[i] != other.meshes[i]) return true;
202  }
203 
204  for each (eastl::pair<eastl::shared_ptr<Mesh>, eastl::shared_ptr<Material>> mat in meshMaterialMap)
205  {
206  if (other.meshMaterialMap[mat.first] != mat.second) return true;
207  }
208 
209  if (other.skeleton != skeleton) return true;
210 
211  return false;
212  }
213 
214  void Model::AddMesh(eastl::shared_ptr<Mesh> meshToAdd)
215  {
216  meshes.push_back(meshToAdd);
217  }
218 } // namespace Engine
eastl::vector< eastl::string > GetAnimations()
Returns a list of the loaded animations as a vector of names. Use these names to load a specific anim...
Definition: Model.cpp:40
eastl::shared_ptr< Skeleton > GetSkeleton() const
Returns the animation data of the model. The animation data contains the skeleton of the model...
Definition: Model.cpp:35
eastl::string currentAnimationName
Definition: Model.hpp:201
eastl::string GetCurrentAnimationName() const
Returns the name of the currently selected animation.
Definition: Model.cpp:143
void SetPaused(bool paused)
Pauses or unpauses the animation.
Definition: Model.cpp:106
void SetAnimation(eastl::string animation, bool resetTime)
Sets the current animation of the mesh.
Definition: Model.cpp:48
void SetLooping(bool looping)
Whether or not the animation should loop after finishing.
Definition: Model.cpp:123
float GetAnimationTime() const
Returns the current progress of the animation.
Definition: Model.cpp:93
eastl::vector< eastl::shared_ptr< Mesh > > & GetModelMeshes()
This method allows you to get a reference of all the meshes in this model.
Definition: Model.cpp:20
bool IsPaused() const
Tells if the animation is paused or not.
Definition: Model.cpp:111
void SetSkeleton(eastl::shared_ptr< Skeleton > skeleton)
Definition: Model.cpp:30
Model(const aiScene *scene, eastl::string name="")
Definition: Model.cpp:8
void SetSpeed(float speed)
Set the speed modifier of the animation. Default is 1.f.
Definition: Model.cpp:133
float GetAnimationDuration() const
Returns the duration of a single loop of the animation.
Definition: Model.cpp:98
eastl::string name
Definition: Model.hpp:175
size_t GetCurrentAnimationIndex() const
Returns the index of the currently selected animation.
Definition: Model.cpp:148
This object is a storage container for meshes.
Definition: Model.hpp:15
void UpdateAnimation(float deltaTime)
Updates animation related values, such as the progress of the animation.
Definition: Model.cpp:80
void Update(float deltaTime)
Updates the model (doesn&#39;t do anything right now).
Definition: Model.cpp:75
eastl::string GetName()
This method allows you to get the name of this model.
Definition: Model.cpp:25
float speed
Definition: Model.hpp:191
void SetMeshMaterial(eastl::shared_ptr< Mesh > mesh, eastl::shared_ptr< Material > material)
Associates a material with the given mesh. Warning: If building for vulkan, make sure that the specif...
Definition: Model.cpp:161
bool looping
Definition: Model.hpp:193
float time
Definition: Model.hpp:197
eastl::shared_ptr< Skeleton > skeleton
Definition: Model.hpp:182
bool operator==(Model &other)
This method allows you to compare a model against another model.
Definition: Model.cpp:166
bool IsLooping() const
Returns a boolean indicating if the animation is looping or not.
Definition: Model.cpp:128
bool paused
Definition: Model.hpp:195
eastl::vector< eastl::shared_ptr< Mesh > > meshes
Definition: Model.hpp:185
eastl::map< eastl::shared_ptr< Mesh >, eastl::shared_ptr< Material > > meshMaterialMap
Definition: Model.hpp:187
eastl::shared_ptr< Material > GetMeshMaterial(eastl::shared_ptr< Mesh > mesh)
Returns the material used by this model for the specified mesh. If the model doesn&#39;t contain a materi...
Definition: Model.cpp:153
size_t currentAnimation
Definition: Model.hpp:199
void SetAnimationTime(float time)
Sets the point in the animation the model is in. Set to 0 to reset the animation. Animation automatic...
Definition: Model.cpp:70
bool operator!=(Model &other)
This method allows you to compare a model against another model.
Definition: Model.cpp:190
void ResetAnimation()
Sets the current animation to 0, returning the model to the default pose.
Definition: Model.cpp:64
void AddMesh(eastl::shared_ptr< Mesh > meshToAdd)
Definition: Model.cpp:214
bool HasAnimations() const
Whether or not any animations have been loaded.
Definition: Model.cpp:116
static eastl::weak_ptr< Engine > GetEngine() noexcept
This method allows you to get the instance of the Engine. This method will automatically initialize t...
Definition: engine.cpp:130
float GetSpeed() const
Returns the speed modifier of the current animation.
Definition: Model.cpp:138