-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.cpp
More file actions
265 lines (241 loc) · 8.7 KB
/
model.cpp
File metadata and controls
265 lines (241 loc) · 8.7 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <QOpenGLTexture>
#include <QOpenGLVertexArrayObject>
#include <QDir>
#include "model.h"
static QOpenGLTexture *TextureFromFile(const char *path, const QString &directory);
Model::Model(QOpenGLContext* context, QString path)
: QObject(context), QOpenGLExtraFunctions(context)
{
loadModel(path);
for(unsigned int i = 0; i < meshes.length(); i++) {
setupMesh(meshes[i]);
}
}
Model::~Model()
{
for(Texture &texture: textures_loaded) {
delete texture.id;
}
}
void Model::draw(QOpenGLShaderProgram* shader)
{
shader->setUniformValue("model", modelMatrix);
for(unsigned int i = 0; i < meshes.length(); i++) {
drawMesh(shader, meshes[i]);
}
}
void Model::loadModel(QString path)
{
Assimp::Importer import;
const aiScene *scene = import.ReadFile(path.toLocal8Bit().constData(),
aiProcess_Triangulate |
aiProcess_GenSmoothNormals |
aiProcess_FlipUVs |
aiProcess_CalcTangentSpace);
if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
qDebug() << "ERROR::ASSIMP::" << import.GetErrorString();
return;
}
QDir dir(path);
dir.cdUp();
; directory = dir.path();
processNode(scene->mRootNode, scene);
}
void Model::processNode(aiNode *node, const aiScene *scene)
{
// process all the node's meshes (if any)
for(unsigned int i = 0; i < node->mNumMeshes; i++) {
aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
meshes.append(processMesh(mesh, scene));
}
// then do the same for each of its children
for(unsigned int i = 0; i < node->mNumChildren; i++) {
processNode(node->mChildren[i], scene);
}
}
Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene)
{
// data to fill
QVector<Vertex> vertices;
QVector<unsigned int> indices;
QVector<Texture> textures;
// walk through each of the mesh's vertices
for(unsigned int i = 0; i < mesh->mNumVertices; i++)
{
Vertex vertex;
QVector3D vector; // we declare a placeholder vector since assimp uses its own vector class that doesn't directly convert to glm's vec3 class so we transfer the data to this placeholder glm::vec3 first.
// positions
vector[0] = mesh->mVertices[i].x;
vector[1] = mesh->mVertices[i].y;
vector[2] = mesh->mVertices[i].z;
vertex.Position = vector;
// normals
if (mesh->HasNormals())
{
vector[0] = mesh->mNormals[i].x;
vector[1] = mesh->mNormals[i].y;
vector[2] = mesh->mNormals[i].z;
vertex.Normal = vector;
}
// texture coordinates
if(mesh->mTextureCoords[0]) // does the mesh contain texture coordinates?
{
QVector2D vec;
// 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).
vec[0] = mesh->mTextureCoords[0][i].x;
vec[1] = mesh->mTextureCoords[0][i].y;
vertex.TexCoords = vec;
// tangent
vector[0] = mesh->mTangents[i].x;
vector[1] = mesh->mTangents[i].y;
vector[2] = mesh->mTangents[i].z;
vertex.Tangent = vector;
// bitangent
vector[0] = mesh->mBitangents[i].x;
vector[1] = mesh->mBitangents[i].y;
vector[2] = mesh->mBitangents[i].z;
vertex.Bitangent = vector;
}
else
vertex.TexCoords = QVector2D(0.0f, 0.0f);
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++)
{
aiFace 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
aiMaterial* 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
// 1. diffuse maps
QVector<Texture> diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse");
textures.append(diffuseMaps);
// // 2. specular maps
QVector<Texture> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular");
textures.append(specularMaps);
// // 3. normal maps
QVector<Texture> normalMaps = loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_normal");
textures.append(normalMaps);
// // 4. height maps
QVector<Texture> heightMaps = loadMaterialTextures(material, aiTextureType_AMBIENT, "texture_height");
textures.append(heightMaps);
// return a mesh object created from the extracted mesh data
return Mesh(vertices, indices, textures);
}
QVector<Texture> Model::loadMaterialTextures(aiMaterial *mat, aiTextureType type, QString typeName)
{
QVector<Texture> textures;
for(unsigned int i = 0; i < mat->GetTextureCount(type); i++) {
aiString str;
mat->GetTexture(type, i, &str);
bool skip = false;
for(unsigned int j = 0; j < textures_loaded.size(); j++) {
if(textures_loaded[j].path == str.C_Str()) {
textures.push_back(textures_loaded[j]);
skip = true;
break;
}
}
if(!skip) { // if texture hasn't been loaded already, load it
Texture texture;
texture.id = TextureFromFile(str.C_Str(), directory);
texture.type = typeName;
texture.path = str.C_Str();
textures.push_back(texture);
textures_loaded.push_back(texture); // add to loaded textures
}
}
return textures;
}
void Model::setupMesh(Mesh& mesh)
{
mesh.VAO = new QOpenGLVertexArrayObject(this);
mesh.VAO->create();
mesh.VAO->bind();
mesh.VBO = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
mesh.VBO.create();
mesh.VBO.setUsagePattern(QOpenGLBuffer::StaticDraw);
mesh.EBO = QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
mesh.EBO.create();
mesh.EBO.setUsagePattern(QOpenGLBuffer::StaticDraw);
mesh.VBO.bind();
mesh.VBO.allocate(mesh.vertices.constData(), (int)(mesh.vertices.size() * sizeof(Vertex)));
mesh.EBO.bind();
mesh.EBO.allocate(mesh.indices.constData(), (int)(mesh.indices.size() * sizeof(unsigned int)));
// vertex positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
// vertex normals
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
// vertex texture coords
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Tangent));
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Bitangent));
glEnableVertexAttribArray(5);
glVertexAttribPointer(5, 3, GL_INT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, m_BoneIDs));
glEnableVertexAttribArray(6);
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
mesh.VAO->release();
}
void Model::drawMesh(QOpenGLShaderProgram* shader, Mesh& mesh)
{
unsigned int diffuseNr = 1;
unsigned int specularNr = 1;
unsigned int normalNr = 1;
unsigned int heightNr = 1;
for (unsigned int i = 0; i < mesh.textures.size(); i++) {
glActiveTexture(GL_TEXTURE0 + i);
QString number;
QString name = mesh.textures[i].type;
if (name == "texture_diffuse") {
number = QString::number(diffuseNr++);
} else if(name == "texture_specular") {
number = QString::number(specularNr++);
} else if(name == "texture_normal") {
number = QString::number(normalNr++);
} else if(name == "texture_height") {
number = QString::number(heightNr++);
}
shader->setUniformValue((name + number).toLocal8Bit().constData(), i);
if (mesh.textures[i].id) {
mesh.textures[i].id->bind();
}
}
mesh.VAO->bind();
glDrawElements(GL_TRIANGLES, mesh.indices.size(), GL_UNSIGNED_INT, 0);
mesh.VAO->release();
}
QOpenGLTexture *TextureFromFile(const char *path, const QString &directory)
{
QString filename = path;
filename = directory + '/' + filename;
QOpenGLTexture *texture = nullptr;
qDebug() << "loading texture file:" << filename;
QImage data(filename);
if(!data.isNull()) {
texture = new QOpenGLTexture(data.flipped(Qt::Vertical));
texture->bind();
texture->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);
texture->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);
texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
texture->setMagnificationFilter(QOpenGLTexture::Linear);
return texture;
} else {
qDebug() << "Texture failed to load at path:" << path;
return nullptr;
}
}