88
99#include < iostream>
1010#include < ppl.h>
11+ #include " ResourceManager.h"
1112
1213/* **********************************************************************************/
1314Model::Model (const std::string_view Path, const std::string_view Name, const bool flipWindingOrder, const bool loadMaterial) : m_name(Name), m_path(Path) {
@@ -18,13 +19,13 @@ Model::Model(const std::string_view Path, const std::string_view Name, const boo
1819}
1920
2021/* **********************************************************************************/
21- Model::Model (const std::string_view Name, const std::vector<Vertex>& vertices, const std::vector<GLuint>& indices, const PBRMaterial & material) noexcept : m_name(Name) {
22+ Model::Model (const std::string_view Name, const std::vector<Vertex>& vertices, const std::vector<GLuint>& indices, const PBRMaterialPtr & material) noexcept : m_name(Name) {
2223m_meshes.emplace_back (vertices, indices, material);
2324}
2425
2526/* **********************************************************************************/
2627Model::Model (const std::string_view Name, const Mesh& mesh) noexcept : m_name(Name) {
27- m_meshes.push_back (std::move ( mesh) );
28+ m_meshes.push_back (mesh);
2829}
2930
3031/* **********************************************************************************/
@@ -203,12 +204,17 @@ Mesh Model::processMesh(aiMesh* mesh, const aiScene* scene, const bool loadMater
203204// http://assimp.sourceforge.net/lib_html/structai_material.html
204205if (loadMaterial) {
205206if (mesh->mMaterialIndex >= 0 ) {
206- PBRMaterial material;
207207const auto * mat = scene->mMaterials [mesh->mMaterialIndex ];
208208
209209aiString name;
210210mat->Get (AI_MATKEY_NAME, name);
211211
212+ // Is the material cached?
213+ const auto cachedMaterial = ResourceManager::GetInstance ().GetMaterial (name.C_Str ());
214+ if (cachedMaterial.has_value ()) {
215+ return Mesh (vertices, indices, cachedMaterial.value ());
216+ }
217+
212218// Get the first texture for each texture type we need
213219// since there could be multiple textures per type
214220aiString albedoPath;
@@ -226,20 +232,16 @@ Mesh Model::processMesh(aiMesh* mesh, const aiScene* scene, const bool loadMater
226232aiString alphaMaskPath;
227233mat->GetTexture (aiTextureType_OPACITY, 0 , &alphaMaskPath);
228234
229- std::cout << " MATERIAL NAME: " << name.C_Str () << std::endl;
230-
231- material.Init (name.C_Str (),
235+ const auto newMaterial = ResourceManager::GetInstance ().CacheMaterial (name.C_Str (),
232236m_path + albedoPath.C_Str (),
233237" " ,
234238m_path + metallicPath.C_Str (),
235239m_path + normalPath.C_Str (),
236240m_path + roughnessPath.C_Str (),
237- m_path + alphaMaskPath.C_Str ()
238- );
241+ m_path + alphaMaskPath.C_Str ());
239242
240243++m_numMats;
241- std::cout << " NUMBER OF MATERIALS: " << m_numMats << std::endl;
242- return Mesh (vertices, indices, material);
244+ return Mesh (vertices, indices, newMaterial);
243245}
244246}
245247
0 commit comments