sculpto
meshes_load.cpp
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file models_load.h
3 * \brief Assets manager model load functions implementation modulule.
4 *
5 * \author Sabitov Kirill
6 * \date 09 July 2022
7 *********************************************************************/
8
9#include "sclpch.h"
10
11#include <assimp/Importer.hpp>
12#include <assimp/scene.h>
13#include <assimp/postprocess.h>
14
15#include "meshes_load.h"
16#include "shaders_load.h"
17#include "textures_load.h"
18#include "core/resources/mesh.h"
23
25{
26 // Process all meshes of current node.
27 for (u32 i = 0; i < Node->mNumMeshes; i++)
28 {
29 aiMesh *mesh = Scene->mMeshes[Node->mMeshes[i]];
30 submesh_props<scl::vertex> generating_submesh_props;
31 GenerateSubmesh(mesh, generating_submesh_props);
32 OutSubmeshes.push_back(generating_submesh_props);
33 }
34
35 // Run processing of all child nodes
36 for (u32 i = 0; i < Node->mNumChildren; i++)
37 ProcessNode(Node->mChildren[i]);
38}
39
40void scl::assets_manager::mesh_loader_phong::GenerateSubmesh(aiMesh *Mesh, scl::submesh_props<scl::vertex> &OutSubmeshProps)
41{
42 OutSubmeshProps.Topology.Vertices.resize(Mesh->mNumVertices);
43 for (u32 i = 0; i < Mesh->mNumVertices; i++)
44 {
45 vertex v;
46 v.Position = { Mesh->mVertices[i].x, Mesh->mVertices[i].y, Mesh->mVertices[i].z };
47 v.TexCoords = Mesh->mTextureCoords[0] ? vec2 { Mesh->mTextureCoords[0][i].x, Mesh->mTextureCoords[0][i].y } : vec2 {};
48 OutSubmeshProps.Topology.Vertices[i] = v;
49 }
50
51 for (u32 i = 0; i < Mesh->mNumFaces; i++)
52 {
53 const aiFace &face = Mesh->mFaces[i];
54 for (u32 j = 0; j < face.mNumIndices; j++)
55 OutSubmeshProps.Topology.Indices.push_back(face.mIndices[j]);
56 }
57
58 OutSubmeshProps.Topology.EvaluateNormals();
59 OutSubmeshProps.Topology.EvaluateTangentSpace();
60 OutSubmeshProps.Material = GenerateSubmeshMaterial(Mesh);
61}
62
63scl::shared<scl::material_phong> scl::assets_manager::mesh_loader_phong::GenerateSubmeshMaterial(aiMesh *Mesh)
64{
65 shared<material_phong> mat = material_phong::Create(vec3 { 0.4 }, vec3 { 0 }, 1);
66 if (Mesh->mMaterialIndex < 0) return mat;
67
68 aiMaterial *ai_mat = Scene->mMaterials[Mesh->mMaterialIndex];
69 aiString path;
70 if (ai_mat->GetTextureCount(aiTextureType_DIFFUSE) > 0)
71 {
72 ai_mat->GetTexture(aiTextureType_DIFFUSE, 0, &path);
73 mat->SetDiffuseMapTexture(assets_manager::LoadTexture(DirectoryPath + '/' + std::string(path.C_Str())));
74 }
75 if (ai_mat->GetTextureCount(aiTextureType_SPECULAR) > 0)
76 {
77 ai_mat->GetTexture(aiTextureType_SPECULAR, 0, &path);
78 mat->SetSpecularMapTexture(assets_manager::LoadTexture(DirectoryPath + '/' + std::string(path.C_Str())));
79 }
80 if (ai_mat->GetTextureCount(aiTextureType_EMISSIVE) > 0)
81 {
82 ai_mat->GetTexture(aiTextureType_EMISSIVE, 0, &path);
83 mat->SetEmissionMapTexture(assets_manager::LoadTexture(DirectoryPath + '/' + std::string(path.C_Str())));
84 }
85 if (ai_mat->GetTextureCount(aiTextureType_NORMALS) > 0)
86 {
87 ai_mat->GetTexture(aiTextureType_NORMALS, 0, &path);
88 mat->SetNormalMapTexture(assets_manager::LoadTexture(DirectoryPath + '/' + std::string(path.C_Str())));
89 }
90
91 return mat;
92}
93
95 const std::string &DirectoryPath,
96 std::vector<submesh_props<vertex>> &OutSubmeshes) :
97 Scene(Scene), DirectoryPath(DirectoryPath), OutSubmeshes(OutSubmeshes) {}
98
99scl::shared<scl::mesh> scl::assets_manager::LoadMeshes(const std::filesystem::path &ModelFilePath)
100{
101 SCL_CORE_INFO("Mesh creation from file \"{}\" started (this may take time).", ModelFilePath.string());
102
103 u32 flags = aiProcess_Triangulate
104 | aiProcess_OptimizeMeshes
105 | aiProcess_GenNormals
106 | aiProcess_CalcTangentSpace;
107 Assimp::Importer importer {};
108 const aiScene *scene = importer.ReadFile(ModelFilePath.string(), flags);
109 SCL_CORE_ASSERT(scene && (!scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) && scene->mRootNode,
110 "Error while loading model: {}", importer.GetErrorString());
111
112 std::vector<submesh_props<vertex>> out_submeshes;
113 mesh_loader_phong mesh_loader(scene, ModelFilePath.parent_path().string(), out_submeshes);
114 mesh_loader.ProcessNode(scene->mRootNode);
115
116 auto loaded_mesh = mesh::Create(out_submeshes);
117 loaded_mesh->FileName = ModelFilePath.string();
118 return loaded_mesh;
119}
#define SCL_CORE_ASSERT(expr,...)
Definition: assert.h:69
mesh_loader_phong(const aiScene *Scene, const std::string &DirectoryPath, std::vector< submesh_props< vertex > > &OutSubmeshes)
Definition: meshes_load.cpp:94
static shared< material_phong > Create(const vec3 &Diffuse, const vec3 &Specular, float Shininess)
Definition: mesh.h:31
static shared< mesh > Create(const Ttopology &TopologyObject, shared< material > Material)
Definition: mesh.h:118
std::vector< Tvertex > Vertices
Definition: basis.h:22
std::vector< u32 > Indices
Definition: basis.h:23
virtual void EvaluateNormals()
Definition: trimesh.cpp:33
virtual void EvaluateTangentSpace()
Definition: trimesh.cpp:47
#define SCL_CORE_INFO(...)
Definition: log.h:41
Mesh material for bling-phone lighting model class deinition module.
Mesh interfaces definition module. Mesh stores vertex and index buffer and implement their binding du...
shared< texture_2d > LoadTexture(const std::filesystem::path &TextureImageFilePath)
shared< mesh > LoadMeshes(const std::filesystem::path &ModelFilePath)
Definition: meshes_load.cpp:99
std::shared_ptr< T > shared
Definition: smart_ptr.h:15
uint32_t u32
Definition: math_common.h:21
math::vec3< float > vec3
Definition: base.h:38
math::vec2< float > vec2
Definition: base.h:37
Base, abstract, backend render api independent render context class implementation module.
Sculpto library prehompiled header. Defines common definitions, includes commonly used modules.
shared< material > Material
Definition: mesh.h:26
topology::trimesh Topology
Definition: mesh.h:25
vec3 Position
Definition: vertex.h:82
vec2 TexCoords
Definition: vertex.h:86
Assets manager texture load function defintion modulule.
Topology object triangles mesh class declaration module.
Vertex description and storage classes definition module.