-
Notifications
You must be signed in to change notification settings - Fork 2
/
FF7PModel.cs
4873 lines (3991 loc) · 205 KB
/
FF7PModel.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
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;
namespace KimeraCS
{
using Defines;
using static FrmPEditor;
using static FF7Skeleton;
using static ModelDrawing;
using static Utils;
using static OpenGL32;
using static FileTools;
public class FF7PModel
{
public const int PPOLY_TAG2 = 0xCFCEA00; //217901568
public const int I_COMPUTENORMALS_VERTEXTHRESHOLD = 58;
public struct PHeader
{
public int version;
public int off04;
public int vertexColor;
public int numVerts;
public int numNormals;
public int numXYZ;
public int numTexCs;
public int numNormIdx;
public int numEdges;
public int numPolys;
public int off28;
public int off2C;
public int mirex_h;
public int numGroups;
public int mirex_g;
public int off3C;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public int[] unknown;
}
public struct PEdge
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public ushort[] Verts;
// This is for create a new deep copy of PEdge
// We will use normally the creator like '= new PEdge();' but there are some exceptions
public PEdge(PEdge pedgeIn)
{
Verts = new ushort[pedgeIn.Verts.Length];
pedgeIn.Verts.CopyTo(Verts, 0);
}
}
public struct PPolygon
{
public short tag1;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public ushort[] Verts;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public ushort[] Normals;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public ushort[] Edges;
public int tag2;
// This is for create a new empty PPolygon with arrays defined
public PPolygon(int tag2In)
{
tag1 = 0;
tag2 = tag2In;
Verts = new ushort[3];
Normals = new ushort[3];
Edges = new ushort[3];
}
// This is for create a new deep copy of PPolygon
// We will use normally the creator like '= new PPolygon();' but there are some exceptions
public PPolygon(PPolygon ppolygonIn)
{
tag1 = ppolygonIn.tag1;
tag2 = ppolygonIn.tag2;
Verts = new ushort[ppolygonIn.Verts.Length];
ppolygonIn.Verts.CopyTo(Verts, 0);
Normals = new ushort[ppolygonIn.Normals.Length];
ppolygonIn.Normals.CopyTo(Normals, 0);
Edges = new ushort[ppolygonIn.Edges.Length];
ppolygonIn.Edges.CopyTo(Edges, 0);
}
}
// Masks for field_8 and field_C:
// 0x1: V_WIREFRAME
// 0x2: V_TEXTURE
// 0x4: V_LINEARFILTER
// 0x8: V_PERSPECTIVE
// 0x10: V_TMAPBLEND
// 0x20: V_WRAP_U
// 0x40: V_WRAP_V
// 0x80: V_UNKNOWN80
// 0x100: V_COLORKEY
// 0x200: V_DITHER
// 0x400: V_ALPHABLEND
// 0x800: V_ALPHATEST
// 0x1000: V_ANTIALIAS
// 0x2000: V_CULLFACE
// 0x4000: V_NOCULL
// 0x8000: V_DEPTHTEST
// 0x10000: V_DEPTHMASK
// 0x20000: V_SHADEMODE
// 0x40000: V_SPECULAR
// 0x80000: V_LIGHTSTATE
// 0x100000: V_FOG
// 0x200000: V_TEXADDR
public struct PHundret
{
public int field_0;
public int field_4;
public int field_8; // Render state part value (if it's actually changed)
public int field_C; // Change render state part?
public int texID; // Texture identifier for the corresponding group. For consistency sake should be the same as on the group,
// but this is the one FF7 actually uses.
public int texture_set_ptr; //This should be filled in real time
public int field_18;
public int field_1C;
public int field_20;
public int shademode;
public int lightstate_ambient;
public int field_2C;
public int lightstate_material_ptr;
public int srcblend;
public int destblend;
public int field_3C;
public int alpharef;
public int blend_mode; // 0 - Average, source color / 2 + destination color / 2.
// 1 - Additive, source color + destination color.
// 2 - Subtractive, broken and unused but it should be destination color - source color.
// 3 - Not sure, but it seems broken and is never used.
// 4 - No blending (FF8 only)
public int zSort; // Filled in real time
public int field_4C;
public int field_50;
public int field_54;
public int field_58;
public int vertex_alpha;
public int field_60;
}
public struct PGroup
{
public int polyType; // Specifies the Polygon Type for this Group:
// 1 - nontextured Polygons
// 2 - textured Polygons with normals
// 3 - textured Polygons without normals
public int offsetPoly;
public int numPoly;
public int offsetVert;
public int numVert;
public int offsetEdge;
public int numEdge;
public int off1C;
public int off20;
public int off24;
public int off28;
public int offsetTex;
public int texFlag;
public int texID;
// added attributes
public float repGroupX, repGroupY, repGroupZ;
public float rszGroupX, rszGroupY, rszGroupZ;
public float rotGroupAlpha, rotGroupBeta, rotGroupGamma;
public Quaternion rotationQuaternionGroup;
public int DListNum;
public bool HiddenQ; // Hidden groups aren't rendered and can't be changed _
// save for the basic geometrical transformations(rotation, scaling and panning),
// palletizzed opeartions and group deletion
public int realGID; // We will use this as maintain the real Group position number of the list for
// Remove/Duplicate/Add features. This is the Border DListNum?
}
public struct PBoundingBox
{
public int unknown4bytes; // It seems that there are 4bytes before BoundingBox. This 4 bytes are unknown.
public float max_x;
public float max_y;
public float max_z;
public float min_x;
public float min_y;
public float min_z;
}
public struct PModel
{
public string fileName;
public PHeader Header;
public Point3D[] Verts;
public Point3D[] Normals;
public Point3D[] XYZ;
public Point2D[] TexCoords;
public Color[] Vcolors;
public Color[] Pcolors;
public PEdge[] Edges;
public PPolygon[] Polys;
public PHundret[] Hundrets;
public PGroup[] Groups;
public PBoundingBox BoundingBox;
public int[] NormalIndex;
// added attributes
public float repositionX, repositionY, repositionZ;
public float resizeX, resizeY, resizeZ;
public float rotateAlpha, rotateBeta, rotateGamma;
public Quaternion rotationQuaternion;
public float diameter;
public int DListNum;
}
public static void LoadPModel(ref PModel Model, string strPFolder, string strPFileName, bool bComputeNormals)
{
byte[] fileBuffer;
long fileBufferPos = 0;
string strPFullFileName = strPFolder + "\\" + strPFileName;
// Let's read P file into memory.
// First check if exists
if (!File.Exists(strPFullFileName))
{
// Debug.Print fileName
throw new FileNotFoundException("Error opening .P Model " + strPFileName + " file.");
}
// Read All P Model file into memory
fileBuffer = File.ReadAllBytes(strPFullFileName);
//// Read P Model structure.
// Put name of P file
Model.fileName = strPFileName.ToUpper();
// Header
Model.Header = new PHeader();
ReadPHeader(fileBuffer, ref fileBufferPos, ref Model.Header, strPFullFileName);
// Check numVerts
if (Model.Header.numVerts <= 0)
{
throw new ApplicationException("The number of vertices of the P Model: " + Model.fileName +
" is not correct.");
}
// Verts
Model.Verts = new Point3D[Model.Header.numVerts];
ReadPVerts(fileBuffer, ref fileBufferPos, Model.Header.numVerts, ref Model.Verts);
// Normals
Model.Normals = new Point3D[Model.Header.numNormals];
ReadPNormals(fileBuffer, ref fileBufferPos, Model.Header.numNormals, ref Model.Normals);
// TryVerts
if (Model.Header.numXYZ > 0)
{
Model.XYZ = new Point3D[Model.Header.numXYZ];
ReadPXYZ(fileBuffer, ref fileBufferPos, Model.Header.numXYZ, ref Model.XYZ);
}
//else
//{
// Model.Header.numXYZ = 200;
// Model.XYZ = new Point3D[Model.Header.numXYZ];
//}
// Texture Coordinates
Model.TexCoords = new Point2D[Model.Header.numTexCs];
ReadPTexCoords(fileBuffer, ref fileBufferPos, Model.Header.numTexCs, ref Model.TexCoords);
// Vertex Colors
Model.Vcolors = new Color[Model.Header.numVerts];
ReadPPVColors(fileBuffer, ref fileBufferPos, Model.Header.numVerts, ref Model.Vcolors);
// Polygon Colors
Model.Pcolors = new Color[Model.Header.numPolys];
ReadPPVColors(fileBuffer, ref fileBufferPos, Model.Header.numPolys, ref Model.Pcolors);
// Edges
Model.Edges = new PEdge[Model.Header.numEdges];
ReadPEdges(fileBuffer, ref fileBufferPos, Model.Header.numEdges, ref Model.Edges);
// Polygons
Model.Polys = new PPolygon[Model.Header.numPolys];
ReadPPolygons(fileBuffer, ref fileBufferPos, Model.Header.numPolys, ref Model.Polys);
// Hundrets
Model.Hundrets = new PHundret[Model.Header.mirex_h];
ReadPHundrets(fileBuffer, ref fileBufferPos, Model.Header.mirex_h, ref Model.Hundrets);
// Groups
Model.Groups = new PGroup[Model.Header.numGroups];
ReadPGroups(fileBuffer, ref fileBufferPos, Model.Header.numGroups, ref Model.Groups);
// BoundingBox
Model.BoundingBox = new PBoundingBox();
ReadPBoundingBox(fileBuffer, ref fileBufferPos, ref Model.BoundingBox);
// NormalIndex
Model.NormalIndex = new int[Model.Header.numNormIdx];
ReadPNormalIndex(fileBuffer, ref fileBufferPos, Model.Header.numNormIdx, ref Model.NormalIndex);
// added attributes
Model.resizeX = 1;
Model.resizeY = 1;
Model.resizeZ = 1;
Model.rotateAlpha = 0;
Model.rotateBeta = 0;
Model.rotateGamma = 0;
Model.rotationQuaternion.x = 0;
Model.rotationQuaternion.y = 0;
Model.rotationQuaternion.z = 0;
Model.rotationQuaternion.w = 1;
Model.repositionX = 0;
Model.repositionY = 0;
Model.repositionZ = 0;
Model.diameter = ComputeDiameter(Model.BoundingBox);
Model.DListNum = 0;
RepairGroups(ref Model);
AssignRealGID(ref Model);
//CheckModelConsistency(ref Model);
if (!bDontCheckRepairPolys) RepairPolys(ref Model);
KillUnusedVertices(ref Model);
ComputeBoundingBox(ref Model);
if (bComputeNormals)
{
ComputeEdges(ref Model);
ComputeNormals(ref Model);
}
CreateDListsFromPModel(ref Model);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Load Model functions
public static int ReadPHeader(byte[] fileBuffer, ref long pos, ref PHeader Header, string fileName)
{
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
Header.version = memReader.ReadInt32();
Header.off04 = memReader.ReadInt32();
if (Header.version != 1 || Header.off04 != 1)
{
MessageBox.Show("The file header of the P file " + fileName + " is not correct.",
"Error");
return 0;
}
Header.vertexColor = memReader.ReadInt32();
Header.numVerts = memReader.ReadInt32();
Header.numNormals = memReader.ReadInt32();
Header.numXYZ = memReader.ReadInt32();
Header.numTexCs = memReader.ReadInt32();
Header.numNormIdx = memReader.ReadInt32();
Header.numEdges = memReader.ReadInt32();
Header.numPolys = memReader.ReadInt32();
Header.off28 = memReader.ReadInt32();
Header.off2C = memReader.ReadInt32();
Header.mirex_h = memReader.ReadInt32();
Header.numGroups = memReader.ReadInt32();
Header.mirex_g = memReader.ReadInt32();
Header.off3C = memReader.ReadInt32();
Header.unknown = new int[16];
for (var i = 0; i < 16; i++) Header.unknown[i] = memReader.ReadInt32();
pos = memReader.BaseStream.Position;
}
}
return 1;
}
public static void ReadPVerts(byte[] fileBuffer, ref long pos, long numVerts, ref Point3D[] Verts)
{
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
for (var i = 0; i < numVerts; i++)
{
Verts[i].x = memReader.ReadSingle();
Verts[i].y = memReader.ReadSingle();
Verts[i].z = memReader.ReadSingle();
}
pos = memReader.BaseStream.Position;
}
}
}
public static void ReadPNormals(byte[] fileBuffer, ref long pos, long numNormals, ref Point3D[] Normals)
{
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
if (numNormals > 0)
{
for (var i = 0; i < numNormals; i++)
{
Normals[i].x = memReader.ReadSingle();
Normals[i].y = memReader.ReadSingle();
Normals[i].z = memReader.ReadSingle();
}
}
pos = memReader.BaseStream.Position;
}
}
}
public static void ReadPXYZ(byte[] fileBuffer, ref long pos, long numTryVerts, ref Point3D[] TryVerts)
{
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
for (var i = 0; i < numTryVerts; i++)
{
TryVerts[i].x = memReader.ReadSingle();
TryVerts[i].y = memReader.ReadSingle();
TryVerts[i].z = memReader.ReadSingle();
}
pos = memReader.BaseStream.Position;
}
}
}
public static void ReadPTexCoords(byte[] fileBuffer, ref long pos, long numTexCs, ref Point2D[] TexCoordinates)
{
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
if (numTexCs > 0)
{
for (var i = 0; i < numTexCs; i++)
{
TexCoordinates[i].x = memReader.ReadSingle();
TexCoordinates[i].y = memReader.ReadSingle();
}
pos = memReader.BaseStream.Position;
}
}
}
}
public static void ReadPPVColors(byte[] fileBuffer, ref long pos, long numVerts, ref Color[] Vcolors)
{
byte red, green, blue, alpha;
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
for (var i = 0; i < numVerts; i++)
{
blue = memReader.ReadByte();
green = memReader.ReadByte();
red = memReader.ReadByte();
alpha = memReader.ReadByte();
Vcolors[i] = Color.FromArgb(alpha, red, green, blue);
}
pos = memReader.BaseStream.Position;
}
}
}
public static void ReadPEdges(byte[] fileBuffer, ref long pos, long numEdges, ref PEdge[] Edges)
{
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
for (var i = 0; i < numEdges; i++)
{
Edges[i].Verts = new ushort[2];
Edges[i].Verts[0] = memReader.ReadUInt16();
Edges[i].Verts[1] = memReader.ReadUInt16();
}
pos = memReader.BaseStream.Position;
}
}
}
public static void ReadPPolygons(byte[] fileBuffer, ref long pos, long numPolys, ref PPolygon[] Polys)
{
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
for (var i = 0; i < numPolys; i++)
{
Polys[i].tag1 = memReader.ReadInt16();
Polys[i].Verts = new ushort[3];
Polys[i].Verts[0] = memReader.ReadUInt16();
Polys[i].Verts[1] = memReader.ReadUInt16();
Polys[i].Verts[2] = memReader.ReadUInt16();
Polys[i].Normals = new ushort[3];
Polys[i].Normals[0] = memReader.ReadUInt16();
Polys[i].Normals[1] = memReader.ReadUInt16();
Polys[i].Normals[2] = memReader.ReadUInt16();
Polys[i].Edges = new ushort[3];
Polys[i].Edges[0] = memReader.ReadUInt16();
Polys[i].Edges[1] = memReader.ReadUInt16();
Polys[i].Edges[2] = memReader.ReadUInt16();
Polys[i].tag2 = memReader.ReadInt32();
}
pos = memReader.BaseStream.Position;
}
}
}
public static void ReadPHundrets(byte[] fileBuffer, ref long pos, long numHundrets, ref PHundret[] Hundrets)
{
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
for (var i = 0; i < numHundrets; i++)
{
Hundrets[i].field_0 = memReader.ReadInt32();
Hundrets[i].field_4 = memReader.ReadInt32();
Hundrets[i].field_8 = memReader.ReadInt32();
Hundrets[i].field_C = memReader.ReadInt32();
Hundrets[i].texID = memReader.ReadInt32();
Hundrets[i].texture_set_ptr = memReader.ReadInt32();
Hundrets[i].field_18 = memReader.ReadInt32();
Hundrets[i].field_1C = memReader.ReadInt32();
Hundrets[i].field_20 = memReader.ReadInt32();
Hundrets[i].shademode = memReader.ReadInt32();
Hundrets[i].lightstate_ambient = memReader.ReadInt32();
Hundrets[i].field_2C = memReader.ReadInt32();
Hundrets[i].lightstate_material_ptr = memReader.ReadInt32();
Hundrets[i].srcblend = memReader.ReadInt32();
Hundrets[i].destblend = memReader.ReadInt32();
Hundrets[i].field_3C = memReader.ReadInt32();
Hundrets[i].alpharef = memReader.ReadInt32();
Hundrets[i].blend_mode = memReader.ReadInt32();
Hundrets[i].zSort = memReader.ReadInt32();
Hundrets[i].field_4C = memReader.ReadInt32();
Hundrets[i].field_50 = memReader.ReadInt32();
Hundrets[i].field_54 = memReader.ReadInt32();
Hundrets[i].field_58 = memReader.ReadInt32();
Hundrets[i].vertex_alpha = memReader.ReadInt32();
Hundrets[i].field_60 = memReader.ReadInt32();
}
pos = memReader.BaseStream.Position;
}
}
}
public static void ReadPGroups(byte[] fileBuffer, ref long pos, long numGroups, ref PGroup[] Groups)
{
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
for (var i = 0; i < numGroups; i++)
{
Groups[i].polyType = memReader.ReadInt32();
Groups[i].offsetPoly = memReader.ReadInt32();
Groups[i].numPoly = memReader.ReadInt32();
Groups[i].offsetVert = memReader.ReadInt32();
Groups[i].numVert = memReader.ReadInt32();
Groups[i].offsetEdge = memReader.ReadInt32();
Groups[i].numEdge = memReader.ReadInt32();
Groups[i].off1C = memReader.ReadInt32();
Groups[i].off20 = memReader.ReadInt32();
Groups[i].off24 = memReader.ReadInt32();
Groups[i].off28 = memReader.ReadInt32();
Groups[i].offsetTex = memReader.ReadInt32();
Groups[i].texFlag = memReader.ReadInt32();
Groups[i].texID = memReader.ReadInt32();
// added attributes
Groups[i].rszGroupX = 1;
Groups[i].rszGroupY = 1;
Groups[i].rszGroupZ = 1;
Groups[i].repGroupX = 0;
Groups[i].repGroupY = 0;
Groups[i].repGroupZ = 0;
Groups[i].rotGroupAlpha = 0;
Groups[i].rotGroupBeta = 0;
Groups[i].rotGroupGamma = 0;
Groups[i].rotationQuaternionGroup = new Quaternion() { x = 0, y = 0, z = 0, w = 1 };
Groups[i].DListNum = -1;
Groups[i].HiddenQ = false;
Groups[i].realGID = i;
}
pos = memReader.BaseStream.Position;
}
}
}
public static void ReadPBoundingBox(byte[] fileBuffer, ref long pos, ref PBoundingBox BoundingBox)
{
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
// There are .P models, like magic/bari_a1 and magic/bari_a2 that
// does not seem to have this unknown4bytes.
if (memReader.BaseStream.Length - memReader.BaseStream.Position - 24 > 0)
BoundingBox.unknown4bytes = memReader.ReadInt32(); // It seems that there are 4bytes before BoundingBox. This 4 bytes are unknown.
BoundingBox.max_x = memReader.ReadSingle();
BoundingBox.max_y = memReader.ReadSingle();
BoundingBox.max_z = memReader.ReadSingle();
// There are .P models, like magic/bari_a2 that
// does not seem to have min_x.
if (memReader.BaseStream.Length - memReader.BaseStream.Position - 12 > 0)
{
BoundingBox.min_x = memReader.ReadSingle();
BoundingBox.min_y = memReader.ReadSingle();
BoundingBox.min_z = memReader.ReadSingle();
}
pos = memReader.BaseStream.Position;
}
}
}
public static void ReadPNormalIndex(byte[] fileBuffer, ref long pos, int numNormIdx, ref int[] NormalIndex)
{
using (var fileMemory = new MemoryStream(fileBuffer))
{
using (var memReader = new BinaryReader(fileMemory))
{
memReader.BaseStream.Position = pos;
for (var i = 0; (i < numNormIdx && memReader.BaseStream.Position < memReader.BaseStream.Length); i++)
NormalIndex[i] = memReader.ReadInt32();
pos = memReader.BaseStream.Position;
}
}
}
// In this procedure we will check the realGID assigned when loading
// the model is correct (this equals the offsetPoly incremental number order).
public static void AssignRealGID(ref PModel Model)
{
int iGroupIdx, iGroupIdxCheck, iMinOffsetPoly = 0, iMaxOffsetPoly = 999999, iGroupFound = 0, iRealGIDCounter = 0;
if (Model.Header.numGroups > 1)
{
iGroupIdx = 0;
while (iGroupIdx < Model.Header.numGroups)
{
if (iGroupIdx == 0)
{
iGroupIdxCheck = 0;
while (Model.Groups[iGroupIdxCheck].offsetPoly != 0 &&
Model.Groups[iGroupIdxCheck].numPoly > 0) iGroupIdxCheck++;
iGroupFound = iGroupIdxCheck;
}
else
{
iGroupIdxCheck = 0;
while (iGroupIdxCheck < Model.Header.numGroups)
{
if (Model.Groups[iGroupIdxCheck].offsetPoly < iMaxOffsetPoly &&
Model.Groups[iGroupIdxCheck].offsetPoly > iMinOffsetPoly)
{
iMaxOffsetPoly = Model.Groups[iGroupIdxCheck].offsetPoly;
iGroupFound = iGroupIdxCheck;
}
iGroupIdxCheck++;
}
}
Model.Groups[iGroupFound].realGID = iRealGIDCounter;
iMinOffsetPoly = Model.Groups[iGroupFound].offsetPoly;
iMaxOffsetPoly = 99999999;
iRealGIDCounter++;
iGroupIdx++;
}
}
}
// ---------------------------------------------------------------------------------------------------------
// ------------------------------------------ COMBINING/MERGING --------------------------------------------
// ---------------------------------------------------------------------------------------------------------
public static void CombineGroups(ref PGroup[] outMergedGroup, PGroup[] inGroup)
{
int iGroupIdx, maxtiGroup, numGroupsMergedGroup, numGroupsinGroup,
iNumPolys, iNumEdges, iNumVerts, iNumTexCs;
numGroupsMergedGroup = outMergedGroup.Length;
numGroupsinGroup = inGroup.Length;
Array.Resize(ref outMergedGroup, numGroupsMergedGroup + numGroupsinGroup);
maxtiGroup = 0;
for (iGroupIdx = 0; iGroupIdx < numGroupsMergedGroup; iGroupIdx++)
{
if (outMergedGroup[iGroupIdx].texFlag == 1)
if (outMergedGroup[iGroupIdx].texID > maxtiGroup)
maxtiGroup = outMergedGroup[iGroupIdx].texID;
}
iNumPolys = outMergedGroup[numGroupsMergedGroup - 1].offsetPoly +
outMergedGroup[numGroupsMergedGroup - 1].numPoly;
iNumEdges = outMergedGroup[numGroupsMergedGroup - 1].offsetEdge +
outMergedGroup[numGroupsMergedGroup - 1].numEdge;
iNumVerts = outMergedGroup[numGroupsMergedGroup - 1].offsetVert +
outMergedGroup[numGroupsMergedGroup - 1].numVert;
if (outMergedGroup[numGroupsMergedGroup].texFlag == 1)
iNumTexCs = outMergedGroup[numGroupsMergedGroup - 1].offsetTex +
outMergedGroup[numGroupsMergedGroup - 1].numVert;
else
iNumTexCs = outMergedGroup[numGroupsMergedGroup - 1].offsetTex;
for (iGroupIdx = 0; iGroupIdx < numGroupsinGroup; iGroupIdx++)
{
inGroup[iGroupIdx].offsetPoly = inGroup[iGroupIdx].offsetPoly + iNumPolys;
inGroup[iGroupIdx].offsetVert = inGroup[iGroupIdx].offsetVert + iNumVerts;
inGroup[iGroupIdx].offsetEdge = inGroup[iGroupIdx].offsetEdge + iNumEdges;
inGroup[iGroupIdx].offsetTex = inGroup[iGroupIdx].offsetTex + iNumTexCs;
if (inGroup[iGroupIdx].texFlag == 1)
inGroup[iGroupIdx].texID = inGroup[iGroupIdx].texID + maxtiGroup;
outMergedGroup[numGroupsMergedGroup + iGroupIdx] = inGroup[iGroupIdx];
}
}
public static void MergeBoundingBox(ref PBoundingBox BoundingBox, PBoundingBox inBoundingBox)
{
if (BoundingBox.max_x < inBoundingBox.max_x) BoundingBox.max_x = inBoundingBox.max_x;
if (BoundingBox.max_y < inBoundingBox.max_y) BoundingBox.max_y = inBoundingBox.max_y;
if (BoundingBox.max_z < inBoundingBox.max_z) BoundingBox.max_z = inBoundingBox.max_z;
if (BoundingBox.min_x > inBoundingBox.min_x) BoundingBox.min_x = inBoundingBox.min_x;
if (BoundingBox.min_y > inBoundingBox.min_y) BoundingBox.min_y = inBoundingBox.min_y;
if (BoundingBox.min_z > inBoundingBox.min_z) BoundingBox.min_z = inBoundingBox.min_z;
}
public static void MergeHeader(ref PHeader Header, PHeader inHeader)
{
Header.numVerts += inHeader.numVerts;
Header.numNormals += inHeader.numNormals;
Header.numTexCs += inHeader.numTexCs;
Header.numNormIdx += inHeader.numNormIdx;
Header.numEdges += inHeader.numEdges;
Header.numPolys += inHeader.numPolys;
Header.mirex_h += inHeader.mirex_h;
Header.numGroups += inHeader.numGroups;
}
public static void MergePModels(ref PModel Model, PModel inModel)
{
try
{
// Merge Verts
Model.Verts = Model.Verts.Concat(inModel.Verts).ToArray();
// Merge Normals
//Model.Normals = Model.Normals.Concat(inModel.Normals).ToArray();
// Texture Coordinates
if (Model.Header.numTexCs == 0)
Model.TexCoords = inModel.TexCoords;
else
if (inModel.Header.numTexCs > 0)
Model.TexCoords = Model.TexCoords.Concat(inModel.TexCoords).ToArray();
// VColors
Model.Vcolors = Model.Vcolors.Concat(inModel.Vcolors).ToArray();
// PColors
Model.Pcolors = Model.Pcolors.Concat(inModel.Pcolors).ToArray();
// Model.Edges = Model.Edges.Concat(inModel.Edges).ToArray();
// PPolygons
Model.Polys = Model.Polys.Concat(inModel.Polys).ToArray();
// Hundrets
Model.Hundrets = Model.Hundrets.Concat(inModel.Hundrets).ToArray();
// Groups
CombineGroups(ref Model.Groups, inModel.Groups);
// BoundingBoxes
MergeBoundingBox(ref Model.BoundingBox, inModel.BoundingBox);
//// Normal's Indexes
//Model.NormalIndex = Model.NormalIndex.Concat(inModel.NormalIndex).ToArray();
// Headers
MergeHeader(ref Model.Header, inModel.Header);
ComputeNormals(ref Model);
ComputeEdges(ref Model);
CheckModelConsistency(ref Model);
}
catch
{
MessageBox.Show("Error merging " + Model.fileName + " with " + inModel.fileName + "!!!", "Error", MessageBoxButtons.OK);
}
}
public static void MergeGroupsIntoOne(PModel inPModel, out PModel outPModel, bool bIncludeTextures)
{
int iGroupIdx;
// Temporary backup of model
PModel tmpPModel = CopyPModel(inPModel);
outPModel = new PModel()
{
Groups = new PGroup[1],
fileName = inPModel.fileName,
};
// For do the merging without textures will try to do some easy solution
// 1. Erase all the textured groups of the model
// 2. Process the groups of the model (theorically not texturized)
// 3. Add the texturized groups from the temporary saved P Model if we want textured groups
if (!bIncludeTextures)
{
for (iGroupIdx = 0; iGroupIdx < inPModel.Header.numGroups; iGroupIdx++)
{
if (inPModel.Groups[iGroupIdx].texFlag == 1)
{
RemoveGroup(ref inPModel, iGroupIdx);
iGroupIdx--;
}
}
}
// 2. Process the groups of the model
// Header
outPModel.Header.version = inPModel.Header.version;
outPModel.Header.off04 = inPModel.Header.off04;
outPModel.Header.numXYZ = inPModel.Header.numXYZ;
outPModel.Header.off28 = inPModel.Header.off28;
outPModel.Header.off2C = inPModel.Header.off2C;
outPModel.Header.off3C = inPModel.Header.off3C;
outPModel.Header.vertexColor = inPModel.Header.vertexColor;
outPModel.Header.numVerts = inPModel.Header.numVerts;
outPModel.Verts = new Point3D[inPModel.Header.numVerts];
Array.Copy(inPModel.Verts, outPModel.Verts, inPModel.Header.numVerts);
outPModel.Header.numPolys = inPModel.Header.numPolys;
outPModel.Polys = new PPolygon[inPModel.Header.numPolys];
Array.Copy(inPModel.Polys, outPModel.Polys, inPModel.Header.numPolys);
outPModel.Vcolors = new Color[inPModel.Header.numVerts];
Array.Copy(inPModel.Vcolors, outPModel.Vcolors, inPModel.Header.numVerts);
outPModel.Pcolors = new Color[inPModel.Header.numPolys];
Array.Copy(inPModel.Pcolors, outPModel.Pcolors, inPModel.Header.numPolys);
outPModel.Groups[0].polyType = 1;
if (inPModel.Header.numTexCs > 0)
{
// We will avoid Texture Coordinates
outPModel.Header.numTexCs = inPModel.Header.numTexCs;
outPModel.TexCoords = new Point2D[inPModel.Header.numTexCs];
Array.Copy(inPModel.TexCoords, outPModel.TexCoords, inPModel.Header.numTexCs);
outPModel.Groups[0].polyType = 2;
}
outPModel.Hundrets = new PHundret[1];
Array.Copy(inPModel.Hundrets, outPModel.Hundrets, 1);
outPModel.Header.mirex_h = 1;
outPModel.Header.mirex_g = inPModel.Header.mirex_g;
outPModel.Header.unknown = new int[16];
outPModel.Header.numGroups = 1;
// Apply dimensional data
outPModel.Groups[0].repGroupX = inPModel.Groups[0].repGroupX;
outPModel.Groups[0].repGroupY = inPModel.Groups[0].repGroupY;
outPModel.Groups[0].repGroupZ = inPModel.Groups[0].repGroupZ;
outPModel.Groups[0].rszGroupX = inPModel.Groups[0].rszGroupX;
outPModel.Groups[0].rszGroupY = inPModel.Groups[0].rszGroupY;
outPModel.Groups[0].rszGroupZ = inPModel.Groups[0].rszGroupZ;
outPModel.Groups[0].rotGroupAlpha = inPModel.Groups[0].rotGroupAlpha;
outPModel.Groups[0].rotGroupBeta = inPModel.Groups[0].rotGroupBeta;
outPModel.Groups[0].rotGroupGamma = inPModel.Groups[0].rotGroupGamma;
// Apply the group number data of the input model to the output model
outPModel.Groups[0].numVert = inPModel.Header.numVerts;
outPModel.Groups[0].numPoly = inPModel.Header.numPolys;
outPModel.Groups[0].numEdge = inPModel.Header.numEdges;
outPModel.Groups[0].DListNum = 1;
// Recalculate tris with the new vertex/polys/edges/normals indexes
iGroupIdx = GetNextGroup(inPModel, GetNextGroup(inPModel, -1));
while (iGroupIdx != -1)
{
// Polys
for (int iPolyCounter = inPModel.Groups[iGroupIdx].offsetPoly;
iPolyCounter < inPModel.Groups[iGroupIdx].numPoly + inPModel.Groups[iGroupIdx].offsetPoly;
iPolyCounter++)
{
outPModel.Polys[iPolyCounter].Verts[0] += (ushort)inPModel.Groups[iGroupIdx].offsetVert;
outPModel.Polys[iPolyCounter].Verts[1] += (ushort)inPModel.Groups[iGroupIdx].offsetVert;
outPModel.Polys[iPolyCounter].Verts[2] += (ushort)inPModel.Groups[iGroupIdx].offsetVert;
}
iGroupIdx = GetNextGroup(inPModel, iGroupIdx);
}
// 3. Add the texturized groups from the temporary saved P Model if we want textured groups
if (!bIncludeTextures)
{
iGroupIdx = GetNextGroup(tmpPModel, -1);