forked from Marginal/FS2XPlane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convbgl.py
2813 lines (2592 loc) · 113 KB
/
convbgl.py
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
from math import acos, atan, atan2, cos, fmod, floor, hypot, pow, sin, pi, radians, degrees
from numpy import array, array_equal
from os import listdir
from os.path import basename, dirname, exists, join, normpath, pardir, splitext
import struct
from struct import unpack
from sys import maxint
from traceback import print_exc
import types
from OpenGL.GL import GL_LINE_LOOP, GL_TRUE, GLfloat
from OpenGL.GLU import *
try:
# apparently older PyOpenGL version didn't define gluTessVertex
gluTessVertex
except NameError:
from OpenGL import GLU
gluTessVertex = GLU._gluTessVertex
from convutil import cirp, m2f, complexity, asciify, unicodeify, normalize, rgb2uv, cross, dot, AptNav, Object, Material, Texture, Point, Matrix, FS2XError, unique, groundfudge, effects
from convobjs import makegenquad, makegenmulti
from convtaxi import taxilayout, Node, Link
from convphoto import blueskyre
# Subset of XML TaxiwayPoint
class TaxiwayPoint:
def __init__(self, loc):
self.type='NORMAL'
self.lat=loc.lat
self.lon=loc.lon
# Subset of XML TaxiwayPath
class TaxiwayPath:
# width<0 -> no lights. -1<=width<=1 -> centreline only
def __init__(self, type, width, centerline, start, end):
self.type=type # TAXI or VEHICLE
if width>0:
self.width=width
else:
self.width=-width
self.surface='ASPHALT'
if -1<=self.width<=1:
# Centreline only
self.width=2 # reasonable number for curves
self.centerline='TRUE'
if width>0:
self.centerLineLighted='TRUE'
else:
self.centerLineLighted='FALSE'
self.rightEdge=self.leftEdge='NONE'
self.rightEdgeLighted=self.leftEdgeLighted='NONE'
self.drawSurface=self.drawDetail='FALSE'
else:
self.drawSurface=self.drawDetail='TRUE'
if type=='VEHICLE':
self.centerLine=centerline
if centerline and width>0:
self.centerLineLighted='TRUE'
else:
self.centerLineLighted='FALSE'
self.rightEdge=self.leftEdge='SOLID'
self.rightEdgeLighted=self.leftEdgeLighted='FALSE'
else:
self.centerline=self.centerLineLighted='FALSE'
self.rightEdge=self.leftEdge='SOLID'
if width>0:
self.rightEdgeLighted=self.leftEdgeLighted='TRUE'
else:
self.rightEdgeLighted=self.leftEdgeLighted='FALSE'
self.start=start
self.end=end
self.name=0 # no name
# Subset of XML TaxiwayName
class TaxiwayName:
def __init__(self, name):
self.name=name
# Read BGL header
class Parse:
def __init__(self, bgl, srcfile, output):
if output.debug: output.debug.write('\nFile: %s\n' % srcfile.encode("latin1",'replace'))
name=basename(srcfile)
texdir=None
for section in [42,46,54,58,102,114]:
bgl.seek(section)
(secbase,)=unpack('<I',bgl.read(4))
if secbase:
bgl.seek(secbase)
if section==42 or section==46:
output.log('Skipping traffic data in file %s' % name)
elif section==54:
try:
ProcTerrain(bgl, srcfile, output)
except:
output.log("Can't parse Terrain section in file %s" % name)
elif section==58: # OBJECT
# Per-BGL counts
old=False
rrt=False
anim=False
first=True
gencache={} # cache of generic building names, by args
objcache=[] # objects generated in this BGL, for dupe detection
if not texdir:
texdir=normpath(join(dirname(srcfile), pardir))
# For case-sensitive filesystems
for d in listdir(texdir):
if d.lower()=='texture':
texdir=maketexdict(join(texdir, d))
break
else:
texdir=None
while True:
# LatBand
posl=bgl.tell()
(c,)=unpack('<B', bgl.read(1))
if not c in [0, 21]:
if output.debug: output.debug.write("!Bogus LatBand %2x\n" % c)
raise struct.error # wtf?
if c==0:
break
(foo,off)=unpack('<Ii', bgl.read(8))
bgl.seek(secbase+off)
while True:
# Header
posa=bgl.tell()
if __debug__:
if output.debug: output.debug.write("----\nArea %x\n" % posa)
(c,)=unpack('<B', bgl.read(1))
bgl.read(9)
if c==0:
break
elif c in [6,9,12,14]:
(l,)=unpack('<I',bgl.read(4))
elif c in [5,8,11,13]:
(l,)=unpack('<H',bgl.read(2))
elif c in [4,7,10]:
(l,)=unpack('<B',bgl.read(1))
else:
if output.debug: output.debug.write("!Bogus area type %x\n"%c)
raise struct.error # wtf?
try:
output.refresh()
p=ProcScen(bgl, posa+l, 1.0, None, srcfile, texdir, output, firstarea=first, gencache=gencache, objcache=objcache)
if p.old:
old=True
if __debug__:
if output.debug: output.debug.write("Pre-FS2002\n")
if p.rrt:
rrt=True
if __debug__:
if output.debug: output.debug.write("Old-style rr\n")
if p.anim:
anim=True
if __debug__:
if output.debug: output.debug.write("Animation\n")
first=False
except:
output.log("Can't parse area %x in file %s" % (posa, name))
if output.debug: print_exc(None, output.debug)
bgl.seek(posa+l)
bgl.seek(posl+9)
if anim:
output.log("Skipping animation in file %s" % name)
if old:
output.log("Skipping pre-FS2002 scenery in file %s" % name)
if rrt:
output.log("Skipping pre-FS2004 runways and/or roads in file %s" % name)
elif section==102:
# Miscellaneous
while True:
# LatBand
posl=bgl.tell()
(c,)=unpack('<B', bgl.read(1))
if not c in [0, 21]:
if output.debug: output.debug.write("!Bogus LatBand %2x\n" % c)
raise struct.error # wtf?
if c==0:
break
(foo,off)=unpack('<Ii', bgl.read(8))
bgl.seek(secbase+off)
try:
ProcMisc(bgl, srcfile, output)
except:
output.log("Can't parse Miscellaneous section in file %s" % name)
bgl.seek(posl+9)
# handle section 9 area and 10 library. libname!=None if this is a library
class ProcScen:
def __init__(self, bgl, enda, scale, libname, srcfile, texdir, output, scen=None, tran=None, firstarea=True, gencache=None, objcache=None):
self.old=False # Old style scenery found and skipped
self.rrt=False # Old style runways/roads found and skipped
self.anim=False # Animations found and skipped
self.debug=output.debug
self.cmd=0
self.bgl=bgl
self.enda=enda
self.libname=libname
self.name=libname # base name for generated objects
self.srcfile=srcfile
self.texdir=texdir
if libname:
self.comment="object %s in file %s" % (
libname,asciify(basename(self.srcfile),False))
else:
self.comment="file %s" % asciify(basename(self.srcfile),False) # Used for reporting
self.output=output
self.firstarea=firstarea
self.gencache=gencache
self.objcache=objcache
self.start=bgl.tell()
if tran: self.tran=self.start+tran # Location of TRAN table
if scen:
# Decode SCEN table
bgl.seek(self.start+scen)
(count,)=unpack('<H', bgl.read(2))
scen=self.start+scen+2
self.scen=[None for i in range(count)]
for i in range(count):
bgl.seek(scen+i*12)
(out,child,peer,size,offset)=unpack('<hhhhi', bgl.read(12))
# Child and Peer are offsets; convert to scene nos
if child!=-1:
bgl.seek(scen+child*12)
(child,)=unpack('<h',bgl.read(2))
if peer!=-1:
bgl.seek(scen+peer*12)
(peer,)=unpack('<h',bgl.read(2))
# Convert offsets to file positions
self.scen[out]=(child, peer, size, scen+i*12+offset, -1)
# Invert Child/Peer pointers to get parents
for i in range(count):
(child, peer, size, posa, parent)=self.scen[i]
if child!=-1: # child's parent is me
(xchild, xpeer, xsize, xposa, xparent)=self.scen[child]
self.scen[child]=(xchild, xpeer, xsize, xposa, i)
if peer!=-1: # peer's parent is my parent
(xchild, xpeer, xsize, xposa, xparent)=self.scen[peer]
self.scen[peer]=(xchild, xpeer, xsize, xposa, parent)
if self.debug:
# Dump SCEN table
self.debug.write("SCEN: %-2d ANIC\n" % count)
maxt=-1
for i in range(count):
(child,peer,size,posa,parent)=self.scen[i]
bgl.seek(posa)
(cmd,src,dst)=unpack('<3H', bgl.read(6))
c="%2d: %2d %2d %2d -> %2x " %(i, child, peer, parent, cmd)
if cmd==0xc6 and size==6:
c+="%2d %2d\n" % (src, dst)
maxt=max(maxt,src)
else:
c+="size=%d\n" % size
self.debug.write(c)
# Dump TRAN table
count=maxt+1
self.debug.write("TRAN: %d\n" % count)
bgl.seek(self.tran)
for i in range(count):
m=[]
for j in range(4):
(a,b,c,d)=unpack('<4f', bgl.read(16))
m.append([a,b,c,d])
self.debug.write("%s = %d\n" % (Matrix(m), i))
bgl.seek(self.start)
# State
self.complexity=1
self.loc=None
self.alt=0
self.altmsl=0
self.layer=None
self.matrix=[None]
self.scale=1.0 # don't know what to do with scale value in FS8 libs
self.basescale=self.scale
self.stack=[] # (return address, layer, billboard, pop matrix?)
self.tex=[]
self.mat=[]
self.vtx=[None] * 2048 # DefRes/ResList limit
self.idx=[] # Indices into vtx
self.m=None # Index into mat
self.t=None # Index into tex
self.lightcol=(1,1,1)
self.billboard=None # Emulating billboarding
self.nodes=[]
self.links=[]
self.linktype=None # Last road/river/taxiway (type, width, centerline)
self.haze=0 # Whether palette-based transparency. 0=none
self.zbias=0 # Polygon offsetting
self.surface=False # StrRes/CntRes does lines not surface by default
self.concave=False
self.objdat={} # (mat, vtx[], idx[]) by (name, loc, layer, alt, altmsl, matrix, scale, Texture)
self.lightdat={} # ((x,y,z), (r,g,b)) by (name, loc, layer, alt, altmsl, matrix, scale, None)
self.effectdat={} # ((x,y,z), (effect, s)) by (name, loc, layer, alt, altmsl, matrix, scale, None)
self.keys=[] # ordered list of keys for indexing the above
self.polydat=[] # ((points, layer, heading, scale, Texture))
self.neednight=False
self.dayloc=None
self.dayobjdat={}
self.daylightdat={}
self.dayeffectdat={}
self.daypolydat=[]
self.vars={
# Vars not listed are assumed to be 0
0: 0, # dummy
0x02: 2, # No idea, but used by Aerosoft to control lighting
0x10: 50, # No idea, but used by Aerosoft to control lighting
0x5c: 255, # Local variable used by Aerosoft in EDDF
#0x07e: 0, # FS5_CG_TO_GROUND_X: ???
0x280: 0, # altmsl (presumably of airplane?)
0x282: 1, # beacon: rotating bitmap shifted 1 bit/3 sec
0x28c: 1, # tod: 2|4 indicates night?
0x30A: 6, # beacon_2: 16-bit bitmap with 0x0506 add/3 sec
0x30C: 8, # beacon_3: 16-bit bitmap with 0x0708 add/3 sec
0x30E: 0xa, # beacon_4: 16-bit bitmap with 0x090A add/3 sec
0x310: 0xc, # beacon_5: 16-bit bitmap with 0x0B0C add/3 sec
#0x312-31a # usrvar-usrvar5
#0x338, 1, # SHADOW_FLAG: 0=do crash detection???
0x33a: 0, # rbias: eye to object distance in meters II.FF
0x33b: 0, # rbias_1: eye to object distance in meters IIII
0x340: 255, # ground_texture
0x342: 255, # building_texture
0x344: 255, # aircraft_texture
0x346: 5, # image_complex: complexity of the scenery 0-5
0x37e: 1, # xv: rotated X-axis eye to object distance
0x382: 67, # yv: rotated Y-axis eye to object distance - must be >66 for UK2000
0x386: 1, # zv: rotated Z-axis eye to object distance
0x389: 12, # zulu hours in the day - Midday
0x390: 0, # water_texture
0xc72: 5, # ground wind velocity [kts?]
0xc74: 0, # ground wind direction [units?]
0xc76: 0, # ground wind turbulence [units?]
}
self.setseason()
cmds={0x02:self.NOP,
0x05:self.Surface,
0x06:self.SPnt,
0x07:self.CPnt,
0x08:self.Closure,
0x0d:self.Jump,
0x0e:self.DefRes,
0x0f:self.StrRes,
0x10:self.CntRes,
0x14:self.SColor,
0x17:self.TextureEnable,
0x18:self.Texture,
0x1a:self.ResList,
0x1b:self.Jump,
0x1c:self.IfIn2,
0x1d:self.FaceT,
0x1e:self.Haze,
0x1f:self.TaxiMarkings,
0x20:self.FaceTTMap,
0x21:self.IfIn3,
0x22:self.Return,
0x23:self.Call,
0x24:self.IfIn1,
0x25:self.SeparationPlane,
0x26:self.SetWrd,
0x29:self.GResList,
0x2a:self.FaceT,
0x2b:self.Call32,
0x2d:self.SColor24,
0x2e:self.LColor24,
0x2f:self.Scale,
0x30:self.NOPh,
0x32:self.Call,
0x33:self.Instance,
0x34:self.SuperScale,
0x35:self.PntRow,
0x37:self.Point,
0x38:self.Concave,
0x39:self.IfMsk,
0x3b:self.VInstance,
0x3c:self.Position,
0x3e:self.FaceT,
0x40:self.ShadowVPosition,
0x42:self.TextureRunway,
0x43:self.Texture2,
0x44:self.TextureRunway,
0x4c:self.VScale,
0x4d:self.MoveL2G,
0x4e:self.MoveL2G,
0x46:self.PointVICall,
0x49:self.Building,
0x3f:self.NOPh,
0x50:self.SColor,
0x51:self.LColor,
0x52:self.SColor,
0x55:self.SurfaceType,
0x5d:self.TextureRepeat,
0x5f:self.IfSizeV,
0x62:self.IfVis,
0x63:self.LibraryCall,
0x69:self.RoadStart,
0x6a:self.RoadCont,
0x6b:self.RiverStart,
0x6c:self.RiverCont,
0x6d:self.IfSizeV,
0x6e:self.TaxiwayStart,
0x6f:self.RoadCont,
0x70:self.AreaSense,
0x73:self.Jump,
0x74:self.AddCat,
0x75:self.Call,
0x76:self.NOP,
0x77:self.ScaleAGL,
0x7a:self.FaceTTMap,
0x7d:self.NOP,
0x80:self.ResPnt,
0x81:self.NOPh,
0x83:self.ReScale,
0x88:self.Jump32,
0x89:self.NOPi,
0x8a:self.Call32,
0x8b:self.AddCat32,
0x8f:self.NOPi,
0x93:self.NOPh,
0x95:self.CrashIndirect,
0x96:self.CrashStart,
0x9e:self.Interpolate,
0x9f:self.NOPi,
0xa0:self.Object,
0xa4:self.NOPh,
0xa6:self.TargetIndicator,
0xa7:self.SpriteVICall,
0xa8:self.TextureRoadStart,
0xaa:self.NewRunway,
0xad:self.Animate,
0xae:self.TransformEnd,
0xac:self.ZBias,
0xaf:self.TransformMatrix,
0xb1:self.Tag,
0xb2:self.Light,
0xb3:self.IfInF1,
0xb4:self.NOPi,
0xb5:self.VertexList,
0xb6:self.MaterialList,
0xb7:self.TextureList,
0xb8:self.SetMaterial,
0xb9:self.DrawTriList,
0xba:self.DrawLineList,
0xbc:self.NOPi,
0xbd:self.NOP,
0xc4:self.SetMatrixIndirect,
}
while True:
pos=bgl.tell()
if pos>=self.enda:
self.cmd=0
if pos>self.enda and self.debug: self.debug.write("!Overrun at %x enda=%x\n" % (pos, self.enda))
elif pos<self.start:
# Underrun - just return if in a call eg ESGJ2K2
self.cmd=0x22
if self.debug: self.debug.write("!Underrun at %x start=%x\n" % (pos, self.start))
else:
(self.cmd,)=unpack('<H',bgl.read(2))
if self.cmd==0 or (self.cmd==0x22 and not self.stack):
if self.donight():
bgl.seek(self.start)
continue
self.makeobjs()
if self.debug:
if __debug__:
self.debug.write("%x: cmd %02x\n" % (pos, self.cmd))
for i in range(1, len(self.matrix)):
self.debug.write("!Unbalanced matrix:\n%s\n" % (
self.matrix[i]))
return
elif self.cmd in cmds:
if __debug__:
if self.debug: self.debug.write("%x: cmd %02x %s\n" % (
pos, self.cmd, cmds[self.cmd].__name__))
cmds[self.cmd]
elif self.donight():
bgl.seek(self.start)
continue
else:
self.makeobjs() # Try to go with what we've got so far
if self.debug: self.debug.write("!Unknown cmd %x at %x\n" % (self.cmd, pos))
raise struct.error
def donight(self):
if self.neednight and self.vars[0x28c]==1:
if __debug__:
if self.debug: self.debug.write("Night\n")
self.vars[0x28c]=4
self.dayloc=self.loc
self.dayobjdat=self.objdat
self.daylightdat=self.lightdat
self.dayeffectdat=self.effectdat
self.daypolydat=self.polydat
self.objdat={}
self.lightdat={}
self.effectdat={}
#self.keys=[] # Don't reset keys - needed to catch day-only objs
self.polydat=[]
self.complexity=1
self.loc=None
self.alt=0
self.altmsl=0
self.layer=None
self.matrix=[None]
self.scale=self.basescale
self.stack=[] # (return address, layer, billboard, pop matrix?)
self.tex=[]
self.mat=[]
self.vtx=[None] * 2048 # DefRes limit
self.idx=[] # Indices into vtx
self.m=None # Index into mat
self.t=None # Index into tex
self.lightcol=(1,1,1)
self.billboard=None # Emulating billboarding
self.nodes=[]
self.links=[]
self.linktype=None # Last road/river/taxiway (type, width, centerline)
self.haze=0 # Whether palette-based transparency. 0=none
self.zbias=0 # Polygon offsetting
self.concave=False
return True
return False
def makekey(self, dotex=True):
# Return a key suitable for indexing self.objdat
if self.t is None:
tex=None
if __debug__:
if self.debug and not tex: self.debug.write("No texture\n")
else:
tex=self.tex[self.t]
if self.haze and tex.d: self.output.haze[tex.d]=self.haze
if self.haze and tex.e: self.output.haze[tex.e]=self.haze
if self.m is None:
mat=Material(self.output.xpver, (0,0,0))
if __debug__:
if self.debug and not mat: self.debug.write("No material\n")
else:
mat=self.mat[self.m].clone()
mat.dblsided=bool(self.billboard)
return (self.name,self.loc,self.layer,self.alt,self.altmsl,self.matrix[-1],self.scale,tex), mat
def makename(self):
# make a unique base filename
assert not self.libname # Shouldn't be called in library objects
if self.firstarea:
self.name=asciify(splitext(basename(self.srcfile))[0])
else:
self.name="%s@%X" % (asciify(splitext(basename(self.srcfile))[0]), self.bgl.tell()-2)
self.firstarea=False
def Surface(self): # 05
self.surface=True # StrRes/CntRes does surface not lines
def SPnt(self): # 06
(sx,sy,sz)=unpack('<hhh', self.bgl.read(6))
self.old=True
def CPnt(self): # 07
(cx,cy,cz)=unpack('<hhh', self.bgl.read(6))
self.old=True
def Closure(self): # 08
if not self.surface:
self.old=True
return
count=len(self.idx)
if count<=4: self.concave=False # Don't bother
vtx=[]
for i in range(count):
(x,y,z,nx,ny,nz,c,c)=self.vtx[self.idx[i]]
vtx.append((x,y,z, nx,ny,nz, x*self.scale/256, z*self.scale/256))
self.idx=[]
self.surface=False
(key,mat)=self.makekey()
if not key in self.objdat and self.makepoly(False, vtx): # don't generate poly if there's already geometry here
return
if self.concave:
(vtx,idx)=subdivide(vtx)
self.concave=False
else:
idx=[]
for i in range(1,len(vtx)-1):
idx.extend([0,i,i+1]) # make a fan
# Change order if necessary to face in direction of normal
(x0,y0,z0,c,c,c,c,c)=vtx[idx[0]]
(x1,y1,z1,c,c,c,c,c)=vtx[idx[1]]
(x2,y2,z2,c,c,c,c,c)=vtx[idx[2]]
(x,y,z)=cross((x1-x0,y1-y0,z1-z0), (x2-x0,y2-y0,z2-z0))
if dot((x,y,z), (nx,ny,nz))>0: # arbitrary normal from last point
idx.reverse()
mat.poly=True # This command sort-of implies ground-level
if not key in self.objdat:
self.objdat[key]=[]
self.keys.append(key)
self.objdat[key].append((mat, vtx, idx))
self.checkmsl()
def Jump(self): # 0d, 1b: IfInBoxRawPlane, 73: IfInBoxP
(off,)=unpack('<h', self.bgl.read(2))
if not off: raise struct.error # infloop
self.bgl.seek(off-4,1)
def DefRes(self): # 0e
(i)=unpack('<H',self.bgl.read(2))
self.vtx[i]=unpack('<3h', self.bgl.read(6))+(0,1,0,0,0)
def StrRes(self): # 0f
(i,)=unpack('<H',self.bgl.read(2))
self.idx=[i]
def CntRes(self): # 10
(i,)=unpack('<H',self.bgl.read(2))
self.idx.append(i) # wait for closure
def SColor(self): # 14:Scolor, 50:GColor, 52:NewSColor
(c,)=unpack('H', self.bgl.read(2))
self.mat=[Material(self.output.xpver, self.unicol(c))]
self.m=0
def TextureEnable(self): # 17
(c,)=unpack('<H', self.bgl.read(2))
if not c: self.t=None
def Texture(self): # 18
self.surface=True # StrRes/CntRes does surface not lines
(c,x,c,y)=unpack('<4h', self.bgl.read(8))
tex=self.bgl.read(14).rstrip(' \0')
l=tex.find('.')
if l!=-1: # Sometimes formatted as 8.3 with spaces
tex=tex[:l].rstrip(' \0')+tex[l:]
tex=findtex(tex, self.texdir, self.output.addtexdir)
self.tex=[tex and Texture(self.output.xpver, tex, None) or None]
self.t=0
if __debug__:
if self.debug:
self.debug.write("%s\n" % self.tex[0])
if x or y: self.debug.write("!Tex offsets %d,%d\n" % (x,y))
def ResList(self): # 1a
(index,count)=unpack('<2H', self.bgl.read(4))
for i in range(index,index+count):
self.vtx[i]=unpack('<3h', self.bgl.read(6))+(0,1,0,0,0)
def IfInBoxRawPlane(self): # 1b
# Skip
(off,)=unpack('<h', self.bgl.read(2))
if not off: raise struct.error # infloop
self.bgl.seek(off-4,1)
def IfIn2(self): # 1c
var=[0,0]
mins=[0,0]
maxs=[0,0]
(off, var[0], mins[0], maxs[0],
var[1], mins[1], maxs[1])=unpack('<7h', self.bgl.read(14))
for i in range(2):
if var[i]==0x346:
self.complexity=complexity(mins[i])
if maxs[i]==4: maxs[i]=5 # bug in eg EGNT
val=self.getvar(var[i])
if val<mins[i] or val>maxs[i]:
if not off: raise struct.error # infloop
self.bgl.seek(off-22,1)
break
def Haze(self): # 1e:Haze
(self.haze,)=unpack('<H', self.bgl.read(2))
def TaxiMarkings(self): # 1f
# ignored - info should now be in FS2004-style BGL
self.rrt=True
(size,)=unpack('<H', self.bgl.read(2))
self.bgl.seek(size-4,1)
def FaceTTMap(self): # 20:FaceTTMap, 7a:GFaceTTMap
(count,fnx,fny,fnz,)=unpack('<H3h', self.bgl.read(8))
if count<=4: self.concave=False # Don't bother
fnx /= 32767.0
fny /= 32767.0
fnz /= 32767.0
self.bgl.read(4)
vtx=[]
maxy=-maxint
for i in range(count):
(idx,tu,tv)=unpack('<H2h', self.bgl.read(6))
(x,y,z,nx,ny,nz,c,c)=self.vtx[idx]
maxy=max(maxy,y)
if self.billboard:
vtx.append((x,y,z, 0,1,0, x*self.scale/256, z*self.scale/256))
elif self.cmd==0x7a:
vtx.append((x,y,z, nx,ny,nz, tu/255.0,tv/255.0))
else:
vtx.append((x,y,z, fnx,fny,fnz, tu/255.0,tv/255.0))
if not self.objdat and blueskyre.match(basename(self.tex[self.t].d)):
if __debug__:
if self.debug: self.debug.write("Photoscenery\n")
return # handled in ProcPhoto
(key,mat)=self.makekey()
if not key in self.objdat and self.makepoly(True, vtx): # don't generate poly if there's already geometry here
return
if self.concave:
(vtx,idx)=subdivide(vtx)
self.concave=False
else:
idx=[]
for i in range(1,len(vtx)-1):
idx.extend([0,i,i+1])
# Change order if necessary to face in direction of normal
(x0,y0,z0,c,c,c,c,c)=vtx[idx[0]]
(x1,y1,z1,c,c,c,c,c)=vtx[idx[1]]
(x2,y2,z2,c,c,c,c,c)=vtx[idx[2]]
(x,y,z)=cross((x1-x0,y1-y0,z1-z0), (x2-x0,y2-y0,z2-z0))
if dot((x,y,z), (fnx,fny,fnz))>0:
idx.reverse()
if maxy+self.alt <= groundfudge:
mat.poly=True
if not key in self.objdat:
self.objdat[key]=[]
self.keys.append(key)
self.objdat[key].append((mat, vtx, idx))
self.checkmsl()
def IfIn3(self): # 21
var=[0,0,0]
mins=[0,0,0]
maxs=[0,0,0]
(off, var[0], mins[0], maxs[0],
var[1], mins[1], maxs[1],
var[2], mins[2], maxs[2])=unpack('<10h', self.bgl.read(20))
for i in range(3):
if var[i]==0x346:
self.complexity=complexity(mins[i])
if maxs[i]==4: maxs[i]=5 # bug in eg EGNT
val=self.getvar(var[i])
if val<mins[i] or val>maxs[i]:
if not off: raise struct.error # infloop
self.bgl.seek(off-22,1)
break
def Return(self): # 22
(off,self.layer,self.billboard,pop)=self.stack.pop()
if pop:
self.matrix.pop()
if __debug__:
if self.debug: self.debug.write("Now\n%s\n" % self.matrix[-1])
self.bgl.seek(off)
def Call(self): # 23:Call, 32:PerspectiveCall/AddObj, 75:AddMnt
(off,)=unpack('<h', self.bgl.read(2))
if not off: raise struct.error # infloop
self.precall(False)
self.bgl.seek(off-4,1)
def IfIn1(self): # 24
(off, var, vmin, vmax)=unpack('<4h', self.bgl.read(8))
if vmin>vmax: # Sigh. Seen in TFFR
foo=vmax
vmax=vmin
vmin=foo
if var==0x346:
self.complexity=complexity(vmin)
if vmax==4: vmax=5 # bug in eg EGNT
val=self.getvar(var)
if __debug__:
if self.debug:
self.debug.write("%x: %d<=%d<=%d\n" % (var, vmin, val, vmax))
if (self.output.registered and var==0) or var in [0x312,0x314,0x316,0x318,0x31a]:
return # user-defined - assume True
if val<vmin or val>vmax:
if not off: raise struct.error # infloop
self.bgl.seek(off-10,1)
def SeparationPlane(self): # 25
# Used for animating distance. Skip
(off,nx,ny,nz,dist)=unpack('<4hi', self.bgl.read(12))
#self.precall(self.matrix[-1])
#self.bgl.seek(off-14,1)
def SetWrd(self): # 26
(var,val)=unpack('<2h',self.bgl.read(4))
self.vars[var]=val
def GResList(self): # 29
(index,count)=unpack('<2H', self.bgl.read(4))
for i in range(index,index+count):
(x,y,z,nx,ny,nz)=unpack('<6h', self.bgl.read(12))
self.vtx[i]=(x,y,z, nx/32767.0,ny/32767.0,nz/32767.0, 0,0)
def FaceT(self): # 1d:Face, 2a:GFaceT, 3e:FaceT
(count,)=unpack('<H', self.bgl.read(2))
if self.cmd==0x1d: self.bgl.read(6) # point
(fnx,fny,fnz)=unpack('<3h', self.bgl.read(6))
if count<=4: self.concave=False # Don't bother
fnx /= 32767.0
fny /= 32767.0
fnz /= 32767.0
if self.cmd!=0x1d: self.bgl.read(4) # dot_ref
vtx=[]
maxy=-maxint
for i in range(count):
(idx,)=unpack('<H', self.bgl.read(2))
(x,y,z,nx,ny,nz,c,c)=self.vtx[idx]
maxy=max(maxy,y)
if self.billboard:
vtx.append((x,y,z, 0,1,0, x*self.scale/256, z*self.scale/256))
elif self.cmd==0x2a:
vtx.append((x,y,z, nx,ny,nz, x*self.scale/256, z*self.scale/256))
else:
vtx.append((x,y,z, fnx,fny,fnz, x*self.scale/256, z*self.scale/256))
if count<3: return # wtf?
(key,mat)=self.makekey(self.cmd!=0x1d)
if not key in self.objdat and self.makepoly(False, vtx): # don't generate poly if there's already geometry here
return
if self.concave:
self.concave=False
(vtx,idx)=subdivide(vtx)
if not vtx:
if self.debug: self.debug.write("!Subdivision failed\n")
return
else:
idx=[]
for i in range(1,len(vtx)-1):
idx.extend([0,i,i+1])
# Change order if necessary to face in direction of normal
(x0,y0,z0,c,c,c,c,c)=vtx[idx[0]]
(x1,y1,z1,c,c,c,c,c)=vtx[idx[1]]
(x2,y2,z2,c,c,c,c,c)=vtx[idx[2]]
(x,y,z)=cross((x1-x0,y1-y0,z1-z0), (x2-x0,y2-y0,z2-z0))
if dot((x,y,z), (fnx,fny,fnz))>0:
idx.reverse()
if maxy+self.alt <= groundfudge:
mat.poly=True
if not key in self.objdat:
self.objdat[key]=[]
self.keys.append(key)
self.objdat[key].append((mat, vtx, idx))
self.checkmsl()
def SColor24(self): # 2d: SColor24
(r,a,g,b)=unpack('4B', self.bgl.read(4))
if a==0xf0: # unicol
self.mat=[Material(self.output.xpver, self.unicol(0xf000+r))]
self.m=0
elif (0xb0 <= a <= 0xb7) or (0xe0 <= a <= 0xe7):
# E0 = transparent ... EF=opaque. Same for B?
# Treat semi-transparent as fully transparent
self.mat=[]
self.m=None
else:
self.mat=[Material(self.output.xpver, (r/255.0,g/255.0,b/255.0))]
self.m=0
def LColor24(self): # 2e: SColor24
(r,a,g,b)=unpack('4B', self.bgl.read(4))
if a==0xf0: # unicol
self.lightcol=self.unicol(0xf000+r)
elif (0xb0 <= a <= 0xb7) or (0xe0 <= a <= 0xe7):
# E0 = transparent ... EF=opaque. Same for B?
# Treat semi-transparent as fully transparent
self.lightcol=None
else:
self.lightcol=(r/255.0,g/255.0,b/255.0)
def Scale(self): # 2f
self.makename()
self.bgl.read(8) # jump,range (LOD) (may be 0),size,reserved
(scale,)=unpack('<I', self.bgl.read(4))
self.scale=65536.0/scale
(lat,lon,self.altmsl)=self.LLA()
self.alt=0
if __debug__:
if self.debug: self.debug.write("AltMSL %.3f\n" % self.altmsl)
if -90 <= lat <= 90 and -180 <= lon <= 180:
self.loc=Point(lat,lon)
if lat<0 and not self.output.hemi:
self.output.hemi=1
self.setseason()
else: # bogus
if __debug__:
if self.debug: self.debug.write("!Bogus Location %s\n" % Point(lat,lon))
self.loc=None
def Instance(self): # 33
(off,p,b,h)=unpack('<h3H', self.bgl.read(8))
if not off: raise struct.error # infloop
self.precall(True)
p=p*360/65536.0
b=b*360/65536.0
h=h*360/65536.0
newmatrix=Matrix()
newmatrix=newmatrix.headed(h)
newmatrix=newmatrix.pitched(p)
newmatrix=newmatrix.banked(b)
if __debug__:
if self.debug:
self.debug.write("Instance\n")
if self.matrix[-1]:
self.debug.write("Old\n%s\n" % self.matrix[-1])
self.debug.write("New\n%s\n" % newmatrix)
if self.matrix[-1]:
newmatrix=newmatrix*self.matrix[-1]
self.matrix.append(newmatrix)
if __debug__:
if self.debug: self.debug.write("Now\n%s\n" % self.matrix[-1])
self.bgl.seek(off-10,1)
def SuperScale(self): # 34
self.bgl.read(6) # jump,range (LOD) (may be 0),size
(scale,)=unpack('<H', self.bgl.read(2))
if scale>31:
self.scale=65536.0/self.getvar(scale)
else:
self.scale=1.0/pow(2,16-scale)
def PntRow(self): # 35
(sx,sy,sz,ex,ey,ez,count)=unpack('<6hH', self.bgl.read(14))
if count>1:
dx=(ex-sx)/(count-1.0)
dy=(ey-sy)/(count-1.0)
dz=(ez-sz)/(count-1.0)
else:
dx=dy=dz=0
(key,mat)=self.makekey(False)
if not key in self.lightdat:
self.lightdat[key]=[]
self.keys.append(key)
for i in range(count):
self.lightdat[key].append(((sx+i*dx,sy+i*dy,sz+i*dz), self.lightcol))
self.checkmsl()
def Point(self): # 37
(x,y,z)=unpack('<3h', self.bgl.read(6))
(key,mat)=self.makekey(False)
if not key in self.lightdat:
self.lightdat[key]=[]
self.keys.append(key)
self.lightdat[key].append(((x,y,z), self.lightcol))
if __debug__:
if self.debug: self.debug.write("%s %s\n" % ((x,y,z), self.lightcol))
self.checkmsl()
def Concave(self): # 38
self.concave=True
def IfMsk(self): # 39
(off, var)=unpack('<2h', self.bgl.read(4))
(mask,)=unpack('<H', self.bgl.read(2))
val=self.getvar(var)
if val&mask==0:
if not off: raise struct.error # infloop
self.bgl.seek(off-8,1)
def VInstance(self): # 3b
(off,var)=unpack('<hH', self.bgl.read(4))
if not off: raise struct.error # infloop
self.precall(True)
p=self.getvar(var)
p=self.getvar(var)*360/65536.0
b=self.getvar(var+2)*360/65536.0
h=self.getvar(var+4)*360/65536.0
newmatrix=Matrix()
newmatrix=newmatrix.headed(h)
newmatrix=newmatrix.pitched(p)
newmatrix=newmatrix.banked(b)
if self.debug:
self.debug.write("VInstance\n")
if self.matrix[-1]:
self.debug.write("Old\n%s\n" % self.matrix[-1])
self.debug.write("New\n%s\n" % newmatrix)
if self.matrix[-1]:
newmatrix=newmatrix*self.matrix[-1]
if __debug__:
if self.debug: self.debug.write("Now\n%s\n" % self.matrix[-1])
self.matrix.append(newmatrix)
self.bgl.seek(off-6,1)
def Position(self): # 3c
self.makename()
self.bgl.read(8) # jump,range (LOD) (may be 0),size,reserved
(lat,lon,self.altmsl)=self.LLA()
self.alt=0
if __debug__:
if self.debug: self.debug.write("Position %s\n" % self.altmsl)