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 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499
|
/**
* cgltf - a single-file glTF 2.0 parser written in C99.
*
* Version: 1.1
*
* Website: https://github.com/jkuhlmann/cgltf
*
* Distributed under the MIT License, see notice at the end of this file.
*
* Building:
* Include this file where you need the struct and function
* declarations. Have exactly one source file where you define
* `CGLTF_IMPLEMENTATION` before including this file to get the
* function definitions.
*
* Reference:
* `cgltf_result cgltf_parse(const cgltf_options*, const void*,
* cgltf_size, cgltf_data**)` parses both glTF and GLB data. If
* this function returns `cgltf_result_success`, you have to call
* `cgltf_free()` on the created `cgltf_data*` variable.
* Note that contents of external files for buffers and images are not
* automatically loaded. You'll need to read these files yourself using
* URIs in the `cgltf_data` structure.
*
* `cgltf_options` is the struct passed to `cgltf_parse()` to control
* parts of the parsing process. You can use it to force the file type
* and provide memory allocation callbacks. Should be zero-initialized
* to trigger default behavior.
*
* `cgltf_data` is the struct allocated and filled by `cgltf_parse()`.
* It generally mirrors the glTF format as described by the spec (see
* https://github.com/KhronosGroup/glTF/tree/master/specification/2.0).
*
* `void cgltf_free(cgltf_data*)` frees the allocated `cgltf_data`
* variable.
*
* `cgltf_result cgltf_load_buffers(const cgltf_options*, cgltf_data*,
* const char*)` can be optionally called to open and read buffer
* files using the `FILE*` APIs.
*
* `cgltf_result cgltf_load_buffer_base64(const cgltf_options* options,
* cgltf_size size, const char* base64, void** out_data)` decodes
* base64-encoded data content. Used internally by `cgltf_load_buffers()`
* and may be useful if you're not dealing with normal files.
*
* `cgltf_result cgltf_parse_file(const cgltf_options* options, const
* char* path, cgltf_data** out_data)` can be used to open the given
* file using `FILE*` APIs and parse the data using `cgltf_parse()`.
*
* `cgltf_result cgltf_validate(cgltf_data*)` can be used to do additional
* checks to make sure the parsed glTF data is valid.
*
* `cgltf_node_transform_local` converts the translation / rotation / scale properties of a node
* into a mat4.
*
* `cgltf_node_transform_world` calls `cgltf_node_transform_local` on every ancestor in order
* to compute the root-to-node transformation.
*
* `cgltf_accessor_read_float` reads a certain element from an accessor and converts it to
* floating point, assuming that `cgltf_load_buffers` has already been called. The passed-in element
* size is the number of floats in the output buffer, which should be in the range [1, 16]. Returns
* false if the passed-in element_size is too small, or if the accessor is sparse.
*
* `cgltf_accessor_read_index` is similar to its floating-point counterpart, but it returns size_t
* and only works with single-component data types.
*/
#ifndef CGLTF_H_INCLUDED__
#define CGLTF_H_INCLUDED__
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef size_t cgltf_size;
typedef float cgltf_float;
typedef int cgltf_int;
typedef int cgltf_bool;
typedef enum cgltf_file_type
{
cgltf_file_type_invalid,
cgltf_file_type_gltf,
cgltf_file_type_glb,
} cgltf_file_type;
typedef struct cgltf_options
{
cgltf_file_type type; /* invalid == auto detect */
cgltf_size json_token_count; /* 0 == auto */
void* (*memory_alloc)(void* user, cgltf_size size);
void (*memory_free) (void* user, void* ptr);
void* memory_user_data;
} cgltf_options;
typedef enum cgltf_result
{
cgltf_result_success,
cgltf_result_data_too_short,
cgltf_result_unknown_format,
cgltf_result_invalid_json,
cgltf_result_invalid_gltf,
cgltf_result_invalid_options,
cgltf_result_file_not_found,
cgltf_result_io_error,
cgltf_result_out_of_memory,
} cgltf_result;
typedef enum cgltf_buffer_view_type
{
cgltf_buffer_view_type_invalid,
cgltf_buffer_view_type_indices,
cgltf_buffer_view_type_vertices,
} cgltf_buffer_view_type;
typedef enum cgltf_attribute_type
{
cgltf_attribute_type_invalid,
cgltf_attribute_type_position,
cgltf_attribute_type_normal,
cgltf_attribute_type_tangent,
cgltf_attribute_type_texcoord,
cgltf_attribute_type_color,
cgltf_attribute_type_joints,
cgltf_attribute_type_weights,
} cgltf_attribute_type;
typedef enum cgltf_component_type
{
cgltf_component_type_invalid,
cgltf_component_type_r_8, /* BYTE */
cgltf_component_type_r_8u, /* UNSIGNED_BYTE */
cgltf_component_type_r_16, /* SHORT */
cgltf_component_type_r_16u, /* UNSIGNED_SHORT */
cgltf_component_type_r_32u, /* UNSIGNED_INT */
cgltf_component_type_r_32f, /* FLOAT */
} cgltf_component_type;
typedef enum cgltf_type
{
cgltf_type_invalid,
cgltf_type_scalar,
cgltf_type_vec2,
cgltf_type_vec3,
cgltf_type_vec4,
cgltf_type_mat2,
cgltf_type_mat3,
cgltf_type_mat4,
} cgltf_type;
typedef enum cgltf_primitive_type
{
cgltf_primitive_type_points,
cgltf_primitive_type_lines,
cgltf_primitive_type_line_loop,
cgltf_primitive_type_line_strip,
cgltf_primitive_type_triangles,
cgltf_primitive_type_triangle_strip,
cgltf_primitive_type_triangle_fan,
} cgltf_primitive_type;
typedef enum cgltf_alpha_mode
{
cgltf_alpha_mode_opaque,
cgltf_alpha_mode_mask,
cgltf_alpha_mode_blend,
} cgltf_alpha_mode;
typedef enum cgltf_animation_path_type {
cgltf_animation_path_type_invalid,
cgltf_animation_path_type_translation,
cgltf_animation_path_type_rotation,
cgltf_animation_path_type_scale,
cgltf_animation_path_type_weights,
} cgltf_animation_path_type;
typedef enum cgltf_interpolation_type {
cgltf_interpolation_type_linear,
cgltf_interpolation_type_step,
cgltf_interpolation_type_cubic_spline,
} cgltf_interpolation_type;
typedef enum cgltf_camera_type {
cgltf_camera_type_invalid,
cgltf_camera_type_perspective,
cgltf_camera_type_orthographic,
} cgltf_camera_type;
typedef enum cgltf_light_type {
cgltf_light_type_invalid,
cgltf_light_type_directional,
cgltf_light_type_point,
cgltf_light_type_spot,
} cgltf_light_type;
typedef struct cgltf_buffer
{
cgltf_size size;
char* uri;
void* data; /* loaded by cgltf_load_buffers */
} cgltf_buffer;
typedef struct cgltf_buffer_view
{
cgltf_buffer* buffer;
cgltf_size offset;
cgltf_size size;
cgltf_size stride; /* 0 == automatically determined by accessor */
cgltf_buffer_view_type type;
} cgltf_buffer_view;
typedef struct cgltf_accessor_sparse
{
cgltf_size count;
cgltf_buffer_view* indices_buffer_view;
cgltf_size indices_byte_offset;
cgltf_component_type indices_component_type;
cgltf_buffer_view* values_buffer_view;
cgltf_size values_byte_offset;
} cgltf_accessor_sparse;
typedef struct cgltf_accessor
{
cgltf_component_type component_type;
cgltf_bool normalized;
cgltf_type type;
cgltf_size offset;
cgltf_size count;
cgltf_size stride;
cgltf_buffer_view* buffer_view;
cgltf_bool has_min;
cgltf_float min[16];
cgltf_bool has_max;
cgltf_float max[16];
cgltf_bool is_sparse;
cgltf_accessor_sparse sparse;
} cgltf_accessor;
typedef struct cgltf_attribute
{
char* name;
cgltf_attribute_type type;
cgltf_int index;
cgltf_accessor* data;
} cgltf_attribute;
typedef struct cgltf_image
{
char* name;
char* uri;
cgltf_buffer_view* buffer_view;
char* mime_type;
} cgltf_image;
typedef struct cgltf_sampler
{
cgltf_int mag_filter;
cgltf_int min_filter;
cgltf_int wrap_s;
cgltf_int wrap_t;
} cgltf_sampler;
typedef struct cgltf_texture
{
char* name;
cgltf_image* image;
cgltf_sampler* sampler;
} cgltf_texture;
typedef struct cgltf_texture_transform
{
cgltf_float offset[2];
cgltf_float rotation;
cgltf_float scale[2];
cgltf_int texcoord;
} cgltf_texture_transform;
typedef struct cgltf_texture_view
{
cgltf_texture* texture;
cgltf_int texcoord;
cgltf_float scale; /* equivalent to strength for occlusion_texture */
cgltf_bool has_transform;
cgltf_texture_transform transform;
} cgltf_texture_view;
typedef struct cgltf_pbr_metallic_roughness
{
cgltf_texture_view base_color_texture;
cgltf_texture_view metallic_roughness_texture;
cgltf_float base_color_factor[4];
cgltf_float metallic_factor;
cgltf_float roughness_factor;
} cgltf_pbr_metallic_roughness;
typedef struct cgltf_pbr_specular_glossiness
{
cgltf_texture_view diffuse_texture;
cgltf_texture_view specular_glossiness_texture;
cgltf_float diffuse_factor[4];
cgltf_float specular_factor[3];
cgltf_float glossiness_factor;
} cgltf_pbr_specular_glossiness;
typedef struct cgltf_material
{
char* name;
cgltf_bool has_pbr_metallic_roughness;
cgltf_bool has_pbr_specular_glossiness;
cgltf_pbr_metallic_roughness pbr_metallic_roughness;
cgltf_pbr_specular_glossiness pbr_specular_glossiness;
cgltf_texture_view normal_texture;
cgltf_texture_view occlusion_texture;
cgltf_texture_view emissive_texture;
cgltf_float emissive_factor[3];
cgltf_alpha_mode alpha_mode;
cgltf_float alpha_cutoff;
cgltf_bool double_sided;
cgltf_bool unlit;
} cgltf_material;
typedef struct cgltf_morph_target {
cgltf_attribute* attributes;
cgltf_size attributes_count;
} cgltf_morph_target;
typedef struct cgltf_primitive {
cgltf_primitive_type type;
cgltf_accessor* indices;
cgltf_material* material;
cgltf_attribute* attributes;
cgltf_size attributes_count;
cgltf_morph_target* targets;
cgltf_size targets_count;
} cgltf_primitive;
typedef struct cgltf_mesh {
char* name;
cgltf_primitive* primitives;
cgltf_size primitives_count;
cgltf_float* weights;
cgltf_size weights_count;
} cgltf_mesh;
typedef struct cgltf_node cgltf_node;
typedef struct cgltf_skin {
char* name;
cgltf_node** joints;
cgltf_size joints_count;
cgltf_node* skeleton;
cgltf_accessor* inverse_bind_matrices;
} cgltf_skin;
typedef struct cgltf_camera_perspective {
cgltf_float aspect_ratio;
cgltf_float yfov;
cgltf_float zfar;
cgltf_float znear;
} cgltf_camera_perspective;
typedef struct cgltf_camera_orthographic {
cgltf_float xmag;
cgltf_float ymag;
cgltf_float zfar;
cgltf_float znear;
} cgltf_camera_orthographic;
typedef struct cgltf_camera {
char* name;
cgltf_camera_type type;
union {
cgltf_camera_perspective perspective;
cgltf_camera_orthographic orthographic;
};
} cgltf_camera;
typedef struct cgltf_light {
char* name;
cgltf_float color[3];
cgltf_float intensity;
cgltf_light_type type;
cgltf_float range;
cgltf_float spot_inner_cone_angle;
cgltf_float spot_outer_cone_angle;
} cgltf_light;
typedef struct cgltf_node {
char* name;
cgltf_node* parent;
cgltf_node** children;
cgltf_size children_count;
cgltf_skin* skin;
cgltf_mesh* mesh;
cgltf_camera* camera;
cgltf_light* light;
cgltf_float* weights;
cgltf_size weights_count;
cgltf_bool has_translation;
cgltf_bool has_rotation;
cgltf_bool has_scale;
cgltf_bool has_matrix;
cgltf_float translation[3];
cgltf_float rotation[4];
cgltf_float scale[3];
cgltf_float matrix[16];
} cgltf_node;
typedef struct cgltf_scene {
char* name;
cgltf_node** nodes;
cgltf_size nodes_count;
} cgltf_scene;
typedef struct cgltf_animation_sampler {
cgltf_accessor* input;
cgltf_accessor* output;
cgltf_interpolation_type interpolation;
} cgltf_animation_sampler;
typedef struct cgltf_animation_channel {
cgltf_animation_sampler* sampler;
cgltf_node* target_node;
cgltf_animation_path_type target_path;
} cgltf_animation_channel;
typedef struct cgltf_animation {
char* name;
cgltf_animation_sampler* samplers;
cgltf_size samplers_count;
cgltf_animation_channel* channels;
cgltf_size channels_count;
} cgltf_animation;
typedef struct cgltf_asset {
char* copyright;
char* generator;
char* version;
char* min_version;
} cgltf_asset;
typedef struct cgltf_data
{
cgltf_file_type file_type;
void* file_data;
cgltf_asset asset;
cgltf_mesh* meshes;
cgltf_size meshes_count;
cgltf_material* materials;
cgltf_size materials_count;
cgltf_accessor* accessors;
cgltf_size accessors_count;
cgltf_buffer_view* buffer_views;
cgltf_size buffer_views_count;
cgltf_buffer* buffers;
cgltf_size buffers_count;
cgltf_image* images;
cgltf_size images_count;
cgltf_texture* textures;
cgltf_size textures_count;
cgltf_sampler* samplers;
cgltf_size samplers_count;
cgltf_skin* skins;
cgltf_size skins_count;
cgltf_camera* cameras;
cgltf_size cameras_count;
cgltf_light* lights;
cgltf_size lights_count;
cgltf_node* nodes;
cgltf_size nodes_count;
cgltf_scene* scenes;
cgltf_size scenes_count;
cgltf_scene* scene;
cgltf_animation* animations;
cgltf_size animations_count;
const void* bin;
cgltf_size bin_size;
void (*memory_free) (void* user, void* ptr);
void* memory_user_data;
} cgltf_data;
cgltf_result cgltf_parse(
const cgltf_options* options,
const void* data,
cgltf_size size,
cgltf_data** out_data);
cgltf_result cgltf_parse_file(
const cgltf_options* options,
const char* path,
cgltf_data** out_data);
cgltf_result cgltf_load_buffers(
const cgltf_options* options,
cgltf_data* data,
const char* base_path);
cgltf_result cgltf_load_buffer_base64(const cgltf_options* options, cgltf_size size, const char* base64, void** out_data);
cgltf_result cgltf_validate(
cgltf_data* data);
void cgltf_free(cgltf_data* data);
void cgltf_node_transform_local(const cgltf_node* node, cgltf_float* out_matrix);
void cgltf_node_transform_world(const cgltf_node* node, cgltf_float* out_matrix);
cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size index, cgltf_float* out, cgltf_size element_size);
cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size index);
#ifdef __cplusplus
}
#endif
#endif /* #ifndef CGLTF_H_INCLUDED__ */
/*
*
* Stop now, if you are only interested in the API.
* Below, you find the implementation.
*
*/
#ifdef __INTELLISENSE__
/* This makes MSVC intellisense work. */
#define CGLTF_IMPLEMENTATION
#endif
#ifdef CGLTF_IMPLEMENTATION
#include <stdint.h> /* For uint8_t, uint32_t */
#include <string.h> /* For strncpy */
#include <stdlib.h> /* For malloc, free */
#include <stdio.h> /* For fopen */
#include <limits.h> /* For UINT_MAX etc */
/*
* -- jsmn.h start --
* Source: https://github.com/zserge/jsmn
* License: MIT
*/
typedef enum {
JSMN_UNDEFINED = 0,
JSMN_OBJECT = 1,
JSMN_ARRAY = 2,
JSMN_STRING = 3,
JSMN_PRIMITIVE = 4
} jsmntype_t;
enum jsmnerr {
/* Not enough tokens were provided */
JSMN_ERROR_NOMEM = -1,
/* Invalid character inside JSON string */
JSMN_ERROR_INVAL = -2,
/* The string is not a full JSON packet, more bytes expected */
JSMN_ERROR_PART = -3
};
typedef struct {
jsmntype_t type;
int start;
int end;
int size;
#ifdef JSMN_PARENT_LINKS
int parent;
#endif
} jsmntok_t;
typedef struct {
unsigned int pos; /* offset in the JSON string */
unsigned int toknext; /* next token to allocate */
int toksuper; /* superior token node, e.g parent object or array */
} jsmn_parser;
static void jsmn_init(jsmn_parser *parser);
static int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, size_t num_tokens);
/*
* -- jsmn.h end --
*/
static const cgltf_size GlbHeaderSize = 12;
static const cgltf_size GlbChunkHeaderSize = 8;
static const uint32_t GlbVersion = 2;
static const uint32_t GlbMagic = 0x46546C67;
static const uint32_t GlbMagicJsonChunk = 0x4E4F534A;
static const uint32_t GlbMagicBinChunk = 0x004E4942;
static void* cgltf_default_alloc(void* user, cgltf_size size)
{
return malloc(size);
}
static void cgltf_default_free(void* user, void* ptr)
{
free(ptr);
}
static void* cgltf_calloc(cgltf_options* options, size_t element_size, cgltf_size count)
{
if (SIZE_MAX / element_size < count)
{
return NULL;
}
void* result = options->memory_alloc(options->memory_user_data, element_size * count);
if (!result)
{
return NULL;
}
memset(result, 0, element_size * count);
return result;
}
static cgltf_result cgltf_parse_json(cgltf_options* options, const uint8_t* json_chunk, cgltf_size size, cgltf_data** out_data);
cgltf_result cgltf_parse(const cgltf_options* options, const void* data, cgltf_size size, cgltf_data** out_data)
{
if (size < GlbHeaderSize)
{
return cgltf_result_data_too_short;
}
if (options == NULL)
{
return cgltf_result_invalid_options;
}
cgltf_options fixed_options = *options;
if (fixed_options.memory_alloc == NULL)
{
fixed_options.memory_alloc = &cgltf_default_alloc;
}
if (fixed_options.memory_free == NULL)
{
fixed_options.memory_free = &cgltf_default_free;
}
uint32_t tmp;
// Magic
memcpy(&tmp, data, 4);
if (tmp != GlbMagic)
{
if (fixed_options.type == cgltf_file_type_invalid)
{
fixed_options.type = cgltf_file_type_gltf;
}
else if (fixed_options.type == cgltf_file_type_glb)
{
return cgltf_result_unknown_format;
}
}
if (fixed_options.type == cgltf_file_type_gltf)
{
cgltf_result json_result = cgltf_parse_json(&fixed_options, (const uint8_t*)data, size, out_data);
if (json_result != cgltf_result_success)
{
return json_result;
}
(*out_data)->file_type = cgltf_file_type_gltf;
return cgltf_result_success;
}
const uint8_t* ptr = (const uint8_t*)data;
// Version
memcpy(&tmp, ptr + 4, 4);
uint32_t version = tmp;
if (version != GlbVersion)
{
return cgltf_result_unknown_format;
}
// Total length
memcpy(&tmp, ptr + 8, 4);
if (tmp > size)
{
return cgltf_result_data_too_short;
}
const uint8_t* json_chunk = ptr + GlbHeaderSize;
if (GlbHeaderSize + GlbChunkHeaderSize > size)
{
return cgltf_result_data_too_short;
}
// JSON chunk: length
uint32_t json_length;
memcpy(&json_length, json_chunk, 4);
if (GlbHeaderSize + GlbChunkHeaderSize + json_length > size)
{
return cgltf_result_data_too_short;
}
// JSON chunk: magic
memcpy(&tmp, json_chunk + 4, 4);
if (tmp != GlbMagicJsonChunk)
{
return cgltf_result_unknown_format;
}
json_chunk += GlbChunkHeaderSize;
const void* bin = 0;
cgltf_size bin_size = 0;
if (GlbHeaderSize + GlbChunkHeaderSize + json_length + GlbChunkHeaderSize <= size)
{
// We can read another chunk
const uint8_t* bin_chunk = json_chunk + json_length;
// Bin chunk: length
uint32_t bin_length;
memcpy(&bin_length, bin_chunk, 4);
if (GlbHeaderSize + GlbChunkHeaderSize + json_length + GlbChunkHeaderSize + bin_length > size)
{
return cgltf_result_data_too_short;
}
// Bin chunk: magic
memcpy(&tmp, bin_chunk + 4, 4);
if (tmp != GlbMagicBinChunk)
{
return cgltf_result_unknown_format;
}
bin_chunk += GlbChunkHeaderSize;
bin = bin_chunk;
bin_size = bin_length;
}
cgltf_result json_result = cgltf_parse_json(&fixed_options, json_chunk, json_length, out_data);
if (json_result != cgltf_result_success)
{
return json_result;
}
(*out_data)->file_type = cgltf_file_type_glb;
(*out_data)->bin = bin;
(*out_data)->bin_size = bin_size;
return cgltf_result_success;
}
cgltf_result cgltf_parse_file(const cgltf_options* options, const char* path, cgltf_data** out_data)
{
if (options == NULL)
{
return cgltf_result_invalid_options;
}
void* (*memory_alloc)(void*, cgltf_size) = options->memory_alloc ? options->memory_alloc : &cgltf_default_alloc;
void (*memory_free)(void*, void*) = options->memory_free ? options->memory_free : &cgltf_default_free;
FILE* file = fopen(path, "rb");
if (!file)
{
return cgltf_result_file_not_found;
}
fseek(file, 0, SEEK_END);
long length = ftell(file);
if (length < 0)
{
fclose(file);
return cgltf_result_io_error;
}
fseek(file, 0, SEEK_SET);
char* file_data = (char*)memory_alloc(options->memory_user_data, length);
if (!file_data)
{
fclose(file);
return cgltf_result_out_of_memory;
}
cgltf_size file_size = (cgltf_size)length;
cgltf_size read_size = fread(file_data, 1, file_size, file);
fclose(file);
if (read_size != file_size)
{
memory_free(options->memory_user_data, file_data);
return cgltf_result_io_error;
}
cgltf_result result = cgltf_parse(options, file_data, file_size, out_data);
if (result != cgltf_result_success)
{
memory_free(options->memory_user_data, file_data);
return result;
}
(*out_data)->file_data = file_data;
return cgltf_result_success;
}
static void cgltf_combine_paths(char* path, const char* base, const char* uri)
{
const char* s0 = strrchr(base, '/');
const char* s1 = strrchr(base, '\\');
const char* slash = s0 ? (s1 && s1 > s0 ? s1 : s0) : s1;
if (slash)
{
size_t prefix = slash - base + 1;
strncpy(path, base, prefix);
strcpy(path + prefix, uri);
}
else
{
strcpy(path, base);
}
}
static cgltf_result cgltf_load_buffer_file(const cgltf_options* options, cgltf_size size, const char* uri, const char* base_path, void** out_data)
{
void* (*memory_alloc)(void*, cgltf_size) = options->memory_alloc ? options->memory_alloc : &cgltf_default_alloc;
void (*memory_free)(void*, void*) = options->memory_free ? options->memory_free : &cgltf_default_free;
char* path = (char*)memory_alloc(options->memory_user_data, strlen(uri) + strlen(base_path) + 1);
if (!path)
{
return cgltf_result_out_of_memory;
}
cgltf_combine_paths(path, base_path, uri);
FILE* file = fopen(path, "rb");
memory_free(options->memory_user_data, path);
if (!file)
{
return cgltf_result_file_not_found;
}
char* file_data = (char*)memory_alloc(options->memory_user_data, size);
if (!file_data)
{
fclose(file);
return cgltf_result_out_of_memory;
}
cgltf_size read_size = fread(file_data, 1, size, file);
fclose(file);
if (read_size != size)
{
memory_free(options->memory_user_data, file_data);
return cgltf_result_io_error;
}
*out_data = file_data;
return cgltf_result_success;
}
cgltf_result cgltf_load_buffer_base64(const cgltf_options* options, cgltf_size size, const char* base64, void** out_data)
{
void* (*memory_alloc)(void*, cgltf_size) = options->memory_alloc ? options->memory_alloc : &cgltf_default_alloc;
void (*memory_free)(void*, void*) = options->memory_free ? options->memory_free : &cgltf_default_free;
unsigned char* data = (unsigned char*)memory_alloc(options->memory_user_data, size);
if (!data)
{
return cgltf_result_out_of_memory;
}
unsigned int buffer = 0;
unsigned int buffer_bits = 0;
for (cgltf_size i = 0; i < size; ++i)
{
while (buffer_bits < 8)
{
char ch = *base64++;
int index =
(unsigned)(ch - 'A') < 26 ? (ch - 'A') :
(unsigned)(ch - 'a') < 26 ? (ch - 'a') + 26 :
(unsigned)(ch - '0') < 10 ? (ch - '0') + 52 :
ch == '+' ? 62 :
ch == '/' ? 63 :
-1;
if (index < 0)
{
memory_free(options->memory_user_data, data);
return cgltf_result_io_error;
}
buffer = (buffer << 6) | index;
buffer_bits += 6;
}
data[i] = (unsigned char)(buffer >> (buffer_bits - 8));
buffer_bits -= 8;
}
*out_data = data;
return cgltf_result_success;
}
cgltf_result cgltf_load_buffers(const cgltf_options* options, cgltf_data* data, const char* base_path)
{
if (options == NULL)
{
return cgltf_result_invalid_options;
}
if (data->buffers_count && data->buffers[0].data == NULL && data->buffers[0].uri == NULL && data->bin)
{
if (data->bin_size < data->buffers[0].size)
{
return cgltf_result_data_too_short;
}
data->buffers[0].data = (void*)data->bin;
}
for (cgltf_size i = 0; i < data->buffers_count; ++i)
{
if (data->buffers[i].data)
{
continue;
}
const char* uri = data->buffers[i].uri;
if (uri == NULL)
{
continue;
}
if (strncmp(uri, "data:", 5) == 0)
{
const char* comma = strchr(uri, ',');
if (comma && comma - uri >= 7 && strncmp(comma - 7, ";base64", 7) == 0)
{
cgltf_result res = cgltf_load_buffer_base64(options, data->buffers[i].size, comma + 1, &data->buffers[i].data);
if (res != cgltf_result_success)
{
return res;
}
}
else
{
return cgltf_result_unknown_format;
}
}
else if (strstr(uri, "://") == NULL)
{
cgltf_result res = cgltf_load_buffer_file(options, data->buffers[i].size, uri, base_path, &data->buffers[i].data);
if (res != cgltf_result_success)
{
return res;
}
}
else
{
return cgltf_result_unknown_format;
}
}
return cgltf_result_success;
}
static cgltf_size cgltf_calc_size(cgltf_type type, cgltf_component_type component_type);
static cgltf_size cgltf_calc_index_bound(cgltf_buffer_view* buffer_view, cgltf_size offset, cgltf_component_type component_type, cgltf_size count)
{
char* data = (char*)buffer_view->buffer->data + offset + buffer_view->offset;
cgltf_size bound = 0;
switch (component_type)
{
case cgltf_component_type_r_8u:
for (size_t i = 0; i < count; ++i)
{
cgltf_size v = ((unsigned char*)data)[i];
bound = bound > v ? bound : v;
}
break;
case cgltf_component_type_r_16u:
for (size_t i = 0; i < count; ++i)
{
cgltf_size v = ((unsigned short*)data)[i];
bound = bound > v ? bound : v;
}
break;
case cgltf_component_type_r_32u:
for (size_t i = 0; i < count; ++i)
{
cgltf_size v = ((unsigned int*)data)[i];
bound = bound > v ? bound : v;
}
break;
default:
;
}
return bound;
}
cgltf_result cgltf_validate(cgltf_data* data)
{
for (cgltf_size i = 0; i < data->accessors_count; ++i)
{
cgltf_accessor* accessor = &data->accessors[i];
cgltf_size element_size = cgltf_calc_size(accessor->type, accessor->component_type);
if (accessor->buffer_view)
{
cgltf_size req_size = accessor->offset + accessor->stride * (accessor->count - 1) + element_size;
if (accessor->buffer_view->size < req_size)
{
return cgltf_result_data_too_short;
}
}
if (accessor->is_sparse)
{
cgltf_accessor_sparse* sparse = &accessor->sparse;
cgltf_size indices_component_size = cgltf_calc_size(cgltf_type_scalar, sparse->indices_component_type);
cgltf_size indices_req_size = sparse->indices_byte_offset + indices_component_size * sparse->count;
cgltf_size values_req_size = sparse->values_byte_offset + element_size * sparse->count;
if (sparse->indices_buffer_view->size < indices_req_size ||
sparse->values_buffer_view->size < values_req_size)
{
return cgltf_result_data_too_short;
}
if (sparse->indices_component_type != cgltf_component_type_r_8u &&
sparse->indices_component_type != cgltf_component_type_r_16u &&
sparse->indices_component_type != cgltf_component_type_r_32u)
{
return cgltf_result_invalid_gltf;
}
if (sparse->indices_buffer_view->buffer->data)
{
cgltf_size index_bound = cgltf_calc_index_bound(sparse->indices_buffer_view, sparse->indices_byte_offset, sparse->indices_component_type, sparse->count);
if (index_bound >= accessor->count)
{
return cgltf_result_data_too_short;
}
}
}
}
for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
{
cgltf_size req_size = data->buffer_views[i].offset + data->buffer_views[i].size;
if (data->buffer_views[i].buffer && data->buffer_views[i].buffer->size < req_size)
{
return cgltf_result_data_too_short;
}
}
for (cgltf_size i = 0; i < data->meshes_count; ++i)
{
if (data->meshes[i].weights)
{
if (data->meshes[i].primitives_count && data->meshes[i].primitives[0].targets_count != data->meshes[i].weights_count)
{
return cgltf_result_invalid_gltf;
}
}
for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j)
{
if (data->meshes[i].primitives[j].targets_count != data->meshes[i].primitives[0].targets_count)
{
return cgltf_result_invalid_gltf;
}
if (data->meshes[i].primitives[j].attributes_count)
{
cgltf_accessor* first = data->meshes[i].primitives[j].attributes[0].data;
for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k)
{
if (data->meshes[i].primitives[j].attributes[k].data->count != first->count)
{
return cgltf_result_invalid_gltf;
}
}
for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k)
{
for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m)
{
if (data->meshes[i].primitives[j].targets[k].attributes[m].data->count != first->count)
{
return cgltf_result_invalid_gltf;
}
}
}
cgltf_accessor* indices = data->meshes[i].primitives[j].indices;
if (indices &&
indices->component_type != cgltf_component_type_r_8u &&
indices->component_type != cgltf_component_type_r_16u &&
indices->component_type != cgltf_component_type_r_32u)
{
return cgltf_result_invalid_gltf;
}
if (indices && indices->buffer_view && indices->buffer_view->buffer->data)
{
cgltf_size index_bound = cgltf_calc_index_bound(indices->buffer_view, indices->offset, indices->component_type, indices->count);
if (index_bound >= first->count)
{
return cgltf_result_data_too_short;
}
}
}
}
}
for (cgltf_size i = 0; i < data->nodes_count; ++i)
{
if (data->nodes[i].weights && data->nodes[i].mesh)
{
if (data->nodes[i].mesh->primitives_count && data->nodes[i].mesh->primitives[0].targets_count != data->nodes[i].weights_count)
{
return cgltf_result_invalid_gltf;
}
}
}
return cgltf_result_success;
}
void cgltf_free(cgltf_data* data)
{
if (!data)
{
return;
}
data->memory_free(data->memory_user_data, data->asset.copyright);
data->memory_free(data->memory_user_data, data->asset.generator);
data->memory_free(data->memory_user_data, data->asset.version);
data->memory_free(data->memory_user_data, data->asset.min_version);
data->memory_free(data->memory_user_data, data->accessors);
data->memory_free(data->memory_user_data, data->buffer_views);
for (cgltf_size i = 0; i < data->buffers_count; ++i)
{
if (data->buffers[i].data != data->bin)
{
data->memory_free(data->memory_user_data, data->buffers[i].data);
}
data->memory_free(data->memory_user_data, data->buffers[i].uri);
}
data->memory_free(data->memory_user_data, data->buffers);
for (cgltf_size i = 0; i < data->meshes_count; ++i)
{
data->memory_free(data->memory_user_data, data->meshes[i].name);
for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j)
{
for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k)
{
data->memory_free(data->memory_user_data, data->meshes[i].primitives[j].attributes[k].name);
}
data->memory_free(data->memory_user_data, data->meshes[i].primitives[j].attributes);
for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k)
{
for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m)
{
data->memory_free(data->memory_user_data, data->meshes[i].primitives[j].targets[k].attributes[m].name);
}
data->memory_free(data->memory_user_data, data->meshes[i].primitives[j].targets[k].attributes);
}
data->memory_free(data->memory_user_data, data->meshes[i].primitives[j].targets);
}
data->memory_free(data->memory_user_data, data->meshes[i].primitives);
data->memory_free(data->memory_user_data, data->meshes[i].weights);
}
data->memory_free(data->memory_user_data, data->meshes);
for (cgltf_size i = 0; i < data->materials_count; ++i)
{
data->memory_free(data->memory_user_data, data->materials[i].name);
}
data->memory_free(data->memory_user_data, data->materials);
for (cgltf_size i = 0; i < data->images_count; ++i)
{
data->memory_free(data->memory_user_data, data->images[i].name);
data->memory_free(data->memory_user_data, data->images[i].uri);
data->memory_free(data->memory_user_data, data->images[i].mime_type);
}
data->memory_free(data->memory_user_data, data->images);
for (cgltf_size i = 0; i < data->textures_count; ++i)
{
data->memory_free(data->memory_user_data, data->textures[i].name);
}
data->memory_free(data->memory_user_data, data->textures);
data->memory_free(data->memory_user_data, data->samplers);
for (cgltf_size i = 0; i < data->skins_count; ++i)
{
data->memory_free(data->memory_user_data, data->skins[i].name);
data->memory_free(data->memory_user_data, data->skins[i].joints);
}
data->memory_free(data->memory_user_data, data->skins);
for (cgltf_size i = 0; i < data->cameras_count; ++i)
{
data->memory_free(data->memory_user_data, data->cameras[i].name);
}
data->memory_free(data->memory_user_data, data->cameras);
for (cgltf_size i = 0; i < data->lights_count; ++i)
{
data->memory_free(data->memory_user_data, data->lights[i].name);
}
data->memory_free(data->memory_user_data, data->lights);
for (cgltf_size i = 0; i < data->nodes_count; ++i)
{
data->memory_free(data->memory_user_data, data->nodes[i].name);
data->memory_free(data->memory_user_data, data->nodes[i].children);
data->memory_free(data->memory_user_data, data->nodes[i].weights);
}
data->memory_free(data->memory_user_data, data->nodes);
for (cgltf_size i = 0; i < data->scenes_count; ++i)
{
data->memory_free(data->memory_user_data, data->scenes[i].name);
data->memory_free(data->memory_user_data, data->scenes[i].nodes);
}
data->memory_free(data->memory_user_data, data->scenes);
for (cgltf_size i = 0; i < data->animations_count; ++i)
{
data->memory_free(data->memory_user_data, data->animations[i].name);
data->memory_free(data->memory_user_data, data->animations[i].samplers);
data->memory_free(data->memory_user_data, data->animations[i].channels);
}
data->memory_free(data->memory_user_data, data->animations);
data->memory_free(data->memory_user_data, data->file_data);
data->memory_free(data->memory_user_data, data);
}
void cgltf_node_transform_local(const cgltf_node* node, cgltf_float* out_matrix)
{
cgltf_float* lm = out_matrix;
if (node->has_matrix)
{
memcpy(lm, node->matrix, sizeof(float) * 16);
}
else
{
float tx = node->translation[0];
float ty = node->translation[1];
float tz = node->translation[2];
float qx = node->rotation[0];
float qy = node->rotation[1];
float qz = node->rotation[2];
float qw = node->rotation[3];
float sx = node->scale[0];
float sy = node->scale[1];
float sz = node->scale[2];
lm[0] = (1 - 2 * qy*qy - 2 * qz*qz) * sx;
lm[1] = (2 * qx*qy + 2 * qz*qw) * sy;
lm[2] = (2 * qx*qz - 2 * qy*qw) * sz;
lm[3] = 0.f;
lm[4] = (2 * qx*qy - 2 * qz*qw) * sx;
lm[5] = (1 - 2 * qx*qx - 2 * qz*qz) * sy;
lm[6] = (2 * qy*qz + 2 * qx*qw) * sz;
lm[7] = 0.f;
lm[8] = (2 * qx*qz + 2 * qy*qw) * sx;
lm[9] = (2 * qy*qz - 2 * qx*qw) * sy;
lm[10] = (1 - 2 * qx*qx - 2 * qy*qy) * sz;
lm[11] = 0.f;
lm[12] = tx;
lm[13] = ty;
lm[14] = tz;
lm[15] = 1.f;
}
}
void cgltf_node_transform_world(const cgltf_node* node, cgltf_float* out_matrix)
{
cgltf_float* lm = out_matrix;
cgltf_node_transform_local(node, lm);
const cgltf_node* parent = node->parent;
while (parent)
{
float pm[16];
cgltf_node_transform_local(parent, pm);
for (int i = 0; i < 4; ++i)
{
float l0 = lm[i * 4 + 0];
float l1 = lm[i * 4 + 1];
float l2 = lm[i * 4 + 2];
float r0 = l0 * pm[0] + l1 * pm[4] + l2 * pm[8];
float r1 = l0 * pm[1] + l1 * pm[5] + l2 * pm[9];
float r2 = l0 * pm[2] + l1 * pm[6] + l2 * pm[10];
lm[i * 4 + 0] = r0;
lm[i * 4 + 1] = r1;
lm[i * 4 + 2] = r2;
}
lm[12] += pm[12];
lm[13] += pm[13];
lm[14] += pm[14];
parent = parent->parent;
}
}
static cgltf_size cgltf_component_read_index(const void* in, cgltf_component_type component_type)
{
switch (component_type)
{
case cgltf_component_type_r_16:
return *((const int16_t*) in);
case cgltf_component_type_r_16u:
return *((const uint16_t*) in);
case cgltf_component_type_r_32u:
return *((const uint32_t*) in);
case cgltf_component_type_r_32f:
return *((const float*) in);
case cgltf_component_type_r_8:
return *((const int8_t*) in);
case cgltf_component_type_r_8u:
case cgltf_component_type_invalid:
default:
return *((const uint8_t*) in);
}
}
static cgltf_float cgltf_component_read_float(const void* in, cgltf_component_type component_type, cgltf_bool normalized)
{
if (component_type == cgltf_component_type_r_32f)
{
return *((const float*) in);
}
if (normalized)
{
switch (component_type)
{
case cgltf_component_type_r_32u:
return *((const uint32_t*) in) / (float) UINT_MAX;
case cgltf_component_type_r_16:
return *((const int16_t*) in) / (float) SHRT_MAX;
case cgltf_component_type_r_16u:
return *((const uint16_t*) in) / (float) USHRT_MAX;
case cgltf_component_type_r_8:
return *((const int8_t*) in) / (float) SCHAR_MAX;
case cgltf_component_type_r_8u:
case cgltf_component_type_invalid:
default:
return *((const uint8_t*) in) / (float) CHAR_MAX;
}
}
return cgltf_component_read_index(in, component_type);
}
static cgltf_size cgltf_num_components(cgltf_type type);
static cgltf_size cgltf_component_size(cgltf_component_type component_type);
static cgltf_bool cgltf_element_read_float(const uint8_t* element, cgltf_type type, cgltf_component_type component_type, cgltf_bool normalized, cgltf_float* out, cgltf_size element_size)
{
cgltf_size num_components = cgltf_num_components(type);
if (element_size < num_components) {
return 0;
}
// There are three special cases for component extraction, see #data-alignment in the 2.0 spec.
cgltf_size component_size = cgltf_component_size(component_type);
if (type == cgltf_type_mat2 && component_size == 1)
{
out[0] = cgltf_component_read_float(element, component_type, normalized);
out[1] = cgltf_component_read_float(element + 1, component_type, normalized);
out[2] = cgltf_component_read_float(element + 4, component_type, normalized);
out[3] = cgltf_component_read_float(element + 5, component_type, normalized);
return 1;
}
if (type == cgltf_type_mat3 && component_size == 1)
{
out[0] = cgltf_component_read_float(element, component_type, normalized);
out[1] = cgltf_component_read_float(element + 1, component_type, normalized);
out[2] = cgltf_component_read_float(element + 2, component_type, normalized);
out[3] = cgltf_component_read_float(element + 4, component_type, normalized);
out[4] = cgltf_component_read_float(element + 5, component_type, normalized);
out[5] = cgltf_component_read_float(element + 6, component_type, normalized);
out[6] = cgltf_component_read_float(element + 8, component_type, normalized);
out[7] = cgltf_component_read_float(element + 9, component_type, normalized);
out[8] = cgltf_component_read_float(element + 10, component_type, normalized);
return 1;
}
if (type == cgltf_type_mat3 && component_size == 2)
{
out[0] = cgltf_component_read_float(element, component_type, normalized);
out[1] = cgltf_component_read_float(element + 2, component_type, normalized);
out[2] = cgltf_component_read_float(element + 4, component_type, normalized);
out[3] = cgltf_component_read_float(element + 8, component_type, normalized);
out[4] = cgltf_component_read_float(element + 10, component_type, normalized);
out[5] = cgltf_component_read_float(element + 12, component_type, normalized);
out[6] = cgltf_component_read_float(element + 16, component_type, normalized);
out[7] = cgltf_component_read_float(element + 18, component_type, normalized);
out[8] = cgltf_component_read_float(element + 20, component_type, normalized);
return 1;
}
for (cgltf_size i = 0; i < num_components; ++i)
{
out[i] = cgltf_component_read_float(element + component_size * i, component_type, normalized);
}
return 1;
}
cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size index, cgltf_float* out, cgltf_size element_size)
{
if (accessor->is_sparse || accessor->buffer_view == NULL)
{
return 0;
}
cgltf_size offset = accessor->offset + accessor->buffer_view->offset;
const uint8_t* element = (const uint8_t*) accessor->buffer_view->buffer->data;
element += offset + accessor->stride * index;
return cgltf_element_read_float(element, accessor->type, accessor->component_type, accessor->normalized, out, element_size);
}
cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size index)
{
if (accessor->buffer_view)
{
cgltf_size offset = accessor->offset + accessor->buffer_view->offset;
const uint8_t* element = (const uint8_t*) accessor->buffer_view->buffer->data;
element += offset + accessor->stride * index;
return cgltf_component_read_index(element, accessor->component_type);
}
return 0;
}
#define CGLTF_ERROR_JSON -1
#define CGLTF_ERROR_NOMEM -2
#define CGLTF_CHECK_TOKTYPE(tok_, type_) if ((tok_).type != (type_)) { return CGLTF_ERROR_JSON; }
#define CGLTF_CHECK_KEY(tok_) if ((tok_).type != JSMN_STRING || (tok_).size == 0) { return CGLTF_ERROR_JSON; } /* checking size for 0 verifies that a value follows the key */
#define CGLTF_PTRINDEX(type, idx) (type*)(cgltf_size)(idx + 1)
#define CGLTF_PTRFIXUP(var, data, size) if (var) { if ((cgltf_size)var > size) { return CGLTF_ERROR_JSON; } var = &data[(cgltf_size)var-1]; }
#define CGLTF_PTRFIXUP_REQ(var, data, size) if (!var || (cgltf_size)var > size) { return CGLTF_ERROR_JSON; } var = &data[(cgltf_size)var-1];
static int cgltf_json_strcmp(jsmntok_t const* tok, const uint8_t* json_chunk, const char* str)
{
CGLTF_CHECK_TOKTYPE(*tok, JSMN_STRING);
size_t const str_len = strlen(str);
size_t const name_length = tok->end - tok->start;
return (str_len == name_length) ? strncmp((const char*)json_chunk + tok->start, str, str_len) : 128;
}
static int cgltf_json_to_int(jsmntok_t const* tok, const uint8_t* json_chunk)
{
CGLTF_CHECK_TOKTYPE(*tok, JSMN_PRIMITIVE);
char tmp[128];
int size = (cgltf_size)(tok->end - tok->start) < sizeof(tmp) ? tok->end - tok->start : sizeof(tmp) - 1;
strncpy(tmp, (const char*)json_chunk + tok->start, size);
tmp[size] = 0;
return atoi(tmp);
}
static cgltf_float cgltf_json_to_float(jsmntok_t const* tok, const uint8_t* json_chunk)
{
CGLTF_CHECK_TOKTYPE(*tok, JSMN_PRIMITIVE);
char tmp[128];
int size = (cgltf_size)(tok->end - tok->start) < sizeof(tmp) ? tok->end - tok->start : sizeof(tmp) - 1;
strncpy(tmp, (const char*)json_chunk + tok->start, size);
tmp[size] = 0;
return (cgltf_float)atof(tmp);
}
static cgltf_bool cgltf_json_to_bool(jsmntok_t const* tok, const uint8_t* json_chunk)
{
int size = tok->end - tok->start;
return size == 4 && memcmp(json_chunk + tok->start, "true", 4) == 0;
}
static int cgltf_skip_json(jsmntok_t const* tokens, int i)
{
if (tokens[i].type == JSMN_ARRAY)
{
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
i = cgltf_skip_json(tokens, i);
if (i < 0)
{
return i;
}
}
}
else if (tokens[i].type == JSMN_OBJECT)
{
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
++i;
i = cgltf_skip_json(tokens, i);
if (i < 0)
{
return i;
}
}
}
else if (tokens[i].type == JSMN_PRIMITIVE
|| tokens[i].type == JSMN_STRING)
{
return i + 1;
}
return i;
}
static void cgltf_fill_float_array(float* out_array, int size, float value)
{
for (int j = 0; j < size; ++j)
{
out_array[j] = value;
}
}
static int cgltf_parse_json_float_array(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, float* out_array, int size)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY);
if (tokens[i].size != size)
{
return CGLTF_ERROR_JSON;
}
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
out_array[j] = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
return i;
}
static int cgltf_parse_json_string(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, char** out_string)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_STRING);
if (*out_string)
{
return CGLTF_ERROR_JSON;
}
int size = tokens[i].end - tokens[i].start;
char* result = (char*)options->memory_alloc(options->memory_user_data, size + 1);
if (!result)
{
return CGLTF_ERROR_NOMEM;
}
strncpy(result, (const char*)json_chunk + tokens[i].start, size);
result[size] = 0;
*out_string = result;
return i + 1;
}
static int cgltf_parse_json_array(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, size_t element_size, void** out_array, cgltf_size* out_size)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY);
if (*out_array)
{
return CGLTF_ERROR_JSON;
}
int size = tokens[i].size;
void* result = cgltf_calloc(options, element_size, size);
if (!result)
{
return CGLTF_ERROR_NOMEM;
}
*out_array = result;
*out_size = size;
return i + 1;
}
static void cgltf_parse_attribute_type(const char* name, cgltf_attribute_type* out_type, int* out_index)
{
const char* us = strchr(name, '_');
size_t len = us ? us - name : strlen(name);
if (len == 8 && strncmp(name, "POSITION", 8) == 0)
{
*out_type = cgltf_attribute_type_position;
}
else if (len == 6 && strncmp(name, "NORMAL", 6) == 0)
{
*out_type = cgltf_attribute_type_normal;
}
else if (len == 7 && strncmp(name, "TANGENT", 7) == 0)
{
*out_type = cgltf_attribute_type_tangent;
}
else if (len == 8 && strncmp(name, "TEXCOORD", 8) == 0)
{
*out_type = cgltf_attribute_type_texcoord;
}
else if (len == 5 && strncmp(name, "COLOR", 5) == 0)
{
*out_type = cgltf_attribute_type_color;
}
else if (len == 6 && strncmp(name, "JOINTS", 6) == 0)
{
*out_type = cgltf_attribute_type_joints;
}
else if (len == 7 && strncmp(name, "WEIGHTS", 7) == 0)
{
*out_type = cgltf_attribute_type_weights;
}
else
{
*out_type = cgltf_attribute_type_invalid;
}
if (us && *out_type != cgltf_attribute_type_invalid)
{
*out_index = atoi(us + 1);
}
}
static int cgltf_parse_json_attribute_list(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_attribute** out_attributes, cgltf_size* out_attributes_count)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
if (*out_attributes)
{
return CGLTF_ERROR_JSON;
}
*out_attributes_count = tokens[i].size;
*out_attributes = (cgltf_attribute*)cgltf_calloc(options, sizeof(cgltf_attribute), *out_attributes_count);
++i;
if (!*out_attributes)
{
return CGLTF_ERROR_NOMEM;
}
for (cgltf_size j = 0; j < *out_attributes_count; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
i = cgltf_parse_json_string(options, tokens, i, json_chunk, &(*out_attributes)[j].name);
if (i < 0)
{
return CGLTF_ERROR_JSON;
}
cgltf_parse_attribute_type((*out_attributes)[j].name, &(*out_attributes)[j].type, &(*out_attributes)[j].index);
(*out_attributes)[j].data = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
return i;
}
static int cgltf_parse_json_primitive(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_primitive* out_prim)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
out_prim->type = cgltf_primitive_type_triangles;
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "mode") == 0)
{
++i;
out_prim->type
= (cgltf_primitive_type)
cgltf_json_to_int(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "indices") == 0)
{
++i;
out_prim->indices = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "material") == 0)
{
++i;
out_prim->material = CGLTF_PTRINDEX(cgltf_material, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "attributes") == 0)
{
i = cgltf_parse_json_attribute_list(options, tokens, i + 1, json_chunk, &out_prim->attributes, &out_prim->attributes_count);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "targets") == 0)
{
i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_morph_target), (void**)&out_prim->targets, &out_prim->targets_count);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < out_prim->targets_count; ++j)
{
i = cgltf_parse_json_attribute_list(options, tokens, i, json_chunk, &out_prim->targets[j].attributes, &out_prim->targets[j].attributes_count);
if (i < 0)
{
return i;
}
}
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_mesh(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_mesh* out_mesh)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_mesh->name);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "primitives") == 0)
{
i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_primitive), (void**)&out_mesh->primitives, &out_mesh->primitives_count);
if (i < 0)
{
return i;
}
for (cgltf_size prim_index = 0; prim_index < out_mesh->primitives_count; ++prim_index)
{
i = cgltf_parse_json_primitive(options, tokens, i, json_chunk, &out_mesh->primitives[prim_index]);
if (i < 0)
{
return i;
}
}
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "weights") == 0)
{
i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_float), (void**)&out_mesh->weights, &out_mesh->weights_count);
if (i < 0)
{
return i;
}
i = cgltf_parse_json_float_array(tokens, i - 1, json_chunk, out_mesh->weights, (int)out_mesh->weights_count);
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_meshes(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
{
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_mesh), (void**)&out_data->meshes, &out_data->meshes_count);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < out_data->meshes_count; ++j)
{
i = cgltf_parse_json_mesh(options, tokens, i, json_chunk, &out_data->meshes[j]);
if (i < 0)
{
return i;
}
}
return i;
}
static cgltf_component_type cgltf_json_to_component_type(jsmntok_t const* tok, const uint8_t* json_chunk)
{
int type = cgltf_json_to_int(tok, json_chunk);
switch (type)
{
case 5120:
return cgltf_component_type_r_8;
case 5121:
return cgltf_component_type_r_8u;
case 5122:
return cgltf_component_type_r_16;
case 5123:
return cgltf_component_type_r_16u;
case 5125:
return cgltf_component_type_r_32u;
case 5126:
return cgltf_component_type_r_32f;
default:
return cgltf_component_type_invalid;
}
}
static int cgltf_parse_json_accessor_sparse(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_accessor_sparse* out_sparse)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
{
++i;
out_sparse->count = cgltf_json_to_int(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "indices") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int indices_size = tokens[i].size;
++i;
for (int k = 0; k < indices_size; ++k)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
{
++i;
out_sparse->indices_buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
{
++i;
out_sparse->indices_byte_offset = cgltf_json_to_int(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "componentType") == 0)
{
++i;
out_sparse->indices_component_type = cgltf_json_to_component_type(tokens + i, json_chunk);
++i;
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "values") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int values_size = tokens[i].size;
++i;
for (int k = 0; k < values_size; ++k)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
{
++i;
out_sparse->values_buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
{
++i;
out_sparse->values_byte_offset = cgltf_json_to_int(tokens + i, json_chunk);
++i;
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_accessor(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_accessor* out_accessor)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
{
++i;
out_accessor->buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
{
++i;
out_accessor->offset =
cgltf_json_to_int(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "componentType") == 0)
{
++i;
out_accessor->component_type = cgltf_json_to_component_type(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "normalized") == 0)
{
++i;
out_accessor->normalized = cgltf_json_to_bool(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
{
++i;
out_accessor->count =
cgltf_json_to_int(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "type") == 0)
{
++i;
if (cgltf_json_strcmp(tokens+i, json_chunk, "SCALAR") == 0)
{
out_accessor->type = cgltf_type_scalar;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "VEC2") == 0)
{
out_accessor->type = cgltf_type_vec2;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "VEC3") == 0)
{
out_accessor->type = cgltf_type_vec3;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "VEC4") == 0)
{
out_accessor->type = cgltf_type_vec4;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "MAT2") == 0)
{
out_accessor->type = cgltf_type_mat2;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "MAT3") == 0)
{
out_accessor->type = cgltf_type_mat3;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "MAT4") == 0)
{
out_accessor->type = cgltf_type_mat4;
}
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "min") == 0)
{
++i;
out_accessor->has_min = 1;
// note: we can't parse the precise number of elements since type may not have been computed yet
int min_size = tokens[i].size > 16 ? 16 : tokens[i].size;
i = cgltf_parse_json_float_array(tokens, i, json_chunk, out_accessor->min, min_size);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "max") == 0)
{
++i;
out_accessor->has_max = 1;
// note: we can't parse the precise number of elements since type may not have been computed yet
int max_size = tokens[i].size > 16 ? 16 : tokens[i].size;
i = cgltf_parse_json_float_array(tokens, i, json_chunk, out_accessor->max, max_size);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "sparse") == 0)
{
out_accessor->is_sparse = 1;
i = cgltf_parse_json_accessor_sparse(tokens, i + 1, json_chunk, &out_accessor->sparse);
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_texture_transform(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_texture_transform* out_texture_transform)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens + i, json_chunk, "offset") == 0)
{
i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_texture_transform->offset, 2);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "rotation") == 0)
{
++i;
out_texture_transform->rotation = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "scale") == 0)
{
i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_texture_transform->scale, 2);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "texCoord") == 0)
{
++i;
out_texture_transform->texcoord = cgltf_json_to_int(tokens + i, json_chunk);
++i;
}
else
{
i = cgltf_skip_json(tokens, i + 1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_texture_view(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_texture_view* out_texture_view)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
out_texture_view->scale = 1.0f;
cgltf_fill_float_array(out_texture_view->transform.scale, 2, 1.0f);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens + i, json_chunk, "index") == 0)
{
++i;
out_texture_view->texture = CGLTF_PTRINDEX(cgltf_texture, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "texCoord") == 0)
{
++i;
out_texture_view->texcoord = cgltf_json_to_int(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "scale") == 0)
{
++i;
out_texture_view->scale = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "strength") == 0)
{
++i;
out_texture_view->scale = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int extensions_size = tokens[i].size;
++i;
for (int k = 0; k < extensions_size; ++k)
{
if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_texture_transform") == 0)
{
out_texture_view->has_transform = 1;
i = cgltf_parse_json_texture_transform(tokens, i + 1, json_chunk, &out_texture_view->transform);
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
}
}
else
{
i = cgltf_skip_json(tokens, i + 1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_pbr_metallic_roughness(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_pbr_metallic_roughness* out_pbr)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "metallicFactor") == 0)
{
++i;
out_pbr->metallic_factor =
cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "roughnessFactor") == 0)
{
++i;
out_pbr->roughness_factor =
cgltf_json_to_float(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "baseColorFactor") == 0)
{
i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_pbr->base_color_factor, 4);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "baseColorTexture") == 0)
{
i = cgltf_parse_json_texture_view(tokens, i + 1, json_chunk,
&out_pbr->base_color_texture);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "metallicRoughnessTexture") == 0)
{
i = cgltf_parse_json_texture_view(tokens, i + 1, json_chunk,
&out_pbr->metallic_roughness_texture);
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_pbr_specular_glossiness(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_pbr_specular_glossiness* out_pbr)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "diffuseFactor") == 0)
{
i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_pbr->diffuse_factor, 4);
if (i < 0)
{
return i;
}
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularFactor") == 0)
{
i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_pbr->specular_factor, 3);
if (i < 0)
{
return i;
}
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "glossinessFactor") == 0)
{
++i;
out_pbr->glossiness_factor = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "diffuseTexture") == 0)
{
i = cgltf_parse_json_texture_view(tokens, i + 1, json_chunk, &out_pbr->diffuse_texture);
if (i < 0)
{
return i;
}
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularGlossinessTexture") == 0)
{
i = cgltf_parse_json_texture_view(tokens, i + 1, json_chunk, &out_pbr->specular_glossiness_texture);
if (i < 0)
{
return i;
}
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_image(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_image* out_image)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens + i, json_chunk, "uri") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->uri);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
{
++i;
out_image->buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "mimeType") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->mime_type);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->name);
}
else
{
i = cgltf_skip_json(tokens, i + 1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_sampler(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_sampler* out_sampler)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
out_sampler->wrap_s = 10497;
out_sampler->wrap_t = 10497;
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens + i, json_chunk, "magFilter") == 0)
{
++i;
out_sampler->mag_filter
= cgltf_json_to_int(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "minFilter") == 0)
{
++i;
out_sampler->min_filter
= cgltf_json_to_int(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "wrapS") == 0)
{
++i;
out_sampler->wrap_s
= cgltf_json_to_int(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "wrapT") == 0)
{
++i;
out_sampler->wrap_t
= cgltf_json_to_int(tokens + i, json_chunk);
++i;
}
else
{
i = cgltf_skip_json(tokens, i + 1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_texture(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_texture* out_texture)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_texture->name);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "sampler") == 0)
{
++i;
out_texture->sampler = CGLTF_PTRINDEX(cgltf_sampler, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "source") == 0)
{
++i;
out_texture->image = CGLTF_PTRINDEX(cgltf_image, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else
{
i = cgltf_skip_json(tokens, i + 1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_material(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_material* out_material)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
cgltf_fill_float_array(out_material->pbr_metallic_roughness.base_color_factor, 4, 1.0f);
out_material->pbr_metallic_roughness.metallic_factor = 1.0f;
out_material->pbr_metallic_roughness.roughness_factor = 1.0f;
cgltf_fill_float_array(out_material->pbr_specular_glossiness.diffuse_factor, 4, 1.0f);
cgltf_fill_float_array(out_material->pbr_specular_glossiness.specular_factor, 3, 1.0f);
out_material->pbr_specular_glossiness.glossiness_factor = 1.0f;
out_material->alpha_cutoff = 0.5f;
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_material->name);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "pbrMetallicRoughness") == 0)
{
out_material->has_pbr_metallic_roughness = 1;
i = cgltf_parse_json_pbr_metallic_roughness(tokens, i + 1, json_chunk, &out_material->pbr_metallic_roughness);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "emissiveFactor") == 0)
{
i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_material->emissive_factor, 3);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "normalTexture") == 0)
{
i = cgltf_parse_json_texture_view(tokens, i + 1, json_chunk,
&out_material->normal_texture);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "occlusionTexture") == 0)
{
i = cgltf_parse_json_texture_view(tokens, i + 1, json_chunk,
&out_material->occlusion_texture);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "emissiveTexture") == 0)
{
i = cgltf_parse_json_texture_view(tokens, i + 1, json_chunk,
&out_material->emissive_texture);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "alphaMode") == 0)
{
++i;
if (cgltf_json_strcmp(tokens + i, json_chunk, "OPAQUE") == 0)
{
out_material->alpha_mode = cgltf_alpha_mode_opaque;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "MASK") == 0)
{
out_material->alpha_mode = cgltf_alpha_mode_mask;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "BLEND") == 0)
{
out_material->alpha_mode = cgltf_alpha_mode_blend;
}
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "alphaCutoff") == 0)
{
++i;
out_material->alpha_cutoff = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "doubleSided") == 0)
{
++i;
out_material->double_sided =
cgltf_json_to_bool(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int extensions_size = tokens[i].size;
++i;
for (int k = 0; k < extensions_size; ++k)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_pbrSpecularGlossiness") == 0)
{
out_material->has_pbr_specular_glossiness = 1;
i = cgltf_parse_json_pbr_specular_glossiness(tokens, i + 1, json_chunk, &out_material->pbr_specular_glossiness);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_unlit") == 0)
{
out_material->unlit = 1;
i = cgltf_skip_json(tokens, i+1);
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_accessors(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
{
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_accessor), (void**)&out_data->accessors, &out_data->accessors_count);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < out_data->accessors_count; ++j)
{
i = cgltf_parse_json_accessor(tokens, i, json_chunk, &out_data->accessors[j]);
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_materials(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
{
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_material), (void**)&out_data->materials, &out_data->materials_count);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < out_data->materials_count; ++j)
{
i = cgltf_parse_json_material(options, tokens, i, json_chunk, &out_data->materials[j]);
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_images(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
{
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_image), (void**)&out_data->images, &out_data->images_count);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < out_data->images_count; ++j)
{
i = cgltf_parse_json_image(options, tokens, i, json_chunk, &out_data->images[j]);
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_textures(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
{
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_texture), (void**)&out_data->textures, &out_data->textures_count);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < out_data->textures_count; ++j)
{
i = cgltf_parse_json_texture(options, tokens, i, json_chunk, &out_data->textures[j]);
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_samplers(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
{
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_sampler), (void**)&out_data->samplers, &out_data->samplers_count);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < out_data->samplers_count; ++j)
{
i = cgltf_parse_json_sampler(options, tokens, i, json_chunk, &out_data->samplers[j]);
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_buffer_view(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_buffer_view* out_buffer_view)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "buffer") == 0)
{
++i;
out_buffer_view->buffer = CGLTF_PTRINDEX(cgltf_buffer, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
{
++i;
out_buffer_view->offset =
cgltf_json_to_int(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
{
++i;
out_buffer_view->size =
cgltf_json_to_int(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteStride") == 0)
{
++i;
out_buffer_view->stride =
cgltf_json_to_int(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "target") == 0)
{
++i;
int type = cgltf_json_to_int(tokens+i, json_chunk);
switch (type)
{
case 34962:
type = cgltf_buffer_view_type_vertices;
break;
case 34963:
type = cgltf_buffer_view_type_indices;
break;
default:
type = cgltf_buffer_view_type_invalid;
break;
}
out_buffer_view->type = (cgltf_buffer_view_type)type;
++i;
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_buffer_views(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
{
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_buffer_view), (void**)&out_data->buffer_views, &out_data->buffer_views_count);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < out_data->buffer_views_count; ++j)
{
i = cgltf_parse_json_buffer_view(tokens, i, json_chunk, &out_data->buffer_views[j]);
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_buffer(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_buffer* out_buffer)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
{
++i;
out_buffer->size =
cgltf_json_to_int(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "uri") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_buffer->uri);
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_buffers(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
{
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_buffer), (void**)&out_data->buffers, &out_data->buffers_count);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < out_data->buffers_count; ++j)
{
i = cgltf_parse_json_buffer(options, tokens, i, json_chunk, &out_data->buffers[j]);
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_skin(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_skin* out_skin)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_skin->name);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "joints") == 0)
{
i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_node*), (void**)&out_skin->joints, &out_skin->joints_count);
if (i < 0)
{
return i;
}
for (cgltf_size k = 0; k < out_skin->joints_count; ++k)
{
out_skin->joints[k] = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "skeleton") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
out_skin->skeleton = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "inverseBindMatrices") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
out_skin->inverse_bind_matrices = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_skins(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
{
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_skin), (void**)&out_data->skins, &out_data->skins_count);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < out_data->skins_count; ++j)
{
i = cgltf_parse_json_skin(options, tokens, i, json_chunk, &out_data->skins[j]);
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_camera(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_camera* out_camera)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_camera->name);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "type") == 0)
{
++i;
if (cgltf_json_strcmp(tokens + i, json_chunk, "perspective") == 0)
{
out_camera->type = cgltf_camera_type_perspective;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "orthographic") == 0)
{
out_camera->type = cgltf_camera_type_orthographic;
}
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "perspective") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int data_size = tokens[i].size;
++i;
out_camera->type = cgltf_camera_type_perspective;
for (int k = 0; k < data_size; ++k)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "aspectRatio") == 0)
{
++i;
out_camera->perspective.aspect_ratio = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "yfov") == 0)
{
++i;
out_camera->perspective.yfov = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "zfar") == 0)
{
++i;
out_camera->perspective.zfar = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "znear") == 0)
{
++i;
out_camera->perspective.znear = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "orthographic") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int data_size = tokens[i].size;
++i;
out_camera->type = cgltf_camera_type_orthographic;
for (int k = 0; k < data_size; ++k)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "xmag") == 0)
{
++i;
out_camera->orthographic.xmag = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "ymag") == 0)
{
++i;
out_camera->orthographic.ymag = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "zfar") == 0)
{
++i;
out_camera->orthographic.zfar = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "znear") == 0)
{
++i;
out_camera->orthographic.znear = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_cameras(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
{
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_camera), (void**)&out_data->cameras, &out_data->cameras_count);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < out_data->cameras_count; ++j)
{
i = cgltf_parse_json_camera(options, tokens, i, json_chunk, &out_data->cameras[j]);
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_light(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_light* out_light)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_light->name);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "color") == 0)
{
i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_light->color, 3);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "intensity") == 0)
{
++i;
out_light->intensity = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "type") == 0)
{
++i;
if (cgltf_json_strcmp(tokens + i, json_chunk, "directional") == 0)
{
out_light->type = cgltf_light_type_directional;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "point") == 0)
{
out_light->type = cgltf_light_type_point;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "spot") == 0)
{
out_light->type = cgltf_light_type_spot;
}
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "range") == 0)
{
++i;
out_light->range = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "spot") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int data_size = tokens[i].size;
++i;
for (int k = 0; k < data_size; ++k)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "innerConeAngle") == 0)
{
++i;
out_light->spot_inner_cone_angle = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "outerConeAngle") == 0)
{
++i;
out_light->spot_outer_cone_angle = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_lights(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
{
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_light), (void**)&out_data->lights, &out_data->lights_count);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < out_data->lights_count; ++j)
{
i = cgltf_parse_json_light(options, tokens, i, json_chunk, &out_data->lights[j]);
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_node(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_node* out_node)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
out_node->rotation[3] = 1.0f;
out_node->scale[0] = 1.0f;
out_node->scale[1] = 1.0f;
out_node->scale[2] = 1.0f;
out_node->matrix[0] = 1.0f;
out_node->matrix[5] = 1.0f;
out_node->matrix[10] = 1.0f;
out_node->matrix[15] = 1.0f;
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_node->name);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "children") == 0)
{
i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_node*), (void**)&out_node->children, &out_node->children_count);
if (i < 0)
{
return i;
}
for (cgltf_size k = 0; k < out_node->children_count; ++k)
{
out_node->children[k] = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "mesh") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
out_node->mesh = CGLTF_PTRINDEX(cgltf_mesh, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "skin") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
out_node->skin = CGLTF_PTRINDEX(cgltf_skin, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "camera") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
out_node->camera = CGLTF_PTRINDEX(cgltf_camera, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "translation") == 0)
{
out_node->has_translation = 1;
i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->translation, 3);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "rotation") == 0)
{
out_node->has_rotation = 1;
i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->rotation, 4);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "scale") == 0)
{
out_node->has_scale = 1;
i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->scale, 3);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "matrix") == 0)
{
out_node->has_matrix = 1;
i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->matrix, 16);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "weights") == 0)
{
i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_float), (void**)&out_node->weights, &out_node->weights_count);
if (i < 0)
{
return i;
}
i = cgltf_parse_json_float_array(tokens, i - 1, json_chunk, out_node->weights, (int)out_node->weights_count);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int extensions_size = tokens[i].size;
++i;
for (int k = 0; k < extensions_size; ++k)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_lights_punctual") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int data_size = tokens[i].size;
++i;
for (int m = 0; m < data_size; ++m)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens + i, json_chunk, "light") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
out_node->light = CGLTF_PTRINDEX(cgltf_light, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else
{
i = cgltf_skip_json(tokens, i + 1);
}
if (i < 0)
{
return i;
}
}
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_nodes(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
{
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_node), (void**)&out_data->nodes, &out_data->nodes_count);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < out_data->nodes_count; ++j)
{
i = cgltf_parse_json_node(options, tokens, i, json_chunk, &out_data->nodes[j]);
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_scene(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_scene* out_scene)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_scene->name);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "nodes") == 0)
{
i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_node*), (void**)&out_scene->nodes, &out_scene->nodes_count);
if (i < 0)
{
return i;
}
for (cgltf_size k = 0; k < out_scene->nodes_count; ++k)
{
out_scene->nodes[k] = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_scenes(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
{
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_scene), (void**)&out_data->scenes, &out_data->scenes_count);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < out_data->scenes_count; ++j)
{
i = cgltf_parse_json_scene(options, tokens, i, json_chunk, &out_data->scenes[j]);
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_animation_sampler(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_animation_sampler* out_sampler)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "input") == 0)
{
++i;
out_sampler->input = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "output") == 0)
{
++i;
out_sampler->output = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "interpolation") == 0)
{
++i;
if (cgltf_json_strcmp(tokens + i, json_chunk, "LINEAR") == 0)
{
out_sampler->interpolation = cgltf_interpolation_type_linear;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "STEP") == 0)
{
out_sampler->interpolation = cgltf_interpolation_type_step;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "CUBICSPLINE") == 0)
{
out_sampler->interpolation = cgltf_interpolation_type_cubic_spline;
}
++i;
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_animation_channel(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_animation_channel* out_channel)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "sampler") == 0)
{
++i;
out_channel->sampler = CGLTF_PTRINDEX(cgltf_animation_sampler, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "target") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int target_size = tokens[i].size;
++i;
for (int k = 0; k < target_size; ++k)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "node") == 0)
{
++i;
out_channel->target_node = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "path") == 0)
{
++i;
if (cgltf_json_strcmp(tokens+i, json_chunk, "translation") == 0)
{
out_channel->target_path = cgltf_animation_path_type_translation;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "rotation") == 0)
{
out_channel->target_path = cgltf_animation_path_type_rotation;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "scale") == 0)
{
out_channel->target_path = cgltf_animation_path_type_scale;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "weights") == 0)
{
out_channel->target_path = cgltf_animation_path_type_weights;
}
++i;
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_animation(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_animation* out_animation)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_animation->name);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "samplers") == 0)
{
i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_animation_sampler), (void**)&out_animation->samplers, &out_animation->samplers_count);
if (i < 0)
{
return i;
}
for (cgltf_size k = 0; k < out_animation->samplers_count; ++k)
{
i = cgltf_parse_json_animation_sampler(options, tokens, i, json_chunk, &out_animation->samplers[k]);
if (i < 0)
{
return i;
}
}
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "channels") == 0)
{
i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_animation_channel), (void**)&out_animation->channels, &out_animation->channels_count);
if (i < 0)
{
return i;
}
for (cgltf_size k = 0; k < out_animation->channels_count; ++k)
{
i = cgltf_parse_json_animation_channel(options, tokens, i, json_chunk, &out_animation->channels[k]);
if (i < 0)
{
return i;
}
}
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_animations(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
{
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_animation), (void**)&out_data->animations, &out_data->animations_count);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < out_data->animations_count; ++j)
{
i = cgltf_parse_json_animation(options, tokens, i, json_chunk, &out_data->animations[j]);
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_asset(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_asset* out_asset)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "copyright") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->copyright);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "generator") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->generator);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "version") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->version);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "minVersion") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->min_version);
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static cgltf_size cgltf_num_components(cgltf_type type) {
switch (type)
{
case cgltf_type_vec2:
return 2;
case cgltf_type_vec3:
return 3;
case cgltf_type_vec4:
return 4;
case cgltf_type_mat2:
return 4;
case cgltf_type_mat3:
return 9;
case cgltf_type_mat4:
return 16;
case cgltf_type_invalid:
case cgltf_type_scalar:
default:
return 1;
}
}
static cgltf_size cgltf_component_size(cgltf_component_type component_type) {
switch (component_type)
{
case cgltf_component_type_r_8:
case cgltf_component_type_r_8u:
return 1;
case cgltf_component_type_r_16:
case cgltf_component_type_r_16u:
return 2;
case cgltf_component_type_r_32u:
case cgltf_component_type_r_32f:
return 4;
case cgltf_component_type_invalid:
default:
return 0;
}
}
static cgltf_size cgltf_calc_size(cgltf_type type, cgltf_component_type component_type)
{
cgltf_size component_size = cgltf_component_size(component_type);
if (type == cgltf_type_mat2 && component_size == 1)
{
return 8 * component_size;
}
else if (type == cgltf_type_mat3 && (component_size == 1 || component_size == 2))
{
return 12 * component_size;
}
return component_size * cgltf_num_components(type);
}
static int cgltf_fixup_pointers(cgltf_data* out_data);
static int cgltf_parse_json_root(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens + i, json_chunk, "asset") == 0)
{
i = cgltf_parse_json_asset(options, tokens, i + 1, json_chunk, &out_data->asset);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "meshes") == 0)
{
i = cgltf_parse_json_meshes(options, tokens, i + 1, json_chunk, out_data);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "accessors") == 0)
{
i = cgltf_parse_json_accessors(options, tokens, i + 1, json_chunk, out_data);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "bufferViews") == 0)
{
i = cgltf_parse_json_buffer_views(options, tokens, i + 1, json_chunk, out_data);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "buffers") == 0)
{
i = cgltf_parse_json_buffers(options, tokens, i + 1, json_chunk, out_data);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "materials") == 0)
{
i = cgltf_parse_json_materials(options, tokens, i + 1, json_chunk, out_data);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "images") == 0)
{
i = cgltf_parse_json_images(options, tokens, i + 1, json_chunk, out_data);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "textures") == 0)
{
i = cgltf_parse_json_textures(options, tokens, i + 1, json_chunk, out_data);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "samplers") == 0)
{
i = cgltf_parse_json_samplers(options, tokens, i + 1, json_chunk, out_data);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "skins") == 0)
{
i = cgltf_parse_json_skins(options, tokens, i + 1, json_chunk, out_data);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "cameras") == 0)
{
i = cgltf_parse_json_cameras(options, tokens, i + 1, json_chunk, out_data);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "nodes") == 0)
{
i = cgltf_parse_json_nodes(options, tokens, i + 1, json_chunk, out_data);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "scenes") == 0)
{
i = cgltf_parse_json_scenes(options, tokens, i + 1, json_chunk, out_data);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "scene") == 0)
{
++i;
out_data->scene = CGLTF_PTRINDEX(cgltf_scene, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "animations") == 0)
{
i = cgltf_parse_json_animations(options, tokens, i + 1, json_chunk, out_data);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int extensions_size = tokens[i].size;
++i;
for (int k = 0; k < extensions_size; ++k)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_lights_punctual") == 0)
{
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int data_size = tokens[i].size;
++i;
for (int m = 0; m < data_size; ++m)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens + i, json_chunk, "lights") == 0)
{
i = cgltf_parse_json_lights(options, tokens, i + 1, json_chunk, out_data);
}
else
{
i = cgltf_skip_json(tokens, i + 1);
}
if (i < 0)
{
return i;
}
}
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
}
else
{
i = cgltf_skip_json(tokens, i + 1);
}
if (i < 0)
{
return i;
}
}
return i;
}
cgltf_result cgltf_parse_json(cgltf_options* options, const uint8_t* json_chunk, cgltf_size size, cgltf_data** out_data)
{
jsmn_parser parser = { 0 };
if (options->json_token_count == 0)
{
int token_count = jsmn_parse(&parser, (const char*)json_chunk, size, NULL, 0);
if (token_count <= 0)
{
return cgltf_result_invalid_json;
}
options->json_token_count = token_count;
}
jsmntok_t* tokens = (jsmntok_t*)options->memory_alloc(options->memory_user_data, sizeof(jsmntok_t) * options->json_token_count);
if (!tokens)
{
return cgltf_result_out_of_memory;
}
jsmn_init(&parser);
int token_count = jsmn_parse(&parser, (const char*)json_chunk, size, tokens, options->json_token_count);
if (token_count <= 0)
{
options->memory_free(options->memory_user_data, tokens);
return cgltf_result_invalid_json;
}
cgltf_data* data = (cgltf_data*)options->memory_alloc(options->memory_user_data, sizeof(cgltf_data));
if (!data)
{
options->memory_free(options->memory_user_data, tokens);
return cgltf_result_out_of_memory;
}
memset(data, 0, sizeof(cgltf_data));
data->memory_free = options->memory_free;
data->memory_user_data = options->memory_user_data;
int i = cgltf_parse_json_root(options, tokens, 0, json_chunk, data);
options->memory_free(options->memory_user_data, tokens);
if (i < 0)
{
cgltf_free(data);
return (i == CGLTF_ERROR_NOMEM) ? cgltf_result_out_of_memory : cgltf_result_invalid_gltf;
}
if (cgltf_fixup_pointers(data) < 0)
{
cgltf_free(data);
return cgltf_result_invalid_gltf;
}
*out_data = data;
return cgltf_result_success;
}
static int cgltf_fixup_pointers(cgltf_data* data)
{
for (cgltf_size i = 0; i < data->meshes_count; ++i)
{
for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j)
{
CGLTF_PTRFIXUP(data->meshes[i].primitives[j].indices, data->accessors, data->accessors_count);
CGLTF_PTRFIXUP(data->meshes[i].primitives[j].material, data->materials, data->materials_count);
for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k)
{
CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].attributes[k].data, data->accessors, data->accessors_count);
}
for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k)
{
for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m)
{
CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].targets[k].attributes[m].data, data->accessors, data->accessors_count);
}
}
}
}
for (cgltf_size i = 0; i < data->accessors_count; ++i)
{
CGLTF_PTRFIXUP(data->accessors[i].buffer_view, data->buffer_views, data->buffer_views_count);
if (data->accessors[i].is_sparse)
{
CGLTF_PTRFIXUP_REQ(data->accessors[i].sparse.indices_buffer_view, data->buffer_views, data->buffer_views_count);
CGLTF_PTRFIXUP_REQ(data->accessors[i].sparse.values_buffer_view, data->buffer_views, data->buffer_views_count);
}
if (data->accessors[i].buffer_view)
{
data->accessors[i].stride = data->accessors[i].buffer_view->stride;
}
if (data->accessors[i].stride == 0)
{
data->accessors[i].stride = cgltf_calc_size(data->accessors[i].type, data->accessors[i].component_type);
}
}
for (cgltf_size i = 0; i < data->textures_count; ++i)
{
CGLTF_PTRFIXUP(data->textures[i].image, data->images, data->images_count);
CGLTF_PTRFIXUP(data->textures[i].sampler, data->samplers, data->samplers_count);
}
for (cgltf_size i = 0; i < data->images_count; ++i)
{
CGLTF_PTRFIXUP(data->images[i].buffer_view, data->buffer_views, data->buffer_views_count);
}
for (cgltf_size i = 0; i < data->materials_count; ++i)
{
CGLTF_PTRFIXUP(data->materials[i].normal_texture.texture, data->textures, data->textures_count);
CGLTF_PTRFIXUP(data->materials[i].emissive_texture.texture, data->textures, data->textures_count);
CGLTF_PTRFIXUP(data->materials[i].occlusion_texture.texture, data->textures, data->textures_count);
CGLTF_PTRFIXUP(data->materials[i].pbr_metallic_roughness.base_color_texture.texture, data->textures, data->textures_count);
CGLTF_PTRFIXUP(data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.texture, data->textures, data->textures_count);
CGLTF_PTRFIXUP(data->materials[i].pbr_specular_glossiness.diffuse_texture.texture, data->textures, data->textures_count);
CGLTF_PTRFIXUP(data->materials[i].pbr_specular_glossiness.specular_glossiness_texture.texture, data->textures, data->textures_count);
}
for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
{
CGLTF_PTRFIXUP_REQ(data->buffer_views[i].buffer, data->buffers, data->buffers_count);
}
for (cgltf_size i = 0; i < data->skins_count; ++i)
{
for (cgltf_size j = 0; j < data->skins[i].joints_count; ++j)
{
CGLTF_PTRFIXUP_REQ(data->skins[i].joints[j], data->nodes, data->nodes_count);
}
CGLTF_PTRFIXUP(data->skins[i].skeleton, data->nodes, data->nodes_count);
CGLTF_PTRFIXUP(data->skins[i].inverse_bind_matrices, data->accessors, data->accessors_count);
}
for (cgltf_size i = 0; i < data->nodes_count; ++i)
{
for (cgltf_size j = 0; j < data->nodes[i].children_count; ++j)
{
CGLTF_PTRFIXUP_REQ(data->nodes[i].children[j], data->nodes, data->nodes_count);
if (data->nodes[i].children[j]->parent)
{
return CGLTF_ERROR_JSON;
}
data->nodes[i].children[j]->parent = &data->nodes[i];
}
CGLTF_PTRFIXUP(data->nodes[i].mesh, data->meshes, data->meshes_count);
CGLTF_PTRFIXUP(data->nodes[i].skin, data->skins, data->skins_count);
CGLTF_PTRFIXUP(data->nodes[i].camera, data->cameras, data->cameras_count);
CGLTF_PTRFIXUP(data->nodes[i].light, data->lights, data->lights_count);
}
for (cgltf_size i = 0; i < data->scenes_count; ++i)
{
for (cgltf_size j = 0; j < data->scenes[i].nodes_count; ++j)
{
CGLTF_PTRFIXUP_REQ(data->scenes[i].nodes[j], data->nodes, data->nodes_count);
if (data->scenes[i].nodes[j]->parent)
{
return CGLTF_ERROR_JSON;
}
}
}
CGLTF_PTRFIXUP(data->scene, data->scenes, data->scenes_count);
for (cgltf_size i = 0; i < data->animations_count; ++i)
{
for (cgltf_size j = 0; j < data->animations[i].samplers_count; ++j)
{
CGLTF_PTRFIXUP_REQ(data->animations[i].samplers[j].input, data->accessors, data->accessors_count);
CGLTF_PTRFIXUP_REQ(data->animations[i].samplers[j].output, data->accessors, data->accessors_count);
}
for (cgltf_size j = 0; j < data->animations[i].channels_count; ++j)
{
CGLTF_PTRFIXUP_REQ(data->animations[i].channels[j].sampler, data->animations[i].samplers, data->animations[i].samplers_count);
CGLTF_PTRFIXUP(data->animations[i].channels[j].target_node, data->nodes, data->nodes_count);
}
}
return 0;
}
/*
* -- jsmn.c start --
* Source: https://github.com/zserge/jsmn
* License: MIT
*
* Copyright (c) 2010 Serge A. Zaitsev
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Allocates a fresh unused token from the token pull.
*/
static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
jsmntok_t *tokens, size_t num_tokens) {
jsmntok_t *tok;
if (parser->toknext >= num_tokens) {
return NULL;
}
tok = &tokens[parser->toknext++];
tok->start = tok->end = -1;
tok->size = 0;
#ifdef JSMN_PARENT_LINKS
tok->parent = -1;
#endif
return tok;
}
/**
* Fills token type and boundaries.
*/
static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
int start, int end) {
token->type = type;
token->start = start;
token->end = end;
token->size = 0;
}
/**
* Fills next available token with JSON primitive.
*/
static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
size_t len, jsmntok_t *tokens, size_t num_tokens) {
jsmntok_t *token;
int start;
start = parser->pos;
for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
switch (js[parser->pos]) {
#ifndef JSMN_STRICT
/* In strict mode primitive must be followed by "," or "}" or "]" */
case ':':
#endif
case '\t' : case '\r' : case '\n' : case ' ' :
case ',' : case ']' : case '}' :
goto found;
}
if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
parser->pos = start;
return JSMN_ERROR_INVAL;
}
}
#ifdef JSMN_STRICT
/* In strict mode primitive must be followed by a comma/object/array */
parser->pos = start;
return JSMN_ERROR_PART;
#endif
found:
if (tokens == NULL) {
parser->pos--;
return 0;
}
token = jsmn_alloc_token(parser, tokens, num_tokens);
if (token == NULL) {
parser->pos = start;
return JSMN_ERROR_NOMEM;
}
jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
#ifdef JSMN_PARENT_LINKS
token->parent = parser->toksuper;
#endif
parser->pos--;
return 0;
}
/**
* Fills next token with JSON string.
*/
static int jsmn_parse_string(jsmn_parser *parser, const char *js,
size_t len, jsmntok_t *tokens, size_t num_tokens) {
jsmntok_t *token;
int start = parser->pos;
parser->pos++;
/* Skip starting quote */
for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
char c = js[parser->pos];
/* Quote: end of string */
if (c == '\"') {
if (tokens == NULL) {
return 0;
}
token = jsmn_alloc_token(parser, tokens, num_tokens);
if (token == NULL) {
parser->pos = start;
return JSMN_ERROR_NOMEM;
}
jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
#ifdef JSMN_PARENT_LINKS
token->parent = parser->toksuper;
#endif
return 0;
}
/* Backslash: Quoted symbol expected */
if (c == '\\' && parser->pos + 1 < len) {
int i;
parser->pos++;
switch (js[parser->pos]) {
/* Allowed escaped symbols */
case '\"': case '/' : case '\\' : case 'b' :
case 'f' : case 'r' : case 'n' : case 't' :
break;
/* Allows escaped symbol \uXXXX */
case 'u':
parser->pos++;
for(i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++) {
/* If it isn't a hex character we have an error */
if(!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
(js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
(js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
parser->pos = start;
return JSMN_ERROR_INVAL;
}
parser->pos++;
}
parser->pos--;
break;
/* Unexpected symbol */
default:
parser->pos = start;
return JSMN_ERROR_INVAL;
}
}
}
parser->pos = start;
return JSMN_ERROR_PART;
}
/**
* Parse JSON string and fill tokens.
*/
static int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
jsmntok_t *tokens, size_t num_tokens) {
int r;
int i;
jsmntok_t *token;
int count = parser->toknext;
for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
char c;
jsmntype_t type;
c = js[parser->pos];
switch (c) {
case '{': case '[':
count++;
if (tokens == NULL) {
break;
}
token = jsmn_alloc_token(parser, tokens, num_tokens);
if (token == NULL)
return JSMN_ERROR_NOMEM;
if (parser->toksuper != -1) {
tokens[parser->toksuper].size++;
#ifdef JSMN_PARENT_LINKS
token->parent = parser->toksuper;
#endif
}
token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
token->start = parser->pos;
parser->toksuper = parser->toknext - 1;
break;
case '}': case ']':
if (tokens == NULL)
break;
type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
#ifdef JSMN_PARENT_LINKS
if (parser->toknext < 1) {
return JSMN_ERROR_INVAL;
}
token = &tokens[parser->toknext - 1];
for (;;) {
if (token->start != -1 && token->end == -1) {
if (token->type != type) {
return JSMN_ERROR_INVAL;
}
token->end = parser->pos + 1;
parser->toksuper = token->parent;
break;
}
if (token->parent == -1) {
if(token->type != type || parser->toksuper == -1) {
return JSMN_ERROR_INVAL;
}
break;
}
token = &tokens[token->parent];
}
#else
for (i = parser->toknext - 1; i >= 0; i--) {
token = &tokens[i];
if (token->start != -1 && token->end == -1) {
if (token->type != type) {
return JSMN_ERROR_INVAL;
}
parser->toksuper = -1;
token->end = parser->pos + 1;
break;
}
}
/* Error if unmatched closing bracket */
if (i == -1) return JSMN_ERROR_INVAL;
for (; i >= 0; i--) {
token = &tokens[i];
if (token->start != -1 && token->end == -1) {
parser->toksuper = i;
break;
}
}
#endif
break;
case '\"':
r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
if (r < 0) return r;
count++;
if (parser->toksuper != -1 && tokens != NULL)
tokens[parser->toksuper].size++;
break;
case '\t' : case '\r' : case '\n' : case ' ':
break;
case ':':
parser->toksuper = parser->toknext - 1;
break;
case ',':
if (tokens != NULL && parser->toksuper != -1 &&
tokens[parser->toksuper].type != JSMN_ARRAY &&
tokens[parser->toksuper].type != JSMN_OBJECT) {
#ifdef JSMN_PARENT_LINKS
parser->toksuper = tokens[parser->toksuper].parent;
#else
for (i = parser->toknext - 1; i >= 0; i--) {
if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
if (tokens[i].start != -1 && tokens[i].end == -1) {
parser->toksuper = i;
break;
}
}
}
#endif
}
break;
#ifdef JSMN_STRICT
/* In strict mode primitives are: numbers and booleans */
case '-': case '0': case '1' : case '2': case '3' : case '4':
case '5': case '6': case '7' : case '8': case '9':
case 't': case 'f': case 'n' :
/* And they must not be keys of the object */
if (tokens != NULL && parser->toksuper != -1) {
jsmntok_t *t = &tokens[parser->toksuper];
if (t->type == JSMN_OBJECT ||
(t->type == JSMN_STRING && t->size != 0)) {
return JSMN_ERROR_INVAL;
}
}
#else
/* In non-strict mode every unquoted value is a primitive */
default:
#endif
r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
if (r < 0) return r;
count++;
if (parser->toksuper != -1 && tokens != NULL)
tokens[parser->toksuper].size++;
break;
#ifdef JSMN_STRICT
/* Unexpected char in strict mode */
default:
return JSMN_ERROR_INVAL;
#endif
}
}
if (tokens != NULL) {
for (i = parser->toknext - 1; i >= 0; i--) {
/* Unmatched opened object or array */
if (tokens[i].start != -1 && tokens[i].end == -1) {
return JSMN_ERROR_PART;
}
}
}
return count;
}
/**
* Creates a new parser based over a given buffer with an array of tokens
* available.
*/
static void jsmn_init(jsmn_parser *parser) {
parser->pos = 0;
parser->toknext = 0;
parser->toksuper = -1;
}
/*
* -- jsmn.c end --
*/
#endif /* #ifdef CGLTF_IMPLEMENTATION */
/* cgltf is distributed under MIT license:
*
* Copyright (c) 2018 Johannes Kuhlmann
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
|