15 #include <ThirdParty/EASTL-master/include/EASTL/algorithm.h> 17 #include <ThirdParty/assimp/include/assimp/postprocess.h> 24 for (
size_t i = 0, size = loadedModels_.size(); i < size; ++i)
26 if (loadedModels_[i]->GetName() == modelName)
27 return loadedModels_[i];
31 return eastl::shared_ptr<Model>();
36 Assimp::Importer importer;
37 eastl::string path =
"Resources/Models/" + meshToLoad;
38 eastl::string skeletonPath;
40 const aiScene* scene = importer.ReadFile(path.c_str(), aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_JoinIdenticalVertices | aiProcess_ImproveCacheLocality |
41 aiProcess_OptimizeMeshes);
45 if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
47 debug_warning(
"ResourceManager",
"CreateModel", importer.GetErrorString());
48 return eastl::shared_ptr<Model>();
51 eastl::weak_ptr<Skeleton> skeleton;
52 if (skeletonToLoad !=
"")
57 loadedModels_.push_back(eastl::shared_ptr<Model>(
new Model(scene, modelName)));
58 eastl::shared_ptr<Model>modelToAddTo = loadedModels_.back();
61 ProcessModel(modelName, modelToAddTo, scene->mRootNode, scene, skeleton.lock());
62 return loadedModels_.back();
67 Assimp::Importer importer;
68 eastl::string path =
"Resources/Models/" + skeletonToLoad;
70 for (
size_t i = 0, size = loadedSkeletons_.size(); i < size; ++i) {
71 if (loadedSkeletons_[i]->GetName() == skeletonToLoad)
72 return loadedSkeletons_[i];
75 const aiScene* scene = importer.ReadFile(path.c_str(), 0);
79 if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
81 debug_warning(
"ResourceManager",
"CreateSkeleton", importer.GetErrorString());
82 return eastl::shared_ptr<Skeleton>();
85 eastl::shared_ptr<Skeleton> skeleton = eastl::shared_ptr<Skeleton>(
new Skeleton(scene));
86 skeleton->SetName(skeletonToLoad);
88 loadedSkeletons_.push_back(skeleton);
94 Assimp::Importer importer;
95 eastl::string path =
"Resources/Animations/" + animationsToLoad;
96 const aiScene* scene = importer.ReadFile(path.c_str(), 0);
98 for (
size_t i = 0, size = loadedSkeletons_.size(); i < size; ++i)
100 if (loadedSkeletons_[i]->GetName() == skeletonName) {
101 loadedSkeletons_[i]->LoadAnimationSet(scene, names);
106 eastl::weak_ptr<Mesh>
ResourceManager::CreateMesh(aiMesh* mesh, eastl::shared_ptr<Skeleton> skeleton, eastl::vector<Vertex> vertices, eastl::vector<unsigned> indices)
109 eastl::weak_ptr<Mesh> meshToReturn = GetMesh(vertices, indices);
110 if (meshToReturn.expired() ==
false && meshToReturn.lock().get() !=
nullptr)
114 eastl::shared_ptr<Mesh> createdMesh = eastl::shared_ptr<OpenGLMesh>(
new OpenGLMesh(vertices, indices));
117 eastl::shared_ptr<Mesh> createdMesh = eastl::shared_ptr<VulkanMesh>(
new VulkanMesh(mesh, skeleton, vertices, indices));
119 loadedMeshes_.push_back(eastl::move(createdMesh));
127 if (loadedTextures_.find(meshName) == loadedTextures_.end())
128 return eastl::shared_ptr<Texture>();
131 return loadedTextures_[meshName];
137 eastl::weak_ptr<Texture> textureToReturn =
GetTexture(textureName);
138 if (textureToReturn.expired() ==
false && textureToReturn.lock().get() !=
nullptr)
139 return textureToReturn;
142 eastl::shared_ptr<Texture> createdTexture = eastl::shared_ptr<OpenGLTexture>(
new OpenGLTexture(textureName));
145 eastl::shared_ptr<Texture> createdTexture = eastl::shared_ptr<VulkanTexture>(
new VulkanTexture(textureName));
147 loadedTextures_.insert(eastl::make_pair(textureName, eastl::move(createdTexture)));
149 return loadedTextures_[textureName];
152 void ResourceManager::ProcessModel(eastl::string modelName, eastl::shared_ptr<Model> modelToAddTo, aiNode* node,
const aiScene* scene, eastl::shared_ptr<Skeleton> skeleton)
155 if (node->mParent ==
nullptr)
156 modelToAddTo->SetSkeleton(skeleton);
160 for (
unsigned short i = 0; i < scene->mNumMeshes; i++)
162 aiMesh* mesh = scene->mMeshes[i];
165 modelToAddTo->AddMesh(ProcessMesh(mesh, scene, skeleton).lock());
167 eastl::shared_ptr<Material> material;
170 material = eastl::shared_ptr<VulkanMaterial>(
new VulkanMaterial(scene,
171 static_cast<uint32_t>(mesh->mMaterialIndex), modelName));
174 material = eastl::shared_ptr<Material>(
new Material(scene,
175 static_cast<uint32_t>(mesh->mMaterialIndex), modelName));
178 modelToAddTo->SetMeshMaterial(modelToAddTo->GetModelMeshes()[modelToAddTo->GetModelMeshes().size() - 1], eastl::move(material));
181 for (
unsigned short i = 0; i < node->mNumChildren; i++)
183 ProcessModel(modelName, modelToAddTo, node->mChildren[i], scene, skeleton);
187 eastl::weak_ptr<Mesh> ResourceManager::ProcessMesh(aiMesh* mesh,
const aiScene* scene, eastl::shared_ptr<Skeleton> skeleton)
189 eastl::vector<Vertex> vertices;
190 eastl::vector<unsigned> indices;
191 eastl::vector<eastl::shared_ptr<Texture>> diffuseTextures;
192 eastl::vector<eastl::shared_ptr<Texture>> specularTextures;
195 for (
unsigned i = 0; i < mesh->mNumVertices; i++)
199 vertex.
position.x = mesh->mVertices[i].x;
200 vertex.
position.y = mesh->mVertices[i].y;
201 vertex.
position.z = mesh->mVertices[i].z;
207 vertex.
normal.x = mesh->mNormals[i].x;
208 vertex.
normal.y = mesh->mNormals[i].y;
209 vertex.
normal.z = mesh->mNormals[i].z;
214 if (mesh->mTextureCoords[0] >
nullptr)
216 vertex.
texCoords.s = mesh->mTextureCoords[0][i].x;
217 vertex.
texCoords.t = mesh->mTextureCoords[0][i].y;
222 vertex.
texCoords = glm::vec2(0.0f, 0.0f);
234 vertices.push_back(vertex);
238 for (
size_t i = 0; i < mesh->mNumFaces; ++i)
240 aiFace *face = &mesh->mFaces[i];
243 for (
size_t j = 0; j < face->mNumIndices; j++)
245 indices.push_back(static_cast<unsigned>(face->mIndices[j]));
250 if (mesh->mMaterialIndex >= 0)
252 aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
255 return CreateMesh(mesh, skeleton, vertices, indices);
261 eastl::weak_ptr<Texture> textureToReturn =
GetTexture(textureName);
262 if (textureToReturn.expired() ==
false && textureToReturn.lock().get() !=
nullptr)
263 return textureToReturn;
266 eastl::shared_ptr<Texture> createdTexture = eastl::shared_ptr<OpenGLTexture>(
new OpenGLTexture(width, height));
269 eastl::shared_ptr<Texture> createdTexture = eastl::shared_ptr<VulkanTexture>(
new VulkanTexture(width, height));
271 createdTexture->CreateTextureWithData(data,
false);
273 loadedTextures_.insert(eastl::make_pair(textureName, eastl::move(createdTexture)));
275 return loadedTextures_[textureName];
278 void ResourceManager::AddTexture(eastl::string textureName, eastl::shared_ptr<Texture> textureToAdd)
281 eastl::weak_ptr<Texture> textureToReturn =
GetTexture(textureName);
282 if (textureToReturn.expired() ==
false && textureToReturn.lock().get() !=
nullptr)
285 loadedTextures_[textureName] = eastl::move(textureToAdd);
288 eastl::weak_ptr<Mesh> ResourceManager::GetMesh(eastl::vector<Vertex> vertices, eastl::vector<unsigned> indices)
290 if (loadedMeshes_.size() == 0)
return eastl::shared_ptr<Mesh>();
292 for (
size_t i = 0, size = loadedMeshes_.size(); i < size; ++i)
296 if (loadedMeshes_[i]->vertices.size() != vertices.size())
continue;
297 if (loadedMeshes_[i]->indices.size() != indices.size())
continue;
299 for (
size_t j = 0, verticesSize = loadedMeshes_[i]->vertices.size(); j < verticesSize; ++j)
301 Vertex vertex = loadedMeshes_[i]->vertices[j];
303 if (vertex.
normal != vertices[j].normal) { isEqual =
false;
break; }
304 if (vertex.
position != vertices[j].position) { isEqual =
false;
break; }
305 if (vertex.
texCoords != vertices[j].texCoords) { isEqual =
false;
break; }
308 if (isEqual ==
false)
continue;
310 for (
size_t j = 0, indicesSize = loadedMeshes_[i]->indices.size(); j < indicesSize; ++j)
312 if (loadedMeshes_[i]->indices[j] != indices[j]) { isEqual =
false;
break; }
316 return loadedMeshes_[i];
319 return eastl::shared_ptr<Mesh>();
322 eastl::vector<eastl::shared_ptr<Texture>> ResourceManager::ProcessDiffuseTextures(aiMaterial* material)
324 eastl::vector<eastl::shared_ptr<Texture>> textures;
326 eastl::vector<eastl::shared_ptr<Texture>> diffuseMaps = LoadMaterialTextures(material, aiTextureType_DIFFUSE,
"texture_diffuse");
327 textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
332 eastl::vector<eastl::shared_ptr<Texture>> ResourceManager::ProcessSpecularTextures(aiMaterial* material)
334 eastl::vector<eastl::shared_ptr<Texture>> textures;
336 eastl::vector<eastl::shared_ptr<Texture>> specularMaps = LoadMaterialTextures(material, aiTextureType_SPECULAR,
"texture_specular");
337 textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
342 eastl::vector<eastl::shared_ptr<Texture>> ResourceManager::LoadMaterialTextures(aiMaterial* material,
343 aiTextureType textureType, eastl::string typeName)
345 eastl::vector<eastl::shared_ptr<Texture>> textures;
346 for (
unsigned short i = 0; i < material->GetTextureCount(textureType); i++)
349 material->GetTexture(textureType, i, &str);
350 eastl::string fileName = eastl::string(str.C_Str());
354 for (
unsigned short j = 0; j < loadedTextures_.size(); j++)
356 if (loadedTextures_.find(fileName) != loadedTextures_.end())
358 textures.push_back(loadedTextures_[fileName]);
371 textures.push_back(texture.lock());
374 loadedTextures_.insert(eastl::make_pair(fileName, texture.lock()));
eastl::weak_ptr< Skeleton > CreateSkeleton(eastl::string skeletonToLoad)
This will load the skeleton data from the file specified. NOTe: This method will not load in the skel...
eastl::weak_ptr< Texture > GetTexture(eastl::string textureName)
This method allows you to get a texture with the defined name.
void AddAnimationsToSkeleton(eastl::string skeletonName, eastl::string animationsToLoad="", eastl::vector< eastl::string > names={})
This method loads animations from a file, and adds them to the skeleton with the specified name...
eastl::weak_ptr< Model > GetModel(eastl::string modelName)
This method allows you to get a model with a specific name.
eastl::weak_ptr< Texture > CreateTexture(eastl::string textureName)
This method allows you to create a new texture with the given name.
eastl::weak_ptr< Mesh > CreateMesh(aiMesh *mesh, eastl::shared_ptr< Skeleton > skeleton, eastl::vector< Vertex > vertices, eastl::vector< unsigned > indices)
This method allows you to create a new mesh. NOTE: This method will not load in the mesh again...
eastl::weak_ptr< Model > CreateModel(eastl::string modelName, eastl::string meshToLoad="", eastl::string skeleton="")
This method will allow you to create a new model with the model name.
#define debug_warning(debug_class, function, value)
This functions logs a debug warning to the log and console.
This object is used to store general data for a vertex.