forked from yiming-cai/OursCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
744 lines (625 loc) · 26.6 KB
/
Copy pathModel.cpp
File metadata and controls
744 lines (625 loc) · 26.6 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
#include "Model.h"
void set_float4(float f[4], float a, float b, float c, float d)
{
f[0] = a;
f[1] = b;
f[2] = c;
f[3] = d;
}
void color4_to_float4(const aiColor4D *c, float f[4])
{
f[0] = c->r;
f[1] = c->g;
f[2] = c->b;
f[3] = c->a;
}
Model::Model(std::string p_filepath) {
bool success = false;
const auto it = references.find(p_filepath);
if (it != references.end())
{
filepath = p_filepath;
scene = (*it).second->getScenePtr();
if (scene != nullptr) success = true;
texCoordsArrays = (*it).second->getTexArrays();
faceArrays = (*it).second->getFaceArrays();
IDs = (*it).second->getIDs();
textureIdMap = (*it).second->getTextureIdMap();
myMeshes = (*it).second->getMeshes();
}
else
{
filepath = p_filepath;
success = importObj(filepath);
// loads the textures
loadGLTextures(scene);
// loads the vao and materials, and tex coords
genVAOsAndUniformBuffer(scene);
}
if (success)
{
setMinMaxObjectCoord(scene);
setBoundingSphere();
}
else
{
std::cerr << "Failed to load " << filepath << std::endl;
}
std::cerr << filepath << ": loaded " << myMeshes.size() << " meshes!" << std::endl;
references[filepath] = this;
}
Model::~Model()
{
//delBuffers();
for (unsigned int i = 0; i < myMeshes.size(); ++i) {
glDeleteVertexArrays(1, &(myMeshes[i].vao));
glDeleteTextures(1, &(myMeshes[i].texIndex));
glDeleteBuffers(1, &(myMeshes[i].uniformBlockIndex));
}
}
void Model::setBoundingBox() {
bounding_box->setVertices(getAABBBoundingBoxVertices());
//bounding_box->update();
bounding_box_hand->setVertices(getAABBBoundingBoxVertices());
//bounding_box_hand->update();
}
bool Model::checkBounds(Model * other) {
bool collide = false;
bool return_collide = false;
//maybe update
return return_collide;
}
/*
* source: http://www.assimp.org
*/
bool Model::importObj(const std::string& path)
{
//check if file exists
std::ifstream fin(path.c_str());
if (!fin.fail()) {
fin.close();
}
else {
std::cerr << ("Couldn't open file: %s\n", path.c_str()) << std::endl;
std::cerr << ("%s\n", importer.GetErrorString()) << std::endl;
return false;
}
// And have it read the given file with some example postprocessing
// Usually - if speed is not the most important aspect for you - you'll
// probably to request more postprocessing than we do in this example.
scene = importer.ReadFile(path,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType |
aiProcess_FlipUVs);
// If the import failed, report it
if (!scene)
{
std::cerr << (importer.GetErrorString()) << std::endl;
return false;
}
return true;
}
//void Model::genBuffers()
//{
// glGenVertexArrays(1, &VAO);
// glGenBuffers(1, &VBO);
// glGenBuffers(1, &EBO);
// glGenBuffers(1, &CBO);
// glGenBuffers(1, &NBO);
//}
//void Model::delBuffers()
//{
// glDeleteVertexArrays(1, &VAO);
// glDeleteBuffers(1, &VBO);
// glDeleteBuffers(1, &EBO);
// glDeleteBuffers(1, &TBO);
// glDeleteBuffers(1, &NBO);
//}
/*
* source: http://www.lighthouse3d.com/cg-topics/code-samples/importing-3d-models-with-assimp/
*/
void Model::genVAOsAndUniformBuffer(const aiScene *sc) {
struct MyMesh aMesh;
struct MyMaterial aMat;
GLuint buffer;
// For each mesh
for (unsigned int n = 0; n < sc->mNumMeshes; ++n)
{
const aiMesh* mesh = sc->mMeshes[n];
// create array with faces
// have to convert from Assimp format to array
//unsigned int *faceArray;
//faceArray = (unsigned int *)malloc(sizeof(unsigned int) * mesh->mNumFaces * 3);
unsigned int face_size = mesh->mNumFaces * 3;
faceArrays.push_back( std::vector<unsigned int> (face_size) );
int latest = faceArrays.size() - 1;
unsigned int faceIndex = 0;
for (unsigned int t = 0; t < mesh->mNumFaces; ++t) {
const aiFace* face = &mesh->mFaces[t];
memcpy(&faceArrays[latest][faceIndex], face->mIndices, 3 * sizeof(unsigned int));
faceIndex += 3;
}
aMesh.numFaces = sc->mMeshes[n]->mNumFaces;
// generate Vertex Array for mesh
glGenVertexArrays(1, &(aMesh.vao));
glBindVertexArray(aMesh.vao);
// buffer for faces
glGenBuffers(1, &aMesh.ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, aMesh.ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * face_size, &(faceArrays[latest][0]), GL_STATIC_DRAW);
// buffer for vertex positions
if (mesh->HasPositions()) {
glGenBuffers(1, &aMesh.vbo);
glBindBuffer(GL_ARRAY_BUFFER, aMesh.vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * mesh->mNumVertices, mesh->mVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(vertexLoc);
glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0);
}
// buffer for vertex normals
if (mesh->HasNormals()) {
glGenBuffers(1, &aMesh.nbo);
glBindBuffer(GL_ARRAY_BUFFER, aMesh.nbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * mesh->mNumVertices, mesh->mNormals, GL_STATIC_DRAW);
glEnableVertexAttribArray(normalLoc);
glVertexAttribPointer(normalLoc, 3, GL_FLOAT, 0, 0, 0);
}
// buffer for vertex texture coordinates
if (mesh->HasTextureCoords(0)) {
//float * texCoords = (float *)malloc(sizeof(float) * 2 * mesh->mNumVertices);
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, mesh->mNumVertices * 3 * sizeof(GLfloat), mesh->mTextureCoords[0], GL_STATIC_DRAW);
glVertexAttribPointer(texCoordLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(texCoordLoc);
//std::cerr << "TEXTURE COORD FOUND!" << std::endl;
}
// unbind buffers
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// create material uniform buffer
aiMaterial *mtl = sc->mMaterials[mesh->mMaterialIndex];
/* ---------------------- Find texture id of the mesh ----------------------------------- */
aiString texPath; //contains filename of texture
if ( mtl->GetTextureCount(aiTextureType_DIFFUSE) > 0 )
{
mtl->GetTexture(aiTextureType_DIFFUSE, 0, &texPath);
//bind texture
unsigned int texId = textureIdMap[texPath.data];
aMesh.texIndex = texId;
aMat.texCount = 1;
}
else
aMat.texCount = 0;
/* --------------------------------------------------------------------------------------- */
/* -------------------------For parsing material properties -------------------------------- */
float c[4];
set_float4(c, 0.8f, 0.8f, 0.8f, 1.0f);
aiColor4D diffuse;
if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse))
color4_to_float4(&diffuse, c);
memcpy(aMat.diffuse, c, sizeof(c));
set_float4(c, 0.2f, 0.2f, 0.2f, 1.0f);
aiColor4D ambient;
if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &ambient))
color4_to_float4(&ambient, c);
memcpy(aMat.ambient, c, sizeof(c));
set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f);
aiColor4D specular;
if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &specular))
color4_to_float4(&specular, c);
memcpy(aMat.specular, c, sizeof(c));
set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f);
aiColor4D emission;
if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_EMISSIVE, &emission))
color4_to_float4(&emission, c);
memcpy(aMat.emissive, c, sizeof(c));
float shininess = 0.0;
unsigned int max;
aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS, &shininess, &max);
aMat.shininess = shininess;
float opacity = 1.0f;
aiGetMaterialFloatArray(mtl, AI_MATKEY_OPACITY, &opacity, &max);
aMat.opacity = opacity;
/* -------------------------------------------------------------------------------------------- */
/* ------------------------ Send the Material to shader ---------------------------------------- */
glGenBuffers(1, &(aMesh.uniformBlockIndex));
glBindBuffer(GL_UNIFORM_BUFFER, aMesh.uniformBlockIndex);
glBufferData(GL_UNIFORM_BUFFER, sizeof(aMat), (void *)(&aMat), GL_STATIC_DRAW);
/* --------------------------------------------------------------------------------------------- */
// unbind buffer
glBindBuffer(GL_UNIFORM_BUFFER, 0);
myMeshes.push_back(aMesh);
}
}
int Model::loadGLTextures(const aiScene * sc)
{
bool success;
/* initialization of DevIL */
//ilInit();
///* scan scene's materials for textures */
//for (unsigned int m = 0; m<scene->mNumMaterials; ++m)
//{
// aiString path; // filename
// for (int i = 0; i < scene->mMaterials[m]->GetTextureCount(aiTextureType_DIFFUSE); i++)
// {
// scene->mMaterials[m]->GetTexture(aiTextureType_DIFFUSE, i, &path);
//
// //fill map with textures, OpenGL image ids set to 0
// textureIdMap[path.data] = 0;
// }
//}
// For each mesh
for (unsigned int n = 0; n < sc->mNumMaterials; ++n)
{
const aiMaterial * mat = sc->mMaterials[n];
/* ---------------------- Find texture id of the mesh ----------------------------------- */
//std::cerr << "NONE texture: " << (int)mat->GetTextureCount(aiTextureType_NONE) << std::endl;
//std::cerr << "Diffused texture: " << (int)mat->GetTextureCount(aiTextureType_DIFFUSE) << std::endl;
//std::cerr << "Specular texture: " << (int)mat->GetTextureCount(aiTextureType_SPECULAR) << std::endl;
//std::cerr << "AMBIENT texture: " << (int)mat->GetTextureCount(aiTextureType_AMBIENT) << std::endl;
//std::cerr << "EMISSIVE texture: " << (int)mat->GetTextureCount(aiTextureType_EMISSIVE) << std::endl;
//std::cerr << "HEIGHT texture: " << (int)mat->GetTextureCount(aiTextureType_HEIGHT) << std::endl;
//std::cerr << "NORMALS texture: " << (int)mat->GetTextureCount(aiTextureType_NORMALS) << std::endl;
//std::cerr << "SHININESS texture: " << (int)mat->GetTextureCount(aiTextureType_SHININESS) << std::endl;
//std::cerr << "OPACITY texture: " << (int)mat->GetTextureCount(aiTextureType_OPACITY) << std::endl;
//std::cerr << "DISPLACEMENT texture: " << (int)mat->GetTextureCount(aiTextureType_DISPLACEMENT) << std::endl;
//std::cerr << "LIGHTMAP texture: " << (int)mat->GetTextureCount(aiTextureType_LIGHTMAP) << std::endl;
//std::cerr << "REFLECTION texture: " << (int)mat->GetTextureCount(aiTextureType_REFLECTION) << std::endl;
//std::cerr << "UNKNOWN texture: " << (int)mat->GetTextureCount(aiTextureType_UNKNOWN) << std::endl;
for (int i = 0; i < mat->GetTextureCount(aiTextureType_DIFFUSE); i++)
{
aiString texPath;
mat->GetTexture(aiTextureType_DIFFUSE, i, &texPath);
textureIdMap[texPath.data] = 0;
}
}
if (textureIdMap.size() > 0)
{
int numTextures = textureIdMap.size();
std::cerr << numTextures << std::endl;
std::vector<GLuint> textureIds(numTextures);
glGenTextures(numTextures, &(textureIds[0]));
// some stbi outputs
int width, height, nrChannels;
int found = filepath.find_last_of("/\\");
std::string path = filepath.substr(0,found+1);
auto itr = textureIdMap.begin();
for (int i = 0; itr != textureIdMap.end(); ++i, ++itr)
{
std::string filename = (*itr).first;
filename = path + filename;
std::cerr << "Trying to load texture file: " << filename << " of length " << filename.size() << std::endl;
(*itr).second = textureIds[i];
unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrChannels, 4);
if (data)
{
glBindTexture(GL_TEXTURE_2D, textureIds[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
std::cerr << "Loaded texture!" << filename << std::endl;
stbi_image_free(data);
}
else
{
std::cerr << "wrong in load texture " << filename << std::endl;
stbi_image_free(data);
}
}
//return success;
return true;
}
return false;
}
void Model::initShader(GLuint shaderProgram)
{
glUniformBlockBinding(shaderProgram, glGetUniformBlockIndex(shaderProgram, "Material"), materialUniLoc);
}
void Model::switchShader(GLuint shaderProgram)
{
glUseProgram(shaderProgram);
}
void Model::render(GLuint shaderProgram)
{
if (scene == nullptr) return;
//glUseProgram(shaderProgram);
for (auto mesh : myMeshes)
{
// We need to calcullate this because modern OpenGL does not keep track of any matrix other than the viewport (D)
// Consequently, we need to forward the projection, view, and model matrices to the shader programs
// Get the location of the uniform variables "projection" and "modelview"
GLuint uProjection = glGetUniformLocation(shaderProgram, "projection");
GLuint uView = glGetUniformLocation(shaderProgram, "view");
GLuint uModel = glGetUniformLocation(shaderProgram, "model");
GLuint uCam = glGetUniformLocation(shaderProgram, "cam_pos");
//printf("%f %f %f\n", k.x, k.y, k.z);
// Now send these values to the shader program
glUniformMatrix4fv(uProjection, 1, GL_FALSE, &P[0][0]);
glUniformMatrix4fv(uView, 1, GL_FALSE, &V[0][0]);
glUniformMatrix4fv(uModel, 1, GL_FALSE, &modelMatrix[0][0]);
glUniform3fv(uCam, 1, &(camera->camera_pos[0]) );
// Now draw the cube. We simply need to bind the VAO associated with it.
glBindVertexArray(mesh.vao);
glBindBufferRange(GL_UNIFORM_BUFFER, materialUniLoc, mesh.uniformBlockIndex, 0, sizeof(struct MyMaterial));
// bind texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mesh.texIndex );
// Tell OpenGL to draw with triangles, using 36 indices, the type of the indices, and the offset to start from
glDrawElements(GL_TRIANGLES, mesh.numFaces * 3, GL_UNSIGNED_INT, 0);
// Unbind the VAO when we're done so we don't accidentally draw extra stuff or tamper with its bound buffers
glBindVertexArray(0);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
}
void Model::draw(glm::mat4 C, GLuint shaderProgram)
{
if (scene == nullptr) return;
setModelMatrix(C);
render(shaderProgram);
}
void Model::update()
{
// temp: check out the rotation
setModelMatrix( glm::rotate(getUModelMatrix(), 1.0f*glm::pi<float>()/180.0f, glm::vec3(1.0f,1.0f,0.0f)) );
}
glm::vec3 Model::getPosition()
{
glm::vec3 pos((min_max_pairs_xyz[INDEX_X_MIN] + min_max_pairs_xyz[INDEX_X_MAX]) / 2.0f,
(min_max_pairs_xyz[INDEX_Y_MIN] + min_max_pairs_xyz[INDEX_Y_MAX]) / 2.0f,
(min_max_pairs_xyz[INDEX_Z_MIN] + min_max_pairs_xyz[INDEX_Z_MAX]) / 2.0f);
return glm::vec3(modelMatrix * glm::vec4(pos, 1.0f));
}
void Model::setCamera(Camera * cam)
{
camera = cam;
}
// in world coordinates
std::vector<glm::vec3> Model::getOBBBoundingPlanes()
{
std::vector<glm::vec3> planes(12);
planes[POINT_LEFT] = glm::vec3( modelMatrix * glm::vec4(min_max_pairs_xyz[INDEX_X_MIN], 0.0f, 0.0f, 1.0f) );
planes[POINT_RIGHT] = glm::vec3(modelMatrix * glm::vec4(min_max_pairs_xyz[INDEX_X_MAX], 0.0f, 0.0f, 1.0f) );
planes[POINT_BOTTOM] = glm::vec3(modelMatrix * glm::vec4(0.0f, min_max_pairs_xyz[INDEX_Y_MIN], 0.0f, 1.0f) );
planes[POINT_TOP] = glm::vec3(modelMatrix * glm::vec4( 0.0f, min_max_pairs_xyz[INDEX_Y_MAX], 0.0f, 1.0f) );
planes[POINT_BACK] = glm::vec3(modelMatrix * glm::vec4( 0.0f, 0.0f, min_max_pairs_xyz[INDEX_Z_MIN], 1.0f) );
planes[POINT_FRONT] = glm::vec3(modelMatrix * glm::vec4( 0.0f, 0.0f, min_max_pairs_xyz[INDEX_Z_MAX], 1.0f) );
std::vector<glm::vec3> refPoints_a(12); // using a bit more memory, but doesn't rly matter
refPoints_a[POINT_LEFT] = glm::vec3( modelMatrix * glm::vec4(min_max_pairs_xyz[INDEX_X_MIN], 0.0f, -1.0f, 1.0f) );
refPoints_a[POINT_RIGHT] = glm::vec3(modelMatrix * glm::vec4(min_max_pairs_xyz[INDEX_X_MAX], 0.0f, -1.0f, 1.0f) );
refPoints_a[POINT_BOTTOM] = glm::vec3(modelMatrix * glm::vec4(0.0f, min_max_pairs_xyz[INDEX_Y_MIN], -1.0f, 1.0f) );
refPoints_a[POINT_TOP] = glm::vec3(modelMatrix * glm::vec4(0.0f, min_max_pairs_xyz[INDEX_Y_MAX], -1.0f, 1.0f) );
refPoints_a[POINT_BACK] = glm::vec3(modelMatrix * glm::vec4(1.0f, 0.0f, min_max_pairs_xyz[INDEX_Z_MIN], 1.0f) );
refPoints_a[POINT_FRONT] = glm::vec3(modelMatrix * glm::vec4(1.0f, 0.0f, min_max_pairs_xyz[INDEX_Z_MAX], 1.0f) );
std::vector<glm::vec3> refPoints_b(12); // using a bit more memory, but doesn't rly matter
refPoints_b[POINT_LEFT] = glm::vec3(modelMatrix * glm::vec4(min_max_pairs_xyz[INDEX_X_MIN], -1.0f, 0.0f, 1.0f) );
refPoints_b[POINT_RIGHT] = glm::vec3(modelMatrix * glm::vec4(min_max_pairs_xyz[INDEX_X_MAX], 1.0f, 0.0f, 1.0f) );
refPoints_b[POINT_BOTTOM] = glm::vec3(modelMatrix * glm::vec4(1.0f, min_max_pairs_xyz[INDEX_Y_MIN], 0.0f,1.0f) );
refPoints_b[POINT_TOP] = glm::vec3(modelMatrix * glm::vec4(-1.0f, min_max_pairs_xyz[INDEX_Y_MAX], 0.0f, 1.0f) );
refPoints_b[POINT_BACK] = glm::vec3(modelMatrix * glm::vec4(0.0f, -1.0f, min_max_pairs_xyz[INDEX_Z_MIN], 1.0f) );
refPoints_b[POINT_FRONT] = glm::vec3(modelMatrix * glm::vec4(0.0f, -1.0f, min_max_pairs_xyz[INDEX_Z_MAX], 1.0f) );
// calculate the normals based on the cross products
planes[NORMAL_LEFT] = glm::normalize( glm::cross( (refPoints_a[POINT_LEFT] - planes[POINT_LEFT]), (refPoints_b[POINT_LEFT] - planes[POINT_LEFT])) );
planes[NORMAL_RIGHT] = glm::normalize(glm::cross((refPoints_a[POINT_RIGHT] - planes[POINT_RIGHT]), (refPoints_b[POINT_RIGHT] - planes[POINT_RIGHT])));
planes[NORMAL_BOTTOM] = glm::normalize(glm::cross((refPoints_a[POINT_BOTTOM] - planes[POINT_BOTTOM]), (refPoints_b[POINT_BOTTOM] - planes[POINT_BOTTOM])));
planes[NORMAL_TOP] = glm::normalize(glm::cross((refPoints_a[POINT_TOP] - planes[POINT_TOP]), (refPoints_b[POINT_TOP] - planes[POINT_TOP])));
planes[NORMAL_BACK] = glm::normalize(glm::cross((refPoints_a[POINT_BACK] - planes[POINT_BACK]), (refPoints_b[POINT_BACK] - planes[POINT_BACK])));
planes[NORMAL_FRONT] = glm::normalize(glm::cross((refPoints_a[POINT_FRONT] - planes[POINT_FRONT]), (refPoints_b[POINT_FRONT] - planes[POINT_FRONT])));
return planes;
}
void Model::centerAndScale(float scale)
{
if (scene == nullptr) return;
float x_offset = (min_max_pairs_xyz[INDEX_X_MIN] + min_max_pairs_xyz[INDEX_X_MAX]) / -2.0f;
float y_offset = (min_max_pairs_xyz[INDEX_Y_MIN] + min_max_pairs_xyz[INDEX_Y_MAX]) / -2.0f;
float z_offset = (min_max_pairs_xyz[INDEX_Z_MIN] + min_max_pairs_xyz[INDEX_Z_MAX]) / -2.0f;
glm::mat4 mat = glm::translate(glm::mat4(1.0f), glm::vec3(x_offset, y_offset, z_offset));
std::vector<float> temp;
temp.push_back(min_max_pairs_xyz[INDEX_X_MIN] + x_offset);
temp.push_back(min_max_pairs_xyz[INDEX_X_MAX] + x_offset);
temp.push_back(min_max_pairs_xyz[INDEX_Y_MIN] + y_offset);
temp.push_back(min_max_pairs_xyz[INDEX_Y_MAX] + y_offset);
temp.push_back(min_max_pairs_xyz[INDEX_Z_MIN] + z_offset);
temp.push_back(min_max_pairs_xyz[INDEX_Z_MAX] + z_offset);
float max = 0.0f;
for (const float p : temp)
{
if (abs(p) > max)
{
max = abs(p);
}
}
scale_matrix = glm::scale(glm::mat4(1.0f), glm::vec3(scale / max / 2.0f )) * mat;
modelMatrix = unscaledModelMatrix * scale_matrix;
}
std::vector<glm::vec3> Model::getAABBBoundingBoxVertices()
{
std::vector< float > min_max_world_pairs_xyz = getAABBBoundingBoxMinMax();
std::vector< glm::vec3 > points(8);
// front face
points[BBV_BOTTOM_LEFT_NEAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MIN], min_max_world_pairs_xyz[INDEX_Y_MIN], min_max_world_pairs_xyz[INDEX_Z_MAX]);
points[BBV_BOTTOM_RIGHT_NEAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MAX], min_max_world_pairs_xyz[INDEX_Y_MIN], min_max_world_pairs_xyz[INDEX_Z_MAX]);
points[BBV_TOP_RIGHT_NEAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MAX], min_max_world_pairs_xyz[INDEX_Y_MAX], min_max_world_pairs_xyz[INDEX_Z_MAX]);
points[BBV_TOP_LEFT_NEAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MIN], min_max_world_pairs_xyz[INDEX_Y_MAX], min_max_world_pairs_xyz[INDEX_Z_MAX]);
// back face
points[BBV_BOTTOM_LEFT_FAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MIN], min_max_world_pairs_xyz[INDEX_Y_MIN], min_max_world_pairs_xyz[INDEX_Z_MIN]);
points[BBV_BOTTOM_RIGHT_FAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MAX], min_max_world_pairs_xyz[INDEX_Y_MIN], min_max_world_pairs_xyz[INDEX_Z_MIN]);
points[BBV_TOP_RIGHT_FAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MAX], min_max_world_pairs_xyz[INDEX_Y_MAX], min_max_world_pairs_xyz[INDEX_Z_MIN]);
points[BBV_TOP_LEFT_FAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MIN], min_max_world_pairs_xyz[INDEX_Y_MAX], min_max_world_pairs_xyz[INDEX_Z_MIN]);
return points;
}
std::vector<glm::vec3> Model::getAABBTightestBoundingVertices()
{
std::vector< float > min_max_world_pairs_xyz = getAABBTightestBoundingBoxMinMax();
std::vector< glm::vec3 > points(8);
// front face
points[BBV_BOTTOM_LEFT_NEAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MIN], min_max_world_pairs_xyz[INDEX_Y_MIN], min_max_world_pairs_xyz[INDEX_Z_MAX]);
points[BBV_BOTTOM_RIGHT_NEAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MAX], min_max_world_pairs_xyz[INDEX_Y_MIN], min_max_world_pairs_xyz[INDEX_Z_MAX]);
points[BBV_TOP_RIGHT_NEAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MAX], min_max_world_pairs_xyz[INDEX_Y_MAX], min_max_world_pairs_xyz[INDEX_Z_MAX]);
points[BBV_TOP_LEFT_NEAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MIN], min_max_world_pairs_xyz[INDEX_Y_MAX], min_max_world_pairs_xyz[INDEX_Z_MAX]);
// back face
points[BBV_BOTTOM_LEFT_FAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MIN], min_max_world_pairs_xyz[INDEX_Y_MIN], min_max_world_pairs_xyz[INDEX_Z_MIN]);
points[BBV_BOTTOM_RIGHT_FAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MAX], min_max_world_pairs_xyz[INDEX_Y_MIN], min_max_world_pairs_xyz[INDEX_Z_MIN]);
points[BBV_TOP_RIGHT_FAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MAX], min_max_world_pairs_xyz[INDEX_Y_MAX], min_max_world_pairs_xyz[INDEX_Z_MIN]);
points[BBV_TOP_LEFT_FAR] = glm::vec3(min_max_world_pairs_xyz[INDEX_X_MIN], min_max_world_pairs_xyz[INDEX_Y_MAX], min_max_world_pairs_xyz[INDEX_Z_MIN]);
return points;
}
// AABB, in world coordinates
std::vector<float> Model::getAABBBoundingBoxMinMax()
{
// min max in world coordinates
std::vector<glm::vec3> minmax = getOBBBoundingBoxVertices();
std::vector<float> x_val, y_val, z_val;
for (auto& v : minmax)
{
x_val.push_back(v.x);
y_val.push_back(v.y);
z_val.push_back(v.z);
}
// sorts ascending order by default
std::sort(x_val.begin(), x_val.end());
std::sort(y_val.begin(), y_val.end());
std::sort(z_val.begin(), z_val.end());
std::vector<float> ret(6);
ret[INDEX_X_MIN] = x_val[0];
ret[INDEX_Y_MIN] = y_val[0];
ret[INDEX_Z_MIN] = z_val[0];
ret[INDEX_X_MAX] = x_val[x_val.size() - 1];
ret[INDEX_Y_MAX] = y_val[y_val.size() - 1];
ret[INDEX_Z_MAX] = z_val[z_val.size() - 1];
return ret;
}
std::vector<float> Model::getAABBTightestBoundingBoxMinMax()
{
std::vector<float> min_max_temp = { FLT_MAX, FLT_MIN, FLT_MAX, FLT_MIN, FLT_MAX, FLT_MIN };
for (unsigned int n = 0; n < scene->mNumMeshes; ++n)
{
const aiMesh* mesh = scene->mMeshes[n];
for (unsigned int i = 0; i < mesh->mNumVertices; i++)
{
const aiVector3D obj_p = mesh->mVertices[i];
glm::vec3 p(obj_p[0], obj_p[1], obj_p[2]);
// transform into world coordinates
p = glm::vec3(modelMatrix * glm::vec4(p, 1.0f));
if (p[0] < min_max_temp[INDEX_X_MIN])
{
min_max_temp[INDEX_X_MIN] = p[0];
}
if (p[0] > min_max_temp[INDEX_X_MAX])
{
min_max_temp[INDEX_X_MAX] = p[0];
}
if (p[1] < min_max_temp[INDEX_Y_MIN])
{
min_max_temp[INDEX_Y_MIN] = p[1];
}
if (p[1] > min_max_temp[INDEX_Y_MAX])
{
min_max_temp[INDEX_Y_MAX] = p[1];
}
if (p[2] < min_max_temp[INDEX_Z_MIN])
{
min_max_temp[INDEX_Z_MIN] = p[2];
}
if (p[2] > min_max_temp[INDEX_Z_MAX])
{
min_max_temp[INDEX_Z_MAX] = p[2];
}
}
}
return min_max_temp;
}
std::pair<glm::vec3, float> Model::getBoundingSphere()
{
std::pair< glm::vec3, float > boundingSphereWorldCoord;
boundingSphereWorldCoord.first = glm::vec3(modelMatrix * glm::vec4(boundingSphereObjectCoord.first, 1.0f));
boundingSphereWorldCoord.second = boundingSphereObjectCoord.second;
// check if we already ever found a bounding sphere
return boundingSphereWorldCoord;
}
void Model::setMinMaxObjectCoord(const aiScene * sc)
{
min_max_pairs_xyz = { FLT_MAX, FLT_MIN, FLT_MAX, FLT_MIN, FLT_MAX, FLT_MIN };
for (unsigned int n = 0; n < sc->mNumMeshes; ++n)
{
const aiMesh* mesh = sc->mMeshes[n];
for (unsigned int i = 0; i < mesh->mNumVertices; i++)
{
//x = p[0];
//y = p[1];
//z = p[2];
const aiVector3D p = mesh->mVertices[i];
if (p[0] < min_max_pairs_xyz[INDEX_X_MIN])
{
min_max_pairs_xyz[INDEX_X_MIN] = p[0];
}
if (p[0] > min_max_pairs_xyz[INDEX_X_MAX])
{
min_max_pairs_xyz[INDEX_X_MAX] = p[0];
}
if (p[1] < min_max_pairs_xyz[INDEX_Y_MIN])
{
min_max_pairs_xyz[INDEX_Y_MIN] = p[1];
}
if (p[1] > min_max_pairs_xyz[INDEX_Y_MAX])
{
min_max_pairs_xyz[INDEX_Y_MAX] = p[1];
}
if (p[2] < min_max_pairs_xyz[INDEX_Z_MIN])
{
min_max_pairs_xyz[INDEX_Z_MIN] = p[2];
}
if (p[2] > min_max_pairs_xyz[INDEX_Z_MAX])
{
min_max_pairs_xyz[INDEX_Z_MAX] = p[2];
}
}
}
}
void Model::setBoundingSphere()
{
float radius = 0.0f;
glm::vec3 centers;
glm::vec3 offsets;
for (int i = 0; i < 3; i++)
{
centers[i] = (min_max_pairs_xyz[2 * i] + min_max_pairs_xyz[2 * i + 1])/2.0f;
}
std::vector<float> temp;
for (int i = 0; i < 3; i++)
{
temp.push_back(min_max_pairs_xyz[2 * i] - centers[i]);
temp.push_back(min_max_pairs_xyz[2 * i + 1] - centers[i]);
}
float rad = 0.0f;
for (const float p : temp)
{
if (abs(p) > rad)
{
rad = abs(p);
}
}
boundingSphereObjectCoord = std::make_pair(centers, rad);
}
// world coordinates, after scaling
std::vector< glm::vec3 > Model::getOBBBoundingBoxVertices()
{
std::vector< glm::vec3 > points(8);
std::vector< float > min_max_scaled_pairs(6);
// front face
points[BBV_BOTTOM_LEFT_NEAR] = modelMatrix * glm::vec4(min_max_pairs_xyz[INDEX_X_MIN], min_max_pairs_xyz[INDEX_Y_MIN], min_max_pairs_xyz[INDEX_Z_MAX],1.0f );
points[BBV_BOTTOM_RIGHT_NEAR] = modelMatrix * glm::vec4(min_max_pairs_xyz[INDEX_X_MAX], min_max_pairs_xyz[INDEX_Y_MIN], min_max_pairs_xyz[INDEX_Z_MAX],1.0f );
points[BBV_TOP_RIGHT_NEAR] = modelMatrix * glm::vec4(min_max_pairs_xyz[INDEX_X_MAX], min_max_pairs_xyz[INDEX_Y_MAX], min_max_pairs_xyz[INDEX_Z_MAX], 1.0f);
points[BBV_TOP_LEFT_NEAR] = modelMatrix * glm::vec4(min_max_pairs_xyz[INDEX_X_MIN], min_max_pairs_xyz[INDEX_Y_MAX], min_max_pairs_xyz[INDEX_Z_MAX], 1.0f);
// back face
points[BBV_BOTTOM_LEFT_FAR] = modelMatrix * glm::vec4(min_max_pairs_xyz[INDEX_X_MIN], min_max_pairs_xyz[INDEX_Y_MIN], min_max_pairs_xyz[INDEX_Z_MIN], 1.0f);
points[BBV_BOTTOM_RIGHT_FAR] = modelMatrix * glm::vec4(min_max_pairs_xyz[INDEX_X_MAX], min_max_pairs_xyz[INDEX_Y_MIN], min_max_pairs_xyz[INDEX_Z_MIN], 1.0f);
points[BBV_TOP_RIGHT_FAR] = modelMatrix * glm::vec4(min_max_pairs_xyz[INDEX_X_MAX], min_max_pairs_xyz[INDEX_Y_MAX], min_max_pairs_xyz[INDEX_Z_MIN], 1.0f);
points[BBV_TOP_LEFT_FAR] = modelMatrix * glm::vec4(min_max_pairs_xyz[INDEX_X_MIN], min_max_pairs_xyz[INDEX_Y_MAX], min_max_pairs_xyz[INDEX_Z_MIN], 1.0f);
return points;
}