forked from matusnovak/SM2OBJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
windowExport.cpp
975 lines (814 loc) · 34.6 KB
/
windowExport.cpp
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
#include "window.hpp"
#include <algorithm>
//#include "exportBlueprint.h"
#include "blockConfig.h"
#include "loadMeta.h"
#include "chunkTempLoader.h"
#include "chunkLoader.h"
#include "chunkHeader.h"
#include "blockConstructor.h"
#include "polygonFiltering.h"
#include "blockExtractor.h"
#include "blockVerticeData.h"
#include "materialExport.h"
#include "textureExport.h"
struct progressStruct {
ffw::uiLabel* label;
ffw::uiProgressbar* progressbar;
int status;
int total;
void setProgress(const std::string& Str){
std::string text = Str + ": " + ffw::valToString(status) + "/" + ffw::valToString(total);
label->setValue(ffw::ansiToWstr(text));
progressbar->setValue((float(status) / float(total))*100.0f);
}
};
static bool recursiveMetaLoader(sm2obj::entityInfoStruct* Output, const std::string& BlueprintFolder, const std::string& BlueprintName, const std::string& SubFolder);
static void recursiveMetaDump(sm2obj::entityInfoStruct* Output, int Indent);
static bool recursiveChunkLoader(sm2obj::entityInfoStruct* Input, const std::string& BlueprintFolder, const std::string& OutputTemp, int& FileIndex, progressStruct& Progress, volatile bool& Cancel);
static void recursiveChunkExport(sm2obj::entityInfoStruct* Input, const std::string& InputOutputTemp, std::vector<sm2obj::threadInfoStruct*>& Threads, progressStruct& Progress, const std::vector<sm2obj::blockInfoStruct>& BlockInfo, volatile bool& Cancel);
static void* processChunk(void* ThreadInfoPtr);
static void generateEntityQueue(sm2obj::entityInfoStruct* Input, std::vector<sm2obj::entityInfoStruct*>* Output);
static bool mergeVertices(sm2obj::entityInfoStruct* Input, const std::string& InputOutputTemp, ffw::file* Output, bool SplitTextures, bool ExportUvMaps);
static int getTotalEntities(const sm2obj::entityInfoStruct* Input);
///=============================================================================
void* sm2obj::window::exportThreadFunc(void* Data){
// General settings
//bool exportMaterials = (radioUvMapsAtlas->getValue() || radioUvMapsTiles->getValue());
//bool exportAtlas = radioUvMapsAtlas->getValue();
//bool exportObject = true;
//bool exportTextures = true;
std::string mtlOutputPath = outputFolder + "\\" + blueprintName + ".mtl";
std::string objOutputPath = outputFolder + "\\" + blueprintName + ".obj";
std::string blueprintFolder = starMadeFolder + "\\Blueprints\\" + blueprintName;
std::string blockTypesPath = starMadeFolder + "\\data\\config\\BlockTypes.properties";
std::string blockConfigPath = starMadeFolder + "\\data\\config\\BlockConfig.xml";
std::string tempFolder = outputFolder + "\\" + blueprintName + "_TEMP_DELETE_THIS";
std::string textureFolder = starMadeFolder + "\\data\\textures\\block\\Default\\";
int textureSize = 0;
int numOfThreads = comboNumOfThreads->getSelected() +1;
bool exportAttachments = checkboxExportAttachments->getValue();
bool exportDiffuse = checkboxMatDiffuse->getValue();
bool exportAlpha = checkboxMatAlpha->getValue();
bool exportNormals = (radioMatBumps->getValue() || radioMatNormals->getValue());
bool exportEmissive = checkboxMatEmissive->getValue();
warningMessageFlag = false; // extern in config
exportFinished = false;
std::vector<threadInfoStruct*> threads;
std::string textureExtension;
progressStruct guiProgress;
switch(comboTextureFormat->getSelected()){
case 0: textureExtension = "png"; break;
case 1: textureExtension = "tga"; break;
case 2: textureExtension = "bmp"; break;
case 3: textureExtension = "tiff"; break;
}
switch(comboTextureSize->getSelected()){
case 0: textureFolder += "64"; textureSize = 64; break;
case 1: textureFolder += "128"; textureSize = 128; break;
case 2: textureFolder += "256"; textureSize = 256; break;
}
ffw::logDebug() << "Exporting object? -> " << (exportObject ? "true" : "False");
ffw::logDebug() << "Exporting textures? -> " << (exportTextures ? "true" : "False");
ffw::logDebug() << "Exporting materials? -> " << (exportMaterials ? "true" : "False");
// Edit GUI labels & progressbars
if(exportObject){
labelReadingMeta->setColor(ffw::rgb(0x000000));
labelLoadingChunks->setColor(ffw::rgb(0x000000));
labelGeneratingBlocks->setColor(ffw::rgb(0x000000));
labelCreatingPolys->setColor(ffw::rgb(0x000000));
labelMergingVertices->setColor(ffw::rgb(0x000000));
labelReadingMeta->setValue(L"Reading meta: ...");
labelLoadingChunks->setValue(L"Loading chunks: ...");
labelGeneratingBlocks->setValue(L"Generating blocks: ...");
labelCreatingPolys->setValue(L"Creating polygons: ...");
labelMergingVertices->setValue(L"Merging vertices: ...");
} else {
labelReadingMeta->setColor(ffw::rgb(0x777777));
labelLoadingChunks->setColor(ffw::rgb(0x777777));
labelGeneratingBlocks->setColor(ffw::rgb(0x777777));
labelCreatingPolys->setColor(ffw::rgb(0x777777));
labelMergingVertices->setColor(ffw::rgb(0x777777));
labelReadingMeta->setValue(L"Reading meta: X");
labelLoadingChunks->setValue(L"Loading chunks: X");
labelGeneratingBlocks->setValue(L"Generating blocks: X");
labelCreatingPolys->setValue(L"Creating polygons: X");
labelMergingVertices->setValue(L"Merging vertices: X");
}
if(exportTextures){
labelExportingTextures->setColor(ffw::rgb(0x000000));
labelExportingTextures->setValue(L"Exporting textures: ...");
} else {
labelExportingTextures->setColor(ffw::rgb(0x777777));
labelExportingTextures->setValue(L"Exporting textures: X");
}
labelBlockConfig->setColor(ffw::rgb(0x000000));
labelBlockConfig->setValue(L"Block config: ...");
// Output files
ffw::file objOutput;
ffw::file mtlOutput;
// Block config data
std::vector<blockTypeStruct> blockTypes;
std::vector<blockInfoStruct> blockInfo;
// Blueprint output data
entityInfoStruct entityRoot;
if(exportObject){
// Open MTL file
if(exportObject && exportMaterials){
if(!mtlOutput.open(mtlOutputPath, std::ios::out | std::ios::trunc)){
ffw::showModalError(this, L"Error!", L"Can not open output file for writing: " + ffw::utf8ToWstr(mtlOutputPath));
buttonCancel->setLabel(L"Close");
exportFinished = true;
return NULL;
} else {
ffw::logSuccess() << "MTL destination path: " << objOutputPath;
}
}
// Open OBJ file
if(exportObject){
if(!objOutput.open(objOutputPath, std::ios::out | std::ios::trunc)){
ffw::showModalError(this, L"Error!", L"Can not open output file for writing: " + ffw::utf8ToWstr(objOutputPath));
buttonCancel->setLabel(L"Close");
exportFinished = true;
return NULL;
} else {
ffw::logSuccess() << "OBJ destination path: " << objOutputPath;
}
}
}
labelBlockConfig->setValue(L"Block config: 0/2");
progressBlockConfig->setValue(0);
std::cout << "checking... " << exportThreadStop << std::endl;
// Check for cancelation
if(exportThreadStop){
return NULL;
}
std::cout << "loading..." << std::endl;
// Load block types
if(!loadBlockTypes(blockTypes, blockTypesPath)){
ffw::showModalError(this, L"Error!", L"Failed to open block type properties: " + ffw::utf8ToWstr(blockTypesPath));
buttonCancel->setLabel(L"Close");
exportFinished = true;
return NULL;
}
ffw::logSuccess() << "Loaded: " << blockTypes.size() << " block types";
labelBlockConfig->setValue(L"Block config: 1/2");
progressBlockConfig->setValue(50);
// Check for cancelation
if(exportThreadStop){
return NULL;
}
// Load block config
if(!loadBlockConfig(blockTypes, blockInfo, blockConfigPath)){
ffw::showModalError(this, L"Error!", L"Failed to open block config: " + ffw::utf8ToWstr(blockConfigPath));
buttonCancel->setLabel(L"Close");
exportFinished = true;
return NULL;
}
// Check for cancelation
if(exportThreadStop){
return NULL;
}
ffw::logSuccess() << "Loaded: " << blockInfo.size() << " blocks from config";
labelBlockConfig->setValue(L"Block config: 2/2");
progressBlockConfig->setValue(100);
if(exportObject){
labelReadingMeta->setValue(L"Reading meta: 0/1");
progressReadingMeta->setValue(0);
// Load meta files if exporting attachments
if(exportAttachments){
ffw::logDebug() << "Loading attached ships and turrets...";
if(!recursiveMetaLoader(&entityRoot, blueprintFolder, blueprintName, "")){
ffw::showModalError(this, L"Error!", L"Failed to load meta data! Try to export the ship without attached turrets and ships!");
buttonCancel->setLabel(L"Close");
exportFinished = true;
return NULL;
}
}
labelReadingMeta->setValue(L"Reading meta: 1/1");
progressReadingMeta->setValue(100);
// Check for cancelation
if(exportThreadStop){
return NULL;
}
// Dump information about ship
recursiveMetaDump(&entityRoot, 0);
// Create temp folder
ffw::createDirectory(tempFolder);
// Export chunks to temp folder
int fileIndex = 0;
guiProgress.label = labelLoadingChunks;
guiProgress.progressbar = progressLoadingChunks;
guiProgress.total = getTotalEntities(&entityRoot);
guiProgress.status = 0;
guiProgress.setProgress("Loading chunks");
if(!recursiveChunkLoader(&entityRoot, blueprintFolder, tempFolder, fileIndex, guiProgress, exportThreadStop)){
ffw::showModalError(this, L"Error!", L"Failed to export chunks to temporary folder! The ship might be corrupted or program has no privileges to create temporary files");
buttonCancel->setLabel(L"Close");
exportFinished = true;
return NULL;
}
// Check for cancelation
if(exportThreadStop){
return NULL;
}
// Create thread data
for(int i = 0; i < numOfThreads; i++){
threads.push_back(new threadInfoStruct(blockInfo));
threads[i]->thread.bindFunction(&processChunk);
}
// Export chunks
guiProgress.label = labelGeneratingBlocks;
guiProgress.progressbar = progressGeneratingBlocks;
guiProgress.total = fileIndex;
guiProgress.status = 0;
guiProgress.setProgress("Generating blocks");
recursiveChunkExport(&entityRoot, tempFolder, threads, guiProgress, blockInfo, exportThreadStop);
// Delete threads
for(int i = 0; i < numOfThreads; i++){
delete threads[i];
}
threads.clear();
// Check for cancelation
if(exportThreadStop){
return NULL;
}
// Generate queue
std::vector<entityInfoStruct*> entityQueue;
generateEntityQueue(&entityRoot, &entityQueue);
// Export vertices
//progress = 0;
uint64_t indicesOffset = 0;
uint64_t texPosOffset = 0;
chunkBufferStruct* chunkRawBuffer = new chunkBufferStruct;
guiProgress.label = labelCreatingPolys;
guiProgress.progressbar = progressCreatingPolys;
guiProgress.total = entityQueue.size();
guiProgress.status = 0;
guiProgress.setProgress("Creating polygons");
for(auto& entity : entityQueue){
chunkRawBuffer->extractedMaterialsList.clear();
for(auto& chunk : entity->chunks){
//progress++;
//ffw::logDebug() << "Generating vertices: " << progress << " out of: " << progressTotal;
//Args.callbackProgress(progress, progressTotal);
// Load a temp block to memory
sm2obj::loadRawBlocks(tempFolder, chunkRawBuffer, chunk.fileIndex);
// Export block to temp as indices and vertices
sm2obj::extractBlocks(tempFolder, chunkRawBuffer, chunk.fileIndex, &indicesOffset, &texPosOffset, exportAtlas, exportMaterials, exportUV, blockInfo);
}
if(!exportAtlas)texPosOffset += 4;
else texPosOffset += chunkRawBuffer->extractedMaterialsList.size()*4;
if(exportAtlas){
for(auto i : chunkRawBuffer->extractedMaterialsList)entity->extractedTiles.push_back(i.x);
} else {
for(auto i : chunkRawBuffer->extractedMaterialsList)entity->extractedMaterials.push_back(i);
}
guiProgress.status++;
guiProgress.setProgress("Creating polygons");
}
delete chunkRawBuffer;
// Check for cancelation
if(exportThreadStop){
return NULL;
}
// Merge files
guiProgress.label = labelMergingVertices;
guiProgress.progressbar = progressMergingVertices;
guiProgress.total = entityQueue.size();
guiProgress.status = 0;
guiProgress.setProgress("Merging vertices");
ffw::logSuccess() << "Merging vertices and indices into a single file...";
objOutput.writeLine("# SM2OBJ");
if(exportMaterials)objOutput.writeLine("mtllib " + blueprintName + ".mtl");
//recursiveVerticesMerge(&entityRoot, tempFolder, &objOutput, !Args.useAtlas, Args.exportUV);
for(auto& entity : entityQueue){
mergeVertices(entity, tempFolder, &objOutput, !exportAtlas, exportUV);
guiProgress.status++;
guiProgress.setProgress("Merging vertices");
}
ffw::logSuccess() << "Creating material file...";
// Check for cancelation
if(exportThreadStop){
return NULL;
}
// Export materials
if(exportMaterials && exportAtlas){
ffw::logDebug() << "Exporting altas materials";
ffw::logDebug() << "Creating 3 materials...";
createMaterialAtlas(&mtlOutput, true, exportDiffuse, exportAlpha, exportNormals, exportEmissive, textureExtension);
} else if(exportMaterials){
ffw::logDebug() << "Exporting block materials";
std::vector<ffw::vec2i> materialList;
for(auto& entity : entityQueue){
for(auto& mat :entity->extractedMaterials){
// Find if material is already added
bool found = false;
for(auto& item : materialList){
if(item == mat){
found = true;
break;
}
}
if(found)continue;
// Add to list
materialList.push_back(mat);
}
}
ffw::logDebug() << "Creating " << materialList.size() << " materials...";
mtlOutput.writeLine("# SM2OBJ\n");
for(auto& mat : materialList){
const blockInfoStruct* block = findBlock(blockInfo, mat.x);
if(block == NULL)continue;
createMaterialTile(&mtlOutput, block, mat.y, true, exportDiffuse, exportAlpha, exportNormals, exportEmissive, textureExtension);
}
}
// Check for cancelation
if(exportThreadStop){
return NULL;
}
}
// Export textures
if(exportTextures){
guiProgress.label = labelExportingTextures;
guiProgress.progressbar = progressExportingTextures;
guiProgress.total = 0;
if(exportDiffuse)guiProgress.total++;
if(exportAlpha)guiProgress.total++;
if(exportEmissive && exportAtlas)guiProgress.total++;
if(exportNormals)guiProgress.total++;
guiProgress.status = 0;
guiProgress.setProgress("Exporting textures");
std::string textureOutputFolder = outputFolder + "\\textures";
ffw::createDirectory(textureOutputFolder);
if(exportAtlas){
if(exportDiffuse){
exportDiffuseAtlas(textureFolder + "\\t000.png", textureSize, textureOutputFolder + "\\Atlas_0_diff." + textureExtension);
exportDiffuseAtlas(textureFolder + "\\t001.png", textureSize, textureOutputFolder + "\\Atlas_1_diff." + textureExtension);
exportDiffuseAtlas(textureFolder + "\\t002.png", textureSize, textureOutputFolder + "\\Atlas_2_diff." + textureExtension);
guiProgress.status++;
guiProgress.setProgress("Exporting textures");
}
// Check for cancelation
if(exportThreadStop){
return NULL;
}
if(exportAlpha){
exportAlphaAtlas(textureFolder + "\\t000.png", textureSize, textureOutputFolder + "\\Atlas_0_alpha." + textureExtension);
exportAlphaAtlas(textureFolder + "\\t001.png", textureSize, textureOutputFolder + "\\Atlas_1_alpha." + textureExtension);
exportAlphaAtlas(textureFolder + "\\t002.png", textureSize, textureOutputFolder + "\\Atlas_2_alpha." + textureExtension);
guiProgress.status++;
guiProgress.setProgress("Exporting textures");
}
// Check for cancelation
if(exportThreadStop){
return NULL;
}
if(exportEmissive){
exportEmissiveAtlas(blockInfo, textureSize, 0, textureOutputFolder + "\\Atlas_0_emissive." + textureExtension);
exportEmissiveAtlas(blockInfo, textureSize, 1, textureOutputFolder + "\\Atlas_1_emissive." + textureExtension);
exportEmissiveAtlas(blockInfo, textureSize, 2, textureOutputFolder + "\\Atlas_2_emissive." + textureExtension);
guiProgress.status++;
guiProgress.setProgress("Exporting textures");
}
// Check for cancelation
if(exportThreadStop){
return NULL;
}
if(exportNormals && useBumps){
exportBumpAtlas(textureFolder + "\\t000_NRM.png", textureSize, textureOutputFolder + "\\Atlas_0_bump." + textureExtension);
exportBumpAtlas(textureFolder + "\\t001_NRM.png", textureSize, textureOutputFolder + "\\Atlas_1_bump." + textureExtension);
exportBumpAtlas(textureFolder + "\\t002_NRM.png", textureSize, textureOutputFolder + "\\Atlas_2_bump." + textureExtension);
guiProgress.status++;
guiProgress.setProgress("Exporting textures");
} else if(exportNormals){
exportNormalAtlas(textureFolder + "\\t000_NRM.png", textureSize, textureOutputFolder + "\\Atlas_0_bump." + textureExtension);
exportNormalAtlas(textureFolder + "\\t001_NRM.png", textureSize, textureOutputFolder + "\\Atlas_1_bump." + textureExtension);
exportNormalAtlas(textureFolder + "\\t002_NRM.png", textureSize, textureOutputFolder + "\\Atlas_2_bump." + textureExtension);
guiProgress.status++;
guiProgress.setProgress("Exporting textures");
}
} else {
if(exportDiffuse){
exportTileDiffuse(blockInfo, textureFolder, textureSize, textureOutputFolder, textureExtension);
guiProgress.status++;
guiProgress.setProgress("Exporting textures");
}
// Check for cancelation
if(exportThreadStop){
return NULL;
}
if(exportAlpha){
exportTileAlpha(blockInfo, textureFolder, textureSize, textureOutputFolder, textureExtension);
guiProgress.status++;
guiProgress.setProgress("Exporting textures");
}
// Check for cancelation
if(exportThreadStop){
return NULL;
}
if(exportNormals && useBumps){
exportTileBump(blockInfo, textureFolder, textureSize, textureOutputFolder, textureExtension);
guiProgress.status++;
guiProgress.setProgress("Exporting textures");
} else if(exportNormals){
exportTileNormals(blockInfo, textureFolder, textureSize, textureOutputFolder, textureExtension);
guiProgress.status++;
guiProgress.setProgress("Exporting textures");
}
}
}
std::fstream logIn(ffw::getExecutablePath() + "\\log.txt", std::ios::in);
std::fstream logOut(outputFolder + "\\" + blueprintName + "_log.txt", std::ios::out | std::ios::trunc);
if(logIn && logOut){
logOut << logIn.rdbuf();
logIn.close();
logOut.close();
}
if(warningMessageFlag) ffw::showModalWarning(this, L"Warning!", L"Blueprint might not be correctly exported! There were some minor errors! Check log file located in exported folder for warning and errors messages!");
else ffw::showModalInfo(this, L"Success!", L"Blueprint exported without any errors nor warnings! Destination folder: " + ffw::utf8ToWstr(outputFolder));
buttonCancel->setLabel(L"Close");
exportFinished = true;
return NULL;
}
///=============================================================================
bool recursiveMetaLoader(sm2obj::entityInfoStruct* Output, const std::string& BlueprintFolder, const std::string& BlueprintName, const std::string& SubFolder){
std::string path = BlueprintFolder + SubFolder + "\\meta.smbpm";
if(!loadMeta(Output, path, BlueprintName)){
ffw::logError() << "Failed to open meta file from: " << path;
return false;
} else {
ffw::logSuccess() << "Meta file loaded from: " << path;
}
for(auto& item : Output->attachments){
if(!recursiveMetaLoader(&item, BlueprintFolder, BlueprintName, item.path)){
return false;
}
}
return true;
}
///=============================================================================
void recursiveMetaDump(sm2obj::entityInfoStruct* Output, int Indent){
std::string ind;
ind.resize(Indent, ' ');
ffw::logNotice() << ind + "> Name: " << Output->name;
ffw::logNotice() <<ind + "> Path: " << Output->path;
//Args.callbackLogDebug(ind + "> Position: " + ffw::valToString(Output->pos.x) + ", " + ffw::valToString(Output->pos.y) + ", " + ffw::valToString(Output->pos.z));
//Args.callbackLogDebug(ind + "> Orientation: " + ffw::valToString(Output->orientation.x) + ", " + ffw::valToString(Output->orientation.y) + ", " + ffw::valToString(Output->orientation.z));
ffw::logNotice() << ind + "> Dock module position: " << Output->dockModule.x << ", " << Output->dockModule.y << ", " << Output->dockModule.z;
for(auto& item : Output->attachments){
recursiveMetaDump(&item, Indent+2);
}
}
///=============================================================================
int getTotalEntities(const sm2obj::entityInfoStruct* Input){
int num = 1;
for(auto& item : Input->attachments){
num += getTotalEntities(&item);
}
return num;
}
///=============================================================================
bool recursiveChunkLoader(sm2obj::entityInfoStruct* Input, const std::string& BlueprintFolder, const std::string& OutputTemp, int& FileIndex, progressStruct& Progress, volatile bool& Cancel){
std::vector<std::string> allFiles;
// Open target directory
ffw::directory dir;
std::string path = BlueprintFolder + Input->path + "\\DATA";
if(!dir.open(path)){
ffw::logError() << "Failed to open folder: " << path << " The program might not have permission to do it!";
return false;
}
// Include all files that match
for(const auto& file : dir.getFiles()){
std::string extension;
ffw::getFilePathProperties(file, NULL, NULL, &extension);
if(extension != "smd2")continue;
allFiles.push_back(file);
}
// Load all files
for(const auto& file : allFiles){
ffw::file fileInput;
ffw::logDebug() << "Reading file: " << file;
// Get file position from file name
size_t pos = file.find('.');
if(pos == std::string::npos){
ffw::logError() << "Error while retrieving file position from file name! Expected dot with coordinates!";
return false;
}
// Get position as tokens
std::vector<std::string> tokens = ffw::getTokens(file.substr(pos+1, file.size()-pos-6), '.');
if(tokens.size() != 3){
ffw::logError() << "Error while retrieving file position from file name! Wrong coordinates!";
return false;
}
// Convert position to integers
ffw::vec3i filePos;
try {
filePos.x = ffw::stringToVal<int>(tokens[0]);
filePos.y = ffw::stringToVal<int>(tokens[1]);
filePos.z = ffw::stringToVal<int>(tokens[2]);
filePos *= 256;
} catch (std::exception e){
ffw::logError() << "Error while retrieving file position from file name! Wrong coordinates!";
return false;
}
// Print file position
ffw::logDebug() << "File position: " << tokens[0] << ", " << tokens[1] + ", " << tokens[2];
// Open a file
if(!fileInput.open(path + "\\" + file, std::ios::in | std::ios::binary)){
ffw::logError() << "Failed to open file: " << path << "\\" << file;
return false;
}
// Check if file is big enough to contain header
size_t fileSize = fileInput.getSize();
if(fileSize < size_t(4+32768+16384)){
ffw::logError() << "File is invalid! File is too small. Expected at least 49156 bytes.";
return false;
}
// Load header
int chunkIndex[SM2OBJ_CHUNK_SIZE];
int totalChunks = 0;
if(!sm2obj::loadChunkHeader(&fileInput, &chunkIndex[0], &totalChunks)){
//ffw::logError() << "Failed to load chunk header!";
return false;
}
// Remember the file offset
size_t fileOffset = fileInput.getPos();
ffw::logDebug() << "Total chunks: " << totalChunks;
//fileChunksTotal += totalChunks;
// Sort indexes
// We need to load chunks ordered
std::sort(std::begin(chunkIndex), std::end(chunkIndex));
// Load all chunks
uint32_t chunkData[16][16][16];
ffw::vec3i chunkPos;
for(int i = 0; i < SM2OBJ_CHUNK_SIZE; i++){
if(chunkIndex[i] < 0)continue;
ffw::logDebug() << "Loading chunk index: " << chunkIndex[i];
// Load chunk
fileInput.gotoPos(fileOffset + chunkIndex[i]*5120);
if(!sm2obj::loadChunk(&fileInput, fileOffset, chunkIndex[i], chunkData, &chunkPos)){
continue;
}
// Save decompressed raw chunk data to temp folder
if(!sm2obj::saveTempChunk(OutputTemp, FileIndex, &chunkData[0][0][0])){
ffw::logError() << "Error while saving chunk to temp! Make sure the program has permissions to create files!";
continue;
}
// Add chunk to list
Input->chunks.push_back({chunkPos, filePos, FileIndex});
FileIndex++;
}
}
Progress.status++;
Progress.setProgress("Loading chunks");
for(auto& item : Input->attachments){
if(Cancel)return true;
if(!recursiveChunkLoader(&item, BlueprintFolder, OutputTemp, FileIndex, Progress, Cancel))return false;
}
return true;
}
///=============================================================================
void recursiveChunkExport(sm2obj::entityInfoStruct* Input, const std::string& InputOutputTemp, std::vector<sm2obj::threadInfoStruct*>& Threads, progressStruct& Progress, const std::vector<sm2obj::blockInfoStruct>& BlockInfo, volatile bool& Cancel){
ffw::logDebug() << "Exporting: " << Input->name << " from: " << Input->path;
// export chunks one by one
for(auto& chunk : Input->chunks){
// Find an empty thread
while(true){
bool found = false;
// Loop through all threads
for(int i = 0; i < Threads.size(); i++){
// We can lock a thread only of it has finished
if(Threads[i]->mut.tryLock()){
// We found an empty thread, bind information and run it
Progress.status++;
Progress.setProgress("Generating blocks");
//progress(processedChunks, fileChunksTotal);
//Args.callbackLogInfo("Processing chunk: " + ffw::valToString(Progress) + " out of: " + ffw::valToString(ProgressTotal));
//Args.callbackProgress(Progress, ProgressTotal);
// Where are raw chunks stored
Threads[i]->tempFolder = InputOutputTemp;
// Unlock mutex
Threads[i]->mut.unlock();
// Always join even if thread has stopped
Threads[i]->thread.join();
// Bind chunk
Threads[i]->chunkPtr = &chunk;
// Set matrix
Threads[i]->entityPtr = Input;
// Start
Threads[i]->thread.start(Threads[i]);
// Wait 100ms and go to next chunk
ffw::usleep(100000);
found = true;
break;
}
if(Cancel)break;
}
if(Cancel)break;
// An empty thread was not found, wait and then try again
if(found)break;
ffw::usleep(100000);
}
if(Cancel)break;
}
ffw::logDebug() << "Waiting for threads...";
// Join all threads
for(int i = 0; i < Threads.size(); i++){
Threads[i]->thread.join();
}
for(auto& item : Input->attachments){
if(Cancel)break;
recursiveChunkExport(&item, InputOutputTemp, Threads, Progress, BlockInfo, Cancel);
}
}
///=============================================================================
static void* processChunk(void* ThreadInfoPtr){
// Get pointer to the thread info struct
sm2obj::threadInfoStruct* threadInfo = static_cast<sm2obj::threadInfoStruct*>(ThreadInfoPtr);
// Lock this thread
threadInfo->mut.lock();
// Get chunk pointer
sm2obj::chunkInfoStruct* chunk = threadInfo->chunkPtr;
// Load chunk from file
uint32_t chunkData[16][16][16];
if(!sm2obj::loadTempChunk(threadInfo->tempFolder, chunk->fileIndex, &chunkData[0][0][0])){
ffw::logError() << "Error loading chunk file index: " << chunk->fileIndex;
threadInfo->mut.unlock();
return NULL;
}
// Reset buffer A
threadInfo->chunkBufferA.indicesCount = 0;
threadInfo->chunkBufferA.verticesCount = 0;
threadInfo->chunkBufferA.indicesOffset = 0;
// Build blocks by looking through all XYZ positions in chunk
ffw::vec3i posRel;
for(posRel.z = 0; posRel.z < 16; posRel.z++){
for(posRel.y = 0; posRel.y < 16; posRel.y++){
for(posRel.x = 0; posRel.x < 16; posRel.x++){
if(chunkData[posRel.z][posRel.y][posRel.x] == 0)continue;
buildBlock(posRel, chunk->pos, chunk->posFile, chunkData, &threadInfo->chunkBufferA, threadInfo->blockInfo);
}
}
}
// Reset buffer B
threadInfo->chunkBufferB.indicesCount = 0;
threadInfo->chunkBufferB.verticesCount = 0;
threadInfo->chunkBufferB.indicesOffset = 0;
// Filter polygons that occupies same space
removeDuplicatedFaces(&threadInfo->chunkBufferA, &threadInfo->chunkBufferB);
// Now the data is in buffer B
// We will use buffer A as a output
threadInfo->chunkBufferA.indicesCount = 0;
threadInfo->chunkBufferA.verticesCount = 0;
threadInfo->chunkBufferA.indicesOffset = 0;
// Remove duplicated vertices
removeDuplicatedVertices(&threadInfo->chunkBufferB, &threadInfo->chunkBufferA);
// Transform vertices
ffw::vec3f translation(-8.0f, -8.0f, -8.0f);
//ffw::vec3f dockOffset = threadInfo->entityPtr->dockModule - ffw::vec3f(8.0f, 8.0f, 8.0f);
//translation -= dockOffset;
/*ffw::mat4 m;
ffw::vec3f globalPos;
sm2obj::entityInfoStruct* entity = threadInfo->entityPtr;
std::vector<sm2obj::entityInfoStruct*> entityStack;
while(entity != NULL){
entityStack.push_back(entity);
entity = entity->parent;
}
if(entityStack.size() > 0){
for(int i = entityStack.size()-1; i >= 0; i--){
m.rotate(entityStack[i]->orientation);
}
}
ffw::mat4 mr;
entity = threadInfo->entityPtr;
while(entity != NULL){
ffw::vec3f pos = (entity->pos - ffw::vec3f(8.0f, 8.0f, 8.0f));
ffw::vec3f dockModule;
ffw::vec3f offset = entity->dockOffset;
//std::cout << "pos: " << pos << std::endl;
//std::cout << "dockOffset: " << offset << std::endl;
//ffw::vec4f v;
ffw::mat4 rr;
constructRotation(entity, &rr);
entity = entity->parent;
if(entity != NULL){
dockModule = (entity->dockModule - ffw::vec3f(8.0f, 8.0f, 8.0f));
ffw::mat4 mr;
constructRotation(entity, &mr);
ffw::vec4f v;
v.x = pos.x;
v.y = pos.y;
v.z = pos.z;
v = mr * v;
pos.x = v.x;
pos.y = v.y;
pos.z = v.z;
v.x = offset.x;
v.y = offset.y;
v.z = offset.z;
v.w = 1.0f;
v = mr * v;
offset.x = v.x;
offset.y = v.y;
offset.z = v.z;
v.x = dockModule.x;
v.y = dockModule.y;
v.z = dockModule.z;
v.w = 1.0f;
v = mr * v;
dockModule.x = v.x;
dockModule.y = v.y;
dockModule.z = v.z;
}
globalPos -= dockModule;
globalPos += pos + offset;
}*/
for(uint32_t i = 0; i < threadInfo->chunkBufferA.verticesCount; i++){
threadInfo->chunkBufferA.vertices[i] += translation;
/*ffw::vec4f v;
v.x = threadInfo->chunkBufferA.vertices[i].x;
v.y = threadInfo->chunkBufferA.vertices[i].y;
v.z = threadInfo->chunkBufferA.vertices[i].z;
v.w = 1.0f;
v = m * v;
threadInfo->chunkBufferA.vertices[i].x = v.x;
threadInfo->chunkBufferA.vertices[i].y = v.y;
threadInfo->chunkBufferA.vertices[i].z = v.z;
threadInfo->chunkBufferA.vertices[i] += globalPos;*/
}
// Now the data is in buffer A
// Save it to temp file
saveRawBlocks(threadInfo->tempFolder, &threadInfo->chunkBufferA, chunk->fileIndex);
ffw::logSuccess() << "Chunk index: " << chunk->fileIndex << " exported!";
// Unlock this thread
threadInfo->mut.unlock();
return NULL;
}
///=============================================================================
void generateEntityQueue(sm2obj::entityInfoStruct* Input, std::vector<sm2obj::entityInfoStruct*>* Output){
Output->push_back(Input);
for(auto& item : Input->attachments){
generateEntityQueue(&item, Output);
}
}
///=============================================================================
bool mergeVertices(sm2obj::entityInfoStruct* Input, const std::string& InputOutputTemp, ffw::file* Output, bool SplitTextures, bool ExportUvMaps){
Output->writeLine("o " + Input->name);
for(auto& chunk : Input->chunks){
ffw::file vertices;
// Open chunk temp file which contains vertices
if(!vertices.open(InputOutputTemp + "\\chunk-temp-" + ffw::valToString(chunk.fileIndex) + ".vertices", std::ios::in)){
ffw::logError() << "Error failed to open vertex temp data! Index: " << chunk.fileIndex;
return false;
}
// Read whole file and put contents in output OBJ
std::string temp;
while(!vertices.eof()){
vertices.readLine(&temp);
if(temp.size() == 0)continue;
Output->writeLine(temp);
}
}
// Export UVs
if(ExportUvMaps && SplitTextures){
for(int i = 0; i < 4; i++){
Output->writeLine("vt " + ffw::valToString(sm2obj::globalTextureUvs[i].x, 6) + " " + ffw::valToString(sm2obj::globalTextureUvs[i].y, 6));
}
} else if(ExportUvMaps){
for(const auto& tile : Input->extractedTiles){
int atlasId = tile / 256;
int posY = tile / 16;
int posX = tile - posY*16;
posY -= atlasId*16;
ffw::vec2f tilePos(posX / 16.0f, posY / 16.0f);
tilePos.y = 1.0f - tilePos.y - 0.0625f;
for(int i = 0; i < 4; i++){
ffw::vec2f uvs = sm2obj::globalTextureUvs[i];
uvs.y = 1.0f - uvs.y;
ffw::vec2f texPos = uvs * ffw::vec2f(0.052734375f, 0.052734375f) + tilePos + 0.0048828125f;
Output->writeLine("vt " + ffw::valToString(texPos.x, 10) + " " + ffw::valToString(texPos.y, 10));
}
}
}
// Shading off
Output->writeLine("s off");
// Do the same for indices
for(auto& chunk : Input->chunks){
ffw::file indices;
// Open chunk temp file which contains indices
if(!indices.open(InputOutputTemp + "\\chunk-temp-" + ffw::valToString(chunk.fileIndex) + ".indices", std::ios::in)){
ffw::logError() << "Error failed to open indice temp data! Index: " << chunk.fileIndex;
return false;
}
// Read whole file and put contents in output OBJ
std::string temp;
while(!indices.eof()){
indices.readLine(&temp);
if(temp.size() == 0)continue;
Output->writeLine(temp);
}
}
return true;
}