-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model.h
379 lines (327 loc) · 9.24 KB
/
Model.h
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#ifndef MODEL_H
#define MODEL_H
#define USE_ASSIMP 1
#include <vector>
#include <string>
#include <set>
#include <iostream>
#include <fstream>
#include <sstream>
#include <glm/vec3.hpp>
#include <glm/vec2.hpp>
#if USE_ASSIMP
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#endif
#include"Log.h"
#include <io.h> // _access
#include <QObject>
//#include <QOpenGLFunctions>
#include <QtGui/QOpenGLFunctions_4_1_Core>
#include <QtOpenGLExtensions/QOpenGLExtensions>
using glm::vec2;
using glm::vec3;
using glm::vec4;
using namespace std;
class Model : public QObject,protected QOpenGLFunctions_4_1_Core
{
Q_OBJECT
public:
typedef std::vector<unsigned int> UIntArray;
typedef std::vector<vec3> Vec3Array;
typedef std::vector<vec2> Vec2Array;
public:
Model() {}
Model(const std::string& str_path, bool use_normal = true, bool use_uv = true, bool use_tangent = false)
: _use_normal(use_normal), _use_uv(use_uv), _use_tangent(use_tangent)
{
initializeOpenGLFunctions();
_LoadMeshFromFile(str_path);
_BindBuffer();
}
~Model()
{
destory();
}
void init(const std::string& str_path, bool use_normal = true, bool use_uv = true, bool use_tangent = false, bool use_bitangent = false)
{
initializeOpenGLFunctions();
_use_normal = use_normal;
_use_uv = use_uv;
_use_tangent = use_tangent;
_use_bitangent = use_bitangent;
_LoadMeshFromFile(str_path);
_BindBuffer();
}
void destory()
{
glDeleteBuffers(1, &elementBuffer);
glDeleteBuffers(1, &vertexBuffer);
if (_use_normal)
glDeleteBuffers(1, &normalBuffer);
if (_use_uv)
glDeleteBuffers(1, &uvBuffer);
if (_use_tangent)
glDeleteBuffers(1, &tangent_buffer);
if (_use_bitangent)
glDeleteBuffers(1, &bitangent_buffer);
Log::msg("Clean up model...");
}
void render()
{
GLuint attribute_index = 0;
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(attribute_index);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(
attribute_index, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
attribute_index++;
// 2nd attribute buffer : UVs
if (_use_normal)
{
glEnableVertexAttribArray(attribute_index);
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
glVertexAttribPointer(
attribute_index, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
attribute_index++;
}
if (_use_tangent)
{
glEnableVertexAttribArray(attribute_index);
glBindBuffer(GL_ARRAY_BUFFER, tangent_buffer);
glVertexAttribPointer(
attribute_index, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
attribute_index++;
}
if (_use_uv)
{
// 3rd attribute buffer : UVs
glEnableVertexAttribArray(attribute_index);
glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
glVertexAttribPointer(
attribute_index, // attribute
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
attribute_index++;
}
if (_use_bitangent)
{
glEnableVertexAttribArray(attribute_index);
glBindBuffer(GL_ARRAY_BUFFER, bitangent_buffer);
glVertexAttribPointer(
attribute_index, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
attribute_index++;
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
// Draw the triangle !
//glDrawArrays(GL_TRIANGLES, 0, vertices.size());
glDrawElements(
GL_TRIANGLES,
(GLsizei)_triangles.size(),
GL_UNSIGNED_INT,
(void*)0
);
glDisableVertexAttribArray(0);
if (_use_normal)
glDisableVertexAttribArray(1);
if (_use_uv)
glDisableVertexAttribArray(2);
}
int VertexCount() const {
return _vertices.size();
}
int IndexCount() const {
return _triangles.size();
}
const UIntArray& Triangles() const
{
return _triangles;
}
const Vec3Array& Vertices() const
{
return _vertices;
}
const Vec3Array& Normals() const
{
return _normals;
}
const Vec2Array& UV() const
{
return _uv;
}
private:
#if USE_ASSIMP
void _LoadMeshFromFile(const std::string& str_path)
{
_triangles.clear();
_vertices.clear();
_normals.clear();
_uv.clear();
_triangles.clear();
_bitangent.clear();
if (_access(str_path.c_str(), 0) == -1)
{
Log::msg("Model file " + str_path + " not exists!");
return;
}
Assimp::Importer importer;
const char* path = str_path.c_str();
unsigned int load_option =
//aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType;
if (_use_normal) load_option |= aiProcess_GenSmoothNormals;
if (_use_tangent || _use_bitangent) load_option |= aiProcess_CalcTangentSpace;
const aiScene* scene = importer.ReadFile(path, load_option);
if (!scene) {
Log::msg("Can not open model " + str_path + ". Maybe this file type is not supported");
return; // TODO
}
// get each mesh
int nvertices = 0;
int ntriangles = 0;
for (unsigned int i = 0; i < scene->mNumMeshes; i++) {
aiMesh* mesh = scene->mMeshes[i];
nvertices += mesh->mNumVertices;
ntriangles += mesh->mNumFaces;
}
_vertices.reserve(nvertices);
if (_use_normal) _normals.reserve(nvertices);
if (_use_uv) _uv.reserve(nvertices);
if (_use_tangent) _tangent.reserve(nvertices);
if (_use_bitangent) _bitangent.reserve(nvertices);
_triangles.resize(ntriangles * 3); // TODO, *3?
int idx = 0;
int idx2 = 0;
for (unsigned int i = 0; i < scene->mNumMeshes; i++)
{
aiMesh* mesh = scene->mMeshes[i];
if (_use_uv)
assert(mesh->HasTextureCoords(0) == true);
for (unsigned int j = 0; j < mesh->mNumVertices; j++)
{
aiVector3D& v = mesh->mVertices[j];
_vertices.push_back(vec3(v.x, v.y, v.z));
if (_use_normal)
{
aiVector3D& n = mesh->mNormals[j];
_normals.push_back(vec3(n.x, n.y, n.z));
}
if (_use_uv)
{
aiVector3D& u = mesh->mTextureCoords[0][j];
_uv.push_back(vec2(u.x, u.y));
}
if (_use_tangent)
{
auto& t = mesh->mTangents[j];
_tangent.push_back(vec3(t.x, t.y, t.z));
}
if (_use_bitangent)
{
auto& t = mesh->mBitangents[j];
_bitangent.push_back(vec3(t.x, t.y, t.z));
}
//aabb.expand(glm::vec3(v.x, v.y, v.z));
}
//int temp_idx = idx/3;
for (unsigned int j = 0; j < mesh->mNumFaces; j++)
{
const aiFace& Face = mesh->mFaces[j];
assert(Face.mNumIndices == 3);
_triangles[idx++] = Face.mIndices[0] + idx2;
_triangles[idx++] = Face.mIndices[1] + idx2;
_triangles[idx++] = Face.mIndices[2] + idx2;
}
idx2 += mesh->mNumVertices;
}
}
#else
#endif
void _BindBuffer()
{
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
glGenBuffers(1, &elementBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, _triangles.size() * sizeof(unsigned int), &_triangles[0], GL_STATIC_DRAW);
// Load it into a VBO
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, _vertices.size() * sizeof(glm::vec3), &_vertices[0], GL_STATIC_DRAW);
if (_use_normal)
{
glGenBuffers(1, &normalBuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
glBufferData(GL_ARRAY_BUFFER, _normals.size() * sizeof(glm::vec3), &_normals[0], GL_STATIC_DRAW);
}
if (_use_tangent)
{
glGenBuffers(1, &tangent_buffer);
glBindBuffer(GL_ARRAY_BUFFER, tangent_buffer);
glBufferData(GL_ARRAY_BUFFER, _tangent.size() * sizeof(glm::vec3), &_tangent[0], GL_STATIC_DRAW);
}
if (_use_uv)
{
glGenBuffers(1, &uvBuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
glBufferData(GL_ARRAY_BUFFER, _uv.size() * sizeof(glm::vec2), &_uv[0], GL_STATIC_DRAW);
}
if (_use_bitangent)
{
glGenBuffers(1, &bitangent_buffer);
glBindBuffer(GL_ARRAY_BUFFER, bitangent_buffer);
glBufferData(GL_ARRAY_BUFFER, _bitangent.size() * sizeof(glm::vec3), &_bitangent[0], GL_STATIC_DRAW);
}
}
public:
GLuint elementBuffer;
GLuint vertexBuffer;
GLuint normalBuffer;
GLuint tangent_buffer;
GLuint bitangent_buffer;
GLuint uvBuffer;
private:
std::vector<unsigned int> _triangles;
std::vector<glm::vec3> _vertices;
std::vector<glm::vec3> _normals;
std::vector<glm::vec2> _uv;
std::vector<glm::vec3> _tangent;
std::vector<glm::vec3> _bitangent;
bool _use_normal = true;
bool _use_uv = true;
bool _use_tangent = false;
bool _use_bitangent = false;
};
#endif