1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384
|
// clang-format off
/* This file is based on OpenSceneGraph's src/osgShadow/ViewDependentShadowMap.cpp.
* Where applicable, any changes made are covered by OpenMW's GPL 3 license, not the OSGPL.
* The original copyright notice is listed below.
*/
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2011 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include "mwshadowtechnique.hpp"
#include <osgShadow/ShadowedScene>
#include <osg/CullFace>
#include <osg/Geometry>
#include <osg/io_utils>
#include <osg/Depth>
#include <osg/ClipControl>
#include <sstream>
#include <deque>
#include <vector>
#include "glextensions.hpp"
#include "shadowsbin.hpp"
namespace {
using namespace osgShadow;
using namespace SceneUtil;
#define dbl_max std::numeric_limits<double>::max()
//////////////////////////////////////////////////////////////////
// fragment shader
//
#if 0
static const char fragmentShaderSource_withBaseTexture[] =
"uniform sampler2D baseTexture; \n"
"uniform sampler2DShadow shadowTexture; \n"
" \n"
"void main(void) \n"
"{ \n"
" vec4 colorAmbientEmissive = gl_FrontLightModelProduct.sceneColor; \n"
" vec4 color = texture2D( baseTexture, gl_TexCoord[0].xy ); \n"
" color *= mix( colorAmbientEmissive, gl_Color, shadow2DProj( shadowTexture, gl_TexCoord[1] ).r ); \n"
" gl_FragColor = color; \n"
"} \n";
#else
static const char fragmentShaderSource_withBaseTexture[] =
"uniform sampler2D baseTexture; \n"
"uniform int baseTextureUnit; \n"
"uniform sampler2DShadow shadowTexture0; \n"
"uniform int shadowTextureUnit0; \n"
" \n"
"void main(void) \n"
"{ \n"
" vec4 colorAmbientEmissive = gl_FrontLightModelProduct.sceneColor; \n"
" vec4 color = texture2D( baseTexture, gl_TexCoord[baseTextureUnit].xy ); \n"
" color *= mix( colorAmbientEmissive, gl_Color, shadow2DProj( shadowTexture0, gl_TexCoord[shadowTextureUnit0] ).r ); \n"
" gl_FragColor = color; \n"
"} \n";
static const char fragmentShaderSource_withBaseTexture_twoShadowMaps[] =
"uniform sampler2D baseTexture; \n"
"uniform int baseTextureUnit; \n"
"uniform sampler2DShadow shadowTexture0; \n"
"uniform int shadowTextureUnit0; \n"
"uniform sampler2DShadow shadowTexture1; \n"
"uniform int shadowTextureUnit1; \n"
" \n"
"void main(void) \n"
"{ \n"
" vec4 colorAmbientEmissive = gl_FrontLightModelProduct.sceneColor; \n"
" vec4 color = texture2D( baseTexture, gl_TexCoord[baseTextureUnit].xy ); \n"
" float shadow0 = shadow2DProj( shadowTexture0, gl_TexCoord[shadowTextureUnit0] ).r; \n"
" float shadow1 = shadow2DProj( shadowTexture1, gl_TexCoord[shadowTextureUnit1] ).r; \n"
" color *= mix( colorAmbientEmissive, gl_Color, shadow0*shadow1 ); \n"
" gl_FragColor = color; \n"
"} \n";
#endif
std::string debugVertexShaderSource = "void main(void){gl_Position = gl_Vertex; gl_TexCoord[0]=gl_MultiTexCoord0;}";
std::string debugFragmentShaderSource =
"uniform sampler2D texture; \n"
" \n"
"void main(void) \n"
"{ \n"
#if 1
" float f = texture2D(texture, gl_TexCoord[0].xy).r; \n"
" \n"
" f = 256.0 * f; \n"
" float fC = floor( f ) / 256.0; \n"
" \n"
" f = 256.0 * fract( f ); \n"
" float fS = floor( f ) / 256.0; \n"
" \n"
" f = 256.0 * fract( f ); \n"
" float fH = floor( f ) / 256.0; \n"
" \n"
" fS *= 0.5; \n"
" fH = ( fH * 0.34 + 0.66 ) * ( 1.0 - fS ); \n"
" \n"
" vec3 rgb = vec3( ( fC > 0.5 ? ( 1.0 - fC ) : fC ), \n"
" abs( fC - 0.333333 ), \n"
" abs( fC - 0.666667 ) ); \n"
" \n"
" rgb = min( vec3( 1.0, 1.0, 1.0 ), 3.0 * rgb ); \n"
" \n"
" float fMax = max( max( rgb.r, rgb.g ), rgb.b ); \n"
" fMax = 1.0 / fMax; \n"
" \n"
" vec3 color = fMax * rgb; \n"
" \n"
" gl_FragColor = vec4( fS + fH * color, 1 ); \n"
#else
" gl_FragColor = texture2D(texture, gl_TexCoord[0].xy); \n"
#endif
"} \n";
std::string debugFrustumVertexShaderSource = "varying float depth; uniform mat4 transform; void main(void){gl_Position = transform * gl_Vertex; depth = gl_Position.z / gl_Position.w;}";
std::string debugFrustumFragmentShaderSource =
"varying float depth; \n"
" \n"
"void main(void) \n"
"{ \n"
#if 1
" float f = depth; \n"
" \n"
" f = 256.0 * f; \n"
" float fC = floor( f ) / 256.0; \n"
" \n"
" f = 256.0 * fract( f ); \n"
" float fS = floor( f ) / 256.0; \n"
" \n"
" f = 256.0 * fract( f ); \n"
" float fH = floor( f ) / 256.0; \n"
" \n"
" fS *= 0.5; \n"
" fH = ( fH * 0.34 + 0.66 ) * ( 1.0 - fS ); \n"
" \n"
" vec3 rgb = vec3( ( fC > 0.5 ? ( 1.0 - fC ) : fC ), \n"
" abs( fC - 0.333333 ), \n"
" abs( fC - 0.666667 ) ); \n"
" \n"
" rgb = min( vec3( 1.0, 1.0, 1.0 ), 3.0 * rgb ); \n"
" \n"
" float fMax = max( max( rgb.r, rgb.g ), rgb.b ); \n"
" fMax = 1.0 / fMax; \n"
" \n"
" vec3 color = fMax * rgb; \n"
" \n"
" gl_FragColor = vec4( fS + fH * color, 1 ); \n"
#else
" gl_FragColor = vec4(0.0, 0.0, 1.0, 0.0); \n"
#endif
"} \n";
template<class T>
class RenderLeafTraverser : public T
{
public:
RenderLeafTraverser()
{
}
void traverse(const osgUtil::RenderStage* rs)
{
traverse(static_cast<const osgUtil::RenderBin*>(rs));
}
void traverse(const osgUtil::RenderBin* renderBin)
{
const osgUtil::RenderBin::RenderBinList& rbl = renderBin->getRenderBinList();
for(osgUtil::RenderBin::RenderBinList::const_iterator itr = rbl.begin();
itr != rbl.end();
++itr)
{
traverse(itr->second.get());
}
const osgUtil::RenderBin::RenderLeafList& rll = renderBin->getRenderLeafList();
for(osgUtil::RenderBin::RenderLeafList::const_iterator itr = rll.begin();
itr != rll.end();
++itr)
{
handle(*itr);
}
const osgUtil::RenderBin::StateGraphList& rgl = renderBin->getStateGraphList();
for(osgUtil::RenderBin::StateGraphList::const_iterator itr = rgl.begin();
itr != rgl.end();
++itr)
{
traverse(*itr);
}
}
void traverse(const osgUtil::StateGraph* stateGraph)
{
const osgUtil::StateGraph::ChildList& cl = stateGraph->_children;
for(osgUtil::StateGraph::ChildList::const_iterator itr = cl.begin();
itr != cl.end();
++itr)
{
traverse(itr->second.get());
}
const osgUtil::StateGraph::LeafList& ll = stateGraph->_leaves;
for(osgUtil::StateGraph::LeafList::const_iterator itr = ll.begin();
itr != ll.end();
++itr)
{
handle(itr->get());
}
}
inline void handle(const osgUtil::RenderLeaf* renderLeaf)
{
this->operator()(renderLeaf);
}
};
///////////////////////////////////////////////////////////////////////////////////////////////
//
// VDSMCameraCullCallback
//
class VDSMCameraCullCallback : public osg::NodeCallback
{
public:
VDSMCameraCullCallback(MWShadowTechnique* vdsm, osg::Polytope& polytope);
void operator()(osg::Node*, osg::NodeVisitor* nv) override;
osg::RefMatrix* getProjectionMatrix() { return _projectionMatrix.get(); }
osgUtil::RenderStage* getRenderStage() { return _renderStage.get(); }
protected:
MWShadowTechnique* _vdsm;
osg::ref_ptr<osg::RefMatrix> _projectionMatrix;
osg::ref_ptr<osgUtil::RenderStage> _renderStage;
osg::Polytope _polytope;
};
VDSMCameraCullCallback::VDSMCameraCullCallback(MWShadowTechnique* vdsm, osg::Polytope& polytope):
_vdsm(vdsm),
_polytope(polytope)
{
}
void VDSMCameraCullCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osgUtil::CullVisitor* cv = static_cast<osgUtil::CullVisitor*>(nv);
osg::Camera* camera = node->asCamera();
OSG_INFO<<"VDSMCameraCullCallback::operator()(osg::Node* "<<camera<<", osg::NodeVisitor* "<<cv<<")"<<std::endl;
#if 1
if (!_polytope.empty())
{
OSG_INFO<<"Pushing custom Polytope"<<std::endl;
osg::CullingSet& cs = cv->getProjectionCullingStack().back();
cs.setFrustum(_polytope);
cv->pushCullingSet();
}
#endif
// bin has to go inside camera cull or the rendertexture stage will override it
cv->pushStateSet(_vdsm->getOrCreateShadowsBinStateSet());
if (_vdsm->getShadowedScene())
{
_vdsm->getShadowedScene()->osg::Group::traverse(*nv);
}
cv->popStateSet();
#if 1
if (!_polytope.empty())
{
OSG_INFO<<"Popping custom Polytope"<<std::endl;
cv->popCullingSet();
}
#endif
_renderStage = cv->getCurrentRenderBin()->getStage();
OSG_INFO<<"VDSM second : _renderStage = "<<_renderStage<<std::endl;
if (cv->getComputeNearFarMode() != osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR)
{
// make sure that the near plane is computed correctly.
cv->computeNearPlane();
osg::Matrixd projection = *(cv->getProjectionMatrix());
OSG_INFO<<"RTT Projection matrix "<<projection<<std::endl;
osg::Matrix::value_type left, right, bottom, top, zNear, zFar;
osg::Matrix::value_type epsilon = 1e-6;
if (fabs(projection(0,3))<epsilon && fabs(projection(1,3))<epsilon && fabs(projection(2,3))<epsilon )
{
projection.getOrtho(left, right,
bottom, top,
zNear, zFar);
OSG_INFO<<"Ortho zNear="<<zNear<<", zFar="<<zFar<<std::endl;
}
else
{
projection.getFrustum(left, right,
bottom, top,
zNear, zFar);
OSG_INFO<<"Frustum zNear="<<zNear<<", zFar="<<zFar<<std::endl;
}
OSG_INFO<<"Calculated zNear = "<<cv->getCalculatedNearPlane()<<", zFar = "<<cv->getCalculatedFarPlane()<<std::endl;
zNear = osg::maximum(zNear, cv->getCalculatedNearPlane());
zFar = osg::minimum(zFar, cv->getCalculatedFarPlane());
cv->setCalculatedNearPlane(zNear);
cv->setCalculatedFarPlane(zFar);
cv->clampProjectionMatrix(projection, zNear, zFar);
//OSG_INFO<<"RTT zNear = "<<zNear<<", zFar = "<<zFar<<std::endl;
OSG_INFO<<"RTT Projection matrix after clamping "<<projection<<std::endl;
camera->setProjectionMatrix(projection);
}
_projectionMatrix = cv->getProjectionMatrix();
}
} // namespace
MWShadowTechnique::ComputeLightSpaceBounds::ComputeLightSpaceBounds() :
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN)
{
setCullingMode(osg::CullSettings::VIEW_FRUSTUM_CULLING);
}
void MWShadowTechnique::ComputeLightSpaceBounds::reset()
{
osg::CullStack::reset();
_bb = osg::BoundingBox();
}
void MWShadowTechnique::ComputeLightSpaceBounds::apply(osg::Node& node)
{
if (isCulled(node)) return;
// push the culling mode.
pushCurrentMask();
traverse(node);
// pop the culling mode.
popCurrentMask();
}
void MWShadowTechnique::ComputeLightSpaceBounds::apply(osg::Group& node)
{
apply(static_cast<osg::Node&>(node));
}
void MWShadowTechnique::ComputeLightSpaceBounds::apply(osg::Drawable& drawable)
{
if (isCulled(drawable)) return;
// push the culling mode.
pushCurrentMask();
updateBound(drawable.getBoundingBox());
// pop the culling mode.
popCurrentMask();
}
void MWShadowTechnique::ComputeLightSpaceBounds::apply(osg::Geometry& drawable)
{
apply(static_cast<osg::Drawable&>(drawable));
}
void MWShadowTechnique::ComputeLightSpaceBounds::apply(osg::Billboard&)
{
OSG_INFO << "Warning Billboards not yet supported" << std::endl;
return;
}
void MWShadowTechnique::ComputeLightSpaceBounds::apply(osg::Projection&)
{
// projection nodes won't affect a shadow map so their subgraphs should be ignored
return;
}
void MWShadowTechnique::ComputeLightSpaceBounds::apply(osg::Transform& transform)
{
if (isCulled(transform)) return;
// push the culling mode.
pushCurrentMask();
// absolute transforms won't affect a shadow map so their subgraphs should be ignored.
if (transform.getReferenceFrame() == osg::Transform::RELATIVE_RF)
{
osg::RefMatrix* matrix = createOrReuseMatrix(*getModelViewMatrix());
transform.computeLocalToWorldMatrix(*matrix, this);
pushModelViewMatrix(matrix, transform.getReferenceFrame());
traverse(transform);
popModelViewMatrix();
}
// pop the culling mode.
popCurrentMask();
}
void MWShadowTechnique::ComputeLightSpaceBounds::apply(osg::MatrixTransform& transform)
{
apply(static_cast<osg::Transform&>(transform));
}
void MWShadowTechnique::ComputeLightSpaceBounds::apply(osg::Camera&)
{
// camera nodes won't affect a shadow map so their subgraphs should be ignored
return;
}
void MWShadowTechnique::ComputeLightSpaceBounds::updateBound(const osg::BoundingBox& bb)
{
if (!bb.valid()) return;
const osg::Matrix& matrix = *getModelViewMatrix() * *getProjectionMatrix();
update(bb.corner(0) * matrix);
update(bb.corner(1) * matrix);
update(bb.corner(2) * matrix);
update(bb.corner(3) * matrix);
update(bb.corner(4) * matrix);
update(bb.corner(5) * matrix);
update(bb.corner(6) * matrix);
update(bb.corner(7) * matrix);
}
void MWShadowTechnique::ComputeLightSpaceBounds::update(const osg::Vec3& v)
{
if (v.z()<-1.0f)
{
//OSG_NOTICE<<"discarding("<<v<<")"<<std::endl;
return;
}
float x = v.x();
if (x<-1.0f) x = -1.0f;
if (x>1.0f) x = 1.0f;
float y = v.y();
if (y<-1.0f) y = -1.0f;
if (y>1.0f) y = 1.0f;
_bb.expandBy(osg::Vec3(x, y, v.z()));
}
///////////////////////////////////////////////////////////////////////////////////////////////
//
// LightData
//
MWShadowTechnique::LightData::LightData(MWShadowTechnique::ViewDependentData* vdd):
_viewDependentData(vdd),
directionalLight(false)
{
}
void MWShadowTechnique::LightData::setLightData(osg::RefMatrix* lm, const osg::Light* l, const osg::Matrixd& modelViewMatrix)
{
lightMatrix = lm;
light = l;
lightPos = light->getPosition();
directionalLight = (light->getPosition().w()== 0.0);
if (directionalLight)
{
lightPos3.set(0.0, 0.0, 0.0); // directional light has no destinct position
lightDir.set(-lightPos.x(), -lightPos.y(), -lightPos.z());
lightDir.normalize();
OSG_INFO<<" Directional light, lightPos="<<lightPos<<", lightDir="<<lightDir<<std::endl;
if (lightMatrix.valid() && lightMatrix->operator==(osg::Matrixf(modelViewMatrix)))
{
OSG_INFO<<" Light matrix "<<*lightMatrix<<std::endl;
osg::Matrix lightToLocalMatrix(*lightMatrix * osg::Matrix::inverse(modelViewMatrix) );
lightDir = osg::Matrix::transform3x3( lightDir, lightToLocalMatrix );
lightDir.normalize();
OSG_INFO<<" new LightDir ="<<lightDir<<std::endl;
}
}
else
{
OSG_INFO<<" Positional light, lightPos="<<lightPos<<std::endl;
lightDir = light->getDirection();
lightDir.normalize();
if (lightMatrix.valid())
{
OSG_INFO<<" Light matrix "<<*lightMatrix<<std::endl;
osg::Matrix lightToLocalMatrix(*lightMatrix * osg::Matrix::inverse(modelViewMatrix) );
lightPos = lightPos * lightToLocalMatrix;
lightDir = osg::Matrix::transform3x3( lightDir, lightToLocalMatrix );
lightDir.normalize();
OSG_INFO<<" new LightPos ="<<lightPos<<std::endl;
OSG_INFO<<" new LightDir ="<<lightDir<<std::endl;
}
lightPos3.set(lightPos.x()/lightPos.w(), lightPos.y()/lightPos.w(), lightPos.z()/lightPos.w());
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
//
// ShadowData
//
MWShadowTechnique::ShadowData::ShadowData(MWShadowTechnique::ViewDependentData* vdd):
_viewDependentData(vdd),
_textureUnit(0)
{
const ShadowSettings* settings = vdd->getViewDependentShadowMap()->getShadowedScene()->getShadowSettings();
bool debug = settings->getDebugDraw();
// set up the texture
_texture = new osg::Texture2D;
osg::Vec2s textureSize = debug ? osg::Vec2s(512,512) : settings->getTextureSize();
_texture->setTextureSize(textureSize.x(), textureSize.y());
if (debug)
{
_texture->setInternalFormat(GL_RGB);
}
else
{
_texture->setInternalFormat(GL_DEPTH_COMPONENT);
_texture->setShadowComparison(true);
_texture->setShadowTextureMode(osg::Texture2D::LUMINANCE);
}
_texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
_texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
// the shader clips sampled coordinates, so no need for border
_texture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_EDGE);
_texture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_EDGE);
// set up the camera
_camera = new osg::Camera;
_camera->setName("ShadowCamera");
_camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF_INHERIT_VIEWPOINT);
#ifndef __APPLE__ // workaround shadow issue on macOS, https://gitlab.com/OpenMW/openmw/-/issues/6057
_camera->setImplicitBufferAttachmentMask(0, 0);
#endif
//_camera->setClearColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
_camera->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,0.0f));
//_camera->setComputeNearFarMode(osg::Camera::COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES);
//_camera->setComputeNearFarMode(osg::Camera::COMPUTE_NEAR_FAR_USING_PRIMITIVES);
// Now we are using Depth Clamping, we want to not cull things on the wrong side of the near plane.
// When the near and far planes are computed, OSG always culls anything on the wrong side of the near plane, even if it's told not to.
// Even if that weren't an issue, the near plane can't go past any shadow receivers or the depth-clamped fragments which ended up on the near plane can't cast shadows on those receivers.
// Unfortunately, this change will make shadows have less depth precision when there are no casters outside the view frustum.
// TODO: Find a better solution. E.g. detect when there are no casters outside the view frustum, write a new cull visitor that does all the wacky things we'd need it to.
_camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR);
// switch off small feature culling as this can cull out geometry that will still be large enough once perspective correction takes effect.
_camera->setCullingMode(_camera->getCullingMode() & ~osg::CullSettings::SMALL_FEATURE_CULLING);
// set viewport
_camera->setViewport(0,0,textureSize.x(),textureSize.y());
if (debug)
{
// clear just the depth buffer
_camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
// render after the main camera
_camera->setRenderOrder(osg::Camera::POST_RENDER);
// attach the texture and use it as the color buffer.
//_camera->attach(osg::Camera::DEPTH_BUFFER, _texture.get());
_camera->attach(osg::Camera::COLOR_BUFFER, _texture.get());
}
else
{
// clear the depth and colour bufferson each clear.
_camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
// set the camera to render before the main camera.
_camera->setRenderOrder(osg::Camera::PRE_RENDER);
// tell the camera to use OpenGL frame buffer object where supported.
_camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
// attach the texture and use it as the color buffer.
_camera->attach(osg::Camera::DEPTH_BUFFER, _texture.get());
//_camera->attach(osg::Camera::COLOR_BUFFER, _texture.get());
}
}
void MWShadowTechnique::ShadowData::releaseGLObjects(osg::State* state) const
{
OSG_INFO<<"MWShadowTechnique::ShadowData::releaseGLObjects"<<std::endl;
_texture->releaseGLObjects(state);
_camera->releaseGLObjects(state);
}
///////////////////////////////////////////////////////////////////////////////////////////////
//
// Frustum
//
MWShadowTechnique::Frustum::Frustum(osgUtil::CullVisitor* cv, double minZNear, double maxZFar):
useCustomClipSpace(false),
corners(8),
faces(6),
edges(12)
{
projectionMatrix = *(cv->getProjectionMatrix());
modelViewMatrix = *(cv->getModelViewMatrix());
OSG_INFO<<"Projection matrix "<<projectionMatrix<<std::endl;
if (cv->getComputeNearFarMode()!=osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR)
{
osg::Matrix::value_type zNear = osg::maximum<osg::Matrix::value_type>(cv->getCalculatedNearPlane(),minZNear);
osg::Matrix::value_type zFar = osg::minimum<osg::Matrix::value_type>(cv->getCalculatedFarPlane(),maxZFar);
cv->clampProjectionMatrix(projectionMatrix, zNear, zFar);
OSG_INFO<<"zNear = "<<zNear<<", zFar = "<<zFar<<std::endl;
OSG_INFO<<"Projection matrix after clamping "<<projectionMatrix<<std::endl;
}
}
void SceneUtil::MWShadowTechnique::Frustum::setCustomClipSpace(const osg::BoundingBoxd& clipCornersOverride)
{
useCustomClipSpace = true;
customClipSpace = clipCornersOverride;
}
void SceneUtil::MWShadowTechnique::Frustum::init()
{
osg::Matrixd clipToWorld;
clipToWorld.invert(modelViewMatrix * projectionMatrix);
if (useCustomClipSpace)
{
corners.clear();
// Add corners in the same order OSG expects them
for (int i : {0, 1, 5, 4, 2, 3, 7, 6})
{
corners.push_back(customClipSpace.corner(i));
}
}
else
{
corners[0].set(-1.0, -1.0, -1.0);
corners[1].set(1.0, -1.0, -1.0);
corners[2].set(1.0, -1.0, 1.0);
corners[3].set(-1.0, -1.0, 1.0);
corners[4].set(-1.0, 1.0, -1.0);
corners[5].set(1.0, 1.0, -1.0);
corners[6].set(1.0, 1.0, 1.0);
corners[7].set(-1.0, 1.0, 1.0);
}
// transform frustum corners from clipspace to world coords, and compute center
for(Vertices::iterator itr = corners.begin();
itr != corners.end();
++itr)
{
*itr = (*itr) * clipToWorld;
OSG_INFO<<" corner "<<*itr<<std::endl;
}
// compute eye point
eye = osg::Vec3d(0.0,0.0,0.0) * osg::Matrix::inverse(modelViewMatrix);
// compute center and the frustumCenterLine
centerNearPlane = (corners[0]+corners[1]+corners[5]+corners[4])*0.25;
centerFarPlane = (corners[3]+corners[2]+corners[6]+corners[7])*0.25;
center = (centerNearPlane+centerFarPlane)*0.5;
frustumCenterLine = centerFarPlane-centerNearPlane;
frustumCenterLine.normalize();
OSG_INFO<<" center "<<center<<std::endl;
faces[0].push_back(0);
faces[0].push_back(3);
faces[0].push_back(7);
faces[0].push_back(4);
faces[1].push_back(1);
faces[1].push_back(5);
faces[1].push_back(6);
faces[1].push_back(2);
faces[2].push_back(0);
faces[2].push_back(1);
faces[2].push_back(2);
faces[2].push_back(3);
faces[3].push_back(4);
faces[3].push_back(7);
faces[3].push_back(6);
faces[3].push_back(5);
faces[4].push_back(0);
faces[4].push_back(4);
faces[4].push_back(5);
faces[4].push_back(1);
faces[5].push_back(2);
faces[5].push_back(6);
faces[5].push_back(7);
faces[5].push_back(3);
edges[0].push_back(0); edges[0].push_back(1); // corner points on edge
edges[0].push_back(2); edges[0].push_back(4); // faces on edge
edges[1].push_back(1); edges[1].push_back(2); // corner points on edge
edges[1].push_back(2); edges[1].push_back(1); // faces on edge
edges[2].push_back(2); edges[2].push_back(3); // corner points on edge
edges[2].push_back(2); edges[2].push_back(5); // faces on edge
edges[3].push_back(3); edges[3].push_back(0); // corner points on edge
edges[3].push_back(2); edges[3].push_back(0); // faces on edge
edges[4].push_back(0); edges[4].push_back(4); // corner points on edge
edges[4].push_back(0); edges[4].push_back(4); // faces on edge
edges[5].push_back(1); edges[5].push_back(5); // corner points on edge
edges[5].push_back(4); edges[5].push_back(1); // faces on edge
edges[6].push_back(2); edges[6].push_back(6); // corner points on edge
edges[6].push_back(1); edges[6].push_back(5); // faces on edge
edges[7].push_back(3); edges[7].push_back(7); // corner points on edge
edges[7].push_back(5); edges[7].push_back(0); // faces on edge
edges[8].push_back(4); edges[8].push_back(5); // corner points on edge
edges[8].push_back(3); edges[8].push_back(4); // faces on edge
edges[9].push_back(5); edges[9].push_back(6); // corner points on edge
edges[9].push_back(3); edges[9].push_back(1); // faces on edge
edges[10].push_back(6);edges[10].push_back(7); // corner points on edge
edges[10].push_back(3);edges[10].push_back(5); // faces on edge
edges[11].push_back(7); edges[11].push_back(4); // corner points on edge
edges[11].push_back(3); edges[11].push_back(0); // faces on edge
}
///////////////////////////////////////////////////////////////////////////////////////////////
//
// ViewDependentData
//
MWShadowTechnique::ViewDependentData::ViewDependentData(MWShadowTechnique* vdsm):
_viewDependentShadowMap(vdsm)
{
OSG_INFO<<"ViewDependentData::ViewDependentData()"<<this<<std::endl;
for (auto& perFrameStateset : _stateset)
perFrameStateset = new osg::StateSet;
}
void MWShadowTechnique::ViewDependentData::releaseGLObjects(osg::State* state) const
{
for(ShadowDataList::const_iterator itr = _shadowDataList.begin();
itr != _shadowDataList.end();
++itr)
{
(*itr)->releaseGLObjects(state);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
//
// MWShadowTechnique
//
MWShadowTechnique::MWShadowTechnique():
ShadowTechnique(),
_enableShadows(false),
_debugHud(nullptr),
_castingPrograms{ nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr }
{
_shadowRecievingPlaceholderStateSet = new osg::StateSet;
mSetDummyStateWhenDisabled = false;
}
MWShadowTechnique::MWShadowTechnique(const MWShadowTechnique& vdsm, const osg::CopyOp& copyop):
ShadowTechnique(vdsm,copyop)
, _castingPrograms(vdsm._castingPrograms)
{
_shadowRecievingPlaceholderStateSet = new osg::StateSet;
_enableShadows = vdsm._enableShadows;
mSetDummyStateWhenDisabled = vdsm.mSetDummyStateWhenDisabled;
}
MWShadowTechnique::~MWShadowTechnique()
{
if (_shadowsBin != nullptr)
osgUtil::RenderBin::removeRenderBinPrototype(_shadowsBin);
}
void MWShadowTechnique::init()
{
if (!_shadowedScene) return;
OSG_INFO<<"MWShadowTechnique::init()"<<std::endl;
createShaders();
_dirty = false;
}
void MWShadowTechnique::cleanSceneGraph()
{
OSG_INFO<<"MWShadowTechnique::cleanSceneGraph()"<<std::endl;
}
void MWShadowTechnique::enableShadows()
{
_enableShadows = true;
}
void MWShadowTechnique::disableShadows(bool setDummyState)
{
_enableShadows = false;
mSetDummyStateWhenDisabled = setDummyState;
}
void SceneUtil::MWShadowTechnique::enableDebugHUD()
{
_debugHud = new DebugHUD(getShadowedScene()->getShadowSettings()->getNumShadowMapsPerLight());
}
void SceneUtil::MWShadowTechnique::disableDebugHUD()
{
_debugHud = nullptr;
}
void SceneUtil::MWShadowTechnique::setSplitPointUniformLogarithmicRatio(double ratio)
{
_splitPointUniformLogRatio = ratio;
}
void SceneUtil::MWShadowTechnique::setSplitPointDeltaBias(double bias)
{
_splitPointDeltaBias = bias;
}
void SceneUtil::MWShadowTechnique::setPolygonOffset(float factor, float units)
{
_polygonOffsetFactor = factor;
_polygonOffsetUnits = units;
if (_polygonOffset)
{
_polygonOffset->setFactor(factor);
_polygonOffset->setUnits(units);
}
}
void SceneUtil::MWShadowTechnique::setShadowFadeStart(float shadowFadeStart)
{
_shadowFadeStart = shadowFadeStart;
}
void SceneUtil::MWShadowTechnique::enableFrontFaceCulling()
{
_useFrontFaceCulling = true;
if (_shadowCastingStateSet)
{
_shadowCastingStateSet->setAttribute(new osg::CullFace(osg::CullFace::FRONT), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
_shadowCastingStateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
}
}
void SceneUtil::MWShadowTechnique::disableFrontFaceCulling()
{
_useFrontFaceCulling = false;
if (_shadowCastingStateSet)
{
_shadowCastingStateSet->removeAttribute(osg::StateAttribute::CULLFACE);
_shadowCastingStateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
}
}
void SceneUtil::MWShadowTechnique::setupCastingShader(Shader::ShaderManager & shaderManager)
{
// This can't be part of the constructor as OSG mandates that there be a trivial constructor available
osg::ref_ptr<osg::Shader> castingVertexShader = shaderManager.getShader("shadowcasting.vert");
std::string useGPUShader4 = SceneUtil::getGLExtensions().isGpuShader4Supported ? "1" : "0";
for (int alphaFunc = GL_NEVER; alphaFunc <= GL_ALWAYS; ++alphaFunc)
{
auto& program = _castingPrograms[alphaFunc - GL_NEVER];
program = new osg::Program();
program->addShader(castingVertexShader);
program->addShader(shaderManager.getShader("shadowcasting.frag", { {"alphaFunc", std::to_string(alphaFunc)},
{"alphaToCoverage", "0"},
{"adjustCoverage", "1"},
{"useGPUShader4", useGPUShader4}
}));
}
}
MWShadowTechnique::ViewDependentData* MWShadowTechnique::createViewDependentData(osgUtil::CullVisitor* /*cv*/)
{
return new ViewDependentData(this);
}
MWShadowTechnique::ViewDependentData* MWShadowTechnique::getViewDependentData(osgUtil::CullVisitor* cv)
{
std::lock_guard<std::mutex> lock(_viewDependentDataMapMutex);
ViewDependentDataMap::iterator itr = _viewDependentDataMap.find(cv);
if (itr!=_viewDependentDataMap.end()) return itr->second.get();
osg::ref_ptr<ViewDependentData> vdd = createViewDependentData(cv);
_viewDependentDataMap[cv] = vdd;
return vdd.release();
}
void SceneUtil::MWShadowTechnique::copyShadowMap(osgUtil::CullVisitor& cv, ViewDependentData* lhs, ViewDependentData* rhs)
{
// Prepare for rendering shadows using the shadow map owned by rhs.
// To achieve this i first copy all data that is not specific to this cv's camera and thus read-only,
// trusting openmw and osg won't overwrite that data before this frame is done rendering.
// This works due to the double buffering of CullVisitors by osg, but also requires that cull passes are serialized (relative to one another).
// Then initialize new copies of the data that will be written with view-specific data
// (the stateset and the texgens).
lhs->_viewDependentShadowMap = rhs->_viewDependentShadowMap;
auto* stateset = lhs->getStateSet(cv.getTraversalNumber());
stateset->clear();
lhs->_lightDataList = rhs->_lightDataList;
lhs->_numValidShadows = rhs->_numValidShadows;
ShadowDataList& sdl = lhs->getShadowDataList();
ShadowDataList previous_sdl;
previous_sdl.swap(sdl);
for (const auto& rhs_sd : rhs->getShadowDataList())
{
osg::ref_ptr<ShadowData> lhs_sd;
if (previous_sdl.empty())
{
OSG_INFO << "Create new ShadowData" << std::endl;
lhs_sd = new ShadowData(lhs);
}
else
{
OSG_INFO << "Taking ShadowData from from of previous_sdl" << std::endl;
lhs_sd = previous_sdl.front();
previous_sdl.erase(previous_sdl.begin());
}
lhs_sd->_camera = rhs_sd->_camera;
lhs_sd->_textureUnit = rhs_sd->_textureUnit;
lhs_sd->_texture = rhs_sd->_texture;
lhs_sd->_sm_i = rhs_sd->_sm_i;
sdl.push_back(lhs_sd);
}
copyShadowStateSettings(cv, lhs);
if (lhs->_numValidShadows > 0)
{
prepareStateSetForRenderingShadow(*lhs, cv.getTraversalNumber());
}
}
void SceneUtil::MWShadowTechnique::setCustomFrustumCallback(CustomFrustumCallback* cfc)
{
_customFrustumCallback = cfc;
}
void SceneUtil::MWShadowTechnique::copyShadowStateSettings(osgUtil::CullVisitor& cv, ViewDependentData* vdd)
{
for (const auto& sd : vdd->getShadowDataList())
{
assignValidRegionSettings(cv, sd->_camera, sd->_sm_i, vdd->_uniforms[cv.getTraversalNumber()%2]);
assignShadowStateSettings(cv, sd->_camera, sd->_sm_i, vdd->_uniforms[cv.getTraversalNumber()%2]);
}
}
void MWShadowTechnique::update(osg::NodeVisitor& nv)
{
OSG_INFO<<"MWShadowTechnique::update(osg::NodeVisitor& "<<&nv<<")"<<std::endl;
_shadowedScene->osg::Group::traverse(nv);
}
void MWShadowTechnique::cull(osgUtil::CullVisitor& cv)
{
if (!_enableShadows)
{
if (mSetDummyStateWhenDisabled)
{
osg::ref_ptr<osg::StateSet> dummyState = new osg::StateSet();
ShadowSettings* settings = getShadowedScene()->getShadowSettings();
int baseUnit = settings->getBaseShadowTextureUnit();
int endUnit = baseUnit + settings->getNumShadowMapsPerLight();
for (int i = baseUnit; i < endUnit; ++i)
{
dummyState->setTextureAttribute(i, _fallbackShadowMapTexture, osg::StateAttribute::ON);
dummyState->addUniform(new osg::Uniform(("shadowTexture" + std::to_string(i - baseUnit)).c_str(), i));
}
cv.pushStateSet(dummyState);
}
_shadowedScene->osg::Group::traverse(cv);
if (mSetDummyStateWhenDisabled)
cv.popStateSet();
return;
}
OSG_INFO<<std::endl<<std::endl<<"MWShadowTechnique::cull(osg::CullVisitor&"<<&cv<<")"<<std::endl;
if (!_shadowCastingStateSet)
{
OSG_INFO<<"Warning, init() has not yet been called so ShadowCastingStateSet has not been setup yet, unable to create shadows."<<std::endl;
_shadowedScene->osg::Group::traverse(cv);
return;
}
ViewDependentData* vdd = getViewDependentData(&cv);
if (!vdd)
{
OSG_INFO<<"Warning, now ViewDependentData created, unable to create shadows."<<std::endl;
_shadowedScene->osg::Group::traverse(cv);
return;
}
Uniforms& vddUniforms = vdd->_uniforms[cv.getTraversalNumber() % 2];
ShadowSettings* settings = getShadowedScene()->getShadowSettings();
OSG_INFO<<"cv->getProjectionMatrix()="<<*cv.getProjectionMatrix()<<std::endl;
osg::CullSettings::ComputeNearFarMode cachedNearFarMode = cv.getComputeNearFarMode();
osg::RefMatrix& viewProjectionMatrix = *cv.getProjectionMatrix();
// check whether this main views projection is perspective or orthographic
bool orthographicViewFrustum = viewProjectionMatrix(0,3)==0.0 &&
viewProjectionMatrix(1,3)==0.0 &&
viewProjectionMatrix(2,3)==0.0;
double minZNear = 0.0;
double maxZFar = dbl_max;
if (cachedNearFarMode==osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR)
{
double left, right, top, bottom;
if (orthographicViewFrustum)
{
viewProjectionMatrix.getOrtho(left, right, bottom, top, minZNear, maxZFar);
}
else
{
viewProjectionMatrix.getFrustum(left, right, bottom, top, minZNear, maxZFar);
}
OSG_INFO<<"minZNear="<<minZNear<<", maxZFar="<<maxZFar<<std::endl;
}
// set the compute near/far mode to the highest quality setting to ensure we push the near plan out as far as possible
if (settings->getComputeNearFarModeOverride()!=osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR)
{
cv.setComputeNearFarMode(settings->getComputeNearFarModeOverride());
}
// 1. Traverse main scene graph
auto* shadowReceiverStateSet = vdd->getStateSet(cv.getTraversalNumber());
shadowReceiverStateSet->clear();
cv.pushStateSet(shadowReceiverStateSet);
cullShadowReceivingScene(&cv);
cv.popStateSet();
if (cv.getComputeNearFarMode()!=osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR)
{
OSG_INFO<<"Just done main subgraph traversak"<<std::endl;
// make sure that the near plane is computed correctly so that any projection matrix computations
// are all done correctly.
cv.computeNearPlane();
}
// clamp the minZNear and maxZFar to those provided by ShadowSettings
maxZFar = osg::minimum(settings->getMaximumShadowMapDistance(),maxZFar);
if (minZNear>maxZFar) minZNear = maxZFar*settings->getMinimumShadowMapNearFarRatio();
//OSG_NOTICE<<"maxZFar "<<maxZFar<<std::endl;
// Workaround for absurdly huge viewing distances where OSG would otherwise push the near plane out.
cv.setNearFarRatio(minZNear / maxZFar);
Frustum frustum(&cv, minZNear, maxZFar);
if (_customFrustumCallback)
{
OSG_INFO << "Calling custom frustum callback" << std::endl;
osgUtil::CullVisitor* sharedFrustumHint = nullptr;
_customClipSpace.init();
_customFrustumCallback->operator()(cv, _customClipSpace, sharedFrustumHint);
frustum.setCustomClipSpace(_customClipSpace);
if (sharedFrustumHint)
{
// user hinted another view shares its frustum
std::lock_guard<std::mutex> lock(_viewDependentDataMapMutex);
auto itr = _viewDependentDataMap.find(sharedFrustumHint);
if (itr != _viewDependentDataMap.end())
{
OSG_INFO << "User provided a valid shared frustum hint, re-using previously generated shadow map" << std::endl;
copyShadowMap(cv, vdd, itr->second);
// return compute near far mode back to it's original settings
cv.setComputeNearFarMode(cachedNearFarMode);
return;
}
else
{
OSG_INFO << "User provided a shared frustum hint, but it was not valid." << std::endl;
}
}
}
frustum.init();
if (_debugHud)
{
osg::ref_ptr<osg::Vec3Array> vertexArray = new osg::Vec3Array();
for (osg::Vec3d &vertex : frustum.corners)
vertexArray->push_back((osg::Vec3)vertex);
_debugHud->setFrustumVertices(vertexArray, cv.getTraversalNumber());
}
double reducedNear, reducedFar;
if (cv.getComputeNearFarMode() != osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR)
{
reducedNear = osg::maximum<double>(cv.getCalculatedNearPlane(), minZNear);
reducedFar = osg::minimum<double>(cv.getCalculatedFarPlane(), maxZFar);
}
else
{
reducedNear = minZNear;
reducedFar = maxZFar;
}
// return compute near far mode back to it's original settings
cv.setComputeNearFarMode(cachedNearFarMode);
OSG_INFO<<"frustum.eye="<<frustum.eye<<", frustum.centerNearPlane, "<<frustum.centerNearPlane<<" distance = "<<(frustum.eye-frustum.centerNearPlane).length()<<std::endl;
// 2. select active light sources
// create a list of light sources + their matrices to place them
selectActiveLights(&cv, vdd);
unsigned int pos_x = 0;
unsigned int textureUnit = settings->getBaseShadowTextureUnit();
unsigned int numValidShadows = 0;
ShadowDataList& sdl = vdd->getShadowDataList();
ShadowDataList previous_sdl;
previous_sdl.swap(sdl);
unsigned int numShadowMapsPerLight = settings->getNumShadowMapsPerLight();
LightDataList& pll = vdd->getLightDataList();
for(LightDataList::iterator itr = pll.begin();
itr != pll.end();
++itr)
{
// 3. create per light/per shadow map division of lightspace/frustum
// create a list of light/shadow map data structures
LightData& pl = **itr;
// 3.1 compute light space polytope
//
osg::Polytope polytope = computeLightViewFrustumPolytope(frustum, pl);
// if polytope is empty then no rendering.
if (polytope.empty())
{
OSG_NOTICE<<"Polytope empty no shadow to render"<<std::endl;
continue;
}
// 3.2 compute RTT camera view+projection matrix settings
//
osg::Matrixd projectionMatrix;
osg::Matrixd viewMatrix;
if (!computeShadowCameraSettings(frustum, pl, projectionMatrix, viewMatrix))
{
OSG_NOTICE<<"No valid Camera settings, no shadow to render"<<std::endl;
continue;
}
// if we are using multiple shadow maps and CastShadowTraversalMask is being used
// traverse the scene to compute the extents of the objects
if (/*numShadowMapsPerLight>1 &&*/ (_shadowedScene->getCastsShadowTraversalMask() & _worldMask) == 0)
{
// osg::ElapsedTime timer;
osg::ref_ptr<osg::Viewport> viewport = new osg::Viewport(0,0,2048,2048);
if (!_clsb) _clsb = new ComputeLightSpaceBounds;
ComputeLightSpaceBounds& clsb = *_clsb;
clsb.reset();
clsb.pushViewport(viewport);
clsb.pushProjectionMatrix(new osg::RefMatrix(projectionMatrix));
clsb.pushModelViewMatrix(new osg::RefMatrix(viewMatrix), osg::Transform::ABSOLUTE_RF);
clsb.setTraversalMask(_shadowedScene->getCastsShadowTraversalMask());
osg::Matrixd invertModelView;
invertModelView.invert(viewMatrix);
osg::Polytope local_polytope(polytope);
local_polytope.transformProvidingInverse(invertModelView);
osg::CullingSet& cs = clsb.getProjectionCullingStack().back();
cs.setFrustum(local_polytope);
clsb.pushCullingSet();
_shadowedScene->accept(clsb);
clsb.popCullingSet();
clsb.popModelViewMatrix();
clsb.popProjectionMatrix();
clsb.popViewport();
// OSG_NOTICE<<"Extents of LightSpace "<<clsb._bb.xMin()<<", "<<clsb._bb.xMax()<<", "<<clsb._bb.yMin()<<", "<<clsb._bb.yMax()<<", "<<clsb._bb.zMin()<<", "<<clsb._bb.zMax()<<std::endl;
// OSG_NOTICE<<" time "<<timer.elapsedTime_m()<<"ms, mask = "<<std::hex<<_shadowedScene->getCastsShadowTraversalMask()<<std::endl;
if (clsb._bb.xMin()>-1.0f || clsb._bb.xMax()<1.0f || clsb._bb.yMin()>-1.0f || clsb._bb.yMax()<1.0f)
{
// OSG_NOTICE<<"Need to clamp projection matrix"<<std::endl;
#if 1
double xMid = (clsb._bb.xMin()+clsb._bb.xMax())*0.5f;
double xRange = clsb._bb.xMax()-clsb._bb.xMin();
#else
double xMid = 0.0;
double xRange = 2.0;
#endif
double yMid = (clsb._bb.yMin()+clsb._bb.yMax())*0.5f;
double yRange = (clsb._bb.yMax()-clsb._bb.yMin());
osg::Matrixd cornerConverter = osg::Matrixd::inverse(projectionMatrix) * osg::Matrixd::inverse(viewMatrix) * *cv.getModelViewMatrix();
double minZ = dbl_max;
double maxZ = -dbl_max;
clsb._bb._max[2] = 1.0;
for (unsigned int i = 0; i < 8; i++)
{
osg::Vec3 corner = clsb._bb.corner(i);
corner = corner * cornerConverter;
maxZ = osg::maximum<double>(maxZ, -corner.z());
minZ = osg::minimum<double>(minZ, -corner.z());
}
reducedNear = osg::maximum<double>(reducedNear, minZ);
reducedFar = osg::minimum<double>(reducedFar, maxZ);
// OSG_NOTICE<<" xMid="<<xMid<<", yMid="<<yMid<<", xRange="<<xRange<<", yRange="<<yRange<<std::endl;
projectionMatrix =
projectionMatrix *
osg::Matrixd::translate(osg::Vec3d(-xMid,-yMid,0.0)) *
osg::Matrixd::scale(osg::Vec3d(2.0/xRange, 2.0/yRange,1.0));
}
}
#if 0
double splitPoint = 0.0;
if (numShadowMapsPerLight>1)
{
osg::Vec3d eye_v = frustum.eye * viewMatrix;
osg::Vec3d center_v = frustum.center * viewMatrix;
osg::Vec3d viewdir_v = center_v-eye_v; viewdir_v.normalize();
osg::Vec3d lightdir(0.0,0.0,-1.0);
double dotProduct_v = lightdir * viewdir_v;
double angle = acosf(dotProduct_v);
osg::Vec3d eye_ls = eye_v * projectionMatrix;
OSG_INFO<<"Angle between view vector and eye "<<osg::RadiansToDegrees(angle)<<std::endl;
OSG_INFO<<"eye_ls="<<eye_ls<<std::endl;
if (eye_ls.y()>=-1.0 && eye_ls.y()<=1.0)
{
OSG_INFO<<"Eye point inside light space clip region "<<std::endl;
splitPoint = 0.0;
}
else
{
double n = -1.0-eye_ls.y();
double f = 1.0-eye_ls.y();
double sqrt_nf = sqrt(n*f);
double mid = eye_ls.y()+sqrt_nf;
double ratioOfMidToUseForSplit = 0.8;
splitPoint = mid * ratioOfMidToUseForSplit;
OSG_INFO<<" n="<<n<<", f="<<f<<", sqrt_nf="<<sqrt_nf<<" mid="<<mid<<std::endl;
}
}
#endif
// 4. For each light/shadow map
for (unsigned int sm_i=0; sm_i<numShadowMapsPerLight; ++sm_i)
{
osg::ref_ptr<ShadowData> sd;
if (previous_sdl.empty())
{
OSG_INFO<<"Create new ShadowData"<<std::endl;
sd = new ShadowData(vdd);
}
else
{
OSG_INFO<<"Taking ShadowData from from of previous_sdl"<<std::endl;
sd = previous_sdl.front();
previous_sdl.erase(previous_sdl.begin());
}
osg::ref_ptr<osg::Camera> camera = sd->_camera;
camera->setProjectionMatrix(projectionMatrix);
camera->setViewMatrix(viewMatrix);
if (settings->getDebugDraw())
{
camera->getViewport()->x() = pos_x;
pos_x += static_cast<unsigned int>(camera->getViewport()->width()) + 40;
}
// transform polytope in model coords into light spaces eye coords.
osg::Matrixd invertModelView;
invertModelView.invert(camera->getViewMatrix());
osg::Polytope local_polytope(polytope);
local_polytope.transformProvidingInverse(invertModelView);
double cascaseNear = reducedNear;
double cascadeFar = reducedFar;
if (numShadowMapsPerLight>1)
{
// compute the start and end range in non-dimensional coords
#if 0
double r_start = (sm_i==0) ? -1.0 : (double(sm_i)/double(numShadowMapsPerLight)*2.0-1.0);
double r_end = (sm_i+1==numShadowMapsPerLight) ? 1.0 : (double(sm_i+1)/double(numShadowMapsPerLight)*2.0-1.0);
#elif 0
// hardwired for 2 splits
double r_start = (sm_i==0) ? -1.0 : splitPoint;
double r_end = (sm_i+1==numShadowMapsPerLight) ? 1.0 : splitPoint;
#else
double r_start, r_end;
// split system based on the original Parallel Split Shadow Maps paper.
double n = reducedNear;
double f = reducedFar;
double i = double(sm_i);
double m = double(numShadowMapsPerLight);
if (sm_i == 0)
r_start = -1.0;
else
{
// compute the split point in main camera view
double ciLog = n * pow(f / n, i / m);
double ciUniform = n + (f - n) * i / m;
double ci = _splitPointUniformLogRatio * ciLog + (1.0 - _splitPointUniformLogRatio) * ciUniform + _splitPointDeltaBias;
cascaseNear = ci;
// work out where this is in light space
osg::Vec3d worldSpacePos = frustum.eye + frustum.frustumCenterLine * ci;
osg::Vec3d lightSpacePos = worldSpacePos * viewMatrix * projectionMatrix;
r_start = lightSpacePos.y();
}
if (sm_i + 1 == numShadowMapsPerLight)
r_end = 1.0;
else
{
// compute the split point in main camera view
double ciLog = n * pow(f / n, (i + 1) / m);
double ciUniform = n + (f - n) * (i + 1) / m;
double ci = _splitPointUniformLogRatio * ciLog + (1.0 - _splitPointUniformLogRatio) * ciUniform + _splitPointDeltaBias;
cascadeFar = ci;
// work out where this is in light space
osg::Vec3d worldSpacePos = frustum.eye + frustum.frustumCenterLine * ci;
osg::Vec3d lightSpacePos = worldSpacePos * viewMatrix * projectionMatrix;
r_end = lightSpacePos.y();
}
#endif
// for all by the last shadowmap shift the r_end so that it overlaps slightly with the next shadowmap
// to prevent a seam showing through between the shadowmaps
if (sm_i+1<numShadowMapsPerLight) r_end+=0.01;
// We can't add these clipping planes with cascaded shadow maps as they wouldn't be parallel to the light direction.
if (settings->getMultipleShadowMapHint() == ShadowSettings::PARALLEL_SPLIT && sm_i>0)
{
// not the first shadowmap so insert a polytope to clip the scene from before r_start
// plane in clip space coords
osg::Plane plane(0.0,1.0,0.0,-r_start);
// transform into eye coords
plane.transformProvidingInverse(projectionMatrix);
local_polytope.getPlaneList().push_back(plane);
//OSG_NOTICE<<"Adding r_start plane "<<plane<<std::endl;
}
if (settings->getMultipleShadowMapHint() == ShadowSettings::PARALLEL_SPLIT && sm_i+1<numShadowMapsPerLight)
{
// not the last shadowmap so insert a polytope to clip the scene from beyond r_end
// plane in clip space coords
osg::Plane plane(0.0,-1.0,0.0,r_end);
// transform into eye coords
plane.transformProvidingInverse(projectionMatrix);
local_polytope.getPlaneList().push_back(plane);
//OSG_NOTICE<<"Adding r_end plane "<<plane<<std::endl;
}
local_polytope.setupMask();
if (settings->getMultipleShadowMapHint() == ShadowSettings::PARALLEL_SPLIT)
{
// OSG_NOTICE<<"Need to adjust RTT camera projection and view matrix here, r_start="<<r_start<<", r_end="<<r_end<<std::endl;
// OSG_NOTICE<<" textureUnit = "<<textureUnit<<std::endl;
double mid_r = (r_start + r_end)*0.5;
double range_r = (r_end - r_start);
// OSG_NOTICE<<" mid_r = "<<mid_r<<", range_r = "<<range_r<<std::endl;
camera->setProjectionMatrix(
camera->getProjectionMatrix() *
osg::Matrixd::translate(osg::Vec3d(0.0,-mid_r,0.0)) *
osg::Matrixd::scale(osg::Vec3d(1.0,2.0/range_r,1.0)));
}
}
std::vector<osg::Plane> extraPlanes;
if (settings->getMultipleShadowMapHint() == ShadowSettings::CASCADED)
{
cropShadowCameraToMainFrustum(frustum, camera, cascaseNear, cascadeFar, extraPlanes);
for (const auto& plane : extraPlanes)
local_polytope.getPlaneList().push_back(plane);
local_polytope.setupMask();
}
else
cropShadowCameraToMainFrustum(frustum, camera, reducedNear, reducedFar, extraPlanes);
osg::ref_ptr<VDSMCameraCullCallback> vdsmCallback = new VDSMCameraCullCallback(this, local_polytope);
camera->setCullCallback(vdsmCallback.get());
// 4.3 traverse RTT camera
//
cv.pushStateSet(_shadowCastingStateSet.get());
cullShadowCastingScene(&cv, camera.get());
cv.popStateSet();
if (!orthographicViewFrustum && settings->getShadowMapProjectionHint()==ShadowSettings::PERSPECTIVE_SHADOW_MAP)
{
assignValidRegionSettings(cv, camera, sm_i, vddUniforms);
if (settings->getMultipleShadowMapHint() == ShadowSettings::CASCADED)
adjustPerspectiveShadowMapCameraSettings(vdsmCallback->getRenderStage(), frustum, pl, camera.get(), cascaseNear, cascadeFar);
else
adjustPerspectiveShadowMapCameraSettings(vdsmCallback->getRenderStage(), frustum, pl, camera.get(), reducedNear, reducedFar);
if (vdsmCallback->getProjectionMatrix())
{
vdsmCallback->getProjectionMatrix()->set(camera->getProjectionMatrix());
}
}
// 4.4 compute main scene graph TexGen + uniform settings + setup state
//
{
assignShadowStateSettings(cv, camera, sm_i, vddUniforms);
}
// mark the light as one that has active shadows and requires shaders
pl.textureUnits.push_back(textureUnit);
// pass on shadow data to ShadowDataList
sd->_textureUnit = textureUnit;
sd->_sm_i = sm_i;
sdl.push_back(sd);
// increment counters.
++textureUnit;
++numValidShadows ;
if (_debugHud)
_debugHud->draw(sd->_texture, sm_i, camera->getViewMatrix() * camera->getProjectionMatrix(), cv);
}
}
vdd->setNumValidShadows(numValidShadows);
if (numValidShadows>0)
{
prepareStateSetForRenderingShadow(*vdd, cv.getTraversalNumber());
}
// OSG_NOTICE<<"End of shadow setup Projection matrix "<<*cv.getProjectionMatrix()<<std::endl;
}
bool MWShadowTechnique::selectActiveLights(osgUtil::CullVisitor* cv, ViewDependentData* vdd) const
{
OSG_INFO<<"selectActiveLights"<<std::endl;
LightDataList& pll = vdd->getLightDataList();
LightDataList previous_ldl;
previous_ldl.swap(pll);
//MR testing giving a specific light
osgUtil::RenderStage * rs = cv->getCurrentRenderBin()->getStage();
OSG_INFO<<"selectActiveLights osgUtil::RenderStage="<<rs<<std::endl;
osg::Matrixd modelViewMatrix = *(cv->getModelViewMatrix());
osgUtil::PositionalStateContainer::AttrMatrixList& aml =
rs->getPositionalStateContainer()->getAttrMatrixList();
const ShadowSettings* settings = getShadowedScene()->getShadowSettings();
for(osgUtil::PositionalStateContainer::AttrMatrixList::reverse_iterator itr = aml.rbegin();
itr != aml.rend();
++itr)
{
const osg::Light* light = dynamic_cast<const osg::Light*>(itr->first.get());
if (light && light->getLightNum() >= 0)
{
// is LightNum matched to that defined in settings
if (settings && settings->getLightNum()>=0 && light->getLightNum()!=settings->getLightNum()) continue;
LightDataList::iterator pll_itr = pll.begin();
for(; pll_itr != pll.end(); ++pll_itr)
{
if ((*pll_itr)->light->getLightNum()==light->getLightNum()) break;
}
if (pll_itr==pll.end())
{
OSG_INFO<<"Light num "<<light->getLightNum()<<std::endl;
LightData* ld = new LightData(vdd);
ld->setLightData(itr->second.get(), light, modelViewMatrix);
pll.push_back(ld);
}
else
{
OSG_INFO<<"Light num "<<light->getLightNum()<<" already used, ignore light"<<std::endl;
}
}
}
return !pll.empty();
}
void MWShadowTechnique::createShaders()
{
OSG_INFO<<"MWShadowTechnique::createShaders()"<<std::endl;
unsigned int _baseTextureUnit = 0;
_shadowCastingStateSet = new osg::StateSet;
ShadowSettings* settings = getShadowedScene()->getShadowSettings();
if (!settings->getDebugDraw())
{
// note soft (attribute only no mode override) setting. When this works ?
// 1. for objects prepared for backface culling
// because they usually also set CullFace and CullMode on in their state
// For them we override CullFace but CullMode remains set by them
// 2. For one faced, trees, and similar objects which cannot use
// backface nor front face so they usually use CullMode off set here.
// In this case we will draw them in their entirety.
if (_useFrontFaceCulling)
{
_shadowCastingStateSet->setAttribute(new osg::CullFace(osg::CullFace::FRONT), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
// make sure GL_CULL_FACE is off by default
// we assume that if object has cull face attribute set to back
// it will also set cull face mode ON so no need for override
_shadowCastingStateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
}
else
_shadowCastingStateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
}
_polygonOffset = new osg::PolygonOffset(_polygonOffsetFactor, _polygonOffsetUnits);
_shadowCastingStateSet->setAttribute(_polygonOffset.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
_shadowCastingStateSet->setMode(GL_POLYGON_OFFSET_FILL, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
osg::ref_ptr<osg::Uniform> baseTextureSampler = new osg::Uniform("baseTexture",(int)_baseTextureUnit);
osg::ref_ptr<osg::Uniform> baseTextureUnit = new osg::Uniform("baseTextureUnit",(int)_baseTextureUnit);
osg::ref_ptr<osg::Uniform> maxDistance = new osg::Uniform("maximumShadowMapDistance", (float)settings->getMaximumShadowMapDistance());
osg::ref_ptr<osg::Uniform> fadeStart = new osg::Uniform("shadowFadeStart", (float)_shadowFadeStart);
for (auto& perFrameUniformList : _uniforms)
{
perFrameUniformList.clear();
perFrameUniformList.push_back(baseTextureSampler);
perFrameUniformList.emplace_back(baseTextureUnit.get());
perFrameUniformList.push_back(maxDistance);
perFrameUniformList.push_back(fadeStart);
}
for(unsigned int sm_i=0; sm_i<settings->getNumShadowMapsPerLight(); ++sm_i)
{
{
std::stringstream sstr;
sstr<<"shadowTexture"<<sm_i;
osg::ref_ptr<osg::Uniform> shadowTextureSampler = new osg::Uniform(sstr.str().c_str(),(int)(settings->getBaseShadowTextureUnit()+sm_i));
for (auto& perFrameUniformList : _uniforms)
perFrameUniformList.emplace_back(shadowTextureSampler.get());
}
}
switch(settings->getShaderHint())
{
case(ShadowSettings::NO_SHADERS):
{
OSG_INFO<<"No shaders provided by, user must supply own shaders"<<std::endl;
break;
}
case(ShadowSettings::PROVIDE_VERTEX_AND_FRAGMENT_SHADER):
case(ShadowSettings::PROVIDE_FRAGMENT_SHADER):
{
_program = new osg::Program;
//osg::ref_ptr<osg::Shader> fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource_noBaseTexture);
if (settings->getNumShadowMapsPerLight()==2)
{
_program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource_withBaseTexture_twoShadowMaps));
}
else
{
_program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource_withBaseTexture));
}
break;
}
}
{
osg::ref_ptr<osg::Image> image = new osg::Image;
image->allocateImage( 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE );
*(osg::Vec4ub*)image->data() = osg::Vec4ub( 0xFF, 0xFF, 0xFF, 0xFF );
_fallbackBaseTexture = new osg::Texture2D(image.get());
_fallbackBaseTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::REPEAT);
_fallbackBaseTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::REPEAT);
_fallbackBaseTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::NEAREST);
_fallbackBaseTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::NEAREST);
_fallbackShadowMapTexture = new osg::Texture2D(image.get());
_fallbackShadowMapTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::REPEAT);
_fallbackShadowMapTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::REPEAT);
_fallbackShadowMapTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::NEAREST);
_fallbackShadowMapTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::NEAREST);
_fallbackShadowMapTexture->setShadowComparison(true);
_fallbackShadowMapTexture->setShadowCompareFunc(osg::Texture::ShadowCompareFunc::ALWAYS);
}
if (!_castingPrograms[GL_ALWAYS - GL_NEVER])
OSG_NOTICE << "Shadow casting shader has not been set up. Remember to call setupCastingShader(Shader::ShaderManager &)" << std::endl;
// Always use the GL_ALWAYS shader as the shadows bin will change it if necessary
_shadowCastingStateSet->setAttributeAndModes(_castingPrograms[GL_ALWAYS - GL_NEVER], osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
// The casting program uses a sampler, so to avoid undefined behaviour, we must bind a dummy texture in case no other is supplied
_shadowCastingStateSet->setTextureAttribute(0, _fallbackBaseTexture.get(), osg::StateAttribute::ON);
_shadowCastingStateSet->addUniform(new osg::Uniform("useDiffuseMapForShadowAlpha", true));
_shadowCastingStateSet->addUniform(new osg::Uniform("alphaTestShadows", false));
osg::ref_ptr<osg::Depth> depth = new osg::Depth;
depth->setWriteMask(true);
osg::ref_ptr<osg::ClipControl> clipcontrol = new osg::ClipControl(osg::ClipControl::LOWER_LEFT, osg::ClipControl::NEGATIVE_ONE_TO_ONE);
_shadowCastingStateSet->setAttribute(clipcontrol, osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
_shadowCastingStateSet->setAttribute(depth, osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
_shadowCastingStateSet->setMode(GL_DEPTH_CLAMP, osg::StateAttribute::ON);
// TODO: compare performance when alpha testing is handled here versus using a discard in the fragment shader
}
osg::Polytope MWShadowTechnique::computeLightViewFrustumPolytope(Frustum& frustum, LightData& positionedLight)
{
OSG_INFO<<"computeLightViewFrustumPolytope()"<<std::endl;
osg::Polytope polytope;
if (frustum.useCustomClipSpace)
{
polytope.setToBoundingBox(frustum.customClipSpace);
}
else
{
polytope.setToUnitFrustum();
}
polytope.transformProvidingInverse( frustum.projectionMatrix );
polytope.transformProvidingInverse( frustum.modelViewMatrix );
osg::Polytope lightVolumePolytope;
if (positionedLight.directionalLight)
{
osg::Polytope::PlaneList& planes = polytope.getPlaneList();
osg::Polytope::ClippingMask selector_mask = 0x1;
osg::Polytope::ClippingMask result_mask = 0x0;
for(unsigned int i=0; i<planes.size(); ++i, selector_mask <<= 1)
{
OSG_INFO<<" plane "<<planes[i]<<" planes["<<i<<"].dotProductNormal(lightDir)="<<planes[i].dotProductNormal(positionedLight.lightDir);
if (planes[i].dotProductNormal(positionedLight.lightDir)>=0.0)
{
OSG_INFO<<" Need remove side "<<i<<std::endl;
}
else
{
OSG_INFO<<std::endl;
lightVolumePolytope.add(planes[i]);
result_mask = result_mask | selector_mask;
}
}
OSG_INFO<<" planes.size() = "<<planes.size()<<std::endl;
OSG_INFO<<" planes.getResultMask() = "<<polytope.getResultMask()<<std::endl;
OSG_INFO<<" resultMask = "<<result_mask<<std::endl;
polytope.setResultMask(result_mask);
}
else
{
const osg::Polytope::PlaneList& planes = polytope.getPlaneList();
osg::Polytope::ClippingMask selector_mask = 0x1;
osg::Polytope::ClippingMask result_mask = 0x0;
for(unsigned int i=0; i<planes.size(); ++i, selector_mask <<= 1)
{
double d = planes[i].distance(positionedLight.lightPos3);
OSG_INFO<<" plane "<<planes[i]<<" planes["<<i<<"].distance(lightPos3)="<<d;
if (d<0.0)
{
OSG_INFO<<" Need remove side "<<i<<std::endl;
}
else
{
OSG_INFO<<std::endl;
lightVolumePolytope.add(planes[i]);
result_mask = result_mask | selector_mask;
}
}
OSG_INFO<<" planes.size() = "<<planes.size()<<std::endl;
OSG_INFO<<" planes.getResultMask() = "<<polytope.getResultMask()<<std::endl;
OSG_INFO<<" resultMask = "<<result_mask<<std::endl;
polytope.setResultMask(result_mask);
}
OSG_INFO<<"Which frustum edges are active?"<<std::endl;
for(unsigned int i=0; i<12; ++i)
{
Frustum::Indices& indices = frustum.edges[i];
unsigned int corner_a = indices[0];
unsigned int corner_b = indices[1];
unsigned int face_a = indices[2];
unsigned int face_b = indices[3];
bool face_a_active = (polytope.getResultMask()&(0x1<<face_a))!=0;
bool face_b_active = (polytope.getResultMask()&(0x1<<face_b))!=0;
unsigned int numActive = 0;
if (face_a_active) ++numActive;
if (face_b_active) ++numActive;
if (numActive==1)
{
osg::Plane boundaryPlane;
if (positionedLight.directionalLight)
{
osg::Vec3d normal = (frustum.corners[corner_b]-frustum.corners[corner_a])^positionedLight.lightDir;
normal.normalize();
boundaryPlane.set(normal, frustum.corners[corner_a]);
}
else
{
boundaryPlane.set(positionedLight.lightPos3, frustum.corners[corner_a], frustum.corners[corner_b]);
}
OSG_INFO<<"Boundary Edge "<<i<<", corner_a="<<corner_a<<", corner_b="<<corner_b<<", face_a_active="<<face_a_active<<", face_b_active="<<face_b_active;
if (boundaryPlane.distance(frustum.center)<0.0)
{
boundaryPlane.flip();
OSG_INFO<<", flipped boundary edge "<<boundaryPlane<<std::endl;
}
else
{
OSG_INFO<<", no need to flip boundary edge "<<boundaryPlane<<std::endl;
}
lightVolumePolytope.add(boundaryPlane);
}
else OSG_INFO<<"Internal Edge "<<i<<", corner_a="<<corner_a<<", corner_b="<<corner_b<<", face_a_active="<<face_a_active<<", face_b_active="<<face_b_active<<std::endl;
}
const osg::Polytope::PlaneList& planes = lightVolumePolytope.getPlaneList();
for(unsigned int i=0; i<planes.size(); ++i)
{
OSG_INFO<<" plane "<<planes[i]<<" "<<((lightVolumePolytope.getResultMask() & (0x1<<i))?"on":"off")<<std::endl;
}
return lightVolumePolytope;
}
bool MWShadowTechnique::computeShadowCameraSettings(Frustum& frustum, LightData& positionedLight, osg::Matrixd& projectionMatrix, osg::Matrixd& viewMatrix)
{
OSG_INFO<<"standardShadowMapCameraSettings()"<<std::endl;
osg::Vec3d lightSide;
const ShadowSettings* settings = getShadowedScene()->getShadowSettings();
double dotProduct_v = positionedLight.lightDir * frustum.frustumCenterLine;
double gamma_v = acos(dotProduct_v);
if (gamma_v<osg::DegreesToRadians(settings->getPerspectiveShadowMapCutOffAngle()) || gamma_v>osg::DegreesToRadians(180.0-settings->getPerspectiveShadowMapCutOffAngle()))
{
OSG_INFO<<"View direction and Light direction below tolerance"<<std::endl;
osg::Vec3d viewSide = osg::Matrixd::transform3x3(frustum.modelViewMatrix, osg::Vec3d(1.0,0.0,0.0));
lightSide = positionedLight.lightDir ^ (viewSide ^ positionedLight.lightDir);
lightSide.normalize();
}
else
{
lightSide = positionedLight.lightDir ^ frustum.frustumCenterLine;
lightSide.normalize();
}
osg::Vec3d lightUp = lightSide ^ positionedLight.lightDir;
#if 0
OSG_NOTICE<<"positionedLight.lightDir="<<positionedLight.lightDir<<std::endl;
OSG_NOTICE<<"lightSide="<<lightSide<<std::endl;
OSG_NOTICE<<"lightUp="<<lightUp<<std::endl;
#endif
if (positionedLight.directionalLight)
{
double xMin=0.0, xMax=0.0;
double yMin=0.0, yMax=0.0;
double zMin=0.0, zMax=0.0;
for(Frustum::Vertices::iterator itr = frustum.corners.begin();
itr != frustum.corners.end();
++itr)
{
osg::Vec3d cornerDelta(*itr - frustum.center);
osg::Vec3d cornerInLightCoords(cornerDelta*lightSide,
cornerDelta*lightUp,
cornerDelta*positionedLight.lightDir);
OSG_INFO<<" corner ="<<*itr<<" in lightcoords "<<cornerInLightCoords<<std::endl;
xMin = osg::minimum( xMin, cornerInLightCoords.x());
xMax = osg::maximum( xMax, cornerInLightCoords.x());
yMin = osg::minimum( yMin, cornerInLightCoords.y());
yMax = osg::maximum( yMax, cornerInLightCoords.y());
zMin = osg::minimum( zMin, cornerInLightCoords.z());
zMax = osg::maximum( zMax, cornerInLightCoords.z());
}
OSG_INFO<<"before bs xMin="<<xMin<<", xMax="<<xMax<<", yMin="<<yMin<<", yMax="<<yMax<<", zMin="<<zMin<<", zMax="<<zMax<<std::endl;
osg::BoundingSphere bs = _shadowedScene->getBound();
osg::Vec3d modelCenterRelativeFrustumCenter(bs.center()-frustum.center);
osg::Vec3d modelCenterInLightCoords(modelCenterRelativeFrustumCenter*lightSide,
modelCenterRelativeFrustumCenter*lightUp,
modelCenterRelativeFrustumCenter*positionedLight.lightDir);
OSG_INFO<<"modelCenterInLight="<<modelCenterInLightCoords<<" radius="<<bs.radius()<<std::endl;
double radius(bs.radius());
xMin = osg::maximum(xMin, modelCenterInLightCoords.x()-radius);
xMax = osg::minimum(xMax, modelCenterInLightCoords.x()+radius);
yMin = osg::maximum(yMin, modelCenterInLightCoords.y()-radius);
yMax = osg::minimum(yMax, modelCenterInLightCoords.y()+radius);
zMin = modelCenterInLightCoords.z()-radius;
zMax = osg::minimum(zMax, modelCenterInLightCoords.z()+radius);
OSG_INFO<<"after bs xMin="<<xMin<<", xMax="<<xMax<<", yMin="<<yMin<<", yMax="<<yMax<<", zMin="<<zMin<<", zMax="<<zMax<<std::endl;
if (xMin>=xMax || yMin>=yMax || zMin>=zMax)
{
OSG_INFO<<"Warning nothing available to create shadows"<<zMax<<std::endl;
return false;
}
else
{
projectionMatrix.makeOrtho(xMin,xMax, yMin, yMax,0.0,zMax-zMin);
viewMatrix.makeLookAt(frustum.center+positionedLight.lightDir*zMin, frustum.center+positionedLight.lightDir*zMax, lightUp);
}
}
else
{
double zMax=-dbl_max;
OSG_INFO<<"lightDir = "<<positionedLight.lightDir<<std::endl;
OSG_INFO<<"lightPos3 = "<<positionedLight.lightPos3<<std::endl;
for(Frustum::Vertices::iterator itr = frustum.corners.begin();
itr != frustum.corners.end();
++itr)
{
osg::Vec3d cornerDelta(*itr - positionedLight.lightPos3);
osg::Vec3d cornerInLightCoords(cornerDelta*lightSide,
cornerDelta*lightUp,
cornerDelta*positionedLight.lightDir);
OSG_INFO<<" cornerInLightCoords= "<<cornerInLightCoords<<std::endl;
zMax = osg::maximum( zMax, cornerInLightCoords.z());
}
OSG_INFO<<"zMax = "<<zMax<<std::endl;
if (zMax<0.0)
{
// view frustum entirely behind light
return false;
}
double minRatio = 0.0001;
double zMin=zMax*minRatio;
double fov = positionedLight.light->getSpotCutoff() * 2.0;
if(fov < 180.0) // spotlight
{
projectionMatrix.makePerspective(fov, 1.0, zMin, zMax);
viewMatrix.makeLookAt(positionedLight.lightPos3,
positionedLight.lightPos3+positionedLight.lightDir, lightUp);
}
else
{
double fovMAX = 160.0f;
fov = 0.0;
// calculate the max FOV from the corners of the frustum relative to the light position
for(Frustum::Vertices::iterator itr = frustum.corners.begin();
itr != frustum.corners.end();
++itr)
{
osg::Vec3d cornerDelta(*itr - positionedLight.lightPos3);
double length = cornerDelta.length();
if (length==0.0) fov = osg::minimum(fov, 180.0);
else
{
double dotProduct = cornerDelta*positionedLight.lightDir;
double angle = 2.0*osg::RadiansToDegrees( acos(dotProduct/length) );
fov = osg::maximum(fov, angle);
}
}
OSG_INFO<<"Computed fov = "<<fov<<std::endl;
if (fov>fovMAX)
{
OSG_INFO<<"Clampping fov = "<<fov<<std::endl;
fov=fovMAX;
}
projectionMatrix.makePerspective(fov, 1.0, zMin, zMax);
viewMatrix.makeLookAt(positionedLight.lightPos3,
positionedLight.lightPos3+positionedLight.lightDir, lightUp);
}
}
return true;
}
struct ConvexHull
{
typedef std::vector<osg::Vec3d> Vertices;
typedef std::pair<osg::Vec3d, osg::Vec3d> Edge;
typedef std::vector<Edge> Edges;
typedef std::vector<osg::Vec3d> VertexSet;
Edges _edges;
bool valid() const { return !_edges.empty(); }
void setToFrustum(MWShadowTechnique::Frustum& frustum)
{
_edges.emplace_back(frustum.corners[0], frustum.corners[1]);
_edges.emplace_back(frustum.corners[1], frustum.corners[2]);
_edges.emplace_back(frustum.corners[2], frustum.corners[3]);
_edges.emplace_back(frustum.corners[3], frustum.corners[0]);
_edges.emplace_back(frustum.corners[4], frustum.corners[5]);
_edges.emplace_back(frustum.corners[5], frustum.corners[6]);
_edges.emplace_back(frustum.corners[6], frustum.corners[7]);
_edges.emplace_back(frustum.corners[7], frustum.corners[4]);
_edges.emplace_back(frustum.corners[0], frustum.corners[4]);
_edges.emplace_back(frustum.corners[1], frustum.corners[5]);
_edges.emplace_back(frustum.corners[2], frustum.corners[6]);
_edges.emplace_back(frustum.corners[3], frustum.corners[7]);
}
struct ConvexHull2D
{
// Implementation based on https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain#C++
typedef osg::Vec3d Point;
static double cross(const Point &O, const Point &A, const Point &B)
{
return (A.x() - O.x())*(B.y() - O.y()) - (A.y() - O.y())*(B.x() - O.x());
}
// Calculates the 2D convex hull and returns it as a vector containing the points in CCW order with the first and last point being the same.
static Vertices convexHull(const VertexSet &P)
{
size_t n = P.size(), k = 0;
if (n <= 3)
return Vertices(P.cbegin(), P.cend());
Vertices H(2 * n);
// Points are already sorted in a std::set
// Build lower hull
for(const auto& vert : P)
{
while (k >= 2 && cross(H[k - 2], H[k - 1], vert) <= 0)
k--;
H[k++] = vert;
}
// Build upper hull
size_t t = k + 1;
for (auto pItr = std::next(P.crbegin()); pItr != P.crend(); ++pItr)
{
while (k >= t && cross(H[k - 2], H[k - 1], *pItr) <= 0)
k--;
H[k++] = *pItr;
}
H.resize(k - 1);
return H;
}
};
Vertices findInternalEdges(const osg::Vec3d& mainVertex, const Vertices& connectedVertices)
{
Vertices internalEdgeVertices;
for (const auto& vertex : connectedVertices)
{
osg::Matrixd matrix;
osg::Vec3d dir = vertex - mainVertex;
matrix.makeLookAt(mainVertex, vertex, dir.z() == 0 ? osg::Vec3d(0, 0, 1) : osg::Vec3d(1, 0, 0));
Vertices testVertices;
for (const auto& testVertex : connectedVertices)
{
if (vertex != testVertex)
testVertices.push_back(testVertex);
}
std::vector<double> bearings;
for (const auto& testVertex : testVertices)
{
osg::Vec3d transformedVertex = testVertex * matrix;
bearings.push_back(atan2(transformedVertex.y(), transformedVertex.x()));
}
std::sort(bearings.begin(), bearings.end());
bool keep = false;
for (auto itr = bearings.begin(); itr + 1 != bearings.end(); ++itr)
{
if (*itr + osg::PI < *(itr + 1))
{
keep = true;
break;
}
}
if (!keep && bearings[0] + osg::PI > bearings.back())
keep = true;
if (!keep)
internalEdgeVertices.push_back(vertex);
}
return internalEdgeVertices;
}
void extendTowardsNegativeZ()
{
// Collect the set of vertices
VertexSet vertices;
for (const Edge& edge : _edges)
{
vertices.emplace_back(edge.first);
vertices.emplace_back(edge.second);
}
// Sort and make unique.
std::sort(vertices.begin(), vertices.end());
vertices.erase(std::unique(vertices.begin(), vertices.end()), vertices.end());
if (vertices.size() == 0)
return;
// Get the vertices contributing to the 2D convex hull
Vertices extremeVertices = ConvexHull2D::convexHull(vertices);
// Add their extrusions to the final edge collection
// We extrude as far as -1.5 as the coordinate space shouldn't ever put any shadow casters further than -1.0
Edges finalEdges;
// Add edges towards -Z
for (const auto& vertex : extremeVertices)
finalEdges.emplace_back(vertex, osg::Vec3d(vertex.x(), vertex.y(), -1.5));
// Add edge loop to 'seal' the hull
for (auto itr = extremeVertices.cbegin(); itr != extremeVertices.cend() - 1; ++itr)
finalEdges.emplace_back(osg::Vec3d(itr->x(), itr->y(), -1.5), osg::Vec3d((itr + 1)->x(), (itr + 1)->y(), -1.5));
// The convex hull algorithm we are using sometimes places a point at both ends of the vector, so we don't always need to add the last edge separately.
if (extremeVertices.front() != extremeVertices.back())
finalEdges.emplace_back(osg::Vec3d(extremeVertices.front().x(), extremeVertices.front().y(), -1.5), osg::Vec3d(extremeVertices.back().x(), extremeVertices.back().y(), -1.5));
// Remove internal edges connected to extreme vertices
for (const auto& vertex : extremeVertices)
{
Vertices connectedVertices;
for (const Edge& edge : _edges)
{
if (edge.first == vertex)
connectedVertices.push_back(edge.second);
else if (edge.second == vertex)
connectedVertices.push_back(edge.first);
}
connectedVertices.emplace_back(vertex.x(), vertex.y(), -1.5);
Vertices unwantedEdgeEnds = findInternalEdges(vertex, connectedVertices);
for (const auto& edgeEnd : unwantedEdgeEnds)
{
const auto edgeA = Edge(vertex, edgeEnd);
const auto edgeB = Edge(edgeEnd, vertex);
_edges.erase(std::remove_if(_edges.begin(), _edges.end(), [&](const auto& elem)
{
return elem == edgeA || elem == edgeB;
}), _edges.end());
}
}
// Gather connected vertices
VertexSet unprocessedConnectedVertices = std::move(extremeVertices);
VertexSet connectedVertices;
const auto containsVertex = [&](const auto& vert)
{
return std::find(connectedVertices.begin(), connectedVertices.end(), vert) != connectedVertices.end();
};
while (!unprocessedConnectedVertices.empty())
{
osg::Vec3d vertex = unprocessedConnectedVertices.back();
unprocessedConnectedVertices.pop_back();
connectedVertices.emplace_back(vertex);
for (const Edge& edge : _edges)
{
osg::Vec3d otherEnd;
if (edge.first == vertex)
otherEnd = edge.second;
else if (edge.second == vertex)
otherEnd = edge.first;
else
continue;
if (containsVertex(otherEnd))
continue;
unprocessedConnectedVertices.emplace_back(otherEnd);
}
}
for (const Edge& edge : _edges)
{
if (containsVertex(edge.first) || containsVertex(edge.second))
finalEdges.push_back(edge);
}
_edges = std::move(finalEdges);
}
void transform(const osg::Matrixd& m)
{
for (auto& edge : _edges)
{
edge.first = edge.first * m;
edge.second = edge.second * m;
}
}
void clip(const osg::Plane& plane)
{
Vertices intersections;
// OSG_NOTICE<<"clip("<<plane<<") edges.size()="<<_edges.size()<<std::endl;
for(auto itr = _edges.begin(); itr != _edges.end();)
{
double d0 = plane.distance(itr->first);
double d1 = plane.distance(itr->second);
if (d0<0.0 && d1<0.0)
{
// OSG_NOTICE<<" Edge completely outside, removing"<<std::endl;
itr = _edges.erase(itr);
}
else if (d0>=0.0 && d1>=0.0)
{
// OSG_NOTICE<<" Edge completely inside"<<std::endl;
++itr;
}
else
{
osg::Vec3d& v0 = itr->first;
osg::Vec3d& v1 = itr->second;
osg::Vec3d intersection = v0 - (v1-v0)*(d0/(d1-d0));
intersections.push_back(intersection);
// OSG_NOTICE<<" Edge across clip plane, v0="<<v0<<", v1="<<v1<<", intersection= "<<intersection<<std::endl;
if (d0<0.0)
{
// move first vertex on edge
v0 = intersection;
}
else
{
// move second vertex on edge
v1 = intersection;
}
++itr;
}
}
// OSG_NOTICE<<"After clipping, have "<<intersections.size()<<" to insert"<<std::endl;
if (intersections.size() < 2)
{
return;
}
if (intersections.size() == 2)
{
_edges.emplace_back(intersections[0], intersections[1]);
return;
}
if (intersections.size() == 3)
{
_edges.emplace_back(intersections[0], intersections[1]);
_edges.emplace_back(intersections[1], intersections[2]);
_edges.emplace_back(intersections[2], intersections[0]);
return;
}
// more than 3 intersections so have to sort them in clockwise order so that
// we can generate the edges correctly.
osg::Vec3d normal(plane.getNormal());
osg::Vec3d side_x = osg::Vec3d(1.0,0.0,0.0) ^ normal;
osg::Vec3d side_y = osg::Vec3d(0.0,1.0,0.0) ^ normal;
osg::Vec3d side = (side_x.length2()>=side_y.length2()) ? side_x : side_y;
side.normalize();
osg::Vec3d up = side ^ normal;
up.normalize();
osg::Vec3d center;
for(auto& vertex : intersections)
{
center += vertex;
center.x() = osg::maximum(center.x(), -dbl_max);
center.y() = osg::maximum(center.y(), -dbl_max);
center.z() = osg::maximum(center.z(), -dbl_max);
center.x() = osg::minimum(center.x(), dbl_max);
center.y() = osg::minimum(center.y(), dbl_max);
center.z() = osg::minimum(center.z(), dbl_max);
}
center /= double(intersections.size());
typedef std::map<double, std::list<std::pair<osg::Vec3d, double>>> VertexMap;
VertexMap vertexMap;
for (const auto& vertex : intersections)
{
osg::Vec3d dv = vertex - center;
double h = dv * side;
double v = dv * up;
double angle = atan2(h,v);
// OSG_NOTICE<<"angle = "<<osg::RadiansToDegrees(angle)<<", h="<<h<<" v= "<<v<<std::endl;
// We need to make sure all intersections are added to the list in the right order, even if they're so far away that their angles work out the same.
double sortValue;
if (angle < osg::DegreesToRadians(-135.0) || angle > osg::DegreesToRadians(135.0))
sortValue = -h;
else if (angle < osg::DegreesToRadians(-45.0))
sortValue = v;
else if (angle < osg::DegreesToRadians(45.0))
sortValue = h;
else
sortValue = -v;
if (vertexMap.count(angle))
{
auto listItr = vertexMap[angle].begin();
while (listItr != vertexMap[angle].end() && listItr->second < sortValue)
++listItr;
vertexMap[angle].emplace(listItr, std::make_pair(vertex, sortValue));
}
else
vertexMap[angle].emplace_back(vertex, sortValue);
}
osg::Vec3d previous_v = vertexMap.rbegin()->second.back().first;
for (auto itr = vertexMap.begin(); itr != vertexMap.end(); ++itr)
{
for (const auto& vertex : itr->second)
{
_edges.emplace_back(previous_v, vertex.first);
previous_v = vertex.first;
}
}
// OSG_NOTICE<<" after clip("<<plane<<") edges.size()="<<_edges.size()<<std::endl;
}
void clip(const osg::Polytope& polytope)
{
const osg::Polytope::PlaneList& planes = polytope.getPlaneList();
for(const auto& plane : planes)
{
clip(plane);
}
}
double min(unsigned int index) const
{
double m = dbl_max;
for(const auto& edge : _edges)
{
if (edge.first[index] < m) m = edge.first[index];
if (edge.second[index] < m) m = edge.second[index];
}
return m;
}
double max(unsigned int index) const
{
double m = -dbl_max;
for (const auto& edge : _edges)
{
if (edge.first[index] > m) m = edge.first[index];
if (edge.second[index] > m) m = edge.second[index];
}
return m;
}
double minRatio(const osg::Vec3d& eye, unsigned int index) const
{
double m = dbl_max;
osg::Vec3d delta;
double ratio;
for (const auto& edge : _edges)
{
delta = edge.first - eye;
ratio = delta[index] / delta[1];
if (ratio < m) m = ratio;
delta = edge.second - eye;
ratio = delta[index] / delta[1];
if (ratio < m) m = ratio;
}
return m;
}
double maxRatio(const osg::Vec3d& eye, unsigned int index) const
{
double m = -dbl_max;
osg::Vec3d delta;
double ratio;
for (const auto& edge : _edges)
{
delta = edge.first - eye;
ratio = delta[index] / delta[1];
if (ratio > m) m = ratio;
delta = edge.second - eye;
ratio = delta[index] / delta[1];
if (ratio > m) m = ratio;
}
return m;
}
void output(std::ostream& out)
{
out << "ConvexHull" << std::endl;
for (const auto& edge : _edges)
{
out << " edge (" << edge.first << ") (" << edge.second << ")" << std::endl;
}
}
};
struct RenderLeafBounds
{
RenderLeafBounds():
computeRatios(false),
numRenderLeaf(0),
n(0.0),
previous_modelview(0),
clip_min_x(-1.0), clip_max_x(1.0),
clip_min_y(-1.0), clip_max_y(1.0),
clip_min_z(-1.0), clip_max_z(1.0),
clip_min_x_ratio(-dbl_max), clip_max_x_ratio(dbl_max),
clip_min_z_ratio(-dbl_max), clip_max_z_ratio(dbl_max),
min_x_ratio(dbl_max), max_x_ratio(-dbl_max),
min_z_ratio(dbl_max), max_z_ratio(-dbl_max),
min_x(1.0), max_x(-1.0),
min_y(1.0), max_y(-1.0),
min_z(1.0), max_z(-1.0)
{
//OSG_NOTICE<<std::endl<<"RenderLeafBounds"<<std::endl;
}
void set(const osg::Matrixd& p)
{
computeRatios = false;
light_p = p;
clip_min_x = -dbl_max; clip_max_x = dbl_max;
clip_min_y = -dbl_max; clip_max_y = dbl_max;
clip_min_z = -dbl_max; clip_max_z = dbl_max;
min_x = dbl_max; max_x = -dbl_max;
min_y = dbl_max; max_y = -dbl_max;
min_z = dbl_max; max_z = -dbl_max;
}
void set(const osg::Matrixd& p, osg::Vec3d& e_ls, double nr)
{
computeRatios = true;
light_p = p;
eye_ls = e_ls;
n = nr;
}
void operator() (const osgUtil::RenderLeaf* renderLeaf)
{
++numRenderLeaf;
if (renderLeaf->_modelview.get()!=previous_modelview)
{
previous_modelview = renderLeaf->_modelview.get();
if (previous_modelview)
{
light_mvp.mult(*renderLeaf->_modelview, light_p);
}
else
{
// no modelview matrix (such as when LightPointNode is in the scene graph) so assume
// that modelview matrix is indentity.
light_mvp = light_p;
}
// OSG_INFO<<"Computing new light_mvp "<<light_mvp<<std::endl;
}
else
{
// OSG_INFO<<"Reusing light_mvp "<<light_mvp<<std::endl;
}
const osg::BoundingBox& bb = renderLeaf->_drawable->getBoundingBox();
if (bb.valid())
{
// OSG_NOTICE<<"checked extents of "<<renderLeaf->_drawable->getName()<<std::endl;
handle(osg::Vec3d(bb.xMin(),bb.yMin(),bb.zMin()));
handle(osg::Vec3d(bb.xMax(),bb.yMin(),bb.zMin()));
handle(osg::Vec3d(bb.xMin(),bb.yMax(),bb.zMin()));
handle(osg::Vec3d(bb.xMax(),bb.yMax(),bb.zMin()));
handle(osg::Vec3d(bb.xMin(),bb.yMin(),bb.zMax()));
handle(osg::Vec3d(bb.xMax(),bb.yMin(),bb.zMax()));
handle(osg::Vec3d(bb.xMin(),bb.yMax(),bb.zMax()));
handle(osg::Vec3d(bb.xMax(),bb.yMax(),bb.zMax()));
}
else
{
OSG_INFO<<"bb invalid"<<std::endl;
}
}
void handle(const osg::Vec3d& v)
{
osg::Vec3d ls = v * light_mvp;
// OSG_NOTICE<<" corner v="<<v<<", ls="<<ls<<std::endl;
if (computeRatios)
{
osg::Vec3d delta = ls-eye_ls;
double x_ratio, z_ratio;
if (delta.y()>n)
{
x_ratio = delta.x()/delta.y();
z_ratio = delta.z()/delta.y();
}
else
{
x_ratio = delta.x()/n;
z_ratio = delta.z()/n;
}
if (x_ratio<min_x_ratio) min_x_ratio = x_ratio;
if (x_ratio>max_x_ratio) max_x_ratio = x_ratio;
if (z_ratio<min_z_ratio) min_z_ratio = z_ratio;
if (z_ratio>max_z_ratio) max_z_ratio = z_ratio;
}
// clip to the light space
if (ls.x()<clip_min_x) ls.x()=clip_min_x;
if (ls.x()>clip_max_x) ls.x()=clip_max_x;
if (ls.y()<clip_min_y) ls.y()=clip_min_y;
if (ls.y()>clip_max_y) ls.y()=clip_max_y;
if (ls.z()<clip_min_z) ls.z()=clip_min_z;
if (ls.z()>clip_max_z) ls.z()=clip_max_z;
// compute the xyz range.
if (ls.x()<min_x) min_x=ls.x();
if (ls.x()>max_x) max_x=ls.x();
if (ls.y()<min_y) min_y=ls.y();
if (ls.y()>max_y) max_y=ls.y();
if (ls.z()<min_z) { min_z=ls.z(); /* OSG_NOTICE<<" - ";*/ }
if (ls.z()>max_z) { max_z=ls.z(); /* OSG_NOTICE<<" + ";*/ }
// OSG_NOTICE<<" bb.z() in ls = "<<ls.z()<<std::endl;
}
bool computeRatios;
unsigned int numRenderLeaf;
osg::Matrixd light_p;
osg::Vec3d eye_ls;
double n;
osg::Matrixd light_mvp;
osg::RefMatrix* previous_modelview;
double clip_min_x, clip_max_x;
double clip_min_y, clip_max_y;
double clip_min_z, clip_max_z;
double clip_min_x_ratio, clip_max_x_ratio;
double clip_min_z_ratio, clip_max_z_ratio;
double min_x_ratio, max_x_ratio;
double min_z_ratio, max_z_ratio;
double min_x, max_x;
double min_y, max_y;
double min_z, max_z;
};
bool MWShadowTechnique::cropShadowCameraToMainFrustum(Frustum& frustum, osg::Camera* camera, double viewNear, double viewFar, std::vector<osg::Plane>& planeList)
{
osg::Matrixd light_p = camera->getProjectionMatrix();
osg::Matrixd light_v = camera->getViewMatrix();
osg::Matrixd light_vp = light_v * light_p;
osg::Matrixd oldLightP = light_p;
ConvexHull convexHull;
convexHull.setToFrustum(frustum);
osg::Vec3d nearPoint = frustum.eye + frustum.frustumCenterLine * viewNear;
osg::Vec3d farPoint = frustum.eye + frustum.frustumCenterLine * viewFar;
double nearDist = -frustum.frustumCenterLine * nearPoint;
double farDist = frustum.frustumCenterLine * farPoint;
convexHull.clip(osg::Plane(frustum.frustumCenterLine, nearDist));
convexHull.clip(osg::Plane(-frustum.frustumCenterLine, farDist));
convexHull.transform(light_vp);
double xMin = -1.0, xMax = 1.0;
double yMin = -1.0, yMax = 1.0;
double zMin = -1.0, zMax = 1.0;
if (convexHull.valid())
{
xMin = osg::maximum(-1.0, convexHull.min(0));
xMax = osg::minimum(1.0, convexHull.max(0));
yMin = osg::maximum(-1.0, convexHull.min(1));
yMax = osg::minimum(1.0, convexHull.max(1));
zMin = osg::maximum(-1.0, convexHull.min(2));
zMax = osg::minimum(1.0, convexHull.max(2));
}
else
return false;
if (xMin != -1.0 || yMin != -1.0 || zMin != -1.0 ||
xMax != 1.0 || yMax != 1.0 || zMax != 1.0)
{
osg::Matrix m;
m.makeTranslate(osg::Vec3d(-0.5*(xMax + xMin),
-0.5*(yMax + yMin),
-0.5*(zMax + zMin)));
m.postMultScale(osg::Vec3d(2.0 / (xMax - xMin),
2.0 / (yMax - yMin),
2.0 / (zMax - zMin)));
light_p.postMult(m);
camera->setProjectionMatrix(light_p);
convexHull.transform(osg::Matrixd::inverse(oldLightP));
xMin = convexHull.min(0);
xMax = convexHull.max(0);
yMin = convexHull.min(1);
yMax = convexHull.max(1);
zMin = convexHull.min(2);
planeList.emplace_back(0.0, -1.0, 0.0, yMax);
planeList.emplace_back(0.0, 1.0, 0.0, -yMin);
planeList.emplace_back(-1.0, 0.0, 0.0, xMax);
planeList.emplace_back(1.0, 0.0, 0.0, -xMin);
// In view space, the light is at the most positive value, and we want to cull stuff beyond the minimum value.
planeList.emplace_back(0.0, 0.0, 1.0, -zMin);
// Don't add a zMax culling plane - we still want those objects, but don't care about their depth buffer value.
}
return true;
}
bool MWShadowTechnique::adjustPerspectiveShadowMapCameraSettings(osgUtil::RenderStage* renderStage, Frustum& frustum, LightData& /*positionedLight*/, osg::Camera* camera, double viewNear, double viewFar)
{
const ShadowSettings* settings = getShadowedScene()->getShadowSettings();
//frustum.projectionMatrix;
//frustum.modelViewMatrix;
osg::Matrixd light_p = camera->getProjectionMatrix();
osg::Matrixd light_v = camera->getViewMatrix();
osg::Matrixd light_vp = light_v * light_p;
osg::Vec3d lightdir(0.0,0.0,-1.0);
// check whether this light space projection is perspective or orthographic.
bool orthographicLightSpaceProjection = light_p(0,3)==0.0 && light_p(1,3)==0.0 && light_p(2,3)==0.0;
if (!orthographicLightSpaceProjection)
{
OSG_INFO<<"perspective light space projection not yet supported."<<std::endl;
return false;
}
//OSG_NOTICE<<"light_v="<<light_v<<std::endl;
//OSG_NOTICE<<"light_p="<<light_p<<std::endl;
ConvexHull convexHull;
convexHull.setToFrustum(frustum);
osg::Vec3d nearPoint = frustum.eye + frustum.frustumCenterLine * viewNear;
osg::Vec3d farPoint = frustum.eye + frustum.frustumCenterLine * viewFar;
double nearDist = -frustum.frustumCenterLine * nearPoint;
double farDist = frustum.frustumCenterLine * farPoint;
convexHull.clip(osg::Plane(frustum.frustumCenterLine, nearDist));
convexHull.clip(osg::Plane(-frustum.frustumCenterLine, farDist));
#if 0
OSG_NOTICE<<"ws ConvexHull xMin="<<convexHull.min(0)<<", xMax="<<convexHull.max(0)<<std::endl;
OSG_NOTICE<<"ws ConvexHull yMin="<<convexHull.min(1)<<", yMax="<<convexHull.max(1)<<std::endl;
OSG_NOTICE<<"ws ConvexHull zMin="<<convexHull.min(2)<<", zMax="<<convexHull.max(2)<<std::endl;
convexHull.output(osg::notify(osg::NOTICE));
#endif
convexHull.transform(light_vp);
ConvexHull convexHullUnextended = convexHull;
convexHull.extendTowardsNegativeZ();
#if 0
convexHull.output(osg::notify(osg::NOTICE));
OSG_NOTICE<<"ls ConvexHull xMin="<<convexHull.min(0)<<", xMax="<<convexHull.max(0)<<std::endl;
OSG_NOTICE<<"ls ConvexHull yMin="<<convexHull.min(1)<<", yMax="<<convexHull.max(1)<<std::endl;
OSG_NOTICE<<"ls ConvexHull zMin="<<convexHull.min(2)<<", zMax="<<convexHull.max(2)<<std::endl;
#endif
#if 0
// only applicable when the light space contains the whole model contained in the view frustum.
{
convexHull.clip(osg::Plane(0.0,0.0,1,1.0)); // clip by near plane of light space.
convexHull.clip(osg::Plane(0.0,0.0,-1,1.0)); // clip by far plane of light space.
}
#endif
#if 1
if (renderStage)
{
#if 1
osg::ElapsedTime timer;
#endif
RenderLeafTraverser<RenderLeafBounds> rli;
rli.set(light_p);
rli.traverse(renderStage);
if (rli.numRenderLeaf==0)
{
return false;
}
#if 0
OSG_NOTICE<<"New Time for RenderLeafTraverser "<<timer.elapsedTime_m()<<"ms, number of render leaves "<<rli.numRenderLeaf<<std::endl;
OSG_NOTICE<<" scene bounds min_x="<<rli.min_x<<", max_x="<<rli.max_x<<std::endl;
OSG_NOTICE<<" scene bounds min_y="<<rli.min_y<<", max_y="<<rli.max_y<<std::endl;
OSG_NOTICE<<" scene bounds min_z="<<rli.min_z<<", max_z="<<rli.max_z<<std::endl;
#endif
#if 0
double widest_x = osg::maximum(fabs(rli.min_x), fabs(rli.max_x));
double widest_y = osg::maximum(fabs(rli.min_y), fabs(rli.max_y));
double widest_z = osg::maximum(fabs(rli.min_z), fabs(rli.max_z));
#endif
#if 1
#if 1
convexHull.clip(osg::Plane(1.0,0.0,0.0,-rli.min_x));
convexHull.clip(osg::Plane(-1.0,0.0,0.0,rli.max_x));
convexHullUnextended.clip(osg::Plane(1.0, 0.0, 0.0, -rli.min_x));
convexHullUnextended.clip(osg::Plane(-1.0, 0.0, 0.0, rli.max_x));
#else
convexHull.clip(osg::Plane(1.0,0.0,0.0,widest_x));
convexHull.clip(osg::Plane(-1.0,0.0,0.0,widest_x));
#endif
#if 1
convexHull.clip(osg::Plane(0.0,1.0,0.0,-rli.min_y));
convexHull.clip(osg::Plane(0.0,-1.0,0.0,rli.max_y));
convexHullUnextended.clip(osg::Plane(0.0, 1.0, 0.0, -rli.min_y));
convexHullUnextended.clip(osg::Plane(0.0, -1.0, 0.0, rli.max_y));
#endif
#endif
#if 1
convexHull.clip(osg::Plane(0.0,0.0,1.0,-rli.min_z));
convexHull.clip(osg::Plane(0.0,0.0,-1.0,rli.max_z));
convexHullUnextended.clip(osg::Plane(0.0, 0.0, 1.0, -rli.min_z));
convexHullUnextended.clip(osg::Plane(0.0, 0.0, -1.0, rli.max_z));
#elif 0
convexHull.clip(osg::Plane(0.0,0.0,1.0,1.0));
convexHull.clip(osg::Plane(0.0,0.0,-1.0,1.0));
#endif
#if 0
OSG_NOTICE<<"widest_x = "<<widest_x<<std::endl;
OSG_NOTICE<<"widest_y = "<<widest_y<<std::endl;
OSG_NOTICE<<"widest_z = "<<widest_z<<std::endl;
#endif
}
#endif
#if 0
convexHull.output(osg::notify(osg::NOTICE));
OSG_NOTICE<<"after clipped ls ConvexHull xMin="<<convexHull.min(0)<<", xMax="<<convexHull.max(0)<<std::endl;
OSG_NOTICE<<"after clipped ls ConvexHull yMin="<<convexHull.min(1)<<", yMax="<<convexHull.max(1)<<std::endl;
OSG_NOTICE<<"after clipped ls ConvexHull zMin="<<convexHull.min(2)<<", zMax="<<convexHull.max(2)<<std::endl;
#endif
double xMin=-1.0, xMax=1.0;
double yMin=-1.0, yMax=1.0;
double zMin=-1.0, zMax=1.0;
if (convexHull.valid())
{
double widest_x = osg::maximum(fabs(convexHull.min(0)), fabs(convexHull.max(0)));
xMin = osg::maximum(-1.0,-widest_x);
xMax = osg::minimum(1.0,widest_x);
yMin = osg::maximum(-1.0,convexHull.min(1));
yMax = osg::minimum(1.0,convexHull.max(1));
}
else
{
// clipping of convex hull has invalidated it, so reset it so later checks on it provide valid results.
convexHull.setToFrustum(frustum);
convexHull.transform(light_vp);
}
#if 0
OSG_NOTICE<<"xMin = "<<xMin<<", \txMax = "<<xMax<<std::endl;
OSG_NOTICE<<"yMin = "<<yMin<<", \tyMax = "<<yMax<<std::endl;
OSG_NOTICE<<"zMin = "<<zMin<<", \tzMax = "<<zMax<<std::endl;
#endif
#if 1
// we always want the lightspace to include the computed near plane.
zMin = -1.0;
if (xMin!=-1.0 || yMin!=-1.0 || zMin!=-1.0 ||
xMax!=1.0 || yMax!=1.0 || zMax!=1.0)
{
osg::Matrix m;
m.makeTranslate(osg::Vec3d(-0.5*(xMax+xMin),
-0.5*(yMax+yMin),
-0.5*(zMax+zMin)));
m.postMultScale(osg::Vec3d(2.0/(xMax-xMin),
2.0/(yMax-yMin),
2.0/(zMax-zMin)));
convexHull.transform(m);
convexHullUnextended.transform(m);
light_p.postMult(m);
light_vp = light_v * light_p;
#if 0
OSG_NOTICE<<"Adjusting projection matrix "<<m<<std::endl;
convexHull.output(osg::notify(osg::NOTICE));
#endif
camera->setProjectionMatrix(light_p);
}
#endif
osg::Vec3d eye_v = frustum.eye * light_v;
//osg::Vec3d centerNearPlane_v = frustum.centerNearPlane * light_v;
osg::Vec3d center_v = frustum.center * light_v;
osg::Vec3d viewdir_v = center_v-eye_v; viewdir_v.normalize();
double dotProduct_v = lightdir * viewdir_v;
double gamma_v = acos(dotProduct_v);
if (gamma_v<osg::DegreesToRadians(settings->getPerspectiveShadowMapCutOffAngle()) || gamma_v>osg::DegreesToRadians(180-settings->getPerspectiveShadowMapCutOffAngle()))
{
// OSG_NOTICE<<"Light and view vectors near parallel - use standard shadow map."<<std::endl;
return true;
}
//OSG_NOTICE<<"gamma="<<osg::RadiansToDegrees(gamma_v)<<std::endl;
//OSG_NOTICE<<"eye_v="<<eye_v<<std::endl;
//OSG_NOTICE<<"viewdir_v="<<viewdir_v<<std::endl;
osg::Vec3d eye_ls = frustum.eye * light_vp;
#if 0
if (eye_ls.y()>-1.0)
{
OSG_NOTICE<<"Eye point within light space - use standard shadow map."<<std::endl;
return true;
}
#endif
//osg::Vec3d centerNearPlane_ls = frustum.centerNearPlane * light_vp;
//osg::Vec3d centerFarPlane_ls = frustum.centerFarPlane * light_vp;
osg::Vec3d center_ls = frustum.center * light_vp;
osg::Vec3d viewdir_ls = center_ls-eye_ls; viewdir_ls.normalize();
osg::Vec3d side = lightdir ^ viewdir_ls; side.normalize();
osg::Vec3d up = side ^ lightdir;
double d = 2.0;
double alpha = osg::DegreesToRadians(30.0);
double n = tan(alpha)*tan(osg::PI_2-gamma_v)*tan(osg::PI_2-gamma_v);
//double n = tan(alpha)*tan(osg::PI_2-gamma_v);
//OSG_NOTICE<<"n = "<<n<<", eye_ls.y()="<<eye_ls.y()<<", eye_v="<<eye_v<<", eye="<<frustum.eye<<std::endl;
double min_n = osg::maximum(-1.0-eye_ls.y(), settings->getMinimumShadowMapNearFarRatio());
if (n<min_n)
{
//OSG_NOTICE<<"Clamping n to eye point"<<std::endl;
n=min_n;
}
//n = min_n;
//n = 0.01;
//n = z_n;
double f = n+d;
double a = (f+n)/(f-n);
double b = -2.0*f*n/(f-n);
osg::Vec3d virtual_eye(0.0,-1.0-n, eye_ls.z());
osg::Matrixd lightView;
lightView.makeLookAt(virtual_eye, virtual_eye+lightdir, up);
#if 0
OSG_NOTICE<<"n = "<<n<<", f="<<f<<std::endl;
OSG_NOTICE<<"eye_ls = "<<eye_ls<<", virtual_eye="<<virtual_eye<<std::endl;
OSG_NOTICE<<"frustum.eyes="<<frustum.eye<<std::endl;
#endif
double min_x_ratio = 0.0;
double max_x_ratio = 0.0;
double min_z_ratio = dbl_max;
double max_z_ratio = -dbl_max;
min_x_ratio = convexHull.valid() ? convexHull.minRatio(virtual_eye,0) : -dbl_max;
max_x_ratio = convexHull.valid() ? convexHull.maxRatio(virtual_eye,0) : dbl_max;
//min_z_ratio = convexHull.minRatio(virtual_eye,2);
//max_z_ratio = convexHull.maxRatio(virtual_eye,2);
if (convexHullUnextended.valid())
{
min_z_ratio = convexHullUnextended.minRatio(virtual_eye, 2);
max_z_ratio = convexHullUnextended.maxRatio(virtual_eye, 2);
}
#if 0
OSG_NOTICE<<"convexHull min_x_ratio = "<<min_x_ratio<<std::endl;
OSG_NOTICE<<"convexHull max_x_ratio = "<<max_x_ratio<<std::endl;
OSG_NOTICE<<"convexHull min_z_ratio = "<<min_z_ratio<<std::endl;
OSG_NOTICE<<"convexHull max_z_ratio = "<<max_z_ratio<<std::endl;
#endif
#if 1
if (renderStage)
{
#if 1
osg::ElapsedTime timer;
#endif
RenderLeafTraverser<RenderLeafBounds> rli;
rli.set(light_p, virtual_eye, n);
rli.traverse(renderStage);
if (rli.numRenderLeaf==0)
{
return false;
}
#if 0
OSG_NOTICE<<"Time for RenderLeafTraverser "<<timer.elapsedTime_m()<<"ms, number of render leaves "<<rli.numRenderLeaf<<std::endl;
OSG_NOTICE<<"scene bounds min_x="<<rli.min_x<<", max_x="<<rli.max_x<<std::endl;
OSG_NOTICE<<"scene bounds min_y="<<rli.min_y<<", max_y="<<rli.max_y<<std::endl;
OSG_NOTICE<<"scene bounds min_z="<<rli.min_z<<", max_z="<<rli.max_z<<std::endl;
OSG_NOTICE<<"min_x_ratio="<<rli.min_x_ratio<<", max_x_ratio="<<rli.max_x_ratio<<std::endl;
OSG_NOTICE<<"min_z_ratio="<<rli.min_z_ratio<<", max_z_ratio="<<rli.max_z_ratio<<std::endl;
#endif
if (rli.min_x_ratio>min_x_ratio) min_x_ratio = rli.min_x_ratio;
if (rli.max_x_ratio<max_x_ratio) max_x_ratio = rli.max_x_ratio;
if (min_z_ratio == dbl_max || rli.min_z_ratio > min_z_ratio)
min_z_ratio = rli.min_z_ratio;
if (max_z_ratio == -dbl_max || rli.max_z_ratio < max_z_ratio)
max_z_ratio = rli.max_z_ratio;
}
#endif
double best_x_ratio = osg::maximum(fabs(min_x_ratio),fabs(max_x_ratio));
double best_z_ratio = osg::maximum(fabs(min_z_ratio),fabs(max_z_ratio));
//best_z_ratio = osg::maximum(1.0, best_z_ratio);
#if 0
OSG_NOTICE<<"min_x_ratio = "<<min_x_ratio<<std::endl;
OSG_NOTICE<<"max_x_ratio = "<<max_x_ratio<<std::endl;
OSG_NOTICE<<"best_x_ratio = "<<best_x_ratio<<std::endl;
OSG_NOTICE<<"min_z_ratio = "<<min_z_ratio<<std::endl;
OSG_NOTICE<<"max_z_ratio = "<<max_z_ratio<<std::endl;
OSG_NOTICE<<"best_z_ratio = "<<best_z_ratio<<std::endl;
#endif
//best_z_ratio *= 10.0;
osg::Matrixd lightPerspective( 1.0/best_x_ratio, 0.0, 0.0, 0.0,
0.0, a, 0.0, 1.0,
0.0, 0.0, 1.0/best_z_ratio, 0.0,
0.0, b, 0.0, 0.0 );
osg::Matrixd light_persp = light_p * lightView * lightPerspective;
#if 0
OSG_NOTICE<<"light_p = "<<light_p<<std::endl;
OSG_NOTICE<<"lightView = "<<lightView<<std::endl;
OSG_NOTICE<<"lightPerspective = "<<lightPerspective<<std::endl;
OSG_NOTICE<<"light_persp result = "<<light_persp<<std::endl;
#endif
camera->setProjectionMatrix(light_persp);
return true;
}
void MWShadowTechnique::assignShadowStateSettings(osgUtil::CullVisitor& cv, osg::Camera* camera, unsigned int sm_i, Uniforms& uniforms)
{
osg::Matrix inverseViewMatrix = osg::Matrix::inverse(*cv.getModelViewMatrix());
osg::Matrix shadowSpaceMatrix = inverseViewMatrix *
camera->getViewMatrix() *
camera->getProjectionMatrix() *
osg::Matrix::translate(1.0,1.0,1.0) *
osg::Matrix::scale(0.5,0.5,0.5);
std::string shadowSpaceUniformName = "shadowSpaceMatrix" + std::to_string(sm_i);
osg::ref_ptr<osg::Uniform> shadowSpaceUniform;
for (const auto & uniform : uniforms)
{
if (uniform->getName() == shadowSpaceUniformName)
{
shadowSpaceUniform = uniform;
break;
}
}
if (!shadowSpaceUniform)
{
shadowSpaceUniform = new osg::Uniform(osg::Uniform::FLOAT_MAT4, shadowSpaceUniformName);
uniforms.push_back(shadowSpaceUniform);
}
shadowSpaceUniform->set(shadowSpaceMatrix);
}
void SceneUtil::MWShadowTechnique::assignValidRegionSettings(osgUtil::CullVisitor & cv, osg::Camera* camera, unsigned int sm_i, Uniforms & uniforms)
{
osg::Matrix validRegionMatrix = osg::Matrix::inverse(*cv.getModelViewMatrix()) * camera->getViewMatrix() * camera->getProjectionMatrix();
std::string validRegionUniformName = "validRegionMatrix" + std::to_string(sm_i);
osg::ref_ptr<osg::Uniform> validRegionUniform;
for (const auto & uniform : uniforms)
{
if (uniform->getName() == validRegionUniformName)
{
validRegionUniform = uniform;
break;
}
}
if (!validRegionUniform)
{
validRegionUniform = new osg::Uniform(osg::Uniform::FLOAT_MAT4, validRegionUniformName);
uniforms.push_back(validRegionUniform);
}
validRegionUniform->set(validRegionMatrix);
}
void MWShadowTechnique::cullShadowReceivingScene(osgUtil::CullVisitor* cv) const
{
OSG_INFO<<"cullShadowReceivingScene()"<<std::endl;
// record the traversal mask on entry so we can reapply it later.
unsigned int traversalMask = cv->getTraversalMask();
cv->setTraversalMask( traversalMask & _shadowedScene->getShadowSettings()->getReceivesShadowTraversalMask() );
_shadowedScene->osg::Group::traverse(*cv);
cv->setTraversalMask( traversalMask );
return;
}
void MWShadowTechnique::cullShadowCastingScene(osgUtil::CullVisitor* cv, osg::Camera* camera) const
{
OSG_INFO<<"cullShadowCastingScene()"<<std::endl;
// record the traversal mask on entry so we can reapply it later.
unsigned int traversalMask = cv->getTraversalMask();
cv->setTraversalMask( traversalMask & _shadowedScene->getShadowSettings()->getCastsShadowTraversalMask() );
if (camera) camera->accept(*cv);
cv->setTraversalMask( traversalMask );
return;
}
osg::StateSet* MWShadowTechnique::prepareStateSetForRenderingShadow(ViewDependentData& vdd, unsigned int traversalNumber) const
{
OSG_INFO<<" prepareStateSetForRenderingShadow() "<<vdd.getStateSet(traversalNumber)<<std::endl;
osg::ref_ptr<osg::StateSet> stateset = vdd.getStateSet(traversalNumber);
stateset->clear();
stateset->setTextureAttributeAndModes(0, _fallbackBaseTexture.get(), osg::StateAttribute::ON);
for(const auto& uniform : _uniforms[traversalNumber % 2])
{
OSG_INFO<<"addUniform("<<uniform->getName()<<")"<<std::endl;
stateset->addUniform(uniform);
}
for(const auto& uniform : vdd._uniforms[traversalNumber % 2])
{
OSG_INFO<<"addUniform("<<uniform->getName()<<")"<<std::endl;
stateset->addUniform(uniform);
}
if (_program.valid())
{
stateset->setAttribute(_program.get());
}
LightDataList& pll = vdd.getLightDataList();
for(LightDataList::iterator itr = pll.begin();
itr != pll.end();
++itr)
{
// 3. create per light/per shadow map division of lightspace/frustum
// create a list of light/shadow map data structures
LightData& pl = (**itr);
// if no texture units have been activated for this light then no shadow state required.
if (pl.textureUnits.empty()) continue;
for(LightData::ActiveTextureUnits::iterator atu_itr = pl.textureUnits.begin();
atu_itr != pl.textureUnits.end();
++atu_itr)
{
OSG_INFO<<" Need to assign state for "<<*atu_itr<<std::endl;
}
}
const ShadowSettings* settings = getShadowedScene()->getShadowSettings();
unsigned int shadowMapModeValue = settings->getUseOverrideForShadowMapTexture() ?
osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE :
osg::StateAttribute::ON;
ShadowDataList& sdl = vdd.getShadowDataList();
for(ShadowDataList::iterator itr = sdl.begin();
itr != sdl.end();
++itr)
{
// 3. create per light/per shadow map division of lightspace/frustum
// create a list of light/shadow map data structures
ShadowData& sd = (**itr);
OSG_INFO<<" ShadowData for "<<sd._textureUnit<<std::endl;
stateset->setTextureAttribute(sd._textureUnit, sd._texture.get(), shadowMapModeValue);
}
return stateset;
}
void MWShadowTechnique::resizeGLObjectBuffers(unsigned int /*maxSize*/)
{
// the way that ViewDependentData is mapped shouldn't
}
void MWShadowTechnique::releaseGLObjects(osg::State* state) const
{
std::lock_guard<std::mutex> lock(_viewDependentDataMapMutex);
for(ViewDependentDataMap::const_iterator itr = _viewDependentDataMap.begin();
itr != _viewDependentDataMap.end();
++itr)
{
ViewDependentData* vdd = itr->second.get();
if (vdd)
{
vdd->releaseGLObjects(state);
}
}
if (_debugHud)
_debugHud->releaseGLObjects(state);
}
class DoubleBufferCallback : public osg::Callback
{
public:
DoubleBufferCallback(osg::NodeList &children) : mChildren(children) {}
bool run(osg::Object* node, osg::Object* visitor) override
{
// We can't use a static cast as NodeVisitor virtually inherits from Object
osg::ref_ptr<osg::NodeVisitor> nodeVisitor = visitor->asNodeVisitor();
unsigned int traversalNumber = nodeVisitor->getTraversalNumber();
mChildren[traversalNumber % 2]->accept(*nodeVisitor);
return true;
}
protected:
osg::NodeList mChildren;
};
SceneUtil::MWShadowTechnique::DebugHUD::DebugHUD(int numberOfShadowMapsPerLight) : mDebugProgram(new osg::Program)
{
osg::ref_ptr<osg::Shader> vertexShader = new osg::Shader(osg::Shader::VERTEX, debugVertexShaderSource);
mDebugProgram->addShader(vertexShader);
osg::ref_ptr<osg::Shader> fragmentShader = new osg::Shader(osg::Shader::FRAGMENT, debugFragmentShaderSource);
mDebugProgram->addShader(fragmentShader);
osg::ref_ptr<osg::Program> frustumProgram = new osg::Program;
vertexShader = new osg::Shader(osg::Shader::VERTEX, debugFrustumVertexShaderSource);
frustumProgram->addShader(vertexShader);
fragmentShader = new osg::Shader(osg::Shader::FRAGMENT, debugFrustumFragmentShaderSource);
frustumProgram->addShader(fragmentShader);
for (auto& frustumGeometry : mFrustumGeometries)
{
frustumGeometry = new osg::Geometry();
frustumGeometry->setCullingActive(false);
frustumGeometry->getOrCreateStateSet()->setAttributeAndModes(frustumProgram, osg::StateAttribute::ON);
}
osg::ref_ptr<osg::DrawElementsUShort> frustumDrawElements = new osg::DrawElementsUShort(osg::PrimitiveSet::LINE_STRIP);
for (auto & geom : mFrustumGeometries)
geom->addPrimitiveSet(frustumDrawElements);
frustumDrawElements->push_back(0);
frustumDrawElements->push_back(1);
frustumDrawElements->push_back(2);
frustumDrawElements->push_back(3);
frustumDrawElements->push_back(0);
frustumDrawElements->push_back(4);
frustumDrawElements->push_back(5);
frustumDrawElements->push_back(6);
frustumDrawElements->push_back(7);
frustumDrawElements->push_back(4);
frustumDrawElements = new osg::DrawElementsUShort(osg::PrimitiveSet::LINES);
for (auto & geom : mFrustumGeometries)
geom->addPrimitiveSet(frustumDrawElements);
frustumDrawElements->push_back(1);
frustumDrawElements->push_back(5);
frustumDrawElements->push_back(2);
frustumDrawElements->push_back(6);
frustumDrawElements->push_back(3);
frustumDrawElements->push_back(7);
for (int i = 0; i < numberOfShadowMapsPerLight; ++i)
addAnotherShadowMap();
}
void SceneUtil::MWShadowTechnique::DebugHUD::draw(osg::ref_ptr<osg::Texture2D> texture, unsigned int shadowMapNumber, const osg::Matrixd &matrix, osgUtil::CullVisitor& cv)
{
// It might be possible to change shadow settings at runtime
if (shadowMapNumber > mDebugCameras.size())
addAnotherShadowMap();
osg::ref_ptr<osg::StateSet> stateSet = new osg::StateSet();
stateSet->setTextureAttribute(sDebugTextureUnit, texture, osg::StateAttribute::ON);
auto frustumUniform = mFrustumUniforms[cv.getTraversalNumber() % 2][shadowMapNumber];
frustumUniform->set(matrix);
stateSet->addUniform(frustumUniform);
// Some of these calls may be superfluous.
unsigned int traversalMask = cv.getTraversalMask();
cv.setTraversalMask(mDebugGeometry[shadowMapNumber]->getNodeMask());
cv.pushStateSet(stateSet);
mDebugCameras[shadowMapNumber]->accept(cv);
cv.popStateSet();
cv.setTraversalMask(traversalMask);
}
void SceneUtil::MWShadowTechnique::DebugHUD::releaseGLObjects(osg::State* state) const
{
for (auto const& camera : mDebugCameras)
camera->releaseGLObjects(state);
mDebugProgram->releaseGLObjects(state);
for (auto const& node : mDebugGeometry)
node->releaseGLObjects(state);
for (auto const& node : mFrustumTransforms)
node->releaseGLObjects(state);
for (auto const& node : mFrustumGeometries)
node->releaseGLObjects(state);
}
void SceneUtil::MWShadowTechnique::DebugHUD::setFrustumVertices(osg::ref_ptr<osg::Vec3Array> vertices, unsigned int traversalNumber)
{
mFrustumGeometries[traversalNumber % 2]->setVertexArray(vertices);
}
void SceneUtil::MWShadowTechnique::DebugHUD::addAnotherShadowMap()
{
unsigned int shadowMapNumber = mDebugCameras.size();
mDebugCameras.push_back(new osg::Camera);
mDebugCameras[shadowMapNumber]->setViewport(200 * shadowMapNumber, 0, 200, 200);
mDebugCameras[shadowMapNumber]->setRenderOrder(osg::Camera::POST_RENDER);
mDebugCameras[shadowMapNumber]->setClearColor(osg::Vec4(1.0, 1.0, 0.0, 1.0));
mDebugCameras[shadowMapNumber]->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
mDebugGeometry.emplace_back(osg::createTexturedQuadGeometry(osg::Vec3(-1, -1, 0), osg::Vec3(2, 0, 0), osg::Vec3(0, 2, 0)));
mDebugGeometry[shadowMapNumber]->setCullingActive(false);
mDebugCameras[shadowMapNumber]->addChild(mDebugGeometry[shadowMapNumber]);
osg::ref_ptr<osg::StateSet> stateSet = mDebugGeometry[shadowMapNumber]->getOrCreateStateSet();
stateSet->setAttributeAndModes(mDebugProgram, osg::StateAttribute::ON);
osg::ref_ptr<osg::Uniform> textureUniform = new osg::Uniform("texture", sDebugTextureUnit);
//textureUniform->setType(osg::Uniform::SAMPLER_2D);
stateSet->addUniform(textureUniform.get());
mFrustumTransforms.push_back(new osg::Group);
osg::NodeList frustumGeometryNodeList(mFrustumGeometries.cbegin(), mFrustumGeometries.cend());
mFrustumTransforms[shadowMapNumber]->setCullCallback(new DoubleBufferCallback(frustumGeometryNodeList));
mFrustumTransforms[shadowMapNumber]->setCullingActive(false);
mDebugCameras[shadowMapNumber]->addChild(mFrustumTransforms[shadowMapNumber]);
for(auto& uniformVector : mFrustumUniforms)
uniformVector.push_back(new osg::Uniform(osg::Uniform::FLOAT_MAT4, "transform"));
}
osg::ref_ptr<osg::StateSet> SceneUtil::MWShadowTechnique::getOrCreateShadowsBinStateSet()
{
if (_shadowsBinStateSet == nullptr)
{
if (_shadowsBin == nullptr)
{
_shadowsBin = new ShadowsBin(_castingPrograms);
osgUtil::RenderBin::addRenderBinPrototype(_shadowsBinName, _shadowsBin);
}
_shadowsBinStateSet = new osg::StateSet;
_shadowsBinStateSet->setRenderBinDetails(osg::StateSet::OPAQUE_BIN, _shadowsBinName, osg::StateSet::OVERRIDE_PROTECTED_RENDERBIN_DETAILS);
}
return _shadowsBinStateSet;
}
// clang-format on
|