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
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
// vrad.c
#include "vrad.h"
#include "physdll.h"
#include "lightmap.h"
#include "tier1/strtools.h"
#include "vmpi.h"
#include "macro_texture.h"
#include "vmpi_tools_shared.h"
#include "leaf_ambient_lighting.h"
#include "tools_minidump.h"
#include "loadcmdline.h"
#include "byteswap.h"
#define ALLOWDEBUGOPTIONS (0 || _DEBUG)
static FileHandle_t pFpTrans = NULL;
/*
NOTES
-----
every surface must be divided into at least two patches each axis
*/
CUtlVector<CPatch> g_Patches;
CUtlVector<int> g_FacePatches; // contains all patches, children first
CUtlVector<int> faceParents; // contains only root patches, use next parent to iterate
CUtlVector<int> clusterChildren;
CUtlVector<Vector> emitlight;
CUtlVector<bumplights_t> addlight;
int num_sky_cameras;
sky_camera_t sky_cameras[MAX_MAP_AREAS];
int area_sky_cameras[MAX_MAP_AREAS];
entity_t *face_entity[MAX_MAP_FACES];
Vector face_offset[MAX_MAP_FACES]; // for rotating bmodels
int fakeplanes;
unsigned numbounce = 100; // 25; /* Originally this was 8 */
float maxchop = 4; // coarsest allowed number of luxel widths for a patch
float minchop = 4; // "-chop" tightest number of luxel widths for a patch, used on edges
float dispchop = 8.0f; // number of luxel widths for a patch
float g_MaxDispPatchRadius = 1500.0f; // Maximum radius allowed for displacement patches
qboolean g_bDumpPatches;
bool bDumpNormals = false;
bool g_bDumpRtEnv = false;
bool bRed2Black = true;
bool g_bFastAmbient = false;
bool g_bNoSkyRecurse = false;
bool g_bDumpPropLightmaps = false;
int junk;
Vector ambient( 0, 0, 0 );
float lightscale = 1.0;
float dlight_threshold = 0.1; // was DIRECT_LIGHT constant
char source[MAX_PATH] = "";
char level_name[MAX_PATH] = ""; // map filename, without extension or path info
char global_lights[MAX_PATH] = "";
char designer_lights[MAX_PATH] = "";
char level_lights[MAX_PATH] = "";
char vismatfile[_MAX_PATH] = "";
char incrementfile[_MAX_PATH] = "";
IIncremental *g_pIncremental = 0;
bool g_bInterrupt = false; // Wsed with background lighting in WC. Tells VRAD
// to stop lighting.
float g_SunAngularExtent=0.0;
float g_flSkySampleScale = 1.0;
bool g_bLargeDispSampleRadius = false;
bool g_bOnlyStaticProps = false;
bool g_bShowStaticPropNormals = false;
float gamma_value = 0.5;
float indirect_sun = 1.0;
float reflectivityScale = 1.0;
qboolean do_extra = true;
bool debug_extra = false;
qboolean do_fast = false;
qboolean do_centersamples = false;
int extrapasses = 4;
float smoothing_threshold = 0.7071067; // cos(45.0*(M_PI/180))
// Cosine of smoothing angle(in radians)
float coring = 1.0; // Light threshold to force to blackness(minimizes lightmaps)
qboolean texscale = true;
int dlight_map = 0; // Setting to 1 forces direct lighting into different lightmap than radiosity
float luxeldensity = 1.0;
unsigned num_degenerate_faces;
qboolean g_bLowPriority = false;
qboolean g_bLogHashData = false;
bool g_bNoDetailLighting = false;
double g_flStartTime;
bool g_bStaticPropLighting = false;
bool g_bStaticPropPolys = false;
bool g_bTextureShadows = false;
bool g_bDisablePropSelfShadowing = false;
CUtlVector<byte> g_FacesVisibleToLights;
RayTracingEnvironment g_RtEnv;
dface_t *g_pFaces=0;
// this is a list of material names used on static props which shouldn't cast shadows. a
// sequential search is used since we allow substring matches. its not time critical, and this
// functionality is a stopgap until vrad starts reading .vmt files.
CUtlVector<char const *> g_NonShadowCastingMaterialStrings;
/*
===================================================================
MISC
===================================================================
*/
int leafparents[MAX_MAP_LEAFS];
int nodeparents[MAX_MAP_NODES];
void MakeParents (int nodenum, int parent)
{
int i, j;
dnode_t *node;
nodeparents[nodenum] = parent;
node = &dnodes[nodenum];
for (i=0 ; i<2 ; i++)
{
j = node->children[i];
if (j < 0)
leafparents[-j - 1] = nodenum;
else
MakeParents (j, nodenum);
}
}
/*
===================================================================
TEXTURE LIGHT VALUES
===================================================================
*/
typedef struct
{
char name[256];
Vector value;
char *filename;
} texlight_t;
#define MAX_TEXLIGHTS 128
texlight_t texlights[MAX_TEXLIGHTS];
int num_texlights;
/*
============
ReadLightFile
============
*/
void ReadLightFile (char *filename)
{
char buf[1024];
int file_texlights = 0;
FileHandle_t f = g_pFileSystem->Open( filename, "r" );
if (!f)
{
Warning("Warning: Couldn't open texlight file %s.\n", filename);
return;
}
Msg("[Reading texlights from '%s']\n", filename);
while ( CmdLib_FGets( buf, sizeof( buf ), f ) )
{
// check ldr/hdr
char * scan = buf;
if ( !strnicmp( "hdr:", scan, 4) )
{
scan += 4;
if ( ! g_bHDR )
{
continue;
}
}
if ( !strnicmp( "ldr:", scan, 4) )
{
scan += 4;
if ( g_bHDR )
{
continue;
}
}
scan += strspn( scan, " \t" );
char NoShadName[1024];
if ( sscanf(scan,"noshadow %s",NoShadName)==1)
{
char * dot = strchr( NoShadName, '.' );
if ( dot ) // if they specify .vmt, kill it
* dot = 0;
//printf("add %s as a non shadow casting material\n",NoShadName);
g_NonShadowCastingMaterialStrings.AddToTail( strdup( NoShadName ));
}
else if ( sscanf( scan, "forcetextureshadow %s", NoShadName ) == 1 )
{
//printf("add %s as a non shadow casting material\n",NoShadName);
ForceTextureShadowsOnModel( NoShadName );
}
else
{
char szTexlight[256];
Vector value;
if ( num_texlights == MAX_TEXLIGHTS )
Error ("Too many texlights, max = %d", MAX_TEXLIGHTS);
int argCnt = sscanf (scan, "%s ",szTexlight );
if( argCnt != 1 )
{
if ( strlen( scan ) > 4 )
Msg( "ignoring bad texlight '%s' in %s", scan, filename );
continue;
}
LightForString( scan + strlen( szTexlight ) + 1, value );
int j = 0;
for( j; j < num_texlights; j ++ )
{
if ( strcmp( texlights[j].name, szTexlight ) == 0 )
{
if ( strcmp( texlights[j].filename, filename ) == 0 )
{
Msg( "ERROR\a: Duplication of '%s' in file '%s'!\n",
texlights[j].name, texlights[j].filename );
}
else if ( texlights[j].value[0] != value[0]
|| texlights[j].value[1] != value[1]
|| texlights[j].value[2] != value[2] )
{
Warning( "Warning: Overriding '%s' from '%s' with '%s'!\n",
texlights[j].name, texlights[j].filename, filename );
}
else
{
Warning( "Warning: Redundant '%s' def in '%s' AND '%s'!\n",
texlights[j].name, texlights[j].filename, filename );
}
break;
}
}
strcpy( texlights[j].name, szTexlight );
VectorCopy( value, texlights[j].value );
texlights[j].filename = filename;
file_texlights ++;
num_texlights = max( num_texlights, j + 1 );
}
}
qprintf ( "[%i texlights parsed from '%s']\n\n", file_texlights, filename);
g_pFileSystem->Close( f );
}
/*
============
LightForTexture
============
*/
void LightForTexture( const char *name, Vector& result )
{
int i;
result[ 0 ] = result[ 1 ] = result[ 2 ] = 0;
char baseFilename[ MAX_PATH ];
if ( Q_strncmp( "maps/", name, 5 ) == 0 )
{
// this might be a patch texture for cubemaps. try to parse out the original filename.
if ( Q_strncmp( level_name, name + 5, Q_strlen( level_name ) ) == 0 )
{
const char *base = name + 5 + Q_strlen( level_name );
if ( *base == '/' )
{
++base; // step past the path separator
// now we've gotten rid of the 'maps/level_name/' part, so we're left with
// 'originalName_%d_%d_%d'.
strcpy( baseFilename, base );
bool foundSeparators = true;
for ( int i=0; i<3; ++i )
{
char *underscore = Q_strrchr( baseFilename, '_' );
if ( underscore && *underscore )
{
*underscore = '\0';
}
else
{
foundSeparators = false;
}
}
if ( foundSeparators )
{
name = baseFilename;
}
}
}
}
for (i=0 ; i<num_texlights ; i++)
{
if (!Q_strcasecmp (name, texlights[i].name))
{
VectorCopy( texlights[i].value, result );
return;
}
}
}
/*
=======================================================================
MAKE FACES
=======================================================================
*/
/*
=============
WindingFromFace
=============
*/
winding_t *WindingFromFace (dface_t *f, Vector& origin )
{
int i;
int se;
dvertex_t *dv;
int v;
winding_t *w;
w = AllocWinding (f->numedges);
w->numpoints = f->numedges;
for (i=0 ; i<f->numedges ; i++)
{
se = dsurfedges[f->firstedge + i];
if (se < 0)
v = dedges[-se].v[1];
else
v = dedges[se].v[0];
dv = &dvertexes[v];
VectorAdd (dv->point, origin, w->p[i]);
}
RemoveColinearPoints (w);
return w;
}
/*
=============
BaseLightForFace
=============
*/
void BaseLightForFace( dface_t *f, Vector& light, float *parea, Vector& reflectivity )
{
texinfo_t *tx;
dtexdata_t *texdata;
//
// check for light emited by texture
//
tx = &texinfo[f->texinfo];
texdata = &dtexdata[tx->texdata];
LightForTexture (TexDataStringTable_GetString( texdata->nameStringTableID ), light);
*parea = texdata->height * texdata->width;
VectorScale( texdata->reflectivity, reflectivityScale, reflectivity );
// always keep this less than 1 or the solution will not converge
for ( int i = 0; i < 3; i++ )
{
if ( reflectivity[i] > 0.99 )
reflectivity[i] = 0.99;
}
}
qboolean IsSky (dface_t *f)
{
texinfo_t *tx;
tx = &texinfo[f->texinfo];
if (tx->flags & SURF_SKY)
return true;
return false;
}
#ifdef STATIC_FOG
/*=============
IsFog
=============*/
qboolean IsFog( dface_t *f )
{
texinfo_t *tx;
tx = &texinfo[f->texinfo];
// % denotes a fog texture
if( tx->texture[0] == '%' )
return true;
return false;
}
#endif
void ProcessSkyCameras()
{
int i;
num_sky_cameras = 0;
for (i = 0; i < numareas; ++i)
{
area_sky_cameras[i] = -1;
}
for (i = 0; i < num_entities; ++i)
{
entity_t *e = &entities[i];
const char *name = ValueForKey (e, "classname");
if (stricmp (name, "sky_camera"))
continue;
Vector origin;
GetVectorForKey( e, "origin", origin );
int node = PointLeafnum( origin );
int area = -1;
if (node >= 0 && node < numleafs) area = dleafs[node].area;
float scale = FloatForKey( e, "scale" );
if (scale > 0.0f)
{
sky_cameras[num_sky_cameras].origin = origin;
sky_cameras[num_sky_cameras].sky_to_world = scale;
sky_cameras[num_sky_cameras].world_to_sky = 1.0f / scale;
sky_cameras[num_sky_cameras].area = area;
if (area >= 0 && area < numareas)
{
area_sky_cameras[area] = num_sky_cameras;
}
++num_sky_cameras;
}
}
}
/*
=============
MakePatchForFace
=============
*/
float totalarea;
void MakePatchForFace (int fn, winding_t *w)
{
dface_t *f = g_pFaces + fn;
float area;
CPatch *patch;
Vector centroid(0,0,0);
int i, j;
texinfo_t *tx;
// get texture info
tx = &texinfo[f->texinfo];
// No patches at all for fog!
#ifdef STATIC_FOG
if ( IsFog( f ) )
return;
#endif
// the sky needs patches or the form factors don't work out correctly
// if (IsSky( f ) )
// return;
area = WindingArea (w);
if (area <= 0)
{
num_degenerate_faces++;
// Msg("degenerate face\n");
return;
}
totalarea += area;
// get a patch
int ndxPatch = g_Patches.AddToTail();
patch = &g_Patches[ndxPatch];
memset( patch, 0, sizeof( CPatch ) );
patch->ndxNext = g_Patches.InvalidIndex();
patch->ndxNextParent = g_Patches.InvalidIndex();
patch->ndxNextClusterChild = g_Patches.InvalidIndex();
patch->child1 = g_Patches.InvalidIndex();
patch->child2 = g_Patches.InvalidIndex();
patch->parent = g_Patches.InvalidIndex();
patch->needsBumpmap = tx->flags & SURF_BUMPLIGHT ? true : false;
// link and save patch data
patch->ndxNext = g_FacePatches.Element( fn );
g_FacePatches[fn] = ndxPatch;
// patch->next = face_g_Patches[fn];
// face_g_Patches[fn] = patch;
// compute a separate scale for chop - since the patch "scale" is the texture scale
// we want textures with higher resolution lighting to be chopped up more
float chopscale[2];
chopscale[0] = chopscale[1] = 16.0f;
if ( texscale )
{
// Compute the texture "scale" in s,t
for( i=0; i<2; i++ )
{
patch->scale[i] = 0.0f;
chopscale[i] = 0.0f;
for( j=0; j<3; j++ )
{
patch->scale[i] +=
tx->textureVecsTexelsPerWorldUnits[i][j] *
tx->textureVecsTexelsPerWorldUnits[i][j];
chopscale[i] +=
tx->lightmapVecsLuxelsPerWorldUnits[i][j] *
tx->lightmapVecsLuxelsPerWorldUnits[i][j];
}
patch->scale[i] = sqrt( patch->scale[i] );
chopscale[i] = sqrt( chopscale[i] );
}
}
else
{
patch->scale[0] = patch->scale[1] = 1.0f;
}
patch->area = area;
patch->sky = IsSky( f );
// chop scaled up lightmaps coarser
patch->luxscale = ((chopscale[0]+chopscale[1])/2);
patch->chop = maxchop;
#ifdef STATIC_FOG
patch->fog = FALSE;
#endif
patch->winding = w;
patch->plane = &dplanes[f->planenum];
// make a new plane to adjust for origined bmodels
if (face_offset[fn][0] || face_offset[fn][1] || face_offset[fn][2] )
{
dplane_t *pl;
// origin offset faces must create new planes
if (numplanes + fakeplanes >= MAX_MAP_PLANES)
{
Error ("numplanes + fakeplanes >= MAX_MAP_PLANES");
}
pl = &dplanes[numplanes + fakeplanes];
fakeplanes++;
*pl = *(patch->plane);
pl->dist += DotProduct (face_offset[fn], pl->normal);
patch->plane = pl;
}
patch->faceNumber = fn;
WindingCenter (w, patch->origin);
// Save "center" for generating the face normals later.
VectorSubtract( patch->origin, face_offset[fn], face_centroids[fn] );
VectorCopy( patch->plane->normal, patch->normal );
WindingBounds (w, patch->face_mins, patch->face_maxs);
VectorCopy( patch->face_mins, patch->mins );
VectorCopy( patch->face_maxs, patch->maxs );
BaseLightForFace( f, patch->baselight, &patch->basearea, patch->reflectivity );
// Chop all texlights very fine.
if ( !VectorCompare( patch->baselight, vec3_origin ) )
{
// patch->chop = do_extra ? maxchop / 2 : maxchop;
tx->flags |= SURF_LIGHT;
}
// get rid of do extra functionality on displacement surfaces
if( ValidDispFace( f ) )
{
patch->chop = maxchop;
}
// FIXME: If we wanted to add a dependency from vrad to the material system,
// we could do this. It would add a bunch of file accesses, though:
/*
// Check for a material var which would override the patch chop
bool bFound;
const char *pMaterialName = TexDataStringTable_GetString( dtexdata[ tx->texdata ].nameStringTableID );
MaterialSystemMaterial_t hMaterial = FindMaterial( pMaterialName, &bFound, false );
if ( bFound )
{
const char *pChopValue = GetMaterialVar( hMaterial, "%chop" );
if ( pChopValue )
{
float flChopValue;
if ( sscanf( pChopValue, "%f", &flChopValue ) > 0 )
{
patch->chop = flChopValue;
}
}
}
*/
}
entity_t *EntityForModel (int modnum)
{
int i;
char *s;
char name[16];
sprintf (name, "*%i", modnum);
// search the entities for one using modnum
for (i=0 ; i<num_entities ; i++)
{
s = ValueForKey (&entities[i], "model");
if (!strcmp (s, name))
return &entities[i];
}
return &entities[0];
}
/*
=============
MakePatches
=============
*/
void MakePatches (void)
{
int i, j;
dface_t *f;
int fn;
winding_t *w;
dmodel_t *mod;
Vector origin;
entity_t *ent;
ParseEntities ();
qprintf ("%i faces\n", numfaces);
for (i=0 ; i<nummodels ; i++)
{
mod = dmodels+i;
ent = EntityForModel (i);
VectorCopy (vec3_origin, origin);
// bmodels with origin brushes need to be offset into their
// in-use position
GetVectorForKey (ent, "origin", origin);
for (j=0 ; j<mod->numfaces ; j++)
{
fn = mod->firstface + j;
face_entity[fn] = ent;
VectorCopy (origin, face_offset[fn]);
f = &g_pFaces[fn];
if( f->dispinfo == -1 )
{
w = WindingFromFace (f, origin );
MakePatchForFace( fn, w );
}
}
}
if (num_degenerate_faces > 0)
{
qprintf("%d degenerate faces\n", num_degenerate_faces );
}
qprintf ("%i square feet [%.2f square inches]\n", (int)(totalarea/144), totalarea );
// make the displacement surface patches
StaticDispMgr()->MakePatches();
}
/*
=======================================================================
SUBDIVIDE
=======================================================================
*/
//-----------------------------------------------------------------------------
// Purpose: does this surface take/emit light
//-----------------------------------------------------------------------------
bool PreventSubdivision( CPatch *patch )
{
dface_t *f = g_pFaces + patch->faceNumber;
texinfo_t *tx = &texinfo[f->texinfo];
if (tx->flags & SURF_NOCHOP)
return true;
if (tx->flags & SURF_NOLIGHT && !(tx->flags & SURF_LIGHT))
return true;
return false;
}
//-----------------------------------------------------------------------------
// Purpose: subdivide the "parent" patch
//-----------------------------------------------------------------------------
int CreateChildPatch( int nParentIndex, winding_t *pWinding, float flArea, const Vector &vecCenter )
{
int nChildIndex = g_Patches.AddToTail();
CPatch *child = &g_Patches[nChildIndex];
CPatch *parent = &g_Patches[nParentIndex];
// copy all elements of parent patch to children
*child = *parent;
// Set up links
child->ndxNext = g_Patches.InvalidIndex();
child->ndxNextParent = g_Patches.InvalidIndex();
child->ndxNextClusterChild = g_Patches.InvalidIndex();
child->child1 = g_Patches.InvalidIndex();
child->child2 = g_Patches.InvalidIndex();
child->parent = nParentIndex;
child->m_IterationKey = 0;
child->winding = pWinding;
child->area = flArea;
VectorCopy( vecCenter, child->origin );
if ( ValidDispFace( g_pFaces + child->faceNumber ) )
{
// shouldn't get here anymore!!
Msg( "SubdividePatch: Error - Should not be here!\n" );
StaticDispMgr()->GetDispSurfNormal( child->faceNumber, child->origin, child->normal, true );
}
else
{
GetPhongNormal( child->faceNumber, child->origin, child->normal );
}
child->planeDist = child->plane->dist;
WindingBounds(child->winding, child->mins, child->maxs);
if ( !VectorCompare( child->baselight, vec3_origin ) )
{
// don't check edges on surf lights
return nChildIndex;
}
// Subdivide patch towards minchop if on the edge of the face
Vector total;
VectorSubtract( child->maxs, child->mins, total );
VectorScale( total, child->luxscale, total );
if ( child->chop > minchop && (total[0] < child->chop) && (total[1] < child->chop) && (total[2] < child->chop) )
{
for ( int i=0; i<3; ++i )
{
if ( (child->face_maxs[i] == child->maxs[i] || child->face_mins[i] == child->mins[i] )
&& total[i] > minchop )
{
child->chop = max( minchop, child->chop / 2 );
break;
}
}
}
return nChildIndex;
}
//-----------------------------------------------------------------------------
// Purpose: subdivide the "parent" patch
//-----------------------------------------------------------------------------
void SubdividePatch( int ndxPatch )
{
winding_t *w, *o1, *o2;
Vector total;
Vector split;
vec_t dist;
vec_t widest = -1;
int i, widest_axis = -1;
bool bSubdivide = false;
// get the current patch
CPatch *patch = &g_Patches.Element( ndxPatch );
if ( !patch )
return;
// never subdivide sky patches
if ( patch->sky )
return;
// get the patch winding
w = patch->winding;
// subdivide along the widest axis
VectorSubtract (patch->maxs, patch->mins, total);
VectorScale( total, patch->luxscale, total );
for (i=0 ; i<3 ; i++)
{
if ( total[i] > widest )
{
widest_axis = i;
widest = total[i];
}
if ( (total[i] >= patch->chop) && (total[i] >= minchop) )
{
bSubdivide = true;
}
}
if ((!bSubdivide) && widest_axis != -1)
{
// make more square
if (total[widest_axis] > total[(widest_axis + 1) % 3] * 2 && total[widest_axis] > total[(widest_axis + 2) % 3] * 2)
{
if (patch->chop > minchop)
{
bSubdivide = true;
patch->chop = max( minchop, patch->chop / 2 );
}
}
}
if ( !bSubdivide )
return;
// split the winding
VectorCopy (vec3_origin, split);
split[widest_axis] = 1;
dist = (patch->mins[widest_axis] + patch->maxs[widest_axis])*0.5f;
ClipWindingEpsilon (w, split, dist, ON_EPSILON, &o1, &o2);
// calculate the area of the patches to see if they are "significant"
Vector center1, center2;
float area1 = WindingAreaAndBalancePoint( o1, center1 );
float area2 = WindingAreaAndBalancePoint( o2, center2 );
if( area1 == 0 || area2 == 0 )
{
Msg( "zero area child patch\n" );
return;
}
// create new child patches
int ndxChild1Patch = CreateChildPatch( ndxPatch, o1, area1, center1 );
int ndxChild2Patch = CreateChildPatch( ndxPatch, o2, area2, center2 );
// FIXME: This could go into CreateChildPatch if child1, child2 were stored in the patch as child[0], child[1]
patch = &g_Patches.Element( ndxPatch );
patch->child1 = ndxChild1Patch;
patch->child2 = ndxChild2Patch;
SubdividePatch( ndxChild1Patch );
SubdividePatch( ndxChild2Patch );
}
/*
=============
SubdividePatches
=============
*/
void SubdividePatches (void)
{
unsigned i, num;
if (numbounce == 0)
return;
unsigned int uiPatchCount = g_Patches.Size();
qprintf ("%i patches before subdivision\n", uiPatchCount);
for (i = 0; i < uiPatchCount; i++)
{
CPatch *pCur = &g_Patches.Element( i );
pCur->planeDist = pCur->plane->dist;
pCur->ndxNextParent = faceParents.Element( pCur->faceNumber );
faceParents[pCur->faceNumber] = pCur - g_Patches.Base();
}
for (i=0 ; i< uiPatchCount; i++)
{
CPatch *patch = &g_Patches.Element( i );
patch->parent = -1;
if ( PreventSubdivision(patch) )
continue;
if (!do_fast)
{
if( g_pFaces[patch->faceNumber].dispinfo == -1 )
{
SubdividePatch( i );
}
else
{
StaticDispMgr()->SubdividePatch( i );
}
}
}
// fixup next pointers
for (i = 0; i < (unsigned)numfaces; i++)
{
g_FacePatches[i] = g_FacePatches.InvalidIndex();
}
uiPatchCount = g_Patches.Size();
for (i = 0; i < uiPatchCount; i++)
{
CPatch *pCur = &g_Patches.Element( i );
pCur->ndxNext = g_FacePatches.Element( pCur->faceNumber );
g_FacePatches[pCur->faceNumber] = pCur - g_Patches.Base();
#if 0
CPatch *prev;
prev = face_g_Patches[g_Patches[i].faceNumber];
g_Patches[i].next = prev;
face_g_Patches[g_Patches[i].faceNumber] = &g_Patches[i];
#endif
}
// Cache off the leaf number:
// We have to do this after subdivision because some patches span leaves.
// (only the faces for model #0 are split by it's BSP which is what governs the PVS, and the leaves we're interested in)
// Sub models (1-255) are only split for the BSP that their model forms.
// When those patches are subdivided their origins can end up in a different leaf.
// The engine will split (clip) those faces at run time to the world BSP because the models
// are dynamic and can be moved. In the software renderer, they must be split exactly in order
// to sort per polygon.
for ( i = 0; i < uiPatchCount; i++ )
{
g_Patches[i].clusterNumber = ClusterFromPoint( g_Patches[i].origin );
//
// test for point in solid space (can happen with detail and displacement surfaces)
//
if( g_Patches[i].clusterNumber == -1 )
{
for( int j = 0; j < g_Patches[i].winding->numpoints; j++ )
{
int clusterNumber = ClusterFromPoint( g_Patches[i].winding->p[j] );
if( clusterNumber != -1 )
{
g_Patches[i].clusterNumber = clusterNumber;
break;
}
}
}
}
// build the list of patches that need to be lit
for ( num = 0; num < uiPatchCount; num++ )
{
// do them in reverse order
i = uiPatchCount - num - 1;
// skip patches with children
CPatch *pCur = &g_Patches.Element( i );
if( pCur->child1 == g_Patches.InvalidIndex() )
{
if( pCur->clusterNumber != - 1 )
{
pCur->ndxNextClusterChild = clusterChildren.Element( pCur->clusterNumber );
clusterChildren[pCur->clusterNumber] = pCur - g_Patches.Base();
}
}
#if 0
if (g_Patches[i].child1 == g_Patches.InvalidIndex() )
{
if( g_Patches[i].clusterNumber != -1 )
{
g_Patches[i].nextclusterchild = cluster_children[g_Patches[i].clusterNumber];
cluster_children[g_Patches[i].clusterNumber] = &g_Patches[i];
}
}
#endif
}
qprintf ("%i patches after subdivision\n", uiPatchCount);
}
//=====================================================================
/*
=============
MakeScales
This is the primary time sink.
It can be run multi threaded.
=============
*/
int total_transfer;
int max_transfer;
//-----------------------------------------------------------------------------
// Purpose: Computes the form factor from a polygon patch to a differential patch
// using formula 81 of Philip Dutre's Global Illumination Compendium,
// [email protected], http://www.graphics.cornell.edu/~phil/GI/
//-----------------------------------------------------------------------------
float FormFactorPolyToDiff ( CPatch *pPolygon, CPatch* pDifferential )
{
winding_t *pWinding = pPolygon->winding;
float flFormFactor = 0.0f;
for ( int iPoint = 0; iPoint < pWinding->numpoints; iPoint++ )
{
int iNextPoint = ( iPoint < pWinding->numpoints - 1 ) ? iPoint + 1 : 0;
Vector vGammaVector, vVector1, vVector2;
VectorSubtract( pWinding->p[ iPoint ], pDifferential->origin, vVector1 );
VectorSubtract( pWinding->p[ iNextPoint ], pDifferential->origin, vVector2 );
VectorNormalize( vVector1 );
VectorNormalize( vVector2 );
CrossProduct( vVector1, vVector2, vGammaVector );
float flSinAlpha = VectorNormalize( vGammaVector );
if (flSinAlpha < -1.0f || flSinAlpha > 1.0f)
return 0.0f;
vGammaVector *= asin( flSinAlpha );
flFormFactor += DotProduct( vGammaVector, pDifferential->normal );
}
flFormFactor *= ( 0.5f / pPolygon->area ); // divide by pi later, multiply by area later
return flFormFactor;
}
//-----------------------------------------------------------------------------
// Purpose: Computes the form factor from a differential element to a differential
// element. This is okay when the distance between patches is 5 times
// greater than patch size. Lecture slides by Pat Hanrahan,
// http://graphics.stanford.edu/courses/cs348b-00/lectures/lecture17/radiosity.2.pdf
//-----------------------------------------------------------------------------
float FormFactorDiffToDiff ( CPatch *pDiff1, CPatch* pDiff2 )
{
Vector vDelta;
VectorSubtract( pDiff1->origin, pDiff2->origin, vDelta );
float flLength = VectorNormalize( vDelta );
return -DotProduct( vDelta, pDiff1->normal ) * DotProduct( vDelta, pDiff2->normal ) / ( flLength * flLength );
}
void MakeTransfer( int ndxPatch1, int ndxPatch2, transfer_t *all_transfers )
//void MakeTransfer (CPatch *patch, CPatch *patch2, transfer_t *all_transfers )
{
Vector delta;
vec_t scale;
float trans;
transfer_t *transfer;
//
// get patches
//
if( ndxPatch1 == g_Patches.InvalidIndex() || ndxPatch2 == g_Patches.InvalidIndex() )
return;
CPatch *pPatch1 = &g_Patches.Element( ndxPatch1 );
CPatch *pPatch2 = &g_Patches.Element( ndxPatch2 );
if (IsSky( &g_pFaces[ pPatch2->faceNumber ] ) )
return;
// overflow check!
if ( pPatch1->numtransfers >= MAX_PATCHES)
{
return;
}
// hack for patch areas that area <= 0 (degenerate)
if ( pPatch2->area <= 0)
{
return;
}
transfer = &all_transfers[pPatch1->numtransfers];
scale = FormFactorDiffToDiff( pPatch2, pPatch1 );
// patch normals may be > 90 due to smoothing groups
if (scale <= 0)
{
//Msg("scale <= 0\n");
return;
}
// Test 5 times rule
Vector vDelta;
VectorSubtract( pPatch1->origin, pPatch2->origin, vDelta );
float flThreshold = ( M_PI * 0.04 ) * DotProduct( vDelta, vDelta );
if (flThreshold < pPatch2->area)
{
scale = FormFactorPolyToDiff( pPatch2, pPatch1 );
if (scale <= 0.0)
return;
}
trans = (pPatch2->area*scale);
if (trans <= TRANSFER_EPSILON)
{
return;
}
transfer->patch = pPatch2 - g_Patches.Base();
// FIXME: why is this not trans?
transfer->transfer = trans;
#if 0
// DEBUG! Dump patches and transfer connection for displacements. This creates a lot of data, so only
// use it when you really want it - that is why it is #if-ed out.
if ( g_bDumpPatches )
{
if ( !pFpTrans )
{
pFpTrans = g_pFileSystem->Open( "trans.txt", "w" );
}
Vector light = pPatch1->totallight.light[0] + pPatch1->directlight;
WriteWinding( pFpTrans, pPatch1->winding, light );
light = pPatch2->totallight.light[0] + pPatch2->directlight;
WriteWinding( pFpTrans, pPatch2->winding, light );
WriteLine( pFpTrans, pPatch1->origin, pPatch2->origin, Vector( 255, 0, 255 ) );
}
#endif
pPatch1->numtransfers++;
}
void MakeScales ( int ndxPatch, transfer_t *all_transfers )
{
int j;
float total;
transfer_t *t, *t2;
total = 0;
if( ndxPatch == g_Patches.InvalidIndex() )
return;
CPatch *patch = &g_Patches.Element( ndxPatch );
// copy the transfers out
if (patch->numtransfers)
{
if (patch->numtransfers > max_transfer)
{
max_transfer = patch->numtransfers;
}
patch->transfers = ( transfer_t* )calloc (1, patch->numtransfers * sizeof(transfer_t));
if (!patch->transfers)
Error ("Memory allocation failure");
// get total transfer energy
t2 = all_transfers;
// overflow check!
for (j=0 ; j<patch->numtransfers ; j++, t2++)
{
total += t2->transfer;
}
// the total transfer should be PI, but we need to correct errors due to overlaping surfaces
if (total > M_PI)
total = 1.0f/total;
else
total = 1.0f/M_PI;
t = patch->transfers;
t2 = all_transfers;
for (j=0 ; j<patch->numtransfers ; j++, t++, t2++)
{
t->transfer = t2->transfer*total;
t->patch = t2->patch;
}
if (patch->numtransfers > max_transfer)
{
max_transfer = patch->numtransfers;
}
}
else
{
// Error - patch has no transfers
// patch->totallight[2] = 255;
}
ThreadLock ();
total_transfer += patch->numtransfers;
ThreadUnlock ();
}
/*
=============
WriteWorld
=============
*/
void WriteWorld (char *name, int iBump)
{
unsigned j;
FileHandle_t out;
CPatch *patch;
out = g_pFileSystem->Open( name, "w" );
if (!out)
Error ("Couldn't open %s", name);
unsigned int uiPatchCount = g_Patches.Size();
for (j=0; j<uiPatchCount; j++)
{
patch = &g_Patches.Element( j );
// skip parent patches
if (patch->child1 != g_Patches.InvalidIndex() )
continue;
if( patch->clusterNumber == -1 )
{
Vector vGreen;
VectorClear( vGreen );
vGreen[1] = 256.0f;
WriteWinding( out, patch->winding, vGreen );
}
else
{
Vector light = patch->totallight.light[iBump] + patch->directlight;
WriteWinding( out, patch->winding, light );
if( bDumpNormals )
{
WriteNormal( out, patch->origin, patch->plane->normal, 15.0f, patch->plane->normal * 255.0f );
}
}
}
g_pFileSystem->Close( out );
}
void WriteRTEnv (char *name)
{
FileHandle_t out;
out = g_pFileSystem->Open( name, "w" );
if (!out)
Error ("Couldn't open %s", name);
winding_t *triw = AllocWinding( 3 );
triw->numpoints = 3;
for( int i = 0; i < g_RtEnv.OptimizedTriangleList.Size(); i++ )
{
triw->p[0] = g_RtEnv.OptimizedTriangleList[i].Vertex( 0);
triw->p[1] = g_RtEnv.OptimizedTriangleList[i].Vertex( 1);
triw->p[2] = g_RtEnv.OptimizedTriangleList[i].Vertex( 2);
int id = g_RtEnv.OptimizedTriangleList[i].m_Data.m_GeometryData.m_nTriangleID;
Vector color(0, 0, 0);
if (id & TRACE_ID_OPAQUE) color.Init(0, 255, 0);
if (id & TRACE_ID_SKY) color.Init(0, 0, 255);
if (id & TRACE_ID_STATICPROP) color.Init(255, 0, 0);
WriteWinding(out, triw, color);
}
FreeWinding(triw);
g_pFileSystem->Close( out );
}
void WriteWinding (FileHandle_t out, winding_t *w, Vector& color )
{
int i;
CmdLib_FPrintf (out, "%i\n", w->numpoints);
for (i=0 ; i<w->numpoints ; i++)
{
CmdLib_FPrintf (out, "%5.2f %5.2f %5.2f %5.3f %5.3f %5.3f\n",
w->p[i][0],
w->p[i][1],
w->p[i][2],
color[ 0 ] / 256,
color[ 1 ] / 256,
color[ 2 ] / 256 );
}
}
void WriteNormal( FileHandle_t out, Vector const &nPos, Vector const &nDir,
float length, Vector const &color )
{
CmdLib_FPrintf( out, "2\n" );
CmdLib_FPrintf( out, "%5.2f %5.2f %5.2f %5.3f %5.3f %5.3f\n",
nPos.x, nPos.y, nPos.z,
color.x / 256, color.y / 256, color.z / 256 );
CmdLib_FPrintf( out, "%5.2f %5.2f %5.2f %5.3f %5.3f %5.3f\n",
nPos.x + ( nDir.x * length ),
nPos.y + ( nDir.y * length ),
nPos.z + ( nDir.z * length ),
color.x / 256, color.y / 256, color.z / 256 );
}
void WriteLine( FileHandle_t out, const Vector &vecPos1, const Vector &vecPos2, const Vector &color )
{
CmdLib_FPrintf( out, "2\n" );
CmdLib_FPrintf( out, "%5.2f %5.2f %5.2f %5.3f %5.3f %5.3f\n",
vecPos1.x, vecPos1.y, vecPos1.z,
color.x / 256, color.y / 256, color.z / 256 );
CmdLib_FPrintf( out, "%5.2f %5.2f %5.2f %5.3f %5.3f %5.3f\n",
vecPos2.x, vecPos2.y, vecPos2.z,
color.x / 256, color.y / 256, color.z / 256 );
}
void WriteTrace( const char *pFileName, const FourRays &rays, const RayTracingResult& result )
{
FileHandle_t out;
out = g_pFileSystem->Open( pFileName, "a" );
if (!out)
Error ("Couldn't open %s", pFileName);
// Draws rays
for ( int i = 0; i < 4; ++i )
{
Vector vecOrigin = rays.origin.Vec(i);
Vector vecEnd = rays.direction.Vec(i);
VectorNormalize( vecEnd );
vecEnd *= SubFloat( result.HitDistance, i );
vecEnd += vecOrigin;
WriteLine( out, vecOrigin, vecEnd, Vector( 256, 0, 0 ) );
WriteNormal( out, vecEnd, result.surface_normal.Vec(i), 10.0f, Vector( 256, 265, 0 ) );
}
g_pFileSystem->Close( out );
}
/*
=============
CollectLight
=============
*/
// patch's totallight += new light received to each patch
// patch's emitlight = addlight (newly received light from GatherLight)
// patch's addlight = 0
// pull received light from children.
void CollectLight( Vector& total )
{
int i, j;
CPatch *patch;
VectorFill( total, 0 );
// process patches in reverse order so that children are processed before their parents
unsigned int uiPatchCount = g_Patches.Size();
for( i = uiPatchCount - 1; i >= 0; i-- )
{
patch = &g_Patches.Element( i );
int normalCount = patch->needsBumpmap ? NUM_BUMP_VECTS+1 : 1;
// sky's never collect light, it is just dropped
if (patch->sky)
{
VectorFill( emitlight[ i ], 0 );
}
else if ( patch->child1 == g_Patches.InvalidIndex() )
{
// This is a leaf node.
for ( j = 0; j < normalCount; j++ )
{
VectorAdd( patch->totallight.light[j], addlight[i].light[j], patch->totallight.light[j] );
}
VectorCopy( addlight[i].light[0], emitlight[i] );
VectorAdd( total, emitlight[i], total );
}
else
{
// This is an interior node.
// Pull received light from children.
float s1, s2;
CPatch *child1;
CPatch *child2;
child1 = &g_Patches[patch->child1];
child2 = &g_Patches[patch->child2];
// BUG: This doesn't do anything?
if ((int)patch->area != (int)(child1->area + child2->area))
s1 = 0;
s1 = child1->area / (child1->area + child2->area);
s2 = child2->area / (child1->area + child2->area);
// patch->totallight = s1 * child1->totallight + s2 * child2->totallight
for ( j = 0; j < normalCount; j++ )
{
VectorScale( child1->totallight.light[j], s1, patch->totallight.light[j] );
VectorMA( patch->totallight.light[j], s2, child2->totallight.light[j], patch->totallight.light[j] );
}
// patch->emitlight = s1 * child1->emitlight + s2 * child2->emitlight
VectorScale( emitlight[patch->child1], s1, emitlight[i] );
VectorMA( emitlight[i], s2, emitlight[patch->child2], emitlight[i] );
}
for ( j = 0; j < NUM_BUMP_VECTS+1; j++ )
{
VectorFill( addlight[ i ].light[j], 0 );
}
}
}
/*
=============
GatherLight
Get light from other patches
Run multi-threaded
=============
*/
#ifdef _WIN32
#pragma warning (disable:4701)
#endif
extern void GetBumpNormals( const float* sVect, const float* tVect, const Vector& flatNormal,
const Vector& phongNormal, Vector bumpNormals[NUM_BUMP_VECTS] );
void PreGetBumpNormalsForDisp( texinfo_t *pTexinfo, Vector &vecU, Vector &vecV, Vector &vecNormal )
{
Vector vecTexU( pTexinfo->textureVecsTexelsPerWorldUnits[0][0], pTexinfo->textureVecsTexelsPerWorldUnits[0][1], pTexinfo->textureVecsTexelsPerWorldUnits[0][2] );
Vector vecTexV( pTexinfo->textureVecsTexelsPerWorldUnits[1][0], pTexinfo->textureVecsTexelsPerWorldUnits[1][1], pTexinfo->textureVecsTexelsPerWorldUnits[1][2] );
Vector vecLightU( pTexinfo->lightmapVecsLuxelsPerWorldUnits[0][0], pTexinfo->lightmapVecsLuxelsPerWorldUnits[0][1], pTexinfo->lightmapVecsLuxelsPerWorldUnits[0][2] );
Vector vecLightV( pTexinfo->lightmapVecsLuxelsPerWorldUnits[1][0], pTexinfo->lightmapVecsLuxelsPerWorldUnits[1][1], pTexinfo->lightmapVecsLuxelsPerWorldUnits[1][2] );
VectorNormalize( vecTexU );
VectorNormalize( vecTexV );
VectorNormalize( vecLightU );
VectorNormalize( vecLightV );
bool bDoConversion = false;
if ( fabs( vecTexU.Dot( vecLightU ) ) < 0.999f )
{
bDoConversion = true;
}
if ( fabs( vecTexV.Dot( vecLightV ) ) < 0.999f )
{
bDoConversion = true;
}
if ( bDoConversion )
{
matrix3x4_t matTex( vecTexU, vecTexV, vecNormal, vec3_origin );
matrix3x4_t matLight( vecLightU, vecLightV, vecNormal, vec3_origin );
matrix3x4_t matTmp;
ConcatTransforms ( matLight, matTex, matTmp );
MatrixGetColumn( matTmp, 0, vecU );
MatrixGetColumn( matTmp, 1, vecV );
MatrixGetColumn( matTmp, 2, vecNormal );
Assert( fabs( vecTexU.Dot( vecTexV ) ) <= 0.001f );
return;
}
vecU = vecTexU;
vecV = vecTexV;
}
void GatherLight (int threadnum, void *pUserData)
{
int i, j, k;
transfer_t *trans;
int num;
CPatch *patch;
Vector sum, v;
while (1)
{
j = GetThreadWork ();
if (j == -1)
break;
patch = &g_Patches[j];
trans = patch->transfers;
num = patch->numtransfers;
if ( patch->needsBumpmap )
{
Vector delta;
Vector bumpSum[NUM_BUMP_VECTS+1];
Vector normals[NUM_BUMP_VECTS+1];
// Disps
bool bDisp = ( g_pFaces[patch->faceNumber].dispinfo != -1 );
if ( bDisp )
{
normals[0] = patch->normal;
texinfo_t *pTexinfo = &texinfo[g_pFaces[patch->faceNumber].texinfo];
Vector vecTexU, vecTexV;
PreGetBumpNormalsForDisp( pTexinfo, vecTexU, vecTexV, normals[0] );
// use facenormal along with the smooth normal to build the three bump map vectors
GetBumpNormals( vecTexU, vecTexV, normals[0], normals[0], &normals[1] );
}
else
{
GetPhongNormal( patch->faceNumber, patch->origin, normals[0] );
texinfo_t *pTexinfo = &texinfo[g_pFaces[patch->faceNumber].texinfo];
// use facenormal along with the smooth normal to build the three bump map vectors
GetBumpNormals( pTexinfo->textureVecsTexelsPerWorldUnits[0],
pTexinfo->textureVecsTexelsPerWorldUnits[1], patch->normal,
normals[0], &normals[1] );
}
// force the base lightmap to use the flat normal instead of the phong normal
// FIXME: why does the patch not use the phong normal?
normals[0] = patch->normal;
for ( i = 0; i < NUM_BUMP_VECTS+1; i++ )
{
VectorFill( bumpSum[i], 0 );
}
float dot;
for (k=0 ; k<num ; k++, trans++)
{
CPatch *patch2 = &g_Patches[trans->patch];
// get vector to other patch
VectorSubtract (patch2->origin, patch->origin, delta);
VectorNormalize (delta);
// find light emitted from other patch
for(i=0; i<3; i++)
{
v[i] = emitlight[trans->patch][i] * patch2->reflectivity[i];
}
// remove normal already factored into transfer steradian
float scale = 1.0f / DotProduct (delta, patch->normal);
VectorScale( v, trans->transfer * scale, v );
Vector bumpTransfer;
for ( i = 0; i < NUM_BUMP_VECTS+1; i++ )
{
dot = DotProduct( delta, normals[i] );
if ( dot <= 0 )
{
// Assert( i > 0 ); // if this hits, then the transfer shouldn't be here. It doesn't face the flat normal of this face!
continue;
}
bumpTransfer = v * dot;
VectorAdd( bumpSum[i], bumpTransfer, bumpSum[i] );
}
}
for ( i = 0; i < NUM_BUMP_VECTS+1; i++ )
{
VectorCopy( bumpSum[i], addlight[j].light[i] );
}
}
else
{
VectorFill( sum, 0 );
for (k=0 ; k<num ; k++, trans++)
{
for(i=0; i<3; i++)
{
v[i] = emitlight[trans->patch][i] * g_Patches[trans->patch].reflectivity[i];
}
VectorScale( v, trans->transfer, v );
VectorAdd( sum, v, sum );
}
VectorCopy( sum, addlight[j].light[0] );
}
}
}
#ifdef _WIN32
#pragma warning (default:4701)
#endif
/*
=============
BounceLight
=============
*/
void BounceLight (void)
{
unsigned i;
Vector added;
char name[64];
qboolean bouncing = numbounce > 0;
unsigned int uiPatchCount = g_Patches.Size();
for (i=0 ; i<uiPatchCount; i++)
{
// totallight has a copy of the direct lighting. Move it to the emitted light and zero it out (to integrate bounces only)
VectorCopy( g_Patches[i].totallight.light[0], emitlight[i] );
// NOTE: This means that only the bounced light is integrated into totallight!
VectorFill( g_Patches[i].totallight.light[0], 0 );
}
#if 0
FileHandle_t dFp = g_pFileSystem->Open( "lightemit.txt", "w" );
unsigned int uiPatchCount = g_Patches.Size();
for (i=0 ; i<uiPatchCount; i++)
{
CmdLib_FPrintf( dFp, "Emit %d: %f %f %f\n", i, emitlight[i].x, emitlight[i].y, emitlight[i].z );
}
g_pFileSystem->Close( dFp );
for (i=0; i<num_patches ; i++)
{
Vector total;
VectorSubtract (g_Patches[i].maxs, g_Patches[i].mins, total);
Msg("%4d %4d %4d %4d (%d) %.0f", i, g_Patches[i].parent, g_Patches[i].child1, g_Patches[i].child2, g_Patches[i].samples, g_Patches[i].area );
Msg(" [%.0f %.0f %.0f]", total[0], total[1], total[2] );
if (g_Patches[i].child1 != g_Patches.InvalidIndex() )
{
Vector tmp;
VectorScale( g_Patches[i].totallight.light[0], g_Patches[i].area, tmp );
VectorMA( tmp, -g_Patches[g_Patches[i].child1].area, g_Patches[g_Patches[i].child1].totallight.light[0], tmp );
VectorMA( tmp, -g_Patches[g_Patches[i].child2].area, g_Patches[g_Patches[i].child2].totallight.light[0], tmp );
// Msg("%.0f ", VectorLength( tmp ) );
// Msg("%d ", g_Patches[i].samples - g_Patches[g_Patches[i].child1].samples - g_Patches[g_Patches[i].child2].samples );
// Msg("%d ", g_Patches[i].samples );
}
Msg("\n");
}
#endif
i = 0;
while ( bouncing )
{
// transfer light from to the leaf patches from other patches via transfers
// this moves shooter->emitlight to receiver->addlight
unsigned int uiPatchCount = g_Patches.Size();
RunThreadsOn (uiPatchCount, true, GatherLight);
// move newly received light (addlight) to light to be sent out (emitlight)
// start at children and pull light up to parents
// light is always received to leaf patches
CollectLight( added );
qprintf ("\tBounce #%i added RGB(%.0f, %.0f, %.0f)\n", i+1, added[0], added[1], added[2] );
if ( i+1 == numbounce || (added[0] < 1.0 && added[1] < 1.0 && added[2] < 1.0) )
bouncing = false;
i++;
if ( g_bDumpPatches && !bouncing && i != 1)
{
sprintf (name, "bounce%i.txt", i);
WriteWorld (name, 0);
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Counts the number of clusters in a map with no visibility
// Output : int
//-----------------------------------------------------------------------------
int CountClusters( void )
{
int clusterCount = 0;
for ( int i = 0; i < numleafs; i++ )
{
if ( dleafs[i].cluster > clusterCount )
clusterCount = dleafs[i].cluster;
}
return clusterCount + 1;
}
/*
=============
RadWorld
=============
*/
void RadWorld_Start()
{
unsigned i;
if (luxeldensity < 1.0)
{
// Remember the old lightmap vectors.
float oldLightmapVecs[MAX_MAP_TEXINFO][2][4];
for (i = 0; i < texinfo.Count(); i++)
{
for( int j=0; j < 2; j++ )
{
for( int k=0; k < 3; k++ )
{
oldLightmapVecs[i][j][k] = texinfo[i].lightmapVecsLuxelsPerWorldUnits[j][k];
}
}
}
// rescale luxels to be no denser than "luxeldensity"
for (i = 0; i < texinfo.Count(); i++)
{
texinfo_t *tx = &texinfo[i];
for (int j = 0; j < 2; j++ )
{
Vector tmp( tx->lightmapVecsLuxelsPerWorldUnits[j][0], tx->lightmapVecsLuxelsPerWorldUnits[j][1], tx->lightmapVecsLuxelsPerWorldUnits[j][2] );
float scale = VectorNormalize( tmp );
// only rescale them if the current scale is "tighter" than the desired scale
// FIXME: since this writes out to the BSP file every run, once it's set high it can't be reset
// to a lower value.
if (fabs( scale ) > luxeldensity)
{
if (scale < 0)
{
scale = -luxeldensity;
}
else
{
scale = luxeldensity;
}
VectorScale( tmp, scale, tmp );
tx->lightmapVecsLuxelsPerWorldUnits[j][0] = tmp.x;
tx->lightmapVecsLuxelsPerWorldUnits[j][1] = tmp.y;
tx->lightmapVecsLuxelsPerWorldUnits[j][2] = tmp.z;
}
}
}
UpdateAllFaceLightmapExtents();
}
MakeParents (0, -1);
BuildClusterTable();
// turn each face into a single patch
MakePatches ();
PairEdges ();
// store the vertex normals calculated in PairEdges
// so that the can be written to the bsp file for
// use in the engine
SaveVertexNormals();
// subdivide patches to a maximum dimension
SubdividePatches ();
// add displacement faces to cluster table
AddDispsToClusterTable();
// create directlights out of patches and lights
CreateDirectLights ();
// set up sky cameras
ProcessSkyCameras();
}
// This function should fill in the indices into g_pFaces[] for the faces
// with displacements that touch the specified leaf.
void STUB_GetDisplacementsTouchingLeaf( int iLeaf, CUtlVector<int> &dispFaces )
{
}
void BuildFacesVisibleToLights( bool bAllVisible )
{
g_FacesVisibleToLights.SetSize( numfaces/8 + 1 );
if( bAllVisible )
{
memset( g_FacesVisibleToLights.Base(), 0xFF, g_FacesVisibleToLights.Count() );
return;
}
// First merge all the light PVSes.
CUtlVector<byte> aggregate;
aggregate.SetSize( (dvis->numclusters/8) + 1 );
memset( aggregate.Base(), 0, aggregate.Count() );
int nDWords = aggregate.Count() / 4;
int nBytes = aggregate.Count() - nDWords*4;
for( directlight_t *dl = activelights; dl != NULL; dl = dl->next )
{
byte *pIn = dl->pvs;
byte *pOut = aggregate.Base();
for( int iDWord=0; iDWord < nDWords; iDWord++ )
{
*((unsigned long*)pOut) |= *((unsigned long*)pIn);
pIn += 4;
pOut += 4;
}
for( int iByte=0; iByte < nBytes; iByte++ )
{
*pOut |= *pIn;
++pOut;
++pIn;
}
}
// Now tag any faces that are visible to this monster PVS.
for( int iCluster=0; iCluster < dvis->numclusters; iCluster++ )
{
if( g_ClusterLeaves[iCluster].leafCount )
{
if( aggregate[iCluster>>3] & (1 << (iCluster & 7)) )
{
for ( int i = 0; i < g_ClusterLeaves[iCluster].leafCount; i++ )
{
int iLeaf = g_ClusterLeaves[iCluster].leafs[i];
// Tag all the faces.
int iFace;
for( iFace=0; iFace < dleafs[iLeaf].numleaffaces; iFace++ )
{
int index = dleafs[iLeaf].firstleafface + iFace;
index = dleaffaces[index];
assert( index < numfaces );
g_FacesVisibleToLights[index >> 3] |= (1 << (index & 7));
}
// Fill in STUB_GetDisplacementsTouchingLeaf when it's available
// so displacements get relit.
CUtlVector<int> dispFaces;
STUB_GetDisplacementsTouchingLeaf( iLeaf, dispFaces );
for( iFace=0; iFace < dispFaces.Count(); iFace++ )
{
int index = dispFaces[iFace];
g_FacesVisibleToLights[index >> 3] |= (1 << (index & 7));
}
}
}
}
}
// For stats.. figure out how many faces it's going to touch.
int nFacesToProcess = 0;
for( int i=0; i < numfaces; i++ )
{
if( g_FacesVisibleToLights[i>>3] & (1 << (i & 7)) )
++nFacesToProcess;
}
}
void MakeAllScales (void)
{
// determine visibility between patches
BuildVisMatrix ();
// release visibility matrix
FreeVisMatrix ();
Msg("transfers %d, max %d\n", total_transfer, max_transfer );
qprintf ("transfer lists: %5.1f megs\n"
, (float)total_transfer * sizeof(transfer_t) / (1024*1024));
}
// Helper function. This can be useful to visualize the world and faces and see which face
// corresponds to which dface.
#if 0
#include "iscratchpad3d.h"
void ScratchPad_DrawWorld()
{
IScratchPad3D *pPad = ScratchPad3D_Create();
pPad->SetAutoFlush( false );
for ( int i=0; i < numfaces; i++ )
{
dface_t *f = &g_pFaces[i];
// Draw the face's outline, then put text for its face index on it too.
CUtlVector<Vector> points;
for ( int iEdge = 0; iEdge < f->numedges; iEdge++ )
{
int v;
int se = dsurfedges[f->firstedge + iEdge];
if ( se < 0 )
v = dedges[-se].v[1];
else
v = dedges[se].v[0];
dvertex_t *dv = &dvertexes[v];
points.AddToTail( dv->point );
}
// Draw the outline.
Vector vCenter( 0, 0, 0 );
for ( iEdge=0; iEdge < points.Count(); iEdge++ )
{
pPad->DrawLine( CSPVert( points[iEdge] ), CSPVert( points[(iEdge+1)%points.Count()] ) );
vCenter += points[iEdge];
}
vCenter /= points.Count();
// Draw the text.
char str[512];
Q_snprintf( str, sizeof( str ), "%d", i );
CTextParams params;
params.m_bCentered = true;
params.m_bOutline = true;
params.m_flLetterWidth = 2;
params.m_vColor.Init( 1, 0, 0 );
VectorAngles( dplanes[f->planenum].normal, params.m_vAngles );
params.m_bTwoSided = true;
params.m_vPos = vCenter;
pPad->DrawText( str, params );
}
pPad->Release();
}
#endif
bool RadWorld_Go()
{
g_iCurFace = 0;
InitMacroTexture( source );
if( g_pIncremental )
{
g_pIncremental->PrepareForLighting();
// Cull out faces that aren't visible to any of the lights that we're updating with.
BuildFacesVisibleToLights( false );
}
else
{
// Mark all faces visible.. when not doing incremental lighting, it's highly
// likely that all faces are going to be touched by at least one light so don't
// waste time here.
BuildFacesVisibleToLights( true );
}
// build initial facelights
if (g_bUseMPI)
{
// RunThreadsOnIndividual (numfaces, true, BuildFacelights);
RunMPIBuildFacelights();
}
else
{
RunThreadsOnIndividual (numfaces, true, BuildFacelights);
}
// Was the process interrupted?
if( g_pIncremental && (g_iCurFace != numfaces) )
return false;
// Figure out the offset into lightmap data for each face.
PrecompLightmapOffsets();
// If we're doing incremental lighting, stop here.
if( g_pIncremental )
{
g_pIncremental->Finalize();
}
else
{
// free up the direct lights now that we have facelights
ExportDirectLightsToWorldLights();
if ( g_bDumpPatches )
{
for( int iBump = 0; iBump < 4; ++iBump )
{
char szName[64];
sprintf ( szName, "bounce0_%d.txt", iBump );
WriteWorld( szName, iBump );
}
}
if (numbounce > 0)
{
// allocate memory for emitlight/addlight
emitlight.SetSize( g_Patches.Size() );
memset( emitlight.Base(), 0, g_Patches.Size() * sizeof( Vector ) );
addlight.SetSize( g_Patches.Size() );
memset( addlight.Base(), 0, g_Patches.Size() * sizeof( bumplights_t ) );
MakeAllScales ();
// spread light around
BounceLight ();
}
//
// displacement surface luxel accumulation (make threaded!!!)
//
StaticDispMgr()->StartTimer( "Build Patch/Sample Hash Table(s)....." );
StaticDispMgr()->InsertSamplesDataIntoHashTable();
StaticDispMgr()->InsertPatchSampleDataIntoHashTable();
StaticDispMgr()->EndTimer();
// blend bounced light into direct light and save
VMPI_SetCurrentStage( "FinalLightFace" );
if ( !g_bUseMPI || g_bMPIMaster )
RunThreadsOnIndividual (numfaces, true, FinalLightFace);
// Distribute the lighting data to workers.
VMPI_DistributeLightData();
Msg("FinalLightFace Done\n"); fflush(stdout);
}
return true;
}
// declare the sample file pointer -- the whole debug print system should
// be reworked at some point!!
FileHandle_t pFileSamples[4][4];
void LoadPhysicsDLL( void )
{
PhysicsDLLPath( "VPHYSICS.DLL" );
}
void InitDumpPatchesFiles()
{
for( int iStyle = 0; iStyle < 4; ++iStyle )
{
for ( int iBump = 0; iBump < 4; ++iBump )
{
char szFilename[MAX_PATH];
sprintf( szFilename, "samples_style%d_bump%d.txt", iStyle, iBump );
pFileSamples[iStyle][iBump] = g_pFileSystem->Open( szFilename, "w" );
if( !pFileSamples[iStyle][iBump] )
{
Error( "Can't open %s for -dump.\n", szFilename );
}
}
}
}
extern IFileSystem *g_pOriginalPassThruFileSystem;
void VRAD_LoadBSP( char const *pFilename )
{
ThreadSetDefault ();
g_flStartTime = Plat_FloatTime();
if( g_bLowPriority )
{
SetLowPriority();
}
strcpy( level_name, source );
// This must come after InitFileSystem because the file system pointer might change.
if ( g_bDumpPatches )
InitDumpPatchesFiles();
// This part is just for VMPI. VMPI's file system needs the basedir in front of all filenames,
// so we prepend qdir here.
strcpy( source, ExpandPath( source ) );
if ( !g_bUseMPI )
{
// Setup the logfile.
char logFile[512];
_snprintf( logFile, sizeof(logFile), "%s.log", source );
SetSpewFunctionLogFile( logFile );
}
LoadPhysicsDLL();
// Set the required global lights filename and try looking in qproject
strcpy( global_lights, "lights.rad" );
if ( !g_pFileSystem->FileExists( global_lights ) )
{
// Otherwise, try looking in the BIN directory from which we were run from
Msg( "Could not find lights.rad in %s.\nTrying VRAD BIN directory instead...\n",
global_lights );
GetModuleFileName( NULL, global_lights, sizeof( global_lights ) );
Q_ExtractFilePath( global_lights, global_lights, sizeof( global_lights ) );
strcat( global_lights, "lights.rad" );
}
// Set the optional level specific lights filename
strcpy( level_lights, source );
Q_DefaultExtension( level_lights, ".rad", sizeof( level_lights ) );
if ( !g_pFileSystem->FileExists( level_lights ) )
*level_lights = 0;
ReadLightFile(global_lights); // Required
if ( *designer_lights ) ReadLightFile(designer_lights); // Command-line
if ( *level_lights ) ReadLightFile(level_lights); // Optional & implied
strcpy(incrementfile, source);
Q_DefaultExtension(incrementfile, ".r0", sizeof(incrementfile));
Q_DefaultExtension(source, ".bsp", sizeof( source ));
Msg( "Loading %s\n", source );
VMPI_SetCurrentStage( "LoadBSPFile" );
LoadBSPFile (source);
// Add this bsp to our search path so embedded resources can be found
if ( g_bUseMPI && g_bMPIMaster )
{
// MPI Master, MPI workers don't need to do anything
g_pOriginalPassThruFileSystem->AddSearchPath(source, "GAME", PATH_ADD_TO_HEAD);
g_pOriginalPassThruFileSystem->AddSearchPath(source, "MOD", PATH_ADD_TO_HEAD);
}
else if ( !g_bUseMPI )
{
// Non-MPI
g_pFullFileSystem->AddSearchPath(source, "GAME", PATH_ADD_TO_HEAD);
g_pFullFileSystem->AddSearchPath(source, "MOD", PATH_ADD_TO_HEAD);
}
// now, set whether or not static prop lighting is present
if (g_bStaticPropLighting)
g_LevelFlags |= g_bHDR? LVLFLAGS_BAKED_STATIC_PROP_LIGHTING_HDR : LVLFLAGS_BAKED_STATIC_PROP_LIGHTING_NONHDR;
else
{
g_LevelFlags &= ~( LVLFLAGS_BAKED_STATIC_PROP_LIGHTING_HDR | LVLFLAGS_BAKED_STATIC_PROP_LIGHTING_NONHDR );
}
// now, we need to set our face ptr depending upon hdr, and if hdr, init it
if (g_bHDR)
{
g_pFaces = dfaces_hdr;
if (numfaces_hdr==0)
{
numfaces_hdr = numfaces;
memcpy( dfaces_hdr, dfaces, numfaces*sizeof(dfaces[0]) );
}
}
else
{
g_pFaces = dfaces;
}
ParseEntities ();
ExtractBrushEntityShadowCasters();
StaticPropMgr()->Init();
StaticDispMgr()->Init();
if (!visdatasize)
{
Msg("No vis information, direct lighting only.\n");
numbounce = 0;
ambient[0] = ambient[1] = ambient[2] = 0.1f;
dvis->numclusters = CountClusters();
}
//
// patches and referencing data (ensure capacity)
//
// TODO: change the maxes to the amount from the bsp!!
//
// g_Patches.EnsureCapacity( MAX_PATCHES );
g_FacePatches.SetSize( MAX_MAP_FACES );
faceParents.SetSize( MAX_MAP_FACES );
clusterChildren.SetSize( MAX_MAP_CLUSTERS );
int ndx;
for ( ndx = 0; ndx < MAX_MAP_FACES; ndx++ )
{
g_FacePatches[ndx] = g_FacePatches.InvalidIndex();
faceParents[ndx] = faceParents.InvalidIndex();
}
for ( ndx = 0; ndx < MAX_MAP_CLUSTERS; ndx++ )
{
clusterChildren[ndx] = clusterChildren.InvalidIndex();
}
// Setup ray tracer
AddBrushesForRayTrace();
StaticDispMgr()->AddPolysForRayTrace();
StaticPropMgr()->AddPolysForRayTrace();
// Dump raytracer for glview
if ( g_bDumpRtEnv )
WriteRTEnv("trace.txt");
// Build acceleration structure
printf ( "Setting up ray-trace acceleration structure... ");
float start = Plat_FloatTime();
g_RtEnv.SetupAccelerationStructure();
float end = Plat_FloatTime();
printf ( "Done (%.2f seconds)\n", end-start );
#if 0 // To test only k-d build
exit(0);
#endif
RadWorld_Start();
// Setup incremental lighting.
if( g_pIncremental )
{
if( !g_pIncremental->Init( source, incrementfile ) )
{
Error( "Unable to load incremental lighting file in %s.\n", incrementfile );
return;
}
}
}
void VRAD_ComputeOtherLighting()
{
// Compute lighting for the bsp file
if ( !g_bNoDetailLighting )
{
ComputeDetailPropLighting( THREADINDEX_MAIN );
}
ComputePerLeafAmbientLighting();
// bake the static props high quality vertex lighting into the bsp
if ( !do_fast && g_bStaticPropLighting )
{
StaticPropMgr()->ComputeLighting( THREADINDEX_MAIN );
}
}
extern void CloseDispLuxels();
void VRAD_Finish()
{
Msg( "Ready to Finish\n" );
fflush( stdout );
if ( verbose )
{
PrintBSPFileSizes();
}
Msg( "Writing %s\n", source );
VMPI_SetCurrentStage( "WriteBSPFile" );
WriteBSPFile(source);
if ( g_bDumpPatches )
{
for ( int iStyle = 0; iStyle < 4; ++iStyle )
{
for ( int iBump = 0; iBump < 4; ++iBump )
{
g_pFileSystem->Close( pFileSamples[iStyle][iBump] );
}
}
}
CloseDispLuxels();
StaticPropMgr()->Shutdown();
double end = Plat_FloatTime();
char str[512];
GetHourMinuteSecondsString( (int)( end - g_flStartTime ), str, sizeof( str ) );
Msg( "%s elapsed\n", str );
ReleasePakFileLumps();
}
// Run startup code like initialize mathlib (called from main() and from the
// WorldCraft interface into vrad).
void VRAD_Init()
{
MathLib_Init( 2.2f, 2.2f, 0.0f, 2.0f, false, false, false, false );
InstallAllocationFunctions();
InstallSpewFunction();
}
int ParseCommandLine( int argc, char **argv, bool *onlydetail )
{
*onlydetail = false;
int mapArg = -1;
// default to LDR
SetHDRMode( false );
int i;
for( i=1 ; i<argc ; i++ )
{
if ( !Q_stricmp( argv[i], "-StaticPropLighting" ) )
{
g_bStaticPropLighting = true;
}
else if ( !stricmp( argv[i], "-StaticPropNormals" ) )
{
g_bShowStaticPropNormals = true;
}
else if ( !stricmp( argv[i], "-OnlyStaticProps" ) )
{
g_bOnlyStaticProps = true;
}
else if ( !Q_stricmp( argv[i], "-StaticPropPolys" ) )
{
g_bStaticPropPolys = true;
}
else if ( !Q_stricmp( argv[i], "-nossprops" ) )
{
g_bDisablePropSelfShadowing = true;
}
else if ( !Q_stricmp( argv[i], "-textureshadows" ) )
{
g_bTextureShadows = true;
}
else if ( !strcmp(argv[i], "-dump") )
{
g_bDumpPatches = true;
}
else if ( !Q_stricmp( argv[i], "-nodetaillight" ) )
{
g_bNoDetailLighting = true;
}
else if ( !Q_stricmp( argv[i], "-rederrors" ) )
{
bRed2Black = false;
}
else if ( !Q_stricmp( argv[i], "-dumpnormals" ) )
{
bDumpNormals = true;
}
else if ( !Q_stricmp( argv[i], "-dumptrace" ) )
{
g_bDumpRtEnv = true;
}
else if ( !Q_stricmp( argv[i], "-LargeDispSampleRadius" ) )
{
g_bLargeDispSampleRadius = true;
}
else if (!Q_stricmp( argv[i], "-dumppropmaps"))
{
g_bDumpPropLightmaps = true;
}
else if (!Q_stricmp(argv[i],"-bounce"))
{
if ( ++i < argc )
{
int bounceParam = atoi (argv[i]);
if ( bounceParam < 0 )
{
Warning("Error: expected non-negative value after '-bounce'\n" );
return -1;
}
numbounce = (unsigned)bounceParam;
}
else
{
Warning("Error: expected a value after '-bounce'\n" );
return -1;
}
}
else if (!Q_stricmp(argv[i],"-verbose") || !Q_stricmp(argv[i],"-v"))
{
verbose = true;
}
else if (!Q_stricmp(argv[i],"-threads"))
{
if ( ++i < argc )
{
numthreads = atoi (argv[i]);
if ( numthreads <= 0 )
{
Warning("Error: expected positive value after '-threads'\n" );
return -1;
}
}
else
{
Warning("Error: expected a value after '-threads'\n" );
return -1;
}
}
else if ( !Q_stricmp(argv[i], "-lights" ) )
{
if ( ++i < argc && *argv[i] )
{
strcpy( designer_lights, argv[i] );
}
else
{
Warning("Error: expected a filepath after '-lights'\n" );
return -1;
}
}
else if (!Q_stricmp(argv[i],"-noextra"))
{
do_extra = false;
}
else if (!Q_stricmp(argv[i],"-debugextra"))
{
debug_extra = true;
}
else if ( !Q_stricmp(argv[i], "-fastambient") )
{
g_bFastAmbient = true;
}
else if (!Q_stricmp(argv[i],"-fast"))
{
do_fast = true;
}
else if (!Q_stricmp(argv[i],"-noskyboxrecurse"))
{
g_bNoSkyRecurse = true;
}
else if (!Q_stricmp(argv[i],"-final"))
{
g_flSkySampleScale = 16.0;
}
else if (!Q_stricmp(argv[i],"-extrasky"))
{
if ( ++i < argc && *argv[i] )
{
g_flSkySampleScale = atof( argv[i] );
}
else
{
Warning("Error: expected a scale factor after '-extrasky'\n" );
return -1;
}
}
else if (!Q_stricmp(argv[i],"-centersamples"))
{
do_centersamples = true;
}
else if (!Q_stricmp(argv[i],"-smooth"))
{
if ( ++i < argc )
{
smoothing_threshold = (float)cos(atof(argv[i])*(M_PI/180.0));
}
else
{
Warning("Error: expected an angle after '-smooth'\n" );
return -1;
}
}
else if (!Q_stricmp(argv[i],"-dlightmap"))
{
dlight_map = 1;
}
else if (!Q_stricmp(argv[i],"-luxeldensity"))
{
if ( ++i < argc )
{
luxeldensity = (float)atof (argv[i]);
if (luxeldensity > 1.0)
luxeldensity = 1.0 / luxeldensity;
}
else
{
Warning("Error: expected a value after '-luxeldensity'\n" );
return -1;
}
}
else if( !Q_stricmp( argv[i], "-low" ) )
{
g_bLowPriority = true;
}
else if( !Q_stricmp( argv[i], "-loghash" ) )
{
g_bLogHashData = true;
}
else if( !Q_stricmp( argv[i], "-onlydetail" ) )
{
*onlydetail = true;
}
else if (!Q_stricmp(argv[i],"-softsun"))
{
if ( ++i < argc )
{
g_SunAngularExtent=atof(argv[i]);
g_SunAngularExtent=sin((M_PI/180.0)*g_SunAngularExtent);
printf("sun extent=%f\n",g_SunAngularExtent);
}
else
{
Warning("Error: expected an angular extent value (0..180) '-softsun'\n" );
return -1;
}
}
else if ( !Q_stricmp( argv[i], "-maxdispsamplesize" ) )
{
if ( ++i < argc )
{
g_flMaxDispSampleSize = ( float )atof( argv[i] );
}
else
{
Warning( "Error: expected a sample size after '-maxdispsamplesize'\n" );
return -1;
}
}
else if ( stricmp( argv[i], "-StopOnExit" ) == 0 )
{
g_bStopOnExit = true;
}
else if ( stricmp( argv[i], "-steam" ) == 0 )
{
}
else if ( stricmp( argv[i], "-allowdebug" ) == 0 )
{
// Don't need to do anything, just don't error out.
}
else if ( !Q_stricmp( argv[i], CMDLINEOPTION_NOVCONFIG ) )
{
}
else if ( !Q_stricmp( argv[i], "-vproject" ) || !Q_stricmp( argv[i], "-game" ) || !Q_stricmp( argv[i], "-insert_search_path" ) )
{
++i;
}
else if ( !Q_stricmp( argv[i], "-FullMinidumps" ) )
{
EnableFullMinidumps( true );
}
else if ( !Q_stricmp( argv[i], "-hdr" ) )
{
SetHDRMode( true );
}
else if ( !Q_stricmp( argv[i], "-ldr" ) )
{
SetHDRMode( false );
}
else if (!Q_stricmp(argv[i],"-maxchop"))
{
if ( ++i < argc )
{
maxchop = (float)atof (argv[i]);
if ( maxchop < 1 )
{
Warning("Error: expected positive value after '-maxchop'\n" );
return -1;
}
}
else
{
Warning("Error: expected a value after '-maxchop'\n" );
return -1;
}
}
else if (!Q_stricmp(argv[i],"-chop"))
{
if ( ++i < argc )
{
minchop = (float)atof (argv[i]);
if ( minchop < 1 )
{
Warning("Error: expected positive value after '-chop'\n" );
return -1;
}
minchop = min( minchop, maxchop );
}
else
{
Warning("Error: expected a value after '-chop'\n" );
return -1;
}
}
else if ( !Q_stricmp( argv[i], "-dispchop" ) )
{
if ( ++i < argc )
{
dispchop = ( float )atof( argv[i] );
if ( dispchop < 1.0f )
{
Warning( "Error: expected positive value after '-dipschop'\n" );
return -1;
}
}
else
{
Warning( "Error: expected a value after '-dispchop'\n" );
return -1;
}
}
else if ( !Q_stricmp( argv[i], "-disppatchradius" ) )
{
if ( ++i < argc )
{
g_MaxDispPatchRadius = ( float )atof( argv[i] );
if ( g_MaxDispPatchRadius < 10.0f )
{
Warning( "Error: g_MaxDispPatchRadius < 10.0\n" );
return -1;
}
}
else
{
Warning( "Error: expected a value after '-disppatchradius'\n" );
return -1;
}
}
#if ALLOWDEBUGOPTIONS
else if (!Q_stricmp(argv[i],"-scale"))
{
if ( ++i < argc )
{
lightscale = (float)atof (argv[i]);
}
else
{
Warning("Error: expected a value after '-scale'\n" );
return -1;
}
}
else if (!Q_stricmp(argv[i],"-ambient"))
{
if ( i+3 < argc )
{
ambient[0] = (float)atof (argv[++i]) * 128;
ambient[1] = (float)atof (argv[++i]) * 128;
ambient[2] = (float)atof (argv[++i]) * 128;
}
else
{
Warning("Error: expected three color values after '-ambient'\n" );
return -1;
}
}
else if (!Q_stricmp(argv[i],"-dlight"))
{
if ( ++i < argc )
{
dlight_threshold = (float)atof (argv[i]);
}
else
{
Warning("Error: expected a value after '-dlight'\n" );
return -1;
}
}
else if (!Q_stricmp(argv[i],"-sky"))
{
if ( ++i < argc )
{
indirect_sun = (float)atof (argv[i]);
}
else
{
Warning("Error: expected a value after '-sky'\n" );
return -1;
}
}
else if (!Q_stricmp(argv[i],"-notexscale"))
{
texscale = false;
}
else if (!Q_stricmp(argv[i],"-coring"))
{
if ( ++i < argc )
{
coring = (float)atof( argv[i] );
}
else
{
Warning("Error: expected a light threshold after '-coring'\n" );
return -1;
}
}
#endif
// NOTE: the -mpi checks must come last here because they allow the previous argument
// to be -mpi as well. If it game before something else like -game, then if the previous
// argument was -mpi and the current argument was something valid like -game, it would skip it.
else if ( !Q_strncasecmp( argv[i], "-mpi", 4 ) || !Q_strncasecmp( argv[i-1], "-mpi", 4 ) )
{
if ( stricmp( argv[i], "-mpi" ) == 0 )
g_bUseMPI = true;
// Any other args that start with -mpi are ok too.
if ( i == argc - 1 && V_stricmp( argv[i], "-mpi_ListParams" ) != 0 )
break;
}
else if ( mapArg == -1 )
{
mapArg = i;
}
else
{
return -1;
}
}
return mapArg;
}
void PrintCommandLine( int argc, char **argv )
{
Warning( "Command line: " );
for ( int z=0; z < argc; z++ )
{
Warning( "\"%s\" ", argv[z] );
}
Warning( "\n\n" );
}
void PrintUsage( int argc, char **argv )
{
PrintCommandLine( argc, argv );
Warning(
"usage : vrad [options...] bspfile\n"
"example: vrad c:\\hl2\\hl2\\maps\\test\n"
"\n"
"Common options:\n"
"\n"
" -v (or -verbose): Turn on verbose output (also shows more command\n"
" -bounce # : Set max number of bounces (default: 100).\n"
" -fast : Quick and dirty lighting.\n"
" -fastambient : Per-leaf ambient sampling is lower quality to save compute time.\n"
" -final : High quality processing. equivalent to -extrasky 16.\n"
" -extrasky n : trace N times as many rays for indirect light and sky ambient.\n"
" -low : Run as an idle-priority process.\n"
" -mpi : Use VMPI to distribute computations.\n"
" -rederror : Show errors in red.\n"
"\n"
" -vproject <directory> : Override the VPROJECT environment variable.\n"
" -game <directory> : Same as -vproject.\n"
"\n"
"Other options:\n"
" -novconfig : Don't bring up graphical UI on vproject errors.\n"
" -dump : Write debugging .txt files.\n"
" -dumpnormals : Write normals to debug files.\n"
" -dumptrace : Write ray-tracing environment to debug files.\n"
" -threads : Control the number of threads vbsp uses (defaults to the #\n"
" or processors on your machine).\n"
" -lights <file> : Load a lights file in addition to lights.rad and the\n"
" level lights file.\n"
" -noextra : Disable supersampling.\n"
" -debugextra : Places debugging data in lightmaps to visualize\n"
" supersampling.\n"
" -smooth # : Set the threshold for smoothing groups, in degrees\n"
" (default 45).\n"
" -dlightmap : Force direct lighting into different lightmap than\n"
" radiosity.\n"
" -stoponexit : Wait for a keypress on exit.\n"
" -mpi_pw <pw> : Use a password to choose a specific set of VMPI workers.\n"
" -nodetaillight : Don't light detail props.\n"
" -centersamples : Move sample centers.\n"
" -luxeldensity # : Rescale all luxels by the specified amount (default: 1.0).\n"
" The number specified must be less than 1.0 or it will be\n"
" ignored.\n"
" -loghash : Log the sample hash table to samplehash.txt.\n"
" -onlydetail : Only light detail props and per-leaf lighting.\n"
" -maxdispsamplesize #: Set max displacement sample size (default: 512).\n"
" -softsun <n> : Treat the sun as an area light source of size <n> degrees."
" Produces soft shadows.\n"
" Recommended values are between 0 and 5. Default is 0.\n"
" -FullMinidumps : Write large minidumps on crash.\n"
" -chop : Smallest number of luxel widths for a bounce patch, used on edges\n"
" -maxchop : Coarsest allowed number of luxel widths for a patch, used in face interiors\n"
"\n"
" -LargeDispSampleRadius: This can be used if there are splotches of bounced light\n"
" on terrain. The compile will take longer, but it will gather\n"
" light across a wider area.\n"
" -StaticPropLighting : generate backed static prop vertex lighting\n"
" -StaticPropPolys : Perform shadow tests of static props at polygon precision\n"
" -OnlyStaticProps : Only perform direct static prop lighting (vrad debug option)\n"
" -StaticPropNormals : when lighting static props, just show their normal vector\n"
" -textureshadows : Allows texture alpha channels to block light - rays intersecting alpha surfaces will sample the texture\n"
" -noskyboxrecurse : Turn off recursion into 3d skybox (skybox shadows on world)\n"
" -nossprops : Globally disable self-shadowing on static props\n"
"\n"
#if 1 // Disabled for the initial SDK release with VMPI so we can get feedback from selected users.
);
#else
" -mpi_ListParams : Show a list of VMPI parameters.\n"
"\n"
);
// Show VMPI parameters?
for ( int i=1; i < argc; i++ )
{
if ( V_stricmp( argv[i], "-mpi_ListParams" ) == 0 )
{
Warning( "VMPI-specific options:\n\n" );
bool bIsSDKMode = VMPI_IsSDKMode();
for ( int i=k_eVMPICmdLineParam_FirstParam+1; i < k_eVMPICmdLineParam_LastParam; i++ )
{
if ( (VMPI_GetParamFlags( (EVMPICmdLineParam)i ) & VMPI_PARAM_SDK_HIDDEN) && bIsSDKMode )
continue;
Warning( "[%s]\n", VMPI_GetParamString( (EVMPICmdLineParam)i ) );
Warning( VMPI_GetParamHelpString( (EVMPICmdLineParam)i ) );
Warning( "\n\n" );
}
break;
}
}
#endif
}
int RunVRAD( int argc, char **argv )
{
#if defined(_MSC_VER) && ( _MSC_VER >= 1310 )
Msg("Valve Software - vrad.exe SSE (" __DATE__ ")\n" );
#else
Msg("Valve Software - vrad.exe (" __DATE__ ")\n" );
#endif
Msg("\n Valve Radiosity Simulator \n");
verbose = true; // Originally FALSE
bool onlydetail;
int i = ParseCommandLine( argc, argv, &onlydetail );
if (i == -1)
{
PrintUsage( argc, argv );
DeleteCmdLine( argc, argv );
CmdLib_Exit( 1 );
}
// Initialize the filesystem, so additional commandline options can be loaded
Q_StripExtension( argv[ i ], source, sizeof( source ) );
CmdLib_InitFileSystem( argv[ i ] );
Q_FileBase( source, source, sizeof( source ) );
VRAD_LoadBSP( argv[i] );
if ( (! onlydetail) && (! g_bOnlyStaticProps ) )
{
RadWorld_Go();
}
VRAD_ComputeOtherLighting();
VRAD_Finish();
VMPI_SetCurrentStage( "master done" );
DeleteCmdLine( argc, argv );
CmdLib_Cleanup();
return 0;
}
int VRAD_Main(int argc, char **argv)
{
g_pFileSystem = NULL; // Safeguard against using it before it's properly initialized.
VRAD_Init();
// This must come first.
VRAD_SetupMPI( argc, argv );
#if !defined( _DEBUG )
if ( g_bUseMPI && !g_bMPIMaster )
{
SetupToolsMinidumpHandler( VMPI_ExceptionFilter );
}
else
#endif
{
LoadCmdLineFromFile( argc, argv, source, "vrad" ); // Don't do this if we're a VMPI worker..
SetupDefaultToolsMinidumpHandler();
}
return RunVRAD( argc, argv );
}
|