This repository was archived by the owner on Feb 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathasciipostprocess.cpp
More file actions
1392 lines (1252 loc) · 80.2 KB
/
Copy pathasciipostprocess.cpp
File metadata and controls
1392 lines (1252 loc) · 80.2 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 "MDL.h"
#include <algorithm>
#include "shlwapi.h"
/**
Functions:
GetNormal()
FindThirdIndex()
MDL::GatherChildren()
MDL::GetSupernodes()
MDL::AsciiPostProcess()
/**/
/// Utility function, gets triangular face normalized normal from vert vectors
Vector GetNormal(Vector v1, Vector v2, Vector v3){
Vector vNormal = (v2 - v1) / (v3 - v1);
vNormal.Normalize();
return vNormal;
}
/// Utility function for mesh->saber conversion
/// Loops through the faces, until it finds one where both of the vert indices exits, then it returns the third index (unless it's being ignored), otherwise -1
int FindThirdIndex(const std::vector<Face> & faces, int ind1, int ind2, int ignore = -1){
for(int f = 0; f < faces.size(); f++){
const Face & face = faces.at(f);
int nFound = 0;
for(int i = 0; i < 3; i++){
if(face.nIndexVertex.at(i) == ind1 || face.nIndexVertex.at(i) == ind2) nFound++;
}
if(nFound == 2){
for(int i = 0; i < 3; i++){
if(face.nIndexVertex.at(i) != ind1 && face.nIndexVertex.at(i) != ind2 && face.nIndexVertex.at(i) != ignore) return face.nIndexVertex.at(i);
}
}
}
return -1;
}
void MDL::GatherChildren(Node & node, std::vector<Node> & ArrayOfNodes, Vector vFromRoot, Quaternion qFromRoot, std::vector<MdlInteger<unsigned short>> indices, unsigned long * p_nNumFacesTotal){
node.Head.ChildIndices.resize(0); //Reset child array
node.Head.ChildIndices.reserve(ArrayOfNodes.size());
indices.push_back(node.Head.nNameIndex);
/// Let's do the transformations/translations here. First orientation, then translation.
//Location loc = node.GetLocation();
Vector vAdd = node.Head.vPos;
vAdd.Rotate(qFromRoot);
vFromRoot += vAdd;
qFromRoot *= node.Head.oOrient.GetQuaternion();
node.Head.vFromRoot = vFromRoot;
node.Head.qFromRoot = qFromRoot;
if(p_nNumFacesTotal && node.Head.nType & NODE_MESH && !(node.Head.nType & NODE_SABER)){
*p_nNumFacesTotal += node.Mesh.Faces.size();
}
for(Node & child : ArrayOfNodes){
if(child.Head.nParentIndex == node.Head.nNameIndex){
//The nodes with this index is a child, adopt it
//node.Head.Children.push_back(ArrayOfNodes[n]);
node.Head.ChildIndices.push_back(child.Head.nNameIndex);
if(std::find(indices.begin(), indices.end(), child.Head.nNameIndex) == indices.end())
GatherChildren(child, ArrayOfNodes, vFromRoot, qFromRoot, indices, p_nNumFacesTotal);
}
}
node.Head.ChildIndices.shrink_to_fit();
}
void GetSupernodes(ModelHeader & MH, ModelHeader & superMH, unsigned & nHighest, unsigned & nTotalSupermodelNodes, MdlInteger<unsigned short> nNodeCurrent, MdlInteger<unsigned short> nSupernodeCurrent){
if(!nNodeCurrent.Valid()) return;
Node & node = MH.ArrayOfNodes.at(nNodeCurrent);
if(!nSupernodeCurrent.Valid()){
node.Head.nSupernodeNumber = nHighest;
nHighest++;
for(int n = 0; n < node.Head.ChildIndices.size(); n++){
GetSupernodes(MH, superMH, nHighest, nTotalSupermodelNodes, node.Head.ChildIndices.at(n), -2);
}
}
else if(nSupernodeCurrent == static_cast<unsigned short>(-2)){
/// Question: I am adding this to an already existing value in this variable.
/// It is probably the name index, but is it possible that it actually has to be the node index?
node.Head.nSupernodeNumber = static_cast<unsigned short>(node.Head.nSupernodeNumber) + nTotalSupermodelNodes + 1;
for(int n = 0; n < node.Head.ChildIndices.size(); n++){
GetSupernodes(MH, superMH, nHighest, nTotalSupermodelNodes, node.Head.ChildIndices.at(n), -2);
}
}
else{
Node & supernode = superMH.ArrayOfNodes.at(nSupernodeCurrent);
node.Head.nSupernodeNumber = supernode.Head.nSupernodeNumber;
for(int n = 0; n < node.Head.ChildIndices.size(); n++){
bool bFound = false;
std::string sNodeName = MH.Names.at(node.Head.ChildIndices.at(n)).sName.c_str();
std::transform(sNodeName.begin(), sNodeName.end(), sNodeName.begin(), ::tolower);
for(int n2 = 0; n2 < supernode.Head.ChildIndices.size() && !bFound; n2++){
std::string sSupernodeName = superMH.Names.at(supernode.Head.ChildIndices.at(n2)).sName.c_str();
std::transform(sSupernodeName.begin(), sSupernodeName.end(), sSupernodeName.begin(), ::tolower);
if(sNodeName == sSupernodeName){
bFound = true;
GetSupernodes(MH, superMH, nHighest, nTotalSupermodelNodes, node.Head.ChildIndices.at(n), supernode.Head.ChildIndices.at(n2));
}
}
if(!bFound) GetSupernodes(MH, superMH, nHighest, nTotalSupermodelNodes, node.Head.ChildIndices.at(n), -1);
}
}
}
void MDL::AsciiPostProcess(std::vector<std::string> & sBumpmapped){
ReportObject ReportMdl(*this);
ReportMdl << "Ascii post-processing...\n";
Report("Post-processing imported ASCII...");
FileHeader & Data = *FH;
/// PART 0 ///
/// Get rid of the duplication marks
for(int n = 0; n < Data.MH.Names.size(); n++){
std::string & sNode = Data.MH.Names.at(n).sName;
if(sNode.find("__dpl") != std::string::npos){
sNode.resize(sNode.find("__dpl"));
}
}
/// Implementation of 'bumpmapped_texture': applies bumpmaps
for(int s = 0; s < sBumpmapped.size(); s++){
for(int n = 0; n < Data.MH.ArrayOfNodes.size(); n++){
//ReportMdl << "Checking node\n";
Node & node = Data.MH.ArrayOfNodes.at(n);
if(node.Head.nType & NODE_MESH && !(node.Head.nType & NODE_AABB) && !(node.Head.nType & NODE_SABER)){
if(std::string(node.Mesh.cTexture1.c_str()) != "" && std::string(node.Mesh.cTexture1.c_str()) != "NULL"){
if(StringEqual(sBumpmapped.at(s).c_str(), node.Mesh.cTexture1.c_str())){
node.Mesh.nMdxDataBitmap = node.Mesh.nMdxDataBitmap | MDX_FLAG_TANGENT1;
}
}
if(node.Mesh.cTexture2.c_str() != std::string("") && node.Mesh.cTexture2.c_str() != std::string("NULL")){
if(StringEqual(sBumpmapped.at(s).c_str(), node.Mesh.cTexture2.c_str())){
node.Mesh.nMdxDataBitmap = node.Mesh.nMdxDataBitmap | MDX_FLAG_TANGENT2;
}
}
if(node.Mesh.cTexture3.c_str() != std::string("") && node.Mesh.cTexture3.c_str() != std::string("NULL")){
if(StringEqual(sBumpmapped.at(s).c_str(), node.Mesh.cTexture3.c_str())){
node.Mesh.nMdxDataBitmap = node.Mesh.nMdxDataBitmap | MDX_FLAG_TANGENT3;
}
}
if(node.Mesh.cTexture4.c_str() != std::string("") && node.Mesh.cTexture4.c_str() != std::string("NULL")){
if(StringEqual(sBumpmapped.at(s).c_str(), node.Mesh.cTexture4.c_str())){
node.Mesh.nMdxDataBitmap = node.Mesh.nMdxDataBitmap | MDX_FLAG_TANGENT4;
}
}
}
}
}
/// PART 1 ///
/// Gather all the children (the indices!!!)
/// This part means going from every node only specifying its parent to every node also specifying its children
Report("Building node hierarchies...");
// 1. Gather children for animations
for(Animation & anim : Data.MH.Animations){
for(Node & animnode : anim.ArrayOfNodes){
if(!animnode.Head.nParentIndex.Valid()){
//anim.RootAnimationNode = anim.ArrayOfNodes[n];
std::vector<MdlInteger<unsigned short>> indices; /// Must keep track of all the indices of its parents because we must stop recursion in the case of a loop
indices.reserve(anim.ArrayOfNodes.size());
GatherChildren(animnode, anim.ArrayOfNodes, Vector(), Quaternion(), indices);
break;
}
}
}
// 2. Gather children for geometry
unsigned long nNumFacesTotal = 0;
for(Node & node : Data.MH.ArrayOfNodes){
if(!node.Head.nParentIndex.Valid()){
//Data.MH.RootNode = Data.MH.ArrayOfNodes[n];
std::vector<MdlInteger<unsigned short>> indices; /// Must keep track of all the indices of its parents because we must stop recursion in the case of a loop
indices.reserve(Data.MH.ArrayOfNodes.size());
GatherChildren(node, Data.MH.ArrayOfNodes, Vector(), Quaternion(), indices, &nNumFacesTotal);
break;
}
}
/// PART 2 ///
/// Do supernodes
/// This loads up all the supermodels and calculates the supernode numbers
Report("Processing supermodel...");
Data.MH.GH.nTotalNumberOfNodes = Data.MH.nNodeCount;
nSupermodel = 0; // As far as we're concerned, supermodel not loaded (yet).
if(Data.MH.cSupermodelName != "NULL" && Data.MH.cSupermodelName != ""){
std::unique_ptr<MDL> Supermodel;
LoadSupermodel(*this, Supermodel);
//First, update the TotalNodeCount
if(Supermodel){
/// Supernode loaded, record its status
nSupermodel = bK2 ? 2 : 1;
unsigned nTotalSupermodelNodes = Supermodel->GetFileData()->MH.GH.nTotalNumberOfNodes;
ReportMdl << "Total Supermodel Nodes: " << nTotalSupermodelNodes << "\n";
if(nTotalSupermodelNodes > 0)
Data.MH.GH.nTotalNumberOfNodes += 1 + nTotalSupermodelNodes;
//Next we need the largest supernode number.
int nMaxSupernode = 0;
for(int n = 0; n < Supermodel->GetFileData()->MH.ArrayOfNodes.size(); n++){
nMaxSupernode = std::max(nMaxSupernode, (int) Supermodel->GetFileData()->MH.ArrayOfNodes.at(n).Head.nSupernodeNumber);
}
unsigned nCurrentSupernode = nMaxSupernode + 1;
GetSupernodes(Data.MH, Supermodel->GetFileData()->MH, nCurrentSupernode, nTotalSupermodelNodes, 0, 0);
}
}
/// Apply supernode numbers to anim nodes
for(Animation & anim : Data.MH.Animations){
for(Node & anim_node : anim.ArrayOfNodes){
MdlInteger<unsigned short> nNodeIndex = GetNodeIndexByNameIndex(anim_node.Head.nNameIndex);
if(nNodeIndex.Valid()){
anim_node.Head.nSupernodeNumber = Data.MH.ArrayOfNodes.at(nNodeIndex).Head.nSupernodeNumber;
}
else{
/// I don't know what to do with the name indices that are present as animation nodes but are not found as geometry nodes.
}
}
}
/// Build Array of Indices By Tree Order
Data.MH.NameIndicesInTreeOrder.reserve(Data.MH.ArrayOfNodes.size());
Data.MH.BuildTreeOrderArray(Data.MH.ArrayOfNodes.front());
/// PART 3 ///
/// Interpret ascii data
/// This constructs the Mesh.Vertices, Mesh.VertIndices, Dangly.Data2, Dangly.Constraints and Saber.SaberData structures.
/// And not to forget the weights. Also face normals, average, aabb tree .... everything.
unsigned long nFaceCounter = 0;
Report("Interpreting ascii data...");
ProgressSize(0, 100);
unsigned long nStepper = 0;
unsigned nUnit = std::max((unsigned long) 1, 2 * nNumFacesTotal / 100);
ProgressPos(0);
ProgressSetStep(1);
for(int n = 0; n < Data.MH.ArrayOfNodes.size(); n++){
//ReportMdl << "n = " << n << "\n";
Node & node = Data.MH.ArrayOfNodes.at(n);
Report("Interpreting ascii data... (" + GetNodeName(node) + ")");
//std::cout << "Processing: " << GetNodeName(node) << std::endl;
//ReportMdl << "Analyzing node " << Data.MH.Names.at(node.Head.nNameIndex).sName << " (" << n << "/" << Data.MH.ArrayOfNodes.size() << ")\n";
//ReportMdl << "PART 3 - stage 1" << "\n";
if(node.Head.nType & NODE_SABER){
/// Saber interpretation goes here.
if((node.Mesh.TempVerts.size() == 16 && node.Mesh.TempTverts.size() == 16) ||
(node.Mesh.TempVerts.size() == 176 && node.Mesh.TempTverts.size() == 176)){
int nBase = 8;
if (node.Mesh.TempVerts.size() == 176) nBase = 88;
Vector v0 = node.Mesh.TempVerts.at(0);
Vector v1 = node.Mesh.TempVerts.at(1);
Vector v2 = node.Mesh.TempVerts.at(2);
Vector v3 = node.Mesh.TempVerts.at(3);
Vector v4 = node.Mesh.TempVerts.at(4);
Vector v5 = node.Mesh.TempVerts.at(5);
Vector v6 = node.Mesh.TempVerts.at(6);
Vector v7 = node.Mesh.TempVerts.at(7);
Vector v8 = node.Mesh.TempVerts.at(nBase+0);
Vector v9 = node.Mesh.TempVerts.at(nBase+1);
Vector v10 = node.Mesh.TempVerts.at(nBase+2);
Vector v11 = node.Mesh.TempVerts.at(nBase+3);
Vector v12 = node.Mesh.TempVerts.at(nBase+4);
Vector v13 = node.Mesh.TempVerts.at(nBase+5);
Vector v14 = node.Mesh.TempVerts.at(nBase+6);
Vector v15 = node.Mesh.TempVerts.at(nBase+7);
node.Saber.SaberData.reserve(50);
node.Saber.SaberData.push_back(VertexData(v0, node.Mesh.TempTverts.at(0)));
node.Saber.SaberData.push_back(VertexData(v1, node.Mesh.TempTverts.at(1)));
node.Saber.SaberData.push_back(VertexData(v2, node.Mesh.TempTverts.at(2)));
node.Saber.SaberData.push_back(VertexData(v3, node.Mesh.TempTverts.at(3)));
node.Saber.SaberData.push_back(VertexData(v4, node.Mesh.TempTverts.at(4)));
node.Saber.SaberData.push_back(VertexData(v5, node.Mesh.TempTverts.at(5)));
node.Saber.SaberData.push_back(VertexData(v6, node.Mesh.TempTverts.at(6)));
node.Saber.SaberData.push_back(VertexData(v7, node.Mesh.TempTverts.at(7)));
for(int r = 0; r < 20; r++){
node.Saber.SaberData.push_back(VertexData(v0, node.Mesh.TempTverts.at(0)));
node.Saber.SaberData.push_back(VertexData(v1, node.Mesh.TempTverts.at(1)));
node.Saber.SaberData.push_back(VertexData(v2, node.Mesh.TempTverts.at(2)));
node.Saber.SaberData.push_back(VertexData(v3, node.Mesh.TempTverts.at(3)));
}
node.Saber.SaberData.push_back(VertexData(v8, node.Mesh.TempTverts.at(nBase+0)));
node.Saber.SaberData.push_back(VertexData(v9, node.Mesh.TempTverts.at(nBase+1)));
node.Saber.SaberData.push_back(VertexData(v10, node.Mesh.TempTverts.at(nBase+2)));
node.Saber.SaberData.push_back(VertexData(v11, node.Mesh.TempTverts.at(nBase+3)));
node.Saber.SaberData.push_back(VertexData(v12, node.Mesh.TempTverts.at(nBase+4)));
node.Saber.SaberData.push_back(VertexData(v13, node.Mesh.TempTverts.at(nBase+5)));
node.Saber.SaberData.push_back(VertexData(v14, node.Mesh.TempTverts.at(nBase+6)));
node.Saber.SaberData.push_back(VertexData(v15, node.Mesh.TempTverts.at(nBase+7)));
for(int r = 0; r < 20; r++){
node.Saber.SaberData.push_back(VertexData(v8, node.Mesh.TempTverts.at(nBase+0)));
node.Saber.SaberData.push_back(VertexData(v9, node.Mesh.TempTverts.at(nBase+1)));
node.Saber.SaberData.push_back(VertexData(v10, node.Mesh.TempTverts.at(nBase+2)));
node.Saber.SaberData.push_back(VertexData(v11, node.Mesh.TempTverts.at(nBase+3)));
}
node.Mesh.Faces.resize(0);
node.Mesh.Faces.shrink_to_fit();
}
else{
ReportMdl << "Warning! Requirements for saber mesh not met for '" << Data.MH.Names.at(node.Head.nNameIndex).sName << "'! Converting to trimesh...\n";
node.Head.nType = NODE_HEADER | NODE_MESH;
}
}
//ReportMdl << "PART 3 - stage 2" << "\n";
if(node.Head.nType & NODE_MESH && !(node.Head.nType & NODE_SABER)){
std::vector<Vector> vectorarray;
vectorarray.reserve(node.Mesh.Faces.size()*3);
node.Mesh.fTotalArea = 0.0;
/// Build mdx bitmap
if(node.Mesh.TempVerts.size() > 0) node.Mesh.nMdxDataBitmap |= (MDX_FLAG_VERTEX | MDX_FLAG_NORMAL);
if(node.Mesh.TempColors.size() > 0) node.Mesh.nMdxDataBitmap |= (MDX_FLAG_COLOR);
if(node.Mesh.TempTverts.size() > 0) node.Mesh.nMdxDataBitmap |= (MDX_FLAG_UV1);
if(node.Mesh.TempTverts1.size() > 0) node.Mesh.nMdxDataBitmap |= (MDX_FLAG_UV2);
if(node.Mesh.TempTverts2.size() > 0) node.Mesh.nMdxDataBitmap |= (MDX_FLAG_UV3);
if(node.Mesh.TempTverts3.size() > 0) node.Mesh.nMdxDataBitmap |= (MDX_FLAG_UV4);
if(node.Mesh.TangentSpace.at(0)) node.Mesh.nMdxDataBitmap |= (MDX_FLAG_TANGENT1);
if(node.Mesh.TangentSpace.at(1)) node.Mesh.nMdxDataBitmap |= (MDX_FLAG_TANGENT2);
if(node.Mesh.TangentSpace.at(2)) node.Mesh.nMdxDataBitmap |= (MDX_FLAG_TANGENT3);
if(node.Mesh.TangentSpace.at(3)) node.Mesh.nMdxDataBitmap |= (MDX_FLAG_TANGENT4);
/// If this a skin, we need to build the bonemap, the bone indices and convert the name indices in the weights to bone indices
if(node.Head.nType & NODE_SKIN){
/// First, get the correct name index to all the bones
for(int nb = 0; nb < node.Skin.Bones.size(); nb++){
Bone & bone = node.Skin.Bones.at(nb);
bone.nNameIndex = Data.MH.NameIndicesInTreeOrder.at(nb);
}
/// Next, go through the weights and build the actual bones
std::vector<int> nBoneIndices;
nBoneIndices.reserve(16);
for(Weight & w : node.Skin.TempWeights){
for(MdlInteger<unsigned short> & ind : w.nWeightIndex){
// We have found the name index for the current name, now we need to make sure this name has been indexed in the skin
// Check if we already have this name indexed in the skin
/// If the index is -1, we may just skip
if(!ind.Valid()) continue;
/// First, convert the name index into the tree order index
unsigned short nNodeIndex = 0;
for(unsigned short ind2 = 0; ind2 < Data.MH.NameIndicesInTreeOrder.size(); ind2++)
if(ind == Data.MH.NameIndicesInTreeOrder.at(ind2))
nNodeIndex = ind2;
/// nNodeIndex is now the index of the bone node in tree order, now let's see if there is already a bone index for it
bool bPresent = false;
int nBoneIndex = 0;
while(nBoneIndex < nBoneIndices.size() && !bPresent){
if(nBoneIndices.at(nBoneIndex) == nNodeIndex){
bPresent = true;
}
else nBoneIndex++;
}
/// If this is a new bone index:
if(!bPresent){
/// Add it to the list of bone node indices. Here the value is the node index and the order is the bone index.
/// This is used in the skin. It is the same as the one in the skin header, but this one is not limited to 16 bones, in case that happens.
nBoneIndices.push_back(nNodeIndex);
/// Add it to the list of bone name indices. Here the value is the name index and the order is the bone index
/// This is used for getting the name index from the bone index.
node.Skin.BoneNameIndices.push_back(ind);
//std::cout << "Saving node index " << nNodeIndex << " (name index " << ind << " - " << (ind > 0 && ind < Data.MH.Names.size() ? Data.MH.Names.at(ind).sName : std::string("None")) << ") under bone index " << nBoneIndex << std::endl;
//nBoneIndex = nBoneIndices.size() - 1; //Update nBoneIndex so it always points to the correct bone
/// Add it to the bonemap. The value is the bone index and the order is the node index.
node.Skin.Bones.at(nNodeIndex).nBonemap = nBoneIndex;
/// Add it to the skin header's bone list if there are less than 16 bones. Here the value is the node index and the order is the bone index.
if(nBoneIndex < 16){
node.Skin.nBoneIndices.at(nBoneIndex) = nNodeIndex;
}
else Warning(std::string("Warning! The skin node '") + Data.MH.Names.at(node.Head.nNameIndex).sName + "' has more than 16 bones, which is the number of available slots in one of the lists. "
"The extra bones will simply not be included in that list, but I do not know how this will affect the game.");
}
/// Now that we have a bone index, update the one in the weights
ind = nBoneIndex;
}
}
}
//std::cout << "Done with bones" << std::endl;
/// Go through all the faces
for(int f = 0; f < node.Mesh.Faces.size(); f++){
Face & face = node.Mesh.Faces.at(f);
face.nID = f; /// Why is this necessary?
nStepper++;
if(nStepper % nUnit == 0) ProgressStepIt();
/// MDLOps may leave out texindicesX arrays, I need to check for unset indices and make them use the diffuse ones instead.
for(int i = 0; i < 3; i++){
if(node.Mesh.TempTverts1.size() > 0 && !face.nIndexTvert1.at(i).Valid()){
for(int i2 = 0; i2 < 3; i2++) face.nIndexTvert1.at(i2) = face.nIndexTvert.at(i2);
}
if(node.Mesh.TempTverts2.size() > 0 && !face.nIndexTvert2.at(i).Valid()){
for(int i2 = 0; i2 < 3; i2++) face.nIndexTvert2.at(i2) = face.nIndexTvert.at(i2);
}
if(node.Mesh.TempTverts3.size() > 0 && !face.nIndexTvert3.at(i).Valid()){
for(int i2 = 0; i2 < 3; i2++) face.nIndexTvert3.at(i2) = face.nIndexTvert.at(i2);
}
}
/// Go through all the verts in the face
for(int i = 0; i < 3; i++){
if(!face.bProcessed[i]){
bool bIgnoreVert = true, bIgnoreTvert = true, bIgnoreTvert1 = true, bIgnoreTvert2 = true, bIgnoreTvert3 = true, bIgnoreColor = true;
Vertex vert;
vert.MDXData.nNameIndex = node.Head.nNameIndex;
if(node.Mesh.TempVerts.size() > 0){
bIgnoreVert = false;
vert.assign(node.Mesh.TempVerts.at(face.nIndexVertex[i]));
//Add to vectorarray if no identical
bool bAdd = true;
for(int v = 0; v < vectorarray.size() && bAdd; v++){
if(vectorarray.at(v).Compare(node.Mesh.TempVerts.at((unsigned short) face.nIndexVertex[i]))) bAdd = false;
}
if(bAdd){
vectorarray.push_back(node.Mesh.TempVerts.at((unsigned short) face.nIndexVertex[i]));
}
vert.vFromRoot = node.Mesh.TempVerts.at((unsigned short) face.nIndexVertex[i]);
vert.vFromRoot.Rotate(node.Head.qFromRoot);
vert.vFromRoot += node.Head.vFromRoot;
vert.MDXData.vVertex = node.Mesh.TempVerts.at((unsigned short) face.nIndexVertex[i]);
if(node.Head.nType & NODE_DANGLY){
node.Dangly.Data2.push_back(node.Mesh.TempVerts.at((unsigned short) face.nIndexVertex[i]));
node.Dangly.Constraints.push_back(node.Dangly.TempConstraints.at((unsigned short) face.nIndexVertex[i]));
}
if(node.Head.nType & NODE_SKIN){
vert.MDXData.Weights = node.Skin.TempWeights.at((unsigned short) face.nIndexVertex[i]);
double fTotalWeight = 0.0;
fTotalWeight += vert.MDXData.Weights.fWeightValue.at(0);
fTotalWeight += vert.MDXData.Weights.fWeightValue.at(1);
fTotalWeight += vert.MDXData.Weights.fWeightValue.at(2);
fTotalWeight += vert.MDXData.Weights.fWeightValue.at(3);
if(abs(fTotalWeight - 1.0) >= 0.0001) ReportMdl << "Warning! Skin weights for ascii vertex " << (unsigned short) face.nIndexVertex[i] << " on '" << Data.MH.Names.at(node.Head.nNameIndex).sName << "' do not equal 1.0, instead they equal " << fTotalWeight << ". This may cause problems in the game.\n";
}
}
if(node.Mesh.TempTverts.size() > 0){
bIgnoreTvert = false;
vert.MDXData.vUV1 = node.Mesh.TempTverts.at((unsigned short) face.nIndexTvert[i]);
}
if(node.Mesh.TempTverts1.size() > 0){
bIgnoreTvert1 = false;
vert.MDXData.vUV2 = node.Mesh.TempTverts1.at((unsigned short) face.nIndexTvert1[i]);
}
if(node.Mesh.TempTverts2.size() > 0){
bIgnoreTvert2 = false;
vert.MDXData.vUV3 = node.Mesh.TempTverts2.at((unsigned short) face.nIndexTvert2[i]);
}
if(node.Mesh.TempTverts3.size() > 0){
bIgnoreTvert3 = false;
vert.MDXData.vUV4 = node.Mesh.TempTverts3.at((unsigned short) face.nIndexTvert3[i]);
}
if(node.Mesh.TempColors.size() > 0){
bIgnoreColor = false;
vert.MDXData.cColor = node.Mesh.TempColors.at((unsigned short) face.nIndexColor[i]);
}
//Find identical verts
for(int f2 = f; f2 < node.Mesh.Faces.size(); f2++){
Face & face2 = node.Mesh.Faces.at(f2);
for(int i2 = 0; i2 < 3; i2++){
//Make sure that we're only changing what's past our current position if we are in the same face.
if(f2 != f || i2 > i){
if(bMinimizeVerts){
try{
if( !face2.bProcessed[i2] &&
(bIgnoreVert || node.Mesh.TempVerts.at(face2.nIndexVertex[i2]) == node.Mesh.TempVerts.at(face.nIndexVertex[i]) ) &&
(bIgnoreVert || !(node.Head.nType & NODE_DANGLY) || node.Dangly.TempConstraints.at(face2.nIndexVertex[i2]) == node.Dangly.TempConstraints.at(face.nIndexVertex[i]) ) &&
(bIgnoreVert || !(node.Head.nType & NODE_SKIN) || node.Skin.TempWeights.at(face2.nIndexVertex[i2]) == node.Skin.TempWeights.at(face.nIndexVertex[i]) ) &&
(bIgnoreTvert || node.Mesh.TempTverts.at(face2.nIndexTvert[i2]) == node.Mesh.TempTverts.at(face.nIndexTvert[i]) ) &&
(bIgnoreTvert1 || node.Mesh.TempTverts1.at(face2.nIndexTvert1[i2]) == node.Mesh.TempTverts1.at(face.nIndexTvert1[i]) ) &&
(bIgnoreTvert2 || node.Mesh.TempTverts2.at(face2.nIndexTvert2[i2]) == node.Mesh.TempTverts2.at(face.nIndexTvert2[i]) ) &&
(bIgnoreTvert3 || node.Mesh.TempTverts3.at(face2.nIndexTvert3[i2]) == node.Mesh.TempTverts3.at(face.nIndexTvert3[i]) ) &&
(bIgnoreColor || node.Mesh.TempColors.at(face2.nIndexColor[i2]) == node.Mesh.TempColors.at(face.nIndexColor[i]) ) &&
face.nSmoothingGroup & face2.nSmoothingGroup)
{
//If we find a reference to the exact same vert, we have to link to it
//Actually we only need to link vert indices, the correct UV are now already included in the Vertex struct
face2.nIndexVertex[i2] = node.Mesh.Vertices.size();
face2.bProcessed[i2] = true;
}
}
catch(const std::exception & e){
throw mdlexception("Exception while handling temp arrays (face2=" + std::to_string(f2) + ", i2=" + std::to_string(i2) + ") node '" + Data.MH.Names.at(node.Head.nNameIndex).sName + "':\n" + e.what());
}
}
else{
if( (bIgnoreVert || face2.nIndexVertex[i2] == face.nIndexVertex[i] ) &&
(bIgnoreTvert || face2.nIndexTvert[i2] == face.nIndexTvert[i] ) &&
(bIgnoreTvert1 || face2.nIndexTvert1[i2] == face.nIndexTvert1[i] ) &&
(bIgnoreTvert2 || face2.nIndexTvert2[i2] == face.nIndexTvert2[i] ) &&
(bIgnoreTvert3 || face2.nIndexTvert3[i2] == face.nIndexTvert3[i] ) &&
(bIgnoreColor || face2.nIndexColor[i2] == face.nIndexColor[i] ) &&
!face2.bProcessed[i2] &&
face.nSmoothingGroup & face2.nSmoothingGroup)
{
//If we find a reference to the exact same vert, we have to link to it
//Actually we only need to link vert indices, the correct UV are now already included in the Vertex struct
face2.nIndexVertex[i2] = node.Mesh.Vertices.size();
face2.bProcessed[i2] = true;
}
}
}
}
}
//Now we're allowed to link the original vert as well
face.nIndexVertex[i] = node.Mesh.Vertices.size();
face.bProcessed[i] = true;
//Put the new vert into the array
node.Mesh.Vertices.push_back(std::move(vert));
}
}
std::array<unsigned short, 3> vertindicesarray = {face.nIndexVertex[0], face.nIndexVertex[1], face.nIndexVertex[2]};
node.Mesh.VertIndices.push_back(std::move(vertindicesarray));
/// Surprise! Face normal calculation! Moved here so it can be used by BuildAABB
Vertex & v1 = node.Mesh.Vertices.at(face.nIndexVertex[0]);
Vertex & v2 = node.Mesh.Vertices.at(face.nIndexVertex[1]);
Vertex & v3 = node.Mesh.Vertices.at(face.nIndexVertex[2]);
Vector & v1UV = v1.MDXData.vUV1;
Vector & v2UV = v2.MDXData.vUV1;
Vector & v3UV = v3.MDXData.vUV1;
Vector Edge1 = v2 - v1;
Vector Edge2 = v3 - v1;
Vector Edge3 = v3 - v2;
Vector EUV1 = v2UV - v1UV;
Vector EUV2 = v3UV - v1UV;
Vector EUV3 = v3UV - v2UV;
/// This is for the face normal
face.vNormal = cross(Edge1, Edge2); //Cross product, unnormalized
face.vNormal.Normalize();
/// This is for the distance.
face.fDistance = - (face.vNormal.fX * v1.fX +
face.vNormal.fY * v1.fY +
face.vNormal.fZ * v1.fZ);
/// Area calculation
face.fArea = HeronFormulaEdge(Edge1, Edge2, Edge3);
face.fAreaUV = HeronFormulaEdge(EUV1, EUV2, EUV3);
/// TODO: report problematic cases
if(face.fArea != -1.0) node.Mesh.fTotalArea += face.fArea;
/// Tangent space vectors
//Now comes the calculation. Will be using edges 1 and 2
double r = (EUV1.fX * EUV2.fY - EUV1.fY * EUV2.fX);
//This is division, need to check for 0
if(r != 0){
r = 1.0 / r;
}
else{
/**
It can be 0 in several ways.
1. any of the two edges is zero (ie. we're dealing with a line, not a triangle) - this happens
2. both x's or both y's are zero, implying parallel edges, but we cannot have any in a triangle
3. both x's are the same and both y's are the same, therefore they have the same angle and are parallel
4. both edges have the same x and y, they both have a 45° angle and are therefore parallel
/**/
//ndix UR's magic factor
r = 2406.6388;
}
face.vTangent = r * (Edge1 * EUV2.fY - Edge2 * EUV1.fY);
face.vBitangent = r * (Edge2 * EUV1.fX - Edge1 * EUV2.fX);
face.vTangent.Normalize();
face.vBitangent.Normalize();
if(face.vTangent.Null()) face.vTangent = Vector(1.0, 0.0, 0.0);
if(face.vBitangent.Null()) face.vBitangent = Vector(1.0, 0.0, 0.0);
//Handedness
Vector vCross = cross(face.vNormal, face.vTangent);
double fDot = dot(vCross, face.vBitangent);
if(fDot > 0.0000000001) face.vTangent *= -1.0;
//Now check if we need to invert T and B. But first we need a UV normal
Vector vNormalUV = cross(EUV1, EUV2); //cross product
if(vNormalUV.fZ < 0.0){
face.vTangent *= -1.0;
face.vBitangent *= -1.0;
}
/// Face Bounding Box calculation for AABB tree
if(node.Head.nType & NODE_AABB){
face.vBBmax = Vector(-10000.0, -10000.0, -10000.0);
face.vBBmin = Vector( 10000.0, 10000.0, 10000.0);
face.vCentroid = Vector(0.0, 0.0, 0.0);
for(int i = 0; i < 3; i++){
face.vBBmax.fX = std::max(face.vBBmax.fX, node.Mesh.Vertices.at(face.nIndexVertex[i]).fX);
face.vBBmax.fY = std::max(face.vBBmax.fY, node.Mesh.Vertices.at(face.nIndexVertex[i]).fY);
face.vBBmax.fZ = std::max(face.vBBmax.fZ, node.Mesh.Vertices.at(face.nIndexVertex[i]).fZ);
face.vBBmin.fX = std::min(face.vBBmin.fX, node.Mesh.Vertices.at(face.nIndexVertex[i]).fX);
face.vBBmin.fY = std::min(face.vBBmin.fY, node.Mesh.Vertices.at(face.nIndexVertex[i]).fY);
face.vBBmin.fZ = std::min(face.vBBmin.fZ, node.Mesh.Vertices.at(face.nIndexVertex[i]).fZ);
face.vCentroid += node.Mesh.Vertices.at(face.nIndexVertex[i]);
}
face.vCentroid /= 3.0;
}
}
/// Surprise 2!! Average and BB calculation!
node.Mesh.vAverage = Vector(0.0, 0.0, 0.0);
node.Mesh.vBBmin = Vector(0.0, 0.0, 0.0); /// Wrong, but Bioware-correct
node.Mesh.vBBmax = Vector(0.0, 0.0, 0.0); /// Wrong, but Bioware-correct
for(int v = 0; v < vectorarray.size(); v++){
node.Mesh.vBBmin.fX = std::min(node.Mesh.vBBmin.fX, vectorarray.at(v).fX);
node.Mesh.vBBmin.fY = std::min(node.Mesh.vBBmin.fY, vectorarray.at(v).fY);
node.Mesh.vBBmin.fZ = std::min(node.Mesh.vBBmin.fZ, vectorarray.at(v).fZ);
node.Mesh.vBBmax.fX = std::max(node.Mesh.vBBmax.fX, vectorarray.at(v).fX);
node.Mesh.vBBmax.fY = std::max(node.Mesh.vBBmax.fY, vectorarray.at(v).fY);
node.Mesh.vBBmax.fZ = std::max(node.Mesh.vBBmax.fZ, vectorarray.at(v).fZ);
node.Mesh.vAverage += vectorarray.at(v);
//if(v % 1000 == 0) std::cout << "Done with average for vert " << v << std::endl;
}
node.Mesh.vAverage /= (double) vectorarray.size();
//std::cout << "Done with average for verts" << std::endl;
/// Now find the radius as well!
node.Mesh.fRadius = 0.0;
for(int v = 0; v < vectorarray.size(); v++){
node.Mesh.fRadius = std::max(node.Mesh.fRadius, Vector(vectorarray.at(v) - node.Mesh.vAverage).GetLength());
//if(v % 1000 == 0) std::cout << "Done with radius for vert " << v << std::endl;
}
//std::cout << "Done with radius for verts" << std::endl;
//Calculate adjacent faces
for(int f = 0; f < node.Mesh.Faces.size(); f++){
Face & face = node.Mesh.Faces.at(f);
nStepper++;
if(nStepper % nUnit == 0) ProgressStepIt();
// Skip if none is -1
if(face.nAdjacentFaces[0].Valid() &&
face.nAdjacentFaces[1].Valid() &&
face.nAdjacentFaces[2].Valid() ) continue;
//Go through all the faces coming after this one
for(int f2 = f+1; f2 < node.Mesh.Faces.size(); f2++){
Face & compareface = node.Mesh.Faces.at(f2);
std::vector<bool> VertMatches(3, false);
std::vector<bool> VertMatchesCompare(3, false);
for(int i3 = 0; i3 < 3; i3++){
unsigned short nVertIndex = face.nIndexVertex[i3];
Vector & ourvect = node.Mesh.Vertices.at(nVertIndex).vFromRoot;
for(int i4 = 0; i4 < 3; i4++){
Vector & othervect = node.Mesh.Vertices.at(compareface.nIndexVertex[i4]).vFromRoot;
if(ourvect.Compare(othervect)){
VertMatches.at(i3) = true;
VertMatchesCompare.at(i4) = true;
i4 = 3; // we can only have one matching vert in a face per vert. Once we find a match, we're done.
}
}
}
if(VertMatches.at(0) && VertMatches.at(1)){
if(face.nAdjacentFaces[0].Valid()) ReportMdl<<"Found an additional adjacent face on edge 0 for face " << f << " on '" << Data.MH.Names.at(node.Head.nNameIndex).sName << "'...\n";
else face.nAdjacentFaces[0] = f2;
}
else if(VertMatches.at(1) && VertMatches.at(2)){
if(face.nAdjacentFaces[1].Valid()) ReportMdl<<"Found an additional adjacent face on edge 1 for face " << f << " on '" << Data.MH.Names.at(node.Head.nNameIndex).sName << "'...\n";
else face.nAdjacentFaces[1] = f2;
}
else if(VertMatches.at(2) && VertMatches.at(0)){
if(face.nAdjacentFaces[2].Valid()) ReportMdl<<"Found an additional adjacent face on edge 2 for face " << f << " on '" << Data.MH.Names.at(node.Head.nNameIndex).sName << "'...\n";
else face.nAdjacentFaces[2] = f2;
}
if(VertMatchesCompare.at(0) && VertMatchesCompare.at(1)){
if(compareface.nAdjacentFaces[0].Valid()) ReportMdl<<"Found an additional adjacent face on edge 0 for face " << f2 << " on '" << Data.MH.Names.at(node.Head.nNameIndex).sName << "'...\n";
else compareface.nAdjacentFaces[0] = f;
}
else if(VertMatchesCompare.at(1) && VertMatchesCompare.at(2)){
if(compareface.nAdjacentFaces[1].Valid()) ReportMdl<<"Found an additional adjacent face on edge 1 for face " << f2 << " on '" << Data.MH.Names.at(node.Head.nNameIndex).sName << "'...\n";
else compareface.nAdjacentFaces[1] = f;
}
else if(VertMatchesCompare.at(2) && VertMatchesCompare.at(0)){
if(compareface.nAdjacentFaces[2].Valid()) ReportMdl<<"Found an additional adjacent face on edge 2 for face " << f2 << " on '" << Data.MH.Names.at(node.Head.nNameIndex).sName << "'...\n";
else compareface.nAdjacentFaces[2] = f;
}
if(face.nAdjacentFaces[0].Valid() &&
face.nAdjacentFaces[1].Valid() &&
face.nAdjacentFaces[2].Valid() ){
f2 = node.Mesh.Faces.size(); //Found them all, maybe I finish early?
}
}
//if(f % 100 == 0) std::cout << "Done with face " << f << std::endl;
}
//std::cout << "Done with faces" << std::endl;
/// Texture count depends on the UVs.
node.Mesh.nTextureNumber = (node.Mesh.nMdxDataBitmap & MDX_FLAG_UV1 ? 1 : 0) +
(node.Mesh.nMdxDataBitmap & MDX_FLAG_UV2 ? 1 : 0) +
(node.Mesh.nMdxDataBitmap & MDX_FLAG_UV3 ? 1 : 0) +
(node.Mesh.nMdxDataBitmap & MDX_FLAG_UV4 ? 1 : 0);
node.Mesh.TempVerts.resize(0);
node.Mesh.TempTverts.resize(0);
node.Mesh.TempTverts1.resize(0);
node.Mesh.TempTverts2.resize(0);
node.Mesh.TempTverts3.resize(0);
node.Dangly.TempConstraints.resize(0);
node.Skin.TempWeights.resize(0);
node.Mesh.TempVerts.shrink_to_fit();
node.Mesh.TempTverts.shrink_to_fit();
node.Mesh.TempTverts1.shrink_to_fit();
node.Mesh.TempTverts2.shrink_to_fit();
node.Mesh.TempTverts3.shrink_to_fit();
node.Dangly.TempConstraints.shrink_to_fit();
node.Skin.TempWeights.shrink_to_fit();
}
//ReportMdl << "PART 3 - stage 3" << "\n";
if(node.Head.nType & NODE_MESH &&
!(node.Head.nType & NODE_SABER) &&
Data.MH.Names.at(node.Head.nNameIndex).sName.substr(0, 6) == "2081__" &&
(node.Mesh.Faces.size() == 12 /*|| node.Mesh.Faces.size() == 24*/) &&
node.Mesh.Vertices.size() == 16 )
{
bool bAbort = false; /// If this is set to true at any time, we will abort the conversion to saber
std::array<int, 16> VertRefsArray = {0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0};
/// Go through the faces and build the VertRefsArray
/// If a reference to a higher than 16th vertex, abort
/// If the number of references to a vertex is greater than 4, abort
for(int f = 0; f < 12 && !bAbort; f++){
Face & face = node.Mesh.Faces.at(f);
for(int i = 0; i < 3; i++){
if(face.nIndexVertex.at(i) < 16){
VertRefsArray.at(face.nIndexVertex.at(i)) += 1;
if ( VertRefsArray.at(face.nIndexVertex.at(i)) > 4) bAbort = true;
}
else bAbort = true;
}
}
/// Now we need to find these guys
MdlInteger<unsigned int> nOuter1;
MdlInteger<unsigned int> nOuter2;
MdlInteger<unsigned int> nCorner1;
MdlInteger<unsigned int> nCorner2;
/// Go through the verts, find the two that are referenced by 4 faces
/// Put them into outer1 and outer2
/// If there's more than two of those, abort
int nFound = 0;
for(int v = 0; v < 16 && !bAbort; v++){
if(VertRefsArray.at(v) == 4){
if(nFound == 0){
nOuter1 = v;
nFound++;
}
else if(nFound == 1){
nOuter2 = v;
nFound++;
}
else bAbort = true;
}
}
/// Go through faces again and find the corners – the only ones adjacent to outer1 & 2 that have 1 ref
for(int f = 0; f < 12 && !bAbort; f++){
Face & face = node.Mesh.Faces.at(f);
/// Go through the indices
for(int i = 0; i < 3; i++){
if( face.nIndexVertex.at(i) == nOuter1){
/// Go through the same indices again
for(int i2 = 0; i2 < 3; i2++){
if(!nCorner1.Valid() && VertRefsArray.at(face.nIndexVertex.at(i2)) == 1){
nCorner1 = face.nIndexVertex.at(i2);
break;
}
else if(nCorner1.Valid() && VertRefsArray.at(face.nIndexVertex.at(i2)) == 1){
ReportMdl << "Error! nCorner1 found several times, this shouldn't be happening!!\n";
bAbort = true;
break;
}
}
break;
}
else if( face.nIndexVertex.at(i) == nOuter2){
/// Go through the same indices again
for(int i2 = 0; i2 < 3; i2++){
if(!nCorner2.Valid() && VertRefsArray.at(face.nIndexVertex.at(i2)) == 1){
nCorner2 = face.nIndexVertex.at(i2);
break;
}
else if(nCorner2.Valid() && VertRefsArray.at(face.nIndexVertex.at(i2)) == 1){
ReportMdl << "Error! nCorner2 found several times, this shouldn't be happening!!\n";
bAbort = true;
break;
}
}
break;
}
}
}
if(nOuter1.Valid() && nOuter2.Valid() && nCorner1.Valid() && nCorner2.Valid() && !bAbort){
/// Build blade vert arrays
std::array<MdlInteger<unsigned int>, 8> Blade1VertArray;
std::array<MdlInteger<unsigned int>, 8> Blade2VertArray;
Blade1VertArray.at(6) = nOuter1;
Blade1VertArray.at(7) = nCorner1;
Blade1VertArray.at(3) = FindThirdIndex(node.Mesh.Faces, Blade1VertArray.at(6), Blade1VertArray.at(7));
Blade1VertArray.at(2) = FindThirdIndex(node.Mesh.Faces, Blade1VertArray.at(3), Blade1VertArray.at(6), Blade1VertArray.at(7));
Blade1VertArray.at(1) = FindThirdIndex(node.Mesh.Faces, Blade1VertArray.at(2), Blade1VertArray.at(6), Blade1VertArray.at(3));
Blade1VertArray.at(5) = FindThirdIndex(node.Mesh.Faces, Blade1VertArray.at(1), Blade1VertArray.at(6), Blade1VertArray.at(2));
Blade1VertArray.at(0) = FindThirdIndex(node.Mesh.Faces, Blade1VertArray.at(1), Blade1VertArray.at(5), Blade1VertArray.at(6));
Blade1VertArray.at(4) = FindThirdIndex(node.Mesh.Faces, Blade1VertArray.at(0), Blade1VertArray.at(5), Blade1VertArray.at(1));
Blade2VertArray.at(6) = nOuter2;
Blade2VertArray.at(7) = nCorner2;
Blade2VertArray.at(3) = FindThirdIndex(node.Mesh.Faces, Blade2VertArray.at(6), Blade2VertArray.at(7));
Blade2VertArray.at(2) = FindThirdIndex(node.Mesh.Faces, Blade2VertArray.at(3), Blade2VertArray.at(6), Blade2VertArray.at(7));
Blade2VertArray.at(1) = FindThirdIndex(node.Mesh.Faces, Blade2VertArray.at(2), Blade2VertArray.at(6), Blade2VertArray.at(3));
Blade2VertArray.at(5) = FindThirdIndex(node.Mesh.Faces, Blade2VertArray.at(1), Blade2VertArray.at(6), Blade2VertArray.at(2));
Blade2VertArray.at(0) = FindThirdIndex(node.Mesh.Faces, Blade2VertArray.at(1), Blade2VertArray.at(5), Blade2VertArray.at(6));
Blade2VertArray.at(4) = FindThirdIndex(node.Mesh.Faces, Blade2VertArray.at(0), Blade2VertArray.at(5), Blade2VertArray.at(1));
/// if there is a -1 in any of the two arrays, abort
if(std::find(Blade1VertArray.begin(), Blade1VertArray.end(), INVALID_INT) != Blade1VertArray.end() ||
std::find(Blade2VertArray.begin(), Blade2VertArray.end(), INVALID_INT) != Blade2VertArray.end()) bAbort = true;
if(!bAbort){
///Now all we need to do is decide which of the two blades to invert.
std::array<MdlInteger<unsigned int>, 8> Blade1, Blade2;
if(node.Mesh.Vertices.at(Blade1VertArray.at(6)).fZ - node.Mesh.Vertices.at(Blade1VertArray.at(5)).fZ >
node.Mesh.Vertices.at(Blade2VertArray.at(6)).fZ - node.Mesh.Vertices.at(Blade2VertArray.at(5)).fZ )
{
Blade1 = Blade1VertArray;
Blade2 = {Blade2VertArray[3], Blade2VertArray[2], Blade2VertArray[1], Blade2VertArray[0],
Blade2VertArray[7], Blade2VertArray[6], Blade2VertArray[5], Blade2VertArray[4]};
}
else{
Blade1 = Blade2VertArray;
Blade2 = {Blade1VertArray[3], Blade1VertArray[2], Blade1VertArray[1], Blade1VertArray[0],
Blade1VertArray[7], Blade1VertArray[6], Blade1VertArray[5], Blade1VertArray[4]};
}
node.Saber.SaberData.reserve(50);
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade1.at(0)), node.Mesh.Vertices.at(Blade1.at(0)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade1.at(1)), node.Mesh.Vertices.at(Blade1.at(1)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade1.at(2)), node.Mesh.Vertices.at(Blade1.at(2)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade1.at(3)), node.Mesh.Vertices.at(Blade1.at(3)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade1.at(4)), node.Mesh.Vertices.at(Blade1.at(4)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade1.at(5)), node.Mesh.Vertices.at(Blade1.at(5)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade1.at(6)), node.Mesh.Vertices.at(Blade1.at(6)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade1.at(7)), node.Mesh.Vertices.at(Blade1.at(7)).MDXData.vUV1));
for(int r = 0; r < 20; r++){
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade1.at(0)), node.Mesh.Vertices.at(Blade1.at(0)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade1.at(1)), node.Mesh.Vertices.at(Blade1.at(1)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade1.at(2)), node.Mesh.Vertices.at(Blade1.at(2)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade1.at(3)), node.Mesh.Vertices.at(Blade1.at(3)).MDXData.vUV1));
}
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade2.at(0)), node.Mesh.Vertices.at(Blade2.at(0)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade2.at(1)), node.Mesh.Vertices.at(Blade2.at(1)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade2.at(2)), node.Mesh.Vertices.at(Blade2.at(2)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade2.at(3)), node.Mesh.Vertices.at(Blade2.at(3)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade2.at(4)), node.Mesh.Vertices.at(Blade2.at(4)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade2.at(5)), node.Mesh.Vertices.at(Blade2.at(5)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade2.at(6)), node.Mesh.Vertices.at(Blade2.at(6)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade2.at(7)), node.Mesh.Vertices.at(Blade2.at(7)).MDXData.vUV1));
for(int r = 0; r < 20; r++){
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade2.at(0)), node.Mesh.Vertices.at(Blade2.at(0)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade2.at(1)), node.Mesh.Vertices.at(Blade2.at(1)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade2.at(2)), node.Mesh.Vertices.at(Blade2.at(2)).MDXData.vUV1));
node.Saber.SaberData.push_back(VertexData(node.Mesh.Vertices.at(Blade2.at(3)), node.Mesh.Vertices.at(Blade2.at(3)).MDXData.vUV1));
}
/// Convert trimesh to lightsaber here
node.Head.nType = node.Head.nType | NODE_SABER;
Data.MH.Names.at(node.Head.nNameIndex).sName = Data.MH.Names.at(node.Head.nNameIndex).sName.substr(6);
}
}
}
//ReportMdl << "PART 3 - stage 4" << "\n";
if(node.Head.nType & NODE_SABER){
std::array<std::array<int, 3>, 12> FaceIndices = {{{0,4,5},{1,0,5},{1,5,2},
{2,5,6},{3,2,6},{3,6,7},
{88+4,88+0,88+5},{88+0,88+1,88+5},{88+5,88+1,88+2},
{88+5,88+2,88+6},{88+2,88+3,88+6},{88+6,88+3,88+7}}};
std::array<Vector, 12> vFaceNormals;
for(int v = 0; v < 12; v++){
vFaceNormals[v] = GetNormal(node.Saber.SaberData.at(FaceIndices[v][0]).vVertex,
node.Saber.SaberData.at(FaceIndices[v][1]).vVertex,
node.Saber.SaberData.at(FaceIndices[v][2]).vVertex);
}
std::array<double, 12> fFaceAreas;
for(int v = 0; v < 12; v++){
fFaceAreas[v] = HeronFormulaVert(node.Saber.SaberData.at(FaceIndices[v][0]).vVertex,
node.Saber.SaberData.at(FaceIndices[v][1]).vVertex,
node.Saber.SaberData.at(FaceIndices[v][2]).vVertex);
}
std::array<Vector, 8> vVertNormals;
for(int v = 0; v < 8; v++){
Vector & vCurrent = vVertNormals.at(v);
int nCurrent = v;
if(nCurrent > 3) nCurrent += 84;
for(int f = 0; f < 12; f++){
for(int i = 0; i < 3; i++){
if(FaceIndices[f][i] == nCurrent){
Vector vAdd = vFaceNormals[f];
if(bSmoothAreaWeighting) vAdd *= fFaceAreas[f];
if(bSmoothAngleWeighting){
if(i == 0){
vAdd *= Angle(node.Saber.SaberData.at(FaceIndices[f][2]).vVertex - node.Saber.SaberData.at(FaceIndices[f][0]).vVertex,
node.Saber.SaberData.at(FaceIndices[f][1]).vVertex - node.Saber.SaberData.at(FaceIndices[f][0]).vVertex);
}
else if(i == 1){
vAdd *= Angle(node.Saber.SaberData.at(FaceIndices[f][2]).vVertex - node.Saber.SaberData.at(FaceIndices[f][1]).vVertex,
node.Saber.SaberData.at(FaceIndices[f][0]).vVertex - node.Saber.SaberData.at(FaceIndices[f][1]).vVertex);
}
else if(i == 2){
vAdd *= Angle(node.Saber.SaberData.at(FaceIndices[f][1]).vVertex - node.Saber.SaberData.at(FaceIndices[f][2]).vVertex,
node.Saber.SaberData.at(FaceIndices[f][0]).vVertex - node.Saber.SaberData.at(FaceIndices[f][2]).vVertex);
}
}
vCurrent += vAdd;
}
}
}
vCurrent.Normalize();
}
for(int v = 0; v < node.Saber.SaberData.size(); v++){
if(v < node.Saber.SaberData.size()/2) node.Saber.SaberData.at(v).vNormal = vVertNormals.at(v%4);
else node.Saber.SaberData.at(v).vNormal = vVertNormals.at(4 + v%4);
}
node.Mesh.Vertices.reserve(node.Saber.SaberData.size());
for(VertexData & sd : node.Saber.SaberData) node.Mesh.Vertices.push_back(Vertex().assign(sd.vVertex));
node.Mesh.Faces.resize(12);
node.Mesh.Faces.at(0).nIndexVertex = {0, 1, 2};
node.Mesh.Faces.at(1).nIndexVertex = {3, 4, 5};
node.Mesh.Faces.at(2).nIndexVertex = {6, 7, 8};
node.Mesh.Faces.at(3).nIndexVertex = {9, 10, 11};
node.Mesh.Faces.at(4).nIndexVertex = {12, 13, 14};
node.Mesh.Faces.at(5).nIndexVertex = {15, 16, 17};
node.Mesh.Faces.at(6).nIndexVertex = {18, 19, 20};
node.Mesh.Faces.at(7).nIndexVertex = {21, 22, 23};
node.Mesh.Faces.at(8).nIndexVertex = {24, 25, 26};
node.Mesh.Faces.at(9).nIndexVertex = {27, 28, 29};
node.Mesh.Faces.at(10).nIndexVertex = {30, 31, 32};
node.Mesh.Faces.at(11).nIndexVertex = {33, 32, 31};
}
//ReportMdl << "PART 3 - stage 5" << "\n";
if(node.Head.nType & NODE_AABB){
if(Wok) Warning("Found an aabb node, but Wok already exists! Skipping this node...");
else{
Wok.reset(new WOK());
std::vector<Face*> allfaces;
for(int f = 0; f < node.Mesh.Faces.size(); f++){
allfaces.push_back(&node.Mesh.Faces.at(f));
}
std::stringstream file2;
BuildAabbTree(node.Walkmesh.RootAabb, allfaces, &file2);
//Write to Wok
std::stringstream file;
ReportMdl << "Generating WOK.\n";
Wok->CalculateWokData(node, Data.MH.vLytPosition, &file);
file << "\r\n\r\nAABB\r\n";
file << file2.str();
if(bDebug){
std::wstring sDir = GetFullPath();
sDir.reserve(MAX_PATH);
PathRemoveFileSpecW(&sDir[0]);
sDir.resize(wcslen(sDir.c_str()));
sDir += L"\\debug_aabb.txt";
ReportMdl << "Will write aabb debug to: " << to_ansi(sDir.c_str()) << "\n";
HANDLE hFile = bead_CreateWriteFile(sDir);