-
Notifications
You must be signed in to change notification settings - Fork 0
/
uMD3.pas
2047 lines (1880 loc) · 72.7 KB
/
uMD3.pas
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
unit uMD3;
interface
uses classes, StdCtrls, ExtCtrls, u3DTypes, uQ3Shaders;
// http://icculus.org/homepages/phaethon/q3a/formats/md3format.html
const
MAX_QPATH = 64;
MD3_MAX_LODS = 4;
MD3_MAX_FRAMES = 1024; // per model
MD3_MAX_TAGS = 16; // per frame
MD3_MAX_SURFACES = 32; // per model
MD3_MAX_SHADERS = 256; // per surface
MD3_MAX_VERTS = 1024; // per surface //was 4096 but prints a message from ET: "has more than 1025 verts on a surface"
MD3_MAX_TRIANGLES = 8192; // per surface
MD3_XYZ_SCALE = 1.0/64.0;
MD3_XYZ_SCALE_1 = 64.0;
IDP3 = $33504449; // 'IDP3' cardinal
TAG3 = $33474154; // 'TAG3' cardinal
type
q3string = packed array[0..MAX_QPATH-1] of char;
TTagFileHeader = packed record
Ident: cardinal; // == 'TAG3'
Version: cardinal;
Num_Tags: cardinal;
Ofs_End: cardinal;
end;
TVec3 = packed record
X,Y,Z: single;
end;
TShader = packed record
Name: q3string;
Shader_Index: Integer; //intern gebruik
end;
TTriangle = packed record
Index1, //vertex 1 index
Index2, //vertex 2 index
Index3: cardinal; //vertex 3 index
end;
TTextureCoords = packed record // 0.0 .. 1.0
S,
T: single;
end;
TVertex = packed record
X, // X * MD3_XYZ_SCALE == X-coordinaat
Y, // Y * MD3_XYZ_SCALE == Y-coordinaat
Z: SmallInt; // Z * MD3_XYZ_SCALE == Z-coordinaat
Normal: word; // gecodeerde normaal
end;
TSurface1 = packed record
Ident: cardinal; // == 'IDP3'
Name: q3string;
Flags: cardinal;
Num_Frames: cardinal; //het aantal animatie frames
Num_Shaders: cardinal; //het aantal shaders
Num_Verts: cardinal; //het aantal vertexes
Num_Triangles: cardinal;//het aantal triangles
Ofs_Triangles: cardinal;
Ofs_Shaders: cardinal;
Ofs_ST: cardinal;
Ofs_XYZNormal: cardinal;
Ofs_End: cardinal;
end;
TSurface = packed record
Values: TSurface1;
// Shaders array
Shaders: packed array of TShader;
// Triangles array
Triangles: packed array of TTriangle;
// ST texture-coords array
TextureCoords: packed array of TTextureCoords;
// XYZNormals array
Vertex: packed array of TVertex;
end;
TFrame = packed record
Min_Bounds: TVector;
Max_Bounds: TVector;
Local_Origin: TVector;
Radius: single;
Name: array[0..15] of char;
end;
TTag = packed record
Name: q3string;
Origin: TVector;
Axis: packed array[0..2] of TVector;
end;
THeader1 = packed record
Ident: cardinal; // == 'IDP3'
Version: cardinal;
Name: q3string;
Flags: cardinal;
Num_Frames: cardinal; //maximale waarde = MD3_MAX_FRAMES
Num_Tags: cardinal; //maximale waarde = MD3_MAX_TAGS
Num_Surfaces: cardinal; //maximale waarde = MD3_MAX_SURFACES
Num_Skins: cardinal;
Ofs_Frames: cardinal; //offset van 1e frame-object (vanaf start TMD3)
Ofs_Tags: cardinal; //offset van 1e tag-object " "
Ofs_Surfaces: cardinal; //offset van 1e surface-object " "
Ofs_EOF: cardinal; //offset einde van MD3-object " "
end;
THeader = packed record
Values: THeader1;
// frames array
Frames: packed array of TFrame;
// tags array
Tags: packed array of TTag; // Num_Frames * Num_Tags
// surfaces array
Surfaces: packed array of TSurface;
end;
TMD3 = class(TObject)
private
Fopen, Fsave: TFilestream;
function IsMD3 : boolean;
//
function Size_Header : cardinal;
function Size_Frames : cardinal;
function Size_Tags : cardinal;
function Size_Surfaces : cardinal;
function Size_File : cardinal;
function Size_SurfaceHeader : cardinal;
function Size_SurfaceShaders(SurfaceIndex: cardinal) : cardinal;
function Size_SurfaceTriangles(SurfaceIndex: cardinal) : cardinal;
function Size_SurfaceTexCoords(SurfaceIndex: cardinal) : cardinal;
function Size_SurfaceVertex(SurfaceIndex: cardinal) : cardinal;
function Size_Surface(SurfaceIndex: cardinal) : cardinal;
//
function ClosestTriangle(const origin:TVector; var SurfaceIndex:integer; var TriangleIndex:integer) : boolean;
function CalcBaseFromTriangle(const SurfaceIndex, TriangleIndex:integer) : TMatrix4x4;
function CalcTransformMatrixFromBases(const Base1,Base2:TMatrix4x4; const Origin1,Origin2:TVector) : TMatrix4x4;
public
Header : THeader;
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure ClearMemory;
function StringToQ3(const aString: string) : q3string;
procedure DecodeNormal(const Normal: word; var X,Y,Z: single);
procedure EncodeNormal(const Normal:TVector; var Value: word); overload;
procedure EncodeNormal(const X,Y,Z:single; var Value: word); overload;
//
procedure CalcDimensions(FrameNr:integer; var BoundsMin:TVector; var BoundsMax:TVector; var Center:TVector);
//
procedure ChangeShader(Index: Integer; Name: string);
//
procedure AddTags(Filename: string);
procedure AddTag(const aTag: TTag);
procedure SaveTags(Filename: string);
procedure TagsInvertX;
procedure TagsInvertY;
procedure TagsInvertZ;
procedure TagsSwapXY;
procedure TagsSwapXZ;
procedure TagsSwapYZ;
//
procedure FlipXcoords;
procedure FlipYcoords;
procedure FlipZcoords;
procedure FlipNormals;
procedure FlipWinding;
//
procedure RotateModel(const M:TMatrix4x4);
procedure RotateModelX(const Degrees:single);
procedure RotateModelY(const Degrees:single);
procedure RotateModelZ(const Degrees:single);
// swap texture-coördinaten
procedure TexCoords_SwapST_UV(SurfaceIndex: cardinal);
// surface-normals opnieuw berekenen
procedure CalculateSurfaceNormals(SurfaceNr:integer);
// geen echte weld.. vertices blijven aparte indexen behouden
procedure WeldVertices(SurfaceNr:integer; Distance:Single);
// smooth surface
procedure SmoothSurface(SurfaceNr:integer);
// remove a surface
procedure RemoveSurface(SurfaceNr:integer);
// compact as many surfaces (with same shader) into as few as possible
procedure CompactSurfaces;
//
function LoadFromFile(Filename: string) : boolean;
function SaveToFile(Filename: string) : boolean;
//
function AddFrame(Filename:string; var ErrorString:string) : boolean;
function AddFrames(Filename:string; var ErrorString:string) : boolean;
function InsertFrameAt(FrameNr:cardinal; Filename:string) : boolean;
function DeleteFrame(FrameNr: cardinal) : boolean;
//
procedure InitShaders;
procedure FreeShaders;
function IsTextured : boolean;
procedure Render;
end;
var MD3 : TMD3;
(*
normaal-word:
_____________________________________________
|15 14 13 12 11 10 9 8 | 7 6 5 4 3 2 1 0 |
---------------------------------------------
|lat (latitude) | lng (longitude) |
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Decoding: (Code in q3tools/q3map/misc_model.c:InsertMD3Model)
lat := ((normal shr 8) and 255) * (2 * pi ) / 255
lng := (normal and 255) * (2 * pi) / 255
normal.x := cos(lat) * sin(lng)
normal.y := sin(lat) * sin(lng)
normal.z := cos(lng)
Encoding: (Code in q3tools/common/mathlib.c:NormalToLatLong)
lng := atan2(y/x) * 255 / (2 * pi)
lat := acos(z) * 255 / (2 * pi)
lng := lower 8 bits of lng
lat := lower 8 bits of lat
normal := (lat shl 8) or (lng)
Two special vectors are the ones that point up and point down,
as these values for z result in a singularity for acos.
The special case of straight-up is:
normal := 0
And the special case of straight down is:
lat := 0
lng := 128
normal := (lat shl 8) or (lng)
or, shorter:
normal := 32768
*)
//------------------------------------------------------------------------------
implementation
uses SysUtils, uCalc, Math;
constructor TMD3.Create;
begin
ClearMemory;
end;
destructor TMD3.Destroy;
begin
ClearMemory;
inherited;
end;
procedure TMD3.ClearMemory;
var i: integer;
begin
// memory
with Header do begin
SetLength(Frames, 0);
SetLength(Tags, 0);
for i:=0 to Values.Num_Surfaces-1 do
with Surfaces[i] do begin
SetLength(Shaders, 0);
SetLength(Triangles, 0);
SetLength(TextureCoords, 0);
SetLength(Vertex, 0);
end;
SetLength(Surfaces, 0);
with Values do begin
Num_Frames := 0;
Num_Tags := 0;
Num_Surfaces := 0;
Num_Skins := 0;
end;
end;
// textures uit een shader-bestand
for i:=0 to Header.Values.Num_Surfaces-1 do
Shaders.DeleteShader(Header.Surfaces[i].Shaders[0].Shader_Index);
(*
for i:=0 to Length(ShaderFile)-1 do
SetLength(ShaderFile[i].Textures, 0);
SetLength(ShaderFile, 0);
*)
(*
for i:=0 to Length(GameShaders)-1 do
SetLength(GameShaders[i].Shaders, 0);
SetLength(GameShaders, 0);
*)
end;
procedure TMD3.Clear;
begin
ClearMemory;
end;
//------------------------------------------------------------------------------
function TMD3.Size_Header: cardinal;
begin
Result := SizeOf(THeader1);
end;
function TMD3.Size_Frames: cardinal;
begin
Result := Header.Values.Num_Frames * SizeOf(TFrame);
end;
function TMD3.Size_Tags: cardinal;
begin
Result := Header.Values.Num_Frames * Header.Values.Num_Tags * SizeOf(TTag);
end;
function TMD3.Size_Surfaces: cardinal;
var s: integer;
begin
Result := 0;
for s:=0 to Header.Values.Num_Surfaces-1 do Inc(Result, Size_Surface(s));
end;
function TMD3.Size_File: cardinal;
begin
Result := Size_Header + Size_Frames + Size_Tags + Size_Surfaces;
end;
function TMD3.Size_SurfaceHeader: cardinal;
begin
Result := SizeOf(TSurface1);
end;
function TMD3.Size_SurfaceShaders(SurfaceIndex: cardinal): cardinal;
var Size: cardinal;
begin
Result := 0;
if SurfaceIndex < 0 then Exit;
if SurfaceIndex > Header.Values.Num_Surfaces-1 then Exit;
Size := Header.Surfaces[SurfaceIndex].Values.Num_Shaders * SizeOf(TShader);
Inc(Result, Size);
end;
function TMD3.Size_SurfaceTriangles(SurfaceIndex: cardinal): cardinal;
var Size: cardinal;
begin
Result := 0;
if SurfaceIndex < 0 then Exit;
if SurfaceIndex > Header.Values.Num_Surfaces-1 then Exit;
Size := Header.Surfaces[SurfaceIndex].Values.Num_Triangles * SizeOf(TTriangle);
Inc(Result, Size);
end;
function TMD3.Size_SurfaceTexCoords(SurfaceIndex: cardinal): cardinal;
var Size: cardinal;
begin
Result := 0;
if SurfaceIndex < 0 then Exit;
if SurfaceIndex > Header.Values.Num_Surfaces-1 then Exit;
Size := Header.Surfaces[SurfaceIndex].Values.Num_Verts * SizeOf(TTextureCoords);
Inc(Result, Size);
end;
function TMD3.Size_SurfaceVertex(SurfaceIndex: cardinal): cardinal;
var Size: cardinal;
begin
Result := 0;
if SurfaceIndex < 0 then Exit;
if SurfaceIndex > Header.Values.Num_Surfaces-1 then Exit;
Size := Header.Values.Num_Frames * Header.Surfaces[SurfaceIndex].Values.Num_Verts * SizeOf(TVertex);
Inc(Result, Size);
end;
function TMD3.Size_Surface(SurfaceIndex: cardinal): cardinal;
begin
Result := 0;
if SurfaceIndex < 0 then Exit;
if SurfaceIndex > Header.Values.Num_Surfaces-1 then Exit;
Inc(Result, Size_SurfaceHeader);
Inc(Result, Size_SurfaceShaders(SurfaceIndex));
Inc(Result, Size_SurfaceTriangles(SurfaceIndex));
Inc(Result, Size_SurfaceTexCoords(SurfaceIndex));
Inc(Result, Size_SurfaceVertex(SurfaceIndex));
end;
//------------------------------------------------------------------------------
function TMD3.StringToQ3(const aString: string): q3string;
var Len, i: integer;
begin
Len := Length(aString);
if Len > MAX_QPATH then Len := MAX_QPATH;
for i:=0 to Len-1 do Result[i] := aString[1+i];
for i:=Len to MAX_QPATH-1 do Result[i] := chr(0);
end;
procedure TMD3.DecodeNormal(const Normal:word; var X, Y, Z: single);
var lat, lng: single;
begin
//Decoding: (Code in q3tools/q3map/misc_model.c:InsertMD3Model)
case Normal of
0: begin
X := 0.0;
Y := 0.0;
Z := 1.0;
end;
32768: begin
X := 0.0;
Y := 0.0;
Z := -1.0;
end;
else
lat := ((Normal shr 8) and $FF) / 255.0 * (2 * Pi );
lng := (Normal and $FF) / 255.0 * (2 * Pi);
X := cos(lat) * sin(lng);
Y := sin(lat) * sin(lng);
Z := cos(lng);
end;
end;
procedure TMD3.EncodeNormal(const Normal: TVector; var Value: word);
var lng,lat: single;
N: TVector;
begin
N := UnitVector(Normal);
if AlmostSameVector(N, ZAxisVector, 0.01) then
Value := 0
else
if AlmostSameVector(N, NegativeZAxisVector, 0.01) then
Value := 32768
else begin
{ lng := ATan2(N.Y,N.X) * 255.0 / (2 * Pi);
lat := ArcCos(N.Z) * 255.0 / (2 * Pi);
}
lat := uCalc.ATan2(N.Y,N.X) * constRadToDeg / 360*255;
lng := ArcCos(N.Z) * constRadToDeg / 360*255;
(*
x, y, z = normal
lng = math.acos(z)
lat = math.acos(x / math.sin(lng))
retval = ((lat & 0xFF) << 8) | (lng & 0xFF)
*)
{
lng := ArcCos(N.Z)* 255.0 / (2 * Pi);
lat := ArcCos(N.X/Sin(lng))* 255.0 / (2 * Pi);
}
Value := ((Round(lat) and $FF) shl 8) + (Round(lng) and $FF);
end;
end;
procedure TMD3.EncodeNormal(const X, Y, Z: single; var Value: word);
var N: TVector;
begin
N := Vector(X,Y,Z);
EncodeNormal(N, Value);
end;
function TMD3.IsMD3: boolean;
begin
Result := (Header.Values.Ident = IDP3);
end;
procedure TMD3.TexCoords_SwapST_UV(SurfaceIndex: cardinal);
var j: integer;
begin
if SurfaceIndex < 0 then Exit;
if SurfaceIndex > Header.Values.Num_Surfaces-1 then Exit;
for j:=0 to Header.Surfaces[SurfaceIndex].Values.Num_Verts-1 do
Header.Surfaces[SurfaceIndex].TextureCoords[j].T := 1.0 - Header.Surfaces[SurfaceIndex].TextureCoords[j].T;
end;
procedure TMD3.ChangeShader(Index: Integer; Name: string);
var L: integer;
begin
if Header.Values.Num_Surfaces < 1 then Exit;
if (Index < 0) or (Index >= Header.Values.Num_Surfaces) then Exit;
if Header.Surfaces[Index].Values.Num_Shaders < 1 then Exit;
// string overnemen
for L:=1 to Length(Name) do
Header.Surfaces[Index].Shaders[0].Name[L-1] := Name[L];
// opvullen met nullen
for L:=Length(Name)+1 to MAX_QPATH-1 do
Header.Surfaces[Index].Shaders[0].Name[L-1] := chr(0);
end;
//------------------------------------------------------------------------------
function TMD3.LoadFromFile(Filename: string): boolean;
var N, Size: cardinal;
i,j,f: integer;
FP: Int64;
strX,strY,strZ, strV: string;
begin
Result := false;
Clear; //clear model memory
SetLength(SkinShaders, 0); //skins verwijderen
// stream handle
Fopen := TFileStream.Create(Filename, fmOpenRead);
try
try
N := Fopen.Read(Header.Values, Size_Header);
if N<>Size_Header then Exit;
Result := IsMD3;
if not Result then Exit;
// Frames laden
Fopen.Position := Header.Values.Ofs_Frames;
SetLength(Header.Frames, Header.Values.Num_Frames);
Size := Size_Frames;
N := Fopen.Read(Header.Frames[0], Size);
Result := (N=Size);
if not Result then Exit;
{ ischbinz fix tegen foute boundingbox:
Header.Frames[0].Min_Bounds := vector(-7.526827, -8.115809, -0.126671);
Header.Frames[0].Max_Bounds := vector(8.078770, 7.489825, 18.962357);
Header.Frames[0].Local_Origin := vector(0,0,0);
Header.Frames[0].Radius := 17.7985763549805;
}
// Tags laden
if Header.Values.Num_Tags > 0 then begin
Fopen.Position := Header.Values.Ofs_Tags;
// (lees in totaal 'Size_Tags' bytes aan data)
SetLength(Header.Tags, Header.Values.Num_Frames * Header.Values.Num_Tags);
for f:=0 to Header.Values.Num_Frames-1 do begin
Size := Header.Values.Num_Tags * SizeOf(TTag);
j := f * Header.Values.Num_Tags;
N := Fopen.Read(Header.Tags[j], Size);
Result := (N=Size);
if not Result then Exit;
end;
end;
// Surfaces laden
SetLength(Header.Surfaces, Header.Values.Num_Surfaces);
FP := Header.Values.Ofs_Surfaces;
for i:=0 to Header.Values.Num_Surfaces-1 do begin
Fopen.Position := FP;
Size := Size_SurfaceHeader;
N := Fopen.Read(Header.Surfaces[i].Values, Size);
Result := (N=Size);
if not Result then Exit;
// Shaders
Fopen.Position := FP + Header.Surfaces[i].Values.Ofs_Shaders;
SetLength(Header.Surfaces[i].Shaders, Header.Surfaces[i].Values.Num_Shaders);
Size := Size_SurfaceShaders(i);
N := Fopen.Read(Header.Surfaces[i].Shaders[0], Size);
Result := (N=Size);
if not Result then Exit;
// lege shaders array herstellen..
if Header.Surfaces[i].Values.Num_Shaders=0 then begin
Header.Surfaces[i].Values.Num_Shaders := 1;
SetLength(Header.Surfaces[i].Shaders, Header.Surfaces[i].Values.Num_Shaders);
end;
// Triangles
Fopen.Position := FP + Header.Surfaces[i].Values.Ofs_Triangles;
SetLength(Header.Surfaces[i].Triangles, Header.Surfaces[i].Values.Num_Triangles);
Size := Size_SurfaceTriangles(i);
N := Fopen.Read(Header.Surfaces[i].Triangles[0], Size);
Result := (N=Size);
if not Result then Exit;
// TextureCoords
Fopen.Position := FP + Header.Surfaces[i].Values.Ofs_ST;
SetLength(Header.Surfaces[i].TextureCoords, Header.Surfaces[i].Values.Num_Verts);
Size := Size_SurfaceTexCoords(i);
N := Fopen.Read(Header.Surfaces[i].TextureCoords[0], Size);
Result := (N=Size);
if not Result then Exit;
// texturemapping van ST naar UV converteren
TexCoords_SwapST_UV(i);
// Vertex
Fopen.Position := FP + Header.Surfaces[i].Values.Ofs_XYZNormal;
SetLength(Header.Surfaces[i].Vertex, Header.Surfaces[i].Values.Num_Verts * Header.Surfaces[i].Values.Num_Frames);
Size := Size_SurfaceVertex(i);
N := Fopen.Read(Header.Surfaces[i].Vertex[0], Size);
Result := (N=Size);
if not Result then Exit;
// filepointer op volgende surface-blok
FP := FP + Header.Surfaces[i].Values.Ofs_End;
end;
except
//
end;
finally
Fopen.Free;
end;
end;
//------------------------------------------------------------------------------
function TMD3.SaveToFile(Filename: string): boolean;
var N,i,j, Size: integer;
begin
// bestand opslaan
Fsave := TFileStream.Create(Filename, fmCreate);
try
try
// offsets instellen
Header.Values.Ofs_Frames := Size_Header;
Header.Values.Ofs_Tags := Header.Values.Ofs_Frames + Size_Frames;
Header.Values.Ofs_Surfaces := Header.Values.Ofs_Tags + Size_Tags;
Header.Values.Ofs_EOF := Header.Values.Ofs_Surfaces + Size_Surfaces;
for i:=0 to Header.Values.Num_Surfaces-1 do begin
Header.Surfaces[i].Values.Ofs_Shaders := Size_SurfaceHeader;
Header.Surfaces[i].Values.Ofs_Triangles := Header.Surfaces[i].Values.Ofs_Shaders + Size_SurfaceShaders(i);
Header.Surfaces[i].Values.Ofs_ST := Header.Surfaces[i].Values.Ofs_Triangles + Size_SurfaceTriangles(i);
Header.Surfaces[i].Values.Ofs_XYZNormal := Header.Surfaces[i].Values.Ofs_ST + Size_SurfaceTexCoords(i);
Header.Surfaces[i].Values.Ofs_End := Header.Surfaces[i].Values.Ofs_XYZNormal + Size_SurfaceVertex(i);
end;
// header
Size := Size_Header;
Fsave.WriteBuffer(Header.Values, Size);
// frames
Size := Size_Frames;
Fsave.WriteBuffer(Header.Frames[0], Size);
// tags
Size := Size_Tags;
Fsave.WriteBuffer(Header.Tags[0], Size);
// surfaces
for i:=0 to Header.Values.Num_Surfaces-1 do begin
// surface header
Size := Size_SurfaceHeader;
Fsave.WriteBuffer(Header.Surfaces[i].Values, Size);
// surface shaders
Size := Size_SurfaceShaders(i);
Fsave.WriteBuffer(Header.Surfaces[i].Shaders[0], Size);
// surface triangles
Size := Size_SurfaceTriangles(i);
Fsave.WriteBuffer(Header.Surfaces[i].Triangles[0], Size);
// surface texture-coordinates
// texturemapping van UV naar ST converteren
TexCoords_SwapST_UV(i);
// schrijven
Size := Size_SurfaceTexCoords(i);
Fsave.WriteBuffer(Header.Surfaces[i].TextureCoords[0], Size);
// texturemapping van ST naar UV converteren
TexCoords_SwapST_UV(i);
// surface vertex
Size := Size_SurfaceVertex(i);
Fsave.WriteBuffer(Header.Surfaces[i].Vertex[0], Size);
end;
except
end;
finally
Fsave.Free;
end;
end;
//------------------------------------------------------------------------------
procedure TMD3.AddTags(Filename: string);
var Ftags: TFileStream;
TagsHeader: TTagFileHeader;
ID, N, TagsInFile,Ntags, Nframes, Size: cardinal;
t,f: integer;
newTags: packed array of TTag;
begin
Ftags := TFileStream.Create(Filename, fmOpenRead);
try
Ftags.Position := 0;
N := Ftags.Read(TagsHeader,SizeOf(TTagFileHeader));
if N<>SizeOf(TTagFileHeader) then Exit;
if TagsHeader.Ident<>TAG3 then Exit;
if TagsHeader.Version<>1 then Exit;
if TagsHeader.Num_Tags=0 then Exit;
// het aantal toe te voegen tags bepalen (en array vergroten)
TagsInFile := TagsHeader.Num_Tags;
Ntags := Header.Values.Num_Tags;
Inc(Header.Values.Num_Tags, TagsInFile);
Nframes := Header.Values.Num_Frames;
SetLength(Header.Tags, (Ntags+TagsInFile)*Nframes);
// de tags lezen
SetLength(newTags, TagsInFile);
Size := TagsInFile * SizeOf(TTag);
Ftags.Position := $10; //4*cardinal
N := Ftags.Read(newTags[0], Size);
if N<>Size then Exit;
for f:=Nframes-1 downto 0 do begin
if Ntags>0 then begin
for t:=Ntags-1 downto 0 do begin
Header.Tags[f*(Ntags+TagsInFile)+t] := Header.Tags[f*Ntags+t];
for N:=0 to TagsInFile-1 do
Header.Tags[f*(Ntags+TagsInFile)+Ntags+N] := newTags[N];
end;
end else begin
for N:=0 to TagsInFile-1 do
Header.Tags[f*TagsInFile+N] := newTags[N];
end;
end;
finally
SetLength(newTags, 0);
Ftags.Free;
end;
end;
procedure TMD3.AddTag(const aTag: TTag);
var Ntags,t, Nframes,f, Size: integer;
begin
// 1 tag toevoegen (en array vergroten)
Ntags := Header.Values.Num_Tags;
Inc(Header.Values.Num_Tags, 1);
Nframes := Header.Values.Num_Frames;
SetLength(Header.Tags, (Ntags+1)*Nframes);
for f:=Nframes-1 downto 0 do
for t:=0 to Header.Values.Num_Tags-1 do begin
Header.Tags[f*(Ntags+1)+t] := Header.Tags[f*Ntags+t];
Header.Tags[f*(Ntags+1)+Ntags] := aTag;
{Header.Tags[f*(Ntags+1)+Ntags].Name := aTag.Name;
Header.Tags[f*(Ntags+1)+Ntags].Origin := aTag.Origin;
Header.Tags[f*(Ntags+1)+Ntags].Axis[0] := aTag.Axis[0];
Header.Tags[f*(Ntags+1)+Ntags].Axis[1] := aTag.Axis[1];
Header.Tags[f*(Ntags+1)+Ntags].Axis[2] := aTag.Axis[2];}
end;
end;
procedure TMD3.SaveTags(Filename: string);
var Ftags: TFileStream;
TagsHeader: TTagFileHeader;
t: integer;
Size: cardinal;
begin
if Header.Values.Num_Tags=0 then Exit;
Ftags := TFileStream.Create(Filename, fmCreate);
try
// header vullen
TagsHeader.Ident := TAG3;
TagsHeader.Version := 1;
TagsHeader.Num_Tags := Header.Values.Num_Tags;
TagsHeader.Ofs_End := SizeOf(TTagFileHeader) + (Header.Values.Num_Tags * SizeOf(TTag));
//
Size := SizeOf(TTagFileHeader);
Ftags.WriteBuffer(TagsHeader, Size);
for t:=0 to Header.Values.Num_Tags-1 do begin
Size := SizeOf(TTag);
Ftags.WriteBuffer(Header.Tags[t], Size);
end;
finally
Ftags.Free;
end;
end;
procedure TMD3.TagsInvertX;
var Ntags,t, Nframes,f: cardinal;
begin
Ntags := Header.Values.Num_Tags;
if Ntags=0 then Exit;
Nframes := Header.Values.Num_Frames;
if Nframes=0 then Exit;
for f:=Nframes-1 downto 0 do
for t:=0 to Ntags-1 do
Header.Tags[f*Ntags+t].Axis[0] := InverseVector(Header.Tags[f*Ntags+t].Axis[0]);
end;
procedure TMD3.TagsInvertY;
var Ntags,t, Nframes,f: cardinal;
begin
Ntags := Header.Values.Num_Tags;
if Ntags=0 then Exit;
Nframes := Header.Values.Num_Frames;
if Nframes=0 then Exit;
for f:=Nframes-1 downto 0 do
for t:=0 to Ntags-1 do
Header.Tags[f*Ntags+t].Axis[1] := InverseVector(Header.Tags[f*Ntags+t].Axis[1]);
end;
procedure TMD3.TagsInvertZ;
var Ntags,t, Nframes,f: cardinal;
begin
Ntags := Header.Values.Num_Tags;
if Ntags=0 then Exit;
Nframes := Header.Values.Num_Frames;
if Nframes=0 then Exit;
for f:=Nframes-1 downto 0 do
for t:=0 to Ntags-1 do
Header.Tags[f*Ntags+t].Axis[2] := InverseVector(Header.Tags[f*Ntags+t].Axis[2]);
end;
procedure TMD3.TagsSwapXY;
var Ntags,t, Nframes,f: cardinal;
tmp: TVector;
begin
Ntags := Header.Values.Num_Tags;
if Ntags=0 then Exit;
Nframes := Header.Values.Num_Frames;
if Nframes=0 then Exit;
for f:=Nframes-1 downto 0 do
for t:=0 to Ntags-1 do begin
tmp := Header.Tags[f*Ntags+t].Axis[0];
Header.Tags[f*Ntags+t].Axis[0] := Header.Tags[f*Ntags+t].Axis[1];
Header.Tags[f*Ntags+t].Axis[1] := tmp;
end;
end;
procedure TMD3.TagsSwapXZ;
var Ntags,t, Nframes,f: cardinal;
tmp: TVector;
begin
Ntags := Header.Values.Num_Tags;
if Ntags=0 then Exit;
Nframes := Header.Values.Num_Frames;
if Nframes=0 then Exit;
for f:=Nframes-1 downto 0 do
for t:=0 to Ntags-1 do begin
tmp := Header.Tags[f*Ntags+t].Axis[0];
Header.Tags[f*Ntags+t].Axis[0] := Header.Tags[f*Ntags+t].Axis[2];
Header.Tags[f*Ntags+t].Axis[2] := tmp;
end;
end;
procedure TMD3.TagsSwapYZ;
var Ntags,t, Nframes,f: cardinal;
tmp: TVector;
begin
Ntags := Header.Values.Num_Tags;
if Ntags=0 then Exit;
Nframes := Header.Values.Num_Frames;
if Nframes=0 then Exit;
for f:=Nframes-1 downto 0 do
for t:=0 to Ntags-1 do begin
tmp := Header.Tags[f*Ntags+t].Axis[1];
Header.Tags[f*Ntags+t].Axis[1] := Header.Tags[f*Ntags+t].Axis[2];
Header.Tags[f*Ntags+t].Axis[2] := tmp;
end;
end;
//------------------------------------------------------------------------------
function TMD3.AddFrame(Filename: string; var ErrorString:string): boolean;
var Fopen: TFileStream;
fn: string;
N, Size: cardinal;
i,j,f: integer;
FP: Int64;
strX,strY,strZ, strV, s1,s2: string;
AddFromMD3 : TMD3;
begin
Result := false;
ErrorString := '';
fn := ExtractFilename(Filename);
AddFromMD3 := TMD3.Create;
try
AddFromMD3.ClearMemory; //clear model memory
// stream handle
Fopen := TFileStream.Create(Filename, fmOpenRead);
try
try
// header
N := Fopen.Read(AddFromMD3.Header.Values, SizeOf(THeader1));
if N<>SizeOf(THeader1) then begin
ErrorString := 'Could not read header: '+ fn;
Exit;
end;
Result := AddFromMD3.IsMD3;
if not Result then begin
ErrorString := 'No MD3 format: '+ fn;
Exit;
end;
// test of er tenminste 1 frame is..
Result := (AddFromMD3.Header.Values.Num_Frames>0);
if not Result then begin
ErrorString := 'No frame-data: '+ fn;
Exit;
end;
// test of het aantal tags gelijk is..
Result := (AddFromMD3.Header.Values.Num_Tags = Header.Values.Num_Tags);
if not Result then begin
ErrorString := 'Number of tags not equal: '+ fn;
Exit;
end;
// Frames laden
Fopen.Position := AddFromMD3.Header.Values.Ofs_Frames;
SetLength(AddFromMD3.Header.Frames, AddFromMD3.Header.Values.Num_Frames);
Size := AddFromMD3.Header.Values.Num_Frames * SizeOf(TFrame);
N := Fopen.Read(AddFromMD3.Header.Frames[0], Size);
Result := (N=Size);
if not Result then begin
ErrorString := 'Error reading frame-data: '+ fn;
Exit;
end;
// Tags laden
if AddFromMD3.Header.Values.Num_Tags > 0 then begin
Fopen.Position := AddFromMD3.Header.Values.Ofs_Tags;
SetLength(AddFromMD3.Header.Tags, AddFromMD3.Header.Values.Num_Frames * AddFromMD3.Header.Values.Num_Tags);
if AddFromMD3.Header.Values.Num_Tags>0 then
for f:=0 to AddFromMD3.Header.Values.Num_Frames-1 do begin
Size := AddFromMD3.Header.Values.Num_Tags * SizeOf(TTag);
j := f * AddFromMD3.Header.Values.Num_Tags;
N := Fopen.Read(AddFromMD3.Header.Tags[j], Size);
Result := (N=Size);
if not Result then begin
ErrorString := 'Error reading tag-data: '+ fn;
Exit;
end;
// bestaat deze tag ook in de geladen MD3??
s1 := string(AddFromMD3.Header.Tags[j].Name);
s2 := string(Header.Tags[j].Name);
Result := (s1 = s2);
if not Result then begin
ErrorString := 'Tag "'+ s1 +'" found, expected "'+ s2 +'" in: '+ fn;
Exit;
end;
end;
end;
// Surfaces laden
SetLength(AddFromMD3.Header.Surfaces, AddFromMD3.Header.Values.Num_Surfaces);
FP := AddFromMD3.Header.Values.Ofs_Surfaces;
for i:=0 to AddFromMD3.Header.Values.Num_Surfaces-1 do begin
Fopen.Position := FP;
Size := SizeOf(TSurface1);
N := Fopen.Read(AddFromMD3.Header.Surfaces[i].Values, Size);
Result := (N=Size);
if not Result then begin
ErrorString := 'Error reading surface['+ IntToStr(i) +']-header: '+ fn;
Exit;
end;
s1 := string(AddFromMD3.Header.Surfaces[i].Values.Name);
// controleer of de MD3-to-add past bij de geladen MD3 op het form
Result := (AddFromMD3.Header.Surfaces[i].Values.Num_Shaders = Header.Surfaces[i].Values.Num_Shaders);
if not Result then begin
ErrorString := 'Number of shaders not equal for surface["'+ s1 +'"]: '+ fn;
Exit;
end;
Result := (AddFromMD3.Header.Surfaces[i].Values.Num_Triangles = Header.Surfaces[i].Values.Num_Triangles);
if not Result then begin
ErrorString := 'Number of triangles not equal for surface["'+ s1 +'"]: '+ fn;
Exit;
end;
Result := (AddFromMD3.Header.Surfaces[i].Values.Num_Verts = Header.Surfaces[i].Values.Num_Verts);
if not Result then begin
ErrorString := 'Number of vertexes not equal for surface["'+ s1 +'"]: '+ fn;
Exit;
end;
// Shaders...
// Triangles...
// TextureCoords...
// Vertex
Fopen.Position := FP + AddFromMD3.Header.Surfaces[i].Values.Ofs_XYZNormal;
SetLength(AddFromMD3.Header.Surfaces[i].Vertex, AddFromMD3.Header.Surfaces[i].Values.Num_Verts * AddFromMD3.Header.Surfaces[i].Values.Num_Frames);
Size := AddFromMD3.Header.Surfaces[i].Values.Num_Verts * AddFromMD3.Header.Surfaces[i].Values.Num_Frames * SizeOf(TVertex);
N := Fopen.Read(AddFromMD3.Header.Surfaces[i].Vertex[0], Size);
Result := (N=Size);
if not Result then begin
ErrorString := 'Error reading vertex-data for surface["'+ s1 +'"]: '+ fn;
Exit;
end;
// filepointer op volgende surface-blok
FP := FP + AddFromMD3.Header.Surfaces[i].Values.Ofs_End;
end;
// de gelezen data toevoegen aan de bestaande/geladen MD3:
// Frames:
Inc(Header.Values.Num_Frames, 1); //!!
SetLength(Header.Frames, Header.Values.Num_Frames);
Header.Frames[Header.Values.Num_Frames-1] := AddFromMD3.Header.Frames[0];
Header.Frames[Header.Values.Num_Frames-1].Name := 'C merged';
// Tags:
SetLength(Header.Tags, Header.Values.Num_Frames * Header.Values.Num_Tags);
j := (Header.Values.Num_Frames-1) * Header.Values.Num_Tags;
for i:=0 to Header.Values.Num_Tags-1 do //maar 1 frame overnemen
Header.Tags[j+i] := AddFromMD3.Header.Tags[i];
// Surfaces:
for i:=0 to Header.Values.Num_Surfaces-1 do begin
//Inc(Header.Surfaces[i].Values.Num_Frames, 1); //!!
Header.Surfaces[i].Values.Num_Frames := Header.Values.Num_Frames;
SetLength(Header.Surfaces[i].Vertex, Header.Surfaces[i].Values.Num_Verts * Header.Surfaces[i].Values.Num_Frames);
f := Header.Surfaces[i].Values.Num_Verts * (Header.Surfaces[i].Values.Num_Frames-1);
for j:=0 to Header.Surfaces[i].Values.Num_Verts-1 do
Header.Surfaces[i].Vertex[f+j] := AddFromMD3.Header.Surfaces[i].Vertex[j];
end;