-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model.cpp
143 lines (123 loc) · 5.27 KB
/
Model.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "Model.hpp"
#include <GL/glew.h>
#include <assimp/Importer.hpp>
#include <assimp/postprocess.h>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
Model::Model(const std::string &path)
: m_Meshes(), m_TexturesLoaded(),
m_Directory(path.substr(
0, path.find_last_of(
"/"))) // retrieve the directory path of the filepath
{
// loads a model with supported ASSIMP extensions from file and stores the
// resulting meshes in the meshes vector. read file via ASSIMP
Assimp::Importer importer;
const auto scene = importer.ReadFile(
path, aiProcess_Triangulate | aiProcess_CalcTangentSpace |
aiProcess_GenNormals | aiProcess_JoinIdenticalVertices);
// check for errors
if (!scene or scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE or
!scene->mRootNode) {
std::cerr << "ERROR::ASSIMP::" << importer.GetErrorString() << std::endl;
return;
}
// process ASSIMP's root node recursively
ProcessNode(scene->mRootNode, scene);
}
void Model::ProcessNode(const aiNode *node, const aiScene *scene) {
// process each mesh located at the current node
for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
// the node object only contains indices to index the actual objects in the
// scene. the scene contains all the data, node is just to keep stuff
// organized (like relations between nodes).
const auto mesh = scene->mMeshes[node->mMeshes[i]];
ProcessMesh(mesh, scene);
}
// after we've processed all of the meshes (if any) we then recursively
// process each of the children nodes
for (unsigned int i = 0; i < node->mNumChildren; ++i)
ProcessNode(node->mChildren[i], scene);
}
void Model::ProcessMesh(const aiMesh *const mesh, const aiScene *const scene) {
// data to fill
std::vector<Vertex> vertices;
vertices.reserve(mesh->mNumVertices);
std::vector<uint> indices;
std::vector<std::string> textures;
// Walk through each of the mesh's vertices
for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
Vertex vertex;
// aiVector is container for positions and normals of assimp which cannot be
// converted to glm::vec3 directly For positions
auto &aiVector = mesh->mVertices[i];
vertex.Position = glm::vec3(aiVector.x, aiVector.y, aiVector.z);
// For normals
aiVector = mesh->mNormals[i];
vertex.Normal = glm::vec3(aiVector.x, aiVector.y, aiVector.z);
// For tangent
aiVector = mesh->mTangents[i];
vertex.Tangent = glm::vec3(aiVector.x, aiVector.y, aiVector.z);
// For bitangent
aiVector = mesh->mBitangents[i];
vertex.Bitangent = glm::vec3(aiVector.x, aiVector.y, aiVector.z);
// Texture Coordinates
if (mesh->mTextureCoords[0]) // check if the mesh contains texture
// cooordinates
{
/*
A vertex can contain up to 8 different texture coordinates. We thus make
the assumption that we won't use models where a vertex can have multiple
texture coordinates so we always take the first set (0).
*/
aiVector = mesh->mTextureCoords[0][i];
vertex.TexCoords = glm::vec2(aiVector.x, aiVector.y);
} else
vertex.TexCoords = glm::vec2(0.f);
vertices.push_back(vertex);
}
// now wak through each of the mesh's faces (a face is a mesh its triangle)
// and retrieve the corresponding vertex indices.
for (unsigned int i = 0; i < mesh->mNumFaces; ++i) {
const auto &face = mesh->mFaces[i];
// retrieve all indices of the face and store them in the indices vector
for (unsigned int j = 0; j < face.mNumIndices; ++j)
indices.push_back(face.mIndices[j]);
}
// process materials
const auto material = scene->mMaterials[mesh->mMaterialIndex];
/*
we assume a convention for sampler names in the shaders. Each diffuse
texture should be named as 'texture_diffuseN' where N is a sequential number
ranging from 1 to MAX_SAMPLER_NUMBER. Same applies to other texture as the
following list summarizes: diffuse: texture_diffuseN specular:
texture_specularN normal: texture_normalN
*/
// Diffuse Maps
LoadMaterialTextures(material, textures, aiTextureType_DIFFUSE, "diffuse");
// Specular Maps
LoadMaterialTextures(material, textures, aiTextureType_SPECULAR, "specular");
// Normal maps
// LoadMaterialTextures(material, textures, aiTextureType_HEIGHT, "normal");
// Height maps
// LoadMaterialTextures(material, textures, aiTextureType_AMBIENT, "height");
// return a mesh object created from the extracted mesh data
m_Meshes.emplace_back(vertices, indices, m_TexturesLoaded, textures);
}
void Model::LoadMaterialTextures(const aiMaterial *const mat,
std::vector<std::string> &textures,
const aiTextureType type,
const char *const typeName) {
for (uint i = 0; i < mat->GetTextureCount(type); ++i) {
aiString path;
mat->GetTexture(type, i, &path);
const auto pathString = path.C_Str();
const auto loadedTexture = m_TexturesLoaded.find(pathString);
if (loadedTexture == m_TexturesLoaded.end()) {
m_TexturesLoaded.emplace(
std::piecewise_construct, std::forward_as_tuple(pathString),
std::forward_as_tuple(m_Directory + '/' + pathString, typeName));
}
textures.push_back(pathString);
}
}