Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
Material.cpp
Go to the documentation of this file.
2 #include "Engine/engine.hpp"
3 
4 #include <ThirdParty/stb/stb_image.h>
5 
6 
7 namespace Engine {
8 
9  Material::Material(const aiScene* scene, uint32_t materialIndex, eastl::string modelName)
10  {
11  if (scene->mNumMaterials <= materialIndex)
12  return;
13 
14  materialData_ = {};
15 
16  this->modelName = modelName;
17 
20 
21  aiMaterial* material = scene->mMaterials[materialIndex];
22 
23  diffuseTexture = LoadTexture(scene, material, aiTextureType_DIFFUSE);
25  if (diffuseTexture.expired() == false)
27  specularTexture = LoadTexture(scene, material, aiTextureType_SPECULAR);
29  if (specularTexture.expired() == false)
31 
32  bumpMapTexture = LoadTexture(scene, material, aiTextureType_NORMALS);
34  if (bumpMapTexture.expired() == false)
36 
37  missingTexture = Engine::GetEngine().lock()->GetResourceManager().lock()->CreateTexture("default.png");
38 
39  aiMaterial* mat = scene->mMaterials[materialIndex];
40  aiColor3D color(0.f, 0.f, 0.f);
41  if (mat->Get(AI_MATKEY_COLOR_DIFFUSE, color) != AI_SUCCESS)
42  {
43  // Couldn't find color, add failure code.
44  }
45  float transparency = 1.f;
46  if (mat->Get(AI_MATKEY_OPACITY, transparency) != AI_SUCCESS)
47  {
48  // Couldn't find transparancy, add failure code.
49  }
50  materialData_.diffuseColor = glm::vec4(color.r, color.g, color.b, transparency);
51 
52  color = aiColor3D(0.f, 0.f, 0.f);
53  if (mat->Get(AI_MATKEY_COLOR_SPECULAR, color) != AI_SUCCESS)
54  {
55  // Couldn't find color, add failure code.
56  }
57  materialData_.specularColor = glm::vec4(color.r, color.g, color.b, 0.f);
58 
59  float shiny = 0.f;
60  if (mat->Get(AI_MATKEY_SHININESS, shiny) != AI_SUCCESS)
61  {
62 
63  }
65 
66  float specScale = 1.f;
67  if (mat->Get(AI_MATKEY_SHININESS_STRENGTH, specScale) != AI_SUCCESS)
68  {
69 
70  }
71  materialData_.specularScale = specScale;
72 
74 
76  }
77 
78 
80  {
81  }
82 
83  void Material::SetDiffuseTexture(eastl::string diffuseTextureName)
84  {
85  diffuseTexture = Engine::GetEngine().lock()->GetResourceManager().lock()->CreateTexture(diffuseTextureName);
86 
87  if (materialData_.diffuseTextureLoaded != 1 && diffuseTexture.expired() == false)
88  {
91  }
92  else if (diffuseTexture.expired() == false) {
95  }
96  }
97 
98  void Material::SetDiffuseTexture(eastl::shared_ptr<Texture> diffuseTexture)
99  {
100  this->diffuseTexture = diffuseTexture;
101 
102  if (materialData_.diffuseTextureLoaded != 1 && diffuseTexture!=nullptr)
103  {
106  }
107  else if (diffuseTexture == nullptr) {
110  }
111  }
112 
113  eastl::weak_ptr<Texture> Material::GetDiffuseTexture()
114  {
115  if(diffuseTexture.expired() == false)
116  return diffuseTexture;
117 
118  return missingTexture;
119  }
120 
121  eastl::weak_ptr<Texture> Material::GetDefaultDiffuseTexture()
122  {
123  if(defaultDiffuseTexture.expired() == false)
124  return defaultDiffuseTexture;
125 
126  return missingTexture;
127  }
128 
130  {
132  }
133 
134  void Material::SetBumpMapTexture(eastl::string bumpMapTextureName)
135  {
136  bumpMapTexture = Engine::GetEngine().lock()->GetResourceManager().lock()->CreateTexture(bumpMapTextureName);
137 
138  if (materialData_.bumpMapLoaded != 1 && bumpMapTexture.expired() == false)
139  {
142  }
143  else if (bumpMapTexture.expired()) {
146  }
147  }
148 
149  void Material::SetBumpMapTexture(eastl::shared_ptr<Texture> bumpMapTexture)
150  {
151  this->bumpMapTexture = bumpMapTexture;
152 
153  if (materialData_.bumpMapLoaded != 1 && bumpMapTexture != nullptr)
154  {
157  }
158  else if (bumpMapTexture == nullptr) {
161  }
162  }
163 
164  eastl::weak_ptr<Texture> Material::GetBumpMapTexture() const
165  {
166  if (bumpMapTexture.expired() == false)
167  return bumpMapTexture;
168 
169  return missingTexture;
170  }
171 
172  eastl::weak_ptr<Texture> Material::GetDefaultBumpMapTexture() const
173  {
174  if (defaultBumpMapTexture.expired() == false)
175  return defaultBumpMapTexture;
176 
177  return missingTexture;
178  }
179 
181  {
182  return materialData_.bumpMapLoaded >= 1;
183  }
184 
185  void Material::SetSpecularTexture(eastl::shared_ptr<Texture> specularTexture)
186  {
187  this->specularTexture = specularTexture;
188 
189  if (materialData_.specularTextureLoaded != 1 && specularTexture != nullptr)
190  {
193  }
194  else if (specularTexture == nullptr) {
197  }
198  }
199 
200  eastl::weak_ptr<Texture> Material::GetSpecularTexture() const
201  {
202  return specularTexture;
203  }
204 
205  eastl::weak_ptr<Texture> Material::GetDefaultSpecularTexture() const
206  {
207  return defaultSpecularTexture;
208  }
209 
211  {
212  return static_cast<bool>(materialData_.specularTextureLoaded);
213  }
214 
215  void Material::SetDiffuseColor(glm::vec3 diffuseColor)
216  {
217  materialData_.diffuseColor = glm::vec4(diffuseColor.x, diffuseColor.y, diffuseColor.z, 1.0f);
219  }
220 
221  glm::vec3 Material::GetDiffuseColor() const
222  {
224  }
225 
227  {
229  }
230 
231  void Material::SetSpecularColor(glm::vec3 specularColor)
232  {
233  materialData_.specularColor = glm::vec4(specularColor.x, specularColor.y, specularColor.z, 1.0f);
235  }
236 
237  glm::vec3 Material::GetSpecularColor() const
238  {
240  }
241 
243  {
245  }
246 
247  void Material::SetSpecularScale(float scale)
248  {
251  }
252 
254  {
256  }
257 
259  {
261  }
262 
263  void Material::SetSpecularExponent(float exponent)
264  {
265  materialData_.specularExponent = exponent;
267  }
268 
270  {
272  }
273 
275  {
277  }
278 
279  bool Material::operator==(const Material & other) const
280  {
282  if (materialData_.specularScale != other.materialData_.specularScale) return false;
283 
284  if (materialData_.diffuseColor != other.materialData_.diffuseColor) return false;
285  if (materialData_.specularColor != other.materialData_.specularColor) return false;
286 
287  if (this->diffuseTexture.lock().get() != other.diffuseTexture.lock().get()) return false;
288  if (this->specularTexture.lock().get() != other.specularTexture.lock().get()) return false;
289 
290  return true;
291  }
292 
294  {
295  }
296 
297  eastl::weak_ptr<Texture> Material::LoadTexture(const aiScene* scene, aiMaterial * material, aiTextureType textureType)
298  {
299  if (material->GetTextureCount(textureType) > 0) { // Textures located
300  aiString path;
301  material->GetTexture(textureType, 0, &path);
302 
303  if (path.data[0] == '*') { // Texture is an embedded texture
304 
305  eastl::string name = modelName + eastl::string(path.C_Str());
306 
307  eastl::weak_ptr<Texture> ret = Engine::GetEngine().lock()->
308  GetResourceManager().lock()->GetTexture(name);
309 
310  if (ret.expired() == false)
311  return ret;
312 
313  uint32_t index = atoi(path.C_Str() + 1);
314  if (index < scene->mNumTextures)
315  {
316  aiTexture* texture = scene->mTextures[index];
317 
318  stbi_uc* textureData;
319 
320  int width, height, channels;
321 
322  if (texture->mHeight == 0)
323  { // Compressed texture
324  char* data = (char*)malloc(sizeof(char)*texture->mWidth);
325  memcpy(data, texture->pcData, texture->mWidth);
326  textureData = stbi_load_from_memory((stbi_uc*)data, texture->mWidth, &width, &height, &channels, 4);
327  }
328  else
329  {
330  textureData = (stbi_uc*)malloc(sizeof(stbi_uc)*texture->mWidth*texture->mHeight * 4);
331  for (size_t i = 0; i < texture->mWidth*texture->mHeight*4; i += 4)
332  {
333  textureData[i] = texture->pcData->r;
334  textureData[i + 1] = texture->pcData->g;
335  textureData[i + 2] = texture->pcData->b;
336  textureData[i + 3] = texture->pcData->a;
337  }
338 
339  width = texture->mWidth;
340  height = texture->mHeight;
341  channels = 4;
342  }
343 
344  return Engine::GetEngine().lock()->
345  GetResourceManager().lock()->CreateTexture(name, textureData, width, height);
346 
347  }
348  else
349  {
350  return eastl::shared_ptr<Texture>();
351  }
352  }
353  else
354  {
355  return Engine::GetEngine().lock()->GetResourceManager().lock()->CreateTexture(eastl::string(path.C_Str()));
356  }
357  }
358  else
359  {
360  return eastl::shared_ptr<Texture>();
361  }
362  }
363 
364 } // namespace Engine
MaterialData_t materialData_
Definition: Material.hpp:223
void SetSpecularExponent(float exponent)
Sets the specular exponent of the material (shinyness).
Definition: Material.cpp:263
virtual void SetDiffuseTexture(eastl::string diffuseTextureName)
Changes the diffuse texture of the material. NOTE: Texture will be loaded in, if it doesn&#39;t excist ye...
Definition: Material.cpp:83
MaterialData_t defaultMaterialData_
Definition: Material.hpp:224
virtual void SetSpecularColor(glm::vec3 specularColor)
Sets the material&#39;s specular color.
Definition: Material.cpp:231
eastl::weak_ptr< Texture > missingTexture
Definition: Material.hpp:235
float GetDefaultSpecularScale() const
Returns the scale of the specular highlights (strength) that was defined by the model this material w...
Definition: Material.cpp:258
float GetDefaultSpecularExponent() const
Returns the specular exponent of the material that was defined by the model this material was created...
Definition: Material.cpp:274
eastl::weak_ptr< Texture > GetDefaultBumpMapTexture() const
Returns the bump map texture that was defined by the model this material was created from...
Definition: Material.cpp:172
virtual void SetSpecularTexture(eastl::shared_ptr< Texture > specularTexture)
Changes the specular texture of the material. Pass a nullptr to clear the current texture...
Definition: Material.cpp:185
glm::vec3 GetDiffuseColor() const
Returns the material&#39;s diffuse color.
Definition: Material.cpp:221
Material()=default
eastl::string modelName
Definition: Material.hpp:226
eastl::weak_ptr< Texture > defaultBumpMapTexture
Definition: Material.hpp:231
eastl::weak_ptr< Texture > LoadTexture(const aiScene *scene, aiMaterial *material, aiTextureType textureType)
Definition: Material.cpp:297
glm::vec3 GetDefaultSpecularColor() const
Returns the specular color that was defined by the model this material was created from...
Definition: Material.cpp:242
eastl::weak_ptr< Texture > GetDefaultDiffuseTexture()
Returns the diffuse texture that was defined by the model this material was created from...
Definition: Material.cpp:121
glm::vec3 GetDefaultDiffuseColor() const
Returns the diffuse color that was defined by the model this material was created from...
Definition: Material.cpp:226
float GetSpecularScale() const
Returns the scale of the specular highlights (strength).
Definition: Material.cpp:253
void SetSpecularScale(float scale)
Sets the scale of the specular highlights (strength) of the material.
Definition: Material.cpp:247
glm::vec3 GetSpecularColor() const
Returns the material&#39;s specular color.
Definition: Material.cpp:237
eastl::weak_ptr< Texture > diffuseTexture
Definition: Material.hpp:228
eastl::weak_ptr< Texture > defaultSpecularTexture
Definition: Material.hpp:233
eastl::weak_ptr< Texture > GetDefaultSpecularTexture() const
Returns the specular texture that was defined by the model this material was created from...
Definition: Material.cpp:205
eastl::weak_ptr< Texture > bumpMapTexture
Definition: Material.hpp:230
bool IsBumpMapLoaded() const
Indicates if the material has a bump map texture loaded or not.
Definition: Material.cpp:180
virtual ~Material()
Definition: Material.cpp:79
bool IsSpecularLoaded() const
Indicates if the material has a specular texture loaded or not.
Definition: Material.cpp:210
bool operator==(const Material &other) const
Returns whether or not two materials are the same.
Definition: Material.cpp:279
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 GetSpecularExponent() const
Returns the specular exponent of the material.
Definition: Material.cpp:269
eastl::weak_ptr< Texture > GetDiffuseTexture()
Returns the diffuse texture of the material. Returns nullptr if the current material doesn&#39;t have a d...
Definition: Material.cpp:113
eastl::weak_ptr< Texture > defaultDiffuseTexture
Definition: Material.hpp:229
eastl::weak_ptr< Texture > GetSpecularTexture() const
Returns the specular texture of the material. Returns nullptr if the current material doesn&#39;t have a ...
Definition: Material.cpp:200
bool IsDiffuseLoaded() const
Indicates if the material has a diffuse texture loaded or not.
Definition: Material.cpp:129
virtual void SetDiffuseColor(glm::vec3 diffuseColor)
Sets the material&#39;s diffuse color.
Definition: Material.cpp:215
virtual void UpdateMaterialData()
Definition: Material.cpp:293
eastl::weak_ptr< Texture > GetBumpMapTexture() const
Returns the bump map texture of the material. Returns nullptr if the current material doesn&#39;t have a ...
Definition: Material.cpp:164
virtual void SetBumpMapTexture(eastl::string bumpMapTextureName)
Changes the bump map texture of the material. NOTE: Texture will be loaded in, if it doesn&#39;t excist y...
Definition: Material.cpp:134
eastl::weak_ptr< Texture > specularTexture
Definition: Material.hpp:232