-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmat3.cpp
1327 lines (1129 loc) · 37.5 KB
/
mat3.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
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 "mat3.h"
#include <cassert>
#include <string.h>
#include <sstream> //for debug output
#include <iomanip> //dito
#include <fstream> //dito
#include <iostream>
using namespace std;
static void qfread(void *p, size_t size, size_t num, FILE *stream)
{
int len = fread(p, size, num, stream);
len=len;
}
//this is still somewhat incomplete...
namespace bmd
{
struct Mat3Header
{
char tag[4]; //'MAT3' or 'MAT2'
u32 sizeOfSection;
u16 count;
u16 pad;
/*
Left-indented stuff not completely known
The entries marked with * are missing in MAT2 blocks
(ie. in a MAT2 block entry 3 is cull mode etc.), the
Mat2Header is 12 bytes shorter because of this
0 - MatEntry array (indexed by next array)
1 - indexToMatEntryIndex (count many)
2 - string table
* 3 - MatIndirectTexturingEntry array, count many for all files i've seen
4 - cull mode (indexed by MatEntry.unk[1])
5 - color1 (rgba8) (amb color??)
6 - numChans (?) (indexed by MatEntry.unk[2])
7 - colorChanInfo
* 8 - color2 (rgba8) (mat color??)
* 9 - light
10 - texgen counts (-> vr_back_cloud.bdl) (indexed by MatEntry.unk[3])
11 - TexGen
12 - TexGen2
13 - TexMtxInfo
14 - TexMtxInfo2
15 - index to texture table
16 - TevOrderInfo array
17 - colorS10 (rgba16) (prev, c0-c2)
18 - color3 (rgba8) (konst0-3)
19 - tev counts (indexed by MatEntry.unk[4])
20 - tev stages
21 - tev swap mode info
22 - tev swap mode table
23 - fog info (indexed by MatEntry.indices2[0])
24 - alphacomp array (indexed by MatEntry.indices2[1])
25 - blend info array (indexed by MatEntry.indices2[2])
26 - zmode info array (indexed by MatEntry.unk[6])
The following fields are only here for MAT3 blocks, not for MAT2
27 - matData6
28 - matData7
30 - NBTScale (indexed by MatEntry.indices2[3])
*/
u32 offsets[30];
};
struct MatEntry
{
//(0 - possible values: 1, 4, 253 [1: draw on tree down, 4 on up??])
// - related to transparency sorting
//1 - index into cullModes
//2 - index into numChans
//3 - index into texgen counts
//4 - index into tev counts
//5 - index into matData6 (?)
//6 - index into zModes? (quite sure)
//7 - index into matData7 (?)
//(still missing stuff: isDirect, zCompLoc,
//enable/disable blend alphatest depthtest, ...)
u8 unk[8];
// 0, 1 - index into color1 (e.g. map_delfino3.bmd)
// 6, 7 - index into color2 (e.g. mo.bdl)
// 2, 3, 4, 5 - index into chanControls
//u16 chanControls[8];
u16 color1[2];
u16 chanControls[4];
u16 color2[2]; //not in MAT2 block
u16 lights[8]; //all 0xffff most of the time, not in MAT2 block
u16 texGenInfo[8];
u16 texGenInfo2[8];
u16 texMatrices[10]; //direct index
u16 dttMatrices[20]; //?? (I have no idea what dtt matrices do...)
u16 texStages[8]; //indices into textureTable
//constColor (GX_TEV_KCSEL_K0-3)
u16 color3[4]; //direct index
u8 constColorSel[16]; //0x0c most of the time (const color sel, GX_TEV_KCSEL_*)
u8 constAlphaSel[16]; //0x1c most of the time (const alpha sel, GX_TEV_KASEL_*)
u16 tevOrderInfo[16]; //direct index
//this is to be loaded into
//GX_CC_CPREV - GX_CC_A2??
u16 colorS10[4]; //direct index
//these two always contained the same data in all files
//I've seen...
u16 tevStageInfo[16]; //direct index
u16 tevSwapModeInfo[16]; //direct index
u16 tevSwapModeTable[4];
u16 unknown6[12]; //vf_118 has a float in here (but only in one block...)
//f32 unknown6[6];
//0 - fog index (vf_117.bdl)
//1 - alphaComp (vf_117.bdl, yoshi.bmd)
//2 - blendInfo (cl.bdl)
//3 - nbt scale?
u16 indices2[4];
};
//structs below are wip
struct MatIndirectTexturingEntry
{
//size = 312 = 0x138
//(not always...see default.bmd <- but there 3 and 2 point to the same
//location in file (string table). but default.bmd is the only file i know
//where number of ind tex entries doesn't match number of mats)
//this could be arguments to GX_SetIndTexOrder() plus some dummy values
u16 unk[10];
struct
{
float f[6]; //3x2 matrix? texmatrix?
u8 b[4];
} unk2[3];
//probably the arguments to GX_SetIndTexOrder()
//or GX_SetIndtexCoordScale() (index is first param)
u32 unk3[4];
struct
{
//the first 9 bytes of this array are probably the arguments to
//GX_SetTevIndirect (index is the first argument), the
//other three bytes are padding
u16 unk[4 + 2];
} unk4[16];
};
#if 0 // use the mat3.h version instead
struct ColorChanInfo
{
//this is wrong, the third element is sometimes 3 or 4,
//so perhaps the third argument is the channel, but
//then the order of the other fields may be different too
//perhaps no "channel" but two pad bytes at the end?
//observation:
//the second byte is always 0 if the model has not vertex colors,
//sometimes 1 otherwise
#if 0
u8 channel; // /* chan id */
u8 enable; //0/1
u8 ambSrc; //GX_SRC_REG, GX_SRC_VTX (0, 1) (??)
u8 matSrc; //GX_SRC_REG, GX_SRC_VTX (0, 1) (??)
u8 litMask; //GL_LIGHT* ??
u8 diffuseAttenuationFunc; //GX_DF_NONE, GX_DF_SIGNED, GX_DF_CLAMP (0, 1, 2) (??)
u8 attenuationFracFunc; //GX_AF_SPEC, GX_AF_SPOT, GX_AUF_NONE (0, 1, 2) (??)
u8 pad;
#endif
#if 0
//this could be right: (no)
u8 unk1;
u8 matColorSource;
u8 unk2;
u8 attenuationFracFunc; //quite sure
u8 diffuseAttenuationFunc; //quite sure
u8 unk3;
u8 pad[2];
//(lit mask is implied by index position in array in MatEntry)
#endif
//I think I finally got it:
#if 0
//(enable = litMask != 0...)
u8 ambColorSource;
u8 matColorSource;
u8 litMask;
u8 attenuationFracFunc;
u8 diffuseAttenuationFunc;
u8 unk;
u8 pad[2];
#endif
// http://kuribo64.cjb.net/?page=thread&id=532#14984
u8 enable;
u8 matColorSource;
u8 litMask;
u8 diffuseAttenuationFunc;
u8 attenuationFracFunc;
u8 ambColorSource;
u8 pad[2];
};
#endif
struct TexGenInfo
{
u8 texGenType;
u8 texGenSrc;
u8 matrix;
u8 pad;
};
struct TexMtxInfo
{
u16 unk;
u16 pad; //0xffff most of the time
//0, 1 - translate u, v ????? scale center????
//2 - rotate u, v ?????
//3, 4 - repeat texture this many times
// (texcoord scale u, v) (-> model (texmtx).bmd)
f32 f1[5];
u16 unk2;
u16 pad2;
f32 f2[2];
f32 f3[16]; //nearly always an 4x4 identity matrix
};
struct TevOrderInfo
{
u8 texCoordId;
u8 texMap;
u8 chanId;
u8 pad;
};
struct TevStageInfo
{
u8 unk; //always 0xff
//GX_SetTevColorIn() arguments
u8 colorIn[4]; //GX_CC_*
//GX_SetTevColorOp() arguments
u8 colorOp;
u8 colorBias;
u8 colorScale;
u8 colorClamp;
u8 colorRegId;
//GX_SetTevAlphaIn() arguments
u8 alphaIn[4]; //GC_CA_*
//GX_SetTevAlphaOp() arguments
u8 alphaOp;
u8 alphaBias;
u8 alphaScale;
u8 alphaClamp;
u8 alphaRegId;
u8 unk2; //always 0xff
};
struct TevSwapModeInfo
{
u8 rasSel;
u8 texSel;
u8 pad[2];
};
struct TevSwapModeTable
{
u8 r;
u8 g;
u8 b;
u8 a;
};
struct AlphaCompare
{
u8 comp0, ref0;
u8 alphaOp;
u8 comp1, ref1;
u8 pad[3]; //??
};
struct BlendInfo
{
u8 blendMode;
u8 srcFactor, dstFactor;
u8 logicOp;
};
struct ZModeInfo
{
u8 enable;
u8 func;
u8 updateEnable;
u8 pad; //(ref val?)
};
struct FogInfo
{
u8 fogType;
u8 enable;
u16 center;
f32 startZ;
f32 endZ;
f32 nearZ;
f32 farZ;
u32 color; //rgba
u16 adjTable[10]; //????
};
};
bmd::MatIndirectTexturingEntry g_defaultIndirectEntry =
{
{
0, 0xffff, 0xffff, 0xffff, 0xffff,
0xffff, 0xffff, 0xffff, 0xffff, 0xffff
}, //unk1
{
{
{ .5f, 0.f, 0.f, 0.f, .5f, 0.f },
{ 1, 0xff, 0xff, 0xff }
},
{
{ .5f, 0.f, 0.f, 0.f, .5f, 0.f },
{ 1, 0xff, 0xff, 0xff }
},
{
{ .5f, 0.f, 0.f, 0.f, .5f, 0.f },
{ 1, 0xff, 0xff, 0xff }
}
},
{ 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff },
{
{ {0, 0, 0, 0, 0x00ff, 0xffff} },
{ {0, 0, 0, 0, 0x00ff, 0xffff} },
{ {0, 0, 0, 0, 0x00ff, 0xffff} },
{ {0, 0, 0, 0, 0x00ff, 0xffff} },
{ {0, 0, 0, 0, 0x00ff, 0xffff} },
{ {0, 0, 0, 0, 0x00ff, 0xffff} },
{ {0, 0, 0, 0, 0x00ff, 0xffff} },
{ {0, 0, 0, 0, 0x00ff, 0xffff} },
{ {0, 0, 0, 0, 0x00ff, 0xffff} },
{ {0, 0, 0, 0, 0x00ff, 0xffff} },
{ {0, 0, 0, 0, 0x00ff, 0xffff} },
{ {0, 0, 0, 0, 0x00ff, 0xffff} },
{ {0, 0, 0, 0, 0x00ff, 0xffff} },
{ {0, 0, 0, 0, 0x00ff, 0xffff} },
{ {0, 0, 0, 0, 0x00ff, 0xffff} },
{ {0, 0, 0, 0, 0x00ff, 0xffff} }
}
};
void displayData(ostream& out, const string& str, FILE* f, int offset, int size, int space = -1)
{
long old = ftell(f);
fseek(f, offset, SEEK_SET);
out << str << ":";
for(int i = 0, c = 1; i < size; ++i, ++c)
{
if(space > 0 && (i%space == 0))
{
if(space >= 16)
out << endl;
else
out << ' ';
}
u8 v; qfread(&v, 1, 1, f);
out << hex << setw(2) << setfill('0') << (int)v;
}
if(space > 0)
out << " (" << size/float(space) << " many)" << endl;
//log("%s", textStream.str().c_str());
fseek(f, old, SEEK_SET);
}
void readTevStageInfo(FILE* f, bmd::TevStageInfo& info);
void displayTevStage(ostream& out, FILE* f, int offset, int size)
{
if(size%20 != 0)
{
warn("TevStage has wrong size");
return;
}
long old = ftell(f);
fseek(f, offset, SEEK_SET);
out << "TevStageInfo (ff) (ain, bin, cin, din) (op, bias, scale, doClamp, tevRegId):";
int count = size/20;
for(int i = 0; i < count; ++i)
{
bmd::TevStageInfo info;
readTevStageInfo(f, info);
//textStream << hex << setw(2) << setfill('0') << (int)v;
out << endl << hex << setfill('0') <<
setw(2) << (int)info.unk << " " <<
setw(2) << (int)info.colorIn[0] << ' ' <<
setw(2) << (int)info.colorIn[1] << ' ' <<
setw(2) << (int)info.colorIn[2] << ' ' <<
setw(2) << (int)info.colorIn[3] << " " <<
setw(2) << (int)info.colorOp << ' ' <<
setw(2) << (int)info.colorBias << ' ' <<
setw(2) << (int)info.colorScale << ' ' <<
setw(2) << (int)info.colorClamp << ' ' <<
setw(2) << (int)info.colorRegId << " " <<
setw(2) << (int)info.alphaIn[0] << ' ' <<
setw(2) << (int)info.alphaIn[1] << ' ' <<
setw(2) << (int)info.alphaIn[2] << ' ' <<
setw(2) << (int)info.alphaIn[3] << " " <<
setw(2) << (int)info.alphaOp << ' ' <<
setw(2) << (int)info.alphaBias << ' ' <<
setw(2) << (int)info.alphaScale << ' ' <<
setw(2) << (int)info.alphaClamp << ' ' <<
setw(2) << (int)info.alphaRegId << " " <<
setw(2) << (int)info.unk2;
}
out << " (" << dec << count << " many)" << endl;
//log("%s", textStream.str().c_str(), count);
fseek(f, old, SEEK_SET);
}
void ReadColorChanInfo(ColorChanInfo& dst, FILE *f);
void displayColorChanInfo(ostream& out, FILE* f, int offset, int size)
{
int num = size / 8;
out << "ColorChanInfo: (" << dec << num << " many)\n";
int i;
long old = ftell(f);
fseek(f, offset, SEEK_SET);
for(i=0;i<num;++i)
{
ColorChanInfo t;
ReadColorChanInfo(t, f);
out << i << ": "
<< " enable=" << (int)t.enable
<< " matColorSource=" << (int)t.matColorSource
<< " litMask=" << (int)t.litMask
<< " diff_fn=" << (int)t.diffuseAttenuationFunc
<< " attn_fn=" << (int)t.attenuationFracFunc
<< " ambColorSource=" << (int)t.ambColorSource
<< endl;
}
fseek(f, old, SEEK_SET);
}
void displaySize(ostream& out, const string& str, int len, int space = -1)
{
float count = -1;
if(space > 0)
count = len/float(space);
out << str << ": " << len << " bytes (" << count << " many)" << endl;
}
void writeMat3Data(ostream& debugOut, FILE* f, int mat3Offset,
bmd::Mat3Header& h, const vector<size_t>& lengths)
{
//displayData(debugOut, "matEntries", f, mat3Offset + h.offsets[0], sizeof(bmd::MatEntry));
displayData(debugOut, "indexToMatIndex", f, mat3Offset + h.offsets[1], 2*h.count, 2);
//2: string table
displaySize(debugOut, "IndirectTexturing", lengths[3], 312);
displayData(debugOut, "cull mode", f, mat3Offset + h.offsets[4], lengths[4], 4);
displayData(debugOut, "color1", f, mat3Offset + h.offsets[5], lengths[5], 4);
displayData(debugOut, "numChans (?) (unk[2])", f, mat3Offset + h.offsets[6], lengths[6], 1);
displayColorChanInfo(debugOut, f, mat3Offset + h.offsets[7], lengths[7]);
// displayData(debugOut, "colorChanInfo", f, mat3Offset + h.offsets[7], lengths[7], 8);
displayData(debugOut, "color2", f, mat3Offset + h.offsets[8], lengths[8], 4);
displayData(debugOut, "light", f, mat3Offset + h.offsets[9], lengths[9], 52); //there's one dr_comp.bdl
displayData(debugOut, "texCounts (unk[3])", f, mat3Offset + h.offsets[10], lengths[10], 1);
displayData(debugOut, "TexGen", f, mat3Offset + h.offsets[11], lengths[11], 4);
displayData(debugOut, "TexGen2", f, mat3Offset + h.offsets[12], lengths[12], 4);
displaySize(debugOut, "TexMtxInfo", lengths[13], 100); //13: TexMtxInfo (below)
displaySize(debugOut, "TexMtxInfo2", lengths[14]);
displayData(debugOut, "indexToTexIndex", f, mat3Offset + h.offsets[15], lengths[15], 2);
displayData(debugOut, "tevOrderInfo", f, mat3Offset + h.offsets[16], lengths[16], 4);
displayData(debugOut, "colorS10", f, mat3Offset + h.offsets[17], lengths[17], 8);
displayData(debugOut, "color3", f, mat3Offset + h.offsets[18], lengths[18], 4);
displayData(debugOut, "tevStageCounts (unk[4])", f, mat3Offset + h.offsets[19], lengths[19], 1);
displayTevStage(debugOut, f, mat3Offset + h.offsets[20], lengths[20]);
displayData(debugOut, "tevSwapModeInfo", f, mat3Offset + h.offsets[21], lengths[21], 4);
displayData(debugOut, "tevSwapModeTable", f, mat3Offset + h.offsets[22], lengths[22], 4);
displayData(debugOut, "fog", f, mat3Offset + h.offsets[23], lengths[23], 44);
displayData(debugOut, "alpha comp", f, mat3Offset + h.offsets[24], lengths[24], 8);
displayData(debugOut, "blend info", f, mat3Offset + h.offsets[25], lengths[25], 4);
displayData(debugOut, "z mode", f, mat3Offset + h.offsets[26], lengths[26], 4);
displayData(debugOut, "matData6 (isIndirect (?))", f, mat3Offset + h.offsets[27], lengths[27], 1);
displayData(debugOut, "matData7", f, mat3Offset + h.offsets[28], lengths[28], 1);
displayData(debugOut, "NBTscale", f, mat3Offset + h.offsets[29], lengths[29]);
}
void writeMatEntry(ostream& debugOut, const bmd::MatEntry& init)
{
int j;
debugOut << setfill('0');
const char *unknames[8] = {"unk1", "cull", "numChanIndex", "texCounts",
"tevCountIndex", "matData6Index", "zMode", "matData7Index"};
debugOut << "unk:";
for(j = 0; j < 8; ++j) debugOut << " " << unknames[j] << "=" << hex << setw(2) << (int)init.unk[j]; debugOut << endl;
debugOut << "color1 (?): ";
for(j = 0; j < 2; ++j) debugOut << setw(4) << init.color1[j]; debugOut << endl;
debugOut << "chanControls (?): ";
for(j = 0; j < 4; ++j) debugOut << setw(4) << init.chanControls[j]; debugOut << endl;
debugOut << "color2 (?): ";
for(j = 0; j < 2; ++j) debugOut << setw(4) << init.color2[j]; debugOut << endl;
debugOut << "lights: ";
for(j = 0; j < 8; ++j) debugOut << setw(4) << init.lights[j]; debugOut << endl;
debugOut << "texGenInfo: ";
for(j = 0; j < 8; ++j) debugOut << setw(4) << init.texGenInfo[j]; debugOut << endl;
debugOut << "texGenInfo2: ";
for(j = 0; j < 8; ++j) debugOut << setw(4) << init.texGenInfo2[j]; debugOut << endl;
debugOut << "texMatrices: ";
for(j = 0; j < 10; ++j) debugOut << setw(4) << init.texMatrices[j]; debugOut << endl;
debugOut << "dttMatrices: ";
for(j = 0; j < 20; ++j) debugOut << setw(4) << init.dttMatrices[j]; debugOut << endl;
debugOut << "Textures: ";
for(j = 0; j < 8; ++j) debugOut << setw(4) << init.texStages[j]; debugOut << endl;
debugOut << "color3: ";
for(j = 0; j < 4; ++j) debugOut << setw(4) << init.color3[j]; debugOut << endl;
debugOut << "constColorSel: ";
for(j = 0; j < 16; ++j) debugOut << hex << setw(2) << (int)init.constColorSel[j]; debugOut << endl;
debugOut << "constAlphaSel: ";
for(j = 0; j < 16; ++j) debugOut << hex << setw(2) << (int)init.constAlphaSel[j]; debugOut << endl;
debugOut << "tevOrderInfo: ";
for(j = 0; j < 16; ++j) debugOut << setw(4) << init.tevOrderInfo[j]; debugOut << endl;
debugOut << "colorS10: ";
for(j = 0; j < 4; ++j) debugOut << setw(4) << init.colorS10[j]; debugOut << endl;
debugOut << "tevStageInfo: ";
for(j = 0; j < 16; ++j) debugOut << setw(4) << init.tevStageInfo[j]; debugOut << endl;
debugOut << "tevSwapModeInfo: ";
for(j = 0; j < 16; ++j) debugOut << setw(4) << init.tevSwapModeInfo[j]; debugOut << endl;
debugOut << "tevSwapModeTable: ";
for(j = 0; j < 4; ++j) debugOut << setw(4) << init.tevSwapModeTable[j]; debugOut << endl;
debugOut << "unk6: ";
for(j = 0; j < 12; ++j) debugOut << setw(4) << init.unknown6[j]; debugOut << endl;
//for(j = 0; j < 6; ++j) debugOut << " " << init.unknown6[j]; debugOut << endl;
debugOut << "index fog, alphaComp, blend, nbtScale: ";
for(j = 0; j < 4; ++j) debugOut << setw(4) << init.indices2[j]; debugOut << endl;
debugOut << endl;
}
void writeTexMtxInfo(ostream& debugOut, const bmd::TexMtxInfo& info)
{
int j;
debugOut << hex;
debugOut << info.unk << " ";
debugOut << info.pad << endl;
for(j = 0; j < 5; ++j)
debugOut << info.f1[j] << " ";
debugOut << endl << info.unk2<< " ";
debugOut << info.pad2 << endl;
for(j = 0; j < 2; ++j)
debugOut << info.f2[j] << " "; debugOut << endl;
for(j = 0; j < 16; ++j)
{
debugOut << info.f3[j] << " ";
if((j+1)%4 == 0) debugOut << endl;
}
debugOut << endl;
debugOut << dec;
}
void readMat3Header(FILE* f, bmd::Mat3Header& h)
{
qfread(h.tag, 1, 4, f);
readDWORD(f, h.sizeOfSection);
readWORD(f, h.count);
readWORD(f, h.pad);
for(int i = 0; i < 30; ++i)
readDWORD(f, h.offsets[i]);
if(strncmp(h.tag, "MAT2", 4) == 0)
{
//check if this is a MAT3 section (most of the time) or a MAT2 section
//(TODO: probably there's also a MAT1 section - find one...)
//if this is a mat2 section, convert header to a mat3 header
for(int j = 29; j >= 0; --j)
{
u32 t;
if(j < 3) t = h.offsets[j];
else if(j == 3 || j == 8 || j == 9) t = 0;
else if(j < 8) t = h.offsets[j - 1];
else t = h.offsets[j - 3];
h.offsets[j] = t;
}
}
}
void readMatIndirectTexturingEntry(FILE* f, bmd::MatIndirectTexturingEntry& indEntry)
{
int k, m;
for(k = 0; k < 10; ++k)
readWORD(f, indEntry.unk[k]);
for(k = 0; k < 3; ++k)
{
for(m = 0; m < 6; ++m)
readFLOAT(f, indEntry.unk2[k].f[m]);
qfread(indEntry.unk2[k].b, 1, 4, f);
}
for(k = 0; k < 4; ++k)
readDWORD(f, indEntry.unk3[k]);
for(k = 0; k < 16; ++k)
for(m = 0; m < 6; ++m)
readWORD(f, indEntry.unk4[k].unk[m]);
}
void readMatEntry(FILE* f, bmd::MatEntry& init, bool isMat2)
{
int j;
qfread(init.unk, 1, 8, f);
for(j = 0; j < 2; ++j) readWORD(f, init.color1[j]);
for(j = 0; j < 4; ++j) readWORD(f, init.chanControls[j]);
//these two fields are only in mat3 headers, not in mat2
if(!isMat2) for(j = 0; j < 2; ++j) readWORD(f, init.color2[j]);
else memset(init.color2, 0xff, 2*2);
if(!isMat2) for(j = 0; j < 8; ++j) readWORD(f, init.lights[j]);
else memset(init.lights, 0xff, 8*2);
for(j = 0; j < 8; ++j) readWORD(f, init.texGenInfo[j]);
for(j = 0; j < 8; ++j) readWORD(f, init.texGenInfo2[j]);
for(j = 0; j < 10; ++j) readWORD(f, init.texMatrices[j]);
for(j = 0; j < 20; ++j) readWORD(f, init.dttMatrices[j]);
for(j = 0; j < 8; ++j) readWORD(f, init.texStages[j]);
for(j = 0; j < 4; ++j) readWORD(f, init.color3[j]);
qfread(init.constColorSel, 1, 16, f);
qfread(init.constAlphaSel, 1, 16, f);
for(j = 0; j < 16; ++j) readWORD(f, init.tevOrderInfo[j]);
for(j = 0; j < 4; ++j) readWORD(f, init.colorS10[j]);
for(j = 0; j < 16; ++j) readWORD(f, init.tevStageInfo[j]);
for(j = 0; j < 16; ++j) readWORD(f, init.tevSwapModeInfo[j]);
for(j = 0; j < 4; ++j) readWORD(f, init.tevSwapModeTable[j]);
for(j = 0; j < 12; ++j) readWORD(f, init.unknown6[j]);
//for(j = 0; j < 6; ++j) readFLOAT(f, init.unknown6[j]);
for(j = 0; j < 4; ++j) readWORD(f, init.indices2[j]);
}
void readTexMtxInfo(FILE* f, bmd::TexMtxInfo& info)
{
int j;
readWORD(f, info.unk);
readWORD(f, info.pad);
for(j = 0; j < 5; ++j)
readFLOAT(f, info.f1[j]);
readWORD(f, info.unk2);
readWORD(f, info.pad2);
for(j = 0; j < 2; ++j)
readFLOAT(f, info.f2[j]);
for(j = 0; j < 16; ++j)
readFLOAT(f, info.f3[j]);
}
void readTevStageInfo(FILE* f, bmd::TevStageInfo& info)
{
qfread(&info.unk, 1, 1, f);
qfread(&info.colorIn, 1, 4, f);
qfread(&info.colorOp, 1, 1, f);
qfread(&info.colorBias, 1, 1, f);
qfread(&info.colorScale, 1, 1, f);
qfread(&info.colorClamp, 1, 1, f);
qfread(&info.colorRegId, 1, 1, f);
qfread(&info.alphaIn, 1, 4, f);
qfread(&info.alphaOp, 1, 1, f);
qfread(&info.alphaBias, 1, 1, f);
qfread(&info.alphaScale, 1, 1, f);
qfread(&info.alphaClamp, 1, 1, f);
qfread(&info.alphaRegId, 1, 1, f);
qfread(&info.unk2, 1, 1, f);
}
void computeSectionLengths(const bmd::Mat3Header& h, vector<size_t>& lengths)
{
for(size_t i = 0; i < 30; ++i)
{
size_t length = 0;
if(h.offsets[i] != 0)
{
size_t next = h.sizeOfSection;
for(size_t j = i + 1; j < 30; ++j)
{
if(h.offsets[j] != 0)
{
next = h.offsets[j];
break;
}
}
length = next - h.offsets[i];
}
lengths[i] = length;
if(i == 3)
{
//assert(length%h.count == 0); //violated by luigi's mansion files
//assert(length/h.count == 312); //violated by quite a few files
}
}
}
void ReadColorChanInfo(ColorChanInfo& dst, FILE *f)
{
unsigned char temp[8];
qfread(temp, 1, sizeof(temp), f);
dst.enable = temp[0];
dst.matColorSource = temp[1];
dst.litMask = temp[2];
dst.diffuseAttenuationFunc = temp[3];
dst.attenuationFracFunc = temp[4];
dst.ambColorSource = temp[5];
dst.pad[0] = temp[6];
dst.pad[1] = temp[7];
}
void dumpMat3(FILE* f, Mat3& dst)
{
//warn("Mat3 section support is incomplete");
assert(sizeof(bmd::MatEntry) == 332);
int mat3Offset = ftell(f);
size_t i;
//read header
bmd::Mat3Header h;
readMat3Header(f, h);
bool isMat2 = strncmp(h.tag, "MAT2", 4) == 0;
if(isMat2)
warn("Model contains MAT2 block instead of MAT3");
//read stringtable
//vector<string> stringtable;
readStringtable(mat3Offset + h.offsets[2], f, dst.stringtable);
if(h.count != dst.stringtable.size())
warn("mat3: number of strings (%d) doesn't match number of elements (%d)",
dst.stringtable.size(), h.count);
//compute max length of each subsection
//(it's probably better to check the maximal indices
//of every MatEntry and use these as counts, but
//this works fine as well, so stick with it for now)
vector<size_t> lengths(30);
computeSectionLengths(h, lengths);
//offset[1] (indirection table from indices to init data indices)
fseek(f, mat3Offset + h.offsets[1], SEEK_SET);
u16 maxIndex = 0;
dst.indexToMatIndex.resize(h.count);
for(i = 0; i < h.count; ++i)
{
u16 bla; readWORD(f, bla);
maxIndex = max(maxIndex, bla);
dst.indexToMatIndex[i] = bla;
}
//offset[4] (cull mode)
fseek(f, mat3Offset + h.offsets[4], SEEK_SET);
dst.cullModes.resize(lengths[4]/4);
for(i = 0; i < dst.cullModes.size(); ++i)
{
u32 tmp; readDWORD(f, tmp);
dst.cullModes[i] = tmp;
}
//offset[5] (color1)
fseek(f, mat3Offset + h.offsets[5], SEEK_SET);
dst.color1.resize(lengths[5]/4);
for(i = 0; i < dst.color1.size(); ++i)
{
u8 col[4]; qfread(col, 1, 4, f);
dst.color1[i].r = col[0];
dst.color1[i].g = col[1];
dst.color1[i].b = col[2];
dst.color1[i].a = col[3];
}
//offset[6] (numChans)
fseek(f, mat3Offset + h.offsets[6], SEEK_SET);
dst.numChans.resize(lengths[6]);
qfread(&dst.numChans[0], 1, lengths[6], f);
//offset[7] (colorChanInfo)
fseek(f, mat3Offset + h.offsets[7], SEEK_SET);
dst.colorChanInfos.resize(lengths[7]/8);
for(i = 0; i < dst.colorChanInfos.size(); ++i)
{
ReadColorChanInfo(dst.colorChanInfos[i], f);
}
//offset[8] (color2)
fseek(f, mat3Offset + h.offsets[8], SEEK_SET);
dst.color2.resize(lengths[8]/4);
for(i = 0; i < dst.color2.size(); ++i)
{
u8 col[4]; qfread(col, 1, 4, f);
dst.color2[i].r = col[0];
dst.color2[i].g = col[1];
dst.color2[i].b = col[2];
dst.color2[i].a = col[3];
}
//offset[0] (MatEntries)
fseek(f, mat3Offset + h.offsets[0], SEEK_SET);
dst.materials.resize(maxIndex + 1);
for(i = 0; i <= maxIndex; ++i)
{
bmd::MatEntry init;
readMatEntry(f, init, isMat2);
//copy data
//(this assumes that init has already been endian-fixed)
Material& dstMat = dst.materials[i];
dstMat.flag = init.unk[0];
dstMat.cullIndex = init.unk[1];
dstMat.numChansIndex = init.unk[2];
dstMat.texGenCountIndex = init.unk[3];
dstMat.tevCountIndex = init.unk[4];
dstMat.zModeIndex = init.unk[6];
int j;
for(j = 0; j < 8; ++j)
{
dstMat.texGenInfos[j] = init.texGenInfo[j];
dstMat.texMtxInfos[j] = init.texMatrices[j];
dstMat.texStages[j] = init.texStages[j];
}
for(j = 0; j < 4; ++j)
{
dstMat.color3[j] = init.color3[j];
dstMat.colorS10[j] = init.colorS10[j];
dstMat.chanControls[j] = init.chanControls[j];
dstMat.tevSwapModeTable[j] = init.tevSwapModeTable[j];
}
for(j = 0; j < 2; ++j)
{
dstMat.color1[j] = init.color1[j];
dstMat.color2[j] = init.color2[j];
}
for(j = 0; j < 16; ++j)
{
dstMat.constColorSel[j] = init.constColorSel[j];
dstMat.constAlphaSel[j] = init.constAlphaSel[j];
dstMat.tevOrderInfo[j] = init.tevOrderInfo[j];
dstMat.tevStageInfo[j] = init.tevStageInfo[j];
dstMat.tevSwapModeInfo[j] = init.tevSwapModeInfo[j];
}
dstMat.alphaCompIndex = init.indices2[1];
dstMat.blendIndex = init.indices2[2];
}
//offset[3] indirect texturing blocks (always as many as count)
assert(sizeof(bmd::MatIndirectTexturingEntry) == 312);
fseek(f, mat3Offset + h.offsets[3], SEEK_SET);
if(lengths[3]%312 != 0)
warn("mat3: indirect texturing block size no multiple of 312: %d", lengths[3]);
else if(lengths[3]/312 != h.count)
warn("mat3: number of ind texturing blocks (%d) doesn't match number of materials (%d)",
lengths[3]/312, h.count);
else
{
for(i = 0; i < h.count; ++i)
{
bmd::MatIndirectTexturingEntry indEntry;
readMatIndirectTexturingEntry(f, indEntry);
//...
if(memcmp(&g_defaultIndirectEntry, &indEntry, sizeof(indEntry)) != 0)
warn("found different ind tex block");
}
}
//offsets[10] (read texGenCounts)
fseek(f, mat3Offset + h.offsets[10], SEEK_SET);
dst.texGenCounts.resize(lengths[10]);
qfread(&dst.texGenCounts[0], 1, dst.texGenCounts.size(), f);
//offsets[11] (texGens)
fseek(f, mat3Offset + h.offsets[11], SEEK_SET);
dst.texGenInfos.resize(lengths[11]/4);
for(i = 0; i < dst.texGenInfos.size(); ++i)
{
bmd::TexGenInfo info;
qfread(&info.texGenType, 1, 1, f);
qfread(&info.texGenSrc, 1, 1, f);
qfread(&info.matrix, 1, 1, f);
qfread(&info.pad, 1, 1, f);
dst.texGenInfos[i].texGenType = info.texGenType;
dst.texGenInfos[i].texGenSrc = info.texGenSrc;
dst.texGenInfos[i].matrix = info.matrix;
}
//offset[13] (texmtxinfo debug)
if(lengths[13]%(100) != 0)
warn("ARGH: unexpected texmtxinfo lengths[13]: %d", lengths[13]);
else
{
fseek(f, mat3Offset + h.offsets[13], SEEK_SET);
for(size_t m = 0; m < lengths[13]/(25*4); ++m)
{
bmd::TexMtxInfo info;
readTexMtxInfo(f, info);
if(info.unk != 0x0100) //sometimes violated
warn("(mat3texmtx) %x instead of 0x0100", info.unk);
if(info.pad != 0xffff)
warn("(mat3texmtx) %x instead of 0xffff", info.pad);
if(info.unk2 != 0x0000)
warn("(mat3texmtx) %x instead of 0x0000", info.unk2);
if(info.pad2 != 0xffff)
warn("(mat3texmtx) %x instead of 2nd 0xffff", info.pad2);
}
}
//offsets[13] (read texMtxInfo)
fseek(f, mat3Offset + h.offsets[13], SEEK_SET);
dst.texMtxInfos.resize(lengths[13]/100);
for(i = 0; i < dst.texMtxInfos.size(); ++i)
{
bmd::TexMtxInfo info;