-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFF7TMDModel.cs
1865 lines (1572 loc) · 103 KB
/
FF7TMDModel.cs
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
// Info of this comes from:
// https://wiki.xentax.com/index.php/Playstation_TMD
//Specific info about some fields of the structures:
// U_CHAR FLAG OPTIONS FOR PRIMITIVE
// {
// bit 0; -LGT.Light source calculation
// 0 - On.Light source calculation is carried out
// 1 - Off.Light source calculation is not carried out
// bit 1; -FCE.Determines if primitive is one or two faced
// 0 - Primitive has one face
// 1 - Primitive has two faces
// bit 2; -GRD.Determines single or gradient pigment
// 0 - Primitive has one solid pigment
// 1 - Primitive has different pigments at each vertex
// bits 3-7; -ignored
// }
// U_CHAR MODE OPTIONS FOR PRIMITIVE
// {
// bit 0; -TGE.Brightness calculation at time of calculation
// 0 - On.Calculates light
// 1 - Off.Draws texture as is (without lighting)
// bit 1; -ABE.Activates translucency when rendered
// 0 - Off.Primitive is rendered solid.
// 1 - On.Primitive is rendered semitranslucent.
// bit 2; -TME.Sets whether a texture is used or not
// 0 - Off.No texture is rendered.
// 1 - On.A texture is rendered.
// bit 3; -displays whether a 3 or 4 sided polygon
// 0 - 3 sided polygon
// 1 - 4 sided polygon
// bit 4; -IIP.Shading mode
// 0 - Flat shading
// 1 - Gouraud shading
// bits 5-7; -Code.Defines which sort of entity to draw.
// (5)0 - Polygon(3 sided or 4 sided)
// (6)1 - Straight line
// (7)2 - Sprite
// }
// U_SHORT CBA - position of CLUT in VRAM for primitives with textures
// {
// bits 0-5; -upper 6 bits of 10 bits of X coordinate value
// for CLUT in VRAM
// bits 6-14; -9 bits of Y coordinate value for CLUT in VRAM
// bit 15; -ignored
// }
// U_SHORT TSB - information about image for primitives with textures
//{
// bits 0-4; -texture page number of texture
// bits 5-6; -ABR.Semitransparency method(see below)
// bits 7-8; -TPF.Colour mode of the texture(see below)
// bits 9-15; -ignored
// }
// ABR Values
// 0 - 50 % background + 50 % polygon
// 1 - 100 % background + 100 % polygon
// 2 - 100 % background - 100 % polygon
// 3 - 100 % background + 25 % polygon
// TPF Values
// 0 - 4 bit texture and CLUT
// 1 - 8 bit texture and CLUT
// 2 - 15 bit texture with no CLUT
using System;
using System.IO;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
namespace KimeraCS
{
using Defines;
using static FF7Skeleton;
using static FF7PModel;
using static Utils;
using static FileTools;
public class FF7TMDModel
{
public const int TMD_PADDING = 12;
//////////////////////////////////////////////////////////////////////////////////////////
// TMD Packet Struct types
// 3 SIDED, FLAT SHADING, FLAT PIGMENT
// olen = 0x04; ilen =0x03; flag =0x00; mode =0x20;
public struct TMD_3_FS_FP
{
public byte R, G, B; // pigment of polygon
public byte mode2; // same as mode in primitive
public ushort Normal0; // index value of normal element
public ushort Vertex0; // index value of vertex element
public ushort Vertex1;
public ushort Vertex2;
}
// 3 SIDED, GOURAUD SHADING, FLAT PIGMENT
// olen = 0x06; ilen =0x04; flag =0x00; mode =0x30;
public struct TMD_3_GS_FP
{
public byte R, G, B; // pigment of polygon
public byte mode2; // same as mode in primitive
public ushort Normal0; // index value of normal element
public ushort Vertex0; // index value of vertex element
public ushort Normal1;
public ushort Vertex1;
public ushort Normal2;
public ushort Vertex2;
}
// 3 SIDED, FLAT SHADING, GRADIENT PIGMENT
// olen = 0x06; ilen =0x05; flag =0x04; mode =0x20;
public struct TMD_3_FS_GP
{
public byte R0, G0, B0; // pigment of polygon at Vertex0
public byte mode2; // same as mode in primitive
public byte R1, G1, B1; // pigment of polygon at Vertex1
public byte pad1; // ignored
public byte R2, G2, B2; // pigment of polygon at Vertex2
public byte pad2; // ignored
public ushort Normal0; // index value of normal element
public ushort Vertex0; // index value of vertex element
public ushort Vertex1;
public ushort Vertex2;
}
// 3 SIDED, GOURAUD SHADING, GRADIENT PIGMENT
// olen = 0x06; ilen =0x06; flag =0x04; mode =0x30;
public struct TMD_3_GS_GP
{
public byte R0, G0, B0; // pigment of polygon at Vertex0
public byte mode2; // same as mode in primitive
public byte R1, G1, B1; // pigment of polygon at Vertex1
public byte pad1; // ignored
public byte R2, G2, B2; // pigment of polygon at Vertex2
public byte pad2; // ignored
public ushort Normal0; // index value of normal element
public ushort Vertex0; // index value of vertex element
public ushort Normal1;
public ushort Vertex1;
public ushort Normal2;
public ushort Vertex2;
}
// 3 SIDED, TEXTURED, FLAT SHADING, NO PIGMENT
// olen = 0x07; ilen =0x05; flag =0x00; mode =0x24;
public struct TMD_3_TX_FS_NP
{
public byte U0, V0; // X and Y coordinate of texture for Vertex0
public ushort CBA; // position of CLUT for texture in VRAM(see earlier)
public byte U1, V1; // X and Y coordinate of texture for Vertex1
public ushort TSB; // information about texture in VRAM(see earlier)
public byte U2, V2; // X and Y coordinate of texture for Vertex2
public ushort pad; // ignored
public ushort Normal; // index value of normal element
public ushort Vertex0; // index value of vertex element
public ushort Vertex1;
public ushort Vertex2;
}
// 3 SIDED, TEXTURED, GOURAUD SHADING, NO PIGMENT
// olen = 0x9; ilen =0x06; flag =0x00; mode =0x34;
public struct TMD_3_TX_GS_NP
{
public byte U0, V0; // X and Y coordinate of texture for Vertex0
public ushort CBA; // position of CLUT for texture in VRAM(see earlier)
public byte U1, V1; // X and Y coordinate of texture for Vertex1
public ushort TSB; // information about texture in VRAM(see earlier)
public byte U2, V2; // X and Y coordinate of texture for Vertex2
public ushort pad; // ignored
public ushort Normal; // index value of normal element
public ushort Vertex0; // index value of vertex element
public ushort Normal1;
public ushort Vertex1;
public ushort Normal2;
public ushort Vertex2;
}
// 3 SIDED, NO SHADING, FLAT PIGMENT
// olen = 0x04; ilen =0x03; flag =0x01; mode =0x21;
public struct TMD_3_NS_FP
{
public byte R, G, B; // pigment of polygon
public byte mode2; // same as mode in primitive
public ushort Vertex0; // index value of vertex element
public ushort Vertex1;
public ushort Vertex2;
public ushort pad; // ignored
}
// 3 SIDED, NO SHADING, GRADIENT PIGMENT
// olen = 0x06; ilen =0x05; flag =0x01; mode =0x35;
// olen = 0x06; ilen =0x05; flag =0x01; mode =0x31; FF7 Vertex Colored Version
public struct TMD_3_NS_GP
{
public byte R0, G0, B0; // pigment of polygon at Vertex0
public byte mode2; // same as mode in primitive
public byte R1, G1, B1; // pigment of polygon at Vertex1
public byte pad1; // ignored
public byte R2, G2, B2; // pigment of polygon at Vertex2
public byte pad2; // ignored
public ushort Vertex0; // index value of vertex element
public ushort Vertex1;
public ushort Vertex2;
public ushort pad; // ignored
}
// 3 SIDED, TEXTURED, NO SHADING, FLAT PIGMENT
// olen = 0x07; ilen =0x06; flag =0x01; mode =0x25; FF7 Texture Version
public struct TMD_3_TX_NS_FP
{
public byte U0, V0; // X and Y coordinate of texture for Vertex0
public ushort CBA; // position of CLUT for texture in VRAM(see earlier)
public byte U1, V1; // X and Y coordinate of texture for Vertex1
public ushort TSB; // information about texture in VRAM(see earlier)
public byte U2, V2; // X and Y coordinate of texture for Vertex2
public ushort pad1; // ignored
public byte R, G, B; // pigment of polygon
public byte pad2;
public ushort Vertex0; // index value of vertex element
public ushort Vertex1;
public ushort Vertex2;
public ushort pad; // ignored
}
// 3 SIDED, TEXTURED, NO SHADING, GRADIENT PIGMENT
// olen = 0x9; ilen =0x08; flag =0x01; mode =0x35;
public struct TMD_3_TX_NS_GP
{
public byte U0, V0; // X and Y coordinate of texture for Vertex0
public ushort CBA; // position of CLUT for texture in VRAM(see earlier)
public byte U1, V1; // X and Y coordinate of texture for Vertex1
public ushort TSB; // information about texture in VRAM(see earlier)
public byte U2, V2; // X and Y coordinate of texture for Vertex2
public ushort pad1; // ignored
public byte R0, G0, B0; // pigment of polygon at Vertex0
public byte pad2; // ignored
public byte R1, G1, B1; // pigment of polygon at Vertex1
public byte pad3; // ignored
public byte R2, G2, B2; // pigment of polygon at Vertex2
public byte pad4; // ignored
public ushort Vertex0; // index value of vertex element
public ushort Vertex1;
public ushort Vertex2;
public ushort pad; // ignored
}
//////////////////////////////////////////////////////////////////////////////////////////
// TMD Main structs
public struct TMD_HEADER
{
public int version; // version of TMD. Always 0x00000041
public int flags; // Indicates when addresses are relative or explicit
public int nObjects; // number of objects in the TMD
}
public struct TMD_PRIMITIVE_HEADER
{
public byte olen; // word length of the drawing primitive created by GPU
public byte ilen; // word length of the packet data section
public byte flag; // options when rendering
public byte mode; // indicates type of primitive
}
public struct TMD_VERTEX
{
public short vx; // x value of the vertex
public short vy; // y value of the vertex
public short vz; // z value of the vertex
public short pad; // ignore
public float fvx; // x value (float) of the vertex
public float fvy; // y value (float) of the vertex
public float fvz; // z value (float) of the vertex
public float fpad; // ignore
}
public struct TMD_NORMAL
{
public short nx; // x value of the normal
public short ny; // y value of the normal
public short nz; // z value of the normal
public short pad; // ignore
}
public struct TMD_PRIMITIVE_PACKET
{
public TMD_3_FS_FP tmd3fsfp;
public TMD_3_GS_FP tmd3gsfp;
public TMD_3_FS_GP tmd3fsgp;
public TMD_3_GS_GP tmd3gsgp;
public TMD_3_TX_FS_NP tmd3txfsnp;
public TMD_3_TX_GS_NP tmd3txgsnp;
public TMD_3_NS_FP tmd3nsfp;
public TMD_3_NS_GP tmd3nsgp;
public TMD_3_TX_NS_FP tmd3txnsfp;
public TMD_3_TX_NS_GP tmd3txnsgp;
}
public struct TMD_OBJECT
{
public int offsetVerts; // start address of vertex list
public int nVerts; // number of vertices in the object
public int offsetNormals; // start address of normal list
public int nNormals; // number of normals in the object
public int offsetPrimitives; // start address of primitive list
public int nPrimitives; // number of primitives in the object
public int scale; // Ignored. For FF7 normally 0.
public TMD_PRIMITIVE_HEADER[] TMDPrimitiveList;
public TMD_PRIMITIVE_PACKET[] TMDPrimitiveListPacket;
public TMD_VERTEX[] TMDVertexList;
public TMD_NORMAL[] TMDNormalList;
}
public struct TMDModel
{
public TMD_HEADER TMDHeader;
public TMD_OBJECT[] TMDObjectList;
}
public static bool bConverted2Float;
public static void LoadTMDModel(ref TMDModel mTMDModel, string strTMDFolder, string strTMDFileName)
{
byte[] fileBuffer;
long fileBufferPos = 0, fileBufferPosItems = 0, iCountObj, iCountItem;
string strTMDFullFileName = strTMDFolder + "\\" + strTMDFileName;
// Let's read TMD file into memory.
// First check if exists
if (!File.Exists(strTMDFullFileName))
{
// Debug.Print fileName
throw new FileNotFoundException("Error opening .TMD Model " + strTMDFileName + " file.");
}
// Read raw data of TMD Model file into memory
fileBuffer = File.ReadAllBytes(strTMDFullFileName);
//// Read TMD Model structure.
// Header
mTMDModel.TMDHeader = new TMD_HEADER();
ReadTMDHeader(fileBuffer, ref fileBufferPos, ref mTMDModel.TMDHeader, strTMDFullFileName);
// Objects
mTMDModel.TMDObjectList = new TMD_OBJECT[mTMDModel.TMDHeader.nObjects];
for (iCountObj = 0; iCountObj < mTMDModel.TMDHeader.nObjects; iCountObj++)
{
// Read Object
ReadTMDObject(fileBuffer, ref fileBufferPos, ref mTMDModel.TMDObjectList[iCountObj]);
// Read Object Data
// Read Primitives
mTMDModel.TMDObjectList[iCountObj].TMDPrimitiveList =
new TMD_PRIMITIVE_HEADER[mTMDModel.TMDObjectList[iCountObj].nPrimitives];
mTMDModel.TMDObjectList[iCountObj].TMDPrimitiveListPacket =
new TMD_PRIMITIVE_PACKET[mTMDModel.TMDObjectList[iCountObj].nPrimitives];
fileBufferPosItems = mTMDModel.TMDObjectList[iCountObj].offsetPrimitives + TMD_PADDING;
for (iCountItem = 0; iCountItem < mTMDModel.TMDObjectList[iCountObj].nPrimitives; iCountItem++)
{
// Read Primitive
ReadTMDPrimitive(fileBuffer, ref fileBufferPosItems,
ref mTMDModel.TMDObjectList[iCountObj].TMDPrimitiveList[iCountItem]);
// Read Primitive Packet
ReadTMDPrimitivePacket(fileBuffer, ref fileBufferPosItems,
mTMDModel.TMDObjectList[iCountObj].TMDPrimitiveList[iCountItem].mode,
ref mTMDModel.TMDObjectList[iCountObj].TMDPrimitiveListPacket[iCountItem]);
}
// Read Vertices
mTMDModel.TMDObjectList[iCountObj].TMDVertexList =
new TMD_VERTEX[mTMDModel.TMDObjectList[iCountObj].nVerts];
fileBufferPosItems = mTMDModel.TMDObjectList[iCountObj].offsetVerts + TMD_PADDING;
ReadTMDVertices(fileBuffer, fileBufferPosItems, mTMDModel.TMDObjectList[iCountObj].nVerts,
ref mTMDModel.TMDObjectList[iCountObj].TMDVertexList);
// Read Normals
mTMDModel.TMDObjectList[iCountObj].TMDNormalList =
new TMD_NORMAL[mTMDModel.TMDObjectList[iCountObj].nNormals];
fileBufferPosItems = mTMDModel.TMDObjectList[iCountObj].offsetNormals + TMD_PADDING;
ReadTMDNormals(fileBuffer, fileBufferPosItems, mTMDModel.TMDObjectList[iCountObj].nNormals,
ref mTMDModel.TMDObjectList[iCountObj].TMDNormalList);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Load TMD functions
public static int ReadTMDHeader(byte[] fileBuffer, ref long pos, ref TMD_HEADER TMDHeader, string fileName)
{
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
TMDHeader.version = memReader.ReadInt32();
// Let's use for our own purposes the version variable:
// 0x41 - vanilla model (with vertex coordinates as shorts)
// 0xFF - converted 2 float model (with vertex coordinates as floats)
if (TMDHeader.version != 0x41 && TMDHeader.version != 0xFF)
{
MessageBox.Show("The file header of the TMD file " + fileName + " is not correct.",
"Error");
return 0;
}
if (TMDHeader.version == 0xFF) bConverted2Float = true;
else bConverted2Float = false;
TMDHeader.flags = memReader.ReadInt32();
TMDHeader.nObjects = memReader.ReadInt32();
pos = memReader.BaseStream.Position;
}
}
return 1;
}
public static void ReadTMDObject(byte[] fileBuffer, ref long pos, ref TMD_OBJECT TMDObject)
{
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
TMDObject.offsetVerts = memReader.ReadInt32();
TMDObject.nVerts = memReader.ReadInt32();
TMDObject.offsetNormals = memReader.ReadInt32();
TMDObject.nNormals = memReader.ReadInt32();
TMDObject.offsetPrimitives = memReader.ReadInt32();
TMDObject.nPrimitives = memReader.ReadInt32();
TMDObject.scale = memReader.ReadInt32();
pos = memReader.BaseStream.Position;
}
}
}
public static void ReadTMDPrimitive(byte[] fileBuffer, ref long pos,
ref TMD_PRIMITIVE_HEADER TMDPrimitive)
{
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
TMDPrimitive.olen = memReader.ReadByte();
TMDPrimitive.ilen = memReader.ReadByte();
TMDPrimitive.flag = memReader.ReadByte();
TMDPrimitive.mode = memReader.ReadByte();
pos = memReader.BaseStream.Position;
}
}
}
public static void ReadTMDPrimitivePacket(byte[] fileBuffer, ref long pos, int mode,
ref TMD_PRIMITIVE_PACKET TMDPrimitivePacket)
{
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
switch (mode)
{
case 0x25:
TMDPrimitivePacket.tmd3txnsfp = new TMD_3_TX_NS_FP()
{
U0 = memReader.ReadByte(),
V0 = memReader.ReadByte(),
CBA = memReader.ReadUInt16(),
U1 = memReader.ReadByte(),
V1 = memReader.ReadByte(),
TSB = memReader.ReadUInt16(),
U2 = memReader.ReadByte(),
V2 = memReader.ReadByte(),
pad1 = memReader.ReadUInt16(),
R = memReader.ReadByte(),
G = memReader.ReadByte(),
B = memReader.ReadByte(),
pad2 = memReader.ReadByte(),
Vertex0 = memReader.ReadUInt16(),
Vertex1 = memReader.ReadUInt16(),
Vertex2 = memReader.ReadUInt16(),
pad = memReader.ReadUInt16(),
};
break;
case 0x31:
TMDPrimitivePacket.tmd3nsgp = new TMD_3_NS_GP()
{
R0 = memReader.ReadByte(),
G0 = memReader.ReadByte(),
B0 = memReader.ReadByte(),
mode2 = memReader.ReadByte(),
R1 = memReader.ReadByte(),
G1 = memReader.ReadByte(),
B1 = memReader.ReadByte(),
pad1 = memReader.ReadByte(),
R2 = memReader.ReadByte(),
G2 = memReader.ReadByte(),
B2 = memReader.ReadByte(),
pad2 = memReader.ReadByte(),
Vertex0 = memReader.ReadUInt16(),
Vertex1 = memReader.ReadUInt16(),
Vertex2 = memReader.ReadUInt16(),
pad = memReader.ReadUInt16(),
};
break;
default:
break;
}
pos = memReader.BaseStream.Position;
}
}
}
public static void ReadTMDVertices(byte[] fileBuffer, long pos, int numVerts,
ref TMD_VERTEX[] TMDVertexList)
{
int i;
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
if (bConverted2Float)
{
for (i = 0; i < numVerts; i++)
{
TMDVertexList[i].fvx = memReader.ReadSingle();
TMDVertexList[i].fvy = memReader.ReadSingle();
TMDVertexList[i].fvz = memReader.ReadSingle();
TMDVertexList[i].fpad = memReader.ReadSingle();
TMDVertexList[i].vx = (short)TMDVertexList[i].fvx;
TMDVertexList[i].vy = (short)TMDVertexList[i].fvy;
TMDVertexList[i].vz = (short)TMDVertexList[i].fvz;
TMDVertexList[i].pad = (short)TMDVertexList[i].fpad;
}
}
else
{
for (i = 0; i < numVerts; i++)
{
TMDVertexList[i].vx = memReader.ReadInt16();
TMDVertexList[i].vy = memReader.ReadInt16();
TMDVertexList[i].vz = memReader.ReadInt16();
TMDVertexList[i].pad = memReader.ReadInt16();
TMDVertexList[i].fvx = (float)Math.Round((double)TMDVertexList[i].vx, 6);
TMDVertexList[i].fvy = (float)Math.Round((double)TMDVertexList[i].vy, 6);
TMDVertexList[i].fvz = (float)Math.Round((double)TMDVertexList[i].vz, 6);
TMDVertexList[i].fpad = (float)Math.Round((double)TMDVertexList[i].pad, 6);
}
}
}
}
}
public static void ReadTMDNormals(byte[] fileBuffer, long pos, int numNormals,
ref TMD_NORMAL[] TMDNormalList)
{
int i;
if (numNormals == 0) return;
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
for (i = 0; i < numNormals; i++)
{
TMDNormalList[i].nx = memReader.ReadInt16();
TMDNormalList[i].ny = memReader.ReadInt16();
TMDNormalList[i].nz = memReader.ReadInt16();
TMDNormalList[i].pad = memReader.ReadInt16();
}
}
}
}
// -------------------------------------------------------------------------------------------------
// ===================================== CONVERSION FUNCTIONS ======================================
// -------------------------------------------------------------------------------------------------
private static int FindVertexIdxByColorVArray(byte inR, byte inG, byte inB, Color[] vcolorsV,
short inX, short inY, short inZ,
Point3D[] vertsV, int mode)
{
int iVertexIdx = -1, iCountColor;
bool bFound = false;
// We have to find the vertex + RGB Index
iCountColor = 0;
while (iCountColor < vcolorsV.Length && !bFound)
{
switch (mode)
{
case 0x31:
if (inR == vcolorsV[iCountColor].R &&
inG == vcolorsV[iCountColor].G &&
inB == vcolorsV[iCountColor].B &&
vertsV[iCountColor].x == inX &&
vertsV[iCountColor].y == inY &&
vertsV[iCountColor].z == inZ)
{
bFound = true;
iVertexIdx = iCountColor;
}
else iCountColor++;
break;
default:
break;
}
}
return iVertexIdx;
}
private static int FindVertexIdxByColorVArrayf(byte inR, byte inG, byte inB, Color[] vcolorsV,
float inX, float inY, float inZ,
Point3D[] vertsV, int mode)
{
int iVertexIdx = -1, iCountColor;
bool bFound = false;
// We have to find the vertex + RGB Index
iCountColor = 0;
while (iCountColor < vcolorsV.Length && !bFound)
{
switch (mode)
{
case 0x31:
if (inR == vcolorsV[iCountColor].R &&
inG == vcolorsV[iCountColor].G &&
inB == vcolorsV[iCountColor].B &&
vertsV[iCountColor].x == inX &&
vertsV[iCountColor].y == inY &&
vertsV[iCountColor].z == inZ)
{
bFound = true;
iVertexIdx = iCountColor;
}
else iCountColor++;
break;
default:
break;
}
}
return iVertexIdx;
}
public static bool TMDHasTextureUVs(TMD_PRIMITIVE_HEADER inPrimHdr)
{
bool bHasTexture = false;
switch (inPrimHdr.mode)
{
case 0x25:
bHasTexture = true;
break;
}
return bHasTexture;
}
private static void PopulatePModel(TMD_VERTEX[] TMDVertices,
TMD_PRIMITIVE_HEADER[] TMDPrimitiveHeaders,
TMD_PRIMITIVE_PACKET[] TMDPrimitivePackets,
out Point3D[] vertsV, out PPolygon[] facesV,
out Color[] vcolorsV, out Color[] pcolorsV,
out Point2D[] TexCoords)
{
int iPolyIdx, iVertIdx, iVColorIdx, iFoundColorVArray;
ushort usVertex;
// Let's populate vertices
vertsV = new Point3D[TMDVertices.Length];
vcolorsV = new Color[TMDVertices.Length];
if (bConverted2Float)
{
for (iVertIdx = 0; iVertIdx < TMDVertices.Length; iVertIdx++)
{
vertsV[iVertIdx].x = TMDVertices[iVertIdx].fvx;
vertsV[iVertIdx].y = TMDVertices[iVertIdx].fvy;
vertsV[iVertIdx].z = TMDVertices[iVertIdx].fvz;
}
}
else
{
for (iVertIdx = 0; iVertIdx < TMDVertices.Length; iVertIdx++)
{
vertsV[iVertIdx].x = TMDVertices[iVertIdx].vx;
vertsV[iVertIdx].y = TMDVertices[iVertIdx].vy;
vertsV[iVertIdx].z = TMDVertices[iVertIdx].vz;
}
}
for (iVColorIdx = 0; iVColorIdx < TMDVertices.Length; iVColorIdx++)
vcolorsV[iVColorIdx] = Color.FromArgb(128, 255, 255, 255);
// Let's populate faces
facesV = new PPolygon[TMDPrimitiveHeaders.Length];
pcolorsV = new Color[TMDPrimitiveHeaders.Length];
// Let's prepare for TextureCoordinates if it has any
if (TMDHasTextureUVs(TMDPrimitiveHeaders[0]))
{
TexCoords = new Point2D[TMDVertices.Length];
}
else TexCoords = null;
for (iPolyIdx = 0; iPolyIdx < TMDPrimitiveHeaders.Length; iPolyIdx++)
{
facesV[iPolyIdx].tag1 = 0;
facesV[iPolyIdx].tag2 = PPOLY_TAG2;
// Prepare PPolygon
facesV[iPolyIdx].Verts = new ushort[3];
facesV[iPolyIdx].Normals = new ushort[3];
facesV[iPolyIdx].Edges = new ushort[3];
switch (TMDPrimitiveHeaders[iPolyIdx].mode)
{
case 0x25: // Texturized
facesV[iPolyIdx].Verts[0] =
(ushort)TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.Vertex0;
facesV[iPolyIdx].Verts[1] =
(ushort)TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.Vertex1;
facesV[iPolyIdx].Verts[2] =
(ushort)TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.Vertex2;
pcolorsV[iPolyIdx] = Color.FromArgb(255,
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.R,
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.G,
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.B);
vcolorsV[TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.Vertex0] =
Color.FromArgb(255,
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.R,
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.G,
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.B);
vcolorsV[TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.Vertex1] =
Color.FromArgb(255,
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.R,
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.G,
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.B);
vcolorsV[TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.Vertex2] =
Color.FromArgb(255,
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.R,
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.G,
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.B);
TexCoords[TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.Vertex0].x =
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.U0 / 64f;
TexCoords[TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.Vertex0].y =
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.V0 / 64f;
TexCoords[TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.Vertex1].x =
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.U1 / 64f;
TexCoords[TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.Vertex1].y =
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.V1 / 64f;
TexCoords[TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.Vertex2].x =
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.U2 / 64f;
TexCoords[TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.Vertex2].y =
TMDPrimitivePackets[iPolyIdx].tmd3txnsfp.V2 / 64f;
break;
case 0x31: // Vertex colored
// Vertex 0
// Check if exists the RGB color in vcolorsV array
if (vcolorsV[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].A == 128)
{
usVertex = TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0;
vcolorsV[usVertex] = Color.FromArgb(255,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.R0,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.G0,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.B0);
}
else
{
if (vcolorsV[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].R ==
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.R0 &&
vcolorsV[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].G ==
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.G0 &&
vcolorsV[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].B ==
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.B0)
{
usVertex = TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0;
}
else
{
if (bConverted2Float)
{
iFoundColorVArray =
FindVertexIdxByColorVArrayf(TMDPrimitivePackets[iPolyIdx].tmd3nsgp.R0,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.G0,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.B0,
vcolorsV,
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].fvx,
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].fvy,
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].fvz,
vertsV,
TMDPrimitiveHeaders[iPolyIdx].mode);
}
else
{
iFoundColorVArray =
FindVertexIdxByColorVArray(TMDPrimitivePackets[iPolyIdx].tmd3nsgp.R0,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.G0,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.B0,
vcolorsV,
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].vx,
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].vy,
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].vz,
vertsV,
TMDPrimitiveHeaders[iPolyIdx].mode);
}
if (iFoundColorVArray == -1)
{
// We need to add a new vertex/colorV and reassign the poly vertex index of the poly.
usVertex = (ushort)vertsV.Length;
// Add vertex to P Model vertex array
Array.Resize(ref vertsV, vertsV.Length + 1);
if (bConverted2Float)
{
vertsV[vertsV.Length - 1].x =
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].fvx;
vertsV[vertsV.Length - 1].y =
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].fvy;
vertsV[vertsV.Length - 1].z =
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].fvz;
}
else
{
vertsV[vertsV.Length - 1].x =
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].vx;
vertsV[vertsV.Length - 1].y =
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].vy;
vertsV[vertsV.Length - 1].z =
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex0].vz;
}
// Add color to P Model vertices color array
Array.Resize(ref vcolorsV, vcolorsV.Length + 1);
vcolorsV[vcolorsV.Length - 1] = Color.FromArgb(255,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.R0,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.G0,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.B0);
}
else
{
usVertex = (ushort)iFoundColorVArray;
}
}
}
facesV[iPolyIdx].Verts[0] = usVertex;
// Vertex 1
// Check if exists the RGB color in vcolorsV array
if (vcolorsV[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex1].A == 128)
{
usVertex = TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex1;
vcolorsV[usVertex] = Color.FromArgb(255,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.R1,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.G1,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.B1);
}
else
{
if (vcolorsV[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex1].R ==
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.R1 &&
vcolorsV[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex1].G ==
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.G1 &&
vcolorsV[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex1].B ==
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.B1)
{
usVertex = TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex1;
}
else
{
if (bConverted2Float)
{
iFoundColorVArray =
FindVertexIdxByColorVArrayf(TMDPrimitivePackets[iPolyIdx].tmd3nsgp.R0,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.G0,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.B0,
vcolorsV,
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex1].fvx,
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex1].fvy,
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex1].fvz,
vertsV,
TMDPrimitiveHeaders[iPolyIdx].mode);
}
else
{
iFoundColorVArray =
FindVertexIdxByColorVArray(TMDPrimitivePackets[iPolyIdx].tmd3nsgp.R0,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.G0,
TMDPrimitivePackets[iPolyIdx].tmd3nsgp.B0,
vcolorsV,
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex1].vx,
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex1].vy,
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex1].vz,
vertsV,
TMDPrimitiveHeaders[iPolyIdx].mode);
}
if (iFoundColorVArray == -1)
{
// We need to add a new vertex/colorV and reassign the poly vertex index of the poly.
usVertex = (ushort)vertsV.Length;
// Add vertex to P Model vertex array
Array.Resize(ref vertsV, vertsV.Length + 1);
if (bConverted2Float)
{
vertsV[vertsV.Length - 1].x =
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex1].fvx;
vertsV[vertsV.Length - 1].y =
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex1].fvy;
vertsV[vertsV.Length - 1].z =
TMDVertices[TMDPrimitivePackets[iPolyIdx].tmd3nsgp.Vertex1].fvz;
}
else
{