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
|
/*
* Copyright 2013-2014 Dario Manesku. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include <string>
#include <vector>
#include <map>
#include <tinystl/allocator.h>
#include <tinystl/unordered_map.h>
namespace stl = tinystl;
#include "common.h"
#include "bgfx_utils.h"
#include <bgfx/bgfx.h>
#include <bx/timer.h>
#include <bx/allocator.h>
#include <bx/hash.h>
#include <bx/simd_t.h>
#include <bx/math.h>
#include <bx/file.h>
#include "entry/entry.h"
#include "camera.h"
#include "imgui/imgui.h"
namespace bgfx
{
int32_t read(bx::ReaderI* _reader, bgfx::VertexLayout& _layout, bx::Error* _err = NULL);
}
namespace
{
#define SV_USE_SIMD 1
#define MAX_INSTANCE_COUNT 25
#define MAX_LIGHTS_COUNT 5
#define VIEWID_RANGE1_PASS0 1
#define VIEWID_RANGE1_RT_PASS1 2
#define VIEWID_RANGE15_PASS2 3
#define VIEWID_RANGE1_PASS3 20
struct PosNormalTexcoordVertex
{
float m_x;
float m_y;
float m_z;
uint32_t m_normal;
float m_u;
float m_v;
static void init()
{
ms_layout
.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
.end();
}
static bgfx::VertexLayout ms_layout;
};
bgfx::VertexLayout PosNormalTexcoordVertex::ms_layout;
static const float s_texcoord = 50.0f;
static PosNormalTexcoordVertex s_hplaneVertices[] =
{
{ -1.0f, 0.0f, 1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f), s_texcoord, s_texcoord },
{ 1.0f, 0.0f, 1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f), s_texcoord, 0.0f },
{ -1.0f, 0.0f, -1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f), 0.0f, s_texcoord },
{ 1.0f, 0.0f, -1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f), 0.0f, 0.0f },
};
static PosNormalTexcoordVertex s_vplaneVertices[] =
{
{ -1.0f, 1.0f, 0.0f, encodeNormalRgba8(0.0f, 0.0f, -1.0f), 1.0f, 1.0f },
{ 1.0f, 1.0f, 0.0f, encodeNormalRgba8(0.0f, 0.0f, -1.0f), 1.0f, 0.0f },
{ -1.0f, -1.0f, 0.0f, encodeNormalRgba8(0.0f, 0.0f, -1.0f), 0.0f, 1.0f },
{ 1.0f, -1.0f, 0.0f, encodeNormalRgba8(0.0f, 0.0f, -1.0f), 0.0f, 0.0f },
};
static const uint16_t s_planeIndices[] =
{
0, 1, 2,
1, 3, 2,
};
static bool s_oglNdc = false;
static float s_texelHalf = 0.0f;
static uint32_t s_viewMask = 0;
static bgfx::UniformHandle s_texColor;
static bgfx::UniformHandle s_texStencil;
static bgfx::FrameBufferHandle s_stencilFb;
void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba, float _depth, uint8_t _stencil)
{
for (uint32_t view = 0, viewMask = _viewMask; 0 != viewMask; viewMask >>= 1, view += 1 )
{
const uint32_t ntz = bx::uint32_cnttz(viewMask);
viewMask >>= ntz;
view += ntz;
bgfx::setViewClear( (uint8_t)view, _flags, _rgba, _depth, _stencil);
}
}
void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _proj)
{
for (uint32_t view = 0, viewMask = _viewMask; 0 != viewMask; viewMask >>= 1, view += 1 )
{
const uint32_t ntz = bx::uint32_cnttz(viewMask);
viewMask >>= ntz;
view += ntz;
bgfx::setViewTransform( (uint8_t)view, _view, _proj);
}
}
void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
{
for (uint32_t view = 0, viewMask = _viewMask; 0 != viewMask; viewMask >>= 1, view += 1 )
{
const uint32_t ntz = bx::uint32_cnttz(viewMask);
viewMask >>= ntz;
view += ntz;
bgfx::setViewRect( (uint8_t)view, _x, _y, _width, _height);
}
}
void mtxBillboard(
float* _result
, const float* _view
, const float* _pos
, const float* _scale
)
{
_result[ 0] = _view[0] * _scale[0];
_result[ 1] = _view[4] * _scale[0];
_result[ 2] = _view[8] * _scale[0];
_result[ 3] = 0.0f;
_result[ 4] = _view[1] * _scale[1];
_result[ 5] = _view[5] * _scale[1];
_result[ 6] = _view[9] * _scale[1];
_result[ 7] = 0.0f;
_result[ 8] = _view[2] * _scale[2];
_result[ 9] = _view[6] * _scale[2];
_result[10] = _view[10] * _scale[2];
_result[11] = 0.0f;
_result[12] = _pos[0];
_result[13] = _pos[1];
_result[14] = _pos[2];
_result[15] = 1.0f;
}
void planeNormal(
float* _result
, const float* _v0
, const float* _v1
, const float* _v2
)
{
const bx::Vec3 v0 = bx::load<bx::Vec3>(_v0);
const bx::Vec3 v1 = bx::load<bx::Vec3>(_v1);
const bx::Vec3 v2 = bx::load<bx::Vec3>(_v2);
const bx::Vec3 vec0 = bx::sub(v1, v0);
const bx::Vec3 vec1 = bx::sub(v2, v1);
const bx::Vec3 cross = bx::cross(vec0, vec1);
bx::store(_result, bx::normalize(cross) );
_result[3] = -bx::dot(bx::load<bx::Vec3>(_result), bx::load<bx::Vec3>(_v0) );
}
struct Uniforms
{
void init()
{
m_params.m_ambientPass = 1.0f;
m_params.m_lightingPass = 1.0f;
m_params.m_texelHalf = 0.0f;
m_ambient[0] = 0.05f;
m_ambient[1] = 0.05f;
m_ambient[2] = 0.05f;
m_ambient[3] = 0.0f; //unused
m_diffuse[0] = 0.8f;
m_diffuse[1] = 0.8f;
m_diffuse[2] = 0.8f;
m_diffuse[3] = 0.0f; //unused
m_specular_shininess[0] = 1.0f;
m_specular_shininess[1] = 1.0f;
m_specular_shininess[2] = 1.0f;
m_specular_shininess[3] = 25.0f; //shininess
m_fog[0] = 0.0f; //color
m_fog[1] = 0.0f;
m_fog[2] = 0.0f;
m_fog[3] = 0.0055f; //density
m_color[0] = 1.0f;
m_color[1] = 1.0f;
m_color[2] = 1.0f;
m_color[3] = 1.0f;
m_time = 0.0f;
m_lightPosRadius[0] = 0.0f;
m_lightPosRadius[1] = 0.0f;
m_lightPosRadius[2] = 0.0f;
m_lightPosRadius[3] = 1.0f;
m_lightRgbInnerR[0] = 0.0f;
m_lightRgbInnerR[1] = 0.0f;
m_lightRgbInnerR[2] = 0.0f;
m_lightRgbInnerR[3] = 1.0f;
m_virtualLightPos_extrusionDist[0] = 0.0f;
m_virtualLightPos_extrusionDist[1] = 0.0f;
m_virtualLightPos_extrusionDist[2] = 0.0f;
m_virtualLightPos_extrusionDist[3] = 100.0f;
u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
u_svparams = bgfx::createUniform("u_svparams", bgfx::UniformType::Vec4);
u_ambient = bgfx::createUniform("u_ambient", bgfx::UniformType::Vec4);
u_diffuse = bgfx::createUniform("u_diffuse", bgfx::UniformType::Vec4);
u_specular_shininess = bgfx::createUniform("u_specular_shininess", bgfx::UniformType::Vec4);
u_fog = bgfx::createUniform("u_fog", bgfx::UniformType::Vec4);
u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4);
u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Vec4);
u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Vec4);
u_virtualLightPos_extrusionDist = bgfx::createUniform("u_virtualLightPos_extrusionDist", bgfx::UniformType::Vec4);
}
//call this once at initialization
void submitConstUniforms()
{
bgfx::setUniform(u_ambient, &m_ambient);
bgfx::setUniform(u_diffuse, &m_diffuse);
bgfx::setUniform(u_specular_shininess, &m_specular_shininess);
bgfx::setUniform(u_fog, &m_fog);
}
//call this before each draw call
void submitPerDrawUniforms()
{
bgfx::setUniform(u_params, &m_params);
bgfx::setUniform(u_svparams, &m_svparams);
bgfx::setUniform(u_color, &m_color);
bgfx::setUniform(u_lightPosRadius, &m_lightPosRadius);
bgfx::setUniform(u_lightRgbInnerR, &m_lightRgbInnerR);
bgfx::setUniform(u_virtualLightPos_extrusionDist, &m_virtualLightPos_extrusionDist);
}
void destroy()
{
bgfx::destroy(u_params);
bgfx::destroy(u_svparams);
bgfx::destroy(u_ambient);
bgfx::destroy(u_diffuse);
bgfx::destroy(u_specular_shininess);
bgfx::destroy(u_fog);
bgfx::destroy(u_color);
bgfx::destroy(u_lightPosRadius);
bgfx::destroy(u_lightRgbInnerR);
bgfx::destroy(u_virtualLightPos_extrusionDist);
}
struct Params
{
float m_ambientPass;
float m_lightingPass;
float m_texelHalf;
float m_unused00;
};
struct SvParams
{
float m_useStencilTex;
float m_dfail;
float m_unused10;
float m_unused11;
};
Params m_params;
SvParams m_svparams;
float m_ambient[4];
float m_diffuse[4];
float m_specular_shininess[4];
float m_fog[4];
float m_color[4];
float m_time;
float m_lightPosRadius[4];
float m_lightRgbInnerR[4];
float m_virtualLightPos_extrusionDist[4];
/**
* u_params.x - u_ambientPass
* u_params.y - u_lightingPass
* u_params.z - u_texelHalf
* u_params.w - unused
* u_svparams.x - u_useStencilTex
* u_svparams.y - u_dfail
* u_svparams.z - unused
* u_svparams.w - unused
*/
bgfx::UniformHandle u_params;
bgfx::UniformHandle u_svparams;
bgfx::UniformHandle u_ambient;
bgfx::UniformHandle u_diffuse;
bgfx::UniformHandle u_specular_shininess;
bgfx::UniformHandle u_fog;
bgfx::UniformHandle u_color;
bgfx::UniformHandle u_lightPosRadius;
bgfx::UniformHandle u_lightRgbInnerR;
bgfx::UniformHandle u_virtualLightPos_extrusionDist;
};
static Uniforms s_uniforms;
struct RenderState
{
enum Enum
{
ShadowVolume_UsingStencilTexture_DrawAmbient = 0,
ShadowVolume_UsingStencilTexture_BuildDepth,
ShadowVolume_UsingStencilTexture_CraftStencil_DepthPass,
ShadowVolume_UsingStencilTexture_CraftStencil_DepthFail,
ShadowVolume_UsingStencilTexture_DrawDiffuse,
ShadowVolume_UsingStencilBuffer_DrawAmbient,
ShadowVolume_UsingStencilBuffer_CraftStencil_DepthPass,
ShadowVolume_UsingStencilBuffer_CraftStencil_DepthFail,
ShadowVolume_UsingStencilBuffer_DrawDiffuse,
Custom_Default,
Custom_BlendLightTexture,
Custom_DrawPlaneBottom,
Custom_DrawShadowVolume_Lines,
Count
};
uint64_t m_state;
uint32_t m_blendFactorRgba;
uint32_t m_fstencil;
uint32_t m_bstencil;
};
static void setRenderState(const RenderState& _renderState)
{
bgfx::setStencil(_renderState.m_fstencil, _renderState.m_bstencil);
bgfx::setState(_renderState.m_state, _renderState.m_blendFactorRgba);
}
static RenderState s_renderStates[RenderState::Count] =
{
{ // ShadowVolume_UsingStencilTexture_DrawAmbient
BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_WRITE_Z
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_CULL_CCW
| BGFX_STATE_MSAA
, UINT32_MAX
, BGFX_STENCIL_NONE
, BGFX_STENCIL_NONE
},
{ // ShadowVolume_UsingStencilTexture_BuildDepth
BGFX_STATE_WRITE_Z
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_CULL_CCW
| BGFX_STATE_MSAA
, UINT32_MAX
, BGFX_STENCIL_NONE
, BGFX_STENCIL_NONE
},
{ // ShadowVolume_UsingStencilTexture_CraftStencil_DepthPass
BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE)
| BGFX_STATE_DEPTH_TEST_LEQUAL
| BGFX_STATE_MSAA
, UINT32_MAX
, BGFX_STENCIL_NONE
, BGFX_STENCIL_NONE
},
{ // ShadowVolume_UsingStencilTexture_CraftStencil_DepthFail
BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE)
| BGFX_STATE_DEPTH_TEST_GEQUAL
| BGFX_STATE_MSAA
, UINT32_MAX
, BGFX_STENCIL_NONE
, BGFX_STENCIL_NONE
},
{ // ShadowVolume_UsingStencilTexture_DrawDiffuse
BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE)
| BGFX_STATE_WRITE_Z
| BGFX_STATE_DEPTH_TEST_EQUAL
| BGFX_STATE_CULL_CCW
| BGFX_STATE_MSAA
, UINT32_MAX
, BGFX_STENCIL_NONE
, BGFX_STENCIL_NONE
},
{ // ShadowVolume_UsingStencilBuffer_DrawAmbient
BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_WRITE_Z
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_CULL_CCW
| BGFX_STATE_MSAA
, UINT32_MAX
, BGFX_STENCIL_NONE
, BGFX_STENCIL_NONE
},
{ // ShadowVolume_UsingStencilBuffer_CraftStencil_DepthPass
BGFX_STATE_DEPTH_TEST_LEQUAL
| BGFX_STATE_MSAA
, UINT32_MAX
, BGFX_STENCIL_TEST_ALWAYS
| BGFX_STENCIL_FUNC_REF(1)
| BGFX_STENCIL_FUNC_RMASK(0xff)
| BGFX_STENCIL_OP_FAIL_S_KEEP
| BGFX_STENCIL_OP_FAIL_Z_KEEP
| BGFX_STENCIL_OP_PASS_Z_DECR
, BGFX_STENCIL_TEST_ALWAYS
| BGFX_STENCIL_FUNC_REF(1)
| BGFX_STENCIL_FUNC_RMASK(0xff)
| BGFX_STENCIL_OP_FAIL_S_KEEP
| BGFX_STENCIL_OP_FAIL_Z_KEEP
| BGFX_STENCIL_OP_PASS_Z_INCR
},
{ // ShadowVolume_UsingStencilBuffer_CraftStencil_DepthFail
BGFX_STATE_DEPTH_TEST_LEQUAL
| BGFX_STATE_MSAA
, UINT32_MAX
, BGFX_STENCIL_TEST_ALWAYS
| BGFX_STENCIL_FUNC_REF(1)
| BGFX_STENCIL_FUNC_RMASK(0xff)
| BGFX_STENCIL_OP_FAIL_S_KEEP
| BGFX_STENCIL_OP_FAIL_Z_INCR
| BGFX_STENCIL_OP_PASS_Z_KEEP
, BGFX_STENCIL_TEST_ALWAYS
| BGFX_STENCIL_FUNC_REF(1)
| BGFX_STENCIL_FUNC_RMASK(0xff)
| BGFX_STENCIL_OP_FAIL_S_KEEP
| BGFX_STENCIL_OP_FAIL_Z_DECR
| BGFX_STENCIL_OP_PASS_Z_KEEP
},
{ // ShadowVolume_UsingStencilBuffer_DrawDiffuse
BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE)
| BGFX_STATE_DEPTH_TEST_EQUAL
| BGFX_STATE_CULL_CCW
| BGFX_STATE_MSAA
, UINT32_MAX
, BGFX_STENCIL_TEST_EQUAL
| BGFX_STENCIL_FUNC_REF(0)
| BGFX_STENCIL_FUNC_RMASK(0xff)
| BGFX_STENCIL_OP_FAIL_S_KEEP
| BGFX_STENCIL_OP_FAIL_Z_KEEP
| BGFX_STENCIL_OP_PASS_Z_KEEP
, BGFX_STENCIL_NONE
},
{ // Custom_Default
BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_WRITE_Z
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_CULL_CCW
| BGFX_STATE_MSAA
, UINT32_MAX
, BGFX_STENCIL_NONE
, BGFX_STENCIL_NONE
},
{ // Custom_BlendLightTexture
BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_WRITE_Z
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_COLOR, BGFX_STATE_BLEND_INV_SRC_COLOR)
| BGFX_STATE_CULL_CCW
| BGFX_STATE_MSAA
, UINT32_MAX
, BGFX_STENCIL_NONE
, BGFX_STENCIL_NONE
},
{ // Custom_DrawPlaneBottom
BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_WRITE_Z
| BGFX_STATE_CULL_CW
| BGFX_STATE_MSAA
, UINT32_MAX
, BGFX_STENCIL_NONE
, BGFX_STENCIL_NONE
},
{ // Custom_DrawShadowVolume_Lines
BGFX_STATE_WRITE_RGB
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_FACTOR, BGFX_STATE_BLEND_SRC_ALPHA)
| BGFX_STATE_PT_LINES
| BGFX_STATE_MSAA
, 0x0f0f0fff
, BGFX_STENCIL_NONE
, BGFX_STENCIL_NONE
}
};
struct ViewState
{
ViewState(uint32_t _width = 0, uint32_t _height = 0)
: m_width(_width)
, m_height(_height)
{
}
uint32_t m_width;
uint32_t m_height;
float m_view[16];
float m_proj[16];
};
struct ClearValues
{
uint32_t m_clearRgba;
float m_clearDepth;
uint8_t m_clearStencil;
};
void submit(bgfx::ViewId _id, bgfx::ProgramHandle _handle, int32_t _depth = 0)
{
bgfx::submit(_id, _handle, _depth);
// Keep track of submited view ids.
s_viewMask |= 1 << _id;
}
void touch(bgfx::ViewId _id)
{
bgfx::ProgramHandle handle = BGFX_INVALID_HANDLE;
::submit(_id, handle);
}
struct Face
{
uint16_t m_i[3];
float m_plane[4];
};
typedef std::vector<Face> FaceArray;
struct Edge
{
bool m_faceReverseOrder[2];
uint8_t m_faceIndex;
uint16_t m_i0, m_i1;
};
struct Plane
{
float m_plane[4];
};
struct HalfEdge
{
#define INVALID_EDGE_INDEX UINT16_MAX
uint16_t m_secondIndex;
bool m_marked;
};
struct HalfEdges
{
HalfEdges()
: m_data()
, m_offsets()
, m_endPtr()
{
}
void init(uint16_t* _indices, uint32_t _numIndices)
{
m_data = (HalfEdge*)malloc(2 * _numIndices * sizeof(HalfEdge) );
stl::unordered_map<uint16_t, std::vector<uint16_t> > edges;
for (uint32_t ii = 0; ii < _numIndices; ii+=3)
{
uint16_t idx0 = _indices[ii];
uint16_t idx1 = _indices[ii+1];
uint16_t idx2 = _indices[ii+2];
edges[idx0].push_back(idx1);
edges[idx1].push_back(idx2);
edges[idx2].push_back(idx0);
}
uint32_t numRows = (uint32_t)edges.size();
m_offsets = (uint32_t*)malloc(numRows * sizeof(uint32_t) );
HalfEdge* he = m_data;
for (uint16_t ii = 0; ii < numRows; ++ii)
{
m_offsets[ii] = uint32_t(he - m_data);
std::vector<uint16_t>& row = edges[ii];
for (uint32_t jj = 0, size = (uint32_t)row.size(); jj < size; ++jj)
{
he->m_secondIndex = row[jj];
he->m_marked = false;
++he;
}
he->m_secondIndex = INVALID_EDGE_INDEX;
++he;
}
he->m_secondIndex = 0;
m_endPtr = he;
}
void destroy()
{
free(m_data);
m_data = NULL;
free(m_offsets);
m_offsets = NULL;
}
void mark(uint16_t _firstIndex, uint16_t _secondIndex)
{
HalfEdge* ptr = &m_data[m_offsets[_firstIndex]];
while (INVALID_EDGE_INDEX != ptr->m_secondIndex)
{
if (ptr->m_secondIndex == _secondIndex)
{
ptr->m_marked = true;
break;
}
++ptr;
}
}
bool unmark(uint16_t _firstIndex, uint16_t _secondIndex)
{
bool ret = false;
HalfEdge* ptr = &m_data[m_offsets[_firstIndex]];
while (INVALID_EDGE_INDEX != ptr->m_secondIndex)
{
if (ptr->m_secondIndex == _secondIndex && ptr->m_marked)
{
ptr->m_marked = false;
ret = true;
break;
}
++ptr;
}
return ret;
}
inline HalfEdge* begin() const
{
return m_data;
}
inline HalfEdge* end() const
{
return m_endPtr;
}
HalfEdge* m_data;
uint32_t* m_offsets;
HalfEdge* m_endPtr;
};
struct WeldedVertex
{
uint16_t m_v;
bool m_welded;
};
inline float sqLength(const float _a[3], const float _b[3])
{
const float xx = _a[0] - _b[0];
const float yy = _a[1] - _b[1];
const float zz = _a[2] - _b[2];
return xx*xx + yy*yy + zz*zz;
}
uint16_t weldVertices(WeldedVertex* _output, const bgfx::VertexLayout& _layout, const void* _data, uint16_t _num, float _epsilon)
{
const uint32_t hashSize = bx::uint32_nextpow2(_num);
const uint32_t hashMask = hashSize-1;
const float epsilonSq = _epsilon*_epsilon;
uint16_t numVertices = 0;
const uint32_t size = sizeof(uint16_t)*(hashSize + _num);
uint16_t* hashTable = (uint16_t*)alloca(size);
bx::memSet(hashTable, 0xff, size);
uint16_t* next = hashTable + hashSize;
for (uint16_t ii = 0; ii < _num; ++ii)
{
float pos[4];
vertexUnpack(pos, bgfx::Attrib::Position, _layout, _data, ii);
uint32_t hashValue = bx::hash<bx::HashMurmur2A>(pos, 3*sizeof(float) ) & hashMask;
uint16_t offset = hashTable[hashValue];
for (; UINT16_MAX != offset; offset = next[offset])
{
float test[4];
vertexUnpack(test, bgfx::Attrib::Position, _layout, _data, _output[offset].m_v);
if (sqLength(test, pos) < epsilonSq)
{
_output[ii].m_v = _output[offset].m_v;
_output[ii].m_welded = true;
break;
}
}
if (UINT16_MAX == offset)
{
_output[ii].m_v = ii;
_output[ii].m_welded = false;
next[ii] = hashTable[hashValue];
hashTable[hashValue] = ii;
numVertices++;
}
}
return numVertices;
}
struct Group
{
Group()
{
reset();
}
void reset()
{
m_vbh.idx = bgfx::kInvalidHandle;
m_ibh.idx = bgfx::kInvalidHandle;
m_numVertices = 0;
m_vertices = NULL;
m_numIndices = 0;
m_indices = NULL;
m_numEdges = 0;
m_edges = NULL;
m_edgePlanesUnalignedPtr = NULL;
m_prims.clear();
}
typedef struct { float f[6]; } f6_t;
struct EdgeAndPlane
{
EdgeAndPlane(uint16_t _i0, uint16_t _i1)
: m_faceIndex(0)
, m_i0(_i0)
, m_i1(_i1)
{
}
bool m_faceReverseOrder[2];
uint8_t m_faceIndex;
uint16_t m_i0, m_i1;
Plane m_plane[2];
};
void fillStructures(const bgfx::VertexLayout& _layout)
{
uint16_t stride = _layout.getStride();
m_faces.clear();
m_halfEdges.destroy();
//Init halfedges.
m_halfEdges.init(m_indices, m_numIndices);
//Init faces and edges.
m_faces.reserve(m_numIndices/3); //1 face = 3 indices
m_edges = (Edge*)malloc(m_numIndices * sizeof(Edge) ); //1 triangle = 3 indices = 3 edges.
m_edgePlanesUnalignedPtr = (Plane*)malloc(m_numIndices * sizeof(Plane) + 15);
m_edgePlanes = (Plane*)bx::alignPtr(m_edgePlanesUnalignedPtr, 0, 16);
typedef std::map<std::pair<uint16_t, uint16_t>, EdgeAndPlane> EdgeMap;
EdgeMap edgeMap;
//Get unique indices.
WeldedVertex* uniqueVertices = (WeldedVertex*)malloc(m_numVertices*sizeof(WeldedVertex) );
::weldVertices(uniqueVertices, _layout, m_vertices, m_numVertices, 0.0001f);
uint16_t* uniqueIndices = (uint16_t*)malloc(m_numIndices*sizeof(uint16_t) );
for (uint32_t ii = 0; ii < m_numIndices; ++ii)
{
uint16_t index = m_indices[ii];
if (uniqueVertices[index].m_welded)
{
uniqueIndices[ii] = uniqueVertices[index].m_v;
}
else
{
uniqueIndices[ii] = index;
}
}
free(uniqueVertices);
for (uint32_t ii = 0, size = m_numIndices/3; ii < size; ++ii)
{
const uint16_t* indices = &m_indices[ii*3];
uint16_t i0 = indices[0];
uint16_t i1 = indices[1];
uint16_t i2 = indices[2];
const float* v0 = (float*)&m_vertices[i0*stride];
const float* v1 = (float*)&m_vertices[i1*stride];
const float* v2 = (float*)&m_vertices[i2*stride];
float plane[4];
planeNormal(plane, v0, v2, v1);
Face face;
face.m_i[0] = i0;
face.m_i[1] = i1;
face.m_i[2] = i2;
bx::memCopy(face.m_plane, plane, 4*sizeof(float) );
m_faces.push_back(face);
//Use unique indices for EdgeMap.
const uint16_t* uindices = &uniqueIndices[ii*3];
i0 = uindices[0];
i1 = uindices[1];
i2 = uindices[2];
const uint16_t triangleEdge[3][2] =
{
{ i0, i1 },
{ i1, i2 },
{ i2, i0 },
};
for (uint8_t jj = 0; jj < 3; ++jj)
{
const uint16_t ui0 = triangleEdge[jj][0];
const uint16_t ui1 = triangleEdge[jj][1];
std::pair<uint16_t, uint16_t> key = std::make_pair(ui0, ui1);
std::pair<uint16_t, uint16_t> keyInv = std::make_pair(ui1, ui0);
EdgeMap::iterator iter = edgeMap.find(keyInv);
if (iter != edgeMap.end() )
{
EdgeAndPlane& ep = iter->second;
bx::memCopy(ep.m_plane[ep.m_faceIndex].m_plane, plane, 4*sizeof(float) );
ep.m_faceReverseOrder[ep.m_faceIndex] = true;
}
else
{
std::pair<EdgeMap::iterator, bool> result = edgeMap.insert(std::make_pair(key, EdgeAndPlane(ui0, ui1) ) );
EdgeAndPlane& ep = result.first->second;
bx::memCopy(ep.m_plane[ep.m_faceIndex].m_plane, plane, 4*sizeof(float) );
ep.m_faceReverseOrder[ep.m_faceIndex] = false;
ep.m_faceIndex++;
}
}
}
free(uniqueIndices);
uint32_t index = 0;
for (EdgeMap::const_iterator iter = edgeMap.begin(), end = edgeMap.end(); iter != end; ++iter)
{
Edge* edge = &m_edges[m_numEdges];
Plane* plane = &m_edgePlanes[index];
bx::memCopy(edge, iter->second.m_faceReverseOrder, sizeof(Edge) );
bx::memCopy(plane, iter->second.m_plane, 2 * sizeof(Plane) );
m_numEdges++;
index += 2;
}
}
void unload()
{
bgfx::destroy(m_vbh);
if (bgfx::kInvalidHandle != m_ibh.idx)
{
bgfx::destroy(m_ibh);
}
free(m_vertices);
m_vertices = NULL;
free(m_indices);
m_indices = NULL;
free(m_edges);
m_edges = NULL;
free(m_edgePlanesUnalignedPtr);
m_edgePlanesUnalignedPtr = NULL;
m_halfEdges.destroy();
}
bgfx::VertexBufferHandle m_vbh;
bgfx::IndexBufferHandle m_ibh;
uint16_t m_numVertices;
uint8_t* m_vertices;
uint32_t m_numIndices;
uint16_t* m_indices;
Sphere m_sphere;
Aabb m_aabb;
Obb m_obb;
PrimitiveArray m_prims;
uint32_t m_numEdges;
Edge* m_edges;
Plane* m_edgePlanesUnalignedPtr;
Plane* m_edgePlanes;
FaceArray m_faces;
HalfEdges m_halfEdges;
};
typedef std::vector<Group> GroupArray;
struct Mesh
{
void load(const void* _vertices, uint16_t _numVertices, const bgfx::VertexLayout _layout, const uint16_t* _indices, uint32_t _numIndices)
{
Group group;
const bgfx::Memory* mem;
uint32_t size;
//vertices
group.m_numVertices = _numVertices;
size = _numVertices*_layout.getStride();
group.m_vertices = (uint8_t*)malloc(size);
bx::memCopy(group.m_vertices, _vertices, size);
mem = bgfx::makeRef(group.m_vertices, size);
group.m_vbh = bgfx::createVertexBuffer(mem, _layout);
//indices
group.m_numIndices = _numIndices;
size = _numIndices*2;
group.m_indices = (uint16_t*)malloc(size);
bx::memCopy(group.m_indices, _indices, size);
mem = bgfx::makeRef(group.m_indices, size);
group.m_ibh = bgfx::createIndexBuffer(mem);
m_groups.push_back(group);
}
void load(const char* _filePath)
{
::Mesh* mesh = ::meshLoad(_filePath, true);
m_layout = mesh->m_layout;
uint16_t stride = m_layout.getStride();
for (::GroupArray::iterator it = mesh->m_groups.begin(), itEnd = mesh->m_groups.end(); it != itEnd; ++it)
{
Group group;
group.m_numVertices = it->m_numVertices;
const uint32_t vertexSize = group.m_numVertices*stride;
group.m_vertices = (uint8_t*)malloc(vertexSize);
bx::memCopy(group.m_vertices, it->m_vertices, vertexSize);
const bgfx::Memory* mem = bgfx::makeRef(group.m_vertices, vertexSize);
group.m_vbh = bgfx::createVertexBuffer(mem, m_layout);
group.m_numIndices = it->m_numIndices;
const uint32_t indexSize = 2 * group.m_numIndices;
group.m_indices = (uint16_t*)malloc(indexSize);
bx::memCopy(group.m_indices, it->m_indices, indexSize);
mem = bgfx::makeRef(group.m_indices, indexSize);
group.m_ibh = bgfx::createIndexBuffer(mem);
group.m_sphere = it->m_sphere;
group.m_aabb = it->m_aabb;
group.m_obb = it->m_obb;
group.m_prims = it->m_prims;
m_groups.push_back(group);
}
::meshUnload(mesh);
for (GroupArray::iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
{
it->fillStructures(m_layout);
}
}
void unload()
{
for (GroupArray::iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
{
it->unload();
}
m_groups.clear();
}
bgfx::VertexLayout m_layout;
GroupArray m_groups;
};
struct Model
{
Model()
{
m_program.idx = bgfx::kInvalidHandle;
m_texture.idx = bgfx::kInvalidHandle;
}
void load(const void* _vertices, uint16_t _numVertices, const bgfx::VertexLayout _layout, const uint16_t* _indices, uint32_t _numIndices)
{
m_mesh.load(_vertices, _numVertices, _layout, _indices, _numIndices);
}
void load(const char* _meshFilePath)
{
m_mesh.load(_meshFilePath);
}
void unload()
{
m_mesh.unload();
}
void submit(uint8_t _viewId, float* _mtx, const RenderState& _renderState)
{
for (GroupArray::const_iterator it = m_mesh.m_groups.begin(), itEnd = m_mesh.m_groups.end(); it != itEnd; ++it)
{
const Group& group = *it;
// Set uniforms
s_uniforms.submitPerDrawUniforms();
// Set transform
bgfx::setTransform(_mtx);
// Set buffers
bgfx::setIndexBuffer(group.m_ibh);
bgfx::setVertexBuffer(0, group.m_vbh);
// Set textures
if (bgfx::kInvalidHandle != m_texture.idx)
{
bgfx::setTexture(0, s_texColor, m_texture);
}
bgfx::setTexture(1, s_texStencil, bgfx::getTexture(s_stencilFb) );
// Apply render state
::setRenderState(_renderState);
// Submit
BX_CHECK(bgfx::kInvalidHandle != m_program, "Error, program is not set.");
::submit(_viewId, m_program);
}
}
Mesh m_mesh;
bgfx::ProgramHandle m_program;
bgfx::TextureHandle m_texture;
};
struct Instance
{
Instance()
: m_svExtrusionDistance(150.0f)
{
m_color[0] = 1.0f;
m_color[1] = 1.0f;
m_color[2] = 1.0f;
}
void submit(uint8_t _viewId, const RenderState& _renderState)
{
bx::memCopy(s_uniforms.m_color, m_color, 3*sizeof(float) );
float mtx[16];
bx::mtxSRT(mtx
, m_scale[0]
, m_scale[1]
, m_scale[2]
, m_rotation[0]
, m_rotation[1]
, m_rotation[2]
, m_pos[0]
, m_pos[1]
, m_pos[2]
);
BX_CHECK(NULL != m_model, "Instance model cannot be NULL!");
m_model->submit(_viewId, mtx, _renderState);
}
float m_scale[3];
float m_rotation[3];
float m_pos[3];
float m_color[3];
float m_svExtrusionDistance;
Model* m_model;
};
#define SV_INSTANCE_MEM_SIZE (1500 << 10)
#define SV_INSTANCE_COUNT ( (25 > MAX_INSTANCE_COUNT) ? 25 : MAX_INSTANCE_COUNT)
#define SV_PAGE_SIZE (SV_INSTANCE_MEM_SIZE * SV_INSTANCE_COUNT * MAX_LIGHTS_COUNT)
struct ShadowVolumeAllocator
{
ShadowVolumeAllocator()
{
m_mem = (uint8_t*)malloc(SV_PAGE_SIZE*2);
m_ptr = m_mem;
m_firstPage = true;
}
~ShadowVolumeAllocator()
{
free(m_mem);
}
void* alloc(uint32_t _size)
{
void* ret = (void*)m_ptr;
m_ptr += _size;
BX_CHECK(m_ptr - m_mem < (m_firstPage ? SV_PAGE_SIZE : 2 * SV_PAGE_SIZE), "Buffer overflow!");
return ret;
}
void swap()
{
m_ptr = m_firstPage ? m_mem + SV_PAGE_SIZE : m_mem;
m_firstPage = !m_firstPage;
}
uint8_t* m_mem;
uint8_t* m_ptr;
bool m_firstPage;
};
static ShadowVolumeAllocator s_svAllocator;
struct ShadowVolumeImpl
{
enum Enum
{
DepthPass,
DepthFail,
};
};
struct ShadowVolumeAlgorithm
{
enum Enum
{
FaceBased,
EdgeBased,
};
};
struct ShadowVolume
{
bgfx::VertexBufferHandle m_vbSides;
bgfx::IndexBufferHandle m_ibSides;
bgfx::IndexBufferHandle m_ibFrontCap;
bgfx::IndexBufferHandle m_ibBackCap;
uint32_t m_numVertices;
uint32_t m_numIndices;
const float* m_mtx;
const float* m_lightPos;
bool m_cap;
};
void shadowVolumeLightTransform(
float* _outLightPos
, const float* _scale
, const float* _rotate
, const float* _translate
, const float* _lightPos // world pos
)
{
/**
* Instead of transforming all the vertices, transform light instead:
* mtx = pivotTranslate -> rotateZYX -> invScale
* light = mtx * origin
*/
float pivot[16];
bx::mtxTranslate(pivot
, _lightPos[0] - _translate[0]
, _lightPos[1] - _translate[1]
, _lightPos[2] - _translate[2]
);
float mzyx[16];
bx::mtxRotateZYX(mzyx
, -_rotate[0]
, -_rotate[1]
, -_rotate[2]
);
float invScale[16];
bx::mtxScale(invScale
, 1.0f / _scale[0]
, 1.0f / _scale[1]
, 1.0f / _scale[2]
);
float tmp0[16];
bx::mtxMul(tmp0, pivot, mzyx);
float mtx[16];
bx::mtxMul(mtx, tmp0, invScale);
bx::store(_outLightPos, bx::mul({ 0.0f, 0.0f, 0.0f }, mtx) );
}
void shadowVolumeCreate(
ShadowVolume& _shadowVolume
, Group& _group
, uint16_t _stride
, const float* _mtx
, const float* _light // in model space
, ShadowVolumeImpl::Enum _impl = ShadowVolumeImpl::DepthPass
, ShadowVolumeAlgorithm::Enum _algo = ShadowVolumeAlgorithm::FaceBased
, bool _textureAsStencil = false
)
{
const uint8_t* vertices = _group.m_vertices;
const FaceArray& faces = _group.m_faces;
const Edge* edges = _group.m_edges;
const Plane* edgePlanes = _group.m_edgePlanes;
const uint32_t numEdges = _group.m_numEdges;
HalfEdges& halfEdges = _group.m_halfEdges;
struct VertexData
{
VertexData()
{
}
VertexData(const float* _v3, float _extrude = 0.0f, float _k = 1.0f)
{
bx::memCopy(m_v, _v3, 3*sizeof(float) );
m_extrude = _extrude;
m_k = _k;
}
float m_v[3];
float m_extrude;
float m_k;
};
bool cap = (ShadowVolumeImpl::DepthFail == _impl);
VertexData* verticesSide = (VertexData*) s_svAllocator.alloc(20000 * sizeof(VertexData) );
uint16_t* indicesSide = (uint16_t*) s_svAllocator.alloc(20000 * 3*sizeof(uint16_t) );
uint16_t* indicesFrontCap = 0;
uint16_t* indicesBackCap = 0;
if (cap)
{
indicesFrontCap = (uint16_t*)s_svAllocator.alloc(80000 * 3*sizeof(uint16_t) );
indicesBackCap = (uint16_t*)s_svAllocator.alloc(80000 * 3*sizeof(uint16_t) );
}
uint32_t vsideI = 0;
uint32_t sideI = 0;
uint32_t frontCapI = 0;
uint32_t backCapI = 0;
uint16_t indexSide = 0;
if (ShadowVolumeAlgorithm::FaceBased == _algo)
{
for (FaceArray::const_iterator iter = faces.begin(), end = faces.end(); iter != end; ++iter)
{
const Face& face = *iter;
bool frontFacing = false;
const float f = bx::dot(bx::load<bx::Vec3>(face.m_plane), bx::load<bx::Vec3>(_light) ) + face.m_plane[3];
if (f > 0.0f)
{
frontFacing = true;
uint16_t triangleEdges[3][2] =
{
{ face.m_i[0], face.m_i[1] },
{ face.m_i[1], face.m_i[2] },
{ face.m_i[2], face.m_i[0] },
};
for (uint8_t ii = 0; ii < 3; ++ii)
{
uint16_t first = triangleEdges[ii][0];
uint16_t second = triangleEdges[ii][1];
if (!halfEdges.unmark(second, first) )
{
halfEdges.mark(first, second);
}
}
}
if (cap)
{
if (frontFacing)
{
indicesFrontCap[frontCapI++] = face.m_i[0];
indicesFrontCap[frontCapI++] = face.m_i[1];
indicesFrontCap[frontCapI++] = face.m_i[2];
}
else
{
indicesBackCap[backCapI++] = face.m_i[0];
indicesBackCap[backCapI++] = face.m_i[1];
indicesBackCap[backCapI++] = face.m_i[2];
}
/**
* if '_useFrontFacingFacesAsBackCap' is needed, implement it as such:
*
* bool condition0 = frontFacing && _useFrontFacingFacesAsBackCap;
* bool condition1 = !frontFacing && !_useFrontFacingFacesAsBackCap;
* if (condition0 || condition1)
* {
* indicesBackCap[backCapI++] = face.m_i[0];
* indicesBackCap[backCapI++] = face.m_i[1+condition0];
* indicesBackCap[backCapI++] = face.m_i[2-condition0];
* }
*/
}
}
// Fill side arrays.
uint16_t firstIndex = 0;
HalfEdge* he = halfEdges.begin();
while (halfEdges.end() != he)
{
if (he->m_marked)
{
he->m_marked = false;
const float* v0 = (float*)&vertices[firstIndex*_stride];
const float* v1 = (float*)&vertices[he->m_secondIndex*_stride];
verticesSide[vsideI++] = VertexData(v0, 0.0f);
verticesSide[vsideI++] = VertexData(v0, 1.0f);
verticesSide[vsideI++] = VertexData(v1, 0.0f);
verticesSide[vsideI++] = VertexData(v1, 1.0f);
indicesSide[sideI++] = indexSide+0;
indicesSide[sideI++] = indexSide+1;
indicesSide[sideI++] = indexSide+2;
indicesSide[sideI++] = indexSide+2;
indicesSide[sideI++] = indexSide+1;
indicesSide[sideI++] = indexSide+3;
indexSide += 4;
}
++he;
if (INVALID_EDGE_INDEX == he->m_secondIndex)
{
++he;
++firstIndex;
}
}
}
else // ShadowVolumeAlgorithm::EdgeBased:
{
{
uint32_t ii = 0;
#if SV_USE_SIMD
uint32_t numEdgesRounded = numEdges & (~0x1);
using namespace bx;
const simd128_t lx = simd_splat(_light[0]);
const simd128_t ly = simd_splat(_light[1]);
const simd128_t lz = simd_splat(_light[2]);
for (; ii < numEdgesRounded; ii+=2)
{
const Edge& edge0 = edges[ii];
const Edge& edge1 = edges[ii+1];
const Plane* edgePlane0 = &edgePlanes[ii*2];
const Plane* edgePlane1 = &edgePlanes[ii*2 + 2];
const simd128_t reverse =
simd_ild(edge0.m_faceReverseOrder[0]
, edge1.m_faceReverseOrder[0]
, edge0.m_faceReverseOrder[1]
, edge1.m_faceReverseOrder[1]
);
const simd128_t p00 = simd_ld(edgePlane0[0].m_plane);
const simd128_t p10 = simd_ld(edgePlane1[0].m_plane);
const simd128_t p01 = simd_ld(edgePlane0[1].m_plane);
const simd128_t p11 = simd_ld(edgePlane1[1].m_plane);
const simd128_t xxyy0 = simd_shuf_xAyB(p00, p01);
const simd128_t zzww0 = simd_shuf_zCwD(p00, p01);
const simd128_t xxyy1 = simd_shuf_xAyB(p10, p11);
const simd128_t zzww1 = simd_shuf_zCwD(p10, p11);
const simd128_t vX = simd_shuf_xAyB(xxyy0, xxyy1);
const simd128_t vY = simd_shuf_zCwD(xxyy0, xxyy1);
const simd128_t vZ = simd_shuf_xAyB(zzww0, zzww1);
const simd128_t vW = simd_shuf_zCwD(zzww0, zzww1);
const simd128_t r0 = simd_mul(vX, lx);
const simd128_t r1 = simd_mul(vY, ly);
const simd128_t r2 = simd_mul(vZ, lz);
const simd128_t dot = simd_add(r0, simd_add(r1, r2) );
const simd128_t f = simd_add(dot, vW);
const simd128_t zero = simd_zero();
const simd128_t mask = simd_cmpgt(f, zero);
const simd128_t onef = simd_splat(1.0f);
const simd128_t tmp0 = simd_and(mask, onef);
const simd128_t tmp1 = simd_ftoi(tmp0);
const simd128_t tmp2 = simd_xor(tmp1, reverse);
const simd128_t tmp3 = simd_sll(tmp2, 1);
const simd128_t onei = simd_isplat(1);
const simd128_t tmp4 = simd_isub(tmp3, onei);
BX_ALIGN_DECL_16(int32_t res[4]);
simd_st(&res, tmp4);
for (uint16_t jj = 0; jj < 2; ++jj)
{
int32_t kk = res[jj] + res[jj+2];
if (kk != 0)
{
float* v0 = (float*)&vertices[edges[ii+jj].m_i0*_stride];
float* v1 = (float*)&vertices[edges[ii+jj].m_i1*_stride];
verticesSide[vsideI++] = VertexData(v0, 0.0f, float(kk) );
verticesSide[vsideI++] = VertexData(v0, 1.0f, float(kk) );
verticesSide[vsideI++] = VertexData(v1, 0.0f, float(kk) );
verticesSide[vsideI++] = VertexData(v1, 1.0f, float(kk) );
kk = _textureAsStencil ? 1 : kk;
uint16_t winding = uint16_t(kk > 0);
for (int32_t ll = 0, end = abs(kk); ll < end; ++ll)
{
indicesSide[sideI++] = indexSide;
indicesSide[sideI++] = indexSide + 2 - winding;
indicesSide[sideI++] = indexSide + 1 + winding;
indicesSide[sideI++] = indexSide + 2;
indicesSide[sideI++] = indexSide + 3 - winding*2;
indicesSide[sideI++] = indexSide + 1 + winding*2;
}
indexSide += 4;
}
}
}
#endif
for (; ii < numEdges; ++ii)
{
const Edge& edge = edges[ii];
const Plane* edgePlane = &edgePlanes[ii*2];
int16_t s0 = ( (bx::dot(bx::load<bx::Vec3>(edgePlane[0].m_plane), bx::load<bx::Vec3>(_light) ) + edgePlane[0].m_plane[3]) > 0.0f) ^ edge.m_faceReverseOrder[0];
int16_t s1 = ( (bx::dot(bx::load<bx::Vec3>(edgePlane[1].m_plane), bx::load<bx::Vec3>(_light) ) + edgePlane[1].m_plane[3]) > 0.0f) ^ edge.m_faceReverseOrder[1];
int16_t kk = ( (s0 + s1) << 1) - 2;
if (kk != 0)
{
float* v0 = (float*)&vertices[edge.m_i0*_stride];
float* v1 = (float*)&vertices[edge.m_i1*_stride];
verticesSide[vsideI++] = VertexData(v0, 0.0f, kk);
verticesSide[vsideI++] = VertexData(v0, 1.0f, kk);
verticesSide[vsideI++] = VertexData(v1, 0.0f, kk);
verticesSide[vsideI++] = VertexData(v1, 1.0f, kk);
kk = _textureAsStencil ? 1 : kk;
uint16_t winding = uint16_t(kk > 0);
for (int32_t jj = 0, end = abs(kk); jj < end; ++jj)
{
indicesSide[sideI++] = indexSide;
indicesSide[sideI++] = indexSide + 2 - winding;
indicesSide[sideI++] = indexSide + 1 + winding;
indicesSide[sideI++] = indexSide + 2;
indicesSide[sideI++] = indexSide + 3 - winding*2;
indicesSide[sideI++] = indexSide + 1 + winding*2;
}
indexSide += 4;
}
}
}
if (cap)
{
// This could/should be done on GPU!
for (FaceArray::const_iterator iter = faces.begin(), end = faces.end(); iter != end; ++iter)
{
const Face& face = *iter;
const float f = bx::dot(bx::load<bx::Vec3>(face.m_plane), bx::load<bx::Vec3>(_light) ) + face.m_plane[3];
bool frontFacing = (f > 0.0f);
for (uint8_t ii = 0, num = 1 + uint8_t(!_textureAsStencil); ii < num; ++ii)
{
if (frontFacing)
{
indicesFrontCap[frontCapI++] = face.m_i[0];
indicesFrontCap[frontCapI++] = face.m_i[1];
indicesFrontCap[frontCapI++] = face.m_i[2];
}
else
{
indicesBackCap[backCapI++] = face.m_i[0];
indicesBackCap[backCapI++] = face.m_i[1];
indicesBackCap[backCapI++] = face.m_i[2];
}
}
}
}
}
bgfx::VertexLayout layout;
layout.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
.end();
//fill the structure
_shadowVolume.m_numVertices = vsideI;
_shadowVolume.m_numIndices = sideI + frontCapI + backCapI;
_shadowVolume.m_mtx = _mtx;
_shadowVolume.m_lightPos = _light;
_shadowVolume.m_cap = cap;
const bgfx::Memory* mem;
//sides
uint32_t vsize = vsideI * 5*sizeof(float);
uint32_t isize = sideI * sizeof(uint16_t);
mem = bgfx::makeRef(verticesSide, vsize);
_shadowVolume.m_vbSides = bgfx::createVertexBuffer(mem, layout);
mem = bgfx::makeRef(indicesSide, isize);
_shadowVolume.m_ibSides = bgfx::createIndexBuffer(mem);
// bgfx::destroy*Buffer doesn't actually destroy buffers now.
// Instead, these bgfx::destroy*Buffer commands get queued to be executed after the end of the next frame.
bgfx::destroy(_shadowVolume.m_vbSides);
bgfx::destroy(_shadowVolume.m_ibSides);
if (cap)
{
//front cap
isize = frontCapI * sizeof(uint16_t);
mem = bgfx::makeRef(indicesFrontCap, isize);
_shadowVolume.m_ibFrontCap = bgfx::createIndexBuffer(mem);
//gets destroyed after the end of the next frame
bgfx::destroy(_shadowVolume.m_ibFrontCap);
//back cap
isize = backCapI * sizeof(uint16_t);
mem = bgfx::makeRef(indicesBackCap, isize);
_shadowVolume.m_ibBackCap = bgfx::createIndexBuffer(mem);
//gets destroyed after the end of the next frame
bgfx::destroy(_shadowVolume.m_ibBackCap);
}
}
void createNearClipVolume(
float* _outPlanes24f
, float* _lightPos
, float* _view
, float _fovy
, float _aspect
, float _near
)
{
float (*volumePlanes)[4] = (float(*)[4])_outPlanes24f;
float mtxViewInv[16];
float mtxViewTrans[16];
bx::mtxInverse(mtxViewInv, _view);
bx::mtxTranspose(mtxViewTrans, _view);
float lightPosV[4];
bx::vec4MulMtx(lightPosV, _lightPos, _view);
const float delta = 0.1f;
const float nearNormal[4] = { 0.0f, 0.0f, 1.0f, _near };
const float d = bx::dot(bx::load<bx::Vec3>(lightPosV), bx::load<bx::Vec3>(nearNormal) ) + lightPosV[3] * nearNormal[3];
// Light is:
// 1.0f - in front of near plane
// 0.0f - on the near plane
// -1.0f - behind near plane
const float lightSide = float( (d > delta) - (d < -delta) );
const float t = bx::tan(bx::toRad(_fovy)*0.5f) * _near;
const float b = -t;
const float r = t * _aspect;
const float l = -r;
const bx::Vec3 corners[4] =
{
bx::mul({ r, t, _near }, mtxViewInv),
bx::mul({ l, t, _near }, mtxViewInv),
bx::mul({ l, b, _near }, mtxViewInv),
bx::mul({ r, b, _near }, mtxViewInv),
};
float planeNormals[4][3];
for (uint8_t ii = 0; ii < 4; ++ii)
{
float* outNormal = planeNormals[ii];
float* outPlane = volumePlanes[ii];
const bx::Vec3 c0 = corners[ii];
const bx::Vec3 planeVec = bx::sub(c0, corners[(ii-1)&3]);
const bx::Vec3 light = bx::sub(bx::load<bx::Vec3>(_lightPos), bx::mul(c0, _lightPos[3]) );
const bx::Vec3 normal = bx::mul(bx::cross(planeVec, light), lightSide);
const float invLen = 1.0f / bx::sqrt(bx::dot(normal, normal) );
bx::store(outNormal, normal);
bx::store(outPlane, bx::mul(normal, invLen) );
outPlane[3] = -bx::dot(normal, c0) * invLen;
}
float nearPlaneV[4] =
{
0.0f * lightSide,
0.0f * lightSide,
1.0f * lightSide,
_near * lightSide,
};
bx::vec4MulMtx(volumePlanes[4], nearPlaneV, mtxViewTrans);
float* lightPlane = volumePlanes[5];
const bx::Vec3 lightPlaneNormal = bx::sub(bx::mul({ 0.0f, 0.0f, -_near * lightSide }, mtxViewInv), bx::load<bx::Vec3>(_lightPos) );
float lenInv = 1.0f / bx::sqrt(bx::dot(lightPlaneNormal, lightPlaneNormal) );
lightPlane[0] = lightPlaneNormal.x * lenInv;
lightPlane[1] = lightPlaneNormal.y * lenInv;
lightPlane[2] = lightPlaneNormal.z * lenInv;
lightPlane[3] = -bx::dot(lightPlaneNormal, bx::load<bx::Vec3>(_lightPos) ) * lenInv;
}
bool clipTest(const float* _planes, uint8_t _planeNum, const Mesh& _mesh, const float* _scale, const float* _translate)
{
float (*volumePlanes)[4] = (float(*)[4])_planes;
float scale = bx::max(_scale[0], _scale[1], _scale[2]);
const GroupArray& groups = _mesh.m_groups;
for (GroupArray::const_iterator it = groups.begin(), itEnd = groups.end(); it != itEnd; ++it)
{
const Group& group = *it;
Sphere sphere = group.m_sphere;
sphere.center.x = sphere.center.x * scale + _translate[0];
sphere.center.y = sphere.center.y * scale + _translate[1];
sphere.center.z = sphere.center.z * scale + _translate[2];
sphere.radius *= (scale+0.4f);
bool isInside = true;
for (uint8_t ii = 0; ii < _planeNum; ++ii)
{
const float* plane = volumePlanes[ii];
float positiveSide = bx::dot(bx::load<bx::Vec3>(plane), sphere.center ) + plane[3] + sphere.radius;
if (positiveSide < 0.0f)
{
isInside = false;
break;
}
}
if (isInside)
{
return true;
}
}
return false;
}
struct ShadowVolumeProgramType
{
enum Enum
{
Blank = 0,
Color,
Tex1,
Tex2,
Count
};
};
struct ShadowVolumePart
{
enum Enum
{
Back = 0,
Side,
Front,
Count
};
};
enum LightPattern
{
LightPattern0 = 0,
LightPattern1
};
enum MeshChoice
{
BunnyHighPoly = 0,
BunnyLowPoly
};
enum Scene
{
Scene0 = 0,
Scene1,
SceneCount
};
class ExampleShadowVolumes : public entry::AppI
{
public:
ExampleShadowVolumes(const char* _name, const char* _description, const char* _url)
: entry::AppI(_name, _description, _url)
{
}
void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
{
Args args(_argc, _argv);
m_viewState = ViewState(_width, _height);
m_clearValues = { 0x00000000, 1.0f, 0 };
m_debug = BGFX_DEBUG_TEXT;
m_reset = BGFX_RESET_VSYNC;
bgfx::Init init;
init.type = args.m_type;
init.vendorId = args.m_pciId;
init.resolution.width = m_viewState.m_width;
init.resolution.height = m_viewState.m_height;
init.resolution.reset = m_reset;
bgfx::init(init);
// Enable debug text.
bgfx::setDebug(m_debug);
const bgfx::Caps* caps = bgfx::getCaps();
s_oglNdc = caps->homogeneousDepth;
s_texelHalf = bgfx::RendererType::Direct3D9 == caps->rendererType ? 0.5f : 0.0f;
// Imgui
imguiCreate();
PosNormalTexcoordVertex::init();
s_uniforms.init();
m_figureTex = loadTexture("textures/figure-rgba.dds");
m_flareTex = loadTexture("textures/flare.dds");
m_fieldstoneTex = loadTexture("textures/fieldstone-rgba.dds");
bgfx::TextureHandle fbtextures[] =
{
bgfx::createTexture2D(uint16_t(m_viewState.m_width), uint16_t(m_viewState.m_height), false, 1, bgfx::TextureFormat::BGRA8, BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP | BGFX_TEXTURE_RT),
bgfx::createTexture2D(uint16_t(m_viewState.m_width), uint16_t(m_viewState.m_height), false, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_WRITE_ONLY),
};
s_stencilFb = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
s_texStencil = bgfx::createUniform("s_texStencil", bgfx::UniformType::Sampler);
m_programTextureLighting = loadProgram("vs_shadowvolume_texture_lighting", "fs_shadowvolume_texture_lighting");
m_programColorLighting = loadProgram("vs_shadowvolume_color_lighting", "fs_shadowvolume_color_lighting" );
m_programColorTexture = loadProgram("vs_shadowvolume_color_texture", "fs_shadowvolume_color_texture" );
m_programTexture = loadProgram("vs_shadowvolume_texture", "fs_shadowvolume_texture" );
m_programBackBlank = loadProgram("vs_shadowvolume_svback", "fs_shadowvolume_svbackblank" );
m_programSideBlank = loadProgram("vs_shadowvolume_svside", "fs_shadowvolume_svsideblank" );
m_programFrontBlank = loadProgram("vs_shadowvolume_svfront", "fs_shadowvolume_svfrontblank");
m_programBackColor = loadProgram("vs_shadowvolume_svback", "fs_shadowvolume_svbackcolor" );
m_programSideColor = loadProgram("vs_shadowvolume_svside", "fs_shadowvolume_svsidecolor" );
m_programFrontColor = loadProgram("vs_shadowvolume_svfront", "fs_shadowvolume_svfrontcolor");
m_programSideTex = loadProgram("vs_shadowvolume_svside", "fs_shadowvolume_svsidetex" );
m_programBackTex1 = loadProgram("vs_shadowvolume_svback", "fs_shadowvolume_svbacktex1" );
m_programBackTex2 = loadProgram("vs_shadowvolume_svback", "fs_shadowvolume_svbacktex2" );
m_programFrontTex1 = loadProgram("vs_shadowvolume_svfront", "fs_shadowvolume_svfronttex1" );
m_programFrontTex2 = loadProgram("vs_shadowvolume_svfront", "fs_shadowvolume_svfronttex2" );
bgfx::ProgramHandle svProgs[ShadowVolumeProgramType::Count][ShadowVolumePart::Count] =
{
{ m_programBackBlank, m_programSideBlank, m_programFrontBlank }, // Blank
{ m_programBackColor, m_programSideColor, m_programFrontColor }, // Color
{ m_programBackTex1, m_programSideTex, m_programFrontTex1 }, // Tex1
{ m_programBackTex2, m_programSideTex, m_programFrontTex2 }, // Tex2
};
bx::memCopy(m_svProgs, svProgs, sizeof(svProgs));
m_bunnyHighPolyModel.load("meshes/bunny_patched.bin");
m_bunnyHighPolyModel.m_program = m_programColorLighting;
m_bunnyLowPolyModel.load("meshes/bunny_decimated.bin");
m_bunnyLowPolyModel.m_program = m_programColorLighting;
m_columnModel.load("meshes/column.bin");
m_columnModel.m_program = m_programColorLighting;
m_platformModel.load("meshes/platform.bin");
m_platformModel.m_program = m_programTextureLighting;
m_platformModel.m_texture = m_figureTex;
m_cubeModel.load("meshes/cube.bin");
m_cubeModel.m_program = m_programTextureLighting;
m_cubeModel.m_texture = m_figureTex;
m_hplaneFieldModel.load(s_hplaneVertices
, BX_COUNTOF(s_hplaneVertices)
, PosNormalTexcoordVertex::ms_layout
, s_planeIndices
, BX_COUNTOF(s_planeIndices)
);
m_hplaneFieldModel.m_program = m_programTextureLighting;
m_hplaneFieldModel.m_texture = m_fieldstoneTex;
m_hplaneFigureModel.load(s_hplaneVertices
, BX_COUNTOF(s_hplaneVertices)
, PosNormalTexcoordVertex::ms_layout
, s_planeIndices
, BX_COUNTOF(s_planeIndices)
);
m_hplaneFigureModel.m_program = m_programTextureLighting;
m_hplaneFigureModel.m_texture = m_figureTex;
m_vplaneModel.load(s_vplaneVertices
, BX_COUNTOF(s_vplaneVertices)
, PosNormalTexcoordVertex::ms_layout
, s_planeIndices
, BX_COUNTOF(s_planeIndices)
);
m_vplaneModel.m_program = m_programColorTexture;
m_vplaneModel.m_texture = m_flareTex;
// Setup lights.
const float rgbInnerR[MAX_LIGHTS_COUNT][4] =
{
{ 1.0f, 0.7f, 0.2f, 0.0f }, //yellow
{ 0.7f, 0.2f, 1.0f, 0.0f }, //purple
{ 0.2f, 1.0f, 0.7f, 0.0f }, //cyan
{ 1.0f, 0.4f, 0.2f, 0.0f }, //orange
{ 0.7f, 0.7f, 0.7f, 0.0f }, //white
};
for (uint8_t ii = 0, jj = 0; ii < MAX_LIGHTS_COUNT; ++ii, ++jj)
{
const uint8_t index = jj%MAX_LIGHTS_COUNT;
m_lightRgbInnerR[ii][0] = rgbInnerR[index][0];
m_lightRgbInnerR[ii][1] = rgbInnerR[index][1];
m_lightRgbInnerR[ii][2] = rgbInnerR[index][2];
m_lightRgbInnerR[ii][3] = rgbInnerR[index][3];
}
m_profTime = 0;
m_timeOffset = bx::getHPCounter();
m_numShadowVolumeVertices = 0;
m_numShadowVolumeIndices = 0;
m_oldWidth = 0;
m_oldHeight = 0;
// Imgui.
m_showHelp = false;
m_updateLights = true;
m_updateScene = true;
m_mixedSvImpl = true;
m_useStencilTexture = false;
m_drawShadowVolumes = false;
m_numLights = 1;
m_instanceCount = 9;
m_shadowVolumeImpl = ShadowVolumeImpl::DepthFail;
m_shadowVolumeAlgorithm = ShadowVolumeAlgorithm::EdgeBased;
m_lightPattern = LightPattern0;
m_currentMesh = BunnyLowPoly;
m_currentScene = Scene0;
// Set view matrix
cameraCreate();
cameraSetPosition({ 3.0f, 20.0f, -58.0f });
cameraSetVerticalAngle(-0.25f);
cameraGetViewMtx(m_viewState.m_view);
}
virtual int shutdown() override
{
// Cleanup
m_bunnyLowPolyModel.unload();
m_bunnyHighPolyModel.unload();
m_columnModel.unload();
m_cubeModel.unload();
m_platformModel.unload();
m_hplaneFieldModel.unload();
m_hplaneFigureModel.unload();
m_vplaneModel.unload();
s_uniforms.destroy();
bgfx::destroy(s_texColor);
bgfx::destroy(s_texStencil);
bgfx::destroy(s_stencilFb);
bgfx::destroy(m_figureTex);
bgfx::destroy(m_fieldstoneTex);
bgfx::destroy(m_flareTex);
bgfx::destroy(m_programTextureLighting);
bgfx::destroy(m_programColorLighting);
bgfx::destroy(m_programColorTexture);
bgfx::destroy(m_programTexture);
bgfx::destroy(m_programBackBlank);
bgfx::destroy(m_programSideBlank);
bgfx::destroy(m_programFrontBlank);
bgfx::destroy(m_programBackColor);
bgfx::destroy(m_programSideColor);
bgfx::destroy(m_programFrontColor);
bgfx::destroy(m_programSideTex);
bgfx::destroy(m_programBackTex1);
bgfx::destroy(m_programBackTex2);
bgfx::destroy(m_programFrontTex1);
bgfx::destroy(m_programFrontTex2);
cameraDestroy();
imguiDestroy();
// Shutdown bgfx.
bgfx::shutdown();
return 0;
}
bool update() override
{
if (!entry::processEvents(m_viewState.m_width, m_viewState.m_height, m_debug, m_reset, &m_mouseState) )
{
s_uniforms.submitConstUniforms();
// Set projection matrices.
const float fov = 60.0f;
const float aspect = float(m_viewState.m_width)/float(m_viewState.m_height);
const float nearPlane = 1.0f;
const float farPlane = 1000.0f;
// Respond properly on resize.
if (m_oldWidth != m_viewState.m_width
|| m_oldHeight != m_viewState.m_height)
{
m_oldWidth = m_viewState.m_width;
m_oldHeight = m_viewState.m_height;
bgfx::destroy(s_stencilFb);
bgfx::TextureHandle fbtextures[] =
{
bgfx::createTexture2D(uint16_t(m_viewState.m_width), uint16_t(m_viewState.m_height), false, 1, bgfx::TextureFormat::BGRA8, BGFX_SAMPLER_U_CLAMP|BGFX_SAMPLER_V_CLAMP|BGFX_TEXTURE_RT),
bgfx::createTexture2D(uint16_t(m_viewState.m_width), uint16_t(m_viewState.m_height), false, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_WRITE_ONLY)
};
s_stencilFb = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
}
// Time.
int64_t now = bx::getHPCounter();
static int64_t last = now;
const int64_t frameTime = now - last;
last = now;
const double freq = double(bx::getHPFrequency() );
const double toMs = 1000.0/freq;
float time = (float)( (now - m_timeOffset)/double(bx::getHPFrequency() ) );
const float deltaTime = float(frameTime/freq);
s_uniforms.m_time = time;
// Update camera.
cameraUpdate(deltaTime, m_mouseState);
// Set view and projection matrix for view 0.
{
cameraGetViewMtx(m_viewState.m_view);
bx::mtxProj(m_viewState.m_proj, fov, aspect, nearPlane, farPlane, s_oglNdc);
}
imguiBeginFrame(
m_mouseState.m_mx
, m_mouseState.m_my
, (m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0)
| (m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0)
| (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
, m_mouseState.m_mz
, uint16_t(m_viewState.m_width)
, uint16_t(m_viewState.m_height)
);
showExampleDialog(this);
ImGui::SetNextWindowPos(
ImVec2(m_viewState.m_width - 256.0f, 10.0f)
, ImGuiCond_FirstUseEver
);
ImGui::SetNextWindowSize(
ImVec2(256.0f, 700.0f)
, ImGuiCond_FirstUseEver
);
ImGui::Begin("Settings"
, NULL
, 0
);
const char* titles[2] =
{
"Scene 0",
"Scene 1",
};
if (ImGui::RadioButton(titles[Scene0], Scene0 == m_currentScene) )
{
m_currentScene = Scene0;
}
if (ImGui::RadioButton(titles[Scene1], Scene1 == m_currentScene) )
{
m_currentScene = Scene1;
}
ImGui::SliderInt("Lights", &m_numLights, 1, MAX_LIGHTS_COUNT);
ImGui::Checkbox("Update lights", &m_updateLights);
ImGui::Indent();
if (ImGui::RadioButton("Light pattern 0", LightPattern0 == m_lightPattern) )
{
m_lightPattern = LightPattern0;
}
if (ImGui::RadioButton("Light pattern 1", LightPattern1 == m_lightPattern) )
{
m_lightPattern = LightPattern1;
}
ImGui::Unindent();
if ( Scene0 == m_currentScene )
{
ImGui::Checkbox("Update scene", &m_updateScene);
}
ImGui::Separator();
ImGui::Text("Stencil buffer implementation:");
ImGui::Checkbox("Mixed", &m_mixedSvImpl);
if (!m_mixedSvImpl)
{
m_shadowVolumeImpl = (ImGui::RadioButton("Depth fail", ShadowVolumeImpl::DepthFail == m_shadowVolumeImpl) ? ShadowVolumeImpl::DepthFail : m_shadowVolumeImpl);
m_shadowVolumeImpl = (ImGui::RadioButton("Depth pass", ShadowVolumeImpl::DepthPass == m_shadowVolumeImpl) ? ShadowVolumeImpl::DepthPass : m_shadowVolumeImpl);
}
ImGui::Text("Shadow volume implementation:");
m_shadowVolumeAlgorithm = (ImGui::RadioButton("Face based impl.", ShadowVolumeAlgorithm::FaceBased == m_shadowVolumeAlgorithm) ? ShadowVolumeAlgorithm::FaceBased : m_shadowVolumeAlgorithm);
m_shadowVolumeAlgorithm = (ImGui::RadioButton("Edge based impl.", ShadowVolumeAlgorithm::EdgeBased == m_shadowVolumeAlgorithm) ? ShadowVolumeAlgorithm::EdgeBased : m_shadowVolumeAlgorithm);
ImGui::Text("Stencil:");
if (ImGui::RadioButton("Use stencil buffer", !m_useStencilTexture) )
{
if (m_useStencilTexture)
{
m_useStencilTexture = false;
}
}
if (ImGui::RadioButton("Use texture as stencil", m_useStencilTexture) )
{
if (!m_useStencilTexture)
{
m_useStencilTexture = true;
}
}
ImGui::Separator();
ImGui::Text("Mesh:");
if (ImGui::RadioButton("Bunny - high poly", BunnyHighPoly == m_currentMesh) )
{
m_currentMesh = BunnyHighPoly;
}
if (ImGui::RadioButton("Bunny - low poly", BunnyLowPoly == m_currentMesh) )
{
m_currentMesh = BunnyLowPoly;
}
if (Scene1 == m_currentScene)
{
ImGui::SliderInt("Instance count", &m_instanceCount, 1, MAX_INSTANCE_COUNT);
}
ImGui::Text("CPU Time: %7.1f [ms]", double(m_profTime)*toMs);
ImGui::Text("Volume Vertices: %5.uk", m_numShadowVolumeVertices/1000);
ImGui::Text("Volume Indices: %6.uk", m_numShadowVolumeIndices/1000);
m_numShadowVolumeVertices = 0;
m_numShadowVolumeIndices = 0;
ImGui::Separator();
ImGui::Checkbox("Draw Shadow Volumes", &m_drawShadowVolumes);
ImGui::End();
ImGui::SetNextWindowPos(
ImVec2(10, float(m_viewState.m_height) - 77.0f - 10.0f)
, ImGuiCond_FirstUseEver
);
ImGui::SetNextWindowSize(
ImVec2(120.0f, 77.0f)
, ImGuiCond_FirstUseEver
);
ImGui::Begin("Show help:"
, NULL
, 0
);
if (ImGui::Button(m_showHelp ? "ON" : "OFF") )
{
m_showHelp = !m_showHelp;
}
ImGui::End();
imguiEndFrame();
//update settings
s_uniforms.m_params.m_ambientPass = 1.0f;
s_uniforms.m_params.m_lightingPass = 1.0f;
s_uniforms.m_params.m_texelHalf = s_texelHalf;
s_uniforms.m_svparams.m_useStencilTex = float(m_useStencilTexture);
//set picked bunny model
Model* bunnyModel = BunnyLowPoly == m_currentMesh ? &m_bunnyLowPolyModel : &m_bunnyHighPolyModel;
//update time accumulators
static float sceneTimeAccumulator = 0.0f;
if (m_updateScene)
{
sceneTimeAccumulator += deltaTime;
}
static float lightTimeAccumulator = 0.0f;
if (m_updateLights)
{
lightTimeAccumulator += deltaTime;
}
//setup light positions
float lightPosRadius[MAX_LIGHTS_COUNT][4];
if (LightPattern0 == m_lightPattern)
{
for (uint8_t ii = 0; ii < m_numLights; ++ii)
{
lightPosRadius[ii][0] = bx::cos(2.0f*bx::kPi/float(m_numLights) * float(ii) + lightTimeAccumulator * 1.1f + 3.0f) * 20.0f;
lightPosRadius[ii][1] = 20.0f;
lightPosRadius[ii][2] = bx::sin(2.0f*bx::kPi/float(m_numLights) * float(ii) + lightTimeAccumulator * 1.1f + 3.0f) * 20.0f;
lightPosRadius[ii][3] = 20.0f;
}
}
else
{
for (uint8_t ii = 0; ii < m_numLights; ++ii)
{
lightPosRadius[ii][0] = bx::cos(float(ii) * 2.0f/float(m_numLights) + lightTimeAccumulator * 1.3f + bx::kPi) * 40.0f;
lightPosRadius[ii][1] = 20.0f;
lightPosRadius[ii][2] = bx::sin(float(ii) * 2.0f/float(m_numLights) + lightTimeAccumulator * 1.3f + bx::kPi) * 40.0f;
lightPosRadius[ii][3] = 20.0f;
}
}
if (m_showHelp)
{
uint8_t row = 18;
bgfx::dbgTextPrintf(3, row++, 0x0f, "Stencil buffer implementation:");
bgfx::dbgTextPrintf(8, row++, 0x0f, "Depth fail - Robust, but slower than 'Depth pass'. Requires computing and drawing of shadow volume caps.");
bgfx::dbgTextPrintf(8, row++, 0x0f, "Depth pass - Faster, but not stable. Shadows are wrong when camera is in the shadow.");
bgfx::dbgTextPrintf(8, row++, 0x0f, "Mixed - 'Depth pass' where possible, 'Depth fail' where necessary. Best of both words.");
row++;
bgfx::dbgTextPrintf(3, row++, 0x0f, "Shadow volume implementation:");
bgfx::dbgTextPrintf(8, row++, 0x0f, "Face Based - Slower. Works fine with either stencil buffer or texture as stencil.");
bgfx::dbgTextPrintf(8, row++, 0x0f, "Edge Based - Faster, but requires +2 incr/decr on stencil buffer. To avoid massive redraw, use RGBA texture as stencil.");
row++;
bgfx::dbgTextPrintf(3, row++, 0x0f, "Stencil:");
bgfx::dbgTextPrintf(8, row++, 0x0f, "Stencil buffer - Faster, but capable only of +1 incr.");
bgfx::dbgTextPrintf(8, row++, 0x0f, "Texture as stencil - Slower, but capable of +2 incr.");
}
else
{
bgfx::dbgTextClear();
}
// Setup instances
Instance shadowCasters[SceneCount][60];
uint16_t shadowCastersCount[SceneCount];
for (uint8_t ii = 0; ii < SceneCount; ++ii)
{
shadowCastersCount[ii] = 0;
}
Instance shadowReceivers[SceneCount][10];
uint16_t shadowReceiversCount[SceneCount];
for (uint8_t ii = 0; ii < SceneCount; ++ii)
{
shadowReceiversCount[ii] = 0;
}
// Scene 0 - shadow casters - Bunny
{
Instance& inst = shadowCasters[Scene0][shadowCastersCount[Scene0]++];
inst.m_scale[0] = 5.0f;
inst.m_scale[1] = 5.0f;
inst.m_scale[2] = 5.0f;
inst.m_rotation[0] = 0.0f;
inst.m_rotation[1] = float(4.0f - sceneTimeAccumulator * 0.7f);
inst.m_rotation[2] = 0.0f;
inst.m_pos[0] = 0.0f;
inst.m_pos[1] = 10.0f;
inst.m_pos[2] = 0.0f;
inst.m_color[0] = 0.68f;
inst.m_color[1] = 0.65f;
inst.m_color[2] = 0.60f;
inst.m_model = bunnyModel;
}
// Scene 0 - shadow casters - Cubes top.
const uint8_t numCubesTop = 9;
for (uint16_t ii = 0; ii < numCubesTop; ++ii)
{
Instance& inst = shadowCasters[Scene0][shadowCastersCount[Scene0]++];
inst.m_scale[0] = 1.0f;
inst.m_scale[1] = 1.0f;
inst.m_scale[2] = 1.0f;
inst.m_rotation[0] = 0.0f;
inst.m_rotation[1] = 0.0f;
inst.m_rotation[2] = 0.0f;
inst.m_pos[0] = bx::sin(ii * 2.0f + 13.0f + sceneTimeAccumulator * 1.1f) * 13.0f;
inst.m_pos[1] = 6.0f;
inst.m_pos[2] = bx::cos(ii * 2.0f + 13.0f + sceneTimeAccumulator * 1.1f) * 13.0f;
inst.m_model = &m_cubeModel;
}
// Scene 0 - shadow casters - Cubes bottom.
const uint8_t numCubesBottom = 9;
for (uint16_t ii = 0; ii < numCubesBottom; ++ii)
{
Instance& inst = shadowCasters[Scene0][shadowCastersCount[Scene0]++];
inst.m_scale[0] = 1.0f;
inst.m_scale[1] = 1.0f;
inst.m_scale[2] = 1.0f;
inst.m_rotation[0] = 0.0f;
inst.m_rotation[1] = 0.0f;
inst.m_rotation[2] = 0.0f;
inst.m_pos[0] = bx::sin(ii * 2.0f + 13.0f + sceneTimeAccumulator * 1.1f) * 13.0f;
inst.m_pos[1] = 22.0f;
inst.m_pos[2] = bx::cos(ii * 2.0f + 13.0f + sceneTimeAccumulator * 1.1f) * 13.0f;
inst.m_model = &m_cubeModel;
}
// Scene 0 - shadow casters - Columns.
const float dist = 16.0f;
const float columnPositions[][3] =
{
{ dist, 3.3f, dist },
{ -dist, 3.3f, dist },
{ dist, 3.3f, -dist },
{ -dist, 3.3f, -dist },
};
for (uint8_t ii = 0; ii < 4; ++ii)
{
Instance& inst = shadowCasters[Scene0][shadowCastersCount[Scene0]++];
inst.m_scale[0] = 1.5f;
inst.m_scale[1] = 1.5f;
inst.m_scale[2] = 1.5f;
inst.m_rotation[0] = 0.0f;
inst.m_rotation[1] = 1.57f;
inst.m_rotation[2] = 0.0f;
inst.m_pos[0] = columnPositions[ii][0];
inst.m_pos[1] = columnPositions[ii][1];
inst.m_pos[2] = columnPositions[ii][2];
inst.m_color[0] = 0.25f;
inst.m_color[1] = 0.25f;
inst.m_color[2] = 0.25f;
inst.m_model = &m_columnModel;
}
// Scene 0 - shadow casters - Ceiling.
{
Instance& inst = shadowCasters[Scene0][shadowCastersCount[Scene0]++];
inst.m_scale[0] = 21.0f;
inst.m_scale[1] = 21.0f;
inst.m_scale[2] = 21.0f;
inst.m_rotation[0] = bx::kPi;
inst.m_rotation[1] = 0.0f;
inst.m_rotation[2] = 0.0f;
inst.m_pos[0] = 0.0f;
inst.m_pos[1] = 28.2f;
inst.m_pos[2] = 0.0f;
inst.m_model = &m_platformModel;
inst.m_svExtrusionDistance = 2.0f; //prevent culling on tight view frustum
}
// Scene 0 - shadow casters - Platform.
{
Instance& inst = shadowCasters[Scene0][shadowCastersCount[Scene0]++];
inst.m_scale[0] = 24.0f;
inst.m_scale[1] = 24.0f;
inst.m_scale[2] = 24.0f;
inst.m_rotation[0] = 0.0f;
inst.m_rotation[1] = 0.0f;
inst.m_rotation[2] = 0.0f;
inst.m_pos[0] = 0.0f;
inst.m_pos[1] = 0.0f;
inst.m_pos[2] = 0.0f;
inst.m_model = &m_platformModel;
inst.m_svExtrusionDistance = 2.0f; //prevent culling on tight view frustum
}
// Scene 0 - shadow receivers - Floor.
{
Instance& inst = shadowReceivers[Scene0][shadowReceiversCount[Scene0]++];
inst.m_scale[0] = 500.0f;
inst.m_scale[1] = 500.0f;
inst.m_scale[2] = 500.0f;
inst.m_rotation[0] = 0.0f;
inst.m_rotation[1] = 0.0f;
inst.m_rotation[2] = 0.0f;
inst.m_pos[0] = 0.0f;
inst.m_pos[1] = 0.0f;
inst.m_pos[2] = 0.0f;
inst.m_model = &m_hplaneFieldModel;
}
// Scene 1 - shadow casters - Bunny instances
{
enum Direction
{
Left = 0x0,
Down = 0x1,
Right = 0x2,
Up = 0x3,
};
const uint8_t directionMask = 0x3;
uint8_t currentDirection = Left;
float currX = 0.0f;
float currY = 0.0f;
const float stepX = 20.0f;
const float stepY = 20.0f;
uint8_t stateStep = 0;
uint8_t stateChange = 1;
for (uint8_t ii = 0; ii < m_instanceCount; ++ii)
{
Instance& inst = shadowCasters[Scene1][shadowCastersCount[Scene1]++];
inst.m_scale[0] = 5.0f;
inst.m_scale[1] = 5.0f;
inst.m_scale[2] = 5.0f;
inst.m_rotation[0] = 0.0f;
inst.m_rotation[1] = bx::kPi;
inst.m_rotation[2] = 0.0f;
inst.m_pos[0] = currX;
inst.m_pos[1] = 0.0f;
inst.m_pos[2] = currY;
inst.m_model = bunnyModel;
++stateStep;
if (stateStep >= ( (stateChange & ~0x1) >> 1) )
{
currentDirection = (currentDirection + 1) & directionMask;
stateStep = 0;
++stateChange;
}
switch (currentDirection)
{
case Left: currX -= stepX; break;
case Down: currY -= stepY; break;
case Right: currX += stepX; break;
case Up: currY += stepY; break;
}
}
}
// Scene 1 - shadow receivers - Floor.
{
Instance& inst = shadowReceivers[Scene1][shadowReceiversCount[Scene1]++];
inst.m_scale[0] = 500.0f;
inst.m_scale[1] = 500.0f;
inst.m_scale[2] = 500.0f;
inst.m_rotation[0] = 0.0f;
inst.m_rotation[1] = 0.0f;
inst.m_rotation[2] = 0.0f;
inst.m_pos[0] = 0.0f;
inst.m_pos[1] = 0.0f;
inst.m_pos[2] = 0.0f;
inst.m_model = &m_hplaneFigureModel;
}
// Make sure at the beginning everything gets cleared.
bgfx::setViewClear(0
, BGFX_CLEAR_COLOR
| BGFX_CLEAR_DEPTH
| BGFX_CLEAR_STENCIL
, m_clearValues.m_clearRgba
, m_clearValues.m_clearDepth
, m_clearValues.m_clearStencil
);
::touch(0);
// Draw ambient only.
s_uniforms.m_params.m_ambientPass = 1.0f;
s_uniforms.m_params.m_lightingPass = 0.0f;
s_uniforms.m_color[0] = 1.0f;
s_uniforms.m_color[1] = 1.0f;
s_uniforms.m_color[2] = 1.0f;
const RenderState& drawAmbient = m_useStencilTexture
? s_renderStates[RenderState::ShadowVolume_UsingStencilTexture_DrawAmbient]
: s_renderStates[RenderState::ShadowVolume_UsingStencilBuffer_DrawAmbient]
;
// Draw shadow casters.
for (uint8_t ii = 0; ii < shadowCastersCount[m_currentScene]; ++ii)
{
shadowCasters[m_currentScene][ii].submit(VIEWID_RANGE1_PASS0, drawAmbient);
}
// Draw shadow receivers.
for (uint8_t ii = 0; ii < shadowReceiversCount[m_currentScene]; ++ii)
{
shadowReceivers[m_currentScene][ii].submit(VIEWID_RANGE1_PASS0, drawAmbient);
}
// Using stencil texture requires rendering to separate render target. first pass is building depth buffer.
if (m_useStencilTexture)
{
bgfx::setViewClear(VIEWID_RANGE1_RT_PASS1, BGFX_CLEAR_DEPTH, 0x00000000, 1.0f, 0);
bgfx::setViewFrameBuffer(VIEWID_RANGE1_RT_PASS1, s_stencilFb);
const RenderState& renderState = s_renderStates[RenderState::ShadowVolume_UsingStencilTexture_BuildDepth];
for (uint8_t ii = 0; ii < shadowCastersCount[m_currentScene]; ++ii)
{
shadowCasters[m_currentScene][ii].submit(VIEWID_RANGE1_RT_PASS1, renderState);
}
for (uint8_t ii = 0; ii < shadowReceiversCount[m_currentScene]; ++ii)
{
shadowReceivers[m_currentScene][ii].submit(VIEWID_RANGE1_RT_PASS1, renderState);
}
}
m_profTime = bx::getHPCounter();
/**
* For each light:
* 1. Compute and draw shadow volume to stencil buffer
* 2. Draw diffuse with stencil test
*/
for (uint8_t ii = 0, viewId = VIEWID_RANGE15_PASS2; ii < m_numLights; ++ii, ++viewId)
{
const float* lightPos = lightPosRadius[ii];
bx::memCopy(s_uniforms.m_lightPosRadius, lightPosRadius[ii], 4*sizeof(float) );
bx::memCopy(s_uniforms.m_lightRgbInnerR, m_lightRgbInnerR[ii], 3*sizeof(float) );
bx::memCopy(s_uniforms.m_color, m_lightRgbInnerR[ii], 3*sizeof(float) );
if (m_useStencilTexture)
{
bgfx::setViewFrameBuffer(viewId, s_stencilFb);
bgfx::setViewClear(viewId
, BGFX_CLEAR_COLOR
, 0x00000000
, 1.0f
, 0
);
}
else
{
const bgfx::FrameBufferHandle invalid = BGFX_INVALID_HANDLE;
bgfx::setViewFrameBuffer(viewId, invalid);
bgfx::setViewClear(viewId
, BGFX_CLEAR_STENCIL
, m_clearValues.m_clearRgba
, m_clearValues.m_clearDepth
, m_clearValues.m_clearStencil
);
}
// Create near clip volume for current light.
float nearClipVolume[6 * 4] = {};
float pointLight[4];
if (m_mixedSvImpl)
{
pointLight[0] = lightPos[0];
pointLight[1] = lightPos[1];
pointLight[2] = lightPos[2];
pointLight[3] = 1.0f;
createNearClipVolume(nearClipVolume, pointLight, m_viewState.m_view, fov, aspect, nearPlane);
}
for (uint8_t jj = 0; jj < shadowCastersCount[m_currentScene]; ++jj)
{
const Instance& instance = shadowCasters[m_currentScene][jj];
Model* model = instance.m_model;
ShadowVolumeImpl::Enum shadowVolumeImpl = m_shadowVolumeImpl;
if (m_mixedSvImpl)
{
// If instance is inside near clip volume, depth fail must be used, else depth pass is fine.
bool isInsideVolume = clipTest(nearClipVolume, 6, model->m_mesh, instance.m_scale, instance.m_pos);
shadowVolumeImpl = (isInsideVolume ? ShadowVolumeImpl::DepthFail : ShadowVolumeImpl::DepthPass);
}
s_uniforms.m_svparams.m_dfail = float(ShadowVolumeImpl::DepthFail == shadowVolumeImpl);
// Compute virtual light position for shadow volume generation.
float transformedLightPos[3];
shadowVolumeLightTransform(transformedLightPos
, instance.m_scale
, instance.m_rotation
, instance.m_pos
, lightPos
);
// Set virtual light pos.
bx::memCopy(s_uniforms.m_virtualLightPos_extrusionDist, transformedLightPos, 3*sizeof(float) );
s_uniforms.m_virtualLightPos_extrusionDist[3] = instance.m_svExtrusionDistance;
// Compute transform for shadow volume.
float shadowVolumeMtx[16];
bx::mtxSRT(shadowVolumeMtx
, instance.m_scale[0]
, instance.m_scale[1]
, instance.m_scale[2]
, instance.m_rotation[0]
, instance.m_rotation[1]
, instance.m_rotation[2]
, instance.m_pos[0]
, instance.m_pos[1]
, instance.m_pos[2]
);
GroupArray& groups = model->m_mesh.m_groups;
const uint16_t stride = model->m_mesh.m_layout.getStride();
for (GroupArray::iterator it = groups.begin(), itEnd = groups.end(); it != itEnd; ++it)
{
Group& group = *it;
// Create shadow volume.
ShadowVolume shadowVolume;
shadowVolumeCreate(shadowVolume
, group
, stride
, shadowVolumeMtx
, transformedLightPos
, shadowVolumeImpl
, m_shadowVolumeAlgorithm
, m_useStencilTexture
);
m_numShadowVolumeVertices += shadowVolume.m_numVertices;
m_numShadowVolumeIndices += shadowVolume.m_numIndices;
ShadowVolumeProgramType::Enum programIndex = ShadowVolumeProgramType::Blank;
RenderState::Enum renderStateIndex;
if (m_useStencilTexture)
{
renderStateIndex = ShadowVolumeImpl::DepthFail == shadowVolumeImpl
? RenderState::ShadowVolume_UsingStencilTexture_CraftStencil_DepthFail
: RenderState::ShadowVolume_UsingStencilTexture_CraftStencil_DepthPass
;
programIndex = ShadowVolumeAlgorithm::FaceBased == m_shadowVolumeAlgorithm
? ShadowVolumeProgramType::Tex1
: ShadowVolumeProgramType::Tex2
;
}
else
{
renderStateIndex = ShadowVolumeImpl::DepthFail == shadowVolumeImpl
? RenderState::ShadowVolume_UsingStencilBuffer_CraftStencil_DepthFail
: RenderState::ShadowVolume_UsingStencilBuffer_CraftStencil_DepthPass
;
}
const RenderState& renderStateCraftStencil = s_renderStates[renderStateIndex];
s_uniforms.submitPerDrawUniforms();
bgfx::setTransform(shadowVolumeMtx);
bgfx::setVertexBuffer(0, shadowVolume.m_vbSides);
bgfx::setIndexBuffer(shadowVolume.m_ibSides);
setRenderState(renderStateCraftStencil);
::submit(viewId, m_svProgs[programIndex][ShadowVolumePart::Side]);
if (shadowVolume.m_cap)
{
s_uniforms.submitPerDrawUniforms();
bgfx::setTransform(shadowVolumeMtx);
bgfx::setVertexBuffer(0, group.m_vbh);
bgfx::setIndexBuffer(shadowVolume.m_ibFrontCap);
setRenderState(renderStateCraftStencil);
::submit(viewId, m_svProgs[programIndex][ShadowVolumePart::Front]);
s_uniforms.submitPerDrawUniforms();
bgfx::setTransform(shadowVolumeMtx);
bgfx::setVertexBuffer(0, group.m_vbh);
bgfx::setIndexBuffer(shadowVolume.m_ibBackCap);
::setRenderState(renderStateCraftStencil);
::submit(viewId, m_svProgs[programIndex][ShadowVolumePart::Back]);
}
if (m_drawShadowVolumes)
{
const RenderState& renderState = s_renderStates[RenderState::Custom_DrawShadowVolume_Lines];
s_uniforms.submitPerDrawUniforms();
bgfx::setTransform(shadowVolumeMtx);
bgfx::setVertexBuffer(0, shadowVolume.m_vbSides);
bgfx::setIndexBuffer(shadowVolume.m_ibSides);
::setRenderState(renderState);
::submit(VIEWID_RANGE1_PASS3, m_svProgs[ShadowVolumeProgramType::Color][ShadowVolumePart::Side]);
if (shadowVolume.m_cap)
{
s_uniforms.submitPerDrawUniforms();
bgfx::setTransform(shadowVolumeMtx);
bgfx::setVertexBuffer(0, group.m_vbh);
bgfx::setIndexBuffer(shadowVolume.m_ibFrontCap);
::setRenderState(renderState);
::submit(VIEWID_RANGE1_PASS3, m_svProgs[ShadowVolumeProgramType::Color][ShadowVolumePart::Front]);
s_uniforms.submitPerDrawUniforms();
bgfx::setTransform(shadowVolumeMtx);
bgfx::setVertexBuffer(0, group.m_vbh);
bgfx::setIndexBuffer(shadowVolume.m_ibBackCap);
::setRenderState(renderState);
::submit(VIEWID_RANGE1_PASS3, m_svProgs[ShadowVolumeProgramType::Color][ShadowVolumePart::Back]);
}
}
}
}
// Draw diffuse only.
s_uniforms.m_params.m_ambientPass = 0.0f;
s_uniforms.m_params.m_lightingPass = 1.0f;
RenderState& drawDiffuse = m_useStencilTexture
? s_renderStates[RenderState::ShadowVolume_UsingStencilTexture_DrawDiffuse]
: s_renderStates[RenderState::ShadowVolume_UsingStencilBuffer_DrawDiffuse]
;
// If using stencil texture, viewId is set to render target. Incr it to render to default back buffer.
viewId += uint8_t(m_useStencilTexture);
// Draw shadow casters.
for (uint8_t jj = 0; jj < shadowCastersCount[m_currentScene]; ++jj)
{
shadowCasters[m_currentScene][jj].submit(viewId, drawDiffuse);
}
// Draw shadow receivers.
for (uint8_t jj = 0; jj < shadowReceiversCount[m_currentScene]; ++jj)
{
shadowReceivers[m_currentScene][jj].submit(viewId, drawDiffuse);
}
}
m_profTime = bx::getHPCounter() - m_profTime;
// Lights.
const float lightScale[3] = { 1.5f, 1.5f, 1.5f };
for (uint8_t ii = 0; ii < m_numLights; ++ii)
{
bx::memCopy(s_uniforms.m_color, m_lightRgbInnerR[ii], 3*sizeof(float) );
float lightMtx[16];
mtxBillboard(lightMtx, m_viewState.m_view, lightPosRadius[ii], lightScale);
m_vplaneModel.submit(VIEWID_RANGE1_PASS3, lightMtx, s_renderStates[RenderState::Custom_BlendLightTexture]);
}
// Setup view rect and transform for all used views.
setViewRectMask(s_viewMask, 0, 0, uint16_t(m_viewState.m_width), uint16_t(m_viewState.m_height) );
setViewTransformMask(s_viewMask, m_viewState.m_view, m_viewState.m_proj);
s_viewMask = 0;
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
// Swap memory pages.
s_svAllocator.swap();
// Reset clear values.
setViewClearMask(UINT32_MAX
, BGFX_CLEAR_NONE
, m_clearValues.m_clearRgba
, m_clearValues.m_clearDepth
, m_clearValues.m_clearStencil
);
return true;
}
return false;
}
ViewState m_viewState;
ClearValues m_clearValues;
uint32_t m_debug;
uint32_t m_reset;
bgfx::TextureHandle m_figureTex;
bgfx::TextureHandle m_flareTex;
bgfx::TextureHandle m_fieldstoneTex;
bgfx::ProgramHandle m_programTextureLighting;
bgfx::ProgramHandle m_programColorLighting;
bgfx::ProgramHandle m_programColorTexture;
bgfx::ProgramHandle m_programTexture;
bgfx::ProgramHandle m_programBackBlank;
bgfx::ProgramHandle m_programSideBlank;
bgfx::ProgramHandle m_programFrontBlank;
bgfx::ProgramHandle m_programBackColor;
bgfx::ProgramHandle m_programSideColor;
bgfx::ProgramHandle m_programFrontColor;
bgfx::ProgramHandle m_programSideTex;
bgfx::ProgramHandle m_programBackTex1;
bgfx::ProgramHandle m_programBackTex2;
bgfx::ProgramHandle m_programFrontTex1;
bgfx::ProgramHandle m_programFrontTex2;
bgfx::ProgramHandle m_svProgs[ShadowVolumeProgramType::Count][ShadowVolumePart::Count];
Model m_bunnyLowPolyModel;
Model m_bunnyHighPolyModel;
Model m_columnModel;
Model m_platformModel;
Model m_cubeModel;
Model m_hplaneFieldModel;
Model m_hplaneFigureModel;
Model m_vplaneModel;
float m_lightRgbInnerR[MAX_LIGHTS_COUNT][4];
int64_t m_profTime;
int64_t m_timeOffset;
uint32_t m_numShadowVolumeVertices;
uint32_t m_numShadowVolumeIndices;
uint32_t m_oldWidth;
uint32_t m_oldHeight;
int32_t m_numLights;
int32_t m_instanceCount;
bool m_showHelp;
bool m_updateLights;
bool m_updateScene;
bool m_mixedSvImpl;
bool m_useStencilTexture;
bool m_drawShadowVolumes;
ShadowVolumeImpl::Enum m_shadowVolumeImpl;
ShadowVolumeAlgorithm::Enum m_shadowVolumeAlgorithm;
LightPattern m_lightPattern;
MeshChoice m_currentMesh;
Scene m_currentScene;
entry::MouseState m_mouseState;
};
} // namespace
ENTRY_IMPLEMENT_MAIN(
ExampleShadowVolumes
, "14-shadowvolumes"
, "Shadow volumes."
, "https://bkaradzic.github.io/bgfx/examples.html#shadowvolumes");
|