-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model_CPU.h
363 lines (302 loc) · 12.1 KB
/
Model_CPU.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
#include <vector>
#include <array>
#include <iostream>
#include <string>
#include <fstream>
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "LoadShader.h"
bool loadAssimp(const char* path, std::vector<glm::vec3>& out_vertices, std::vector<glm::vec3>& out_normals, std::vector<unsigned int>& out_indices);
void printVec(glm::vec3 v) {
std::cout << "(" << v.x << ", " << v.y << ", " << v.z << ") ";
}
void printVec(glm::vec4 v) {
std::cout << "(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ") ";
}
class Model {
public:
//Handles
GLuint VAO, positionBuffer, normalBuffer, textureBuffer, smoothedNormalsBuffer, EBO;
GLuint PDBuffer, CurvatureBuffer;
GLuint maxPDVBO, maxCurvVBO, minPDVBO, minCurvVBO;
std::vector<glm::vec3> vertices;
std::vector<glm::vec3> normals;
std::vector<std::array<unsigned int, 3>> faces;
std::vector<glm::vec2> textureCoordinates;
std::vector<glm::vec3> tangents;
std::vector<glm::vec3> bitangents;
std::vector<GLuint> indices;
std::vector<glm::vec4> PDs;
std::vector<GLfloat> PrincipalCurvatures;
std::vector<glm::vec4> maxPDs;
std::vector<glm::vec4> minPDs;
std::vector<GLfloat> maxCurvs;
std::vector<GLfloat> minCurvs;
unsigned int numVertices;
unsigned int numNormals;
unsigned int numFaces;
unsigned int numIndices;
std::string path;
GLfloat diagonalLength = 0.0f;
GLfloat modelScaleFactor = 1.0f;
GLfloat minDistance;
glm::vec3 center = glm::vec3(0.0f);
GLuint size;
bool isSet = false;
bool curvaturesCalculated = false;
Model(std::string path) {
this->path = path;
if (!this->loadAssimp()) { std::cout << "Model at " << path << " not loaded!\n"; };
this->boundingBox();
this->minDistance = this->getMinDistance();
this->size = this->vertices.size();
this->setupCurvatures();
this->setup();
}
void setup() {
//std::cout << "Setting up buffers.\n";
//vector.data() == &vector[0]
glGenVertexArrays(1, &VAO); //vertex array object
glGenBuffers(1, &positionBuffer); //vertex buffer object
glGenBuffers(1, &normalBuffer); //vertex buffer object
glGenBuffers(1, &textureBuffer); //vertex buffer object
glGenBuffers(1, &EBO);
//glGenBuffers(1, &PDBuffer);
//glGenBuffers(1, &CurvBuffer);
//VAO
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), normals.data(), GL_STATIC_DRAW);
//EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
// glVertexAttribPointer(index, size, type, normalized(bool), stride(byte offset between), pointer(offset))
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, textureBuffer);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
// Principal Directions / Principal Curvatures (As VBOs)
if (this->curvaturesCalculated) {
//Send PDs / PCs as attrbutes per vertex, just to uncomplicate some of this process.
glBindBuffer(GL_ARRAY_BUFFER, maxPDVBO);
glBufferData(GL_ARRAY_BUFFER, PDs.size() / 2 * sizeof(glm::vec4), PDs.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 0, (void*)0); //Our PDs are vec4s due to SSBO reasons.
glBindBuffer(GL_ARRAY_BUFFER, minPDVBO);
glBufferData(GL_ARRAY_BUFFER, PDs.size() / 2 * sizeof(glm::vec4), &PDs[this->vertices.size()], GL_STATIC_DRAW);
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, maxCurvVBO);
glBufferData(GL_ARRAY_BUFFER, PrincipalCurvatures.size() / 2 * sizeof(GLfloat), PrincipalCurvatures.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(5);
glVertexAttribPointer(5, 1, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, minCurvVBO);
glBufferData(GL_ARRAY_BUFFER, PrincipalCurvatures.size() / 2 * sizeof(GLfloat), &PrincipalCurvatures[this->vertices.size()], GL_STATIC_DRAW);
glEnableVertexAttribArray(6);
glVertexAttribPointer(6, 1, GL_FLOAT, GL_FALSE, 0, (void*)0);
//glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//std::cout << "Ready to render.\n";
this->isSet = true;
return;
}
//draw function
bool render(GLuint shader) {
if (!isSet) { this->setup(); }
glUseProgram(shader);
glBindVertexArray(VAO);
//Rebind SSBOs
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, PDBuffer);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 8, CurvatureBuffer);
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(indices.size()), GL_UNSIGNED_INT, 0);
//glDisableVertexAttribArray(0);
//glDisableVertexAttribArray(1);
//glBindVertexArray(0);
return true;
}
void boundingBox() {
//simple implemetation calculating model boundary box size
float maxX = vertices[0].x, maxY = vertices[0].y, maxZ = vertices[0].z;
float minX = vertices[0].x, minY = vertices[0].y, minZ = vertices[0].z;
for (int i = 1; i < vertices.size(); i++) {
(vertices[i].x > maxX) ? maxX = vertices[i].x : 0;
(vertices[i].y > maxY) ? maxY = vertices[i].y : 0;
(vertices[i].z > maxZ) ? maxZ = vertices[i].z : 0;
(vertices[i].x < minX) ? minX = vertices[i].x : 0;
(vertices[i].y < minY) ? minY = vertices[i].y : 0;
(vertices[i].z < minZ) ? minZ = vertices[i].z : 0;
}
//center of model
this->center = glm::vec3((maxX + minX) / 2.0f, (maxY + minY) / 2.0f, (maxZ + minZ) / 2.0f);
this->diagonalLength = glm::length(glm::vec3(maxX - minX, maxY - minY, maxZ - minZ));
this->modelScaleFactor = 1.0f / diagonalLength;
}
float getMinDistance() {
float minDist = glm::length(vertices[0] - vertices[1]);
for (int i = 2; i < vertices.size(); i++) {
float dis = glm::length(vertices[i] - vertices[0]);
if (dis < minDist && dis>0.0f) minDist = dis;
}
return minDist;
}
//Calculates principal curvatures and principal directions per vertex
//Using a compute shader with SSBOs
void setupCurvatures() {
//Computed with CPU c++ code only for comparison with GPU compute shaders ----
//per face
for (int i = 0; i < this->numFaces; i++) {
}
}
/*
~Model() {
glDeleteBuffers()
}
*/
float compareVertices(std::vector<glm::vec3> v1, std::vector<glm::vec3> v2) {
float err = 0.0f;
for (int i = 0; i < v1.size();i++) {
err += glm::distance(v1[i],v2[i]);
}
return err;
}
bool loadAssimp() {
Assimp::Importer importer;
if (!this->vertices.empty()) {
return false; //if not empty return
}
std::cout << "Loading file : " << this->path << ".\n";
//aiProcess_Triangulate !!!
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_JoinIdenticalVertices | aiProcess_CalcTangentSpace | aiProcess_GenUVCoords); //aiProcess_JoinIdenticalVertices | aiProcess_SortByPType);
if (!scene) {
fprintf(stderr, importer.GetErrorString());
return false;
}
// TODO : In this code we just use the 1st mesh (for now)
const aiMesh* mesh = scene->mMeshes[0];
// Fill vertices positions
//std::cout << "Number of vertices :" << mesh->mNumVertices << "\n";
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
aiVector3D pos = mesh->mVertices[i];
this->vertices.push_back(glm::vec3(pos.x, pos.y, pos.z));
}
// Fill vertices texture coordinates
if (mesh->HasTextureCoords(0)) {
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
aiVector3D UVW = mesh->mTextureCoords[0][i]; // Assume only 1 set of UV coords; AssImp supports 8 UV sets.
this->textureCoordinates.push_back(glm::vec2(UVW.x, UVW.y));
}
}
// Fill vertices normals
if (mesh->HasNormals()) {
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
// std::cout<<"Number of Vertices : "<<mesh->mNumVertices<<"\n";
aiVector3D n = mesh->mNormals[i];
this->normals.push_back(glm::normalize(glm::vec3(n.x, n.y, n.z)));
}
}
else {
//std::cout << "Model has no normals.\n";
//mesh->
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
// std::cout<<"Number of Vertices : "<<mesh->mNumVertices<<"\n";
aiVector3D n = mesh->mNormals[i];
this->normals.push_back(glm::normalize(glm::vec3(n.x, n.y, n.z)));
}
}
// Fill face indices
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
std::array<unsigned int, 3> fac;
for (unsigned int j = 0; j < mesh->mFaces[i].mNumIndices; j++) {
this->indices.push_back(mesh->mFaces[i].mIndices[j]);
fac[j] = mesh->mFaces[i].mIndices[j];
}
this->faces.push_back(fac);
}
std::cout << "Number of vertices : " << this->vertices.size() << "\n";
std::cout << "Number of normals : " << this->normals.size() << "\n";
std::cout << "Number of indices : " << this->indices.size() << "\n";
std::cout << "Number of faces : " << this->faces.size() << "\n";
this->numVertices = this->vertices.size();
this->numNormals = this->normals.size();
this->numFaces = this->faces.size();
this->numIndices = this->indices.size();
// The "scene" pointer will be deleted automatically by "importer"
return true;
}
};
//end of Model class
bool loadAssimp(
const char* path,
std::vector<glm::vec3>& out_vertices,
std::vector<glm::vec3>& out_normals,
std::vector<unsigned int>& out_indices
) {
std::vector<unsigned int> indices;
std::vector<glm::vec3> vertices;
std::vector<glm::vec2> uvs;
std::vector<glm::vec3> normals;
Assimp::Importer importer;
printf("Loading file : %s...\n", path);
//aiProcess_Triangulate !!!
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_JoinIdenticalVertices | aiProcess_CalcTangentSpace | aiProcess_GenUVCoords); //aiProcess_JoinIdenticalVertices | aiProcess_SortByPType);
if (!scene) {
fprintf(stderr, importer.GetErrorString());
return false;
}
// TODO : In this code we just use the 1st mesh (for now)
const aiMesh* mesh = scene->mMeshes[0];
// Fill vertices positions
//std::cout << "Number of vertices :" << mesh->mNumVertices << "\n";
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
aiVector3D pos = mesh->mVertices[i];
out_vertices.push_back(glm::vec3(pos.x, pos.y, pos.z));
}
// Fill vertices texture coordinates
if (mesh->HasTextureCoords(0)) {
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
aiVector3D UVW = mesh->mTextureCoords[0][i]; // Assume only 1 set of UV coords; AssImp supports 8 UV sets.
uvs.push_back(glm::vec2(UVW.x, UVW.y));
}
}
// Fill vertices normals
if (mesh->HasNormals()) {
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
// std::cout<<"Number of Vertices : "<<mesh->mNumVertices<<"\n";
aiVector3D n = mesh->mNormals[i];
out_normals.push_back(glm::normalize(glm::vec3(n.x, n.y, n.z)));
}
}
else {
//std::cout << "Model has no normals.\n";
//mesh->
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
// std::cout<<"Number of Vertices : "<<mesh->mNumVertices<<"\n";
aiVector3D n = mesh->mNormals[i];
out_normals.push_back(glm::normalize(glm::vec3(n.x, n.y, n.z)));
}
}
// Fill face indices
//std::cout << "Number of faces :" << mesh->mNumFaces << "\n";
//std::cout << "Number of indices in faces :" << mesh->mFaces[0].mNumIndices << "\n";
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
for (unsigned int j = 0; j < mesh->mFaces[i].mNumIndices; j++)
out_indices.push_back(mesh->mFaces[i].mIndices[j]);
}
std::cout << "Size of vertices : " << out_vertices.size() << "\n";
std::cout << "Size of normals : " << out_normals.size() << "\n";
std::cout << "Size of indices : " << out_indices.size() << "\n";
// The "scene" pointer will be deleted automatically by "importer"
return true;
}