-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathofxAssimpModelLoader.cpp
More file actions
1287 lines (1059 loc) · 37.9 KB
/
ofxAssimpModelLoader.cpp
File metadata and controls
1287 lines (1059 loc) · 37.9 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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ofxAssimpModelLoader.h"
#include "ofxAssimpUtils.h"
#include "ofLight.h"
#include "ofImage.h"
#include "ofPixels.h"
#include "ofGraphics.h"
#include "ofConstants.h"
#include "ofMatrix4x4.h"
#include "ofUtils.h" // ofGetElapsedTimef
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <assimp/config.h>
#include <assimp/DefaultLogger.hpp>
using std::shared_ptr;
using std::vector;
ofxAssimpModelLoader::ofxAssimpModelLoader(){
clear();
}
ofxAssimpModelLoader::~ofxAssimpModelLoader(){
clear();
}
// DEPRECATED
bool ofxAssimpModelLoader::load(const of::filesystem::path & fileName, bool optimize){
int optimizeFlags = OPTIMIZE_DEFAULT;
if( optimize ){
optimizeFlags = OPTIMIZE_HIGH;
}
return load(fileName, optimizeFlags);
}
// DEPRECATED
bool ofxAssimpModelLoader::load(ofBuffer & buffer, bool optimize, const char * extension){
int optimizeFlags = OPTIMIZE_DEFAULT;
if( optimize ){
optimizeFlags = OPTIMIZE_HIGH;
}
return load(buffer, optimizeFlags, extension);
}
// DEPRECATED
bool ofxAssimpModelLoader::loadModel(const of::filesystem::path & fileName, bool optimize){
return load(fileName, optimize);
}
// DEPRECATED
bool ofxAssimpModelLoader::loadModel(ofBuffer & buffer, bool optimize, const char * extension){
return load(buffer, optimize, extension);
}
//------------------------------------------
bool ofxAssimpModelLoader::load(const of::filesystem::path & fileName, int assimpOptimizeFlags){
file = ofToDataPath(fileName);
if (!std::filesystem::exists(file)) {
ofLogVerbose("ofxAssimpModelLoader") << "load(): model does not exist: " << fileName ;
return false;
}
ofLogVerbose("ofxAssimpModelLoader") << "load(): loading " << fileName;
if(scene.get() != nullptr){
clear();
// we reset the shared_ptr explicitly here, to force the old
// aiScene to be deleted **before** a new aiScene is created.
scene.reset();
}
// sets various properties & flags to a default preference
unsigned int flags = initImportProperties(assimpOptimizeFlags);
// //enable assimp logging based on ofGetLogLevel
// if( ofGetLogLevel() < OF_LOG_NOTICE ){
// auto logLevel = Assimp::DefaultLogger::LogSeverity::VERBOSE;
// Assimp::DefaultLogger::create(ASSIMP_DEFAULT_LOG_NAME, logLevel, aiDefaultLogStream_DEBUGGER | aiDefaultLogStream_STDOUT );
// }
// loads scene from file
const aiScene * scenePtr = importer.ReadFile(ofPathToString(file), flags);
//this is funky but the scenePtr is managed by assimp and so we can't put it in our shared_ptr without disabling the deleter with: [](const aiScene*){}
scene = shared_ptr<const aiScene>(scenePtr,[](const aiScene*){});
bool bOk = processScene();
return bOk;
}
bool ofxAssimpModelLoader::load(ofBuffer & buffer, int assimpOptimizeFlags, const char * extension){
ofLogVerbose("ofxAssimpModelLoader") << "load(): loading from memory buffer \"." << extension << "\"";
if(scene.get() != nullptr){
clear();
// we reset the shared_ptr explicitly here, to force the old
// aiScene to be deleted **before** a new aiScene is created.
scene.reset();
}
// sets various properties & flags to a default preference
unsigned int flags = initImportProperties(assimpOptimizeFlags);
// //enable assimp logging based on ofGetLogLevel
// if( ofGetLogLevel() < OF_LOG_NOTICE ){
// auto logLevel = Assimp::DefaultLogger::LogSeverity::VERBOSE;
// Assimp::DefaultLogger::create(ASSIMP_DEFAULT_LOG_NAME, logLevel, aiDefaultLogStream_DEBUGGER | aiDefaultLogStream_STDOUT );
// }
// loads scene from memory buffer - note this will not work for multipart files (obj, md3, etc)
const aiScene * scenePtr = importer.ReadFileFromMemory(buffer.getData(), buffer.size(), flags, extension);
// this is funky but the scenePtr is managed by assimp and so we can't put it in our shared_ptr without disabling the deleter with: [](const aiScene*){}
scene = shared_ptr<const aiScene>(scenePtr,[](const aiScene*){});
bool bOk = processScene();
return bOk;
}
unsigned int ofxAssimpModelLoader::initImportProperties(int assimpOptimizeFlags) {
store.reset(aiCreatePropertyStore(), aiReleasePropertyStore);
// only ever give us triangles.
aiSetImportPropertyInteger(store.get(), AI_CONFIG_PP_SBP_REMOVE, aiPrimitiveType_LINE | aiPrimitiveType_POINT );
aiSetImportPropertyInteger(store.get(), AI_CONFIG_PP_PTV_NORMALIZE, true);
unsigned int flags = assimpOptimizeFlags;
if( flags <= OPTIMIZE_HIGH ){
if( flags == OPTIMIZE_NONE ){
flags = 0;
}else if( flags == OPTIMIZE_DEFAULT || flags == OPTIMIZE_HIGH ){
flags = aiProcess_CalcTangentSpace | \
aiProcess_GenSmoothNormals | \
aiProcess_JoinIdenticalVertices | \
aiProcess_ImproveCacheLocality | \
aiProcess_LimitBoneWeights | \
aiProcess_RemoveRedundantMaterials | \
aiProcess_SplitLargeMeshes | \
aiProcess_Triangulate | \
aiProcess_GenUVCoords | \
aiProcess_SortByPType | \
aiProcess_FindDegenerates | \
aiProcess_FindInstances | \
aiProcess_OptimizeMeshes;
}
if( flags == OPTIMIZE_HIGH ){
flags |= aiProcess_OptimizeGraph | \
aiProcess_FindInstances | \
aiProcess_ValidateDataStructure;
}
// this fixes things for OF both tex uvs and model not flipped in z
flags |= aiProcess_ConvertToLeftHanded;
}
return flags;
}
bool ofxAssimpModelLoader::processScene() {
normalizeFactor = ofGetWidth() / 2.0;
if(scene){
loadGLResources();
update();
calculateDimensions();
if(getAnimationCount())
ofLogVerbose("ofxAssimpModelLoader") << "load(): scene has " << getAnimationCount() << "animations";
else {
ofLogVerbose("ofxAssimpModelLoader") << "load(): no animations";
}
return true;
}else{
ofLogError("ofxAssimpModelLoader") << "load(): " + (string) aiGetErrorString();
clear();
return false;
}
return false;
}
//-------------------------------------------
void ofxAssimpModelLoader::createEmptyModel(){
if(scene){
clear();
scene.reset();
}
}
void ofxAssimpModelLoader::setNormalizationFactor(float factor){
normalizeFactor = factor;
}
//-------------------------------------------
void ofxAssimpModelLoader::calculateDimensions(){
if(!scene) return;
ofLogVerbose("ofxAssimpModelLoader") << "calculateDimensions(): inited scene with "
<< scene->mNumMeshes << " meshes & " << scene->mNumAnimations << " animations";
getBoundingBoxWithMinVector(&scene_min, &scene_max);
scene_center.x = (scene_min.x + scene_max.x) / 2.0f;
scene_center.y = (scene_min.y + scene_max.y) / 2.0f;
scene_center.z = (scene_min.z + scene_max.z) / 2.0f;
// optional normalized scaling
normalizedScale = scene_max.x-scene_min.x;
normalizedScale = std::max(double(scene_max.y - scene_min.y), normalizedScale);
normalizedScale = std::max(double(scene_max.z - scene_min.z), normalizedScale);
if (std::abs(normalizedScale) < std::numeric_limits<float>::epsilon()){
ofLogWarning("ofxAssimpModelLoader") << "Error calculating normalized scale of scene" << std::endl;
normalizedScale = 1.0;
} else {
normalizedScale = 1.f / normalizedScale;
normalizedScale *= normalizeFactor;
}
updateModelMatrix();
}
//-------------------------------------------
static GLint getGLFormatFromAiFormat(const char * aiFormat){
std::string formatStr(aiFormat);
if( formatStr.size() >= 8 ){
if( formatStr.substr(0, 4) == "rgba" ){
if(formatStr.substr(4,4) == "8888"){
return GL_RGBA;
}else if(formatStr.substr(4,4) == "8880"){
return GL_RGB;
}
}else{
ofLogError("getGLFormatFromAiFormat") << " can't parse format " << formatStr;
}
}
ofLogWarning("getGLFormatFromAiFormat") << " can't parse format " << formatStr << " returning GL_RGB";
return GL_RGB;
}
//-------------------------------------------
void ofxAssimpModelLoader::createLightsFromAiModel(){
lights.clear();
lights.resize(scene->mNumLights);
for(unsigned int i = 0; i < scene->mNumLights; i++){
lights[i].enable();
if(scene->mLights[i]->mType==aiLightSource_DIRECTIONAL){
lights[i].setDirectional();
lights[i].setOrientation(aiVecToOfVec(scene->mLights[i]->mDirection));
}
if(scene->mLights[i]->mType!=aiLightSource_POINT){
lights[i].setSpotlight();
lights[i].setPosition(aiVecToOfVec(scene->mLights[i]->mPosition));
}
lights[i].setAmbientColor(aiColorToOfColor(scene->mLights[i]->mColorAmbient));
lights[i].setDiffuseColor(aiColorToOfColor(scene->mLights[i]->mColorDiffuse));
lights[i].setSpecularColor(aiColorToOfColor(scene->mLights[i]->mColorSpecular));
}
}
//-------------------------------------------
void ofxAssimpModelLoader::optimizeScene(){
aiApplyPostProcessing(scene.get(),aiProcess_ImproveCacheLocality | aiProcess_OptimizeGraph |
aiProcess_OptimizeMeshes | aiProcess_JoinIdenticalVertices |
aiProcess_RemoveRedundantMaterials);
}
#ifndef TARGET_WIN32
//this is a hack to allow for weak definations of functions that might not exist in older assimp versions
const char *aiTextureTypeToString(enum aiTextureType in)__attribute__((weak));
#endif
//-------------------------------------------
void ofxAssimpModelLoader::loadGLResources(){
ofLogVerbose("ofxAssimpModelLoader") << "loadGLResources(): starting";
// we do this as we have textures and vbos and in meshHelper and we don't want to copy them
// this should really be a vector of shared_ptr's but keeping it as objects for legacy reasons
modelMeshes.clear();
modelMeshes.reserve(scene->mNumMeshes);
// create OpenGL buffers and populate them based on each meshes pertinant info.
for (unsigned int i = 0; i < scene->mNumMeshes; ++i){
ofLogVerbose("ofxAssimpModelLoader") << "loadGLResources(): loading mesh " << i;
// current mesh we are introspecting
aiMesh* mesh = scene->mMeshes[i];
// the current meshHelper we will be populating data into.
modelMeshes.push_back(ofxAssimpMeshHelper());
ofxAssimpMeshHelper & meshHelper = modelMeshes[i];
//ofxAssimpMeshHelper meshHelper;
//meshHelper.texture = NULL;
// Handle material info
aiMaterial* mtl = scene->mMaterials[mesh->mMaterialIndex];
aiColor4D tcolor;
if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &tcolor)){
auto col = aiColorToOfColor(tcolor);
meshHelper.material.setDiffuseColor(col);
}
if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &tcolor)){
auto col = aiColorToOfColor(tcolor);
meshHelper.material.setSpecularColor(col);
}
if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &tcolor)){
auto col = aiColorToOfColor(tcolor);
meshHelper.material.setAmbientColor(col);
}
if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_EMISSIVE, &tcolor)){
auto col = aiColorToOfColor(tcolor);
meshHelper.material.setEmissiveColor(col);
}
float shininess;
if(AI_SUCCESS == aiGetMaterialFloat(mtl, AI_MATKEY_SHININESS, &shininess)){
meshHelper.material.setShininess(shininess);
}
int blendMode;
if(AI_SUCCESS == aiGetMaterialInteger(mtl, AI_MATKEY_BLEND_FUNC, &blendMode)){
if(blendMode==aiBlendMode_Default){
meshHelper.blendMode=OF_BLENDMODE_ALPHA;
}else{
meshHelper.blendMode=OF_BLENDMODE_ADD;
}
}
// Culling
unsigned int max = 1;
int two_sided=0;
if((AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_TWOSIDED, &two_sided, &max)) && two_sided){
meshHelper.twoSided = true;
ofLogVerbose("ofxAssimpModelLoader::loadGLResources") <<": mesh is two sided";
}else{
meshHelper.twoSided = false;
ofLogVerbose("ofxAssimpModelLoader::loadGLResources") <<": mesh is one sided";
}
// Load Textures
int texIndex = 0;
aiString texPath;
//this gets the wrapping in u and v coodinates
//we don't support different wrapping in OF so just read u
aiTextureMapMode texMapMode[2];
for(int d = 0; d <= AI_TEXTURE_TYPE_MAX; d++){
if(AI_SUCCESS == mtl->GetTexture((aiTextureType)d, texIndex, &texPath, NULL, NULL, NULL, NULL, &texMapMode[0])){
//this is a solution to support older versions of assimp. see the weak defination above
if( aiTextureTypeToString ){
ofLogVerbose("ofxAssimpModelLoader") << "loadGLResource(): loading " << aiTextureTypeToString((aiTextureType)d) << " image from \"" << texPath.data << "\"";
}
bool bWrap = (texMapMode[0]==aiTextureMapMode_Wrap);
std::string texPathStr = texPath.C_Str();
//deal with Blender putting "//" in front of local file paths
if( texPathStr.size() > 2 && texPathStr.substr(0, 2) == "//" ){
texPathStr = texPathStr.substr(2, texPathStr.size()-2);
}
//glb embedded texture file starts with *0
bool bTryEmbed = false;
if( texPathStr.size() >= 2 && texPathStr[0] == '*'){
bTryEmbed = true;
}
//stuff for embedded textures
auto ogPath = texPathStr;
bool bHasEmbeddedTexture = false;
auto modelFolder { file.parent_path() };
// auto modelFolder = ofFilePath::getEnclosingDirectory( file.path() );
auto relTexPath = ofFilePath::getEnclosingDirectory(texPathStr,false);
auto realPath = modelFolder / of::filesystem::path{ texPathStr };
#ifndef TARGET_LINUX_ARM
if(bTryEmbed || ofFile::doesFileExist(realPath) == false) {
auto embeddedTexture = scene->GetEmbeddedTexture(ogPath.c_str());
if( embeddedTexture ){
bHasEmbeddedTexture = true;
ofLogVerbose("ofxAssimpModelLoader") << "loadGLResource() texture " << realPath.filename() << " is embedded ";
}else{
ofLogError("ofxAssimpModelLoader") << "loadGLResource(): texture doesn't exist: "
<< file << " in \"" << realPath.string() << "\"";
}
}
#endif
bool bTextureAlreadyExists = false;
if(textures.count(realPath)){
bTextureAlreadyExists = true;
}
if(bTextureAlreadyExists) {
ofxAssimpTexture assimpTexture;
assimpTexture.setup(*textures[realPath].get(), realPath, bWrap);
assimpTexture.setTextureType((aiTextureType)d);
meshHelper.addTexture(assimpTexture);
ofLogVerbose("ofxAssimpModelLoader") << "loadGLResource(): texture already loaded: "
<< file << " from \"" << realPath.string() << "\"" << " adding texture as " << assimpTexture.getTextureTypeAsString() ;
} else {
shared_ptr<ofTexture> texture = std::make_shared<ofTexture>();
if( bHasEmbeddedTexture ){
#ifndef TARGET_LINUX_ARM
auto embeddedTexture = scene->GetEmbeddedTexture(ogPath.c_str());
//compressed texture
if( embeddedTexture->mHeight == 0 && embeddedTexture->mWidth > 0){
ofImage tmp;
ofBuffer buffer;
buffer.set((char *)embeddedTexture->pcData, embeddedTexture->mWidth);
tmp.setUseTexture(false);
tmp.load(buffer);
ofLogVerbose("ofxAssimpModelLoader") << "loadGLResource() texture size is " << tmp.getWidth() << "x" << tmp.getHeight();
texture->loadData(tmp.getPixels());
}else{
//uncompressed texture - might need swizzling from argb to rgba?
auto glFormat = getGLFormatFromAiFormat(embeddedTexture->achFormatHint);
texture->loadData((const uint8_t *)embeddedTexture->pcData, embeddedTexture->mWidth, embeddedTexture->mHeight, glFormat);
}
#endif
}else{
ofLoadImage(*texture.get(), realPath);
}
if(texture && texture->isAllocated()){
ofxAssimpTexture tmpTex;
tmpTex.setup(*texture.get(), realPath, bWrap);
tmpTex.setTextureType((aiTextureType)d);
textures[realPath] = texture;
tmpTex.setTextureType((aiTextureType)d);
meshHelper.addTexture( tmpTex );
ofLogVerbose("ofxAssimpModelLoader") << "loadGLResource(): texture " << tmpTex.getTextureTypeAsString() << " loaded, dimensions: " << texture->getWidth() << "x" << texture->getHeight();
}else{
ofLogError("ofxAssimpModelLoader") << "loadGLResource(): couldn't load texture: "
<< file << " from \"" << realPath.string() << "\"";
}
}
}
}
meshHelper.mesh = mesh;
aiMeshToOfMesh(mesh, meshHelper.cachedMesh, &meshHelper);
meshHelper.cachedMesh.setMode(OF_PRIMITIVE_TRIANGLES);
meshHelper.validCache = true;
meshHelper.hasChanged = false;
int numOfAnimations = scene->mNumAnimations;
for (int i = 0; i<numOfAnimations; i++) {
aiAnimation * animation = scene->mAnimations[i];
animations.push_back(ofxAssimpAnimation(scene, animation));
}
if(hasAnimations()){
meshHelper.animatedPos.resize(mesh->mNumVertices);
if(mesh->HasNormals()){
meshHelper.animatedNorm.resize(mesh->mNumVertices);
}
}
int usage;
if(getAnimationCount()){
#ifndef TARGET_OPENGLES
if(!ofIsGLProgrammableRenderer()){
usage = GL_STATIC_DRAW;
}else{
usage = GL_STREAM_DRAW;
}
#else
usage = GL_DYNAMIC_DRAW;
#endif
}else{
usage = GL_STATIC_DRAW;
}
meshHelper.vbo.setVertexData(&mesh->mVertices[0].x,3,mesh->mNumVertices,usage,sizeof(aiVector3D));
if(mesh->HasVertexColors(0)){
meshHelper.vbo.setColorData(&mesh->mColors[0][0].r,mesh->mNumVertices,GL_STATIC_DRAW,sizeof(aiColor4D));
}
if(mesh->HasNormals()){
meshHelper.vbo.setNormalData(&mesh->mNormals[0].x,mesh->mNumVertices,usage,sizeof(aiVector3D));
}
if (meshHelper.cachedMesh.hasTexCoords()){
meshHelper.vbo.setTexCoordData(&meshHelper.cachedMesh.getTexCoords()[0].x, mesh->mNumVertices,GL_STATIC_DRAW,sizeof(glm::vec2));
}
meshHelper.indices.resize(mesh->mNumFaces * 3);
int j=0;
for (unsigned int x = 0; x < mesh->mNumFaces; ++x){
for (unsigned int a = 0; a < mesh->mFaces[x].mNumIndices; ++a){
meshHelper.indices[j++]=mesh->mFaces[x].mIndices[a];
}
}
meshHelper.vbo.setIndexData(&meshHelper.indices[0],meshHelper.indices.size(),GL_STATIC_DRAW);
//modelMeshes.push_back(meshHelper);
}
ofLogVerbose("ofxAssimpModelLoader") << "loadGLResource(): finished";
}
//-------------------------------------------
void ofxAssimpModelLoader::clear(){
if( scene ){
scene.reset();
importer.FreeScene();
}
ofLogVerbose("ofxAssimpModelLoader") << "clear(): deleting GL resources";
// clear out everything.
modelMeshes.clear();
animations.clear();
pos = glm::vec3(0,0,0);
scale = glm::vec3(1,1,1);
rotAngle.clear();
rotAxis.clear();
lights.clear();
scale = glm::vec3(1, 1, 1);
normalizeScale = true;
bUsingMaterials = true;
bUsingNormals = true;
bUsingTextures = true;
bUsingColors = true;
currentAnimation = -1;
textures.clear();
updateModelMatrix();
}
//------------------------------------------- update.
void ofxAssimpModelLoader::update() {
if(!scene) return;
updateAnimations();
updateMeshes(scene->mRootNode, glm::mat4());
if(hasAnimations() == false) {
return;
}
updateBones();
updateGLResources();
}
void ofxAssimpModelLoader::updateAnimations() {
for(size_t i = 0; i < animations.size(); i++) {
animations[i].update();
}
}
void ofxAssimpModelLoader::updateMeshes(aiNode * node, glm::mat4 parentMatrix) {
aiMatrix4x4 m = node->mTransformation;
m.Transpose();
ofMatrix4x4 matrix(m.a1, m.a2, m.a3, m.a4,
m.b1, m.b2, m.b3, m.b4,
m.c1, m.c2, m.c3, m.c4,
m.d1, m.d2, m.d3, m.d4);
matrix *= parentMatrix;
for(unsigned int i = 0; i < node->mNumMeshes; i++) {
int meshIndex = node->mMeshes[i];
ofxAssimpMeshHelper & mesh = modelMeshes[meshIndex];
mesh.matrix = matrix;
}
for(unsigned int i = 0; i < node->mNumChildren; i++) {
updateMeshes(node->mChildren[i], matrix);
}
}
void ofxAssimpModelLoader::updateBones() {
if (!hasAnimations()){
return;
}
// update mesh position for the animation
for(size_t i = 0; i < modelMeshes.size(); ++i) {
// current mesh we are introspecting
const aiMesh* mesh = modelMeshes[i].mesh;
// calculate bone matrices
vector<aiMatrix4x4> boneMatrices(mesh->mNumBones);
for(unsigned int a = 0; a < mesh->mNumBones; ++a) {
const aiBone* bone = mesh->mBones[a];
// find the corresponding node by again looking recursively through the node hierarchy for the same name
aiNode* node = scene->mRootNode->FindNode(bone->mName);
// start with the mesh-to-bone matrix
boneMatrices[a] = bone->mOffsetMatrix;
// and now append all node transformations down the parent chain until we're back at mesh coordinates again
const aiNode* tempNode = node;
while(tempNode) {
// check your matrix multiplication order here!!!
boneMatrices[a] = tempNode->mTransformation * boneMatrices[a];
// boneMatrices[a] = boneMatrices[a] * tempNode->mTransformation;
tempNode = tempNode->mParent;
}
modelMeshes[i].hasChanged = true;
modelMeshes[i].validCache = false;
}
modelMeshes[i].animatedPos.assign(modelMeshes[i].animatedPos.size(), aiVector3D(0.0f));
if(mesh->HasNormals()){
modelMeshes[i].animatedNorm.assign(modelMeshes[i].animatedNorm.size(), aiVector3D(0.0f));
}
// loop through all vertex weights of all bones
for(unsigned int a = 0; a < mesh->mNumBones; ++a) {
const aiBone* bone = mesh->mBones[a];
const aiMatrix4x4& posTrafo = boneMatrices[a];
for(unsigned int b = 0; b < bone->mNumWeights; ++b) {
const aiVertexWeight& weight = bone->mWeights[b];
size_t vertexId = weight.mVertexId;
const aiVector3D& srcPos = mesh->mVertices[vertexId];
modelMeshes[i].animatedPos[vertexId] += weight.mWeight * (posTrafo * srcPos);
}
if(mesh->HasNormals()){
// 3x3 matrix, contains the bone matrix without the translation, only with rotation and possibly scaling
aiMatrix3x3 normTrafo = aiMatrix3x3( posTrafo);
for(unsigned int b = 0; b < bone->mNumWeights; ++b) {
const aiVertexWeight& weight = bone->mWeights[b];
size_t vertexId = weight.mVertexId;
const aiVector3D& srcNorm = mesh->mNormals[vertexId];
modelMeshes[i].animatedNorm[vertexId] += weight.mWeight * (normTrafo * srcNorm);
}
}
}
}
}
void ofxAssimpModelLoader::updateGLResources(){
// now upload the result position and normal along with the other vertex attributes into a dynamic vertex buffer, VBO or whatever
for (unsigned int i = 0; i < modelMeshes.size(); ++i){
if(modelMeshes[i].hasChanged){
const aiMesh* mesh = modelMeshes[i].mesh;
if(hasAnimations()){
modelMeshes[i].vbo.updateVertexData(&modelMeshes[i].animatedPos[0].x,mesh->mNumVertices);
if(mesh->HasNormals()){
modelMeshes[i].vbo.updateNormalData(&modelMeshes[i].animatedNorm[0].x,mesh->mNumVertices);
}
}
modelMeshes[i].hasChanged = false;
}
}
}
void ofxAssimpModelLoader::updateModelMatrix() {
modelMatrix = glm::identity<glm::mat4>();
modelMatrix = glm::translate(modelMatrix, toGlm(pos));
modelMatrix = glm::rotate(modelMatrix, ofDegToRad(180), glm::vec3(0,0,1));
if(normalizeScale) {
modelMatrix = glm::scale(modelMatrix, glm::vec3(normalizedScale, normalizedScale, normalizedScale));
}
for(size_t i = 0; i < rotAngle.size(); i++){
modelMatrix = glm::rotate(modelMatrix, ofDegToRad(rotAngle[i]), glm::vec3(rotAxis[i].x, rotAxis[i].y, rotAxis[i].z));
}
modelMatrix = glm::scale(modelMatrix, toGlm(scale));
}
//------------------------------------------- animations.
bool ofxAssimpModelLoader::hasAnimations() {
return animations.size() > 0;
}
unsigned int ofxAssimpModelLoader::getAnimationCount(){
return animations.size();
}
ofxAssimpAnimation & ofxAssimpModelLoader::getAnimation(int animationIndex) {
animationIndex = ofClamp(animationIndex, 0, animations.size()-1);
return animations[animationIndex];
}
void ofxAssimpModelLoader::playAllAnimations() {
for(size_t i = 0; i < animations.size(); i++) {
animations[i].play();
}
}
void ofxAssimpModelLoader::stopAllAnimations() {
for(size_t i = 0; i < animations.size(); i++) {
animations[i].stop();
}
}
void ofxAssimpModelLoader::resetAllAnimations() {
for(size_t i = 0; i < animations.size(); i++) {
animations[i].reset();
}
}
void ofxAssimpModelLoader::setPausedForAllAnimations(bool pause) {
for(size_t i = 0; i < animations.size(); i++) {
animations[i].setPaused(pause);
}
}
void ofxAssimpModelLoader::setLoopStateForAllAnimations(ofLoopType state) {
for(size_t i = 0; i < animations.size(); i++) {
animations[i].setLoopState(state);
}
}
void ofxAssimpModelLoader::setPositionForAllAnimations(float position) {
for(size_t i = 0; i < animations.size(); i++) {
animations[i].setPosition(position);
}
}
void ofxAssimpModelLoader::setSpeedForAllAnimations(float speed) {
for (auto & a : animations) {
a.setSpeed(speed);
}
}
// DEPRECATED.
void ofxAssimpModelLoader::setAnimation(int animationIndex) {
if(!hasAnimations()) {
return;
}
currentAnimation = ofClamp(animationIndex, 0, getAnimationCount() - 1);
}
// DEPRECATED.
void ofxAssimpModelLoader::setNormalizedTime(float time) {
if(!hasAnimations()) {
return;
}
currentAnimation = ofClamp(currentAnimation, 0, getAnimationCount() - 1);
ofxAssimpAnimation & animation = animations[currentAnimation];
float realT = ofMap(time, 0.0, 1.0, 0.0, animation.getDurationInSeconds(), false);
animation.setPosition(realT);
update();
}
// DEPRECATED.
void ofxAssimpModelLoader::setTime(float time) {
if(!hasAnimations()) {
return;
}
currentAnimation = ofClamp(currentAnimation, 0, getAnimationCount() - 1);
ofxAssimpAnimation & animation = animations[currentAnimation];
animation.setPosition(time);
update();
}
// DEPRECATED.
float ofxAssimpModelLoader::getDuration(int animationIndex) {
if(!hasAnimations()) {
return 0;
}
animationIndex = ofClamp(animationIndex, 0, getAnimationCount() - 1);
float duration = animations[animationIndex].getDurationInSeconds();
return duration;
}
//------------------------------------------- meshes.
bool ofxAssimpModelLoader::hasMeshes() {
return modelMeshes.size() > 0;
}
unsigned int ofxAssimpModelLoader::getMeshCount() {
return modelMeshes.size();
}
ofxAssimpMeshHelper & ofxAssimpModelLoader::getMeshHelper(int meshIndex) {
meshIndex = ofClamp(meshIndex, 0, modelMeshes.size()-1);
return modelMeshes[meshIndex];
}
//-------------------------------------------
void ofxAssimpModelLoader::getBoundingBoxWithMinVector( aiVector3D* min, aiVector3D* max )
{
aiMatrix4x4 trafo;
aiIdentityMatrix4(&trafo);
min->x = min->y = min->z = 1e10f;
max->x = max->y = max->z = -1e10f;
for(auto & mesh: modelMeshes){
this->getBoundingBoxForNode(mesh, min, max);
}
}
//-------------------------------------------
void ofxAssimpModelLoader::getBoundingBoxForNode(const ofxAssimpMeshHelper & mesh, aiVector3D* min, aiVector3D* max){
if (!hasAnimations()){
for(unsigned int i = 0; i < mesh.mesh->mNumVertices; i++){
auto vertex = mesh.mesh->mVertices[i];
auto tmp = mesh.matrix * glm::vec4(vertex.x,vertex.y,vertex.z,1.0f);
min->x = std::min(min->x,tmp.x);
min->y = std::min(min->y,tmp.y);
min->z = std::min(min->z,tmp.z);
max->x = std::max(max->x,tmp.x);
max->y = std::max(max->y,tmp.y);
max->z = std::max(max->z,tmp.z);
}
} else {
for (auto & animPos: mesh.animatedPos){
auto tmp = mesh.matrix * glm::vec4(animPos.x,animPos.y,animPos.z,1.0f);
min->x = std::min(min->x,tmp.x);
min->y = std::min(min->y,tmp.y);
min->z = std::min(min->z,tmp.z);
max->x = std::max(max->x,tmp.x);
max->y = std::max(max->y,tmp.y);
max->z = std::max(max->z,tmp.z);
}
}
}
//-------------------------------------------
void ofxAssimpModelLoader::setPosition(float x, float y, float z){
pos.x = x;
pos.y = y;
pos.z = z;
updateModelMatrix();
}
//-------------------------------------------
void ofxAssimpModelLoader::setScale(float x, float y, float z){
scale.x = x;
scale.y = y;
scale.z = z;
updateModelMatrix();
}
//-------------------------------------------
void ofxAssimpModelLoader::setScaleNormalization(bool normalize) {
normalizeScale = normalize;
updateModelMatrix();
}
//-------------------------------------------
void ofxAssimpModelLoader::setRotation(int which, float angle, float rot_x, float rot_y, float rot_z){
if(which + 1 > (int)rotAngle.size()){
int diff = 1 + (which - rotAngle.size());
for(int i = 0; i < diff; i++){
rotAngle.push_back(0);
rotAxis.push_back(glm::vec3(0.0,0.0,0.0));
}
}
rotAngle[which] = angle;
rotAxis[which].x = rot_x;
rotAxis[which].y = rot_y;
rotAxis[which].z = rot_z;
updateModelMatrix();
}
//--------------------------------------------------------------
void ofxAssimpModelLoader::drawWireframe(){
draw(OF_MESH_WIREFRAME);
}
//--------------------------------------------------------------
void ofxAssimpModelLoader::drawFaces(){
draw(OF_MESH_FILL);
}
//--------------------------------------------------------------
void ofxAssimpModelLoader::drawVertices(){
draw(OF_MESH_POINTS);
}
//-------------------------------------------
void ofxAssimpModelLoader::enableCulling(int glCullType){
mCullType = glCullType;
}
//-------------------------------------------
void ofxAssimpModelLoader::disableCulling(){
mCullType = -1;
}
//-------------------------------------------
void ofxAssimpModelLoader::draw(ofPolyRenderMode renderType) {
if(scene == NULL) {
return;
}
ofPushStyle();
ofPushMatrix();
ofMultMatrix(modelMatrix);
#ifndef TARGET_OPENGLES
glPolygonMode(GL_FRONT_AND_BACK, ofGetGLPolyMode(renderType));
#endif
for(size_t i = 0; i < modelMeshes.size(); i++) {
ofxAssimpMeshHelper & mesh = modelMeshes[i];
ofPushMatrix();
ofMultMatrix(mesh.matrix);
if(bUsingTextures){
if(mesh.hasTexture(aiTextureType_DIFFUSE)) {
mesh.getTextureRef(aiTextureType_DIFFUSE).bind();
}
}
if(bUsingMaterials){
mesh.material.begin();
}
// this was broken / backwards
if(!mesh.twoSided && mCullType >= 0) {
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(mCullType);
}
else {
glDisable(GL_CULL_FACE);
}
ofEnableBlendMode(mesh.blendMode);
#ifndef TARGET_OPENGLES
mesh.vbo.drawElements(GL_TRIANGLES,mesh.indices.size());
#else
switch(renderType){
case OF_MESH_FILL:
mesh.vbo.drawElements(GL_TRIANGLES,mesh.indices.size());
break;
case OF_MESH_WIREFRAME:
//note this won't look the same as on non ES renderers.
//there is no easy way to convert GL_TRIANGLES to outlines for each triangle
mesh.vbo.drawElements(GL_LINES,mesh.indices.size());
break;
case OF_MESH_POINTS:
mesh.vbo.drawElements(GL_POINTS,mesh.indices.size());
break;
}
#endif
if(bUsingTextures){
if(mesh.hasTexture(aiTextureType_DIFFUSE)) {
mesh.getTextureRef(aiTextureType_DIFFUSE).unbind();
}
}
if(!mesh.twoSided) {
glDisable(GL_CULL_FACE);
}
if(bUsingMaterials){
mesh.material.end();
}