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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//===========================================================================//
#include "IOcclusionSystem.h"
#include "mathlib/vector.h"
#include "UtlSortVector.h"
#include "utllinkedlist.h"
#include "utlvector.h"
#include "collisionutils.h"
#include "filesystem.h"
#include "gl_model_private.h"
#include "gl_matsysiface.h"
#include "client.h"
#include "gl_shader.h"
#include "materialsystem/imesh.h"
#include "tier0/vprof.h"
#include "tier0/icommandline.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
// Uncomment this if you want to get a whole bunch of paranoid error checking
// #define DEBUG_OCCLUSION_SYSTEM 1
//-----------------------------------------------------------------------------
// Used to visualizes what the occlusion system is doing.
//-----------------------------------------------------------------------------
#ifdef _X360
#define DEFAULT_MIN_OCCLUDER_AREA 70.0f
#else
#define DEFAULT_MIN_OCCLUDER_AREA 5.0f
#endif
#define DEFAULT_MAX_OCCLUDEE_AREA 5.0f
ConVar r_visocclusion( "r_visocclusion", "0", FCVAR_CHEAT, "Activate/deactivate wireframe rendering of what the occlusion system is doing." );
ConVar r_occlusion( "r_occlusion", "1", 0, "Activate/deactivate the occlusion system." );
static ConVar r_occludermincount( "r_occludermincount", "0", 0, "At least this many occluders will be used, no matter how big they are." );
static ConVar r_occlusionspew( "r_occlusionspew", "0", FCVAR_CHEAT, "Activate/deactivates spew about what the occlusion system is doing." );
ConVar r_occluderminarea( "r_occluderminarea", "0", 0, "Prevents this occluder from being used if it takes up less than X% of the screen. 0 means use whatever the level said to use." );
ConVar r_occludeemaxarea( "r_occludeemaxarea", "0", 0, "Prevents occlusion testing for entities that take up more than X% of the screen. 0 means use whatever the level said to use." );
#ifdef DEBUG_OCCLUSION_SYSTEM
static ConVar r_occtest( "r_occtest", "0" );
// Set this in the debugger to activate debugging spew
bool s_bSpew = false;
#endif // DEBUG_OCCLUSION_SYSTEM
//-----------------------------------------------------------------------------
// Visualization
//-----------------------------------------------------------------------------
struct EdgeVisualizationInfo_t
{
Vector m_vecPoint[2];
unsigned char m_pColor[4];
};
//-----------------------------------------------------------------------------
// Queued up rendering
//-----------------------------------------------------------------------------
static CUtlVector<EdgeVisualizationInfo_t> g_EdgeVisualization;
//-----------------------------------------------------------------------------
//
// Edge list that's fast to iterate over, fast to insert into
//
//-----------------------------------------------------------------------------
class CWingedEdgeList
{
public:
struct WingedEdge_t
{
Vector m_vecPosition; // of the upper point in y, measured in screen space
Vector m_vecPositionEnd; // of the lower point in y, measured in screen space
float m_flDxDy; // Change in x per unit in y.
float m_flOODy;
float m_flX;
short m_nLeaveSurfID; // Unique index of the surface this is a part of
short m_nEnterSurfID; // Unique index of the surface this is a part of
WingedEdge_t *m_pPrevActiveEdge;
WingedEdge_t *m_pNextActiveEdge;
};
public:
CWingedEdgeList();
// Clears out the edge list
void Clear();
// Iteration
int EdgeCount() const;
WingedEdge_t &WingedEdge( int i );
// Adds an edge
int AddEdge( );
int AddEdge( const Vector &vecStartVert, const Vector &vecEndVert, int nLeaveSurfID, int nEnterSurfID );
// Adds a surface
int AddSurface( const cplane_t &plane );
// Does this edge list occlude another winged edge list?
bool IsOccludingEdgeList( CWingedEdgeList &testList );
// Queues up stuff to visualize
void QueueVisualization( unsigned char *pColor );
// Renders the winged edge list
void Visualize( unsigned char *pColor );
// Checks consistency of the edge list...
void CheckConsistency();
private:
struct Surface_t
{
cplane_t m_Plane; // measured in projection space
};
private:
// Active edges...
WingedEdge_t *FirstActiveEdge( );
WingedEdge_t *LastActiveEdge( );
bool AtListEnd( const WingedEdge_t* pEdge ) const;
bool AtListStart( const WingedEdge_t* pEdge ) const;
void LinkActiveEdgeAfter( WingedEdge_t *pPrevEdge, WingedEdge_t *pInsertEdge );
void UnlinkActiveEdge( WingedEdge_t *pEdge );
// Used to insert an edge into the active edge list
bool IsEdgeXGreater( const WingedEdge_t *pEdge1, const WingedEdge_t *pEdge2 );
// Clears the active edge list
void ResetActiveEdgeList();
// Spew active edge list
void SpewActiveEdgeList( float y, bool bHex = false );
// Inserts an edge into the active edge list, sorted by X
void InsertActiveEdge( WingedEdge_t *pPrevEdge, WingedEdge_t *pInsertEdge );
// Returns true if this active edge list occludes another active edge list
bool IsOccludingActiveEdgeList( CWingedEdgeList &testList, float y );
// Advances the X values of the active edge list, with no reordering
bool AdvanceActiveEdgeList( float flCurrY );
// Advance the active edge list until a particular X value is reached.
WingedEdge_t *AdvanceActiveEdgeListToX( WingedEdge_t *pEdge, float x );
// Returns the z value of a surface given and x,y coordinate
float ComputeZValue( const Surface_t *pSurface, float x, float y ) const;
// Returns the next time in Y the edge list will undergo a change
float NextDiscontinuity() const;
private:
// Active Edge list...
WingedEdge_t m_StartTerminal;
WingedEdge_t m_EndTerminal;
// Back surface...
Surface_t m_BackSurface;
// Next discontinuity..
float m_flNextDiscontinuity;
int m_nCurrentEdgeIndex;
CUtlVector< WingedEdge_t > m_WingedEdges;
CUtlVector< Surface_t > m_Surfaces;
};
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CWingedEdgeList::CWingedEdgeList() : m_WingedEdges( 0, 64 )
{
m_StartTerminal.m_vecPosition.Init( -FLT_MAX, -FLT_MAX, -FLT_MAX );
m_StartTerminal.m_vecPositionEnd.Init( -FLT_MAX, FLT_MAX, -FLT_MAX );
m_StartTerminal.m_nLeaveSurfID = -1;
m_StartTerminal.m_nEnterSurfID = -1;
m_StartTerminal.m_pPrevActiveEdge = NULL;
m_StartTerminal.m_pNextActiveEdge = NULL;
m_StartTerminal.m_flDxDy = 0.0f;
m_StartTerminal.m_flOODy = 0.0f;
m_StartTerminal.m_flX = -FLT_MAX;
m_EndTerminal.m_vecPosition.Init( FLT_MAX, -FLT_MAX, -FLT_MAX );
m_EndTerminal.m_vecPositionEnd.Init( FLT_MAX, FLT_MAX, -FLT_MAX );
m_EndTerminal.m_nLeaveSurfID = -1;
m_EndTerminal.m_nEnterSurfID = -1;
m_EndTerminal.m_pPrevActiveEdge = NULL;
m_EndTerminal.m_pNextActiveEdge = NULL;
m_EndTerminal.m_flDxDy = 0.0f;
m_EndTerminal.m_flOODy = 0.0f;
m_EndTerminal.m_flX = FLT_MAX;
m_BackSurface.m_Plane.normal.Init( 0, 0, 1 );
m_BackSurface.m_Plane.dist = FLT_MAX;
}
//-----------------------------------------------------------------------------
// Renders the winged edge list for debugging
//-----------------------------------------------------------------------------
void CWingedEdgeList::Clear()
{
m_WingedEdges.RemoveAll();
m_Surfaces.RemoveAll();
}
//-----------------------------------------------------------------------------
// Iterate over the winged edges
//-----------------------------------------------------------------------------
inline int CWingedEdgeList::EdgeCount() const
{
return m_WingedEdges.Count();
}
inline CWingedEdgeList::WingedEdge_t &CWingedEdgeList::WingedEdge( int i )
{
return m_WingedEdges[i];
}
//-----------------------------------------------------------------------------
// Adds new edges
//-----------------------------------------------------------------------------
inline int CWingedEdgeList::AddEdge( )
{
int i = m_WingedEdges.AddToTail();
WingedEdge_t &newEdge = m_WingedEdges[i];
newEdge.m_pPrevActiveEdge = NULL;
newEdge.m_pNextActiveEdge = NULL;
return i;
}
int CWingedEdgeList::AddEdge( const Vector &vecStartVert, const Vector &vecEndVert, int nLeaveSurfID, int nEnterSurfID )
{
// This is true if we've clipped to the near clip plane
Assert( (vecStartVert.z >= 0.0) && (vecEndVert.z >= 0.0) );
// Don't bother adding edges with dy == 0
float dy;
dy = vecEndVert.y - vecStartVert.y;
if (dy == 0.0f)
return -1;
int i = m_WingedEdges.AddToTail();
WingedEdge_t &newEdge = m_WingedEdges[i];
newEdge.m_flOODy = 1.0f / dy;
newEdge.m_nLeaveSurfID = nLeaveSurfID;
newEdge.m_nEnterSurfID = nEnterSurfID;
newEdge.m_vecPosition = vecStartVert;
newEdge.m_vecPositionEnd = vecEndVert;
newEdge.m_pPrevActiveEdge = NULL;
newEdge.m_pNextActiveEdge = NULL;
newEdge.m_flDxDy = (vecEndVert.x - vecStartVert.x) * newEdge.m_flOODy;
return i;
}
//-----------------------------------------------------------------------------
// Adds new surfaces
//-----------------------------------------------------------------------------
int CWingedEdgeList::AddSurface( const cplane_t &plane )
{
int i = m_Surfaces.AddToTail();
m_Surfaces[i].m_Plane = plane;
return i;
}
//-----------------------------------------------------------------------------
// Active edges...
//-----------------------------------------------------------------------------
inline CWingedEdgeList::WingedEdge_t *CWingedEdgeList::FirstActiveEdge( )
{
return m_StartTerminal.m_pNextActiveEdge;
}
inline CWingedEdgeList::WingedEdge_t *CWingedEdgeList::LastActiveEdge( )
{
return m_EndTerminal.m_pPrevActiveEdge;
}
inline bool CWingedEdgeList::AtListEnd( const WingedEdge_t* pEdge ) const
{
return pEdge == &m_EndTerminal;
}
inline bool CWingedEdgeList::AtListStart( const WingedEdge_t* pEdge ) const
{
return pEdge == &m_StartTerminal;
}
inline void CWingedEdgeList::LinkActiveEdgeAfter( WingedEdge_t *pPrevEdge, WingedEdge_t *pInsertEdge )
{
pInsertEdge->m_pNextActiveEdge = pPrevEdge->m_pNextActiveEdge;
pInsertEdge->m_pPrevActiveEdge = pPrevEdge;
pInsertEdge->m_pNextActiveEdge->m_pPrevActiveEdge = pInsertEdge;
pPrevEdge->m_pNextActiveEdge = pInsertEdge;
}
inline void CWingedEdgeList::UnlinkActiveEdge( WingedEdge_t *pEdge )
{
pEdge->m_pPrevActiveEdge->m_pNextActiveEdge = pEdge->m_pNextActiveEdge;
pEdge->m_pNextActiveEdge->m_pPrevActiveEdge = pEdge->m_pPrevActiveEdge;
#ifdef _DEBUG
pEdge->m_pPrevActiveEdge = pEdge->m_pNextActiveEdge = NULL;
#endif
}
//-----------------------------------------------------------------------------
// Checks consistency of the edge list...
//-----------------------------------------------------------------------------
void CWingedEdgeList::CheckConsistency()
{
float flLastY = -FLT_MAX;
float flLastX = -FLT_MAX;
float flLastDxDy = 0;
int nEdgeCount = EdgeCount();
for ( int i = 0; i < nEdgeCount; ++i )
{
WingedEdge_t *pEdge = &WingedEdge(i);
Assert( pEdge->m_vecPosition.y >= flLastY );
if ( pEdge->m_vecPosition.y == flLastY )
{
Assert( pEdge->m_vecPosition.x >= flLastX );
if ( pEdge->m_vecPosition.x == flLastX )
{
Assert( pEdge->m_flDxDy >= flLastDxDy );
}
}
flLastX = pEdge->m_vecPosition.x;
flLastY = pEdge->m_vecPosition.y;
flLastDxDy = pEdge->m_flDxDy;
}
ResetActiveEdgeList();
float flCurrentY = NextDiscontinuity();
AdvanceActiveEdgeList( flCurrentY );
while ( flCurrentY != FLT_MAX )
{
// Make sure all edges have correct Xs + enter + leave surfaces..
int nCurrentSurfID = -1;
float flX = -FLT_MAX;
WingedEdge_t *pCurEdge = FirstActiveEdge();
while ( !AtListEnd( pCurEdge ) )
{
Assert( pCurEdge->m_nLeaveSurfID == nCurrentSurfID );
Assert( pCurEdge->m_flX >= flX );
Assert( pCurEdge->m_nLeaveSurfID != pCurEdge->m_nEnterSurfID );
nCurrentSurfID = pCurEdge->m_nEnterSurfID;
flX = pCurEdge->m_flX;
pCurEdge = pCurEdge->m_pNextActiveEdge;
}
// Assert( nCurrentSurfID == -1 );
flCurrentY = NextDiscontinuity();
AdvanceActiveEdgeList( flCurrentY );
}
}
//-----------------------------------------------------------------------------
// Returns the z value of a surface given and x,y coordinate
//-----------------------------------------------------------------------------
inline float CWingedEdgeList::ComputeZValue( const Surface_t *pSurface, float x, float y ) const
{
const cplane_t &plane = pSurface->m_Plane;
Assert( plane.normal.z == 1.0f );
return plane.dist - plane.normal.x * x - plane.normal.y * y;
}
//-----------------------------------------------------------------------------
// Used to insert an edge into the active edge list, sorted by X
// If Xs match, sort by Dx/Dy
//-----------------------------------------------------------------------------
inline bool CWingedEdgeList::IsEdgeXGreater( const WingedEdge_t *pEdge1, const WingedEdge_t *pEdge2 )
{
float flDelta = pEdge1->m_flX - pEdge2->m_flX;
if ( flDelta > 0 )
return true;
if ( flDelta < 0 )
return false;
// NOTE: Using > instead of >= means coincident edges won't continually swap places
return pEdge1->m_flDxDy > pEdge2->m_flDxDy;
}
//-----------------------------------------------------------------------------
// Inserts an edge into the active edge list, sorted by X
//-----------------------------------------------------------------------------
inline void CWingedEdgeList::InsertActiveEdge( WingedEdge_t *pPrevEdge, WingedEdge_t *pInsertEdge )
{
while( !AtListStart(pPrevEdge) && IsEdgeXGreater( pPrevEdge, pInsertEdge ) )
{
pPrevEdge = pPrevEdge->m_pPrevActiveEdge;
}
LinkActiveEdgeAfter( pPrevEdge, pInsertEdge );
}
//-----------------------------------------------------------------------------
// Clears the active edge list
//-----------------------------------------------------------------------------
void CWingedEdgeList::ResetActiveEdgeList()
{
// This shouldn't be called unless we're about to do active edge checking
Assert( EdgeCount() );
m_nCurrentEdgeIndex = 0;
// Don't bother with edges below the screen edge
m_flNextDiscontinuity = WingedEdge( 0 ).m_vecPosition.y;
m_flNextDiscontinuity = max( m_flNextDiscontinuity, -1.0f );
m_StartTerminal.m_pNextActiveEdge = &m_EndTerminal;
m_EndTerminal.m_pPrevActiveEdge = &m_StartTerminal;
Assert( m_StartTerminal.m_pPrevActiveEdge == NULL );
Assert( m_EndTerminal.m_pNextActiveEdge == NULL );
}
//-----------------------------------------------------------------------------
// Spew active edge list
//-----------------------------------------------------------------------------
void CWingedEdgeList::SpewActiveEdgeList( float y, bool bHex )
{
WingedEdge_t *pEdge = FirstActiveEdge();
Msg( "%.3f : ", y );
while ( !AtListEnd( pEdge ) )
{
if (!bHex)
{
Msg( "(%d %.3f [%d/%d]) ", (int)(pEdge - m_WingedEdges.Base()), pEdge->m_flX, pEdge->m_nLeaveSurfID, pEdge->m_nEnterSurfID );
}
else
{
Msg( "(%d %X [%d/%d]) ", (int)(pEdge - m_WingedEdges.Base()), *(int*)&pEdge->m_flX, pEdge->m_nLeaveSurfID, pEdge->m_nEnterSurfID );
}
pEdge = pEdge->m_pNextActiveEdge;
}
Msg( "\n" );
}
//-----------------------------------------------------------------------------
// Returns the next time in Y the edge list will undergo a change
//-----------------------------------------------------------------------------
inline float CWingedEdgeList::NextDiscontinuity() const
{
return m_flNextDiscontinuity;
}
//-----------------------------------------------------------------------------
// Advances the X values of the active edge list, with no reordering
//-----------------------------------------------------------------------------
bool CWingedEdgeList::AdvanceActiveEdgeList( float flCurrY )
{
// Reordering is unnecessary because the winged edges are guaranteed to be non-overlapping
m_flNextDiscontinuity = FLT_MAX;
// Advance all edges until the current Y; we don't need to re-order *any* edges.
WingedEdge_t *pCurEdge;
WingedEdge_t *pNextEdge;
for ( pCurEdge = FirstActiveEdge(); !AtListEnd( pCurEdge ); pCurEdge = pNextEdge )
{
pNextEdge = pCurEdge->m_pNextActiveEdge;
if ( pCurEdge->m_vecPositionEnd.y <= flCurrY )
{
UnlinkActiveEdge( pCurEdge );
continue;
}
pCurEdge->m_flX = pCurEdge->m_vecPosition.x + (flCurrY - pCurEdge->m_vecPosition.y) * pCurEdge->m_flDxDy;
if ( pCurEdge->m_vecPositionEnd.y < m_flNextDiscontinuity )
{
m_flNextDiscontinuity = pCurEdge->m_vecPositionEnd.y;
}
}
int nEdgeCount = EdgeCount();
if ( m_nCurrentEdgeIndex == nEdgeCount )
return (m_flNextDiscontinuity != FLT_MAX);
pCurEdge = &WingedEdge( m_nCurrentEdgeIndex );
// Add new edges, computing the x + z coordinates at the requested y value
while ( pCurEdge->m_vecPosition.y <= flCurrY )
{
// This is necessary because of our initial skip up to y == -1.0f
if ( pCurEdge->m_vecPositionEnd.y > flCurrY )
{
float flDy = flCurrY - pCurEdge->m_vecPosition.y;
pCurEdge->m_flX = pCurEdge->m_vecPosition.x + flDy * pCurEdge->m_flDxDy;
if ( pCurEdge->m_vecPositionEnd.y < m_flNextDiscontinuity )
{
m_flNextDiscontinuity = pCurEdge->m_vecPositionEnd.y;
}
// Now re-insert in the list, sorted by X
InsertActiveEdge( LastActiveEdge(), pCurEdge );
}
if ( ++m_nCurrentEdgeIndex == nEdgeCount )
return (m_flNextDiscontinuity != FLT_MAX);
pCurEdge = &WingedEdge( m_nCurrentEdgeIndex );
}
// The next edge in y will also present a discontinuity
if ( pCurEdge->m_vecPosition.y < m_flNextDiscontinuity )
{
m_flNextDiscontinuity = pCurEdge->m_vecPosition.y;
}
return (m_flNextDiscontinuity != FLT_MAX);
}
//-----------------------------------------------------------------------------
// Advance the active edge list until a particular X value is reached.
//-----------------------------------------------------------------------------
inline CWingedEdgeList::WingedEdge_t *CWingedEdgeList::AdvanceActiveEdgeListToX( WingedEdge_t *pEdge, float x )
{
// <= is necessary because we always want to point *after* the edge
while( pEdge->m_flX <= x )
{
pEdge = pEdge->m_pNextActiveEdge;
}
return pEdge;
}
//-----------------------------------------------------------------------------
// Returns true if this active edge list occludes another active edge list
//-----------------------------------------------------------------------------
bool CWingedEdgeList::IsOccludingActiveEdgeList( CWingedEdgeList &testList, float y )
{
WingedEdge_t *pTestEdge = testList.FirstActiveEdge();
// If the occludee is off screen, it's occluded
if ( pTestEdge->m_flX >= 1.0f )
return true;
pTestEdge = AdvanceActiveEdgeListToX( pTestEdge, -1.0f );
// If all occludee edges have x values <= -1.0f, it's occluded
if ( testList.AtListEnd( pTestEdge ) )
return true;
// Start at the first edge whose x value is <= -1.0f
// if the occludee goes off the left side of the screen.
float flNextTestX = pTestEdge->m_flX;
if ( !testList.AtListStart( pTestEdge->m_pPrevActiveEdge ) )
{
// In this case, we should be on a span crossing from x <= -1.0f to x > 1.0f.
// Do the first occlusion test at x = -1.0f.
Assert( pTestEdge->m_flX > -1.0f );
pTestEdge = pTestEdge->m_pPrevActiveEdge;
Assert( pTestEdge->m_flX <= -1.0f );
flNextTestX = -1.0f;
}
WingedEdge_t *pOccluderEdge = FirstActiveEdge();
pOccluderEdge = AdvanceActiveEdgeListToX( pOccluderEdge, flNextTestX );
Surface_t *pTestSurf = (pTestEdge->m_nEnterSurfID >= 0) ? &testList.m_Surfaces[pTestEdge->m_nEnterSurfID] : &m_BackSurface;
// Use the leave surface because we know the occluder has been advanced *beyond* the test surf X.
Surface_t *pOccluderSurf = (pOccluderEdge->m_nLeaveSurfID >= 0) ? &m_Surfaces[pOccluderEdge->m_nLeaveSurfID] : &m_BackSurface;
float flCurrentX = flNextTestX;
float flNextOccluderX = pOccluderEdge->m_flX;
flNextTestX = pTestEdge->m_pNextActiveEdge->m_flX;
while ( true )
{
// Is the occludee in front of the occluder? No dice!
float flTestOOz = ComputeZValue( pTestSurf, flCurrentX, y );
float flOccluderOOz = ComputeZValue( pOccluderSurf, flCurrentX, y );
if ( flTestOOz < flOccluderOOz )
return false;
// We're done if there's no more occludees
if ( flNextTestX == FLT_MAX )
return true;
// We're done if there's no more occluders
if ( flNextOccluderX == FLT_MAX )
return false;
if ( flNextTestX <= flNextOccluderX )
{
flCurrentX = flNextTestX;
pTestEdge = pTestEdge->m_pNextActiveEdge;
if ( pTestEdge->m_nEnterSurfID >= 0 )
{
pTestSurf = &testList.m_Surfaces[pTestEdge->m_nEnterSurfID];
}
else
{
pTestSurf = (pTestEdge->m_nLeaveSurfID >= 0) ? &testList.m_Surfaces[pTestEdge->m_nLeaveSurfID] : &m_BackSurface;
}
flNextTestX = pTestEdge->m_pNextActiveEdge->m_flX;
}
else
{
flCurrentX = flNextOccluderX;
pOccluderEdge = pOccluderEdge->m_pNextActiveEdge;
pOccluderSurf = (pOccluderEdge->m_nLeaveSurfID >= 0) ? &m_Surfaces[pOccluderEdge->m_nLeaveSurfID] : &m_BackSurface;
flNextOccluderX = pOccluderEdge->m_flX;
}
}
}
//-----------------------------------------------------------------------------
// Does this edge list occlude another winged edge list?
//-----------------------------------------------------------------------------
bool CWingedEdgeList::IsOccludingEdgeList( CWingedEdgeList &testList )
{
#ifdef DEBUG_OCCLUSION_SYSTEM
testList.CheckConsistency();
CheckConsistency();
#endif
// Did all the edges get culled for some reason? Then it's occluded
if ( testList.EdgeCount() == 0 )
return true;
testList.ResetActiveEdgeList();
ResetActiveEdgeList();
// What we're going to do is look for the first discontinuities we can find
// in both edge lists. Then, at each discontinuity, we must check the
// active edge lists against each other and see if the occluders always
// block the occludees...
float flCurrentY = testList.NextDiscontinuity();
// The edge list for the occluder must completely obscure the occludee...
// If, then, the first occluder edge starts *below* the first occludee edge, it doesn't occlude.
if ( flCurrentY < NextDiscontinuity() )
return false;
// If we start outside the screen bounds, then it's occluded!
if ( flCurrentY >= 1.0f )
return true;
testList.AdvanceActiveEdgeList( flCurrentY );
AdvanceActiveEdgeList( flCurrentY );
while ( true )
{
if ( !IsOccludingActiveEdgeList( testList, flCurrentY ) )
return false;
// If we got outside the screen bounds, then it's occluded!
if ( flCurrentY >= 1.0f )
return true;
float flTestY = testList.NextDiscontinuity();
float flOccluderY = NextDiscontinuity();
flCurrentY = min( flTestY, flOccluderY );
// NOTE: This check here is to help occlusion @ the top of the screen
// We cut the occluders off at y = 1.0 + epsilon, which means there's
// not necessarily a discontinuity at y == 1.0. We need to create a discontinuity
// there so that the occluder edges are still being used.
if ( flCurrentY > 1.0f )
{
flCurrentY = 1.0f;
}
// If the occludee list is empty, then it's occluded!
if ( !testList.AdvanceActiveEdgeList( flCurrentY ) )
return true;
// If the occluder list is empty, then the occludee is not occluded!
if ( !AdvanceActiveEdgeList( flCurrentY ) )
return false;
}
}
//-----------------------------------------------------------------------------
// Queues up stuff to visualize
//-----------------------------------------------------------------------------
void CWingedEdgeList::QueueVisualization( unsigned char *pColor )
{
#ifndef SWDS
if ( !r_visocclusion.GetInt() )
return;
int nFirst = g_EdgeVisualization.AddMultipleToTail( m_WingedEdges.Count() );
for ( int i = m_WingedEdges.Count(); --i >= 0; )
{
WingedEdge_t *pEdge = &m_WingedEdges[i];
EdgeVisualizationInfo_t &info = g_EdgeVisualization[nFirst + i];
info.m_vecPoint[0] = pEdge->m_vecPosition;
info.m_vecPoint[1] = pEdge->m_vecPositionEnd;
*(int*)(info.m_pColor) = *(int*)pColor;
}
#endif
}
//-----------------------------------------------------------------------------
// Renders the winged edge list for debugging
//-----------------------------------------------------------------------------
void CWingedEdgeList::Visualize( unsigned char *pColor )
{
#ifndef SWDS
if ( !r_visocclusion.GetInt() )
return;
CMatRenderContextPtr pRenderContext( materials );
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->PushMatrix();
pRenderContext->LoadIdentity();
pRenderContext->MatrixMode( MATERIAL_VIEW );
pRenderContext->PushMatrix();
pRenderContext->LoadIdentity();
pRenderContext->MatrixMode( MATERIAL_PROJECTION );
pRenderContext->PushMatrix();
pRenderContext->LoadIdentity();
pRenderContext->Bind( g_pMaterialWireframeVertexColorIgnoreZ );
IMesh *pMesh = pRenderContext->GetDynamicMesh( );
CMeshBuilder meshBuilder;
meshBuilder.Begin( pMesh, MATERIAL_LINES, m_WingedEdges.Count() );
int i;
int nCount = m_WingedEdges.Count();
for ( i = nCount; --i >= 0; )
{
WingedEdge_t *pEdge = &m_WingedEdges[i];
meshBuilder.Position3fv( pEdge->m_vecPosition.Base() );
meshBuilder.Color4ubv( pColor );
meshBuilder.AdvanceVertex();
meshBuilder.Position3fv( pEdge->m_vecPositionEnd.Base() );
#ifdef DEBUG_OCCLUSION_SYSTEM
meshBuilder.Color4ub( 0, 0, 255, 255 );
#else
meshBuilder.Color4ubv( pColor );
#endif
meshBuilder.AdvanceVertex();
}
meshBuilder.End();
pMesh->Draw();
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->PopMatrix();
pRenderContext->MatrixMode( MATERIAL_VIEW );
pRenderContext->PopMatrix();
pRenderContext->MatrixMode( MATERIAL_PROJECTION );
pRenderContext->PopMatrix();
#endif
}
//-----------------------------------------------------------------------------
// Edge list that's fast to iterate over, fast to insert into
//-----------------------------------------------------------------------------
class CEdgeList
{
public:
struct Edge_t
{
Vector m_vecPosition; // of the upper point in y, measured in screen space
Vector m_vecPositionEnd; // of the lower point in y, measured in screen space
float m_flDxDy; // Change in x per unit in y.
float m_flOODy;
float m_flX;
int m_nSurfID; // Unique index of the surface this is a part of
// Active edge list
Edge_t *m_pPrevActiveEdge;
Edge_t *m_pNextActiveEdge;
};
public:
CEdgeList();
// Insertion
void AddEdge( Vector **ppEdgeVertices, int nSurfID );
// Surface ID management
int AddSurface( const cplane_t &plane );
void SetSurfaceArea( int nSurfID, float flArea );
// Removal
void RemoveAll();
// Visualization
void QueueVisualization( unsigned char *pColor );
void Visualize( unsigned char *pColor );
// Access
int EdgeCount() const;
int ActualEdgeCount() const;
const Edge_t &EdgeFromSortIndex( int nSortIndex ) const;
Edge_t &EdgeFromSortIndex( int nSortIndex );
// Is the test edge list occluded by this edge list
bool IsOccludingEdgeList( CEdgeList &testList );
// Reduces the active occlusion edge list to the bare minimum set of edges
void ReduceActiveList( CWingedEdgeList &newEdgeList );
// Removal of small occluders
void CullSmallOccluders();
private:
struct Surface_t
{
cplane_t m_Plane; // measured in projection space
float m_flOOz;
Surface_t *m_pPrevSurface;
Surface_t *m_pNextSurface;
int m_nSurfID;
float m_flArea; // Area in screen space
};
struct ReduceInfo_t
{
short m_hEdge;
short m_nWingedEdge;
const Edge_t *m_pEdge;
};
enum
{
MAX_EDGE_CROSSINGS = 64
};
typedef CUtlVector<Edge_t> EdgeList_t;
private:
// Gets an edge
const Edge_t &Edge( int nIndex ) const;
// Active edges...
const Edge_t *FirstActiveEdge( ) const;
Edge_t *FirstActiveEdge( );
const Edge_t *LastActiveEdge( ) const;
Edge_t *LastActiveEdge( );
bool AtListEnd( const Edge_t* pEdge ) const;
bool AtListStart( const Edge_t* pEdge ) const;
void LinkActiveEdgeAfter( Edge_t *pPrevEdge, Edge_t *pInsertEdge );
void UnlinkActiveEdge( Edge_t *pEdge );
// Surface list
Surface_t* TopSurface();
bool AtSurfListEnd( const Surface_t* pSurface ) const;
void CleanupCurrentSurfaceList();
// Active edge list
void ResetActiveEdgeList();
float NextDiscontinuity() const;
// Clears the current scan line
float ClearCurrentSurfaceList();
// Returns the z value of a surface given and x,y coordinate
float ComputeZValue( const Surface_t *pSurface, float x, float y ) const;
// Computes a point at a specified y value along an edge
void ComputePointAlongEdge( const Edge_t *pEdge, int nSurfID, float y, Vector *pPoint ) const;
// Inserts an edge into the active edge list, sorted by X
void InsertActiveEdge( Edge_t *pPrevEdge, Edge_t *pInsertEdge );
// Used to insert an edge into the active edge list
bool IsEdgeXGreater( const Edge_t *pEdge1, const Edge_t *pEdge2 );
// Reduces the active edge list into a subset of ones we truly care about
void ReduceActiveEdgeList( CWingedEdgeList &newEdgeList, float flMinY, float flMaxY );
// Discovers the first edge crossing discontinuity
float LocateEdgeCrossingDiscontinuity( float flNextY, float flPrevY, int &nCount, Edge_t **pInfo );
// Generates a list of surfaces on the current scan line
void UpdateCurrentSurfaceZValues( float x, float y );
// Intoruces a single new edge
void IntroduceSingleActiveEdge( const Edge_t *pEdge, float flCurrY );
// Returns true if pTestSurf is closer (lower z value)
bool IsSurfaceBehind( Surface_t *pTestSurf, Surface_t *pSurf );
// Advances the X values of the active edge list, with no reordering
void AdvanceActiveEdgeList( float flNextY );
void IntroduceNewActiveEdges( float y );
void ReorderActiveEdgeList( int nCount, Edge_t **ppInfo );
// Debugging spew
void SpewActiveEdgeList( float y, bool bHex = false );
// Checks consistency of the edge list...
void CheckConsistency();
class EdgeLess
{
public:
bool Less( const unsigned short& src1, const unsigned short& src2, void *pCtx );
};
static int __cdecl SurfCompare( const void *elem1, const void *elem2 );
private:
// Used to sort surfaces by screen area
static Surface_t *s_pSortSurfaces;
// List of all edges
EdgeList_t m_Edges;
CUtlSortVector<unsigned short, EdgeLess > m_OrigSortIndices;
CUtlVector<unsigned short> m_SortIndices;
Edge_t m_StartTerminal;
Edge_t m_EndTerminal;
// Surfaces
CUtlVector< Surface_t > m_Surfaces;
CUtlVector< int > m_SurfaceSort;
Surface_t m_StartSurfTerminal;
Surface_t m_EndSurfTerminal;
// Active edges
int m_nCurrentEdgeIndex;
float m_flNextDiscontinuity;
// List of edges on the current Y scan-line
Edge_t *m_pCurrentActiveEdge;
// Last X on the current scan line
float m_flLastX;
// Reduce list
ReduceInfo_t *m_pNewReduceInfo;
ReduceInfo_t *m_pPrevReduceInfo;
int m_nNewReduceCount;
int m_nPrevReduceCount;
};
//-----------------------------------------------------------------------------
// Used to sort the edge list
//-----------------------------------------------------------------------------
bool CEdgeList::EdgeLess::Less( const unsigned short& src1, const unsigned short& src2, void *pCtx )
{
EdgeList_t *pEdgeList = (EdgeList_t*)pCtx;
const Edge_t &e1 = pEdgeList->Element(src1);
const Edge_t &e2 = pEdgeList->Element(src2);
if ( e1.m_vecPosition.y < e2.m_vecPosition.y )
return true;
if ( e1.m_vecPosition.y > e2.m_vecPosition.y )
return false;
if ( e1.m_vecPosition.x < e2.m_vecPosition.x )
return true;
if ( e1.m_vecPosition.x > e2.m_vecPosition.x )
return false;
// This makes it so that if two edges start on the same point,
// the leftmost edge is always selected
return ( e1.m_flDxDy <= e2.m_flDxDy );
}
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CEdgeList::CEdgeList() : m_Edges( 0, 32 ), m_OrigSortIndices( 0, 32 )
{
m_OrigSortIndices.SetLessContext( &m_Edges );
m_StartTerminal.m_vecPosition.Init( -FLT_MAX, -FLT_MAX, -FLT_MAX );
m_StartTerminal.m_vecPositionEnd.Init( -FLT_MAX, FLT_MAX, -FLT_MAX );
m_StartTerminal.m_nSurfID = -1;
m_StartTerminal.m_pPrevActiveEdge = NULL;
m_StartTerminal.m_pNextActiveEdge = NULL;
m_StartTerminal.m_flDxDy = 0.0f;
m_StartTerminal.m_flOODy = 0.0f;
m_StartTerminal.m_flX = -FLT_MAX;
m_EndTerminal.m_vecPosition.Init( FLT_MAX, -FLT_MAX, -FLT_MAX );
m_EndTerminal.m_vecPositionEnd.Init( FLT_MAX, FLT_MAX, -FLT_MAX );
m_EndTerminal.m_nSurfID = -1;
m_EndTerminal.m_pPrevActiveEdge = NULL;
m_EndTerminal.m_pNextActiveEdge = NULL;
m_EndTerminal.m_flDxDy = 0.0f;
m_EndTerminal.m_flOODy = 0.0f;
m_EndTerminal.m_flX = FLT_MAX;
m_StartSurfTerminal.m_flOOz = -FLT_MAX;
m_StartSurfTerminal.m_Plane.normal.Init( 0, 0, 1 );
m_StartSurfTerminal.m_Plane.dist = -FLT_MAX;
m_StartSurfTerminal.m_nSurfID = -1;
m_StartSurfTerminal.m_pNextSurface = NULL;
m_StartSurfTerminal.m_pPrevSurface = NULL;
m_EndSurfTerminal.m_flOOz = FLT_MAX;
m_EndSurfTerminal.m_Plane.normal.Init( 0, 0, 1 );
m_EndSurfTerminal.m_Plane.dist = FLT_MAX;
m_EndSurfTerminal.m_nSurfID = -1;
m_EndSurfTerminal.m_pNextSurface = NULL;
m_EndSurfTerminal.m_pPrevSurface = NULL;
}
//-----------------------------------------------------------------------------
// iteration
//-----------------------------------------------------------------------------
inline int CEdgeList::EdgeCount() const
{
return m_Edges.Count();
}
inline int CEdgeList::ActualEdgeCount() const
{
return m_SortIndices.Count();
}
inline const CEdgeList::Edge_t &CEdgeList::EdgeFromSortIndex( int nSortIndex ) const
{
return m_Edges[ m_SortIndices[nSortIndex] ];
}
inline CEdgeList::Edge_t &CEdgeList::EdgeFromSortIndex( int nSortIndex )
{
return m_Edges[ m_SortIndices[nSortIndex] ];
}
inline const CEdgeList::Edge_t &CEdgeList::Edge( int nIndex ) const
{
return m_Edges[ nIndex ];
}
//-----------------------------------------------------------------------------
// Active edges...
//-----------------------------------------------------------------------------
inline const CEdgeList::Edge_t *CEdgeList::FirstActiveEdge( ) const
{
return m_StartTerminal.m_pNextActiveEdge;
}
inline CEdgeList::Edge_t *CEdgeList::FirstActiveEdge( )
{
return m_StartTerminal.m_pNextActiveEdge;
}
inline const CEdgeList::Edge_t *CEdgeList::LastActiveEdge( ) const
{
return m_EndTerminal.m_pPrevActiveEdge;
}
inline CEdgeList::Edge_t *CEdgeList::LastActiveEdge( )
{
return m_EndTerminal.m_pPrevActiveEdge;
}
inline bool CEdgeList::AtListEnd( const Edge_t* pEdge ) const
{
return pEdge == &m_EndTerminal;
}
inline bool CEdgeList::AtListStart( const Edge_t* pEdge ) const
{
return pEdge == &m_StartTerminal;
}
inline void CEdgeList::LinkActiveEdgeAfter( Edge_t *pPrevEdge, Edge_t *pInsertEdge )
{
pInsertEdge->m_pNextActiveEdge = pPrevEdge->m_pNextActiveEdge;
pInsertEdge->m_pPrevActiveEdge = pPrevEdge;
pInsertEdge->m_pNextActiveEdge->m_pPrevActiveEdge = pInsertEdge;
pPrevEdge->m_pNextActiveEdge = pInsertEdge;
}
inline void CEdgeList::UnlinkActiveEdge( Edge_t *pEdge )
{
pEdge->m_pPrevActiveEdge->m_pNextActiveEdge = pEdge->m_pNextActiveEdge;
pEdge->m_pNextActiveEdge->m_pPrevActiveEdge = pEdge->m_pPrevActiveEdge;
#ifdef _DEBUG
pEdge->m_pPrevActiveEdge = pEdge->m_pNextActiveEdge = NULL;
#endif
}
//-----------------------------------------------------------------------------
// Surface list
//-----------------------------------------------------------------------------
inline CEdgeList::Surface_t* CEdgeList::TopSurface()
{
return m_StartSurfTerminal.m_pNextSurface;
}
inline bool CEdgeList::AtSurfListEnd( const Surface_t* pSurface ) const
{
return pSurface == &m_EndSurfTerminal;
}
void CEdgeList::CleanupCurrentSurfaceList()
{
Surface_t *pSurf = TopSurface();
while ( !AtSurfListEnd(pSurf) )
{
Surface_t *pNext = pSurf->m_pNextSurface;
pSurf->m_pPrevSurface = pSurf->m_pNextSurface = NULL;
pSurf = pNext;
}
}
inline void CEdgeList::SetSurfaceArea( int nSurfID, float flArea )
{
m_Surfaces[nSurfID].m_flArea = flArea;
}
//-----------------------------------------------------------------------------
// Returns the z value of a surface given and x,y coordinate
//-----------------------------------------------------------------------------
inline float CEdgeList::ComputeZValue( const Surface_t *pSurface, float x, float y ) const
{
const cplane_t &plane = pSurface->m_Plane;
Assert( plane.normal.z == 1.0f );
return plane.dist - plane.normal.x * x - plane.normal.y * y;
}
//-----------------------------------------------------------------------------
// Computes a point at a specified y value along an edge
//-----------------------------------------------------------------------------
inline void CEdgeList::ComputePointAlongEdge( const Edge_t *pEdge, int nSurfID, float y, Vector *pPoint ) const
{
Assert( (y >= pEdge->m_vecPosition.y) && (y <= pEdge->m_vecPositionEnd.y) );
float t;
t = (y - pEdge->m_vecPosition.y) * pEdge->m_flOODy;
pPoint->x = pEdge->m_vecPosition.x + ( pEdge->m_vecPositionEnd.x - pEdge->m_vecPosition.x ) * t;
pPoint->y = y;
pPoint->z = ComputeZValue( &m_Surfaces[nSurfID], pPoint->x, y );
}
//-----------------------------------------------------------------------------
// Surface ID management
//-----------------------------------------------------------------------------
int CEdgeList::AddSurface( const cplane_t &plane )
{
int nIndex = m_Surfaces.AddToTail();
Surface_t &surf = m_Surfaces[nIndex];
surf.m_flOOz = 0.0f;
surf.m_Plane = plane;
surf.m_pNextSurface = NULL;
surf.m_pPrevSurface = NULL;
surf.m_nSurfID = nIndex;
m_SurfaceSort.AddToTail(nIndex);
return nIndex;
}
//-----------------------------------------------------------------------------
// Insertion
//-----------------------------------------------------------------------------
void CEdgeList::AddEdge( Vector **ppEdgeVertices, int nSurfID )
{
int nMinIndex = ( ppEdgeVertices[0]->y >= ppEdgeVertices[1]->y );
const Vector &vecStartVert = *(ppEdgeVertices[ nMinIndex ]);
const Vector &vecEndVert = *(ppEdgeVertices[ 1 - nMinIndex ]);
// This is true if we've clipped to the near clip plane
Assert( (vecStartVert.z >= 0.0f) && (vecEndVert.z >= 0.0f) );
// Don't bother adding edges with dy == 0
float dy = vecEndVert.y - vecStartVert.y;
if (dy == 0.0f)
return;
int i = m_Edges.AddToTail();
Edge_t &newEdge = m_Edges[i];
newEdge.m_flOODy = 1.0f / dy;
newEdge.m_vecPosition = vecStartVert;
newEdge.m_vecPositionEnd = vecEndVert;
newEdge.m_nSurfID = nSurfID;
newEdge.m_flDxDy = (vecEndVert.x - vecStartVert.x) * newEdge.m_flOODy;
newEdge.m_pPrevActiveEdge = NULL;
newEdge.m_pNextActiveEdge = NULL;
// Insert it into the sorted list
m_OrigSortIndices.Insert( i );
}
//-----------------------------------------------------------------------------
// Used to sort the surfaces
//-----------------------------------------------------------------------------
CEdgeList::Surface_t *CEdgeList::s_pSortSurfaces = NULL;
int __cdecl CEdgeList::SurfCompare( const void *elem1, const void *elem2 )
{
int nSurfID1 = *(int*)elem1;
float flArea1 = s_pSortSurfaces[nSurfID1].m_flArea;
int nSurfID2 = *(int*)elem2;
float flArea2 = s_pSortSurfaces[nSurfID2].m_flArea;
if (flArea1 > flArea2)
return -1;
if (flArea1 < flArea2)
return 1;
return 0;
}
//-----------------------------------------------------------------------------
// Removal of small occluders
//-----------------------------------------------------------------------------
void CEdgeList::CullSmallOccluders()
{
// Cull out all surfaces with too small of a screen area...
// Sort the surfaces by screen area, in descending order
int nSurfCount = m_Surfaces.Count();
s_pSortSurfaces = m_Surfaces.Base();
qsort( m_SurfaceSort.Base(), nSurfCount, sizeof(int), SurfCompare );
// We're going to keep the greater of r_occludermin + All surfaces with a screen area >= r_occluderarea
int nMinSurfaces = r_occludermincount.GetInt();
// The *2 here is because surf areas are 2x bigger than actual
float flMinScreenArea = r_occluderminarea.GetFloat() * 0.02f;
if ( flMinScreenArea == 0.0f )
{
flMinScreenArea = OcclusionSystem()->MinOccluderArea() * 0.02f;
}
bool *bUseSurface = (bool*)stackalloc( nSurfCount * sizeof(bool) );
memset( bUseSurface, 0, nSurfCount * sizeof(bool) );
int i;
for ( i = 0; i < nSurfCount; ++i )
{
int nSurfID = m_SurfaceSort[i];
if (( m_Surfaces[ nSurfID ].m_flArea < flMinScreenArea ) && (i >= nMinSurfaces ))
break;
bUseSurface[nSurfID] = true;
}
MEM_ALLOC_CREDIT();
int nEdgeCount = m_OrigSortIndices.Count();
m_SortIndices.RemoveAll();
m_SortIndices.EnsureCapacity( nEdgeCount );
for( i = 0; i < nEdgeCount; ++i )
{
int nEdgeIndex = m_OrigSortIndices[i];
if ( bUseSurface[ m_Edges[ nEdgeIndex ].m_nSurfID ] )
{
m_SortIndices.AddToTail( nEdgeIndex );
}
}
}
//-----------------------------------------------------------------------------
// Removal
//-----------------------------------------------------------------------------
void CEdgeList::RemoveAll()
{
m_Edges.RemoveAll();
m_SortIndices.RemoveAll();
m_OrigSortIndices.RemoveAll();
m_Surfaces.RemoveAll();
m_SurfaceSort.RemoveAll();
}
//-----------------------------------------------------------------------------
// Active edge list
//-----------------------------------------------------------------------------
void CEdgeList::ResetActiveEdgeList()
{
// This shouldn't be called unless we're about to do active edge checking
Assert( ActualEdgeCount() );
m_nCurrentEdgeIndex = 0;
m_flNextDiscontinuity = EdgeFromSortIndex( 0 ).m_vecPosition.y;
m_StartTerminal.m_pNextActiveEdge = &m_EndTerminal;
m_EndTerminal.m_pPrevActiveEdge = &m_StartTerminal;
Assert( m_StartTerminal.m_pPrevActiveEdge == NULL );
Assert( m_EndTerminal.m_pNextActiveEdge == NULL );
}
//-----------------------------------------------------------------------------
// Returns the next time in Y the edge list will undergo a change
//-----------------------------------------------------------------------------
inline float CEdgeList::NextDiscontinuity() const
{
return m_flNextDiscontinuity;
}
//-----------------------------------------------------------------------------
// Used to insert an edge into the active edge list, sorted by X
// If Xs match, sort by Dx/Dy
//-----------------------------------------------------------------------------
inline bool CEdgeList::IsEdgeXGreater( const Edge_t *pEdge1, const Edge_t *pEdge2 )
{
float flDelta = pEdge1->m_flX - pEdge2->m_flX;
if ( flDelta > 0 )
return true;
if ( flDelta < 0 )
return false;
// NOTE: Using > instead of >= means coincident edges won't continually swap places
return pEdge1->m_flDxDy > pEdge2->m_flDxDy;
}
//-----------------------------------------------------------------------------
// Inserts an edge into the active edge list, sorted by X
//-----------------------------------------------------------------------------
inline void CEdgeList::InsertActiveEdge( Edge_t *pPrevEdge, Edge_t *pInsertEdge )
{
while( !AtListStart(pPrevEdge) && IsEdgeXGreater( pPrevEdge, pInsertEdge ) )
{
pPrevEdge = pPrevEdge->m_pPrevActiveEdge;
}
LinkActiveEdgeAfter( pPrevEdge, pInsertEdge );
}
//-----------------------------------------------------------------------------
// Clears the current scan line
//-----------------------------------------------------------------------------
float CEdgeList::ClearCurrentSurfaceList()
{
m_pCurrentActiveEdge = FirstActiveEdge();
m_flLastX = m_pCurrentActiveEdge->m_flX;
m_StartSurfTerminal.m_pNextSurface = &m_EndSurfTerminal;
m_EndSurfTerminal.m_pPrevSurface = &m_StartSurfTerminal;
return m_flLastX;
}
//-----------------------------------------------------------------------------
// Generates a list of surfaces on the current scan line
//-----------------------------------------------------------------------------
inline void CEdgeList::UpdateCurrentSurfaceZValues( float x, float y )
{
// Update the z values of all active surfaces
for ( Surface_t *pSurf = TopSurface(); !AtSurfListEnd( pSurf ); pSurf = pSurf->m_pNextSurface )
{
// NOTE: As long as we assume no interpenetrating surfaces,
// we don't need to re-sort by ooz here.
pSurf->m_flOOz = ComputeZValue( pSurf, x, y );
}
}
//-----------------------------------------------------------------------------
// Returns true if pTestSurf is closer (lower z value)
//-----------------------------------------------------------------------------
inline bool CEdgeList::IsSurfaceBehind( Surface_t *pTestSurf, Surface_t *pSurf )
{
if ( pTestSurf->m_flOOz - pSurf->m_flOOz <= -1e-6 )
return true;
if ( pTestSurf->m_flOOz - pSurf->m_flOOz >= 1e-6 )
return false;
// If they're nearly equal, then the thing that's approaching the screen
// more quickly as we ascend in y is closer
return ( pTestSurf->m_Plane.normal.y >= pSurf->m_Plane.normal.y );
}
//-----------------------------------------------------------------------------
// Introduces a single new edge
//-----------------------------------------------------------------------------
void CEdgeList::IntroduceSingleActiveEdge( const Edge_t *pEdge, float flCurrY )
{
Surface_t *pCurrentSurf = &m_Surfaces[ pEdge->m_nSurfID ];
if ( !pCurrentSurf->m_pNextSurface )
{
pCurrentSurf->m_flOOz = ComputeZValue( pCurrentSurf, pEdge->m_flX, flCurrY );
// Determine where to insert the surface into the surface list...
// Insert it so that the surface list is sorted by OOz
Surface_t *pNextSurface = TopSurface();
while( IsSurfaceBehind( pNextSurface, pCurrentSurf ) )
{
pNextSurface = pNextSurface->m_pNextSurface;
}
pCurrentSurf->m_pNextSurface = pNextSurface;
pCurrentSurf->m_pPrevSurface = pNextSurface->m_pPrevSurface;
pNextSurface->m_pPrevSurface = pCurrentSurf;
pCurrentSurf->m_pPrevSurface->m_pNextSurface = pCurrentSurf;
}
else
{
// This means this edge is associated with a surface
// already in the current surface list
// In this case, simply remove the surface from the surface list
pCurrentSurf->m_pNextSurface->m_pPrevSurface = pCurrentSurf->m_pPrevSurface;
pCurrentSurf->m_pPrevSurface->m_pNextSurface = pCurrentSurf->m_pNextSurface;
pCurrentSurf->m_pPrevSurface = pCurrentSurf->m_pNextSurface = NULL;
}
}
//-----------------------------------------------------------------------------
// Reduces the active occlusion edge list to the bare minimum set of edges
//-----------------------------------------------------------------------------
void CEdgeList::IntroduceNewActiveEdges( float y )
{
int nEdgeCount = ActualEdgeCount();
if ( m_nCurrentEdgeIndex == nEdgeCount )
return;
Edge_t *pCurEdge = &EdgeFromSortIndex( m_nCurrentEdgeIndex );
// Add new edges, computing the x + z coordinates at the requested y value
while ( pCurEdge->m_vecPosition.y <= y )
{
// This is necessary because of our initial skip up to y == -1.0f
if (pCurEdge->m_vecPositionEnd.y > y)
{
float flDy = y - pCurEdge->m_vecPosition.y;
pCurEdge->m_flX = pCurEdge->m_vecPosition.x + flDy * pCurEdge->m_flDxDy;
if ( pCurEdge->m_vecPositionEnd.y < m_flNextDiscontinuity )
{
m_flNextDiscontinuity = pCurEdge->m_vecPositionEnd.y;
}
// Now re-insert in the list, sorted by X
InsertActiveEdge( LastActiveEdge(), pCurEdge );
}
if ( ++m_nCurrentEdgeIndex == nEdgeCount )
return;
pCurEdge = &EdgeFromSortIndex( m_nCurrentEdgeIndex );
}
// The next edge in y will also present a discontinuity
if ( pCurEdge->m_vecPosition.y < m_flNextDiscontinuity )
{
m_flNextDiscontinuity = pCurEdge->m_vecPosition.y;
}
}
//-----------------------------------------------------------------------------
// Reduces the active edge list into a subset of ones we truly care about
//-----------------------------------------------------------------------------
void CEdgeList::ReduceActiveEdgeList( CWingedEdgeList &wingedEdgeList, float flMinY, float flMaxY )
{
// Surface lists should be empty
int i;
#ifdef DEBUG_OCCLUSION_SYSTEM
for ( i = m_Surfaces.Count(); --i >= 0; )
{
Assert( m_Surfaces[i].m_pNextSurface == NULL );
}
#endif
int nLeaveSurfID = -1;
const Edge_t *pCurEdge = FirstActiveEdge();
const Edge_t *pNextEdge;
// NOTE: This algorithm depends on the fact that the active edge
// list is not only sorted by ascending X, but also because edges
// that land on the same X value are sorted by ascending dy/dx
float flPrevX = pCurEdge->m_flX;
for ( ; !AtListEnd( pCurEdge ); pCurEdge = pNextEdge )
{
if ( pCurEdge->m_flX != flPrevX )
{
UpdateCurrentSurfaceZValues( pCurEdge->m_flX, flMinY );
}
IntroduceSingleActiveEdge( pCurEdge, flMinY );
flPrevX = pCurEdge->m_flX;
// If we have coincident edges, we have to introduce them at the same time...
pNextEdge = pCurEdge->m_pNextActiveEdge;
if ( (flPrevX == pNextEdge->m_flX) && (pCurEdge->m_flDxDy == pNextEdge->m_flDxDy) )
continue;
// If there's more than one overlapping surface at this point,
// we can eliminate some edges.
int nEnterSurfID = TopSurface()->m_nSurfID;
// No change in the top surface? No edges needed...
if ( nLeaveSurfID == nEnterSurfID )
continue;
Assert( ( nLeaveSurfID != -1 ) || ( nEnterSurfID != -1 ) );
int nEdgeSurfID = ( nEnterSurfID != -1 ) ? nEnterSurfID : nLeaveSurfID;
// Seam up edges...
for ( i = m_nPrevReduceCount; --i >= 0; )
{
CWingedEdgeList::WingedEdge_t &testEdge = wingedEdgeList.WingedEdge( m_pPrevReduceInfo[i].m_nWingedEdge );
if (( testEdge.m_nLeaveSurfID != nLeaveSurfID ) || ( testEdge.m_nEnterSurfID != nEnterSurfID ))
continue;
if ( ( testEdge.m_flDxDy != pCurEdge->m_flDxDy) || ( fabs( testEdge.m_vecPositionEnd.x - pCurEdge->m_flX ) >= 1e-3 ) )
continue;
ComputePointAlongEdge( m_pPrevReduceInfo[i].m_pEdge, nEdgeSurfID, flMaxY, &testEdge.m_vecPositionEnd );
// Don't try to seam up edges that end on this line...
if ( pCurEdge->m_vecPositionEnd.y > flMaxY )
{
ReduceInfo_t *pNewEdge = &m_pNewReduceInfo[ m_nNewReduceCount ];
++m_nNewReduceCount;
pNewEdge->m_pEdge = m_pPrevReduceInfo[i].m_pEdge;
pNewEdge->m_nWingedEdge = m_pPrevReduceInfo[i].m_nWingedEdge;
}
break;
}
// This edge didn't exist on the previous y discontinuity line
// We'll need to make a new one
if ( i < 0 )
{
i = wingedEdgeList.AddEdge();
CWingedEdgeList::WingedEdge_t &newWingedEdge = wingedEdgeList.WingedEdge(i);
newWingedEdge.m_nLeaveSurfID = nLeaveSurfID;
newWingedEdge.m_nEnterSurfID = nEnterSurfID;
newWingedEdge.m_flDxDy = pCurEdge->m_flDxDy;
ComputePointAlongEdge( pCurEdge, nEdgeSurfID, flMinY, &newWingedEdge.m_vecPosition );
ComputePointAlongEdge( pCurEdge, nEdgeSurfID, flMaxY, &newWingedEdge.m_vecPositionEnd );
// Enforce sort order...
// Required because we're computing the x position here, which can introduce error.
if ( i != 0 )
{
CWingedEdgeList::WingedEdge_t &prevWingedEdge = wingedEdgeList.WingedEdge(i - 1);
if ( newWingedEdge.m_vecPosition.y == prevWingedEdge.m_vecPosition.y )
{
if ( newWingedEdge.m_vecPosition.x < prevWingedEdge.m_vecPosition.x )
{
newWingedEdge.m_vecPosition.x = prevWingedEdge.m_vecPosition.x;
}
}
}
// Don't try to seam up edges that end on this line...
if ( pCurEdge->m_vecPositionEnd.y > flMaxY )
{
ReduceInfo_t *pNewEdge = &m_pNewReduceInfo[ m_nNewReduceCount ];
++m_nNewReduceCount;
pNewEdge->m_pEdge = pCurEdge;
pNewEdge->m_nWingedEdge = i;
}
#ifdef DEBUG_OCCLUSION_SYSTEM
wingedEdgeList.CheckConsistency();
#endif
}
nLeaveSurfID = nEnterSurfID;
}
Assert( nLeaveSurfID == -1 );
// Msg("\n");
}
//-----------------------------------------------------------------------------
// Discovers the first edge crossing discontinuity
//-----------------------------------------------------------------------------
float CEdgeList::LocateEdgeCrossingDiscontinuity( float flNextY, float flPrevY, int &nCount, Edge_t **ppInfo )
{
nCount = 0;
float flCurrX = -FLT_MAX;
float flNextX = -FLT_MAX;
float flCurrY = flNextY;
Vector2D vecDelta, vecIntersection;
Edge_t *pCurEdge;
for ( pCurEdge = FirstActiveEdge(); !AtListEnd(pCurEdge); flCurrX = flNextX, pCurEdge = pCurEdge->m_pNextActiveEdge )
{
// Don't take into account edges that end on the current line
Assert( pCurEdge->m_vecPositionEnd.y >= flCurrY );
flNextX = pCurEdge->m_vecPosition.x + (flCurrY - pCurEdge->m_vecPosition.y) * pCurEdge->m_flDxDy;
// Look for an X-crossing... This check helps for nearly co-linear lines
// NOTE: You might think this would crash since it could dereference a NULL
// pointer the first time through the loop, but it never hits that check since the
// first X test is guaranteed to pass
Edge_t *pPrevEdge = pCurEdge->m_pPrevActiveEdge;
if ( ( flNextX > flCurrX ) || ( pPrevEdge->m_flDxDy <= pCurEdge->m_flDxDy ) )
continue;
// This test is necessary to not capture edges that meet at a point...
if ( pPrevEdge->m_vecPositionEnd == pCurEdge->m_vecPositionEnd )
continue;
Assert( pPrevEdge->m_flDxDy != pCurEdge->m_flDxDy );
// Found one! Let's find the intersection of these two
// edges and up the Y discontinuity to that point.
// We'll solve this by doing an intersection of point + plane in 2D...
// For the line, we'll use the previous line where
// P = Pop + D * t, Pop = prevEdge.m_vecPosition, D = [dx dy] = [(dx/dy) 1]
// For the plane, we'll use the current line where
// N * P = d
// Normal is perpendicular to the line, therefore N = [-dy dx] = [-1 (dx/dy)]
// d = DotProduct( N, edge.m_vecPosition ) = N dot Pon
// So, the t that solve the equation is given by t = (d - N dot Pop) / (N dot D)
// Or, t = (N dot Pon - N dot Pop) / (N dot D)
// t = (N dot (Pon - Pop)) / (N dot D)
float flDenominator = 1.0f / (-pPrevEdge->m_flDxDy + pCurEdge->m_flDxDy);
Vector2DSubtract( pCurEdge->m_vecPosition.AsVector2D(), pPrevEdge->m_vecPosition.AsVector2D(), vecDelta );
float flNumerator = - vecDelta.x + pCurEdge->m_flDxDy * vecDelta.y;
float t = flNumerator * flDenominator;
float flYCrossing = pPrevEdge->m_vecPosition.y + t;
// Precision errors...
// NOTE: The optimizer unfortunately causes this test to not return ==
// if the bitpattern of flYCrossing and flNextY are the exact same, because it's
// doing the test with the 80bit fp registers. flYCrossing is still sitting in the register
// from the computation on the line above, but flNextY isn't. Therefore it returns not equal.
// That's why I have to do the explicit bitpattern check.
if ( ( flYCrossing >= flNextY ) || ( *(int*)&flYCrossing == *(int*)&flNextY ) )
continue;
if ( flYCrossing < flPrevY )
{
flYCrossing = flPrevY;
}
// If we advanced in Y, then reset the edge crossings
if ( flCurrY != flYCrossing )
{
flCurrY = flYCrossing;
nCount = 0;
}
Assert( nCount < MAX_EDGE_CROSSINGS );
flNextX = pPrevEdge->m_vecPosition.x + t * pPrevEdge->m_flDxDy;
ppInfo[nCount++] = pCurEdge;
}
return flCurrY;
}
//-----------------------------------------------------------------------------
// Advances the X values of the active edge list, with no reordering
//-----------------------------------------------------------------------------
void CEdgeList::AdvanceActiveEdgeList( float flCurrY )
{
m_flNextDiscontinuity = FLT_MAX;
// Advance all edges until the current Y; we don't need to re-order *any* edges.
Edge_t *pCurEdge;
Edge_t *pNextEdge;
float flPrevX = -FLT_MAX;
for ( pCurEdge = FirstActiveEdge(); !AtListEnd( pCurEdge ); pCurEdge = pNextEdge )
{
pNextEdge = pCurEdge->m_pNextActiveEdge;
if ( pCurEdge->m_vecPositionEnd.y <= flCurrY )
{
UnlinkActiveEdge( pCurEdge );
continue;
}
pCurEdge->m_flX = pCurEdge->m_vecPosition.x + (flCurrY - pCurEdge->m_vecPosition.y) * pCurEdge->m_flDxDy;
// Eliminate precision errors by guaranteeing sort ordering...
if ( pCurEdge->m_flX < flPrevX )
{
pCurEdge->m_flX = flPrevX;
}
else
{
flPrevX = pCurEdge->m_flX;
}
if ( pCurEdge->m_vecPositionEnd.y < m_flNextDiscontinuity )
{
m_flNextDiscontinuity = pCurEdge->m_vecPositionEnd.y;
}
}
}
//-----------------------------------------------------------------------------
// Reorders the active edge list based on where edge crossings occur
//-----------------------------------------------------------------------------
void CEdgeList::ReorderActiveEdgeList( int nCount, Edge_t **ppCrossings )
{
int nCurCrossing = 0;
while ( nCurCrossing < nCount )
{
// Re-order the list where the edge crossing occurred.
// For all edges that passed through the exact same point, we need only
// reverse the order of those edges. At the same time, slam the X value of each
// crossing edge to reduce precision errors
Edge_t *pCurCrossing = ppCrossings[nCurCrossing++];
Edge_t *pFirstCrossing = pCurCrossing->m_pPrevActiveEdge;
// First, bring shared (or nearly shared) edges into the crossing list...
while ( pFirstCrossing->m_pPrevActiveEdge->m_flX == pFirstCrossing->m_flX )
{
pFirstCrossing = pFirstCrossing->m_pPrevActiveEdge;
}
// Find the last crossing...
Edge_t *pLastCrossing = pCurCrossing->m_pNextActiveEdge;
Edge_t *pPrevCrossing = pCurCrossing;
while ( true )
{
if ( (nCurCrossing < nCount) && (pLastCrossing == ppCrossings[nCurCrossing]) )
{
pPrevCrossing = pLastCrossing;
pLastCrossing = pLastCrossing->m_pNextActiveEdge;
++nCurCrossing;
continue;
}
if ( pPrevCrossing->m_flX != pLastCrossing->m_flX )
break;
pLastCrossing = pLastCrossing->m_pNextActiveEdge;
}
// This should always be true, since there's always an edge at FLT_MAX.
Assert( pLastCrossing );
// Slam all x values to be the same to avoid precision errors...
// This guarantees that this crossing at least will occur
float flXCrossing = pFirstCrossing->m_flX;
for ( Edge_t *pCrossing = pFirstCrossing->m_pNextActiveEdge; pCrossing != pLastCrossing; pCrossing = pCrossing->m_pNextActiveEdge )
{
pCrossing->m_flX = flXCrossing;
}
}
// Now re-insert everything to take into account other edges which may well have
// crossed on this line
Edge_t *pEdge;
Edge_t *pNextEdge;
for( pEdge = FirstActiveEdge(); !AtListEnd(pEdge); pEdge = pNextEdge )
{
pNextEdge = pEdge->m_pNextActiveEdge;
Edge_t *pPrevEdge = pEdge->m_pPrevActiveEdge;
if ( pPrevEdge->m_flX == pEdge->m_flX )
{
UnlinkActiveEdge( pEdge );
InsertActiveEdge( pPrevEdge, pEdge );
}
}
}
//-----------------------------------------------------------------------------
// Reduces the active occlusion edge list to the bare minimum set of edges
//-----------------------------------------------------------------------------
void CEdgeList::SpewActiveEdgeList( float y, bool bHex)
{
Edge_t *pEdge = FirstActiveEdge();
Msg( "%.3f : ", y );
while ( !AtListEnd( pEdge ) )
{
if (!bHex)
{
Msg( "(%d %.3f [%d]) ", (int)(pEdge - m_Edges.Base()), pEdge->m_flX, pEdge->m_nSurfID );
}
else
{
Msg( "(%d %X [%d]) ", (int)(pEdge - m_Edges.Base()), *(int*)&pEdge->m_flX, pEdge->m_nSurfID );
}
pEdge = pEdge->m_pNextActiveEdge;
}
Msg( "\n" );
}
//-----------------------------------------------------------------------------
// Checks consistency of the edge list...
//-----------------------------------------------------------------------------
void CEdgeList::CheckConsistency()
{
Edge_t *pEdge = FirstActiveEdge();
while( !AtListEnd( pEdge ) )
{
Edge_t *pPrevEdge = pEdge->m_pPrevActiveEdge;
Assert( pEdge->m_flX >= pPrevEdge->m_flX );
if ( pEdge->m_flX == pPrevEdge->m_flX )
{
// End point check necessary because of precision errors
Assert( (pEdge->m_flDxDy >= pPrevEdge->m_flDxDy) || (pEdge->m_vecPositionEnd == pPrevEdge->m_vecPositionEnd) );
}
pEdge = pEdge->m_pNextActiveEdge;
}
}
//-----------------------------------------------------------------------------
// Reduces the active occlusion edge list to the bare minimum set of edges
//-----------------------------------------------------------------------------
void CEdgeList::ReduceActiveList( CWingedEdgeList &newEdgeList )
{
int nEdgeCount = ActualEdgeCount();
if ( nEdgeCount == 0 )
return;
// Copy the surfaces over
int nCount = m_Surfaces.Count();
// newEdgeList.m_Surfaces.EnsureCapacity( nCount );
for ( int i = 0; i < nCount; ++i )
{
newEdgeList.AddSurface( m_Surfaces[i].m_Plane );
}
Edge_t *pEdgeCrossings[MAX_EDGE_CROSSINGS];
ReduceInfo_t *pBuf[2];
pBuf[0] = (ReduceInfo_t*)stackalloc( nEdgeCount * sizeof(ReduceInfo_t) );
pBuf[1] = (ReduceInfo_t*)stackalloc( nEdgeCount * sizeof(ReduceInfo_t) );
m_nPrevReduceCount = m_nNewReduceCount = 0;
int nIndex = 0;
ResetActiveEdgeList();
ClearCurrentSurfaceList();
// We can skip everything up to y = -1.0f; since that's offscreen
float flPrevY = NextDiscontinuity();
flPrevY = fpmax( -1.0f, flPrevY );
m_flNextDiscontinuity = FLT_MAX;
IntroduceNewActiveEdges( flPrevY );
int nEdgeCrossingCount = 0;
bool bDone = false;
while( !bDone )
{
// Don't immediately progress to the next discontinuity if there are edge crossings.
float flNextY = LocateEdgeCrossingDiscontinuity( NextDiscontinuity(), flPrevY, nEdgeCrossingCount, pEdgeCrossings );
#ifdef DEBUG_OCCLUSION_SYSTEM
if ( s_bSpew )
{
// Debugging spew
SpewActiveEdgeList( flPrevY );
}
#endif
// Reduce the active edge list
m_pNewReduceInfo = pBuf[1 - nIndex];
m_pPrevReduceInfo = pBuf[nIndex];
m_nPrevReduceCount = m_nNewReduceCount;
m_nNewReduceCount = 0;
// Add a small epsilon so we occlude things on the top edge at y = 1.0
if (flNextY >= 1.001f)
{
flNextY = 1.001f;
bDone = true;
}
ReduceActiveEdgeList( newEdgeList, flPrevY, flNextY );
flPrevY = flNextY;
// Advance the active edge list, with no resorting necessary!!
AdvanceActiveEdgeList( flNextY );
// If we had an edge crossing, re-order the edges. Otherwise introduce new active edges
if ( !nEdgeCrossingCount )
{
IntroduceNewActiveEdges( flNextY );
// Keep advancing the active edge list until it's got no more discontinuities
if ( NextDiscontinuity() == FLT_MAX )
return;
}
else
{
ReorderActiveEdgeList( nEdgeCrossingCount, pEdgeCrossings );
// The next edge in y will also present a discontinuity
if ( m_nCurrentEdgeIndex < nEdgeCount )
{
float flNextEdgeY = EdgeFromSortIndex( m_nCurrentEdgeIndex ).m_vecPosition.y;
if ( flNextEdgeY < m_flNextDiscontinuity )
{
m_flNextDiscontinuity = flNextEdgeY;
}
}
}
#ifdef DEBUG_OCCLUSION_SYSTEM
CheckConsistency();
#endif
nIndex = 1 - nIndex;
}
}
//-----------------------------------------------------------------------------
// Used to debug the occlusion system
//-----------------------------------------------------------------------------
void CEdgeList::QueueVisualization( unsigned char *pColor )
{
#ifndef SWDS
if ( !r_visocclusion.GetInt() )
return;
int nFirst = g_EdgeVisualization.AddMultipleToTail( m_Edges.Count() );
for ( int i = m_Edges.Count(); --i >= 0; )
{
EdgeVisualizationInfo_t &info = g_EdgeVisualization[nFirst + i];
info.m_vecPoint[0] = m_Edges[i].m_vecPosition;
info.m_vecPoint[1] = m_Edges[i].m_vecPositionEnd;
*(int*)(info.m_pColor) = *(int*)pColor;
}
#endif
}
void CEdgeList::Visualize( unsigned char *pColor )
{
#ifndef SWDS
if ( !r_visocclusion.GetInt() )
return;
CMatRenderContextPtr pRenderContext( materials );
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->PushMatrix();
pRenderContext->LoadIdentity();
pRenderContext->MatrixMode( MATERIAL_VIEW );
pRenderContext->PushMatrix();
pRenderContext->LoadIdentity();
pRenderContext->MatrixMode( MATERIAL_PROJECTION );
pRenderContext->PushMatrix();
pRenderContext->LoadIdentity();
pRenderContext->Bind( g_pMaterialWireframeVertexColorIgnoreZ );
IMesh *pMesh = pRenderContext->GetDynamicMesh( );
CMeshBuilder meshBuilder;
meshBuilder.Begin( pMesh, MATERIAL_LINES, m_Edges.Count() );
int i;
for ( i = m_Edges.Count(); --i >= 0; )
{
meshBuilder.Position3fv( m_Edges[i].m_vecPosition.Base() );
meshBuilder.Color4ubv( pColor );
meshBuilder.AdvanceVertex();
meshBuilder.Position3fv( m_Edges[i].m_vecPositionEnd.Base() );
#ifdef DEBUG_OCCLUSION_SYSTEM
meshBuilder.Color4ub( 0, 0, 255, 255 );
#else
meshBuilder.Color4ubv( pColor );
#endif
meshBuilder.AdvanceVertex();
}
meshBuilder.End();
pMesh->Draw();
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->PopMatrix();
pRenderContext->MatrixMode( MATERIAL_VIEW );
pRenderContext->PopMatrix();
pRenderContext->MatrixMode( MATERIAL_PROJECTION );
pRenderContext->PopMatrix();
#endif
}
//-----------------------------------------------------------------------------
// Implementation of IOcclusionSystem
//-----------------------------------------------------------------------------
class COcclusionSystem : public IOcclusionSystem
{
public:
COcclusionSystem();
~COcclusionSystem();
// Inherited from IOcclusionSystem
virtual void ActivateOccluder( int nOccluderIndex, bool bActive );
virtual void SetView( const Vector &vecCameraPos, float flFOV, const VMatrix &worldToCamera, const VMatrix &cameraToProjection, const VPlane &nearClipPlane );
virtual bool IsOccluded( const Vector &vecAbsMins, const Vector &vecAbsMaxs );
virtual void SetOcclusionParameters( float flMaxOccludeeArea, float flMinOccluderArea );
virtual float MinOccluderArea() const;
virtual void DrawDebugOverlays();
private:
struct AxisAlignedPlane_t
{
int m_nAxis;
float m_flSign;
float m_flDist;
};
// Recomputes the edge list for occluders
void RecomputeOccluderEdgeList();
// Is the point inside the near plane?
bool IsPointInsideNearPlane( const Vector &vecPos ) const;
void IntersectWithNearPlane( const Vector &vecStart, const Vector &vecEnd, Vector &outPos ) const;
// Clips a polygon to the near clip plane
int ClipPolygonToNearPlane( Vector **ppVertices, int nVertexCount, Vector **ppOutVerts, bool *pClipped ) const;
// Project world-space verts + add into the edge list
void AddPolygonToEdgeList( CEdgeList &edgeList, Vector **ppPolygon, int nCount, int nSurfID, bool bClipped );
// Computes the plane equation of a polygon in screen space from a camera-space plane
void ComputeScreenSpacePlane( const cplane_t &cameraSpacePlane, cplane_t *pScreenSpacePlane );
// Used to clip the screen space polygons to the screen
void ResetClipTempVerts();
int ClipPolygonToAxisAlignedPlane( Vector **ppVertices, int nVertexCount,
const AxisAlignedPlane_t &plane, Vector **ppOutVerts ) const;
// Is the point within an axis-aligned plane?
bool IsPointInsideAAPlane( const Vector &vecPos, const AxisAlignedPlane_t &plane ) const;
void IntersectWithAAPlane( const Vector &vecStart, const Vector &vecEnd, const AxisAlignedPlane_t &plane, Vector &outPos ) const;
// Stitches up clipped vertices
void StitchClippedVertices( Vector *pVertices, int nCount );
private:
// Per-frame information
bool m_bEdgeListDirty;
VMatrix m_WorldToProjection;
VMatrix m_WorldToCamera;
float m_flXProjScale;
float m_flYProjScale;
float m_flProjDistScale;
float m_flProjDistOffset;
Vector m_vecCameraPosition; // in world space
cplane_t m_NearClipPlane;
float m_flNearPlaneDist;
float m_flFOVFactor;
CEdgeList m_EdgeList;
CWingedEdgeList m_WingedEdgeList;
CUtlVector< Vector > m_ClippedVerts;
float m_flMaxOccludeeArea;
float m_flMinOccluderArea;
// Stats
int m_nTests;
int m_nOccluded;
};
static COcclusionSystem g_OcclusionSystem;
//-----------------------------------------------------------------------------
// Singleton accessor
//-----------------------------------------------------------------------------
IOcclusionSystem *OcclusionSystem()
{
return &g_OcclusionSystem;
}
//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------
COcclusionSystem::COcclusionSystem() : m_ClippedVerts( 0, 64 )
{
m_bEdgeListDirty = false;
m_nTests = 0;
m_nOccluded = 0;
m_flMinOccluderArea = DEFAULT_MIN_OCCLUDER_AREA;
m_flMaxOccludeeArea = DEFAULT_MAX_OCCLUDEE_AREA;
}
COcclusionSystem::~COcclusionSystem()
{
}
//-----------------------------------------------------------------------------
// Occlusion parameters?
//-----------------------------------------------------------------------------
void COcclusionSystem::SetOcclusionParameters( float flMaxOccludeeArea, float flMinOccluderArea )
{
m_flMaxOccludeeArea = (flMaxOccludeeArea ? flMaxOccludeeArea : DEFAULT_MAX_OCCLUDEE_AREA) * 0.01f;
m_flMinOccluderArea = (flMinOccluderArea ? flMinOccluderArea : DEFAULT_MIN_OCCLUDER_AREA);
}
float COcclusionSystem::MinOccluderArea() const
{
return m_flMinOccluderArea;
}
//-----------------------------------------------------------------------------
// Is the point within the near plane?
//-----------------------------------------------------------------------------
inline bool COcclusionSystem::IsPointInsideNearPlane( const Vector &vecPos ) const
{
return DotProduct( vecPos, m_NearClipPlane.normal ) >= m_NearClipPlane.dist;
}
inline void COcclusionSystem::IntersectWithNearPlane( const Vector &vecStart, const Vector &vecEnd, Vector &outPos ) const
{
Vector vecDir;
VectorSubtract( vecEnd, vecStart, vecDir );
float t = IntersectRayWithPlane( vecStart, vecDir, m_NearClipPlane.normal, m_NearClipPlane.dist );
VectorLerp( vecStart, vecEnd, t, outPos );
}
//-----------------------------------------------------------------------------
// Clips a surface to the near clip plane
// FIXME: This blows: a *third* S-H clipper in the engine! All because the
// vertex formats are different owing to different goals of the 3 clippers
//-----------------------------------------------------------------------------
static Vector s_TempVertMemory[256];
int COcclusionSystem::ClipPolygonToNearPlane( Vector **ppVertices, int nVertexCount, Vector **ppOutVerts, bool *pClipped ) const
{
*pClipped = false;
if ( nVertexCount < 3 )
return 0;
// Ye Olde Sutherland-Hodgman clipping algorithm
int nOutVertCount = 0;
int nNewVertCount = 0;
Vector* pStart = ppVertices[ nVertexCount - 1 ];
bool bStartInside = IsPointInsideNearPlane( *pStart );
for ( int i = 0; i < nVertexCount; ++i )
{
Vector* pEnd = ppVertices[ i ];
bool bEndInside = IsPointInsideNearPlane( *pEnd );
if (bEndInside)
{
if (!bStartInside)
{
// Started outside, ended inside, need to clip the edge
ppOutVerts[nOutVertCount] = &s_TempVertMemory[ nNewVertCount++ ];
IntersectWithNearPlane( *pStart, *pEnd, *ppOutVerts[nOutVertCount] );
++nOutVertCount;
*pClipped = true;
}
ppOutVerts[nOutVertCount++] = pEnd;
}
else
{
if (bStartInside)
{
// Started inside, ended outside, need to clip the edge
ppOutVerts[nOutVertCount] = &s_TempVertMemory[ nNewVertCount++ ];
IntersectWithNearPlane( *pStart, *pEnd, *ppOutVerts[nOutVertCount] );
++nOutVertCount;
*pClipped = true;
}
}
pStart = pEnd;
bStartInside = bEndInside;
}
return nOutVertCount;
}
//-----------------------------------------------------------------------------
// Is the point within an axis-aligned plane?
//-----------------------------------------------------------------------------
inline bool COcclusionSystem::IsPointInsideAAPlane( const Vector &vecPos, const AxisAlignedPlane_t &plane ) const
{
return vecPos[plane.m_nAxis] * plane.m_flSign >= plane.m_flDist;
}
inline void COcclusionSystem::IntersectWithAAPlane( const Vector &vecStart, const Vector &vecEnd, const AxisAlignedPlane_t &plane, Vector &outPos ) const
{
float t = IntersectRayWithAAPlane( vecStart, vecEnd, plane.m_nAxis, plane.m_flSign, plane.m_flDist );
VectorLerp( vecStart, vecEnd, t, outPos );
}
//-----------------------------------------------------------------------------
// Clips a surface to the edges of the screen (axis-aligned planes)
//-----------------------------------------------------------------------------
static int s_nTempVertCount = 0;
void COcclusionSystem::ResetClipTempVerts()
{
s_nTempVertCount = 0;
}
int COcclusionSystem::ClipPolygonToAxisAlignedPlane( Vector **ppVertices, int nVertexCount,
const AxisAlignedPlane_t &plane, Vector **ppOutVerts ) const
{
// Ye Olde Sutherland-Hodgman clipping algorithm
int nOutVertCount = 0;
Vector* pStart = ppVertices[ nVertexCount - 1 ];
bool bStartInside = IsPointInsideAAPlane( *pStart, plane );
for ( int i = 0; i < nVertexCount; ++i )
{
Vector* pEnd = ppVertices[ i ];
bool bEndInside = IsPointInsideAAPlane( *pEnd, plane );
if (bEndInside)
{
if (!bStartInside)
{
// Started outside, ended inside, need to clip the edge
ppOutVerts[nOutVertCount] = &s_TempVertMemory[ s_nTempVertCount++ ];
IntersectWithAAPlane( *pStart, *pEnd, plane, *ppOutVerts[nOutVertCount] );
++nOutVertCount;
}
ppOutVerts[nOutVertCount++] = pEnd;
}
else
{
if (bStartInside)
{
// Started inside, ended outside, need to clip the edge
ppOutVerts[nOutVertCount] = &s_TempVertMemory[ s_nTempVertCount++ ];
IntersectWithAAPlane( *pStart, *pEnd, plane, *ppOutVerts[nOutVertCount] );
++nOutVertCount;
}
}
pStart = pEnd;
bStartInside = bEndInside;
}
return nOutVertCount;
}
//-----------------------------------------------------------------------------
// Computes the plane equation of a polygon in screen space from a world-space plane
//-----------------------------------------------------------------------------
void COcclusionSystem::ComputeScreenSpacePlane( const cplane_t &cameraSpacePlane, cplane_t *pScreenSpacePlane )
{
// Here's how this is computed:
// If the *camera* space plane is Ax+By+Cz = D,
// and xs = -(xf) * x/z, ys = -(yf) y/z, zs = - (zc + zf * ooz)
// Then x = -xs * z / xf, y = -ys * z / yf, ooz = -(zs + zc) / zf
// So - A * xs * z / xf - B * ys * z / yf + C * z = D
// - A xs / xf - B ys / yf + C = D * ooz
// (A/D) xs/xf + (B/D) ys/yf + ooz = (C/D)
// (A/D) xs/xf + (B/D) ys/yf - (zs + zc) / zf = (C/D)
// -(A/D) xs/xf - (B/D) ys/yf + (zs + zc) / zf = -(C/D)
// -zf * (A/D) xs/xf - zf * (B/D) ys/yf + zs = -zf * (C/D) - zc
// Let A' = -zf/xf*(A/D), B' = -zf/yf*(B/D), D' = -zf * (C/D) - zc
// A' xs + B' ys + zs = D' is the screen space plane equation
float ooD = (cameraSpacePlane.dist != 0) ? (1.0f / cameraSpacePlane.dist) : 0.0f;
pScreenSpacePlane->normal.x = cameraSpacePlane.normal.x * ooD * m_flXProjScale;
pScreenSpacePlane->normal.y = cameraSpacePlane.normal.y * ooD * m_flYProjScale;
pScreenSpacePlane->normal.z = 1;
pScreenSpacePlane->dist = cameraSpacePlane.normal.z * ooD * m_flProjDistScale + m_flProjDistOffset;
}
//-----------------------------------------------------------------------------
// Stitches up clipped vertices
//-----------------------------------------------------------------------------
void COcclusionSystem::StitchClippedVertices( Vector *pVertices, int nCount )
{
for ( int i = 0; i < nCount; ++i )
{
// Only stitch ones that have been clipped by the near clip plane
if ( fabs( pVertices[i].z ) > 1e-3 )
continue;
int j;
for ( j = m_ClippedVerts.Count(); --j >= 0; )
{
if ( VectorsAreEqual( pVertices[i], m_ClippedVerts[j], 1e-3 ) )
{
pVertices[i] = m_ClippedVerts[j];
break;
}
}
if ( j < 0 )
{
MEM_ALLOC_CREDIT();
// No match found...
m_ClippedVerts.AddToTail( pVertices[i] );
}
}
}
//-----------------------------------------------------------------------------
// Project world-space verts + add into the edge list
//-----------------------------------------------------------------------------
void COcclusionSystem::AddPolygonToEdgeList( CEdgeList &edgeList, Vector **ppPolygon, int nCount, int nSurfID, bool bClipped )
{
// Transform the verts into projection space
// Transform into projection space (extra logic here is to simply guarantee that we project each vert exactly once)
int nMaxClipVerts = (nCount * 4);
int nClipCount, nClipCount1;
Vector **ppClipVertex = (Vector**)stackalloc( nMaxClipVerts * sizeof(Vector*) );
Vector **ppClipVertex1 = (Vector**)stackalloc( nMaxClipVerts * sizeof(Vector*) );
Vector *pVecProjectedVertex = (Vector*)stackalloc( nCount * sizeof(Vector) );
int k;
for ( k = 0; k < nCount; ++k )
{
Vector3DMultiplyPositionProjective( m_WorldToProjection, *(ppPolygon[k]), pVecProjectedVertex[k] );
// Clamp needed to avoid precision problems.
// if ( pVecProjectedVertex[k].z < 0.0f )
// pVecProjectedVertex[k].z = 0.0f;
pVecProjectedVertex[k].z *= (pVecProjectedVertex[k].z > 0.0f);
ppClipVertex[k] = &pVecProjectedVertex[k];
}
// Clip vertices to the screen in x,y...
AxisAlignedPlane_t aaPlane;
aaPlane.m_nAxis = 0;
aaPlane.m_flDist = -1;
aaPlane.m_flSign = -1;
nClipCount = nCount;
ResetClipTempVerts();
nClipCount1 = ClipPolygonToAxisAlignedPlane( ppClipVertex, nClipCount, aaPlane, ppClipVertex1 );
if ( nClipCount1 < 3 )
return;
Assert( nClipCount1 < nMaxClipVerts );
aaPlane.m_flSign = 1;
nClipCount = ClipPolygonToAxisAlignedPlane( ppClipVertex1, nClipCount1, aaPlane, ppClipVertex );
if ( nClipCount < 3 )
return;
Assert( nClipCount < nMaxClipVerts );
aaPlane.m_nAxis = 1;
nClipCount1 = ClipPolygonToAxisAlignedPlane( ppClipVertex, nClipCount, aaPlane, ppClipVertex1 );
if ( nClipCount1 < 3 )
return;
Assert( nClipCount1 < nMaxClipVerts );
aaPlane.m_flSign = -1;
nClipCount = ClipPolygonToAxisAlignedPlane( ppClipVertex1, nClipCount1, aaPlane, ppClipVertex );
if ( nClipCount < 3 )
return;
Assert( nClipCount < nMaxClipVerts );
// Compute the screen area...
float flScreenArea = 0.0f;
int nLastClipVert = nClipCount - 1;
for ( k = 1; k < nLastClipVert; ++k )
{
// Using area times two simply because it's faster...
float flTriArea = TriArea2DTimesTwo( (*ppClipVertex[0]), (*ppClipVertex[k]), (*ppClipVertex[k+1]) );
Assert( flTriArea <= 1e-3 );
if ( flTriArea < 0 )
{
flScreenArea += -flTriArea;
}
}
edgeList.SetSurfaceArea( nSurfID, flScreenArea );
// If there's a clipped vertex, attempt to seam up with other edges...
if ( bClipped )
{
StitchClippedVertices( pVecProjectedVertex, nCount );
}
// Add in the edges of the *unclipped* polygon: to avoid precision errors
Vector *ppEdgeVertices[2];
int nLastVert = nCount - 1;
ppEdgeVertices[ 1 ] = &pVecProjectedVertex[ nLastVert ];
for ( k = 0; k < nLastVert; ++k )
{
ppEdgeVertices[ k & 0x1 ] = &pVecProjectedVertex[ k ];
edgeList.AddEdge( ppEdgeVertices, nSurfID );
}
ppEdgeVertices[ nLastVert & 0x1 ] = &pVecProjectedVertex[ nLastVert ];
edgeList.AddEdge( ppEdgeVertices, nSurfID );
}
//-----------------------------------------------------------------------------
// Recomputes the occluder edge list
//-----------------------------------------------------------------------------
void COcclusionSystem::RecomputeOccluderEdgeList()
{
if ( !m_bEdgeListDirty )
return;
// Tracker 17772: If building cubemaps can end up calling into here w/o cl.pAreaBits setup yet, oh well.
if ( !cl.m_bAreaBitsValid && CommandLine()->FindParm( "-buildcubemaps" ) )
return;
m_bEdgeListDirty = false;
m_EdgeList.RemoveAll();
m_WingedEdgeList.Clear();
m_ClippedVerts.RemoveAll();
mvertex_t *pVertices = host_state.worldbrush->vertexes;
int *pIndices = host_state.worldbrush->occludervertindices;
doccluderdata_t *pOccluders = host_state.worldbrush->occluders;
int i, j, k;
for ( i = host_state.worldbrush->numoccluders ; --i >= 0; )
{
if ( pOccluders[i].flags & OCCLUDER_FLAGS_INACTIVE )
continue;
// Skip the occluder if it's in a disconnected area
if ( cl.m_chAreaBits &&
(cl.m_chAreaBits[pOccluders[i].area >> 3] & (1 << ( pOccluders[i].area & 0x7 )) ) == 0 )
continue;
int nSurfID = pOccluders[i].firstpoly;
int nSurfCount = pOccluders[i].polycount;
for ( j = 0; j < nSurfCount; ++j, ++nSurfID )
{
doccluderpolydata_t *pSurf = &host_state.worldbrush->occluderpolys[nSurfID];
int nFirstVertexIndex = pSurf->firstvertexindex;
int nVertexCount = pSurf->vertexcount;
// If the surface is backfacing, blow it off...
const cplane_t &surfPlane = host_state.worldbrush->planes[ pSurf->planenum ];
if ( DotProduct( surfPlane.normal, m_vecCameraPosition ) <= surfPlane.dist )
continue;
// Clip to the near plane (has to be done in world space)
Vector **ppSurfVerts = (Vector**)stackalloc( ( nVertexCount ) * sizeof(Vector*) );
Vector **ppClipVerts = (Vector**)stackalloc( ( nVertexCount * 2 ) * sizeof(Vector*) );
for ( k = 0; k < nVertexCount; ++k )
{
int nVertIndex = pIndices[nFirstVertexIndex + k];
ppSurfVerts[k] = &( pVertices[nVertIndex].position );
}
bool bClipped;
int nClipCount = ClipPolygonToNearPlane( ppSurfVerts, nVertexCount, ppClipVerts, &bClipped );
Assert( nClipCount <= ( nVertexCount * 2 ) );
if ( nClipCount < 3 )
continue;
cplane_t projectionSpacePlane;
cplane_t cameraSpacePlane;
MatrixTransformPlane( m_WorldToCamera, surfPlane, cameraSpacePlane );
ComputeScreenSpacePlane( cameraSpacePlane, &projectionSpacePlane );
int nEdgeSurfID = m_EdgeList.AddSurface( projectionSpacePlane );
// Transform into projection space (extra logic here is to simply guarantee that we project each vert exactly once)
AddPolygonToEdgeList( m_EdgeList, ppClipVerts, nClipCount, nEdgeSurfID, bClipped );
}
}
m_EdgeList.CullSmallOccluders();
m_EdgeList.ReduceActiveList( m_WingedEdgeList );
// Msg("Edge count %d -> %d\n", m_EdgeList.EdgeCount(), m_WingedEdgeList.EdgeCount() );
// Draw the occluders
unsigned char color[4] = { 255, 255, 255, 255 };
m_WingedEdgeList.QueueVisualization( color );
}
//-----------------------------------------------------------------------------
// Occluder list management
//-----------------------------------------------------------------------------
void COcclusionSystem::ActivateOccluder( int nOccluderIndex, bool bActive )
{
if ( ( nOccluderIndex >= host_state.worldbrush->numoccluders ) || ( nOccluderIndex < 0 ) )
return;
if ( bActive )
{
host_state.worldbrush->occluders[nOccluderIndex].flags &= ~OCCLUDER_FLAGS_INACTIVE;
}
else
{
host_state.worldbrush->occluders[nOccluderIndex].flags |= OCCLUDER_FLAGS_INACTIVE;
}
m_bEdgeListDirty = true;
}
void COcclusionSystem::SetView( const Vector &vecCameraPos, float flFOV, const VMatrix &worldToCamera,
const VMatrix &cameraToProjection, const VPlane &nearClipPlane )
{
m_vecCameraPosition = vecCameraPos;
m_WorldToCamera = worldToCamera;
// See ComputeScreenSpacePlane() for the use of these constants
m_flXProjScale = -cameraToProjection[2][3] / cameraToProjection[0][0];
m_flYProjScale = -cameraToProjection[2][3] / cameraToProjection[1][1];
m_flProjDistScale = -cameraToProjection[2][3];
m_flProjDistOffset = -cameraToProjection[2][2];
MatrixMultiply( cameraToProjection, worldToCamera, m_WorldToProjection );
m_NearClipPlane.normal = nearClipPlane.m_Normal;
m_NearClipPlane.dist = nearClipPlane.m_Dist;
m_NearClipPlane.type = 3;
m_bEdgeListDirty = true;
m_flNearPlaneDist = -( DotProduct( vecCameraPos, m_NearClipPlane.normal ) - m_NearClipPlane.dist );
Assert( m_flNearPlaneDist > 0.0f );
m_flFOVFactor = m_flNearPlaneDist * tan( flFOV * 0.5f * M_PI / 180.0f );
m_flFOVFactor = m_flNearPlaneDist / m_flFOVFactor;
m_flFOVFactor *= m_flFOVFactor;
if ( r_occlusionspew.GetInt() )
{
if ( m_nTests )
{
float flPercent = 100.0f * ((float)m_nOccluded / (float)m_nTests);
Msg("Occl %.2f (%d/%d)\n", flPercent, m_nOccluded, m_nTests );
m_nTests = 0;
m_nOccluded = 0;
}
}
}
//-----------------------------------------------------------------------------
// Used to build the quads to test for occlusion
//-----------------------------------------------------------------------------
static int s_pFaceIndices[6][4] =
{
{ 0, 4, 6, 2 }, // -x
{ 1, 3, 7, 5 }, // +x
{ 0, 1, 5, 4 }, // -y
{ 2, 6, 7, 3 }, // +y
{ 0, 2, 3, 1 }, // -z
{ 4, 5, 7, 6 }, // +z
};
static int s_pSourceIndices[8] =
{
-1, 0, 0, 1, 0, 1, 2, 3
};
static int s_pDeltaIndices[8] =
{
-1, 0, 1, 1, 2, 2, 2, 2
};
static unsigned char s_VisualizationColor[2][4] =
{
{ 255, 0, 0, 255 },
{ 0, 255, 0, 255 }
};
struct EdgeInfo_t
{
unsigned char m_nVert[2];
unsigned char m_nFace[2];
int m_nTestCount;
int m_nMinVert;
};
// NOTE: The face indices here have to very carefully ordered for the algorithm
// to work. They must be ordered so that vert0 -> vert1 is clockwise
// for the first face listed and vert1 -> vert0 is clockwise for the 2nd face listed
static EdgeInfo_t s_pEdges[12] =
{
{ { 0, 1 }, { 2, 4 }, 0, 0 }, // 0: Edge between -y + -z
{ { 2, 0 }, { 0, 4 }, 0, 0 }, // 1: Edge between -x + -z
{ { 1, 3 }, { 1, 4 }, 0, 0 }, // 2: Edge between +x + -z
{ { 3, 2 }, { 3, 4 }, 0, 0 }, // 3: Edge between +y + -z
{ { 0, 4 }, { 0, 2 }, 0, 0 }, // 4: Edge between -x + -y
{ { 5, 1 }, { 1, 2 }, 0, 0 }, // 5: Edge between +x + -y
{ { 6, 2 }, { 0, 3 }, 0, 0 }, // 6: Edge between -x + +y
{ { 3, 7 }, { 1, 3 }, 0, 0 }, // 7: Edge between +x + +y
{ { 5, 4 }, { 2, 5 }, 0, 0 }, // 8: Edge between -y + +z
{ { 4, 6 }, { 0, 5 }, 0, 0 }, // 9: Edge between -x + +z
{ { 7, 5 }, { 1, 5 }, 0, 0 }, // 10:Edge between +x + +z
{ { 6, 7 }, { 3, 5 }, 0, 0 }, // 11:Edge between +y + +z
};
static int s_pFaceEdges[6][4] =
{
{ 4, 9, 6, 1 },
{ 2, 7, 10, 5 },
{ 0, 5, 8, 4 },
{ 6, 11, 7, 3 },
{ 1, 3, 2, 0 },
{ 8, 10, 11, 9 },
};
//-----------------------------------------------------------------------------
// Occlusion checks
//-----------------------------------------------------------------------------
static CWingedEdgeList s_WingedTestEdgeList;
class WingedEdgeLessFunc
{
public:
bool Less( const int& src1, const int& src2, void *pCtx )
{
Vector *pVertices = (Vector*)pCtx;
EdgeInfo_t *pEdge1 = &s_pEdges[ src1 ];
EdgeInfo_t *pEdge2 = &s_pEdges[ src2 ];
Vector *pV1 = &pVertices[ pEdge1->m_nVert[ pEdge1->m_nMinVert ] ];
Vector *pV2 = &pVertices[ pEdge2->m_nVert[ pEdge2->m_nMinVert ] ];
if (pV1->y < pV2->y)
return true;
if (pV1->y > pV2->y)
return false;
if (pV1->x < pV2->x)
return true;
if (pV1->x > pV2->x)
return false;
// This is the same as the following line:
// return (pEdge1->m_flDxDy <= pEdge2->m_flDxDy);
Vector2D dEdge1, dEdge2;
Vector2DSubtract( pVertices[ pEdge1->m_nVert[ 1 - pEdge1->m_nMinVert ] ].AsVector2D(), pV1->AsVector2D(), dEdge1 );
Vector2DSubtract( pVertices[ pEdge2->m_nVert[ 1 - pEdge2->m_nMinVert ] ].AsVector2D(), pV2->AsVector2D(), dEdge2 );
Assert( dEdge1.y >= 0.0f );
Assert( dEdge2.y >= 0.0f );
return dEdge1.x * dEdge2.y <= dEdge1.y * dEdge2.x;
}
};
bool COcclusionSystem::IsOccluded( const Vector &vecAbsMins, const Vector &vecAbsMaxs )
{
if ( r_occlusion.GetInt() == 0 )
return false;
VPROF_BUDGET( "COcclusionSystem::IsOccluded", VPROF_BUDGETGROUP_OCCLUSION );
// @MULTICORE (toml 9/11/2006): need to eliminate this mutex
static CThreadFastMutex mutex;
AUTO_LOCK( mutex );
RecomputeOccluderEdgeList();
// No occluders? Then the edge list isn't occluded
if ( m_WingedEdgeList.EdgeCount() == 0 )
return false;
// Don't occlude things that have large screen area
// Use a super cheap but inaccurate screen area computation
Vector vecCenter;
VectorAdd( vecAbsMaxs, vecAbsMins, vecCenter );
vecCenter *= 0.5f;
vecCenter -= m_vecCameraPosition;
float flDist = DotProduct( m_NearClipPlane.normal, vecCenter );
if (flDist <= 0.0f)
return false;
flDist += m_flNearPlaneDist;
Vector vecSize;
VectorSubtract( vecAbsMaxs, vecAbsMins, vecSize );
float flRadiusSq = DotProduct( vecSize, vecSize ) * 0.25f;
float flScreenArea = m_flFOVFactor * flRadiusSq / (flDist * flDist);
float flMaxSize = r_occludeemaxarea.GetFloat() * 0.01f;
if ( flMaxSize == 0.0f )
{
flMaxSize = m_flMaxOccludeeArea;
}
if (flScreenArea >= flMaxSize)
return false;
// Clear out its state
s_WingedTestEdgeList.Clear();
// NOTE: This assumes that frustum culling has already occurred on this object
// If that were not the case, we'd need to add a little extra into this
// (probably a single plane test, which tests if the box is wholly behind the camera )
// Convert the bbox into a max of 3 quads...
const Vector *pCornerVert[2] = { &vecAbsMins, &vecAbsMaxs };
// Compute the 8 box verts, and transform them into projective space...
// NOTE: We'd want to project them *after* the plane test if there were
// no frustum culling.
int i;
Vector pVecProjectedVertex[8];
// NOTE: The code immediately below is an optimized version of this loop
// The optimization takes advantage of the fact that the verts are all
// axis aligned.
// Vector vecBoxVertex;
// for ( i = 0; i < 8; ++i )
// {
// vecBoxVertex.x = pCornerVert[ (i & 0x1) ]->x;
// vecBoxVertex.y = pCornerVert[ (i & 0x2) >> 1 ]->y;
// vecBoxVertex.z = pCornerVert[ (i & 0x4) >> 2 ]->z;
// Vector3DMultiplyPositionProjective( m_WorldToProjection, vecBoxVertex, pVecProjectedVertex[ i ] );
// if ( pVecProjectedVertex[ i ].z <= 0.0f )
// return false;
// }
Vector4D vecProjVert[8];
Vector4D vecDeltaProj[3];
Vector4D vecAbsMins4D( vecAbsMins.x, vecAbsMins.y, vecAbsMins.z, 1.0f );
Vector4DMultiply( m_WorldToProjection, vecAbsMins4D, vecProjVert[0] );
if ( vecProjVert[0].w <= 0.0f )
return false;
float flOOW = 1.0f / vecProjVert[0].w;
vecDeltaProj[0].Init( vecSize.x * m_WorldToProjection[0][0], vecSize.x * m_WorldToProjection[1][0], vecSize.x * m_WorldToProjection[2][0], vecSize.x * m_WorldToProjection[3][0] );
vecDeltaProj[1].Init( vecSize.y * m_WorldToProjection[0][1], vecSize.y * m_WorldToProjection[1][1], vecSize.y * m_WorldToProjection[2][1], vecSize.y * m_WorldToProjection[3][1] );
vecDeltaProj[2].Init( vecSize.z * m_WorldToProjection[0][2], vecSize.z * m_WorldToProjection[1][2], vecSize.z * m_WorldToProjection[2][2], vecSize.z * m_WorldToProjection[3][2] );
pVecProjectedVertex[0].Init( vecProjVert[0].x * flOOW, vecProjVert[0].y * flOOW, vecProjVert[0].z * flOOW );
if ( pVecProjectedVertex[0].z <= 0.0f )
return false;
for ( i = 1; i < 8; ++i )
{
int nIndex = s_pSourceIndices[i];
int nDelta = s_pDeltaIndices[i];
Vector4DAdd( vecProjVert[nIndex], vecDeltaProj[nDelta], vecProjVert[i] );
if ( vecProjVert[ i ].w <= 0.0f )
return false;
flOOW = 1.0f / vecProjVert[i].w;
pVecProjectedVertex[ i ].Init( vecProjVert[i].x * flOOW, vecProjVert[i].y * flOOW, vecProjVert[i].z * flOOW );
if ( pVecProjectedVertex[ i ].z <= 0.0f )
return false;
}
// Precompute stuff needed by the loop over faces below
float pSign[2] = { -1, 1 };
Vector vecDelta[2];
VectorSubtract( *pCornerVert[0], m_vecCameraPosition, vecDelta[0] );
VectorSubtract( m_vecCameraPosition, *pCornerVert[1], vecDelta[1] );
// Determine which faces + edges are visible...
++m_nTests;
int pSurfInd[6];
for ( i = 0; i < 6; ++i )
{
int nDim = ( i >> 1 );
int nInd = i & 0x1;
// Try to backface cull each of the 6 box faces
if ( vecDelta[nInd][nDim] <= 0.0f )
{
pSurfInd[i] = -1;
continue;
}
cplane_t cameraSpacePlane, projectionSpacePlane;
float flSign = pSign[nInd];
float flPlaneDist = (*pCornerVert[nInd])[ nDim ] * flSign;
MatrixTransformAxisAlignedPlane( m_WorldToCamera, nDim, flSign, flPlaneDist, cameraSpacePlane );
ComputeScreenSpacePlane( cameraSpacePlane, &projectionSpacePlane );
int nSurfID = s_WingedTestEdgeList.AddSurface( projectionSpacePlane );
pSurfInd[i] = nSurfID;
// Mark edges as being used...
int *pFaceEdges = s_pFaceEdges[i];
s_pEdges[ pFaceEdges[0] ].m_nTestCount = m_nTests;
s_pEdges[ pFaceEdges[1] ].m_nTestCount = m_nTests;
s_pEdges[ pFaceEdges[2] ].m_nTestCount = m_nTests;
s_pEdges[ pFaceEdges[3] ].m_nTestCount = m_nTests;
}
// Sort edges by minimum Y + dx/dy...
int pEdgeSort[12];
CUtlSortVector< int, WingedEdgeLessFunc > edgeSort( pEdgeSort, 12 );
edgeSort.SetLessContext( pVecProjectedVertex );
for ( i = 0; i < 12; ++i )
{
// Skip non-visible edges
EdgeInfo_t *pEdge = &s_pEdges[i];
if ( pEdge->m_nTestCount != m_nTests )
continue;
pEdge->m_nMinVert = ( pVecProjectedVertex[ pEdge->m_nVert[0] ].y >= pVecProjectedVertex[ pEdge->m_nVert[1] ].y );
edgeSort.Insert( i );
}
// Now add them into the winged edge list, in sorted order...
int nEdgeCount = edgeSort.Count();
for ( i = 0; i < nEdgeCount; ++i )
{
EdgeInfo_t *pEdge = &s_pEdges[edgeSort[i]];
// The enter + leave ids depend entirely on which edge is further up
// This works because the edges listed in s_pEdges show the edges as they
// would be visited in *clockwise* order
const Vector &startVert = pVecProjectedVertex[pEdge->m_nVert[pEdge->m_nMinVert]];
const Vector &endVert = pVecProjectedVertex[pEdge->m_nVert[1 - pEdge->m_nMinVert]];
int nLeaveSurfID = pSurfInd[ pEdge->m_nFace[pEdge->m_nMinVert] ];
int nEnterSurfID = pSurfInd[ pEdge->m_nFace[1 - pEdge->m_nMinVert] ];
s_WingedTestEdgeList.AddEdge( startVert, endVert, nLeaveSurfID, nEnterSurfID );
}
#ifdef DEBUG_OCCLUSION_SYSTEM
s_WingedTestEdgeList.CheckConsistency();
#endif
// Now let's see if this edge list is occluded or not..
bool bOccluded = m_WingedEdgeList.IsOccludingEdgeList( s_WingedTestEdgeList );
if (bOccluded)
{
++m_nOccluded;
}
s_WingedTestEdgeList.QueueVisualization( s_VisualizationColor[bOccluded] );
return bOccluded;
}
//-----------------------------------------------------------------------------
// Used to debug the occlusion system
//-----------------------------------------------------------------------------
void VisualizeQueuedEdges( )
{
#ifndef SWDS
if ( !g_EdgeVisualization.Count() )
return;
CMatRenderContextPtr pRenderContext( materials );
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->PushMatrix();
pRenderContext->LoadIdentity();
pRenderContext->MatrixMode( MATERIAL_VIEW );
pRenderContext->PushMatrix();
pRenderContext->LoadIdentity();
pRenderContext->MatrixMode( MATERIAL_PROJECTION );
pRenderContext->PushMatrix();
pRenderContext->LoadIdentity();
pRenderContext->Bind( g_pMaterialWireframeVertexColorIgnoreZ );
IMesh *pMesh = pRenderContext->GetDynamicMesh( );
CMeshBuilder meshBuilder;
meshBuilder.Begin( pMesh, MATERIAL_LINES, g_EdgeVisualization.Count() );
int i;
for ( i = g_EdgeVisualization.Count(); --i >= 0; )
{
EdgeVisualizationInfo_t &info = g_EdgeVisualization[i];
meshBuilder.Position3fv( info.m_vecPoint[0].Base() );
meshBuilder.Color4ubv( info.m_pColor );
meshBuilder.AdvanceVertex();
meshBuilder.Position3fv( info.m_vecPoint[1].Base() );
#ifdef DEBUG_OCCLUSION_SYSTEM
meshBuilder.Color4ub( 0, 0, 255, 255 );
#else
meshBuilder.Color4ubv( info.m_pColor );
#endif
meshBuilder.AdvanceVertex();
}
meshBuilder.End();
pMesh->Draw();
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->PopMatrix();
pRenderContext->MatrixMode( MATERIAL_VIEW );
pRenderContext->PopMatrix();
pRenderContext->MatrixMode( MATERIAL_PROJECTION );
pRenderContext->PopMatrix();
g_EdgeVisualization.RemoveAll();
#endif
}
//-----------------------------------------------------------------------------
// Render debugging overlay
//-----------------------------------------------------------------------------
void COcclusionSystem::DrawDebugOverlays()
{
// Draw the occludees
VisualizeQueuedEdges();
}
|