-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxmcommand.pas
1767 lines (1331 loc) · 50.5 KB
/
txmcommand.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 TXMCommand;
interface
uses
dos, StrUtils, TXMUtils, TXMVar ;
Procedure Command(var STXM : string);
implementation
type
T_Subset = record
FirstVertex,
NbVertex,
FirstIndice,
NbIndice : longint;
mshd : string;
VertexStreamIndex : longint;
Texture : string;
MinN1, MinN2, MinN3,
MaxN1, MaxN2, MaxN3 : single;
end;
T_LOD = record
FocusMin, FocusMax : Single;
SubsetMin, SubsetMax : longint;
end;
T_Vertex = record
y,z,x : single;
end;
T_NormalVertex = record
y,z,x : single;
end;
T_TextureVertex = record
x,y : single;
end;
T_ShipVertexMmod = record
V : T_Vertex;
Vn : T_NormalVertex;
V1t : T_TextureVertex;
V2t : T_TextureVertex;
Other : array[0..5] of single;
end;
var
FOBJ : Text; { fichier OBJ source }
FVertex, { fichier temporaire contenant les vertex }
FVertexNormal, { fichier temporaire contenant les normales }
FVertexTexture, { fichier temporaire contenant les coord textures }
FIndices : file; { fichier temporaire contenant les indices à placer dans le fichier Mmod }
TabSubset : array[0..99] of T_Subset;
TabLOD : array[0..9] of T_LOD;
NbSubset,
NbLODPhases : integer;
NbVertex,
NbIndices : longint;
NbIndex : integer;
Procedure Arreter;
begin
{$i-}
close(FOBJ);
{$i+}
TXMUtils.Arreter;
end;
{*************************************************************************}
{ ajouter un vertex dans le fichier Mmod, place le valeur 'Vertex' dedans }
{ et met les autre valeurs à 0 }
{*************************************************************************}
Procedure AddShipVertex(V : T_Vertex);
const
Blank : array [1..13] of single = (0,0,0,0,0,0,0,0,0,0,0,0,0);
begin
Seek(FMmod,FileSize(FMmod));
BlockWrite(FMmod,V,3*4);
BlockWrite(FMmod,Blank,13*4);
end;
{*****************************************************************************************************}
{ cette procedure lit le fichier OBJ et écrit tous les vertex dans le fichier MMod }
{ elle retourne le nombre de vertex ainsi que les valeurs mini et maxi de chaque type de nombre }
{ pour le calcul de BoundingSphere et BoundingBox }
{*****************************************************************************************************}
(*****************
Procedure WriteVertex(var NbVertex : longint; var MinN1, MinN2, MinN3, MaxN1, MaxN2, MaxN3 : single; CoefEchelle, VOffset : single);
var
NbFaces : longint; { nombre de triangle dans le mesh }
VertexFound : boolean;
Compteur : integer;
W1, W2, W3 : word;
LI : longint;
B : Boolean;
s : string;
V : T_Vertex;
VT : T_TextureVertex;
VN : T_NormalVertex;
SVM : T_ShipVertexMmod;
PosVZone : longint; { position du début de la zone des vertex dans le fichier Mmod }
FirstMinMax : boolean; { vrai pour la premiere lecture des min/max afin de les initialiser }
FPremierV, { fichier de booleen contenant vrai tant que le vertex correpondant n'a pas été utilisé }
FVertexNormal, { fichier temporaire contenant les nomaux vertex source }
FVertexTexture : file; { fichier temporaire contenant les coordonèes des textures }
Key : Char;
Function EstPremierV(N : word) : boolean;
var
b : boolean;
begin
seek(FPremierV,N);
blockRead(FPremierV,B,1);
EstPremierV:=b;
end;
Procedure NonPremierV(N:word);
var
b : boolean;
begin
b:=false;
seek(FPremierV,N);
blockWrite(FPremierV,b,1);
end;
Function ValVertex(LI : longint) : T_Vertex;
var
V : T_Vertex;
begin
V.y:=0;
V.z:=0;
V.x:=0;
LI:=LI*16*4;
inc(LI,PosVZone);
if LI<=FileSize(FMmod)-(3*4) then begin
seek(FMmod,LI);
BlockRead(Fmmod,V,3*4);
end;
ValVertex:=V;
end;
Function ValNormalVertex(LI : longint) : T_NormalVertex;
var
TN : T_NormalVertex;
begin
TN.y:=0;
TN.z:=0;
TN.y:=0;
LI:=LI*3*4;
if LI<=FileSize(FVertexNormal)-(3*4) then begin
seek(FVertexNormal, LI);
BlockRead(FVertexNormal,TN,3*4);
end
else
WriteLN(FRapport,'Dépacement normal Vertex : LI=',LI div 3 div 4,' Filesize=',FileSize(FVertexNormal));
ValNormalVertex:=TN;
end;
Function ValTextureVertex(LI : longint) : T_TextureVertex;
var
TV : T_TextureVertex;
begin
TV.x:=0;
TV.y:=0;
WriteLN(FRapport,LI);
LI:=LI*2*4;
if LI<=FileSize(FVertexTexture)-(2*4) then begin
seek(FVertexTexture, LI);
BlockRead(FVertexTexture,TV,2*4);
end
else
WriteLN(FRapport,'Dépacement texture Vertex : LI=',LI div 2 div 4,' Filesize=',FileSize(FVertexTexture));
ValTextureVertex:=TV;
end;
begin
{ se placer à la fin du fichier FMmod }
seek(FMmod,FileSize(FMmod));
{ enregistrer la position de la zone de début des vertexs }
PosVZone:=FilePos(FMmod);
{ initialiser les fichiers temporaire }
assign(FIndices,'Indices.TMP');
assign(FVertexNormal,'VertexNormal.TMP');
assign(FVertexTexture,'VertexTexture.TMP');
Rewrite(FIndices,1);
Rewrite(FVertexNormal,1);
Rewrite(FVertexTexture,1);
{ initialiser le fichier source }
{$I-}
reset(FOBJ);
{$I+}
if IOResult<>0 then arreter;
{ initialiser le nombre de vertex }
NbVertex:=0;
{ lire le fichier source et remplir les fichiers temporaires FVertexNormal, FVertexTexture }
{ écrire dans le fichier mmod les vertex avec des valeurs "Normale" et "Texture" à 0 }
while not (EOF(FOBJ)) and (DosError=0) do begin
{$I-}
ReadLN(FOBJ,S);
{$I+}
if IOResult=0 then begin
if length(S)>0 then begin
if (S[1]='v') or (S[1]='V') then begin
if (S[2]='t') or (S[2]='T') then begin
{ 'Vt' trouvé }
{ éjecter 'Vt' }
EjectTxt(S,length('Vt'));
{ donner les valeurs par défaut à VT }
VT.x:=0; VT.y:=0;
{ lire VT }
if EjectAllPresentKey(S)<>'' then VT.x:=NumberR(S);
if EjectAllPresentKey(S)<>'' then VT.y:=1-NumberR(S);
{ écrire les valeurs de VT dans le fichier temporaire }
BlockWrite(FVertexTexture,VT,2*4);
end
else
begin
if (S[2]='n') or (S[2]='N') then begin
{ 'Vn' trouvé }
{ éjecter 'Vn' }
EjectTxt(S,length('Vn'));
{ donner les valeurs par défaut à VN }
VN.y:=0; VN.z:=0; VN.x:=0;
{ lire VN }
if EjectAllPresentKey(S)<>'' then VN.y:=NumberR(S);
if EjectAllPresentKey(S)<>'' then VN.z:=NumberR(S);
if EjectAllPresentKey(S)<>'' then VN.x:=NumberR(S);
{ écrire les valeurs de VN dans le fichier temporaire }
BlockWrite(FVertexNormal,VN,3*4);
end
else
begin
{ 'V' trouvé }
inc(NbVertex);
{ éjecter 'V' }
EjectTxt(S,length('V'));
{ donner les valeurs par défaut à V }
V.y:=0; V.z:=0; V.x:=0;
{ lire les valeurs de V }
if EjectAllPresentKey(S)<>'' then V.y:=NumberR(S)*CoefEchelle;
if EjectAllPresentKey(S)<>'' then V.z:=NumberR(S)*CoefEchelle+VOffset;
if EjectAllPresentKey(S)<>'' then V.x:=NumberR(S)*CoefEchelle;
{ écrire les valeurs de V dans FMmod }
AddShipVertex(V);
{ mettre à jour les valeurs de Min/Max N1, N2, N3 }
with V do begin
if FirstMinMax then begin
MinN1:=y; MinN2:=z; MinN3:=x;
MaxN1:=y; MaxN2:=z; MaxN3:=x;
FirstMinMax:=false;
end;
if y<MinN1 then MinN1:=y;
if y>MaxN1 then MaxN1:=y;
if z<MinN2 then MinN2:=z;
if z>MaxN2 then MaxN2:=z;
if x<MinN3 then MinN3:=x;
if x>MaxN3 then MaxN3:=x;
end;
end;
end;
end;
end;
end;
end;
{ Lire les indices, les stocker dans le fichier FIndices, et mettre à jour les valeurs de FMmod }
{ initialiser le fichier FPremierV }
B:=true;
assign(FPremierV,'PremierV.TMP');
Rewrite(FPremierV,1);
for Li:=1 to NbVertex do BlockWrite(FPremierV,B,1);
NbFaces:=0; { initialiser le nombre de triangles }
Reset(FOBJ);
while not (EOF(FOBJ)) and (DosError=0) do begin
StopOnEscape;
{$I-}
ReadLN(FOBJ,S);
{$I+}
if IOResult=0 then begin
if (length(S)>0) and ((S[1]='f') or (S[1]='F')) then begin
{ cette ligne contient un indice }
{ ecrire sur l'écran le Nb de faces écrites }
inc(NbFaces);
Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8));
Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8));
Write(NbFaces:8,' faces');
{ retirer le 'f' du début de ligne }
EjectTXT(S,length('f'));
for compteur:=1 to 3 do begin { lire les trois sommets du triangle }
{ donner les valeurs par défaut à W1, W2 et W3 }
W1:=0; W2:=0; W3:=0;
EjectfirstPresentKey(S);
{ lire le premier indice du vertex }
if EjectAllPresentKey(S)<>'' then begin
W1:=pred(NumberW(S));
{ lire le second indice du vertex }
if (length(S)>0) and (S[1]='/') then begin
EjectTXT(S,length('/'));
if S[1]<>'/' then W2:=Pred(NumberW(S)) else EjectTXT(S,Length('/'));
{ lire le 3eme indice du vertex }
if (length(S)>0) and (S[1]<>' ') then W3:=Pred(NumberW(S));
end;
end
else
begin
Writeln;
Writeln('Face have less then 3 vertices');
W1:=0;
W2:=0;
W3:=0;
end;
{ Ecrire les valeur dans le fichier Mmod et dans le fichier temporaire FIndices }
if EstPremierV(W1) then begin
{ c'est la première fois que cet indice est utilisé, écrire directement les valeurs dans Mmod }
LI:=W1;
LI:=(LI*(16*4))+(3*4);
inc(LI,PosVZone);
seek(FMmod,LI);
VN:=ValNormalVertex(W3);
VT:=ValTextureVertex(W2);
BlockWrite(FMmod,VN,3*4);
BlockWrite(FMmod,VT,2*4);
{ mettre à jour PremierV }
NonPremierV(W1);
end
else
begin
{ ce n'est pas la première fois que cette indice est utilisé, comparer les valeurs, et si nécessaire, créer un nouveau vertex }
LI:=W1;
LI:=LI*(16*4);
inc(LI,PosVZone);
seek(FMmod,LI);
Blockread(FMmod,SVM,16*4);
VN:=ValNormalVertex(W3);
VT:=ValTextureVertex(W2);
if not ((VN.y=SVM.VN.y) and (VN.z=SVM.VN.z) and (VN.z=SVM.VN.z) and
(VT.x=SVM.V1t.x) and (VT.y=SVM.V1t.y)) then begin
{ le vertex n'est pas le meme que celui désigné : rechercher dans tous le fichier s'il n'existe pas déja }
V:=ValVertex(W1);
VertexFound:=false;
seek(FMmod,PosVZone);
for LI:=0 to pred(NbVertex) do begin
if not VertexFound then begin
BlockRead(FMmod,SVM,16*4);
if (SVM.V.y=V.y) and (SVM.V.z=V.z) and (SVM.V.x=V.x) and
(SVM.Vn.y=VN.y) and (SVM.Vn.z=VN.z) and (SVM.Vn.x=VN.x) and
(SVM.V1t.x=VT.x) and (SVM.V1t.y=VT.y) then begin
{ le vertex existe }
W1:=LI;
VertexFound:=true;
end;
end;
end;
if not(VertexFound) then begin
{ le vertex n'existe pas : en créer un nouveau }
SVM.V:=ValVertex(W1);
SVM.Vn:=VN;
SVM.V1t:=VT;
SVM.V2t.x:=0; SVM.V2t.y:=0;
SVM.Other[0]:=0; SVM.Other[1]:=0; SVM.Other[2]:=0; SVM.Other[3]:=0; SVM.Other[4]:=0; SVM.Other[5]:=0;
seek(FMmod,FileSize(FMmod));
BlockWrite(FMmod,SVM,16*4);
{ metre à jour la valeur de l'indice }
W1:=NbVertex;
{ mettre à jour le nombre de vertex }
inc(NbVertex);
end;
end;
end;
{ Ecrire l'indice dans le fichier temporaire FIndices }
BlockWrite(FIndices,W1,2);
end;
if EjectAllPresentKey(S)<>'' then begin
Writeln;
WriteLN('Face have more than 3 vertices');
end;
end;
end;
end;
{ fermer et effacer les fichiers temporaire FVertexNormal et FVertexTexture }
close(FPremierV);
close(FVertexNormal);
close(FVertexTexture);
erase(FPremierV);
erase(FVertexNormal);
erase(FVertexTexture);
WriteLN;
end;
***********************)
{*****************************************************************************************************}
{ lit le fichier source OBJ et écrit les indices dans le fichier destination FMMod }
{*****************************************************************************************************}
Procedure WriteIndices(var NbIndices : longint);
var
LI : longint;
TI : array[1..3] of word;
begin
reset(FIndices,1);
seek(FMmod,FileSize(Fmmod));
{ écrire le mote clé 'Indices' }
WriteLI(length('Indices'));
WriteTxt('Indices');
{ écrire la longueur de la section }
WriteLI(FileSize(FIndices)+8);
{ écrire le nombre d'indices }
WriteLI(FileSize(FIndices) div 2);
{ écrire 101 }
WriteLI(101);
while not(EOF(FIndices)) do begin
{$i-}
BlockRead(FIndices,TI,3*2);
{$i+}
if IOResult=0 then BlockWrite(FMmod,TI,3*2);
end;
NbIndices:=FileSize(FIndices) div 6;
end;
Function ValVertex(LI : longint) : T_Vertex;
var
V : T_Vertex;
begin
V.y:=0;
V.z:=0;
V.x:=0;
LI:=LI*3*4;
if LI<=FileSize(FVertex)-(3*4) then begin
seek(FVertex,LI);
BlockRead(FVertex,V,3*4);
end
else
WriteLN(FRapport,'Dépacement Vertex : LI=',LI div 3 div 4,' Filesize=',FileSize(FVertex));
ValVertex:=V;
end;
Function ValNormalVertex(LI : longint) : T_NormalVertex;
var
TN : T_NormalVertex;
begin
TN.y:=0;
TN.z:=0;
TN.y:=0;
LI:=LI*3*4;
if LI<=FileSize(FVertexNormal)-(3*4) then begin
seek(FVertexNormal, LI);
BlockRead(FVertexNormal,TN,3*4);
end
else
WriteLN(FRapport,'Dépacement normal Vertex : LI=',LI div 3 div 4,' Filesize=',FileSize(FVertexNormal));
ValNormalVertex:=TN;
end;
Function ValTextureVertex(LI : longint) : T_TextureVertex;
var
TV : T_TextureVertex;
begin
TV.x:=0;
TV.y:=0;
LI:=LI*2*4;
if LI<=FileSize(FVertexTexture)-(2*4) then begin
seek(FVertexTexture, LI);
BlockRead(FVertexTexture,TV,2*4);
end
else
WriteLN(FRapport,'Dépacement texture Vertex : LI=',LI div 2 div 4,' Filesize=',FileSize(FVertexTexture));
ValTextureVertex:=TV;
end;
Procedure ReadOBJFile(OBJFileName, Texture : string; Rescale, VOffset : single);
var
KW,
S, S2, S3 : string;
V : T_Vertex;
VT : T_TextureVertex;
VN : T_NormalVertex;
TextureMTL : string;
MTLFileName : string;
FMTL : text;
NbFaces : longint;
Compteur : integer;
W,
W1, W2, W3 : word;
PVertexStream : longint; { mémorise l'emplacement de la longueur du vertexStream }
PDebutVertex : longint; { mémorise la position du début de la liste des vertex }
ShipVertexMmod : T_ShipVertexMmod;
CptVertex,
NoVertex : longint;
Procedure InitSubset;
begin
{ initialiser le nouveau Subset }
TabSubset[NbSubset].FirstVertex:=NbVertex;
TabSubset[NbSubset].FirstIndice:=NbIndices*3;
if StrMajuscule(Texture)='MTL' then
TabSubset[NbSubset].Texture:=TextureMTL
else
TabSubset[NbSubset].Texture:=Texture;
if StrMajuscule(RightStr(TabSubset[NbSubset].Texture,4))='.DDS'then
TabSubset[NbSubset].Texture:=LeftStr(TabSubset[NbSubset].Texture,Length(TabSubset[NbSubset].Texture)-4)+'.tga'
else
begin
WriteLN;
WriteLN('=========================================');
Writeln(' Texture isn''t DDS format !');
WriteLN('=========================================');
Arreter;
end;
TabSubset[NbSubset].MSHD:='ship.mshd';
TabSubset[NbSubset].MinN1:=0;
TabSubset[NbSubset].MinN2:=0;
TabSubset[NbSubset].MinN3:=0;
TabSubset[NbSubset].MaxN1:=0;
TabSubset[NbSubset].MaxN2:=0;
TabSubset[NbSubset].MaxN3:=0;
if (NbSubset=0) or (TabSubset[NbSubset].mshd<>TabSubset[pred(NbSubset)].mshd) then begin
if NbSubset>0 then begin
{ mettre à jour le vertexstream en cours avant de passer au suivant }
{ se positionner à l'emplacement de la longueur de la section }
seek(FMmod,PVertexStream);
{ écrire la longueur de la section }
WriteLI(FileSize(FMmod)-PVertexStream-4);;
WriteLI(NbVertex);
end;
{ créer une nouvelle section }
seek(FMmod,FileSize(FMmod));
WriteLI(length('VertexStream'));
WriteTxt('VertexStream');
PVertexStream:=FilePos(FMmod);
WriteLI(0); { 0 dans le longueur de la section en attendant la bonne valeur }
WriteLI(0); { 0 dans le nombre de vertex en attendant la bonne valeur }
if TabSubset[NbSubset].mshd='ship.mshd' then begin
WriteLI(length('ship.mvfm'));
WriteTxt('ship.mvfm');
end;
PDebutVertex:=FilePos(FMmod);
inc(NbIndex);
end;
{ mettre à jour la bonne valeur d'index }
TabSubset[NbSubset].VertexStreamIndex:=Pred(NbIndex);
{ mettre à jour le nombre d'index }
inc(NbSubset);
end;
begin
{ initialiser les fichiers }
assign(FOBJ,OBJFileName); { fichier OBJ source }
{$i-}
Reset(FOBJ);
{$i+}
if IOResult<>0 then begin
WriteLN;
WriteLN('********************************');
WriteLN('file ',OBJFileName,' not found / not opened');
arreter;
end;
assign(FVertex,'Vertex.TMP');
assign(FVertexNormal,'VertexNormal.TMP');
assign(FVertexTexture,'VertexTexture.TMP');
Rewrite(FVertex,1);
Rewrite(FVertexNormal,1);
Rewrite(FVertexTexture,1);
seek(FIndices,FileSize(FIndices));
{ lire le fichier source et remplir les fichiers temporaires FVertex, FVertexNormal, FVertexTexture }
while not (EOF(FOBJ)) do begin
{$I-}
ReadLN(FOBJ,S);
{$I+}
if IOResult<>0 then begin
writeln;
writeln('================ OBJ READ ERROR !!! ======================');
Arreter;
end;
KW:=StrMajuscule(KeyWord(S));
if KW='V' then begin
{ éjecter 'V' }
EjectTxt(S,length('V'));
{ donner les valeurs par défaut à V }
V.y:=0; V.z:=0; V.x:=0;
{ lire les valeurs de V }
if EjectAllPresentKey(S)<>'' then V.y:=NumberR(S)*Rescale;
if EjectAllPresentKey(S)<>'' then V.z:=NumberR(S)*Rescale+VOffset;
if EjectAllPresentKey(S)<>'' then V.x:=NumberR(S)*Rescale;
{ écrire les valeurs de V dans le fichier temporaire }
BlockWrite(FVertex,V,3*4);
end;
if KW='VT' then begin
{ éjecter 'Vt' }
EjectTxt(S,length('VT'));
{ donner les valeurs par défaut à VT }
VT.x:=0; VT.y:=0;
{ lire VT }
if EjectAllPresentKey(S)<>'' then VT.x:=NumberR(S);
if EjectAllPresentKey(S)<>'' then VT.y:=1-NumberR(S);
{ écrire les valeurs de VT dans le fichier temporaire }
BlockWrite(FVertexTexture,VT,2*4);
end;
if KW='VN' then begin
{ éjecter 'Vn' }
EjectTxt(S,length('Vn'));
{ donner les valeurs par défaut à VN }
VN.y:=0; VN.z:=0; VN.x:=0;
{ lire VN }
if EjectAllPresentKey(S)<>'' then VN.y:=NumberR(S);
if EjectAllPresentKey(S)<>'' then VN.z:=NumberR(S);
if EjectAllPresentKey(S)<>'' then VN.x:=NumberR(S);
{ écrire les valeurs de VN dans le fichier temporaire }
BlockWrite(FVertexNormal,VN,3*4);
end;
end;
{ lire le fichier source OBJ depuis le début et extraire les subsets et les indices }
Reset(FOBJ);
TextureMTL:='white.dds';
MTLFileName:='';
NbFaces:=0;
{ initialiser les subsets }
InitSubset;
while not EOF(FOBJ) do begin
StopOnEscape;
{$I-}
ReadLN(FOBJ,S);
{$I+}
if IOResult<>0 then begin
writeln;
writeln('================ OBJ READ ERROR !!! ======================');
Arreter;
end;
{ lire le mot clé et l'éjecter de la chaine S }
S:=EjectFirstPresentKey(S);
KW:=StrMajuscule(KeyWord(S));
EjectTxt(S,length(KW));
if (KW='MTLLIB') and (StrMajuscule(Texture) ='MTL') then
{ définir le fichier 'MTL' à lire }
MTLFileName:=EjectFirstPresentKey(S);
if (KW='USEMTL') and (StrMajuscule(Texture) ='MTL') then begin
{ ouvrir le fichier MTL et retrouver la définition correspondante }
{ puis creer le nouveau subset }
if MTLFileName<>'' then begin
assign(FMTL,MTLFileName);
{$i-}
reset(FMTL);
{$i+}
if IOResult<>0 then begin
writeln;
writeln('======= MTLFileName not found / not opened !!! =========');
arreter;
end;
while not EOF(FMTL) do begin
ReadLN(FMTL,S2);
if StrMajuscule(KeyWord(S2))='NEWMTL' then begin
EjectTxt(S2,Length('NEWMTL'));
if EjectFirstPresentKey(S2)=EjectFirstPresentKey(S) then begin
{ la section correspondante a été trouvée }
{ lire sa définition }
{ donner la valeur par défaut à TextureMTL }
TextureMTL:='white.dds';
Repeat
{$i-}
Readln(FMTL,S3);
{$i+}
if IOResult<>0 then begin
writeln;
writeln('================ MTL READ ERROR !!! ======================');
Arreter;
end;
if StrMajuscule(KeyWord(S3)) = 'MAP_KD' then begin
{ la définition de la texture a été trouvée, la lire }
S3:=EjectFirstPresentKey(S3);
EjectTxt(S3, length('MAP_KD'));
TextureMTL:=EjectFirstPresentKey(S3);
break;
end;
until (StrMajuscule(KeyWord(S3))='NEWMTL') or EOF(FMTL);;
end;
end;
end;
close(FMTL);
end;
if NbSubset>0 then begin
{ cloturer le subset en cours }
TabSubset[pred(NbSubset)].NbVertex:=NbVertex-TabSubset[pred(NbSubset)].FirstVertex;
TabSubset[pred(NbSubset)].NbIndice:=NbIndices-(TabSubset[pred(NbSubset)].FirstIndice div 3);
end;
{ initialiser le nouveau Subset }
InitSubset;
end;
if KW='F' then begin
{ il s'agit d'une face }
{ la lire et l'ajouter dans FIndices et éventuellement mettre à jour les vertex de FMmod }
{ ecrire sur l'écran le Nb de faces écrites }
inc(NbFaces);
Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8));
Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8)); Write(chr(8));
Write(NbFaces:8,' faces');
{ retirer le 'f' du début de ligne }
EjectTXT(S,length('f'));
for compteur:=1 to 3 do begin { lire les trois sommets du triangle }
{ donner les valeurs par défaut à W1, W2 et W3 }
W1:=0; W2:=0; W3:=0;
S:=EjectfirstPresentKey(S);
{ lire le premier indice du vertex }
if EjectAllPresentKey(S)<>'' then begin
W1:=pred(NumberW(S));
{ lire le second indice du vertex }
if (length(S)>0) and (S[1]='/') then begin
EjectTXT(S,length('/'));
if S[1]<>'/' then W2:=Pred(NumberW(S)) else EjectTXT(S,Length('/'));
{ lire le 3eme indice du vertex }
if (length(S)>0) and (S[1]<>' ') then W3:=Pred(NumberW(S));
end;
end
else
begin
Writeln;
Writeln('Face have less then 3 vertices');
W1:=0;
W2:=0;
W3:=0;
end;
{ récupéré les valeurs des vertex, nomalvertex et texture }
V:=ValVertex(W1);
VT:=ValTextureVertex(W2);
VN:=ValNormalVertex(W3);
{ rechercher dans FMmod si ce vertex existe déja }
NoVertex:=-1;
CptVertex:=0;
seek(FMmod,PDebutVertex);
while not EOF(FMmod) do begin
{ lire le vertex dans FMmod }
if TabSubset[pred(NbSubset)].mshd='ship.mshd' then BlockRead(FMmod,ShipVertexMmod,16*4);
{ comparer le vertex lu à celui dont on a besoin }
if (ShipVertexMmod.V.x=V.x) and (ShipVertexMmod.V.y=V.y) and (ShipVertexMmod.V.z=V.z)
and (ShipVertexMmod.V1t.x=VT.x) and (ShipVertexMmod.V1t.y=VT.y)
and (ShipVertexMmod.Vn.x=VN.x) and (ShipVertexMmod.Vn.y=VN.y) and (ShipVertexMmod.Vn.z=VN.z) then begin
{ le vertex existe et est trouvé }
NoVertex:=CptVertex;
break;
end;
inc(CptVertex);
end;
if NoVertex=-1 then begin
{ le vertex n'existe pas : le créer }
ShipVertexMmod.V.x:=V.x; ShipVertexMmod.V.y:=V.y; ShipVertexMmod.V.z:=V.z;
ShipVertexMmod.V1t.x:=VT.x; ShipVertexMmod.V1t.y:=VT.y;
ShipVertexMmod.Vn.x:=VN.x; ShipVertexMmod.Vn.y:=VN.y; ShipVertexMmod.Vn.z:=VN.z;
ShipVertexMmod.V2t.x:=0; ShipVertexMmod.V2t.y:=0;
ShipVertexMmod.Other[0]:=0; ShipVertexMmod.Other[1]:=0; ShipVertexMmod.Other[2]:=0; ShipVertexMmod.Other[3]:=0; ShipVertexMmod.Other[4]:=0; ShipVertexMmod.Other[5]:=0;
seek(FMmod,FileSize(FMmod));
blockWrite(FMmod,ShipVertexMmod,16*4);
{ mettre à jour le numéro du vertex utilisé }
NoVertex:=CptVertex;
{ mettre à jour le nombre de vertex }
inc(NbVertex);
end;
if NoVertex<=$FFFF then begin
{ écrire l'indice correspondant }
W:=NoVertex;
BlockWrite(Findices,W,2);
end
else
begin
writeln;
writeln('========== TOO MANY VERTICES !!! ===============');
arreter;
end;
{ mettre à jour les valeur min/max pour le subset }
if (TabSubset[pred(NbSubset)].MinN1=0) and (TabSubset[pred(NbSubset)].MinN2=0) and (TabSubset[pred(NbSubset)].MinN3=0) and
(TabSubset[pred(NbSubset)].MaxN1=0) and (TabSubset[pred(NbSubset)].MaxN2=0) and (TabSubset[pred(NbSubset)].MaxN3=0) then begin
{ c'est la première fois : initialiser les valeur Min/Max }
TabSubset[pred(NbSubset)].MinN1:=ShipVertexMmod.V.y;
TabSubset[pred(NbSubset)].MaxN1:=ShipVertexMmod.V.y;
TabSubset[pred(NbSubset)].MinN2:=ShipVertexMmod.V.z;
TabSubset[pred(NbSubset)].MaxN2:=ShipVertexMmod.V.z;
TabSubset[pred(NbSubset)].MinN3:=ShipVertexMmod.V.x;
TabSubset[pred(NbSubset)].MaxN3:=ShipVertexMmod.V.x;
end;
if ShipVertexMmod.V.y<TabSubset[pred(NbSubset)].MinN1 then TabSubset[pred(NbSubset)].MinN1:= ShipVertexMmod.V.y;
if ShipVertexMmod.V.y>TabSubset[pred(NbSubset)].MaxN1 then TabSubset[pred(NbSubset)].MaxN1:= ShipVertexMmod.V.y;
if ShipVertexMmod.V.z<TabSubset[pred(NbSubset)].MinN2 then TabSubset[pred(NbSubset)].MinN2:= ShipVertexMmod.V.z;
if ShipVertexMmod.V.z>TabSubset[pred(NbSubset)].MaxN2 then TabSubset[pred(NbSubset)].MaxN2:= ShipVertexMmod.V.z;
if ShipVertexMmod.V.x<TabSubset[pred(NbSubset)].MinN3 then TabSubset[pred(NbSubset)].MinN3:= ShipVertexMmod.V.x;
if ShipVertexMmod.V.x>TabSubset[pred(NbSubset)].MaxN3 then TabSubset[pred(NbSubset)].MaxN3:= ShipVertexMmod.V.x;
end;
{ mettre à jour le nombre d'indices }
inc(NbIndices);
end;
end;
{ cloturer le dernier subset }
TabSubset[pred(NbSubset)].NbVertex:=NbVertex-TabSubset[pred(NbSubset)].FirstVertex;
TabSubset[pred(NbSubset)].NbIndice:=NbIndices-(TabSubset[pred(NbSubset)].FirstIndice div 3);
{ mettre à jour le dernier vertexstream }
{ se positionner à l'emplacement de la longueur de la section }
seek(FMmod,PVertexStream);
{ écrire la longueur de la section }
WriteLI(FileSize(FMmod)-PVertexStream-4);
{ écrire le nombre de sommets }
WriteLI(NbVertex);
{ se replacer à la fin du fichier FMmod }
seek(FMmod,FileSize(FMmod));
{ fermer le fichier source }
close(FOBJ);
{ fermer et effacer les fichiers temporaires }
close(FVertex);
close(FVertexNormal);
close(FVertexTexture);
(**
erase(FVertex);
erase(FVertexNormal);
erase(FVertexTexture);
**)
end;
Procedure LODPhase(var STXM : string);
var
OBJFileName, { nom du fichier OBJ source }