flt2walk.c
81.6 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
/*
* NOTICE: This source code includes data structures for the MultiGen Flight
* Format which is the proprietary property of MultiGen Inc. It is furnished
* here under a license to Silicon Graphics and Nintendo soley for use in this
* program. If you wish to use the Flight Format for any other purpose, a no
* charge license is noramlly available; contact MultiGen Inc. at
* 408-247-4326.
*/
#define SCALE_TEX
/*
* flt2walk.c -- converts .flt files to .c files for Reality
*
* Lawrence Kesteloot
* June 8th, 1994
*
* Richard Webb
* 11jan95
* Added lighting and centering and scaling.
*
* Mike Goslin
* June 95
* Added radiosity textures, high frequency textures
*
* David Luebke
* June 1995
* Changed 'flt2c' to 'flt2walk' and added functionality for parsing
* cells and portals.
*
* Mike Goslin
* July 95
* added Temporal illumination sequences
*
* Revision 1.11 1995/07/21 03:47:16 goslin
* added temporal illumination sequence ability
*
* Revision 1.10 1995/07/12 21:58:21 luebke
* Added support for 'detail objects' within each cell. A detail object
* is specified by a multigen object that has its bounding box
* set. A detail object will be culled by the uportals library to the
* boundaries of the portal sequence through which the object is
* visible. Note that standard view-frustum culling falls out of this for
* detail objects within the viewer's cell.
*
* Revision 1.32 1995/06/14 20:11:19 rww
* Added support for up to 1024 textures
*
* Revision 1.24 1995/05/26 20:35:58 lipes
* modified code to account for texture size in generating s,t vertex
* parameters; added capability of input arguments for changing
* SetRenderMode to handle "decal" textures
*
* Revision 1.23 1995/04/20 22:43:18 hsa
* remove material stuff
*
* Revision 1.20 1995/03/30 23:17:00 rww
* Propagate Full lighting model support from old tree.
* Type "flt2walk" for a usage message.
*
* Revision 1.18 1995/03/07 19:42:26 lipes
* modified code to generate gsSPDisplayList w/ correct arguments
*
* Revision 1.17 1995/01/18 02:04:47 rww
* Added arguments for light sources and flat shading.
* Removed material and lighting references from the
* generated display list.
*
* Revision 1.16 1994/11/23 06:32:29 hsa
* correct mbi to enforce 64-bit alignment.
*
* Revision 1.15 1994/11/04 22:04:08 howardc
* updated flt2walk and topgun now builds again
*
* Revision 1.12 1994/08/10 21:00:01 kluster
* "garbage" was misspelled
*
* Revision 1.11 1994/08/08 22:15:38 jeffd
* Modified to use new g*SetTile command with clamping bits.
*
* Revision 1.10 1994/08/02 16:11:49 jeffd
* Include <mbi.h> since we ship this as source.
*
* Revision 1.9 1994/07/29 00:27:01 lk
* Added support for texture-only vertex type; fixed bug with unsupported
* tree node.
*
* Revision 1.8 1994/07/22 17:43:35 kluster
* Updated parameter list in calls to readtex
*
* Revision 1.7 1994/07/18 23:17:15 lk
* Changed gsLighting calls to gsSetGeometryMode (G_LIGHTING).
*
* Revision 1.6 1994/07/06 19:36:26 kluster
* re-normalized vectors after being converted from int into float
* to compensate for rounding errors
*
* Revision 1.4 1994/06/29 20:32:20 lk
* Switched order of loadblock/settile.
*
* Revision 1.4 1994/06/10 20:08:44 howardc
* added multigen note
*
* Revision 1.3 1994/06/08 23:22:13 lk
* Re-wrote to keep data structure in memory.
*
*/
/*
* Revision 1.19 1995/02/08 19:53:42 rww
* Added feature to keep normals if no lights are on.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <uportals.h>
/* temp (old) material definition: */
typedef struct {
unsigned char amb[4]; /* ambient reflectivity value (rgba) */
unsigned char diff[4]; /* ambient reflectivity value (rgba) */
unsigned char spec[4]; /* ambient reflectivity value (rgba) */
unsigned char shiny; /* phong power. 0-127 */
unsigned char rolloff; /* transparency roll-off */
char flag; /* to be used later */
char pad;
} Material_t;
typedef union {
Material_t m;
long long int force_structure_alignment;
} Material;
#include "flt2walk.h"
#include "readtex.h"
/*
* Maximum number of textures we load/keep track of. This should probably
* be much bigger or dynamic.
*/
#define MAXTEX 2048
/*
* Set ONLYSUBFACE to 1 if you only want to draw polygons that are subfaces
* of other polygons. This is useful when debugging to find out where
* your textured polygons went.
*/
#define ONLYSUBFACE 0
/*
* Set TRUNCTEX to 1 to truncate texture coordinates to 0..1.
*/
#define TRUNCTEX 0
/*
* List of op-code names to help in debugging.
*/
char *opcode_name[] = {
"0",
"Header",
"Group",
"* Level of detail",
"Object",
"Polygon",
"* Vertex with ID",
"* Short vertex",
"* Vertex with color",
"* Vertex with color and normal",
"Push level",
"Pop level",
"* Translate",
"* Degree of freedom",
"Degree of freedom",
"15",
"Local instance record",
"Local instance bead",
"18",
"Push subface",
"Pop subface",
"21",
"22",
"23",
"24",
"25",
"26",
"27",
"28",
"29",
"30",
"Text comment",
"Color table",
"33",
"34",
"35",
"36",
"37",
"38",
"39",
"* Translate",
"* Rotate about point transform",
"* Rotate about edge transform",
"* Scale transform",
"* Translate transform",
"* Scale transform with independent XYZ",
"* Rotate about point transform",
"* Rotate/Scale to point transform",
"* Put transform",
"Transformation matrix",
"Vector",
"* Bounding box",
"52",
"53",
"54",
"55",
"56",
"57",
"58",
"59",
"Replicate",
"Local instance",
"Local instance library",
"External reference",
"Texture reference record",
"65",
"Material table",
"Shared vertex table",
"Vertex coordinate",
"Vertex with normal",
"Vertex with normal and UV",
"Vertex with UV",
"Vertex list",
"Level of detail",
"Bounding box",
"75",
"Rotate about edge transform",
"Scale transform",
"Translate transform",
"Scale with independent XYZ scale",
"Rotate about point transform",
"Rotate and/or scale transform",
"Put transform",
"Eyepoint record",
"84",
"85",
"86",
"Road record",
"Morphing vertex list",
"89",
"Linkage record",
"Sound bead",
"Sound palette",
"93",
"General matrix transform",
"95",
"96",
"97",
"98",
"99"
};
typedef struct bead *bead;
struct bead {
int opcode, numinst;
void *data;
bead child, sibling;
};
struct opColor ColorTable;
struct opMaterial MaterialTable;
Material_t mat_saved[50];
Material_t mat_default = {
{ 179, 179, 179, 255 },
{ 179, 179, 179, 0 },
{ 51, 51, 51, 0 },
0,
255,
0,
0
};
struct opMatHash {
int mat_id;
unsigned char FaColor[4];
int new_mat_id;
} mat_save_hash[64] ;
int mat_hash_num = 0;
int mat_lookup( int , unsigned char * );
int tex_flags = 0;
int tex_siz = -1;
int tex_fmt = -1;
FILE *dothFilePointer;
char dothFileName[1024];
int CurrentMaterialIndex = 0;
int LightingCurrentlyOn = 0;
float obscale;
struct opVertexNUV center;
struct opVertexNUV eye;
struct opVertexNUV lights[16];
#define MATERIAL_NONE (0xffff)
int output_sample_light = 0;
int output_aux_header = 0;
int use_modulateidecala = 0;
int full_range = 0;
int light_num = 0;
int color_num = 0;
int flat_shade_flag = 0;
int specular_flag = 0;
int texture_flag = 1;
int texture_index = -1;
int doportals = 0;
int output_normals = 0;
int render_mode = 0;
char *render_current;
char *render_normal;
char *render_decal;
char *model_name;
int numtex;
bead root;
uchar *buf, *ptr, *end;
ulong *vtx = NULL;
int vtxoffset = 0, vtxcount;
int subface = 0;
struct texture tex[MAXTEX];
struct opVertexNUV *VertexTable;
int VertexTableMallocCount;
#define VERTEXTABLEMALLOCINCREMENT 100
unsigned char FaceColor[4];
/* MPG - structs for per vertex color */
typedef int col3[3];
int tempSeqCount;
#define MAX_TEMP_SEQ 64
FILE *ttexfile[MAX_TEMP_SEQ];
FILE *pttexfile[MAX_TEMP_SEQ];
/*
* uPortals stuff
*/
upLocateData locator;
/*
* Function: upInitLocator
* Description: Clear out the locator data struct
*/
void upInitLocator(void)
{
int i;
locator.numcells = 0;
for (i=0;i<UP_MAXCELLS;i++)
{
locator.cells[i].portals = NULL;
locator.cells[i].objects = NULL;
locator.cells[i].numportals = 0;
locator.cells[i].numobjects = 0;
}
}
/*
* Function: upAddPortal
* Description: Adds a portal described by the specified multigen polygon
* bead to the specified cell.
* Arguments: polybead - the polygon bead which describes the portal.
* cell - the cell to which the portal belongs
* attached - the cell to which the portal connects
* Returns: a pointer to the filled-out portal structure, or NULL if
* problem occurred.
*/
upPortalData *upAddPortal(bead poly, upCellData *cell, upCellData *attached)
{
struct opVertexList *vtxlist = (struct opVertexList *) poly->child->data;
int i, numv;
upPortalData *portal;
if (vtxlist != NULL)
{
numv = (vtxlist->length - 4) / 4;
}
else
{
fprintf(stderr, "Error: portal apparently has no vertices.\n");
return NULL;
}
if (numv > UP_MAXPVERTS)
{
fprintf(stderr, "Error: portal has %d verts (max is %d)\n",
numv, UP_MAXPVERTS);
return NULL;
}
/*
* Dereference the vertex list and fill in the portal structure
*/
portal = (upPortalData *) malloc(sizeof(upPortalData));
portal->attached_cell = attached;
portal->numverts = numv;
for (i=0;i<numv;i++)
{
portal->verts[i][0] = VertexTable[vtx[vtxlist->vertex[i]]].x;
portal->verts[i][1] = VertexTable[vtx[vtxlist->vertex[i]]].y;
portal->verts[i][2] = VertexTable[vtx[vtxlist->vertex[i]]].z;
}
return portal;
}
/*
* Function: upPrint
* Description: Prints the data structures for the locator, cells,
* and portals to the specified file, and prints extern
* declarations of same to a header file (necessary since the
* portal structs reference the cell structs, and vice-versa).
* Arguments: dotcname - name of the main output file
* dothname - name of the .h header file
*/
void upPrint(char *dotcname, char *dothname)
{
int i, j, k;
FILE *dotc = fopen(dotcname,"w");
FILE *doth = fopen(dothname,"w");
if (dotc == NULL || doth == NULL)
{
fprintf(stderr, "Error opening output file(s) for portal data.\n");
exit(1);
}
fprintf(dotc,"/\*\n");
fprintf(dotc," * %s\n", dotcname);
fprintf(dotc," * This file was automatically generated by flt2walk\n");
fprintf(dotc," */\n\n");
fprintf(doth,"/\*\n");
fprintf(doth," * %s\n", dothname);
fprintf(doth," * This file was automatically generated by flt2walk\n");
fprintf(doth," */\n\n");
fprintf(doth,"\n\n#include <uportals.h>\n");
fprintf(doth,"\n#ifndef FLT_MODEL_SCALE\n");
fprintf(doth,"#define FLT_MODEL_SCALE %g\n",obscale);
fprintf(doth,"#endif\n\n");
fprintf(dotc,"#include <%s>\n\n", dothname);
/* First declare the locator */
fprintf(doth, "extern upLocateData upLocator;\n");
fprintf(dotc, "upLocateData upLocator;\n\n");
/* Now declare and initialize variables to refer to the various cells */
for (i=0;i<locator.numcells;i++)
{
fprintf(dotc, "upCellData *struct_%s = & (upLocator.cells[%d]);\n",
locator.cells[i].name, i);
}
fprintf(dotc, "\n/* Declare the portals and objects: */\n");
for (i=0;i<locator.numcells;i++)
{
for (j=0;j<locator.cells[i].numportals;j++)
{
fprintf(dotc, "static upPortalData portal%d%s2%s;\n", j,
locator.cells[i].name, /* from cell, to cell */
locator.cells[i].portals[j]->attached_cell->name);
}
for (j=0;j<locator.cells[i].numobjects;j++)
{
fprintf(dotc, "static upObjectData object%din%s;\n", j,
locator.cells[i].name);
}
}
fprintf(dotc,"\n/* Initialize the portal arrays for each cell: */\n");
for (i=0;i<locator.numcells;i++)
{
if (locator.cells[i].numportals == 0)
{
fprintf(stderr, "WARNING: cell %s appears to have no portals.\n",
locator.cells[i].name);
continue;
}
fprintf(dotc, "\nstatic upPortalData * %s_portal_array[%d] = \n{\n",
locator.cells[i].name, locator.cells[i].numportals);
for (j=0;j<locator.cells[i].numportals;j++)
{
fprintf(dotc, "\t& portal%d%s2%s %s\n", j,
locator.cells[i].name,
locator.cells[i].portals[j]->attached_cell->name,
(j == locator.cells[i].numportals-1 ? "" : ","));
}
fprintf(dotc, "};\t\t/* End of %s_portal_array */\n",
locator.cells[i].name);
}
fprintf(dotc,"\n/* Initialize the object arrays for each cell: */\n");
for (i=0;i<locator.numcells;i++)
{
if (locator.cells[i].numobjects == 0)
{
continue;
}
fprintf(dotc, "\nstatic upObjectData * %s_object_array[%d] = \n{\n",
locator.cells[i].name, locator.cells[i].numobjects);
for (j=0;j<locator.cells[i].numobjects;j++)
{
fprintf(dotc, "\t& object%din%s %s\n",
j, locator.cells[i].name,
(j == locator.cells[i].numobjects-1 ? "" : ","));
}
fprintf(dotc, "};\t\t/* End of %s_object_array */\n",
locator.cells[i].name);
}
/* extern the cell and root display lists in doth for upInit() to use */
fprintf(doth,"\n/* The display lists for the cells, defined in %s.c: */\n",
model_name);
for (i=0;i<locator.numcells;i++)
{
fprintf(doth, "extern Gfx cell_%s[];\n",
locator.cells[i].name);
}
fprintf(doth,"\n/* The display lists for the objects in the cells: */\n");
for (i=0;i<locator.numcells;i++)
{
for (j=0;j<locator.cells[i].numobjects;j++)
{
fprintf(doth, "extern Gfx %s_detailobj_%s[];\n",
model_name, locator.cells[i].objects[j]->name);
}
}
fprintf(doth, "extern Gfx %s[];\n", model_name);
/* Now write a function mystrncpy, to be used by upInit() */
fprintf(dotc, "\n/\*\n");
fprintf(dotc, " * Function: mystrncpy\n");
fprintf(dotc, " * Description: strncpy isn't supported, so....\n");
fprintf(dotc, " */\n");
fprintf(dotc, "static void mystrncpy(char *dst, char *src, int n)\n");
fprintf(dotc, "{\n");
fprintf(dotc, " while (n)\n");
fprintf(dotc, " {\n");
fprintf(dotc, "\tn--;\n");
fprintf(dotc, "\tif (*src == '\\0') {break;}\n");
fprintf(dotc, "\t*dst = *src;\n");
fprintf(dotc, "\tsrc++;\n");
fprintf(dotc, "\tdst++;\n");
fprintf(dotc, " }\n");
fprintf(dotc, "}\n\n");
/* Now write a function upInit() to fill out the data structures */
fprintf(doth, "\nextern void upInit(void);\n");
fprintf(dotc, "/\*\n");
fprintf(dotc, " * Function:\tupInit\n");
fprintf(dotc, " * Description: fills out the locator, portal, and \n");
fprintf(dotc, " * \t\tobject data structures. Must be called before \n");
fprintf(dotc, " * \t\tany other function in the upPortals library.\n");
fprintf(dotc, " * NOTE:\t\tTHIS FUNCTION WAS GENERATED AUTOMATICALLY\n");
fprintf(dotc, " */\n");
fprintf(dotc, "void upInit(void)\n{\n");
fprintf(dotc, "\tupLocator.numcells = %d;\n", locator.numcells);
fprintf(dotc, "\tupLocator.rootdlist = %s;\n", model_name);
fprintf(dotc, "\tupLocator.portalmin[0] = -1.0f;\n");
fprintf(dotc, "\tupLocator.portalmin[1] = -1.0f;\n");
fprintf(dotc, "\tupLocator.portalmax[0] = 1.0f;\n");
fprintf(dotc, "\tupLocator.portalmax[1] = 1.0f;\n");
fprintf(dotc, "\tupLocator.portaldepth = 0;\n");
fprintf(dotc, "\n\t/* Now fill out each cell individually */");
for (i=0;i<locator.numcells;i++)
{
fprintf(dotc, "\n\n\t/* Cell #%d: %s */\n",
i, locator.cells[i].name);
fprintf(dotc, "\tupLocator.cells[%d].numportals = %d;\n",
i, locator.cells[i].numportals);
fprintf(dotc, "\tupLocator.cells[%d].numobjects = %d;\n",
i, locator.cells[i].numobjects);
fprintf(dotc, "\tmystrncpy(upLocator.cells[%d].name, \"%s\",%d);\n",
i, locator.cells[i].name, UP_CELLNL);
fprintf(dotc, "\tupLocator.cells[%d].dlist = cell_%s;\n",
i, locator.cells[i].name);
fprintf(dotc, "\tupLocator.cells[%d].bbox.min[0] = "
"%g * FLT_MODEL_SCALE;\n",
i, locator.cells[i].bbox.min[0]);
fprintf(dotc, "\tupLocator.cells[%d].bbox.min[1] = "
"%g * FLT_MODEL_SCALE;\n",
i, locator.cells[i].bbox.min[1]);
fprintf(dotc, "\tupLocator.cells[%d].bbox.min[2] = "
"%g * FLT_MODEL_SCALE;\n",
i, locator.cells[i].bbox.min[2]);
fprintf(dotc, "\tupLocator.cells[%d].bbox.max[0] = "
"%g * FLT_MODEL_SCALE;\n",
i, locator.cells[i].bbox.max[0]);
fprintf(dotc, "\tupLocator.cells[%d].bbox.max[1] = "
"%g * FLT_MODEL_SCALE;\n",
i, locator.cells[i].bbox.max[1]);
fprintf(dotc, "\tupLocator.cells[%d].bbox.max[2] = "
"%g * FLT_MODEL_SCALE;\n",
i, locator.cells[i].bbox.max[2]);
fprintf(dotc, "\tupLocator.cells[%d].rendered = 0;\n", i);
if (locator.cells[i].numportals == 0)
{
fprintf(dotc, "\tupLocator.cells[%d].portals = NULL;\n", i);
}
else
{
fprintf(dotc, "\tupLocator.cells[%d].portals = %s_portal_array;\n",
i, locator.cells[i].name);
fprintf(dotc, "\n\t/* Now fill in that portal array */\n");
}
for (j=0;j<locator.cells[i].numportals;j++)
{
fprintf(dotc, "\t%s_portal_array[%d]->numverts = %d;\n",
locator.cells[i].name, j,
locator.cells[i].portals[j]->numverts);
fprintf(dotc, "\t%s_portal_array[%d]->attached_cell= struct_%s;\n",
locator.cells[i].name, j,
locator.cells[i].portals[j]->attached_cell->name);
for (k=0;k<locator.cells[i].portals[j]->numverts;k++)
{
fprintf(dotc, "\t%s_portal_array[%d]->verts[%d][0] = "
"%g * FLT_MODEL_SCALE;\n",
locator.cells[i].name, j, k,
locator.cells[i].portals[j]->verts[k][0]);
fprintf(dotc, "\t%s_portal_array[%d]->verts[%d][1] = "
"%g * FLT_MODEL_SCALE;\n",
locator.cells[i].name, j, k,
locator.cells[i].portals[j]->verts[k][1]);
fprintf(dotc, "\t%s_portal_array[%d]->verts[%d][2] = "
"%g * FLT_MODEL_SCALE;\n",
locator.cells[i].name, j, k,
locator.cells[i].portals[j]->verts[k][2]);
}
}
if (locator.cells[i].numobjects == 0)
{
fprintf(dotc, "\tupLocator.cells[%d].objects = NULL;", i);
}
else
{
fprintf(dotc, "\tupLocator.cells[%d].objects = %s_object_array;\n",
i, locator.cells[i].name);
fprintf(dotc, "\t/* Now fill in that object array */\n");
}
for (j=0;j<locator.cells[i].numobjects;j++)
{
fprintf(dotc, "\t%s_object_array[%d]->rendered = 0;\n",
locator.cells[i].name, j);
fprintf(dotc, "\t%s_object_array[%d]->dlist = %s_detailobj_%s;\n",
model_name, locator.cells[i].name, j,
locator.cells[i].objects[j]->name);
fprintf(dotc,"\tmystrncpy(%s_object_array[%d]->name,\"%s\",%d);\n",
locator.cells[i].name, j,
locator.cells[i].objects[j]->name, UP_OBNL);
fprintf(dotc, "\t%s_object_array[%d]->bbox.min[0]= "
"%g * FLT_MODEL_SCALE;\n",
locator.cells[i].name, j,
locator.cells[i].objects[j]->bbox.min[0]);
fprintf(dotc, "\t%s_object_array[%d]->bbox.min[1]= "
"%g * FLT_MODEL_SCALE;\n",
locator.cells[i].name, j,
locator.cells[i].objects[j]->bbox.min[1]);
fprintf(dotc, "\t%s_object_array[%d]->bbox.min[2]= "
"%g * FLT_MODEL_SCALE;\n",
locator.cells[i].name, j,
locator.cells[i].objects[j]->bbox.min[2]);
fprintf(dotc, "\t%s_object_array[%d]->bbox.max[0]= "
"%g * FLT_MODEL_SCALE;\n",
locator.cells[i].name, j,
locator.cells[i].objects[j]->bbox.max[0]);
fprintf(dotc, "\t%s_object_array[%d]->bbox.max[1]= "
"%g * FLT_MODEL_SCALE;\n",
locator.cells[i].name, j,
locator.cells[i].objects[j]->bbox.max[1]);
fprintf(dotc, "\t%s_object_array[%d]->bbox.max[2]= "
"%g * FLT_MODEL_SCALE;\n",
locator.cells[i].name, j,
locator.cells[i].objects[j]->bbox.max[2]);
}
}
fprintf(dotc, "\n}\t\t/* End of function upInit() */\n");
fclose(dotc);
fclose(doth);
}
bead loadtree (void)
{
int opcode, len, reallen, i;
struct opBead *opBead, *opNext;
bead *child, root;
if (ptr >= end) {
return NULL;
}
opBead = (struct opBead *)ptr;
opcode = opBead->opcode;
len = opBead->length;
if (opcode == MG_POP) {
ptr += len; /* Skip pop */
return NULL;
}
root = (bead)malloc (sizeof (struct bead));
root->opcode = opcode;
if (opcode == MG_VTXTABLE) {
reallen = ((struct opVertexTable *)ptr)->lengthvert;
} else {
reallen = len;
}
/*
* It's necessary to malloc() and memcpy() here instead of just
* saying root->data = ptr because we want to make sure that
* p->data is double-word aligned, otherwise references to doubles
* will bus-error. The malloc() will definitely be double-word
* aligned.
*/
root->data = malloc (reallen);
memcpy (root->data, ptr, reallen);
root->child = NULL;
root->sibling = NULL;
ptr += reallen; /* Skip bead */
opNext = (struct opBead *)ptr;
if (opNext->opcode == MG_PUSH) {
ptr += opNext->length; /* Skip push */
child = &root->child;
while ((*child = loadtree ()) != NULL) {
child = &(*child)->sibling;
}
}
return root;
}
/*
* Function: fixtree
* Description: A terrible hack. The way the rest of the code is set up,
* if a group is followed by a comment which is followed by
* a push, the stuff after the push will be the child of the
* comment rather than the child of the group. Rather than
* rework the rest of the code, fixtree() just traverses the
* tree looking for this sort of situation. The correct thing to
* to would be to revamp loadtree() and the data structures of
* the whole program, but this will do the job for now.
*/
void fixtree(bead ptr)
{
struct opBead *thisbead = (struct opBead *) ptr->data;
bead next;
struct opBead *opNext;
if (ptr == NULL)
{
return;
}
next = ptr->sibling;
if ((thisbead->opcode == MG_GROUP || thisbead->opcode == MG_POLYGON)
&& ptr->child == NULL)
{
while (1)
{
if (next == NULL)
{
fprintf(stderr, "Warning: found an empty group or polygon\n");
break;
}
opNext = (struct opBead *) next->data;
if (opNext->opcode != MG_COMMENT && opNext->opcode != MG_BBOX &&
opNext->opcode != MG_LONGID)
{
fprintf(stderr, "Warning: unexpected opcode %s after"
"childless group or poly\n",
opcode_name[opNext->opcode]);
break;
}
if (next->child != NULL)
{
ptr->child = next->child;
next->child = NULL;
break;
}
next = next->sibling;
}
}
if (ptr->child != NULL)
{
fixtree(ptr->child);
}
if (ptr->sibling != NULL)
{
fixtree(ptr->sibling);
}
}
void printtree (bead ptr, int level)
{
int opcode, len, i;
if (ptr == NULL) {
return;
}
for (i = 0; i < level; i++) {
printf (" ");
}
opcode = ptr->opcode;
printf ("/* %s (%d) */\n", opcode_name[opcode], opcode);
printtree (ptr->child, level+1);
printtree (ptr->sibling, level);
}
/*
* Load a file into memory and parse it into the tree.
*/
void loadfile (char *fn)
{
bead *child;
int size, inf;
inf = open (fn, O_RDONLY);
if (inf == -1) {
perror (fn);
exit (1);
}
size = lseek (inf, 0, SEEK_END);
lseek (inf, 0, SEEK_SET);
buf = (uchar *)malloc (size);
read (inf, buf, size);
close (inf);
end = buf + size;
child = &root;
ptr = buf;
while ((*child = loadtree ()) != NULL) {
child = &(*child)->sibling;
}
/*
* Patch up the tree
*/
fixtree(root);
}
static double min_x, max_x;
static double min_y, max_y;
static double min_z, max_z;
void light_vertex( struct opVertexNUV *, int );
void print_vertex( struct opVertexNUV *, int, double );
/* MPG - global for per vertex colors */
int perVertColors = 0;
int doHiFreqTex = 0;
void printvtxtable(void)
{
int i;
double r;
/* MPG - need distinct vertex names */
fprintf (dothFilePointer, "static Vtx v_%s[] = \{\n", model_name);
fprintf (dothFilePointer, "/* %d vertices */ \n", vtxcount);
for (i=0; i<vtxcount; i++)
{
if( light_num > 0 ) {
struct opVertexNUV TmpVert;
TmpVert = VertexTable[i];
/* MPG - don't want lighting for radiosity texture or per vertex color
light_vertex( &TmpVert, TmpVert.opcode );
*/
print_vertex( &TmpVert, i, 255.0 );
} else
print_vertex( &(VertexTable[i]), i, 127.0 );
}
fprintf (dothFilePointer, "};\n\n");
r = max_x - min_x;
r = MAX( r, max_y - min_y );
r = MAX( r, max_z - min_z );
fprintf (dothFilePointer,
"/* Center and max_scale with args: -c %g %g %g -s %g */\n\n",
(max_x + min_x)/2.0,
(max_y + min_y)/2.0,
(max_z + min_z)/2.0,
0xffff/r );
fprintf( stderr,"Center and max_scale with args: -c %g %g %g -s %g\n",
(max_x + min_x)/2.0,
(max_y + min_y)/2.0,
(max_z + min_z)/2.0,
0xffff/r );
}
int
fix_u( double fu , int tx)
{
int i;
if (full_range)
i = (fu + 1.0) * (tx>>1) + 0.5;
else
i = fu * (tx) + 0.5;
if( i < 0 )
i = 0;
else if( i > (tx-1) )
i = tx-1;
return( i );
}
int
fix_v( double fv , int ty)
{
int i;
if (full_range)
i = ty - (fv + 1.0) * (ty>>1) + 0.5;
else
i = ((ty) - (fv * (ty)) + 0.5);
if( i < 0 )
i = 0;
else if( i > (ty-1) )
i = ty-1;
return( i );
}
void
light_vertex( struct opVertexNUV *vp, int mat_id )
{
double r,g,b;
double norm;
double diffuse_fact;
double specular_fact;
double sqrt(double);
double pow(double, double);
double spec_x;
double spec_y;
double spec_z;
double snorm;
int i;
Material TempMaterial;
#ifdef DEBUG
fprintf(stderr," Using Mat %d\n", mat_id );
#endif
if( mat_id != MATERIAL_NONE )
TempMaterial.m = mat_saved[mat_id];
else
TempMaterial.m = mat_default;
norm = vp->nx * vp->nx +
vp->ny * vp->ny +
vp->nz * vp->nz;
norm = sqrt(norm);
r = TempMaterial.m.amb[0];
g = TempMaterial.m.amb[1];
b = TempMaterial.m.amb[2];
for(i=0; i<light_num; i++) {
diffuse_fact = vp->nx * lights[i].x +
vp->ny * lights[i].y +
vp->nz * lights[i].z;
diffuse_fact /= norm;
if( diffuse_fact < 0 )
continue;
r +=diffuse_fact * TempMaterial.m.diff[0] * lights[i].nx;
g +=diffuse_fact * TempMaterial.m.diff[1] * lights[i].ny;
b +=diffuse_fact * TempMaterial.m.diff[2] * lights[i].nz;
if( specular_flag == 0 )
continue;
spec_x = eye.x + lights[i].x;
spec_y = eye.y + lights[i].y;
spec_z = eye.z + lights[i].z;
snorm = sqrt(spec_x * spec_x + spec_y * spec_y + spec_z * spec_z);
if( snorm < 0.01 ) /* eye is almost exactly opposite this light */
continue;
specular_fact= vp->nx * spec_x +
vp->ny * spec_y +
vp->nz * spec_z;
if( specular_fact < 0 )
continue;
specular_fact /= (snorm * norm);
specular_fact =pow( specular_fact, (double)TempMaterial.m.shiny);
r +=specular_fact*TempMaterial.m.spec[0] * lights[i].nx;
g +=specular_fact*TempMaterial.m.spec[1] * lights[i].ny;
b +=specular_fact*TempMaterial.m.spec[2] * lights[i].nz;
};
r = MIN( r, 255.0 );
g = MIN( g, 255.0 );
b = MIN( b, 255.0 );
vp->nx = r/255.0;
vp->ny = g/255.0;
vp->nz = b/255.0;
}
void
print_vertex( struct opVertexNUV *vp, int i, double nv_scale )
{
static int first=1;
double x, y, z;
int nx, ny, nz;
int a, b, c, d, s, t;
x = vp->x - center.x;
y = vp->y - center.y;
z = vp->z - center.z;
if( first ) {
max_x = min_x = x;
max_y = min_y = y;
max_z = min_z = z;
first = 0;
};
min_x = MIN( min_x, x );
min_y = MIN( min_y, y );
min_z = MIN( min_z, z );
max_x = MAX( max_x, x );
max_y = MAX( max_y, y );
max_z = MAX( max_z, z );
nx = (int) (x * obscale + 0.5);
ny = (int) (y * obscale + 0.5);
nz = (int) (z * obscale + 0.5);
if( (nx > 0x7fff) || (ny > 0x7fff) || (nz > 0x7fff) ||
(nx < -0x7fff) || (ny < -0x7fff) || (nz < -0x7fff) ) {
fprintf( stderr, "Point(%d) out of range (%g, %g,%g) => (%d,%d,%d)\n",
i, vp->x, vp->y, vp->z, nx, ny, nz );
};
if (doHiFreqTex)
{
s = 8*(fix_u(vp->u, vp->tx)); /* Flip S for PR */
t = 16*(fix_v(vp->v, vp->ty)); /* Flip T for PR */
}
else
{
#ifdef SCALE_TEX
s = (fix_u(vp->u, 8*(vp->tx-1))); /* Flip S for PR */
t = (fix_v(vp->v, 16*(vp->ty-1))); /* Flip T for PR */
#else
s = fix_u(vp->u, vp->tx);
t = fix_v(vp->v, vp->ty); /* Flip T for PR */
#endif
}
if (output_normals) {
a = (int)(vp->nx*127); /* ranges from -127 to 127 */
b = (int)(vp->ny*127); /* ranges from -127 to 127 */
c = (int)(vp->nz*127); /* ranges from -127 to 127 */
} else {
a = (int)vp->nx;
b = (int)vp->ny;
c = (int)vp->nz;
}
fprintf(dothFilePointer, "\t\{ %d, %d, %d, 0, %d<<5, %d<<5, %d, %d, %d, %d \}, /\* v%d \*/ \n",
nx,
ny,
nz,
s, t,
a, b, c,
/*
* Colors, so 0 -> 0 and 1.0 -> 255
*
* Need to scale the normals to be signed chars,
* so -1.0 -> -128 and 1.0 -> 127
*/
255, i);
}
void setupvtxtable (uchar *ptr, uchar *end)
{
struct opBead *d;
int opcode, len;
double umin=1000.0,umax=-1000.0,vmin=1000.0,vmax=-1000.0;
while (ptr < end) {
opcode = ((struct opBead *)ptr)->opcode;
len = ((struct opBead *)ptr)->length;
if (vtxcount == VertexTableMallocCount)
{
VertexTableMallocCount += VERTEXTABLEMALLOCINCREMENT;
VertexTable =
realloc(VertexTable,
VertexTableMallocCount * sizeof(struct opVertexNUV));
}
VertexTable[vtxcount].opcode = MATERIAL_NONE;
VertexTable[vtxcount].length = MATERIAL_NONE;
VertexTable[vtxcount].color = MATERIAL_NONE;
VertexTable[vtxcount].flags = MATERIAL_NONE;
if (opcode == MG_DVERT_ABS) {
struct opVertex *d = (struct opVertex *)ptr;
VertexTable[vtxcount].x = d->x;
VertexTable[vtxcount].y = d->y;
VertexTable[vtxcount].z = d->z;
VertexTable[vtxcount].u = 0;
VertexTable[vtxcount].v = 0;
VertexTable[vtxcount].nx = 0;
VertexTable[vtxcount].ny = 0;
VertexTable[vtxcount].nz = 0;
vtx[vtxoffset] = vtxcount++;
vtxoffset += len;
} else if (opcode == MG_DVERT_NORM) {
struct opVertexN *d = (struct opVertexN *)ptr;
float NormalLength;
VertexTable[vtxcount].x = d->x;
VertexTable[vtxcount].y = d->y;
VertexTable[vtxcount].z = d->z;
VertexTable[vtxcount].u = 0;
VertexTable[vtxcount].v = 0;
NormalLength =
fsqrt(d->nx * d->nx + d->ny * d->ny + d->nz * d->nz);
VertexTable[vtxcount].nx = d->nx / NormalLength;
VertexTable[vtxcount].ny = d->ny / NormalLength;
VertexTable[vtxcount].nz = d->nz / NormalLength;
vtx[vtxoffset] = vtxcount++;
vtxoffset += len;
} else if (opcode == MG_DVERT_ABS_TEX) {
struct opVertexUV *d = (struct opVertexUV *)ptr;
VertexTable[vtxcount].x = d->x;
VertexTable[vtxcount].y = d->y;
VertexTable[vtxcount].z = d->z;
VertexTable[vtxcount].u = d->u;
VertexTable[vtxcount].v = d->v;
VertexTable[vtxcount].nx = 0;
VertexTable[vtxcount].ny = 0;
VertexTable[vtxcount].nz = 0;
vtx[vtxoffset] = vtxcount++;
vtxoffset += len;
} else if (opcode == MG_DVERT_NORM_TEX) {
struct opVertexNUV *d = (struct opVertexNUV *)ptr;
float NormalLength;
#if TRUNCTEX
if (d->u > 1) {
d->u = 1;
}
if (d->v > 1) {
d->v = 1;
}
if (d->u < 0) {
d->u = 0;
}
if (d->v < 0) {
d->v = 0;
}
#endif
VertexTable[vtxcount].x = d->x;
VertexTable[vtxcount].y = d->y;
VertexTable[vtxcount].z = d->z;
VertexTable[vtxcount].u = d->u;
VertexTable[vtxcount].v = d->v;
/*#################*/
umin = MIN(d->u,umin);
vmin = MIN(d->v,vmin);
umax = MAX(d->u,umax);
vmax = MAX(d->v,vmax);
/*#################*/
NormalLength =
fsqrt(d->nx * d->nx + d->ny * d->ny + d->nz * d->nz);
VertexTable[vtxcount].nx = d->nx / NormalLength;
VertexTable[vtxcount].ny = d->ny / NormalLength;
VertexTable[vtxcount].nz = d->nz / NormalLength;
vtx[vtxoffset] = vtxcount++;
vtxoffset += len;
} else {
fprintf (stderr, "setupvtxtable(): unknown vertex %d\n",
opcode);
exit (1);
}
ptr += len;
}
/*#################*/
fprintf(stderr,"umin=%lf, umax=%lf\n",umin,umax);
fprintf(stderr,"vmin=%lf, vmax=%lf\n",vmin,vmax);
/*#################*/
}
/*
* Function: lookahead
* Description: looks at a bead's siblings for comment and bbox nodes
* that modify the bead in question.
* Arguments: ptr - the bead which might be modified
* c - where to put the pointer to the comment bead, or NULL if
* no comment is found before the next 'real' bead.
* b - where to put the pointer to the bbox bead, or NULL if
* no bbox is found before the next 'real' bead.
*/
void lookahead(bead ptr, struct opComment **c, struct opBox **b)
{
*c = (struct opComment *) NULL;
*b = (struct opBox *) NULL;
while (1)
{
ptr = ptr->sibling;
if (ptr == NULL) { return; }
switch (ptr->opcode)
{
case MG_COMMENT:
*c = (struct opComment *) ptr->data;
break;
case MG_LONGID:
break;
case MG_MATRIX:
break;
case MG_REPLICATE:
break;
case MG_BBOX:
*b = (struct opBox *) ptr->data;
break;
case 42:
case 43:
case 44:
case 45:
case 46:
case 47:
case 48:
case 76:
case 77:
case 78:
case 79:
case 80:
case 81:
case 82:
case 94:
break;
default:
return;
}
}
}
int mat_id = -1;
/* MPG - global for per vertex color */
col3 *vcolors=NULL;
FILE *hftfile=NULL;
typedef struct td {
char name[64];
} TexInfo;
#define MAX_HIFREQTEX 32
TexInfo tlist[MAX_HIFREQTEX];
int numHifreq=0;
/*
* Function: parsetree
* Description: recursively parses the tree in two passes. First pass
* establishes texture stuff, second establishes geometry.
* Arguments: p - the current bead
* pass - which pass is currently being done (0 or 1)
* currcell - the current cell to which portals are assigned
*/
int parsetree (bead p, int pass, upCellData *currcell)
{
static int polynum, level = 0;
int opcode, i, numinst;
char texfilename[64];
if (p == NULL) {
return 0;
}
if (hftfile == NULL)
{
if ((hftfile = fopen("hifreqtex.h", "w")) == NULL)
{
fprintf(stderr, "ERROR: failed to open high frequency texture file\n");
exit(0);
}
}
numinst = 0;
opcode = p->opcode;
if ((pass == 0) && ((tempSeqCount == 0)&&(opcode == MG_TEXTURE_REF))) {
printf ("/\* %s (%d) \*/\n", opcode_name[opcode], opcode);
}
switch (opcode) {
case MG_HEADER: {
parsetree (p->child, pass, currcell);
break;
}
case MG_COMMENT: {
struct opComment *d = (struct opComment *)p->data;
uchar *s;
int col;
if (pass == 1) {
parsetree (p->child, pass, currcell);
break;
}
col = 0;
printf ("/\*\n");
printf (" * Comment:\n");
for (s = d->comment; *s; s++) {
if (col == 0) {
printf (" * ");
}
if ((col > 65 && *s == ' ') || *s == '\n') {
printf ("\n");
col = 0;
} else {
printf ("%c", *s);
col++;
}
}
if (col != 0) {
printf ("\n");
}
printf (" \*/\n\n");
parsetree (p->child, pass, currcell);
break;
}
case MG_COLOR_TABLE: {
/* *d = (struct opColor *)p->data; */
ColorTable = *(struct opColor *)p->data;
if (pass == 1) {
parsetree (p->child, pass, currcell);
break;
}
printf ("#if 0\n");
for (i = 0; i < 32; i++) {
printf ("\t%d = %d %d %d\n", i,
ColorTable.brightest[i].red,
ColorTable.brightest[i].green,
ColorTable.brightest[i].blue);
}
for (i = 0; i < 56; i++) {
printf ("\t%d = %d %d %d\n", i,
ColorTable.fixed[i].red,
ColorTable.fixed[i].green,
ColorTable.fixed[i].blue);
}
printf ("#endif\n\n");
parsetree (p->child, pass, currcell);
break;
}
case MG_MATERIAL_TABLE: {
/* struct opMaterial *d = (struct opMaterial *)p->data; */
MaterialTable = *(struct opMaterial *)p->data;
if (pass == 1) {
parsetree (p->child, pass, currcell);
break;
}
printf ("#if 0\n");
for (i = 0; i < 64; i++) {
printf ("%d: %.2f %.2f %.2f, %.2f %.2f %.2f, %.2f %.2f %.2f, %.2f %.2f %.2f, %.2f %.2f, %d\n",
i,
MaterialTable.mat[i].ambient.red,
MaterialTable.mat[i].ambient.green,
MaterialTable.mat[i].ambient.blue,
MaterialTable.mat[i].diffuse.red,
MaterialTable.mat[i].diffuse.green,
MaterialTable.mat[i].diffuse.blue,
MaterialTable.mat[i].specular.red,
MaterialTable.mat[i].specular.green,
MaterialTable.mat[i].specular.blue,
MaterialTable.mat[i].emissive.red,
MaterialTable.mat[i].emissive.green,
MaterialTable.mat[i].emissive.blue,
MaterialTable.mat[i].shininess,
MaterialTable.mat[i].alpha,
MaterialTable.mat[i].flags & 1);
}
printf ("#endif\n\n");
parsetree (p->child, pass, currcell);
break;
}
case MG_TEXTURE_REF: {
struct opTexture *d = (struct opTexture *)p->data;
uchar *fn;
char path[256], texfilename[64], texfilenum[12], tstring[64];
char *lastpart;
char tempname[64];
struct texture ttex;
int i, output;
if (pass == 1) {
parsetree (p->child, pass, currcell);
break;
}
if (tempSeqCount == 0)
{
printf ("/\*\n");
printf (" * Texture:\n");
printf (" * name = %s\n", d->name);
printf (" * index = %d\n", d->index);
printf (" * x = %d\n", d->x);
printf (" * y = %d\n", d->y);
}
/* For now, strip path: */
fn = strrchr (d->name, '/');
if (fn == NULL) {
fn = d->name;
} else {
fn++;
}
strcpy (path, "Textures/");
strcat (path, fn);
tex[numtex].index = d->index;
sprintf (tex[numtex].name, "tex_%d", d->index);
if (tempSeqCount)
output = NONE;
else
output = C;
if (readtex (path, &tex[numtex], tex_fmt, tex_siz, 1,
0, 0, 0, 255, 255, 255, output, tex_flags)) {
numtex++;
}
/* do temporal illumination texture files if necessary */
if (tempSeqCount > 2)
{
ttex.index = tex[numtex-1].index;
ttex.width = tex[numtex-1].width;
ttex.height = tex[numtex-1].height;
ttex.fmt = tex[numtex-1].fmt;
ttex.siz = tex[numtex-1].siz;
/* textures should be named with a # indicating the */
/* temporal sequence number */
strcpy(texfilename, fn);
strcpy(tstring, fn);
strtok(texfilename, "_");
strtok(tstring, "_");
lastpart = strtok(NULL, "_");
/* number (always 1) is right before "_" */
texfilename[strlen(texfilename)-1] = NULL;
strcpy(tempname, "Textures/");
strcat(tempname, texfilename);
strcpy(texfilename, tempname);
for (i=0; i<tempSeqCount; i++)
{
strcpy(ttex.name, tex[numtex-1].name);
strcat(ttex.name, "_");
strcpy(texfilename, tempname);
sprintf(texfilenum, "%d", i);
strcat(ttex.name, texfilenum);
strcat(texfilename, texfilenum);
strcat(texfilename, "_");
strcat(texfilename, lastpart);
readtexFile(ttexfile[i], texfilename, &ttex,
tex_fmt, tex_siz, 0, 0, 0, 0, 255, 255, 255, C,
tex_flags);
}
strcpy(texfilename, fn);
strcpy(tstring, fn);
strtok(texfilename, "_");
strtok(tstring, "_");
lastpart = strtok(NULL, "_");
/* number (always 1) is right before "_" */
texfilename[strlen(texfilename)-1] = NULL;
strcpy(tempname, "Textures/");
strcat(tempname, texfilename);
strcpy(texfilename, tempname);
sprintf (tex[numtex].name, "pttex_%d", d->index);
for (i=0; i<tempSeqCount-1; i++)
{
strcpy(ttex.name, "pt");
strcat(ttex.name, tex[numtex-1].name);
strcat(ttex.name, "_");
strcpy(texfilename, tempname);
sprintf(texfilenum, "%d", i);
strcat(ttex.name, texfilenum);
strcat(texfilename, texfilenum);
strcat(texfilename, "_");
strcat(texfilename, lastpart);
readtexFile(pttexfile[i], texfilename, &ttex,
tex_fmt, tex_siz, 0, 0, 0, 0, 255, 255, 255, C,
tex_flags);
}
}
else if (tempSeqCount == 2)
{
strcpy(texfilename, fn);
strcpy(tstring, fn);
strtok(texfilename, "_");
strtok(tstring, "_");
lastpart = strtok(NULL, "_");
/* number (always 1) is right before "_" */
texfilename[strlen(texfilename)-1] = NULL;
strcpy(tempname, "Textures/");
strcat(tempname, texfilename);
strcpy(texfilename, tempname);
sprintf (tex[numtex].name, "pttex_%d", d->index);
for (i=0; i<tempSeqCount; i++)
{
strcpy(ttex.name, "pt");
strcat(ttex.name, tex[numtex-1].name);
strcat(ttex.name, "_");
strcpy(texfilename, tempname);
sprintf(texfilenum, "%d", i);
strcat(ttex.name, texfilenum);
strcat(texfilename, texfilenum);
strcat(texfilename, "_");
strcat(texfilename, lastpart);
readtexFile(pttexfile[i], texfilename, &ttex,
tex_fmt, tex_siz, 0, 0, 0, 0, 255, 255, 255, C,
tex_flags);
}
}
parsetree (p->child, pass, currcell);
break;
}
case MG_VTXTABLE: {
struct opVertexTable *d =
(struct opVertexTable *)p->data;
uchar *ptr, *end;
if (pass == 1) {
parsetree (p->child, pass, currcell);
break;
}
vtx = (ulong *)malloc (d->lengthvert * sizeof (ulong));
vtxcount = 0;
vtxoffset = d->length;
ptr = (uchar *)p->data + d->length;
end = (uchar *)p->data + d->lengthvert;
setupvtxtable (ptr, end);
/* Child of VTXTABLE is usually geometry: */
parsetree (p->child, pass, currcell);
break;
}
case MG_PUSH_SF: {
subface = 1;
parsetree (p->child, pass, currcell);
break;
}
case MG_POP_SF: {
subface = 0;
parsetree (p->child, pass, currcell);
break;
}
case MG_GROUP: {
struct opGroup *d = (struct opGroup *)p->data;
/*
* check comment field to see if this is a cell
*/
struct opComment *com = NULL;
struct opBox *bbox = NULL;
int i, cellnum = -1, iscell = 0;
char cellName[UP_CELLNL];
lookahead(p, &com, &bbox);
if (doportals && com != NULL &&
sscanf(com->comment, "CELL %s", cellName) == 1)
{
iscell = 1;
if (pass == 0)
{
/*
* It's a cell. Sanity checks:
* -- does this cell already exist?
* -- does it have an associated bounding box?
* -- does this cell occur within another cell?
* -- have we exceeded the maximum number of cells?
*/
if (bbox == NULL)
{
fprintf(stderr, "Error: cell %s has no bounding box\n",
cellName);
exit(1);
}
if (locator.numcells + 1 == UP_MAXCELLS)
{
fprintf(stderr, "Error: too many cells (%s is # %d)\n",
com->comment, locator.numcells);
exit(1);
}
for (i=0;i<locator.numcells;i++)
{
if (strcmp(locator.cells[i].name, cellName) == 0)
{
cellnum = i;
break;
}
}
if (cellnum == -1)
{
cellnum = locator.numcells;
strcpy(locator.cells[cellnum].name, cellName);
locator.numcells++;
}
locator.cells[cellnum].bbox.min[0] = bbox->xmin;
locator.cells[cellnum].bbox.min[1] = bbox->ymin;
locator.cells[cellnum].bbox.min[2] = bbox->zmin;
locator.cells[cellnum].bbox.max[0] = bbox->xmax;
locator.cells[cellnum].bbox.max[1] = bbox->ymax;
locator.cells[cellnum].bbox.max[2] = bbox->zmax;
currcell = &locator.cells[cellnum];
}
}
if (pass == 1)
{
if (iscell)
{
printf("\tgsSPDisplayList (cell_%s),\n", cellName);
}
else
{
/*
* check for a bounding box to see if this is a 'detail
* object' of the cell. Detail objects will be culled to
* portal boundaries.
*/
upObjectData *ob;
if (!doportals || bbox == NULL || currcell == NULL)
{
/*
* can't attach object to currcell, so just execute it
*/
printf ("\tgsSPDisplayList (grp_%s),\n", d->id);
numinst++;
}
else
{
/*
* Add the object. currcell->objects is inited to
* NULL, so realloc acts like malloc if
* numportal == 0
*/
int numo = currcell->numobjects;
currcell->objects = (upObjectData **)
realloc(currcell->objects,
(numo+1) * sizeof(upObjectData*));
/*
* Fill in the object structure
*/
ob = (upObjectData *) malloc(sizeof(upObjectData));
ob->bbox.min[0] = bbox->xmin;
ob->bbox.min[1] = bbox->ymin;
ob->bbox.min[2] = bbox->zmin;
ob->bbox.max[0] = bbox->xmax;
ob->bbox.max[1] = bbox->ymax;
ob->bbox.max[2] = bbox->zmax;
strncpy(ob->name, d->id, UP_OBNL);
currcell->objects[numo] = ob;
currcell->numobjects++;
}
}
numinst++;
break;
}
parsetree (p->child, 0, currcell);
if (iscell) /* its a cell */
{
printf("/\*\n");
printf(" * Cell:\n");
printf(" * Number = %d\n", cellnum);
printf(" * Name = %s\n", cellName);
printf(" * Ident = cell_%s\n", cellName);
printf(" * Min = [%f %f %f]\n",
locator.cells[cellnum].bbox.min[0],
locator.cells[cellnum].bbox.min[1],
locator.cells[cellnum].bbox.min[2]);
printf(" * Max = [%f %f %f]\n",
locator.cells[cellnum].bbox.max[0],
locator.cells[cellnum].bbox.max[1],
locator.cells[cellnum].bbox.max[2]);
printf(" \*/\n\n");
printf ("Gfx cell_%s[] = {\n", cellName); /* not static! */
p->numinst = parsetree (p->child, 1, currcell);
printf ("\tgsSPEndDisplayList(),\n");
}
else if (doportals && bbox != NULL && currcell != NULL)
{ /* its a detail obj */
printf ("/\*\n");
printf (" * Detail Object:\n");
printf (" * Name = %s\n", d->id);
printf (" * Ident = %s_detailobj_%s\n", model_name, d->id);
printf (" \*/\n\n");
printf ("Gfx %s_detailobj_%s[] = {\n", /* not static! */
model_name, d->id);
p->numinst = parsetree (p->child, 1, currcell);
printf ("\tgsSPEndDisplayList(),\n");
}
else /* its a vanilla grp */
{
printf ("/\*\n");
printf (" * Group:\n");
printf (" * Id = %s\n", d->id);
printf (" * Flags = 0x%x\n", d->flags);
printf (" * Priority = %d\n", d->priority);
printf (" * Special effect 1 = %d\n", d->effect1);
printf (" * Special effect 2 = %d\n", d->effect2);
printf (" * Significance = %d\n", d->significance);
printf (" \*/\n\n");
printf ("static Gfx grp_%s[] = {\n", d->id);
p->numinst = parsetree (p->child, 1, currcell);
printf ("\tgsSPEndDisplayList(),\n");
}
numinst++;
printf ("};\n\n");
break;
}
case MG_LODFLOAT: {
struct opLODFloat *d = (struct opLODFloat *)p->data;
if (pass == 1) {
printf ("\tgsSPDisplayList (lod_%s),\n",
d->id);
numinst++;
break;
}
parsetree (p->child, 0, currcell);
printf ("/\*\n");
printf (" * LOD Float:\n");
printf (" * Id = %s\n", d->id);
printf (" * Switch in dist = %g\n", d->switchin);
printf (" * Switch out dist = %g\n", d->switchout);
printf (" * Special effect 1 = %d\n", d->effect1);
printf (" * Special effect 2 = %d\n", d->effect2);
printf (" * Flags = 0x%x\n", d->flags);
printf (" * Center = %g %g %g\n", d->x, d->y, d->z);
printf (" * Transition range = %g\n", d->morph);
printf (" \*/\n\n");
printf ("static Gfx lod_%s[] = {\n", d->id);
p->numinst = parsetree (p->child, 1, currcell);
printf ("\tgsSPEndDisplayList(),\n");
numinst++;
printf ("};\n\n");
break;
}
case MG_OBJECT: {
struct opObject *d = (struct opObject *)p->data;
if (pass == 1)
{
printf ("\tgsSPDisplayList (obj_%s),\n", d->id);
numinst++;
break;
}
printf ("/\*\n");
printf (" * Object:\n");
printf (" * Id = %s\n", d->id);
printf (" * Flags = 0x%x\n", d->flags);
printf (" * Priority = %d\n", d->priority);
printf (" * Transparency = %d\n", d->transparency);
printf (" * Special effect 1 = %d\n", d->effect1);
printf (" * Special effect 2 = %d\n", d->effect2);
printf (" * Significance = %d\n", d->significance);
printf (" \*/\n\n");
polynum = 0;
printf ("static Gfx obj_%s[] = {\n", d->id);
/* XXX - possibly remove this: */
if (use_modulateidecala)
printf ("\tgsDPSetCombineMode(G_CC_MODULATEIDECALA, G_CC_MODULATEIDECALA),\n");
else
printf ("\tgsDPSetCombineMode(G_CC_SHADE, G_CC_SHADE),\n");
numinst += parsetree (p->child, 0, currcell) + 1;
printf ("\tgsSPEndDisplayList(),\n");
numinst++;
printf ("};\n\n");
p->numinst = numinst;
break;
}
case MG_POLYGON: {
struct opPolygon *d = (struct opPolygon *)p->data;
static ushort texture = (ushort)-1;
int t;
/*
* Check comment field for per vertex color, hifreq texture,
* and portal tags
*/
int nverts, nump;
char *texname;
char tempstring[64];
struct opComment *com;
struct opBox *bbox;
char cellName[UP_CELLNL];
perVertColors = 0;
doHiFreqTex = 0;
lookahead(p, &com, &bbox);
/*
* Is it a portal?
*/
if (doportals && com != NULL &&
sscanf(com->comment,"PORTAL %s",cellName) == 1)
{
int i, cellnum = -1;
upPortalData *newportal;
/*
* Does the cell this portal references alrady exist?
* If not, create it.
*/
for (i=0;i<locator.numcells;i++)
{
if (strcmp(locator.cells[i].name, cellName) == 0)
{
cellnum = i;
break;
}
}
if (cellnum == -1)
{
cellnum = locator.numcells;
strcpy(locator.cells[cellnum].name, cellName);
locator.numcells++;
}
/*
* Add the portal. Portals is inited to NULL, so
* realloc acts like malloc if numportal == 0
*/
nump = currcell->numportals;
currcell->portals =
(upPortalData **) realloc(currcell->portals,
(nump+1) * sizeof(upPortalData*));
newportal = upAddPortal(p, currcell, &locator.cells[cellnum]);
if (newportal != NULL)
{
currcell->portals[nump] = newportal;
currcell->numportals++;
}
break;
}
/*
* It's not a portal. Does it have per-vertex color
* specified in the comment field?
*/
if (com != NULL &&
sscanf(com->comment, "VCOLOR %d", &nverts) == 1)
{
perVertColors = 1;
vcolors = (col3 *)calloc(nverts, sizeof(col3));
if (nverts == 3)
{
if (10 != sscanf(com->comment,
"VCOLOR %d %d %d %d %d %d %d %d %d %d",
&nverts,
&(vcolors[0][0]),&(vcolors[0][1]),&(vcolors[0][2]),
&(vcolors[1][0]),&(vcolors[1][1]),&(vcolors[1][2]),
&(vcolors[2][0]),&(vcolors[2][1]),&(vcolors[2][2])))
{
fprintf(stderr, "ERROR: can't read 3 vert colors\n");
free(vcolors);
vcolors = NULL;
break;
}
}
else if (nverts == 4)
{
if (13 != sscanf(com->comment,
"VCOLOR %d %d %d %d %d %d %d %d %d %d %d %d %d",
&nverts,
&(vcolors[0][0]),&(vcolors[0][1]),&(vcolors[0][2]),
&(vcolors[1][0]),&(vcolors[1][1]),&(vcolors[1][2]),
&(vcolors[2][0]),&(vcolors[2][1]),&(vcolors[2][2]),
&(vcolors[3][0]),&(vcolors[3][1]),&(vcolors[3][2])))
{
fprintf(stderr, "ERROR: can't read 4 vert colors\n");
free(vcolors);
vcolors = NULL;
break;
}
}
else
{
fprintf(stderr, "ERROR: poly tagged with %d vert colors\n",
nverts);
fprintf(stderr, "Only 3 or 4 verts allowed (for now)\n");
free(vcolors);
vcolors = NULL;
break;
}
} /* end of per vertex color setup */
/*
* It doesn't have per-vertex color specified in the comment field.
* Is it tagged with a high-frequency texture?
*/
if (com != NULL &&
sscanf(com->comment, "HIFREQ %s", texfilename) == 1)
{
TexInfo *temptex, *tcurrent;
int found=0, i;
char tpath[72];
doHiFreqTex = 1;
texname = strtok(texfilename, ".");
/* see if texture is already in table */
for (i=0; i<numHifreq; i++)
{
if (!(strcmp(texname, tlist[i].name)))
found = 1;
}
if ((numHifreq == 0) || (found == 0))
{
struct texture tex;
strcpy(tlist[numHifreq++].name, texname);
strcpy(tex.name, texname);
/* write the texture to the hifreq.h file */
sprintf(tpath, "Textures/");
strcat(tpath, texfilename);
strcat(tpath, ".rgb");
#define IMG_SIZE 32
if (!(readtexFile(hftfile, tpath, &tex, RGBA,
IMG_SIZE, 1,
0, 0, 0, 255, 255, 255, C, 1)))
fprintf(stderr, "ERROR: couldn't readtexFile\n");
}
}
/* end of high-frequency texture setup */
polynum++;
printf ("\t/* Polygon #%d (%s) */\n", polynum, d->id);
if (d->detail != (ushort)-1) {
printf ("\t/* Detail = %d */\n", (int)d->detail);
}
if (d->texture != (ushort)-1) {
printf ("\t/* Texture = %d */\n",
(int)d->texture);
}
if (texture_flag && (d->texture != texture)) {
t = -1;
if (d->texture != (ushort)-1) {
for (t = 0; t < numtex; t++) {
if (tex[t].index == d->texture) {
break;
}
}
if (t == numtex) {
fprintf (stderr,
"ERROR: Texture %d"
" not found.\n",
d->texture);
exit (1);
}
printf ("\tgsSPTexture (0xffff, 0xffff, 0, G_TX_RENDERTILE, G_ON),\n");
/* XXX do the modulate with respect to component */
printf ("\tgsDPPipeSync(),\n");
if (doHiFreqTex)
{
printf("\tgsDPSetCycleType(G_CYC_2CYCLE),\n");
printf("\tgsDPSetCombineMode(G_CC_INTERFERENCE, G_CC_PASS2),\n");
printf("\tgsDPSetRenderMode(G_RM_PASS, G_RM_AA_ZB_OPA_TERR2),\n");
printf("\tgsDPPipeSync(),\n");
}
else if (tempSeqCount)
{
printf("\tgsDPSetCycleType(G_CYC_2CYCLE),\n");
printf("\tgsDPSetCombineMode(G_CC_TEMPLERP, G_CC_PASS2),\n");
printf("\tgsDPSetRenderMode(G_RM_PASS, G_RM_AA_ZB_OPA_TERR2),\n");
printf("\tgsDPPipeSync(),\n");
}
else
{
if (render_mode == 1) {
if (subface == 0) render_current = render_normal;
if (subface == 1) render_current = render_decal;
printf ("\tgsDPSetRenderMode(%s, %s2),\n", render_current, render_current);
}
if (use_modulateidecala)
printf ("\tgsDPSetCombineMode(G_CC_MODULATEIDECALA, G_CC_MODULATEIDECALA),\n");
else
printf ("\tgsDPSetCombineMode(G_CC_MODULATE%s, G_CC_MODULATE%s),\n", cmbstr (tex[t].fmt), cmbstr (tex[t].fmt));
}
if (tempSeqCount)
{
strcpy(tempstring, "pt");
strcat(tempstring, tex[t].name);
strcat(tempstring, "_0");
}
else
strcpy(tempstring, tex[t].name);
printf ("\tgsDPLoadTextureBlock(%s, %s, %s,\n", tempstring, fmtstr (tex[t].fmt), sizstr (tex[t].siz));
printf ("\t\t%d, %d, 0,\n", tex[t].width, tex[t].height);
printf ("\t\tG_TX_CLAMP | G_TX_NOMIRROR, G_TX_CLAMP | G_TX_NOMIRROR,\n");
/* MPG - 2exp5 (32) is max width and height possible for a texture */
/* 3,4 are the 8, 16 -> see MG_VTXLIST */
#ifdef SCALE_TEX
if (tempSeqCount)
printf ("\t\tG_TX_NOMASK, G_TX_NOMASK, 3, 4),\n");
else if (doHiFreqTex)
printf ("\t\t5, 5, 3, 4),\n");
else
printf ("\t\t5, 5, 3, 4),\n");
#else
printf ("\t\tG_TX_NOMASK, G_TX_NOMASK, G_TX_NOLOD, G_TX_NOLOD),\n");
#endif
printf ("\tgsDPLoadSync(),\n");
numinst += 10;
} else {
printf ("\tgsSPTexture (0xffff, 0xffff, 0, G_TX_RENDERTILE, G_ON),\n");
printf ("\tgsDPSetCombineMode(G_CC_SHADE, G_CC_SHADE),\n");
numinst += 2;
}
if (doHiFreqTex)
{
#ifdef SCALE_TEX
int ts=20, t3=0, scales=4, scalet=4, num1=0, num2=0;
#else
int ts=16, t3=0, scales=4, scalet=4, num1=13, num2=12;
#endif
printf("\t_gsDPLoadTextureBlockTile(%s, (%d*%d)/4, 1, G_IM_FMT_RGBA,\n", texname, tex[t].width, tex[t].height);
printf("\t\tG_IM_SIZ_32b, %d, %d, %d,\n", ts, ts, t3);
printf("\t\tG_TX_WRAP | G_TX_NOMIRROR, G_TX_WRAP | G_TX_NOMIRROR,\n");
printf("\t\t%d, %d, %d, %d),\n", scales, scalet, num1, num2);
printf("\tgsDPLoadSync(),\n");
}
else if (tempSeqCount && (d->texture != (ushort)-1) )
{
int ts=16, t3=0;
if ((tex[t].width<=0)||(tex[t].height<=0))
{
fprintf(stderr,"ERROR: zero or negative value for dimensions of texture: %s -> tex[%d]\n", tex[t].name, t);
}
strcpy(tempstring, "pt");
strcat(tempstring, tex[t].name);
strcat(tempstring, "_1");
printf("\t_gsDPLoadTextureBlockTile(%s, (%d*%d)/4, 1, G_IM_FMT_RGBA,\n", tempstring, tex[t].width, tex[t].height);
printf("\t\tG_IM_SIZ_32b, %d, %d, %d,\n", tex[t].width, tex[t].height, t3);
printf("\t\tG_TX_WRAP | G_TX_NOMIRROR, G_TX_WRAP | G_TX_NOMIRROR,\n");
printf ("\t\tG_TX_NOMASK, G_TX_NOMASK, 3, 4),\n");
printf("\tgsDPLoadSync(),\n");
}
texture = d->texture;
texture_index = t;
}
if (d->color1 >= 4096)
{
FaceColor[0] = (unsigned char) ColorTable.fixed[d->color1-4096].red;
FaceColor[1] = (unsigned char) ColorTable.fixed[d->color1-4096].green;
FaceColor[2] = (unsigned char) ColorTable.fixed[d->color1-4096].blue;
}
else
{
int Intensity = (d->color1 & 0x7F);
int Index = (d->color1 >> 7);
FaceColor[0] = (int) ((float) ColorTable.brightest[Index].red *
(float) Intensity / 127.0);
FaceColor[1] = (int) ((float) ColorTable.brightest[Index].green *
(float) Intensity / 127.0);
FaceColor[2] = (int) ((float) ColorTable.brightest[Index].blue *
(float) Intensity / 127.0);
}
if (d->material != (ushort)-1) {
/* Compute the material properties in here. Oh Boy(tm) */
Material TempMaterial;
int i;
FaceColor[3] =
(unsigned char) (MaterialTable.mat[(int)d->material].alpha *
(1.0 - (float) d->transparency / (float) 0xffff) * 255.0 + 0.5);
mat_id = mat_lookup( (int)d->material, FaceColor );
if( mat_id < 0 ) { /* New one! */
mat_id = -mat_id;
mat_id = mat_id - 1;
TempMaterial.m.amb[0] =
(unsigned int) (MaterialTable.mat[(int)d->material].ambient.red * (float) FaceColor[0] + 0.5);
TempMaterial.m.amb[1] =
(unsigned int) (MaterialTable.mat[(int)d->material].ambient.green * (float) FaceColor[1] + 0.5);
TempMaterial.m.amb[2] =
(unsigned int) (MaterialTable.mat[(int)d->material].ambient.blue * (float) FaceColor[2] + 0.5);
TempMaterial.m.amb[3] = FaceColor[3];
TempMaterial.m.diff[0] =
(unsigned int) (MaterialTable.mat[(int)d->material].diffuse.red * (float) FaceColor[0] + 0.5);
TempMaterial.m.diff[1] =
(unsigned int) (MaterialTable.mat[(int)d->material].diffuse.green * (float) FaceColor[1] + 0.5);
TempMaterial.m.diff[2] =
(unsigned int) (MaterialTable.mat[(int)d->material].diffuse.blue * (float) FaceColor[2] + 0.5);
TempMaterial.m.diff[3] = FaceColor[3];
TempMaterial.m.spec[0] =
(unsigned int) (MaterialTable.mat[(int)d->material].specular.red * 255.0 + 0.5);
TempMaterial.m.spec[1] =
(unsigned int) (MaterialTable.mat[(int)d->material].specular.green * 255.0 + 0.5);
TempMaterial.m.spec[2] =
(unsigned int) (MaterialTable.mat[(int)d->material].specular.blue * 255.0 + 0.5);
TempMaterial.m.spec[3] = FaceColor[3];
TempMaterial.m.shiny = (int) (MaterialTable.mat[(int)d->material].shininess + 0.5);
if (TempMaterial.m.shiny == 128)
TempMaterial.m.shiny == 127;
TempMaterial.m.rolloff = 0;
TempMaterial.m.flag = 0;
/* HAVE TO PRINT THIS CRAP OUT TO A SEPARATE .h FILE*/
#if 0
/* no more Material type */
fprintf (dothFilePointer, "{%d,\t%d,\t%d,\t%d,\t /* ambient (mat_id = %d) \n",
TempMaterial.m.amb[0], TempMaterial.m.amb[1], TempMaterial.m.amb[2], TempMaterial.m.amb[3], mat_id);
fprintf (dothFilePointer, "%d,\t%d,\t%d,\t%d,\t /* diffuse */ \n",
TempMaterial.m.diff[0], TempMaterial.m.diff[1], TempMaterial.m.diff[2], TempMaterial.m.diff[3]);
fprintf (dothFilePointer, "%d,\t%d,\t%d,\t%d,\t /* specular */ \n",
TempMaterial.m.spec[0], TempMaterial.m.spec[1], TempMaterial.m.spec[2], TempMaterial.m.spec[3]);
fprintf (dothFilePointer, "%d,\t%d,\t%d,\t0}, /* shininess plus random unused garbage */ \n",
TempMaterial.m.shiny , TempMaterial.m.rolloff, TempMaterial.m.flag);
#endif
mat_saved[mat_id] = TempMaterial.m;
} else {
TempMaterial.m = mat_saved[mat_id];
};
printf("/* Specular %d %d %d %d, Shiny %d | Initial %f %f %f, %f*/ \n",
TempMaterial.m.spec[0], TempMaterial.m.spec[1], TempMaterial.m.spec[2],
TempMaterial.m.spec[3], TempMaterial.m.shiny,
MaterialTable.mat[(int)d->material].specular.red,
MaterialTable.mat[(int)d->material].specular.green,
MaterialTable.mat[(int)d->material].specular.blue,
MaterialTable.mat[(int)d->material].shininess);
printf ("\t/* Material = %d (mat_id = %d) */\n", (int)d->material, mat_id);
printf ("\t/* Face intensity = %d, Face Index = %d */ \n",
(d->color1 & 0x7F), (d->color1 >> 7));
#ifdef UCODE_LIGHTING
printf ("\tgsSPMaterial(material+%d), \n", mat_id);
numinst++;
if (!LightingCurrentlyOn)
{
printf("\tgsSPSetGeometryMode (G_LIGHTING),\n");
LightingCurrentlyOn = 1;
numinst++;
}
#endif
}
else {
/* Compute the material properties in here. Oh Boy(tm) */
Material TempMaterial;
int i;
unsigned int PrimColorInt;
printf ("\t/* Material = default (diffuse white) */ \n");
printf ("\t/* Face intensity = %d, Face Index = %d */ \n",
(d->color1 & 0x7F), (d->color1 >> 7));
printf ("\t/* Face color %d %d %d */ \n",
FaceColor[0], FaceColor[1], FaceColor[2]);
FaceColor[3] = (unsigned char) ((1.0 - (float) d->transparency / (float) 0xffff) * 255.0 + 0.5);
mat_id = mat_lookup( (int)d->material, FaceColor );
if( mat_id < 0 ) { /* New one! */
mat_id = -mat_id;
mat_id = mat_id - 1;
TempMaterial.m.amb[0] =
(unsigned int) (mat_default.amb[0]/255.0 * (float) FaceColor[0] + 0.5);
TempMaterial.m.amb[1] =
(unsigned int) (mat_default.amb[1]/255.0 * (float) FaceColor[1] + 0.5);
TempMaterial.m.amb[2] =
(unsigned int) (mat_default.amb[2]/255.0 * (float) FaceColor[2] + 0.5);
TempMaterial.m.amb[3] = FaceColor[3];
TempMaterial.m.diff[0] =
(unsigned int) (mat_default.diff[0]/255.0 * (float) FaceColor[0] + 0.5);
TempMaterial.m.diff[1] =
(unsigned int) (mat_default.diff[1]/255.0 * (float) FaceColor[1] + 0.5);
TempMaterial.m.diff[2] =
(unsigned int) (mat_default.diff[2]/255.0 * (float) FaceColor[2] + 0.5);
TempMaterial.m.diff[3] = FaceColor[3];
TempMaterial.m.spec[0] =
(unsigned int) (mat_default.spec[0]/255.0 * 255.0 + 0.5);
TempMaterial.m.spec[1] =
(unsigned int) (mat_default.spec[1]/255.0 * 255.0 + 0.5);
TempMaterial.m.spec[2] =
(unsigned int) (mat_default.spec[2]/255.0 * 255.0 + 0.5);
TempMaterial.m.spec[3] = FaceColor[3];
TempMaterial.m.shiny = (int) (mat_default.shiny + 0.5);
if (TempMaterial.m.shiny >= 128)
TempMaterial.m.shiny == 127;
TempMaterial.m.rolloff = 0;
TempMaterial.m.flag = 0;
/* HAVE TO PRINT THIS CRAP OUT TO A SEPARATE .h FILE*/
#if 0
/* no more Material type */
fprintf (stderr, "{%d,\t%d,\t%d,\t%d,\t /* ambient (mat_id = %d) */ \n",
TempMaterial.m.amb[0], TempMaterial.m.amb[1], TempMaterial.m.amb[2], TempMaterial.m.amb[3], mat_id);
fprintf (stderr, "%d,\t%d,\t%d,\t%d,\t /* diffuse */ \n",
TempMaterial.m.diff[0], TempMaterial.m.diff[1], TempMaterial.m.diff[2], TempMaterial.m.diff[3]);
fprintf (stderr, "%d,\t%d,\t%d,\t%d,\t /* specular */ \n",
TempMaterial.m.spec[0], TempMaterial.m.spec[1], TempMaterial.m.spec[2], TempMaterial.m.spec[3]);
fprintf (stderr, "%d,\t%d,\t%d,\t0}, /* shininess plus random unused garbage */ \n",
TempMaterial.m.shiny , TempMaterial.m.rolloff, TempMaterial.m.flag);
#endif
mat_saved[mat_id] = TempMaterial.m;
} else {
TempMaterial.m = mat_saved[mat_id];
};
#ifdef UCODE_LIGHTING
if (LightingCurrentlyOn)
{
printf("\tgsSPClearGeometryMode (G_LIGHTING),\n");
LightingCurrentlyOn = 0;
numinst++;
}
#endif
}
numinst += parsetree (p->child, 0, currcell);
break;
}
case MG_VTXLIST: {
struct opVertexList *d = (struct opVertexList *)p->data;
int last, numv;
numv = (d->length - 4) / 4;
#if ONLYSUBFACE
if (!subface) {
numv = 0;
}
#endif
if( numv <= 16 ) {
for (i = 0; i < numv; i++) {
if( VertexTable[vtx[d->vertex[i]]].opcode == MATERIAL_NONE ) {
VertexTable[vtx[d->vertex[i]]].opcode = mat_id;
} else if( VertexTable[vtx[d->vertex[i]]].opcode != mat_id ) {
fprintf(stderr,"Vertex %d is already using mat_id %d (new=%d)\n",
vtx[d->vertex[i]], VertexTable[vtx[d->vertex[i]]].opcode, mat_id );
};
if( texture_index != -1 ) {
VertexTable[vtx[d->vertex[i]]].tx = tex[texture_index].width;
VertexTable[vtx[d->vertex[i]]].ty = tex[texture_index].height;
} else {
VertexTable[vtx[d->vertex[i]]].tx = 2;
VertexTable[vtx[d->vertex[i]]].ty = 2;
};
/* MPG - write the proper colors for each vertex */
if (perVertColors)
{
if (i < 4)
{
VertexTable[vtx[d->vertex[i]]].nx = vcolors[i][0];
VertexTable[vtx[d->vertex[i]]].ny = vcolors[i][1];
VertexTable[vtx[d->vertex[i]]].nz = vcolors[i][2];
}
else
fprintf(stderr, "ERROR: Tried to write color for > 4 vertices\n");
}
else
{
/*
* here's where flat shading got broken
* needs a 'rat file' flag -- dave
*/
if (i < 4)
{
if (!output_normals) {
/* need to overwrite garbage in these fields for rat texture */
VertexTable[vtx[d->vertex[i]]].nx = 255;
VertexTable[vtx[d->vertex[i]]].ny = 255;
VertexTable[vtx[d->vertex[i]]].nz = 255;
}
}
else
fprintf(stderr, "ERROR: Tried to write color for > 4 vertices\n");
}
printf ("\tgsSPVertex(v_%s + %d, 1, %d),\n",
model_name, vtx[d->vertex[i]], i);
numinst++;
#ifdef SIMPLE_LIGHTING
if ((!LightingCurrentlyOn) && (!perVertColors))
{
VertexTable[vtx[d->vertex[i]]].nx =
(float) FaceColor[0] / 255.0;
VertexTable[vtx[d->vertex[i]]].ny =
(float) FaceColor[1] / 255.0;
VertexTable[vtx[d->vertex[i]]].nz =
(float) FaceColor[2] / 255.0;
VertexTable[vtx[d->vertex[i]]].unused =
(int) FaceColor[3];
}
#endif
};
printf ("/* %d */\n", numinst);
last = 1;
for (i = 2; i < numv; i++) {
printf ("\tgsSP1Triangle(0, %d, %d, 0),\n",
last, i );
numinst++;
last = i;
};
/* MPG */
if ((doHiFreqTex) || (tempSeqCount))
{
printf("\tgsDPPipeSync(),\n");
printf("\tgsDPSetCycleType(G_CYC_1CYCLE),\n");
printf("\tgsDPSetRenderMode(G_RM_AA_ZB_OPA_TERR, G_RM_AA_ZB_OPA_TERR2),\n");
printf("\tgsDPPipeSync(),\n");
}
printf ("/* %d */\n", numinst);
} else {
fprintf(stderr,
"Too many (%d) VTXs for this polygon!\n",
numv );
last = 1;
for (i = 2; i < numv; i++) {
/* Rewrite this to be more optimal */
printf ("\tgsSPVertex(v_%s + %d, 1, 0),\n",
model_name, vtx[d->vertex[0]]);
#ifdef SIMPLE_LIGHTING
if ((!LightingCurrentlyOn) && (!perVertColors))
{
VertexTable[vtx[d->vertex[0]]].nx = (float) FaceColor[0] / 255.0;
VertexTable[vtx[d->vertex[0]]].ny = (float) FaceColor[1] / 255.0;
VertexTable[vtx[d->vertex[0]]].nz = (float) FaceColor[2] / 255.0;
VertexTable[vtx[d->vertex[0]]].unused = (int) FaceColor[3];
}
#endif
printf ("\tgsSPVertex(v_%s + %d, 1, 1),\n",
model_name, vtx[d->vertex[last]]);
#ifdef SIMPLE_LIGHTING
if ((!LightingCurrentlyOn) && (!perVertColors))
{
VertexTable[vtx[d->vertex[last]]].nx = (float) FaceColor[0] / 255.0;
VertexTable[vtx[d->vertex[last]]].ny = (float) FaceColor[1] / 255.0;
VertexTable[vtx[d->vertex[last]]].nz = (float) FaceColor[2] / 255.0;
VertexTable[vtx[d->vertex[last]]].unused = (int) FaceColor[3];
}
#endif
printf ("\tgsSPVertex(v_%s + %d, 1, 2),\n",
model_name, vtx[d->vertex[i]]);
#ifdef SIMPLE_LIGHTING
if ((!LightingCurrentlyOn) && (!perVertColors))
{
VertexTable[vtx[d->vertex[i]]].nx = (float) FaceColor[0] / 255.0;
VertexTable[vtx[d->vertex[i]]].ny = (float) FaceColor[1] / 255.0;
VertexTable[vtx[d->vertex[i]]].nz = (float) FaceColor[2] / 255.0;
VertexTable[vtx[d->vertex[i]]].unused = (int) FaceColor[3];
}
#endif
printf ("\tgsSP1Triangle(0, 1, 2, 0),\n");
numinst += 4;
printf ("/* %d */\n", numinst);
last = i;
};
}
break;
}
default: {
printf ("/* UNSUPPORTED opcode: %s */\n",
opcode_name[opcode]);
numinst += parsetree (p->child, pass, currcell);
}
}
numinst += parsetree (p->sibling, pass, currcell);
return numinst;
}
int
mat_lookup( int id, unsigned char *fc )
{
int i;
for(i=0; i<mat_hash_num; i++ )
if( (mat_save_hash[i].mat_id == id) &&
(mat_save_hash[i].FaColor[0] == fc[0]) &&
(mat_save_hash[i].FaColor[1] == fc[1]) &&
(mat_save_hash[i].FaColor[2] == fc[2]) &&
(mat_save_hash[i].FaColor[3] == fc[3]) )
return(i);
mat_save_hash[i].mat_id = id;
mat_save_hash[i].FaColor[0] = fc[0];
mat_save_hash[i].FaColor[1] = fc[1];
mat_save_hash[i].FaColor[2] = fc[2];
mat_save_hash[i].FaColor[3] = fc[3];
mat_hash_num++;
return( -(i+1) );
}
void usage (void)
{
fprintf (stderr, "Usage: flt2walk [options] filename\n\n");
fprintf (stderr, "options: -m name Name the model (defaults to \"model\")\n");
/* the below is bogus! -dan */
fprintf (stderr, " -f Flat shading per polygon\n");
fprintf (stderr, " -F Flip texture\n");
fprintf (stderr, " -3 Use 32-bit RGBA textures\n");
fprintf (stderr, " -P Pad odd sized textures correctly\n");
fprintf (stderr, " -t Turn off texturing\n");
fprintf (stderr, " -n Output normals\n");
fprintf (stderr, " -N Output normals and sample light\n");
fprintf (stderr, " -R Allow for tex coords from -1,+1\n");
fprintf (stderr, " -x Use G_CC_MODULATEIDECALA\n");
fprintf (stderr, " -a Auto scale tex's to fit in 32x32\n");
fprintf (stderr, " -A Output auxiliary header file\n");
fprintf (stderr, " -c x,y,z Move (x,y,z) point to (0,0,0)\n");
fprintf (stderr, " -s scale Scale model by <scale> (after centering)\n");
fprintf (stderr, " -L x,y,z Light source in (x,y,z) direction\n");
fprintf (stderr, " -C r,g,b Color Light source (r,g,b)\n");
fprintf (stderr, " -r rn,rd SetRenderMode arguments for normal, decal texturing\n");
fprintf (stderr, " -p Enable portals\n");
fprintf (stderr, " -T frames Number of keyframes for temporal illumination sequence\n");
exit (1);
}
void openMaterialFile(void)
{
sprintf(dothFileName, "%s.h", model_name);
dothFilePointer = fopen (dothFileName, "w");
/*
* Embed the scale used by flt2walk in a macro so the walk app can
* scale the joystick movements intelligently
*/
fprintf(dothFilePointer,"\n#ifndef FLT_MODEL_SCALE\n");
fprintf(dothFilePointer,"#define FLT_MODEL_SCALE %g\n",obscale);
fprintf(dothFilePointer,"#endif\n\n");
#if 0
/* no more Material type */
fprintf (dothFilePointer, "Material material[] = { \n");
#endif
}
int main (int argc, char *argv[])
{
int c, i;
extern int optind;
extern char *optarg;
struct opVertexNUV tv;
double norm, sqrt(double);
char uppername[64],upfile[256],upheader[256],auxheader[256];
/* MPG - temporal illumination */
char texfilename[64], texfilenum[12];
FILE *pttfile, *ttfile, *extfile, *auxfile;
tempSeqCount = 0;
obscale = 1;
numtex = 0;
light_num = 0;
color_num = 0;
doportals = 0;
VertexTable = malloc(VERTEXTABLEMALLOCINCREMENT * sizeof(struct opVertexNUV));
VertexTableMallocCount = VERTEXTABLEMALLOCINCREMENT;
model_name = "model";
/* defaults to being a white light at 1 0 0 */
lights[light_num].x = 1.0;
lights[light_num].y = 0;
lights[light_num].z = 0;
fprintf (stderr, "Light[%d] = (%g,%g,%g)\n",
light_num, lights[light_num].x,
lights[light_num].y,
lights[light_num].z );
light_num++;
lights[color_num].nx = 1.0;
lights[color_num].ny = 1.0;
lights[color_num].nz = 1.0;
fprintf (stderr, "Color[%d] = (%g,%g,%g)\n",
color_num, lights[color_num].nx,
lights[color_num].ny,
lights[color_num].nz );
color_num++;
while ((c = getopt (argc, argv, "E:L:C:c:s:T:m:pr:RfaAnNtxFP3")) != EOF) {
switch (c)
{
case 'L': /* Light Position Direction */
tv.x = atof (optarg);
tv.y = atof (argv[optind++]);
tv.z = atof (argv[optind++]);
norm = sqrt( tv.x * tv.x +
tv.y * tv.y +
tv.z * tv.z );
lights[light_num].x = tv.x / norm;
lights[light_num].y = tv.y / norm;
lights[light_num].z = tv.z / norm;
fprintf (stderr, "Light[%d] = (%g,%g,%g)\n",
light_num, lights[light_num].x,
lights[light_num].y,
lights[light_num].z );
light_num++;
break;
case 'C': /* Color of Light Source */
lights[color_num].nx = atof (optarg);
lights[color_num].ny = atof (argv[optind++]);
lights[color_num].nz = atof (argv[optind++]);
fprintf (stderr, "Color[%d] = (%g,%g,%g)\n",
color_num, lights[color_num].nx,
lights[color_num].ny,
lights[color_num].nz );
color_num++;
break;
case 'T': /* Indicates a temporal illumination sequence */
tempSeqCount = atoi (optarg);
if (tempSeqCount > MAX_TEMP_SEQ)
{
fprintf(stderr, "ERROR: temporal sequence number exceeds maximum allowed\n");
exit(-1);
}
break;
case 'E': /* Eye Direction (for Specular Calc) */
eye.x = atof (optarg);
eye.y = atof (argv[optind++]);
eye.z = atof (argv[optind++]);
norm = sqrt( eye.x * eye.x +
eye.y * eye.y +
eye.z * eye.z );
eye.x /= norm;
eye.y /= norm;
eye.z /= norm;
fprintf (stderr, "Eye = (%g,%g,%g)\n",
eye.x, eye.y, eye.z );
specular_flag = 1;
break;
case 'c': /* Center of object (Move it to origin) */
center.x = atof (optarg);
center.y = atof (argv[optind++]);
center.z = atof (argv[optind++]);
fprintf (stderr, "Center = (%g,%g,%g)\n",
center.x, center.y, center.z );
break;
case 's':
obscale = atof (optarg);
if (obscale == 0) {
usage ();
}
break;
case 'm':
model_name = optarg;
break;
case 'p':
doportals = 1;
break;
case 'R':
full_range = 1;
break;
case 'n':
output_normals = 1;
break;
case 'N':
output_normals = 1;
output_sample_light = 1;
break;
case 'a':
autoscale = 1;
break;
case 'x':
use_modulateidecala = 1;
break;
case 'A':
output_aux_header = 1;
break;
case 'r':
render_normal = optarg;
render_current = optarg;
render_decal = argv[optind++];
render_mode = 1;
break;
case 'F':
tex_flags ^= FLIP_FLAG;
break;
case 'P':
tex_flags ^= PAD_FLAG;
break;
case '3':
tex_siz = 32;
tex_fmt = RGBA;
break;
case 'f':
flat_shade_flag = 1;
break;
case 't':
texture_flag ^= 1;
break;
case '?':
usage ();
break;
default:
fprintf (stderr, "Bug in getopt <%c>\n", c );
break;
}
}
fprintf (stderr, "Doing %s\n", model_name);
if( color_num != light_num ) {
fprintf (stderr, "Number of lights and color of lights is wrong (%d != %d)\n", light_num, color_num );
if( color_num < light_num )
light_num = color_num;
else
color_num = light_num;
usage ();
};
if (optind == argc) {
usage ();
}
/* check to see if temporal illumination sequence */
if (tempSeqCount > 2)
{
fprintf(stderr, "Generating a temporal illumination sequence of length: %d\n", tempSeqCount);
for (i=0; i<tempSeqCount; i++)
{
strcpy(texfilename, "ttextures");
sprintf(texfilenum, "%d", i);
strcat(texfilename, texfilenum);
strcat(texfilename, ".c");
if ((ttexfile[i] = fopen(texfilename, "w")) == NULL)
{
fprintf(stderr, "ERROR: couldn't open temporal texture file: %s\n", texfilename);
exit(-1);
}
}
/* now do prototype texture files */
for (i=0; i<tempSeqCount-1; i++)
{
strcpy(texfilename, "pttextures");
sprintf(texfilenum, "%d", i);
strcat(texfilename, texfilenum);
strcat(texfilename, ".c");
if ((pttexfile[i] = fopen(texfilename, "w")) == NULL)
{
fprintf(stderr, "ERROR: couldn't open temporal texture file: %s\n", texfilename);
exit(-1);
}
}
}
else if (tempSeqCount == 2)
{
for (i=0; i<tempSeqCount; i++)
{
strcpy(texfilename, "pttextures");
sprintf(texfilenum, "%d", i);
strcat(texfilename, texfilenum);
strcat(texfilename, ".c");
if ((pttexfile[i] = fopen(texfilename, "w")) == NULL)
{
fprintf(stderr, "ERROR: couldn't open temporal texture file: %s\n", texfilename);
exit(-1);
}
}
}
/* Initialize upPortals */
if (doportals)
{
upInitLocator();
}
openMaterialFile();
loadfile (argv[optind]);
printf ("\n");
printf ("/\*\n");
printf (" * Do not edit this file. It was automatically generated\n");
printf (" * by \"flt2walk\" from the file \"%s\".\n", argv[optind]);
printf (" \*/\n");
printf ("\n");
printf ("#include <mbi.h>\n");
printf ("#include <%s>\n\n", dothFileName);
/* MPG */
printf("\n#include \"hifreqtex.h\"\n\n");
if (tempSeqCount)
printf("#include \"cexterns.h\"\n\n");
/* printtree (root, 0); */
parsetree (root, 0, NULL);
/* extern the appropriate temporal illumination texture structures */
if (tempSeqCount)
{
if ((extfile = fopen("cexterns.h", "w")) == NULL)
{
fprintf(stderr,"ERROR:couldn't open temporal texture externs file\n");
exit(-1);
}
if (tempSeqCount > 2)
{
for (i=1; i<=numtex; i++)
{
fprintf(extfile, "extern unsigned char pttex_%d_0[];\n", i);
fprintf(extfile, "extern unsigned char pttex_%d_1[];\n", i);
}
}
else
{
fprintf(extfile, "#include \"pttextures0.c\"\n");
fprintf(extfile, "#include \"pttextures1.c\"\n");
}
fclose(extfile);
}
if (output_sample_light) {
printf ("\nLights2 %sLight2 = gdSPDefLights2(\n",model_name);
printf ("\t11, 11, 19,\n");
printf ("\t113, 113, 198,\n");
printf ("\t29, -119, -29,\n");
printf ("\t113, 113, 198,\n");
printf ("\t-27, 110, 55);\n\n");
}
printf ("Gfx %s[] = {\n", model_name);
if (output_sample_light)
printf("\tgsSPSetLights2(%sLight2),\n",model_name);
parsetree (root, 1, NULL);
printf ("\tgsSPEndDisplayList(),\n");
printf ("};\n\n");
sprintf(upfile,"%s_portals.c", model_name);
sprintf(upheader,"%s_portals.h", model_name);
if (doportals)
{
upPrint(upfile, upheader);
}
free (buf);
printvtxtable();
fclose(dothFilePointer);
fclose(hftfile);
if (tempSeqCount)
{
/* generate the prototype texture master file */
if ((pttfile = fopen("pttextures.c", "w")) == NULL)
{
fprintf(stderr,"ERROR:couldn't open temporal proto texture file\n");
exit(-1);
}
if (tempSeqCount > 2)
{
fprintf(pttfile, "/* Prototype Texture Banks */\n\n");
fprintf(pttfile, "#include <mbi.h>\n\n");
for (i=0; i<tempSeqCount-1; i++)
{
fprintf(pttfile, "static int pttexture_bank%d[1] = {0};\n\n", i);
fprintf(pttfile, "#include \"pttextures%d.c\"\n\n", i);
}
fclose(pttfile);
/* generate the texture master file */
if ((ttfile = fopen("ttextures.c", "w")) == NULL)
{
fprintf(stderr, "ERROR: couldn't open temporal texture file\n");
exit(-1);
}
if ((pttfile = fopen("ttexterns.h", "w")) == NULL)
{
fprintf(stderr, "ERROR: couldn't open temporal externs file\n");
exit(-1);
}
fprintf(ttfile, "/* Texture Banks */\n\n");
fprintf(ttfile, "#include <mbi.h>\n\n");
for (i=0; i<tempSeqCount; i++)
{
fprintf(ttfile, "int ttexture_bank%d[1] = {0};\n\n", i);
fprintf(pttfile, "extern int ttexture_bank%d[];\n", i);
fprintf(ttfile, "#include \"ttextures%d.c\"\n\n", i);
}
fclose(ttfile);
fclose(pttfile);
}
/* close all the files */
for (i=0; i<tempSeqCount-1; i++)
fclose(ttexfile[i]);
for (i=0; i<tempSeqCount-1; i++)
fclose(pttexfile[i]);
}
if (output_aux_header) {
sprintf(auxheader,"%s_aux.h",model_name);
auxfile = fopen(auxheader,"w");
for (i=0; i<strlen(model_name); i++)
uppername[i] = toupper(model_name[i]);
uppername[i] = '\0';
fprintf(auxfile,"/*\n");
fprintf(auxfile," * Auxiliar header file for \"%s\"\n",model_name);
fprintf(auxfile," */\n\n");
fprintf(auxfile,"#define %s_CENTER_X\t\t%lf\n",
uppername,(max_x + min_x)/2.0*obscale);
fprintf(auxfile,"#define %s_CENTER_Y\t\t%lf\n",
uppername,(max_y + min_y)/2.0*obscale);
fprintf(auxfile,"#define %s_CENTER_Z\t\t%lf\n\n",
uppername,(max_z + min_z)/2.0*obscale);
fprintf(auxfile,"#define %s_MIN_X\t\t%lf\n",uppername,min_x*obscale);
fprintf(auxfile,"#define %s_MIN_Y\t\t%lf\n",uppername,min_y*obscale);
fprintf(auxfile,"#define %s_MIN_Z\t\t%lf\n",uppername,min_z*obscale);
fprintf(auxfile,"#define %s_MAX_X\t\t%lf\n",uppername,max_x*obscale);
fprintf(auxfile,"#define %s_MAX_Y\t\t%lf\n",uppername,max_y*obscale);
fprintf(auxfile,"#define %s_MAX_Z\t\t%lf\n\n",uppername,max_z*obscale);
fclose(auxfile);
}
}