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 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534
|
/* $Id: togl.c,v 1.2 2009/05/14 20:43:34 vareille Exp $ */
/* vi:set sw=4: */
/*
* Togl - a Tk OpenGL widget
*
* Copyright (C) 1996-2002 Brian Paul and Ben Bederson
* Copyright (C) 2005-2008 Greg Couch
* See the LICENSE file for copyright details.
*/
/*
* Currently we support X11, Win32 and Mac OS X only
*/
#define USE_TOGL_STUB_PROCS
#include "togl.h"
#include <tclInt.h>
#include <tkInt.h>
#include <limits.h>
#ifndef TOGL_USE_FONTS
# define TOGL_USE_FONTS 1
#endif
#ifndef TOGL_USE_OVERLAY
# if defined(TOGL_X11) || defined(TOGL_WGL)
# define TOGL_USE_OVERLAY 1
# endif
#endif
/* Use TCL_STUPID to cast (const char *) to (char *) where the Tcl function
* prototype argument should really be const */
#define TCL_STUPID (char *)
/* Use WIDGREC to cast widgRec or recordPtr arguments */
#define WIDGREC (char *)
/*** Windows headers ***/
#if defined(TOGL_WGL)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# undef WIN32_LEAN_AND_MEAN
# include <winnt.h>
# ifndef PFD_SUPPORT_COMPOSITION
// for Vista -- not strictly needed because we don't use PFD_SUPPORT_GDI/BITMAP
# define PFD_SUPPORT_COMPOSITION 0x00008000
# endif
# include <objbase.h>
# ifdef _MSC_VER
# include <strsafe.h>
# include "StereoI/StereoI.h" /* NVidia Consumer 3D stereo */
# else
# ifdef UNICODE
# define StringCchPrintf snwprintf
# else
# define StringCchPrintf snprintf
# endif
# endif
/*** X Window System headers ***/
#elif defined(TOGL_X11)
# include <X11/Xlib.h>
# include <X11/Xutil.h>
# include <X11/Xatom.h> /* for XA_RGB_DEFAULT_MAP atom */
# if defined(__vms)
# include <X11/StdCmap.h> /* for XmuLookupStandardColormap */
# else
# include <X11/Xmu/StdCmap.h> /* for XmuLookupStandardColormap */
# endif
# include <GL/glx.h>
# ifdef __sgi
# include <X11/extensions/SGIStereo.h>
# endif
# ifdef HAVE_AUTOSTEREO
# include <autostereo.h>
# endif
/*** Mac headers ***/
#elif defined(TOGL_AGL)
# define Cursor QDCursor
# include <AGL/agl.h>
# undef Cursor
# include <tkMacOSXInt.h> /* usa MacDrawable */
# include <ApplicationServices/ApplicationServices.h>
#else /* make sure only one platform defined */
# error Unsupported platform, or confused platform defines...
#endif
#define NC3D "NVidia Consumer 3D Stereo"
#ifndef STEREO_BUFFER_NONE
/* From <X11/extensions/SGIStereo.h>, but we use this constants elsewhere */
# define STEREO_BUFFER_NONE 0
# define STEREO_BUFFER_LEFT 1
# define STEREO_BUFFER_RIGHT 2
#endif
/*** Standard C headers ***/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#ifdef TOGL_WGL
# include <tkPlatDecls.h>
#endif
#if TK_MAJOR_VERSION < 8
# error Sorry Togl requires Tcl/Tk ver 8.0 or higher.
#endif
#ifdef USE_TCL_STUBS
# if TK_MAJOR_VERSION < 8 || (TK_MAJOR_VERSION == 8 && TK_MINOR_VERSION < 1)
# error Sorry stub support requires Tcl/Tk ver 8.1 or higher.
# endif
#endif
#if defined(TOGL_AGL)
# if TK_MAJOR_VERSION < 8 || (TK_MAJOR_VERSION == 8 && TK_MINOR_VERSION < 4)
# error Sorry Mac Aqua version requires Tcl/Tk ver 8.4.0 or higher.
# endif
#endif /* TOGL_AGL */
/* workaround for bug #123153 in tcl ver8.4a2 (tcl.h) */
#if defined(Tcl_InitHashTable) && defined(USE_TCL_STUBS)
# undef Tcl_InitHashTable
# define Tcl_InitHashTable (tclStubsPtr->tcl_InitHashTable)
#endif
#if TK_MAJOR_VERSION > 8 || (TK_MAJOR_VERSION == 8 && TK_MINOR_VERSION >= 4)
# define HAVE_TK_SETCLASSPROCS
/* pointer to Tk_SetClassProcs function in the stub table */
static void (*SetClassProcsPtr)
_ANSI_ARGS_((Tk_Window, Tk_ClassProcs *, ClientData));
#endif
/*
* Copy of TkClassProcs declarations from tkInt.h
* (this is needed for Tcl ver =< 8.4a3)
*/
typedef Window (TkClassCreateProc) _ANSI_ARGS_((Tk_Window tkwin,
Window parent, ClientData instanceData));
typedef void (TkClassGeometryProc) _ANSI_ARGS_((ClientData instanceData));
typedef void (TkClassModalProc) _ANSI_ARGS_((Tk_Window tkwin,
XEvent *eventPtr));
typedef struct TkClassProcs
{
TkClassCreateProc *createProc;
TkClassGeometryProc *geometryProc;
TkClassModalProc *modalProc;
} TkClassProcs;
/* Defaults */
#define DEFAULT_WIDTH "400"
#define DEFAULT_HEIGHT "400"
#define DEFAULT_IDENT ""
#define DEFAULT_FONTNAME "Courier"
#define DEFAULT_TIME "1"
#ifdef TOGL_WGL
/* Maximum size of a logical palette corresponding to a colormap in color index
* mode. */
# define MAX_CI_COLORMAP_SIZE 4096
# define MAX_CI_COLORMAP_BITS 12
# if TOGL_USE_FONTS != 1
/*
* copy of TkWinColormap from tkWinInt.h
*/
typedef struct
{
HPALETTE palette; /* Palette handle used when drawing. */
UINT size; /* Number of entries in the palette. */
int stale; /* 1 if palette needs to be realized, otherwise
* 0. If the palette is stale, then an idle
* handler is scheduled to realize the palette.
*/
Tcl_HashTable refCounts; /* Hash table of palette entry reference counts
* indexed by pixel value. */
} TkWinColormap;
# else
# include <tkWinInt.h>
# endif
static LRESULT(CALLBACK *tkWinChildProc) (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam) = NULL;
# define TK_WIN_CHILD_CLASS_NAME "TkChild"
#endif /* TOGL_WGL */
#define MAX(a,b) (((a)>(b))?(a):(b))
#define TCL_ERR(interp, string) \
do { \
Tcl_ResetResult(interp); \
Tcl_AppendResult(interp, string, NULL); \
return TCL_ERROR; \
} while (0)
/* The constant DUMMY_WINDOW is used to signal window creation failure from
* Togl_CreateWindow() */
#define DUMMY_WINDOW ((Window) -1)
#define ALL_EVENTS_MASK \
(KeyPressMask \
|KeyReleaseMask \
|ButtonPressMask \
|ButtonReleaseMask \
|EnterWindowMask \
|LeaveWindowMask \
|PointerMotionMask \
|ExposureMask \
|VisibilityChangeMask \
|FocusChangeMask \
|PropertyChangeMask \
|ColormapChangeMask)
/*
* The following structure contains pointers to functions used for
* processing the custom "-stereo" option. Copied from tkPanedWindow.c.
*/
static int SetStereo(ClientData clientData, Tcl_Interp *interp,
Tk_Window tkwin, Tcl_Obj **value, char *recordPtr,
int internalOffset, char *oldInternalPtr, int flags);
static Tcl_Obj *GetStereo(ClientData clientData, Tk_Window tkwin,
char *recordPtr, int internalOffset);
static void RestoreStereo(ClientData clientData, Tk_Window tkwin,
char *internalPtr, char *oldInternalPtr);
static Tk_ObjCustomOption stereoOption = {
"stereo", /* name */
SetStereo, /* setProc */
GetStereo, /* getProc */
RestoreStereo, /* restoreProc */
NULL, /* freeProc */
0
};
/*
* Stuff we initialize on a per package (Togl_Init) basis.
* Since Tcl uses one interpreter per thread, any per-thread
* data goes here.
*/
struct Togl_PackageGlobals
{
Tk_OptionTable optionTable; /* Used to parse options */
Togl *toglHead; /* Head of linked list of all Togl widgets */
int nextContextTag; /* Used to assign similar context tags */
};
typedef struct Togl_PackageGlobals Togl_PackageGlobals;
extern ToglStubs toglStubs; /* should be only non-const global */
struct Togl
{
Togl *Next; /* next in linked list */
#if defined(TOGL_WGL)
HDC tglGLHdc; /* Device context of device that OpenGL calls
* will be drawn on */
HGLRC tglGLHglrc; /* OpenGL rendering context to be made current */
int CiColormapSize; /* (Maximum) size of colormap in color index
* mode */
# ifdef STEREO_I_H
StereoI *pStereoI;
# endif
#elif defined(TOGL_X11)
GLXContext GlCtx; /* Normal planes GLX context */
#elif defined(TOGL_AGL)
AGLContext aglCtx;
#endif
int contextTag; /* all contexts with same tag share display
* lists */
XVisualInfo *VisInfo; /* Visual info of the current */
Display *display; /* X's token for the window's display. */
Tk_Window TkWin; /* Tk window structure */
Tcl_Interp *Interp; /* Tcl interpreter */
Tcl_Command widgetCmd; /* Token for togl's widget command */
Togl_PackageGlobals *tpg; /* Used to access globals */
#ifndef NO_TK_CURSOR
Tk_Cursor Cursor; /* The widget's cursor */
#endif
int Width, Height; /* Dimensions of window */
int SetGrid; /* positive is grid size for window manager */
int TimerInterval; /* Time interval for timer in milliseconds */
Tcl_TimerToken timerHandler; /* Token for togl's timer handler */
Bool RgbaFlag; /* configuration flags (ala GLX parameters) */
int RgbaRed;
int RgbaGreen;
int RgbaBlue;
Bool DoubleFlag;
Bool DepthFlag;
int DepthSize;
Bool AccumFlag;
int AccumRed;
int AccumGreen;
int AccumBlue;
int AccumAlpha;
Bool AlphaFlag;
int AlphaSize;
Bool StencilFlag;
int StencilSize;
Bool PrivateCmapFlag;
Bool OverlayFlag;
int Stereo;
double EyeSeparation;
double Convergence;
int AuxNumber;
Bool Indirect;
int PixelFormat;
int SwapInterval;
const char *ShareList; /* name (ident) of Togl to share dlists with */
const char *ShareContext; /* name (ident) to share OpenGL context with */
const char *Ident; /* User's identification string */
ClientData Client_Data; /* Pointer to user data */
Bool UpdatePending; /* Should normal planes be redrawn? */
Tcl_Obj *CreateProc; /* Callback when widget is realized */
Tcl_Obj *DisplayProc; /* Callback when widget is redrawn */
Tcl_Obj *ReshapeProc; /* Callback when window size changes */
Tcl_Obj *DestroyProc; /* Callback when widget is destroyed */
Tcl_Obj *TimerProc; /* Callback when widget is idle */
/* Overlay stuff */
#if defined(TOGL_X11)
GLXContext OverlayCtx; /* Overlay planes OpenGL context */
#elif defined(TOGL_WGL)
HGLRC tglGLOverlayHglrc;
#endif
Window OverlayWindow; /* The overlay window, or 0 */
Tcl_Obj *OverlayDisplayProc; /* Overlay redraw proc */
Bool OverlayUpdatePending; /* Should overlay be redrawn? */
Colormap OverlayCmap; /* colormap for overlay is created */
int OverlayTransparentPixel; /* transparent pixel */
Bool OverlayIsMapped;
GLfloat *EpsRedMap; /* Index2RGB Maps for Color index modes */
GLfloat *EpsGreenMap;
GLfloat *EpsBlueMap;
GLint EpsMapSize; /* = Number of indices in our Togl */
int currentStereoBuffer;
#ifdef HAVE_AUTOSTEREO
int as_initialized; /* for autostereo package */
ASHandle ash; /* for autostereo package */
#endif
int badWindow; /* true when Togl_CreateWindow fails */
};
/*
* Prototypes for functions local to this file
*/
static int Togl_ObjCmd(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *const *objv);
static void Togl_ObjCmdDelete(ClientData clientData);
static void Togl_EventProc(ClientData clientData, XEvent *eventPtr);
static Window Togl_CreateWindow(Tk_Window, Window, ClientData);
static void Togl_WorldChanged(ClientData);
#ifdef MESA_COLOR_HACK
static int get_free_color_cells(Display *display, int screen,
Colormap colormap);
static void free_default_color_cells(Display *display, Colormap colormap);
#endif
static void ToglCmdDeletedProc(ClientData);
#if defined(TOGL_AGL)
static void SetMacBufRect(Togl *togl);
#endif
/*
* Setup Togl widget configuration options:
*/
#define GEOMETRY_MASK 0x1 /* widget geometry */
#define FORMAT_MASK 0x2 /* pixel format */
#define CURSOR_MASK 0x4
#define TIMER_MASK 0x8
#define OVERLAY_MASK 0x10
#define SWAP_MASK 0x20
#define STEREO_MASK 0x40
#define STEREO_FORMAT_MASK 0x80
static Tk_OptionSpec optionSpecs[] = {
{TK_OPTION_PIXELS, TCL_STUPID "-height", "height", "Height",
DEFAULT_HEIGHT, -1, Tk_Offset(Togl, Height), 0, NULL,
GEOMETRY_MASK},
{TK_OPTION_PIXELS, TCL_STUPID "-width", "width", "Width",
DEFAULT_WIDTH, -1, Tk_Offset(Togl, Width), 0, NULL,
GEOMETRY_MASK},
{TK_OPTION_BOOLEAN, TCL_STUPID "-rgba", "rgba", "Rgba",
"true", -1, Tk_Offset(Togl, RgbaFlag), 0, NULL, FORMAT_MASK},
{TK_OPTION_INT, TCL_STUPID "-redsize", "redsize", "RedSize",
"1", -1, Tk_Offset(Togl, RgbaRed), 0, NULL, FORMAT_MASK},
{TK_OPTION_INT, TCL_STUPID "-greensize", "greensize", "GreenSize",
"1", -1, Tk_Offset(Togl, RgbaGreen), 0, NULL, FORMAT_MASK},
{TK_OPTION_INT, TCL_STUPID "-bluesize", "bluesize", "BlueSize",
"1", -1, Tk_Offset(Togl, RgbaBlue), 0, NULL, FORMAT_MASK},
{TK_OPTION_BOOLEAN, TCL_STUPID "-double", "double", "Double",
"false", -1, Tk_Offset(Togl, DoubleFlag), 0, NULL, FORMAT_MASK},
{TK_OPTION_BOOLEAN, TCL_STUPID "-depth", "depth", "Depth",
"false", -1, Tk_Offset(Togl, DepthFlag), 0, NULL, FORMAT_MASK},
{TK_OPTION_INT, TCL_STUPID "-depthsize", "depthsize", "DepthSize",
"1", -1, Tk_Offset(Togl, DepthSize), 0, NULL, FORMAT_MASK},
{TK_OPTION_BOOLEAN, TCL_STUPID "-accum", "accum", "Accum",
"false", -1, Tk_Offset(Togl, AccumFlag), 0, NULL, FORMAT_MASK},
{TK_OPTION_INT, TCL_STUPID "-accumredsize", "accumredsize", "AccumRedSize",
"1", -1, Tk_Offset(Togl, AccumRed), 0, NULL, FORMAT_MASK},
{TK_OPTION_INT, TCL_STUPID "-accumgreensize", "accumgreensize",
"AccumGreenSize",
"1", -1, Tk_Offset(Togl, AccumGreen), 0, NULL, FORMAT_MASK},
{TK_OPTION_INT, TCL_STUPID "-accumbluesize", "accumbluesize",
"AccumBlueSize",
"1", -1, Tk_Offset(Togl, AccumBlue), 0, NULL, FORMAT_MASK},
{TK_OPTION_INT, TCL_STUPID "-accumalphasize", "accumalphasize",
"AccumAlphaSize",
"1", -1, Tk_Offset(Togl, AccumAlpha), 0, NULL, FORMAT_MASK},
{TK_OPTION_BOOLEAN, TCL_STUPID "-alpha", "alpha", "Alpha",
"false", -1, Tk_Offset(Togl, AlphaFlag), 0, NULL, FORMAT_MASK},
{TK_OPTION_INT, TCL_STUPID "-alphasize", "alphasize", "AlphaSize",
"1", -1, Tk_Offset(Togl, AlphaSize), 0, NULL, FORMAT_MASK},
{TK_OPTION_BOOLEAN, TCL_STUPID "-stencil", "stencil", "Stencil",
"false", -1, Tk_Offset(Togl, StencilFlag), 0, NULL, FORMAT_MASK},
{TK_OPTION_INT, TCL_STUPID "-stencilsize", "stencilsize", "StencilSize",
"1", -1, Tk_Offset(Togl, StencilSize), 0, NULL, FORMAT_MASK},
{TK_OPTION_INT, TCL_STUPID "-auxbuffers", "auxbuffers", "AuxBuffers",
"0", -1, Tk_Offset(Togl, AuxNumber), 0, NULL, FORMAT_MASK},
{TK_OPTION_BOOLEAN, TCL_STUPID "-privatecmap", "privateCmap", "PrivateCmap",
"false", -1, Tk_Offset(Togl, PrivateCmapFlag), 0, NULL,
FORMAT_MASK},
{TK_OPTION_BOOLEAN, TCL_STUPID "-overlay", "overlay", "Overlay",
"false", -1, Tk_Offset(Togl, OverlayFlag), 0, NULL, OVERLAY_MASK},
{TK_OPTION_CUSTOM, TCL_STUPID "-stereo", "stereo", "Stereo",
"", -1, Tk_Offset(Togl, Stereo), 0,
(ClientData) &stereoOption, STEREO_FORMAT_MASK},
{TK_OPTION_DOUBLE, TCL_STUPID "-eyeseparation", "eyeseparation",
"EyeSeparation",
"2.0", -1, Tk_Offset(Togl, EyeSeparation), 0, NULL, STEREO_MASK},
{TK_OPTION_DOUBLE, TCL_STUPID "-convergence", "convergence", "Convergence",
"35.0", -1, Tk_Offset(Togl, Convergence), 0, NULL, STEREO_MASK},
#ifndef NO_TK_CURSOR
{TK_OPTION_CURSOR, TCL_STUPID "-cursor", "cursor", "Cursor",
"", -1, Tk_Offset(Togl, Cursor), TK_OPTION_NULL_OK, NULL,
CURSOR_MASK},
#endif
{TK_OPTION_INT, TCL_STUPID "-setgrid", "setGrid", "SetGrid",
"0", -1, Tk_Offset(Togl, SetGrid), 0, NULL, GEOMETRY_MASK},
{TK_OPTION_INT, TCL_STUPID "-time", "time", "Time",
DEFAULT_TIME, -1, Tk_Offset(Togl, TimerInterval), 0, NULL,
TIMER_MASK},
{TK_OPTION_STRING, TCL_STUPID "-sharelist", "sharelist", "ShareList",
NULL, -1, Tk_Offset(Togl, ShareList), 0, NULL, FORMAT_MASK},
{TK_OPTION_STRING, TCL_STUPID "-sharecontext", "sharecontext",
"ShareContext", NULL,
-1, Tk_Offset(Togl, ShareContext), 0, NULL, FORMAT_MASK},
{TK_OPTION_STRING, TCL_STUPID "-ident", "ident", "Ident",
DEFAULT_IDENT, -1, Tk_Offset(Togl, Ident), 0, NULL, 0},
{TK_OPTION_BOOLEAN, TCL_STUPID "-indirect", "indirect", "Indirect",
"false", -1, Tk_Offset(Togl, Indirect), 0, NULL, FORMAT_MASK},
{TK_OPTION_INT, TCL_STUPID "-pixelformat", "pixelFormat", "PixelFormat",
"0", -1, Tk_Offset(Togl, PixelFormat), 0, NULL, FORMAT_MASK},
{TK_OPTION_INT, TCL_STUPID "-swapinterval", "swapInterval", "SwapInterval",
"1", -1, Tk_Offset(Togl, SwapInterval), 0, NULL, SWAP_MASK},
{TK_OPTION_STRING, TCL_STUPID "-createcommand", "createCommand",
"CallbackCommand", NULL,
Tk_Offset(Togl, CreateProc), -1, TK_OPTION_NULL_OK, NULL, 0},
{TK_OPTION_SYNONYM, TCL_STUPID "-create", NULL, NULL,
NULL, -1, -1, 0, (ClientData) "-createcommand", 0},
{TK_OPTION_STRING, TCL_STUPID "-displaycommand", "displayCommand",
"CallbackCommand", NULL,
Tk_Offset(Togl, DisplayProc), -1, TK_OPTION_NULL_OK, NULL, 0},
{TK_OPTION_SYNONYM, TCL_STUPID "-display", NULL, NULL,
NULL, -1, -1, 0, (ClientData) "-displaycommand", 0},
{TK_OPTION_STRING, TCL_STUPID "-reshapecommand", "reshapeCommand",
"CallbackCommand", NULL,
Tk_Offset(Togl, ReshapeProc), -1, TK_OPTION_NULL_OK, NULL, 0},
{TK_OPTION_SYNONYM, TCL_STUPID "-reshape", NULL, NULL,
NULL, -1, -1, 0, (ClientData) "-reshapecommand", 0},
{TK_OPTION_STRING, TCL_STUPID "-destroycommand", "destroyCommand",
"CallbackCommand", NULL,
Tk_Offset(Togl, DestroyProc), -1, TK_OPTION_NULL_OK, NULL, 0},
{TK_OPTION_SYNONYM, TCL_STUPID "-destroy", NULL, NULL,
NULL, -1, -1, 0, (ClientData) "-destroycommand", 0},
{TK_OPTION_STRING, TCL_STUPID "-timercommand", "timerCommand",
"CallabckCommand", NULL,
Tk_Offset(Togl, TimerProc), -1, TK_OPTION_NULL_OK, NULL, 0},
{TK_OPTION_SYNONYM, TCL_STUPID "-timer", NULL, NULL,
NULL, -1, -1, 0, (ClientData) "-timercommand", 0},
{TK_OPTION_STRING, TCL_STUPID "-overlaydisplaycommand",
"overlaydisplayCommand", "CallbackCommand", NULL,
Tk_Offset(Togl, OverlayDisplayProc), -1,
TK_OPTION_NULL_OK, NULL, OVERLAY_MASK},
{TK_OPTION_SYNONYM, TCL_STUPID "-overlaydisplay", NULL, NULL,
NULL, -1, -1, 0, (ClientData) "-overlaydisplaycommand", 0},
/* Tcl3D backwards compatibility */
{TK_OPTION_SYNONYM, TCL_STUPID "-createproc", NULL, NULL,
NULL, -1, -1, 0, (ClientData) "-createcommand", 0},
{TK_OPTION_SYNONYM, TCL_STUPID "-displayproc", NULL, NULL,
NULL, -1, -1, 0, (ClientData) "-displaycommand", 0},
{TK_OPTION_SYNONYM, TCL_STUPID "-reshapeproc", NULL, NULL,
NULL, -1, -1, 0, (ClientData) "-reshapecommand", 0},
/* end Tcl3D compatibility */
{TK_OPTION_END, NULL, NULL, NULL, NULL, -1, -1, 0, NULL, 0}
};
/*
* Add given togl widget to linked list.
*/
static void
AddToList(Togl *t)
{
t->Next = t->tpg->toglHead;
t->tpg->toglHead = t;
}
/*
* Remove given togl widget from linked list.
*/
static void
RemoveFromList(Togl *t)
{
Togl *prev;
Togl *cur;
for (cur = t->tpg->toglHead, prev = NULL; cur; prev = cur, cur = cur->Next) {
if (t != cur)
continue;
if (prev) {
prev->Next = cur->Next;
} else {
t->tpg->toglHead = cur->Next;
}
break;
}
if (cur)
cur->Next = NULL;
}
/*
* Return pointer to togl widget given a user identifier string.
*/
static Togl *
FindTogl(Togl *togl, const char *ident)
{
Togl *t;
for (t = togl->tpg->toglHead; t; t = t->Next) {
if (strcmp(t->Ident, ident) == 0)
break;
}
return t;
}
/*
* Return pointer to another togl widget with same OpenGL context.
*/
static Togl *
FindToglWithSameContext(Togl *togl)
{
Togl *t;
for (t = togl->tpg->toglHead; t != NULL; t = t->Next) {
if (t == togl)
continue;
#if defined(TOGL_WGL)
if (t->tglGLHglrc == togl->tglGLHglrc)
#elif defined(TOGL_X11)
if (t->GlCtx == togl->GlCtx)
#elif defined(TOGL_AGL)
if (t->aglCtx == togl->aglCtx)
#endif
{
return t;
}
}
return NULL;
}
#if TOGL_USE_OVERLAY
/*
* Return pointer to another togl widget with same OpenGL overlay context.
*/
static Togl *
FindToglWithSameOverlayContext(Togl *togl)
{
Togl *t;
for (t = togl->tpg->toglHead; t != NULL; t = t->Next) {
if (t == togl)
continue;
# if defined(TOGL_X11)
if (t->OverlayCtx == togl->OverlayCtx)
# elif defined(TOGL_WGL)
if (t->tglGLOverlayHglrc == togl->tglGLOverlayHglrc)
# endif
{
return t;
}
}
return NULL;
}
#endif
#if defined(TOGL_X11)
/*
* Return an X colormap to use for OpenGL RGB-mode rendering.
* Input: dpy - the X display
* scrnum - the X screen number
* visinfo - the XVisualInfo as returned by glXChooseVisual()
* Return: an X Colormap or 0 if there's a _serious_ error.
*/
static Colormap
get_rgb_colormap(Display *dpy,
int scrnum, const XVisualInfo *visinfo, Tk_Window tkwin)
{
Atom hp_cr_maps;
Status status;
int numCmaps;
int i;
XStandardColormap *standardCmaps;
Window root = XRootWindow(dpy, scrnum);
Bool using_mesa;
/*
* First check if visinfo's visual matches the default/root visual.
*/
if (visinfo->visual == Tk_Visual(tkwin)) {
/* use the default/root colormap */
Colormap cmap;
cmap = Tk_Colormap(tkwin);
# ifdef MESA_COLOR_HACK
(void) get_free_color_cells(dpy, scrnum, cmap);
# endif
return cmap;
}
/*
* Check if we're using Mesa.
*/
if (strstr(glXQueryServerString(dpy, scrnum, GLX_VERSION), "Mesa")) {
using_mesa = True;
} else {
using_mesa = False;
}
/*
* Next, if we're using Mesa and displaying on an HP with the "Color
* Recovery" feature and the visual is 8-bit TrueColor, search for a
* special colormap initialized for dithering. Mesa will know how to
* dither using this colormap.
*/
if (using_mesa) {
hp_cr_maps = XInternAtom(dpy, "_HP_RGB_SMOOTH_MAP_LIST", True);
if (hp_cr_maps
# ifdef __cplusplus
&& visinfo->visual->c_class == TrueColor
# else
&& visinfo->visual->class == TrueColor
# endif
&& visinfo->depth == 8) {
status = XGetRGBColormaps(dpy, root, &standardCmaps,
&numCmaps, hp_cr_maps);
if (status) {
for (i = 0; i < numCmaps; i++) {
if (standardCmaps[i].visualid == visinfo->visual->visualid) {
Colormap cmap = standardCmaps[i].colormap;
(void) XFree(standardCmaps);
return cmap;
}
}
(void) XFree(standardCmaps);
}
}
}
/*
* Next, try to find a standard X colormap.
*/
# if !HP && !SUN
# ifndef SOLARIS_BUG
status = XmuLookupStandardColormap(dpy, visinfo->screen,
visinfo->visualid, visinfo->depth, XA_RGB_DEFAULT_MAP,
/* replace */ False, /* retain */ True);
if (status == 1) {
status = XGetRGBColormaps(dpy, root, &standardCmaps,
&numCmaps, XA_RGB_DEFAULT_MAP);
if (status == 1) {
for (i = 0; i < numCmaps; i++) {
if (standardCmaps[i].visualid == visinfo->visualid) {
Colormap cmap = standardCmaps[i].colormap;
(void) XFree(standardCmaps);
return cmap;
}
}
(void) XFree(standardCmaps);
}
}
# endif
# endif
/*
* If we get here, give up and just allocate a new colormap.
*/
return XCreateColormap(dpy, root, visinfo->visual, AllocNone);
}
#elif defined(TOGL_WGL)
/* Code to create RGB palette is taken from the GENGL sample program of Win32
* SDK */
static const unsigned char threeto8[8] = {
0, 0111 >> 1, 0222 >> 1, 0333 >> 1, 0444 >> 1, 0555 >> 1, 0666 >> 1, 0377
};
static const unsigned char twoto8[4] = {
0, 0x55, 0xaa, 0xff
};
static const unsigned char oneto8[2] = {
0, 255
};
static const int defaultOverride[13] = {
0, 3, 24, 27, 64, 67, 88, 173, 181, 236, 247, 164, 91
};
static const PALETTEENTRY defaultPalEntry[20] = {
{0, 0, 0, 0},
{0x80, 0, 0, 0},
{0, 0x80, 0, 0},
{0x80, 0x80, 0, 0},
{0, 0, 0x80, 0},
{0x80, 0, 0x80, 0},
{0, 0x80, 0x80, 0},
{0xC0, 0xC0, 0xC0, 0},
{192, 220, 192, 0},
{166, 202, 240, 0},
{255, 251, 240, 0},
{160, 160, 164, 0},
{0x80, 0x80, 0x80, 0},
{0xFF, 0, 0, 0},
{0, 0xFF, 0, 0},
{0xFF, 0xFF, 0, 0},
{0, 0, 0xFF, 0},
{0xFF, 0, 0xFF, 0},
{0, 0xFF, 0xFF, 0},
{0xFF, 0xFF, 0xFF, 0}
};
static unsigned char
ComponentFromIndex(int i, UINT nbits, UINT shift)
{
unsigned char val;
val = (unsigned char) (i >> shift);
switch (nbits) {
case 1:
val &= 0x1;
return oneto8[val];
case 2:
val &= 0x3;
return twoto8[val];
case 3:
val &= 0x7;
return threeto8[val];
default:
return 0;
}
}
static Colormap
Win32CreateRgbColormap(PIXELFORMATDESCRIPTOR pfd)
{
TkWinColormap *cmap = (TkWinColormap *) ckalloc(sizeof (TkWinColormap));
LOGPALETTE *pPal;
int n, i;
n = 1 << pfd.cColorBits;
pPal = (PLOGPALETTE) LocalAlloc(LMEM_FIXED, sizeof (LOGPALETTE)
+ n * sizeof (PALETTEENTRY));
pPal->palVersion = 0x300;
pPal->palNumEntries = n;
for (i = 0; i < n; i++) {
pPal->palPalEntry[i].peRed =
ComponentFromIndex(i, pfd.cRedBits, pfd.cRedShift);
pPal->palPalEntry[i].peGreen =
ComponentFromIndex(i, pfd.cGreenBits, pfd.cGreenShift);
pPal->palPalEntry[i].peBlue =
ComponentFromIndex(i, pfd.cBlueBits, pfd.cBlueShift);
pPal->palPalEntry[i].peFlags = 0;
}
/* fix up the palette to include the default GDI palette */
if ((pfd.cColorBits == 8)
&& (pfd.cRedBits == 3) && (pfd.cRedShift == 0)
&& (pfd.cGreenBits == 3) && (pfd.cGreenShift == 3)
&& (pfd.cBlueBits == 2) && (pfd.cBlueShift == 6)) {
for (i = 1; i <= 12; i++)
pPal->palPalEntry[defaultOverride[i]] = defaultPalEntry[i];
}
cmap->palette = CreatePalette(pPal);
LocalFree(pPal);
cmap->size = n;
cmap->stale = 0;
/* Since this is a private colormap of a fix size, we do not need a valid
* hash table, but a dummy one */
Tcl_InitHashTable(&cmap->refCounts, TCL_ONE_WORD_KEYS);
return (Colormap) cmap;
}
static Colormap
Win32CreateCiColormap(Togl *togl)
{
/* Create a colormap with size of togl->CiColormapSize and set all entries
* to black */
LOGPALETTE logPalette;
TkWinColormap *cmap = (TkWinColormap *) ckalloc(sizeof (TkWinColormap));
logPalette.palVersion = 0x300;
logPalette.palNumEntries = 1;
logPalette.palPalEntry[0].peRed = 0;
logPalette.palPalEntry[0].peGreen = 0;
logPalette.palPalEntry[0].peBlue = 0;
logPalette.palPalEntry[0].peFlags = 0;
cmap->palette = CreatePalette(&logPalette);
cmap->size = togl->CiColormapSize;
ResizePalette(cmap->palette, cmap->size); /* sets new entries to black */
cmap->stale = 0;
/* Since this is a private colormap of a fix size, we do not need a valid
* hash table, but a dummy one */
Tcl_InitHashTable(&cmap->refCounts, TCL_ONE_WORD_KEYS);
return (Colormap) cmap;
}
/* ErrorExit is from <http://msdn2.microsoft.com/en-us/library/ms680582.aspx> */
static void
ErrorExit(LPTSTR lpszFunction)
{
/* Retrieve the system error message for the last-error code */
LPTSTR lpMsgBuf;
LPTSTR lpDisplayBuf;
DWORD err = GetLastError();
if (err == 0) {
/* The function said it failed, but GetLastError says it didn't, so
* pretend it didn't. */
return;
}
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf, 0, NULL);
/* Display the error message and exit the process */
lpDisplayBuf = (LPTSTR) LocalAlloc(LMEM_ZEROINIT,
(lstrlen(lpMsgBuf) + lstrlen(lpszFunction) + 40) * sizeof (TCHAR));
StringCchPrintf(lpDisplayBuf, LocalSize(lpDisplayBuf),
TEXT("%s failed with error %ld: %s"), lpszFunction, err, lpMsgBuf);
MessageBox(NULL, lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(err);
}
#endif
/*
* Togl_Init
*
* Called upon system startup to create togl command.
*/
int
Togl_Init(Tcl_Interp *interp)
{
int major, minor, patchLevel, releaseType;
#ifdef USE_TCL_STUBS
if (Tcl_InitStubs(interp, "8.1", 0) == NULL) {
return TCL_ERROR;
}
#endif
#ifdef USE_TK_STUBS
if (Tk_InitStubs(interp, TCL_STUPID "8.1", 0) == NULL) {
return TCL_ERROR;
}
#endif
Tcl_GetVersion(&major, &minor, &patchLevel, &releaseType);
#ifdef HAVE_TK_SETCLASSPROCS
if (major > 8
|| (major == 8
&& (minor > 4
|| (minor == 4 && (releaseType > 0
|| patchLevel >= 2))))) {
# ifdef USE_TK_STUBS
SetClassProcsPtr = tkStubsPtr->tk_SetClassProcs;
# else
SetClassProcsPtr = Tk_SetClassProcs;
# endif
} else {
SetClassProcsPtr = NULL;
}
#else
if (major > 8
|| (major == 8
&& (minor > 4
|| (minor == 4 && (releaseType > 0
|| patchLevel >= 2))))) {
TCL_ERR(interp,
"Sorry, this instance of Togl was not compiled to work with Tcl/Tk 8.4a2 or higher.");
}
#endif
if (Tcl_CreateObjCommand(interp, "togl", Togl_ObjCmd, NULL,
Togl_ObjCmdDelete) == NULL) {
return TCL_ERROR;
}
if (Tcl_PkgProvideEx(interp, "Togl", TOGL_VERSION, &toglStubs) != TCL_OK) {
return TCL_ERROR;
}
return TCL_OK;
}
/*
* Togl_CallCallback
*
* Call command with togl widget as only argument
*/
int
Togl_CallCallback(Togl *togl, Tcl_Obj *cmd)
{
int result;
Tcl_Obj *objv[3];
if (cmd == NULL || togl->widgetCmd == NULL)
return TCL_OK;
objv[0] = cmd;
Tcl_IncrRefCount(objv[0]);
objv[1] =
Tcl_NewStringObj(Tcl_GetCommandName(togl->Interp, togl->widgetCmd),
-1);
Tcl_IncrRefCount(objv[1]);
objv[2] = NULL;
result = Tcl_EvalObjv(togl->Interp, 2, objv, TCL_EVAL_GLOBAL);
Tcl_DecrRefCount(objv[1]);
Tcl_DecrRefCount(objv[0]);
return result;
}
/*
* Togl_Timer
*
* Gets called from Tk_CreateTimerHandler.
*/
static void
Togl_Timer(ClientData clientData)
{
Togl *togl = (Togl *) clientData;
if (togl->TimerProc) {
if (Togl_CallCallback(togl, togl->TimerProc) != TCL_OK) {
togl->timerHandler = NULL;
return;
}
/*
* Re-register this callback since Tcl/Tk timers are "one-shot".
* That is, after the timer callback is called it not normally
* called again. That's not the behavior we want for Togl.
*/
togl->timerHandler =
Tcl_CreateTimerHandler(togl->TimerInterval, Togl_Timer,
(ClientData) togl);
}
}
/*
* Togl_MakeCurrent
*
* Bind the OpenGL rendering context to the specified
* Togl widget. If given a NULL argument, then the
* OpenGL context is released without assigning a new one.
*/
void
Togl_MakeCurrent(const Togl *togl)
{
#if defined(TOGL_WGL)
int res = TRUE;
if (togl == NULL) {
HDC hdc = wglGetCurrentDC();
if (hdc != NULL)
res = wglMakeCurrent(hdc, NULL);
} else {
res = wglMakeCurrent(togl->tglGLHdc, togl->tglGLHglrc);
}
if (!res) {
ErrorExit(TEXT("wglMakeCurrent"));
}
#elif defined(TOGL_X11)
if (togl == NULL || togl->GlCtx == NULL) {
Display *display = togl ? togl->display : glXGetCurrentDisplay();
if (display)
(void) glXMakeCurrent(display, None, NULL);
} else {
GLXDrawable drawable = togl->TkWin ? Tk_WindowId(togl->TkWin) : None;
(void) glXMakeCurrent(togl->display, drawable, togl->GlCtx);
}
#elif defined(TOGL_AGL)
if (togl == NULL || togl->aglCtx == NULL) {
(void) aglSetCurrentContext(NULL);
} else {
(void) aglSetCurrentContext(togl->aglCtx);
}
#endif
}
/*
* Togl_TakePhoto
*
* Take a photo image of the current OpenGL window. May have problems
* if window is partially obscured, either by other windows or by the
* edges of the display.
*/
int
Togl_TakePhoto(Togl *togl, Tk_PhotoHandle photo)
{
GLubyte *buffer;
Tk_PhotoImageBlock photoBlock;
int y, midy;
unsigned char *cp;
int width = Togl_Width(togl), height = Togl_Height(togl);
/*
* TIP #116 altered Tk_PhotoPutBlock API to add interp arg that 8.4
* doesn't have.
* We need to remove that for compiling with 8.4.
*/
#if (TK_MAJOR_VERSION == 8) && (TK_MINOR_VERSION < 5)
# define TK_PHOTOPUTBLOCK(interp, hdl, blk, x, y, w, h, cr) \
Tk_PhotoPutBlock(hdl, blk, x, y, w, h, cr)
#else
# define TK_PHOTOPUTBLOCK Tk_PhotoPutBlock
#endif
buffer = (GLubyte *) ckalloc(width * height * 4);
photoBlock.pixelPtr = buffer;
photoBlock.width = width;
photoBlock.height = height;
photoBlock.pitch = width * 4;
photoBlock.pixelSize = 4;
photoBlock.offset[0] = 0;
photoBlock.offset[1] = 1;
photoBlock.offset[2] = 2;
photoBlock.offset[3] = 3;
glPushAttrib(GL_PIXEL_MODE_BIT);
if (togl->DoubleFlag) {
glReadBuffer(GL_FRONT);
}
if (!togl->RgbaFlag) {
#if defined(TOGL_WGL)
/* Due to the lack of a unique inverse mapping from the frame buffer to
* the logical palette we need a translation map from the complete
* logical palette. */
int n, i;
TkWinColormap *cmap = (TkWinColormap *) Tk_Colormap(togl->TkWin);
LPPALETTEENTRY entry =
(LPPALETTEENTRY) malloc(togl->EpsMapSize *
sizeof (PALETTEENTRY));
n = GetPaletteEntries(cmap->palette, 0, togl->EpsMapSize, entry);
for (i = 0; i < n; i++) {
togl->EpsRedMap[i] = (GLfloat) (entry[i].peRed / 255.0);
togl->EpsGreenMap[i] = (GLfloat) (entry[i].peGreen / 255.0);
togl->EpsBlueMap[i] = (GLfloat) (entry[i].peBlue / 255.0);
}
free(entry);
#endif /* TOGL_WGL */
glPixelMapfv(GL_PIXEL_MAP_I_TO_R, togl->EpsMapSize, togl->EpsRedMap);
glPixelMapfv(GL_PIXEL_MAP_I_TO_G, togl->EpsMapSize, togl->EpsGreenMap);
glPixelMapfv(GL_PIXEL_MAP_I_TO_B, togl->EpsMapSize, togl->EpsBlueMap);
}
glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
glPixelStorei(GL_PACK_ALIGNMENT, 4); /* guarantee performance */
glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
#if 1
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glPixelStorei(GL_PACK_SKIP_ROWS, 0);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
/* Some OpenGL drivers are buggy and return zero for Alpha instead of one
* for RGB pixel formats. If that is happening to you, upgrade your
* graphics driver. */
/* OpenGL's origin is bottom-left, Tk Photo image's is top-left, so mirror
* the rows around the middle row. */
midy = height / 2;
cp = buffer;
for (y = 0; y < midy; ++y) {
int m_y = height - 1 - y; /* mirror y */
unsigned char *m_cp = buffer + m_y * photoBlock.pitch;
int x;
for (x = 0; x < photoBlock.pitch; ++x) {
unsigned char c = *cp;
*cp++ = *m_cp;
*m_cp++ = c;
}
}
#else
/* OpenGL's origin is bottom-left, Tk Photo image's is top-left, so save
* rows in reverse order. */
glPixelStorei(GL_PACK_ROW_LENGTH, width);
glPixelStorei(GL_PACK_SKIP_ROWS, -1);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE,
buffer + width * (height - 1) * 4);
#endif
TK_PHOTOPUTBLOCK(togl->Interp, photo, &photoBlock, 0, 0, width, height,
TK_PHOTO_COMPOSITE_SET);
glPopClientAttrib(); /* restore PACK_ALIGNMENT */
glPopAttrib(); /* restore glReadBuffer */
ckfree((char *) buffer);
return TCL_OK;
}
Bool
Togl_SwapInterval(const Togl *togl, int interval)
{
#ifdef TOGL_AGL
GLint swapInterval = interval;
return aglSetInteger(togl->aglCtx, AGL_SWAP_INTERVAL, &swapInterval);
#endif
#ifdef TOGL_WGL
typedef BOOL (WINAPI *BOOLFuncInt) (int);
typedef const char *(WINAPI *StrFuncHDC) (HDC);
static BOOLFuncInt swapInterval = NULL;
static initialized = False;
if (!initialized) {
const char *extensions;
StrFuncHDC getExtensionsString;
getExtensionsString = (StrFuncHDC)
wglGetProcAddress("wglGetExtensionsStringARB");
if (getExtensionsString == NULL)
getExtensionsString = (StrFuncHDC)
wglGetProcAddress("wglGetExtensionsStringEXT");
if (getExtensionsString) {
extensions = getExtensionsString(togl->tglGLHdc);
if (strstr(extensions, "WGL_EXT_swap_control") != NULL) {
swapInterval =
(BOOLFuncInt) wglGetProcAddress("wglSwapIntervalEXT");
}
}
initialized = True;
}
if (swapInterval)
return swapInterval(interval);
return False;
#endif
#ifdef TOGL_X11
typedef int (*IntFuncInt) (int);
static IntFuncInt swapInterval = NULL;
static int initialized = False;
if (!initialized) {
const char *extensions = glXQueryExtensionsString(togl->display,
Tk_ScreenNumber(togl->TkWin));
if (strstr(extensions, "GLX_SGI_swap_control") != NULL) {
swapInterval = (IntFuncInt) Togl_GetProcAddr("glXSwapIntervalSGI");
} else if (strstr(extensions, "GLX_MESA_swap_control") != NULL) {
swapInterval = (IntFuncInt) Togl_GetProcAddr("glXSwapIntervalMESA");
}
initialized = True;
}
if (swapInterval)
return swapInterval(interval) == 0;
return False;
#endif
}
#if defined(TOGL_AGL)
/* tell OpenGL which part of the Mac window to render to */
static void
SetMacBufRect(Togl *togl)
{
GLint wrect[4];
/* set wrect[0,1] to lower left corner of widget */
wrect[2] = Tk_Width(togl->TkWin);
wrect[3] = Tk_Height(togl->TkWin);
wrect[0] = ((TkWindow *) (togl->TkWin))->privatePtr->xOff;
Rect r;
GetPortBounds(((TkWindow *) (togl->TkWin))->privatePtr->toplevel->grafPtr,
&r);
wrect[1] = r.bottom -
wrect[3] - ((TkWindow *) (togl->TkWin))->privatePtr->yOff;
aglSetInteger(togl->aglCtx, AGL_BUFFER_RECT, wrect);
aglEnable(togl->aglCtx, AGL_BUFFER_RECT);
aglUpdateContext(togl->aglCtx);
}
#endif
/*
* Called when the widget's contents must be redrawn. Basically, we
* just call the user's render callback function.
*
* Note that the parameter type is ClientData so this function can be
* passed to Tk_DoWhenIdle().
*/
static void
Togl_Render(ClientData clientData)
{
Togl *togl = (Togl *) clientData;
if (togl->DisplayProc) {
Togl_MakeCurrent(togl);
#ifdef TOGL_AGL
SetMacBufRect(togl);
#endif
Togl_CallCallback(togl, togl->DisplayProc);
}
#if defined(TOGL_AGL)
else {
/* Always need to update on resize */
SetMacBufRect(togl);
}
#endif
togl->UpdatePending = False;
}
static void
Togl_RenderOverlay(ClientData clientData)
{
Togl *togl = (Togl *) clientData;
if (togl->OverlayFlag && togl->OverlayDisplayProc) {
#if defined(TOGL_WGL)
int res = wglMakeCurrent(togl->tglGLHdc, togl->tglGLOverlayHglrc);
if (!res) {
ErrorExit(TEXT("wglMakeCurrent overlay"));
}
#elif defined(TOGL_X11)
(void) glXMakeCurrent(Tk_Display(togl->TkWin),
togl->OverlayWindow, togl->OverlayCtx);
#endif /* TOGL_WGL */
Togl_CallCallback(togl, togl->OverlayDisplayProc);
}
togl->OverlayUpdatePending = False;
}
static void
Togl_LeaveStereo(Togl *togl, int oldStereo)
{
switch (oldStereo) {
default:
break;
#ifdef STEREO_I_H
case TOGL_STEREO_NVIDIA_CON:
if (togl->pStereoI) {
togl->pStereoI->SetStereoState(STEREO_STATE_OFF);
}
break;
#endif
#ifdef HAVE_AUTOSTEREO
case TOGL_STEREO_NATIVE:
if (togl->ash != -1) {
ASClosedStereoWindow(togl->ash);
togl->ash = -1;
}
break;
#endif
#ifdef __sgi
case TOGL_STEREO_SGIOLDSTYLE:
togl->currentStereoBuffer = STEREO_BUFFER_NONE;
glXWaitGL(); /* sync with GL command stream before calling X
*/
XSGISetStereoBuffer(togl->display, Tk_WindowId(togl->TkWin),
togl->currentStereoBuffer);
glXWaitX(); /* sync with X command stream before calling GL
*/
break;
#endif
}
}
/*
* See domentation about what can't be changed
*/
static int
Togl_ObjConfigure(Tcl_Interp *interp, Togl *togl,
int objc, Tcl_Obj *const *objv)
{
Tk_SavedOptions savedOptions;
int error, mask;
int undoMask = 0;
Tcl_Obj *errorResult = NULL;
int oldStereo = togl->Stereo;
for (error = 0; error <= 1; ++error, mask = undoMask) {
if (error == 0) {
/*
* Tk_SetOptions parses the command arguments
* and looks for defaults in the resource database.
*/
if (Tk_SetOptions(interp, WIDGREC togl, togl->tpg->optionTable,
objc, objv, togl->TkWin, &savedOptions, &mask)
!= TCL_OK) {
/* previous values are restored, so nothing to do */
return TCL_ERROR;
}
} else {
/*
* Restore options from saved values
*/
errorResult = Tcl_GetObjResult(interp);
Tcl_IncrRefCount(errorResult);
Tk_RestoreSavedOptions(&savedOptions);
}
if (mask & GEOMETRY_MASK) {
Togl_WorldChanged((ClientData) togl);
/* this added per Lou Arata <arata@enya.picker.com> */
Tk_ResizeWindow(togl->TkWin, togl->Width, togl->Height);
if (togl->ReshapeProc &&
#if defined(TOGL_WGL)
togl->tglGLHglrc
#elif defined(TOGL_X11)
togl->GlCtx
#elif defined(TOGL_AGL)
togl->aglCtx
#endif
) {
Togl_MakeCurrent(togl);
Togl_CallCallback(togl, togl->ReshapeProc);
}
undoMask |= GEOMETRY_MASK;
}
if (mask & OVERLAY_MASK) {
#if !TOGL_USE_OVERLAY
if (togl->OverlayFlag) {
Tcl_AppendResult(interp, "Sorry, overlay was disabled", NULL);
continue;
}
#else
# if defined(TOGL_X11)
if (togl->OverlayCtx)
# elif defined(TOGL_WGL)
if (togl->tglGLOverlayHglrc)
# endif
{
/*
* Trying to change existing pixel format/graphics context
*/
Tcl_AppendResult(interp,
"Unable to change overlay pixel format", NULL);
continue;
}
#endif
}
if (mask & SWAP_MASK) {
if (
#if defined(TOGL_WGL)
togl->tglGLHglrc
#elif defined(TOGL_X11)
togl->GlCtx
#elif defined(TOGL_AGL)
togl->aglCtx
#endif
) {
/*
* Change existing swap interval
*/
Togl_MakeCurrent(togl); /* TODO: needed? */
Togl_SwapInterval(togl, togl->SwapInterval);
undoMask |= SWAP_MASK;
}
}
if (error == 0 && (mask & STEREO_FORMAT_MASK) != 0) {
if (oldStereo == TOGL_STEREO_NATIVE
|| togl->Stereo == TOGL_STEREO_NATIVE) {
/* only native stereo affects the visual format */
mask |= FORMAT_MASK;
}
if (togl->Stereo == TOGL_STEREO_SGIOLDSTYLE) {
#ifndef __sgi
Tcl_AppendResult(interp,
"sgioldstyle: only available on SGI computers", NULL);
continue;
#else
int event, error;
/* Make sure Display supports SGIStereo */
if (XSGIStereoQueryExtension(Tk_Display(togl->TkWin), &event,
&error) == False) {
Tcl_AppendResult(interp,
"sgioldstyle: SGIStereo X extension is missing",
NULL);
continue;
}
/* Make sure Window (Screen) supports SGIStereo */
if (XSGIQueryStereoMode(Tk_Display(togl->TkWin),
Tk_WindowId(Tk_Parent(togl->TkWin))) ==
X_STEREO_UNSUPPORTED) {
Tcl_AppendResult(interp,
"sgioldstyle: unsupported by screen", NULL);
continue;
}
#endif
}
}
if (mask & FORMAT_MASK) {
#if defined(TOGL_WGL)
if (togl->tglGLHglrc)
#elif defined(TOGL_X11)
if (togl->GlCtx)
#elif defined(TOGL_AGL)
if (togl->aglCtx)
#endif
{
/*
* Trying to change existing pixel format/graphics context
* TODO: (re)create graphics context
*
* save old graphics context
* try to create new one and share display lists
* if failure, then restore old one
*/
Tcl_AppendResult(interp, "Unable to change pixel format", NULL);
continue;
}
/* Whether or not the format is okay is figured out when togl tries
* to create the window. */
#ifdef MESA_COLOR_HACK
free_default_color_cells(Tk_Display(togl->TkWin),
Tk_Colormap(togl->TkWin));
#endif
undoMask |= FORMAT_MASK;
}
if (oldStereo != togl->Stereo) {
/* leaving stereo */
Togl_LeaveStereo(togl, oldStereo);
}
#if !defined(_WIN32) || !defined(STEREO_I_H)
if (togl->Stereo == TOGL_STEREO_NVIDIA_CON) {
# ifndef _WIN32
Tcl_AppendResult(interp, NC3D ": Microsoft Windows option only",
NULL);
continue;
# elif !defined(STEREO_I_H)
Tcl_AppendResult(interp,
NC3D
": only available when togl is compiled with Microsoft Visual Studio",
NULL);
continue;
# endif
}
#endif
if (mask & TIMER_MASK) {
if (togl->timerHandler != NULL) {
Tcl_DeleteTimerHandler(togl->timerHandler);
}
if (togl->TimerProc) {
togl->timerHandler =
Tcl_CreateTimerHandler(togl->TimerInterval, Togl_Timer,
(ClientData) togl);
}
undoMask |= TIMER_MASK;
}
break;
}
if (error == 0) {
Tk_FreeSavedOptions(&savedOptions);
return TCL_OK;
} else {
Tcl_SetObjResult(interp, errorResult);
Tcl_DecrRefCount(errorResult);
return TCL_ERROR;
}
}
static int
Togl_ObjWidget(ClientData clientData, Tcl_Interp *interp, int objc,
Tcl_Obj *const *objv)
{
Togl *togl = (Togl *) clientData;
const char *commands[] = {
"cget", "configure", "extensions",
"postredisplay", "render",
"swapbuffers", "makecurrent", "takephoto",
"loadbitmapfont", "unloadbitmapfont", "write",
"uselayer", "showoverlay", "hideoverlay",
"postredisplayoverlay", "renderoverlay",
"existsoverlay", "ismappedoverlay",
"getoverlaytransparentvalue",
"drawbuffer", "clear", "frustum", "ortho",
"numeyes", "contexttag",
NULL
};
enum command
{
TOGL_CGET, TOGL_CONFIGURE, TOGL_EXTENSIONS,
TOGL_POSTREDISPLAY, TOGL_RENDER,
TOGL_SWAPBUFFERS, TOGL_MAKECURRENT, TOGL_TAKEPHOTO,
TOGL_LOADBITMAPFONT, TOGL_UNLOADBITMAPFONT, TOGL_WRITE,
TOGL_USELAYER, TOGL_SHOWOVERLAY, TOGL_HIDEOVERLAY,
TOGL_POSTREDISPLAYOVERLAY, TOGL_RENDEROVERLAY,
TOGL_EXISTSOVERLAY, TOGL_ISMAPPEDOVERLAY,
TOGL_GETOVERLAYTRANSPARENTVALUE,
TOGL_DRAWBUFFER, TOGL_CLEAR, TOGL_FRUSTUM, TOGL_ORTHO,
TOGL_NUMEYES, TOGL_CONTEXTTAG
};
int result = TCL_OK;
Tcl_Obj *objPtr;
int index;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "command ?arg arg ...?");
return TCL_ERROR;
}
Tk_Preserve((ClientData) togl);
result = Tcl_GetIndexFromObj(interp, objv[1], commands, "option", 0,
&index);
switch (index) {
case TOGL_CGET:
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "option");
result = TCL_ERROR;
break;
}
objPtr = Tk_GetOptionValue(interp, WIDGREC togl,
togl->tpg->optionTable, (objc == 3) ? objv[2] : NULL,
togl->TkWin);
if (objPtr == NULL) {
result = TCL_ERROR;
break;
}
Tcl_SetObjResult(interp, objPtr);
break;
case TOGL_CONFIGURE:
if (objc <= 3) {
/*
* Return one item if the option is given,
* or return all configuration information
*/
objPtr = Tk_GetOptionInfo(interp, WIDGREC togl,
togl->tpg->optionTable, (objc == 3) ? objv[2] : NULL,
togl->TkWin);
if (objPtr == NULL) {
result = TCL_ERROR;
} else {
Tcl_SetObjResult(interp, objPtr);
}
} else {
/* Execute a configuration change */
result = Togl_ObjConfigure(interp, togl, objc - 2, objv + 2);
}
break;
case TOGL_EXTENSIONS:
/* Return a list of OpenGL extensions available */
/* TODO: -glu for glu extensions, -platform for glx/wgl extensions */
if (objc == 2) {
const char *extensions;
Tcl_Obj *objPtr;
int length = -1;
extensions = (const char *) glGetString(GL_EXTENSIONS);
objPtr = Tcl_NewStringObj(extensions, -1);
/* convert to list by asking for its length */
(void) Tcl_ListObjLength(interp, objPtr, &length);
Tcl_SetObjResult(interp, objPtr);
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
result = TCL_ERROR;
}
break;
case TOGL_POSTREDISPLAY:
/* schedule the widget to be redrawn */
if (objc == 2) {
Togl_PostRedisplay(togl);
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
result = TCL_ERROR;
}
break;
case TOGL_RENDER:
/* force the widget to be redrawn */
if (objc == 2) {
Togl_Render((ClientData) togl);
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
result = TCL_ERROR;
}
break;
case TOGL_SWAPBUFFERS:
/* force the widget to be redrawn */
if (objc == 2) {
Togl_SwapBuffers(togl);
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
result = TCL_ERROR;
}
break;
case TOGL_MAKECURRENT:
/* force the widget to be redrawn */
if (objc == 2) {
Togl_MakeCurrent(togl);
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
result = TCL_ERROR;
}
break;
case TOGL_TAKEPHOTO:
{
/* force the widget to be redrawn */
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "name");
result = TCL_ERROR;
} else {
const char *name;
Tk_PhotoHandle photo;
name = Tcl_GetStringFromObj(objv[2], NULL);
photo = Tk_FindPhoto(interp, name);
if (photo == NULL) {
Tcl_AppendResult(interp, "image \"", name,
"\" doesn't exist or is not a photo image", NULL);
result = TCL_ERROR;
break;
}
Togl_TakePhoto(togl, photo);
}
break;
}
case TOGL_LOADBITMAPFONT:
#if TOGL_USE_FONTS != 1
Tcl_AppendResult(interp, "unsupported", NULL);
result = TCL_ERROR;
#else
if (objc >= 3) {
Tcl_Obj *font, *list;
list = Tcl_NewListObj(objc - 2, objv + 2);
Tcl_IncrRefCount(list);
font = Togl_LoadBitmapFont(togl, Tcl_GetString(list));
Tcl_DecrRefCount(list);
if (font) {
Tcl_SetObjResult(interp, font);
result = TCL_OK;
} else {
Tcl_AppendResult(interp, "Could not allocate font", NULL);
result = TCL_ERROR;
}
} else {
Tcl_WrongNumArgs(interp, 2, objv, "fontname");
result = TCL_ERROR;
}
#endif
break;
case TOGL_UNLOADBITMAPFONT:
#if TOGL_USE_FONTS != 1
Tcl_AppendResult(interp, "unsupported", NULL);
result = TCL_ERROR;
#else
if (objc == 3) {
result = Togl_UnloadBitmapFont(togl, objv[2]);
} else {
Tcl_WrongNumArgs(interp, 2, objv, "toglfont");
result = TCL_ERROR;
}
#endif
break;
case TOGL_WRITE:{
#if TOGL_USE_FONTS != 1
Tcl_AppendResult(interp, "unsupported", NULL);
result = TCL_ERROR;
#else
/* Tcl_Obj *toglfont = objv[2]; */
int wobjc = objc - 3;
Tcl_Obj *const *wobjv = objv + 3;
while (wobjc > 1) {
const char *name = Tcl_GetStringFromObj(wobjv[0], NULL);
int oc, i;
Tcl_Obj **ov;
double args[4];
if (Tcl_ListObjGetElements(NULL, wobjv[1], &oc, &ov) != TCL_OK) {
oc = 0;
} else if (oc <= 4) {
for (i = 0; i < oc; ++i) {
if (Tcl_GetDoubleFromObj(NULL, ov[i], &args[i]) != TCL_OK) {
}
}
}
if (strcmp(name, "-color") == 0) {
if (oc == 4)
glColor4f(args[0], args[1], args[2], args[3]);
else if (oc == 3)
glColor3f(args[0], args[1], args[2]);
else
goto write_usage;
} else if (strcmp(name, "-pos") == 0) {
if (oc == 4)
glRasterPos4f(args[0], args[1], args[2], args[3]);
else if (oc == 3)
glRasterPos3f(args[0], args[1], args[2]);
else if (oc == 2)
glRasterPos2f(args[0], args[1]);
else
goto write_usage;
} else
goto write_usage;
wobjc -= 2;
wobjv += 2;
}
if (wobjc != 1)
goto write_usage;
result = Togl_WriteObj(togl, objv[2], wobjv[0]);
if (result != -1)
result = TCL_OK;
else {
Tcl_AppendResult(interp, "togl write failed", NULL);
result = TCL_ERROR;
}
break;
write_usage:
Tcl_WrongNumArgs(interp, 2, objv, "[-pos {x y [z [w]]}]"
" [-color {r g b [a]}" " string");
result = TCL_ERROR;
#endif
break;
}
case TOGL_USELAYER:
if (objc == 3) {
int layer;
result = Tcl_GetIntFromObj(interp, objv[2], &layer);
if (result == TCL_OK) {
Togl_UseLayer(togl, layer);
}
} else {
Tcl_WrongNumArgs(interp, 2, objv, "layer");
result = TCL_ERROR;
}
break;
case TOGL_SHOWOVERLAY:
if (objc == 2) {
Togl_ShowOverlay(togl);
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
result = TCL_ERROR;
}
break;
case TOGL_HIDEOVERLAY:
if (objc == 2) {
Togl_HideOverlay(togl);
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
result = TCL_ERROR;
}
break;
case TOGL_POSTREDISPLAYOVERLAY:
if (objc == 2) {
Togl_PostOverlayRedisplay(togl);
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
result = TCL_ERROR;
}
break;
case TOGL_RENDEROVERLAY:
/* force the overlay to be redrawn */
if (objc == 2) {
Togl_RenderOverlay((ClientData) togl);
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
result = TCL_ERROR;
}
break;
case TOGL_EXISTSOVERLAY:
if (objc == 2) {
Tcl_SetObjResult(interp, Tcl_NewIntObj(Togl_ExistsOverlay(togl)));
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
result = TCL_ERROR;
}
break;
case TOGL_ISMAPPEDOVERLAY:
if (objc == 2) {
Tcl_SetObjResult(interp,
Tcl_NewIntObj(Togl_IsMappedOverlay(togl)));
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
result = TCL_ERROR;
}
break;
case TOGL_GETOVERLAYTRANSPARENTVALUE:
if (objc == 2) {
Tcl_SetObjResult(interp,
Tcl_NewIntObj(Togl_GetOverlayTransparentValue(togl)));
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
result = TCL_ERROR;
}
break;
case TOGL_DRAWBUFFER:
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "mode");
result = TCL_ERROR;
} else {
int mask;
result = Tcl_GetIntFromObj(interp, objv[2], &mask);
if (result == TCL_ERROR)
break;
Togl_DrawBuffer(togl, (GLenum) mask);
}
break;
case TOGL_CLEAR:
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "mask");
result = TCL_ERROR;
} else {
int mask;
result = Tcl_GetIntFromObj(interp, objv[2], &mask);
if (result == TCL_ERROR)
break;
Togl_Clear(togl, (GLbitfield) mask);
}
break;
case TOGL_FRUSTUM:
if (objc != 8) {
Tcl_WrongNumArgs(interp, 2, objv,
"left right bottom top near far");
result = TCL_ERROR;
} else {
double left, right, bottom, top, zNear, zFar;
if (Tcl_GetDoubleFromObj(interp, objv[2], &left) == TCL_ERROR
|| Tcl_GetDoubleFromObj(interp, objv[3],
&right) == TCL_ERROR
|| Tcl_GetDoubleFromObj(interp, objv[4],
&bottom) == TCL_ERROR
|| Tcl_GetDoubleFromObj(interp, objv[5],
&top) == TCL_ERROR
|| Tcl_GetDoubleFromObj(interp, objv[6],
&zNear) == TCL_ERROR
|| Tcl_GetDoubleFromObj(interp, objv[7],
&zFar) == TCL_ERROR) {
result = TCL_ERROR;
break;
}
Togl_Frustum(togl, left, right, bottom, top, zNear, zFar);
}
break;
case TOGL_ORTHO:
if (objc != 8) {
Tcl_WrongNumArgs(interp, 2, objv,
"left right bottom top near far");
result = TCL_ERROR;
} else {
double left, right, bottom, top, zNear, zFar;
if (Tcl_GetDoubleFromObj(interp, objv[2], &left) == TCL_ERROR
|| Tcl_GetDoubleFromObj(interp, objv[3],
&right) == TCL_ERROR
|| Tcl_GetDoubleFromObj(interp, objv[4],
&bottom) == TCL_ERROR
|| Tcl_GetDoubleFromObj(interp, objv[5],
&top) == TCL_ERROR
|| Tcl_GetDoubleFromObj(interp, objv[6],
&zNear) == TCL_ERROR
|| Tcl_GetDoubleFromObj(interp, objv[7],
&zFar) == TCL_ERROR) {
result = TCL_ERROR;
break;
}
Togl_Ortho(togl, left, right, bottom, top, zNear, zFar);
}
break;
case TOGL_NUMEYES:
if (objc == 2) {
Tcl_SetObjResult(interp, Tcl_NewIntObj(Togl_NumEyes(togl)));
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
result = TCL_ERROR;
}
break;
case TOGL_CONTEXTTAG:
if (objc == 2) {
Tcl_SetObjResult(interp, Tcl_NewIntObj(Togl_ContextTag(togl)));
} else {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
result = TCL_ERROR;
}
break;
}
Tk_Release((ClientData) togl);
return result;
}
/*
* Togl_ObjCmdDelete
*
* Called when togl command is removed from interpreter.
*/
static void
Togl_ObjCmdDelete(ClientData clientData)
{
if (clientData != NULL) {
Togl_PackageGlobals *tpg = (Togl_PackageGlobals *) clientData;
Tk_DeleteOptionTable(tpg->optionTable);
ckfree((char *) clientData);
}
}
/*
* Togl_ObjCmd
*
* Called when Togl is executed - creation of a Togl widget.
* * Creates a new window
* * Creates an 'Togl' data structure
* * Creates an event handler for this window
* * Creates a command that handles this object
* * Configures this Togl for the given arguments
*/
int
Togl_ObjCmd(ClientData clientData, Tcl_Interp *interp, int objc,
Tcl_Obj *const *objv)
{
Togl_PackageGlobals *tpg;
Togl *togl;
Tk_Window tkwin;
if (objc <= 1) {
Tcl_WrongNumArgs(interp, 1, objv, "pathName ?options?");
return TCL_ERROR;
}
tpg = (Togl_PackageGlobals *) clientData;
if (tpg == NULL) {
Tcl_CmdInfo info;
const char *name;
/*
* Initialize the Togl_PackageGlobals for this widget the
* first time a Togl widget is created. The globals are
* saved as our client data.
*/
tpg = (Togl_PackageGlobals *) ckalloc(sizeof (Togl_PackageGlobals));
if (tpg == NULL) {
return TCL_ERROR;
}
tpg->nextContextTag = 0;
tpg->optionTable = Tk_CreateOptionTable(interp, optionSpecs);
tpg->toglHead = NULL;
name = Tcl_GetString(objv[0]);
Tcl_GetCommandInfo(interp, name, &info);
info.objClientData = (ClientData) tpg;
Tcl_SetCommandInfo(interp, name, &info);
}
/* Create the window. */
tkwin = Tk_CreateWindowFromPath(interp, Tk_MainWindow(interp),
Tcl_GetString(objv[1]), NULL);
if (tkwin == NULL) {
return TCL_ERROR;
}
Tk_SetClass(tkwin, "Togl");
/* Create Togl data structure */
togl = (Togl *) ckalloc(sizeof (Togl));
if (togl == NULL) {
return TCL_ERROR;
}
/* initialize Togl data structures values */
togl->Next = NULL;
#if defined(TOGL_WGL)
togl->tglGLHdc = NULL;
togl->tglGLHglrc = NULL;
togl->tglGLOverlayHglrc = NULL;
# ifdef STEREO_I_H
togl->pStereoI = NULL;
# endif
#elif defined(TOGL_X11)
togl->GlCtx = NULL;
togl->OverlayCtx = NULL;
#elif defined(TOGL_AGL)
togl->aglCtx = NULL;
#endif
togl->contextTag = 0;
togl->display = Tk_Display(tkwin);
togl->TkWin = tkwin;
togl->Interp = interp;
togl->VisInfo = NULL;
togl->OverlayWindow = None;
togl->OverlayCmap = None;
togl->OverlayTransparentPixel = 0;
togl->OverlayIsMapped = False;
togl->UpdatePending = False;
togl->OverlayUpdatePending = False;
togl->tpg = tpg;
togl->Client_Data = NULL;
/* for EPS Output */
togl->EpsRedMap = togl->EpsGreenMap = togl->EpsBlueMap = NULL;
togl->EpsMapSize = 0;
#ifndef NO_TK_CURSOR
togl->Cursor = None;
#endif
togl->Width = 0;
togl->Height = 0;
togl->SetGrid = 0;
togl->TimerInterval = 0;
togl->RgbaFlag = True;
togl->RgbaRed = 1;
togl->RgbaGreen = 1;
togl->RgbaBlue = 1;
togl->DoubleFlag = False;
togl->DepthFlag = False;
togl->DepthSize = 1;
togl->AccumFlag = False;
togl->AccumRed = 1;
togl->AccumGreen = 1;
togl->AccumBlue = 1;
togl->AccumAlpha = 1;
togl->AlphaFlag = False;
togl->AlphaSize = 1;
togl->StencilFlag = False;
togl->StencilSize = 1;
togl->OverlayFlag = False;
togl->Stereo = TOGL_STEREO_NONE;
togl->AuxNumber = 0;
togl->Indirect = False;
togl->PixelFormat = 0;
togl->SwapInterval = 1;
togl->CreateProc = NULL;
togl->DisplayProc = NULL;
togl->ReshapeProc = NULL;
togl->DestroyProc = NULL;
togl->TimerProc = NULL;
togl->timerHandler = NULL;
togl->OverlayDisplayProc = NULL;
togl->ShareList = NULL;
togl->ShareContext = NULL;
togl->Ident = NULL;
togl->PrivateCmapFlag = False;
togl->currentStereoBuffer = STEREO_BUFFER_NONE;
#ifdef HAVE_AUTOSTEREO
togl->as_initialized = False;
togl->ash = -1;
#endif
togl->badWindow = False;
/* Create command event handler */
togl->widgetCmd = Tcl_CreateObjCommand(interp, Tk_PathName(tkwin),
Togl_ObjWidget, (ClientData) togl, ToglCmdDeletedProc);
/*
* Setup the Tk_ClassProcs callbacks to point at our own window creation
* function
*
* We need to check at runtime if we should use the new Tk_SetClassProcs()
* API or if we need to modify the window structure directly
*/
#ifdef HAVE_TK_SETCLASSPROCS
if (SetClassProcsPtr != NULL) { /* use public API (Tk 8.4+) */
Tk_ClassProcs *procsPtr;
procsPtr = (Tk_ClassProcs *) ckalloc(sizeof (Tk_ClassProcs));
procsPtr->size = sizeof (Tk_ClassProcs);
procsPtr->createProc = Togl_CreateWindow;
procsPtr->worldChangedProc = Togl_WorldChanged;
procsPtr->modalProc = NULL;
/* Tk_SetClassProcs(togl->TkWin, procsPtr, (ClientData) togl); */
(SetClassProcsPtr) (togl->TkWin, procsPtr, (ClientData) togl);
} else
#endif
{ /* use private API */
/*
* We need to set these fields in the Tk_FakeWin structure: dummy17 =
* classProcsPtr dummy18 = instanceData */
TkClassProcs *procsPtr;
Tk_FakeWin *winPtr = (Tk_FakeWin *) (togl->TkWin);
procsPtr = (TkClassProcs *) ckalloc(sizeof (TkClassProcs));
procsPtr->createProc = Togl_CreateWindow;
procsPtr->geometryProc = Togl_WorldChanged;
procsPtr->modalProc = NULL;
winPtr->dummy17 = (char *) procsPtr;
winPtr->dummy18 = (ClientData) togl;
}
Tk_CreateEventHandler(tkwin, ExposureMask | StructureNotifyMask,
Togl_EventProc, (ClientData) togl);
/* Configure Togl widget */
if (Tk_InitOptions(interp, WIDGREC togl, tpg->optionTable, tkwin) != TCL_OK
|| Togl_ObjConfigure(interp, togl, objc - 2, objv + 2) != TCL_OK) {
goto error;
}
/*
* If OpenGL window wasn't already created by Togl_ObjConfigure() we
* create it now. We can tell by checking if the OpenGL context has
* been initialized.
*/
if (!
#if defined(TOGL_WGL)
togl->tglGLHdc
#elif defined(TOGL_X11)
togl->GlCtx
#elif defined(TOGL_AGL)
togl->aglCtx
#endif
) {
Tk_MakeWindowExist(togl->TkWin);
if (Tk_WindowId(togl->TkWin) == DUMMY_WINDOW) {
/* Make sure Tk does not try to delete dummy window by creating a
* non-OpenGL one (because Tk _really_ doesn't want
* Tk_MakeWindowExist to fail). */
togl->badWindow = True;
Tk_WindowId(togl->TkWin) = None;
Tk_MakeWindowExist(togl->TkWin);
goto error;
}
Togl_MakeCurrent(togl);
}
if (togl->contextTag == 0)
togl->contextTag = ++tpg->nextContextTag;
(void) Togl_SwapInterval(togl, togl->SwapInterval);
/* If defined, call create callback */
if (togl->CreateProc) {
if (Togl_CallCallback(togl, togl->CreateProc) != TCL_OK) {
goto error;
}
}
glViewport(0, 0, togl->Width, togl->Height);
#if defined(TOGL_X11)
if (togl->OverlayFlag) {
Togl_UseLayer(togl, TOGL_OVERLAY);
glViewport(0, 0, togl->Width, togl->Height);
Togl_UseLayer(togl, TOGL_NORMAL);
}
#endif
/* If defined, call reshape proc */
if (togl->ReshapeProc) {
if (Togl_CallCallback(togl, togl->ReshapeProc) != TCL_OK) {
goto error;
}
}
#if defined(STEREO_I_H)
if (togl->Stereo == TOGL_STEREO_NVIDIA_CON) {
/* check if driver is installed */
if (!togl->pStereoI && !CreateStereoI(&togl->pStereoI)) {
Tcl_AppendResult(interp,
NC3D " driver is not installed;"
" see <http://www.nvidia.com/>", NULL);
goto error;
}
if (togl->pStereoI->GetStereoState() == STEREO_STATE_DISABLED) {
Tcl_AppendResult(interp,
NC3D " is disabled."
" Please enable it in the driver control panel.", NULL);
goto error;
}
# if 0
/* check if full screen */
if (togl->Width != WidthOfScreen(Tk_Screen(togl->TkWin))
|| togl->Height != HeightOfScreen(Tk_Screen(togl->TkWin))) {
fprintf(stderr, " not fullscreen %d x %d should be %d x %d\n",
togl->Width, togl->Height,
WidthOfScreen(Tk_Screen(togl->TkWin)),
HeightOfScreen(Tk_Screen(togl->TkWin)));
Tcl_AppendResult(interp, NC3D " only works for fullscreen windows.",
NULL);
goto error;
}
# endif
/* Turn on stereo */
togl->pStereoI->SetStereoState(STEREO_STATE_ON);
}
#endif
Tcl_AppendResult(interp, Tk_PathName(tkwin), NULL);
/* Add to linked list */
AddToList(togl);
return TCL_OK;
error:
(void) Tcl_DeleteCommandFromToken(interp, togl->widgetCmd);
Tcl_AppendResult(interp, "\nCouldn't configure togl widget", NULL);
return TCL_ERROR;
}
#if TOGL_USE_OVERLAY
/*
* Do all the setup for overlay planes
* Return: TCL_OK or TCL_ERROR
*/
static int
SetupOverlay(Togl *togl)
{
# if defined(TOGL_X11)
# ifdef GLX_TRANSPARENT_TYPE_EXT
static int ovAttributeList[] = {
GLX_BUFFER_SIZE, 2,
GLX_LEVEL, 1,
GLX_TRANSPARENT_TYPE_EXT, GLX_TRANSPARENT_INDEX_EXT,
None
};
# else
static int ovAttributeList[] = {
GLX_BUFFER_SIZE, 2,
GLX_LEVEL, 1,
None
};
# endif
Display *dpy;
XVisualInfo *visinfo;
TkWindow *winPtr = (TkWindow *) togl->TkWin;
XSetWindowAttributes swa;
Tcl_HashEntry *hPtr;
int new_flag;
dpy = Tk_Display(togl->TkWin);
visinfo = glXChooseVisual(dpy, Tk_ScreenNumber(winPtr), ovAttributeList);
if (!visinfo) {
Tcl_AppendResult(togl->Interp, Tk_PathName(winPtr),
": No suitable overlay index visual available", NULL);
togl->OverlayCtx = 0;
togl->OverlayWindow = 0;
togl->OverlayCmap = 0;
return TCL_ERROR;
}
# ifdef GLX_TRANSPARENT_INDEX_EXT
{
int fail =
glXGetConfig(dpy, visinfo, GLX_TRANSPARENT_INDEX_VALUE_EXT,
&togl->OverlayTransparentPixel);
if (fail)
togl->OverlayTransparentPixel = 0; /* maybe, maybe ... */
}
# else
togl->OverlayTransparentPixel = 0; /* maybe, maybe ... */
# endif
/* share display lists with normal layer context */
togl->OverlayCtx =
glXCreateContext(dpy, visinfo, togl->GlCtx, !togl->Indirect);
swa.colormap = XCreateColormap(dpy, XRootWindow(dpy, visinfo->screen),
visinfo->visual, AllocNone);
togl->OverlayCmap = swa.colormap;
swa.border_pixel = 0;
swa.event_mask = ALL_EVENTS_MASK;
togl->OverlayWindow = XCreateWindow(dpy, Tk_WindowId(togl->TkWin), 0, 0,
togl->Width, togl->Height, 0,
visinfo->depth, InputOutput,
visinfo->visual, CWBorderPixel | CWColormap | CWEventMask, &swa);
hPtr = Tcl_CreateHashEntry(&winPtr->dispPtr->winTable,
(const char *) togl->OverlayWindow, &new_flag);
Tcl_SetHashValue(hPtr, winPtr);
/* XMapWindow( dpy, togl->OverlayWindow ); */
togl->OverlayIsMapped = False;
/* Make sure window manager installs our colormap */
XSetWMColormapWindows(dpy, togl->OverlayWindow, &togl->OverlayWindow, 1);
return TCL_OK;
# elif defined(TOGL_WGL) || defined(TOGL_AGL)
/* not yet implemented on these */
return TCL_ERROR;
# endif
}
#endif /* TOGL_USE_OVERLAY */
#ifdef TOGL_WGL
# define TOGL_CLASS_NAME "Togl Class"
static Bool ToglClassInitialized = False;
static LRESULT CALLBACK
Win32WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LONG result;
Togl *togl = (Togl *) GetWindowLongPtr(hwnd, 0);
WNDCLASS childClass;
switch (message) {
case WM_WINDOWPOSCHANGED:
/* Should be processed by DefWindowProc, otherwise a double buffered
* context is not properly resized when the corresponding window is
* resized. */
break;
case WM_DESTROY:
if (togl && togl->TkWin != NULL) {
if (togl->SetGrid > 0) {
Tk_UnsetGrid(togl->TkWin);
}
(void) Tcl_DeleteCommandFromToken(togl->Interp, togl->widgetCmd);
}
break;
case WM_ERASEBKGND:
/* We clear our own window */
return 1;
default:
# if USE_STATIC_LIB
return TkWinChildProc(hwnd, message, wParam, lParam);
# else
/*
* OK, since TkWinChildProc is not explicitly exported in the
* dynamic libraries, we have to retrieve it from the class info
* registered with windows.
*
*/
if (tkWinChildProc == NULL) {
GetClassInfo(Tk_GetHINSTANCE(), TK_WIN_CHILD_CLASS_NAME,
&childClass);
tkWinChildProc = childClass.lpfnWndProc;
}
return tkWinChildProc(hwnd, message, wParam, lParam);
# endif
}
result = DefWindowProc(hwnd, message, wParam, lParam);
Tcl_ServiceAll();
return result;
}
#endif /* TOGL_WGL */
/*
* Togl_CreateWindow
*
* Window creation function, invoked as a callback from Tk_MakeWindowExist.
* Creates an OpenGL window for the Togl widget.
*/
static Window
Togl_CreateWindow(Tk_Window tkwin, Window parent, ClientData instanceData)
{
Togl *togl = (Togl *) instanceData;
XVisualInfo *visinfo = NULL;
Display *dpy;
Colormap cmap;
int scrnum;
Window window;
#if defined(TOGL_X11)
Bool directCtx = True;
int attrib_list[1000];
int attrib_count;
int dummy;
XSetWindowAttributes swa;
# define MAX_ATTEMPTS 12
static int ci_depths[MAX_ATTEMPTS] = {
8, 4, 2, 1, 12, 16, 8, 4, 2, 1, 12, 16
};
static int dbl_flags[MAX_ATTEMPTS] = {
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1
};
#elif defined(TOGL_WGL)
HWND hwnd, parentWin;
int pixelformat;
HINSTANCE hInstance;
WNDCLASS ToglClass;
PIXELFORMATDESCRIPTOR pfd;
#elif defined(TOGL_AGL)
GLint attribs[20];
int na;
AGLPixelFormat fmt;
#endif /* TOGL_X11 */
if (togl->badWindow) {
TkWindow *winPtr = (TkWindow *) tkwin;
window = TkpMakeWindow(winPtr, parent);
return window;
}
dpy = Tk_Display(tkwin);
#if defined(TOGL_X11)
/* Make sure OpenGL's GLX extension supported */
if (!glXQueryExtension(dpy, &dummy, &dummy)) {
Tcl_SetResult(togl->Interp,
TCL_STUPID "X server has no OpenGL GLX extension", TCL_STATIC);
return DUMMY_WINDOW;
}
if (togl->ShareContext && FindTogl(togl, togl->ShareContext)) {
/* share OpenGL context with existing Togl widget */
Togl *shareWith = FindTogl(togl, togl->ShareContext);
assert(shareWith != NULL);
assert(shareWith->GlCtx != NULL);
togl->GlCtx = shareWith->GlCtx;
togl->contextTag = shareWith->contextTag;
togl->VisInfo = shareWith->VisInfo;
visinfo = togl->VisInfo;
} else {
if (togl->PixelFormat) {
XVisualInfo template;
int count = 1;
int stereo = 0;
template.visualid = togl->PixelFormat;
visinfo = XGetVisualInfo(dpy, VisualIDMask, &template, &count);
if (visinfo == NULL) {
Tcl_SetResult(togl->Interp,
TCL_STUPID "couldn't choose pixel format", TCL_STATIC);
return DUMMY_WINDOW;
}
/* fill in flags normally passed in that affect behavior */
(void) glXGetConfig(dpy, visinfo, GLX_RGBA, &togl->RgbaFlag);
(void) glXGetConfig(dpy, visinfo, GLX_DOUBLEBUFFER,
&togl->DoubleFlag);
(void) glXGetConfig(dpy, visinfo, GLX_STEREO, &stereo);
if (stereo)
togl->Stereo = TOGL_STEREO_NATIVE;
else
togl->Stereo = TOGL_STEREO_NONE;
} else {
int attempt;
/* It may take a few tries to get a visual */
for (attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
attrib_count = 0;
attrib_list[attrib_count++] = GLX_USE_GL;
if (togl->RgbaFlag) {
/* RGB[A] mode */
attrib_list[attrib_count++] = GLX_RGBA;
attrib_list[attrib_count++] = GLX_RED_SIZE;
attrib_list[attrib_count++] = togl->RgbaRed;
attrib_list[attrib_count++] = GLX_GREEN_SIZE;
attrib_list[attrib_count++] = togl->RgbaGreen;
attrib_list[attrib_count++] = GLX_BLUE_SIZE;
attrib_list[attrib_count++] = togl->RgbaBlue;
if (togl->AlphaFlag) {
attrib_list[attrib_count++] = GLX_ALPHA_SIZE;
attrib_list[attrib_count++] = togl->AlphaSize;
}
/* for EPS Output */
if (togl->EpsRedMap)
free(togl->EpsRedMap);
if (togl->EpsGreenMap)
free(togl->EpsGreenMap);
if (togl->EpsBlueMap)
free(togl->EpsBlueMap);
togl->EpsRedMap = togl->EpsGreenMap = togl->EpsBlueMap =
NULL;
togl->EpsMapSize = 0;
} else {
/* Color index mode */
int depth;
attrib_list[attrib_count++] = GLX_BUFFER_SIZE;
depth = ci_depths[attempt];
attrib_list[attrib_count++] = depth;
}
if (togl->DepthFlag) {
attrib_list[attrib_count++] = GLX_DEPTH_SIZE;
attrib_list[attrib_count++] = togl->DepthSize;
}
if (togl->DoubleFlag || dbl_flags[attempt]) {
attrib_list[attrib_count++] = GLX_DOUBLEBUFFER;
}
if (togl->StencilFlag) {
attrib_list[attrib_count++] = GLX_STENCIL_SIZE;
attrib_list[attrib_count++] = togl->StencilSize;
}
if (togl->AccumFlag) {
attrib_list[attrib_count++] = GLX_ACCUM_RED_SIZE;
attrib_list[attrib_count++] = togl->AccumRed;
attrib_list[attrib_count++] = GLX_ACCUM_GREEN_SIZE;
attrib_list[attrib_count++] = togl->AccumGreen;
attrib_list[attrib_count++] = GLX_ACCUM_BLUE_SIZE;
attrib_list[attrib_count++] = togl->AccumBlue;
if (togl->AlphaFlag) {
attrib_list[attrib_count++] = GLX_ACCUM_ALPHA_SIZE;
attrib_list[attrib_count++] = togl->AccumAlpha;
}
}
if (togl->AuxNumber != 0) {
attrib_list[attrib_count++] = GLX_AUX_BUFFERS;
attrib_list[attrib_count++] = togl->AuxNumber;
}
if (togl->Indirect) {
directCtx = False;
}
if (togl->Stereo == TOGL_STEREO_NATIVE) {
attrib_list[attrib_count++] = GLX_STEREO;
}
attrib_list[attrib_count++] = None;
visinfo = glXChooseVisual(dpy, Tk_ScreenNumber(tkwin),
attrib_list);
if (visinfo) {
/* found a GLX visual! */
break;
}
}
togl->VisInfo = visinfo;
if (visinfo == NULL) {
Tcl_SetResult(togl->Interp,
TCL_STUPID "couldn't choose pixel format", TCL_STATIC);
return DUMMY_WINDOW;
}
}
/*
* Create a new OpenGL rendering context.
*/
if (togl->ShareList) {
/* share display lists with existing togl widget */
Togl *shareWith = FindTogl(togl, togl->ShareList);
GLXContext shareCtx;
if (shareWith) {
shareCtx = shareWith->GlCtx;
togl->contextTag = shareWith->contextTag;
} else {
shareCtx = None;
}
togl->GlCtx = glXCreateContext(dpy, visinfo, shareCtx, directCtx);
} else {
/* don't share display lists */
togl->GlCtx = glXCreateContext(dpy, visinfo, None, directCtx);
}
if (togl->GlCtx == NULL) {
Tcl_SetResult(togl->Interp,
TCL_STUPID "could not create rendering context",
TCL_STATIC);
return DUMMY_WINDOW;
}
}
#endif /* TOGL_X11 */
#ifdef TOGL_WGL
parentWin = Tk_GetHWND(parent);
hInstance = Tk_GetHINSTANCE();
if (!ToglClassInitialized) {
ToglClassInitialized = True;
ToglClass.style = CS_HREDRAW | CS_VREDRAW;
ToglClass.cbClsExtra = 0;
ToglClass.cbWndExtra = sizeof (LONG_PTR); /* to save Togl* */
ToglClass.hInstance = hInstance;
ToglClass.hbrBackground = NULL;
ToglClass.lpszMenuName = NULL;
ToglClass.lpszClassName = TOGL_CLASS_NAME;
ToglClass.lpfnWndProc = Win32WinProc;
ToglClass.hIcon = NULL;
ToglClass.hCursor = NULL;
if (!RegisterClass(&ToglClass)) {
Tcl_SetResult(togl->Interp,
TCL_STUPID "unable register Togl window class", TCL_STATIC);
return DUMMY_WINDOW;
}
}
hwnd = CreateWindow(TOGL_CLASS_NAME, NULL,
WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0,
togl->Width, togl->Height, parentWin, NULL, hInstance, NULL);
SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
togl->tglGLHdc = GetDC(hwnd);
pfd.nSize = sizeof (PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags =
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_SUPPORT_COMPOSITION;
if (togl->DoubleFlag) {
pfd.dwFlags |= PFD_DOUBLEBUFFER;
}
/* The stereo flag is not supported in the generic OpenGL implementation,
* but may be supported by specific hardware devices. */
if (togl->Stereo == TOGL_STEREO_NATIVE) {
pfd.dwFlags |= PFD_STEREO;
}
if (togl->PixelFormat) {
pixelformat = togl->PixelFormat;
} else {
pfd.cColorBits = togl->RgbaRed + togl->RgbaGreen + togl->RgbaBlue;
pfd.iPixelType = togl->RgbaFlag ? PFD_TYPE_RGBA : PFD_TYPE_COLORINDEX;
/* Alpha bitplanes are not supported in the current generic OpenGL
* implementation, but may be supported by specific hardware devices. */
pfd.cAlphaBits = togl->AlphaFlag ? togl->AlphaSize : 0;
pfd.cAccumBits = togl->AccumFlag ? (togl->AccumRed + togl->AccumGreen +
togl->AccumBlue + togl->AccumAlpha) : 0;
pfd.cDepthBits = togl->DepthFlag ? togl->DepthSize : 0;
pfd.cStencilBits = togl->StencilFlag ? togl->StencilSize : 0;
/* Auxiliary buffers are not supported in the current generic OpenGL
* implementation, but may be supported by specific hardware devices. */
pfd.cAuxBuffers = togl->AuxNumber;
pfd.iLayerType = PFD_MAIN_PLANE;
if ((pixelformat = ChoosePixelFormat(togl->tglGLHdc, &pfd)) == 0) {
Tcl_SetResult(togl->Interp,
TCL_STUPID "couldn't choose pixel format", TCL_STATIC);
ReleaseDC(hwnd, togl->tglGLHdc);
togl->tglGLHdc = NULL;
DestroyWindow(hwnd);
return DUMMY_WINDOW;
}
}
if (SetPixelFormat(togl->tglGLHdc, pixelformat, &pfd) == FALSE) {
Tcl_SetResult(togl->Interp,
TCL_STUPID "couldn't choose pixel format", TCL_STATIC);
ReleaseDC(hwnd, togl->tglGLHdc);
togl->tglGLHdc = NULL;
DestroyWindow(hwnd);
return DUMMY_WINDOW;
}
/* Get the actual pixel format */
DescribePixelFormat(togl->tglGLHdc, pixelformat, sizeof (pfd), &pfd);
if (togl->PixelFormat) {
/* fill in flags normally passed in that affect behavior */
togl->RgbaFlag = pfd.iPixelType == PFD_TYPE_RGBA;
togl->DoubleFlag = pfd.cDepthBits > 0;
if ((pfd.dwFlags & PFD_STEREO) != 0)
togl->Stereo = TOGL_STEREO_NATIVE;
else
togl->Stereo = TOGL_STEREO_NONE;
/* TODO: set depth flag, and more */
} else if (togl->Stereo == TOGL_STEREO_NATIVE
&& (pfd.dwFlags & PFD_STEREO) == 0) {
Tcl_SetResult(togl->Interp,
TCL_STUPID "couldn't choose stereo pixel format", TCL_STATIC);
ReleaseDC(hwnd, togl->tglGLHdc);
togl->tglGLHdc = NULL;
DestroyWindow(hwnd);
return DUMMY_WINDOW;
}
if (togl->ShareContext && FindTogl(togl, togl->ShareContext)) {
/* share OpenGL context with existing Togl widget */
Togl *shareWith = FindTogl(togl, togl->ShareContext);
assert(shareWith);
assert(shareWith->tglGLHglrc);
togl->tglGLHglrc = shareWith->tglGLHglrc;
togl->contextTag = shareWith->contextTag;
togl->VisInfo = shareWith->VisInfo;
visinfo = togl->VisInfo;
} else {
/*
* Create a new OpenGL rendering context. And check to share lists.
*/
togl->tglGLHglrc = wglCreateContext(togl->tglGLHdc);
if (togl->ShareList) {
/* share display lists with existing togl widget */
Togl *shareWith = FindTogl(togl, togl->ShareList);
if (shareWith) {
if (!wglShareLists(shareWith->tglGLHglrc, togl->tglGLHglrc)) {
# if 0
LPVOID lpMsgBuf;
DWORD err = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf, 0, NULL);
fprintf(stderr, "unable to share display lists: %d: %s\n",
err, lpMsgBuf);
LocalFree(lpMsgBuf);
# endif
Tcl_SetResult(togl->Interp,
TCL_STUPID "unable to share display lists",
TCL_STATIC);
ReleaseDC(hwnd, togl->tglGLHdc);
togl->tglGLHdc = NULL;
DestroyWindow(hwnd);
return DUMMY_WINDOW;
}
togl->contextTag = shareWith->contextTag;
}
}
if (!togl->tglGLHglrc) {
Tcl_SetResult(togl->Interp,
TCL_STUPID "could not create rendering context",
TCL_STATIC);
ReleaseDC(hwnd, togl->tglGLHdc);
togl->tglGLHdc = NULL;
DestroyWindow(hwnd);
return DUMMY_WINDOW;
}
/* Just for portability, define the simplest visinfo */
visinfo = (XVisualInfo *) malloc(sizeof (XVisualInfo));
visinfo->visual = DefaultVisual(dpy, DefaultScreen(dpy));
visinfo->depth = visinfo->visual->bits_per_rgb;
togl->VisInfo = visinfo;
}
SetWindowLongPtr(hwnd, 0, (LONG_PTR) togl);
#endif /* TOGL_WGL */
/* for TOGL_AGL, we create the window first, then choose the pixel format */
/*
* find a colormap
*/
scrnum = Tk_ScreenNumber(tkwin);
if (togl->RgbaFlag) {
/* Colormap for RGB mode */
#if defined(TOGL_X11)
cmap = get_rgb_colormap(dpy, scrnum, visinfo, tkwin);
#elif defined(TOGL_WGL)
if (pfd.dwFlags & PFD_NEED_PALETTE) {
cmap = Win32CreateRgbColormap(pfd);
} else {
cmap = DefaultColormap(dpy, scrnum);
}
/* for EPS Output */
if (togl->EpsRedMap)
free(togl->EpsRedMap);
if (togl->EpsGreenMap)
free(togl->EpsGreenMap);
if (togl->EpsBlueMap)
free(togl->EpsBlueMap);
togl->EpsRedMap = togl->EpsGreenMap = togl->EpsBlueMap = NULL;
togl->EpsMapSize = 0;
#elif defined(TOGL_AGL)
cmap = DefaultColormap(dpy, scrnum);
/* for EPS Output */
if (togl->EpsRedMap)
free(togl->EpsRedMap);
if (togl->EpsGreenMap)
free(togl->EpsGreenMap);
if (togl->EpsBlueMap)
free(togl->EpsBlueMap);
togl->EpsRedMap = togl->EpsGreenMap = togl->EpsBlueMap = NULL;
togl->EpsMapSize = 0;
#endif
} else {
/* Colormap for CI mode */
#ifdef TOGL_WGL
/* this logic is to overcome a combination driver/compiler bug: 1.
* cColorBits may be unusally large (e.g., 32 instead of 8 or 12) 2. 1
* << 32 might be 1 instead of zero (gcc for ia32) */
if (pfd.cColorBits >= MAX_CI_COLORMAP_BITS) {
togl->CiColormapSize = MAX_CI_COLORMAP_SIZE;
} else {
togl->CiColormapSize = 1 << pfd.cColorBits;
if (togl->CiColormapSize >= MAX_CI_COLORMAP_SIZE)
togl->CiColormapSize = MAX_CI_COLORMAP_SIZE;
}
#endif
if (togl->PrivateCmapFlag) {
/* need read/write colormap so user can store own color entries */
#if defined(TOGL_X11)
cmap = XCreateColormap(dpy, XRootWindow(dpy, visinfo->screen),
visinfo->visual, AllocAll);
#elif defined(TOGL_WGL)
cmap = Win32CreateCiColormap(togl);
#elif defined(TOGL_AGL)
/* need to figure out how to do this correctly on Mac... */
cmap = DefaultColormap(dpy, scrnum);
#endif
} else {
if (visinfo->visual == DefaultVisual(dpy, scrnum)) {
/* share default/root colormap */
cmap = Tk_Colormap(tkwin);
} else {
/* make a new read-only colormap */
cmap = XCreateColormap(dpy, XRootWindow(dpy, visinfo->screen),
visinfo->visual, AllocNone);
}
}
}
#if !defined(TOGL_AGL)
/* Make sure Tk knows to switch to the new colormap when the cursor is over
* this window when running in color index mode. */
(void) Tk_SetWindowVisual(tkwin, visinfo->visual, visinfo->depth, cmap);
#endif
#ifdef TOGL_WGL
/* Install the colormap */
SelectPalette(togl->tglGLHdc, ((TkWinColormap *) cmap)->palette, TRUE);
RealizePalette(togl->tglGLHdc);
#endif
#if defined(TOGL_X11)
swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = ALL_EVENTS_MASK;
window = XCreateWindow(dpy, parent,
0, 0, togl->Width, togl->Height,
0, visinfo->depth,
InputOutput, visinfo->visual,
CWBorderPixel | CWColormap | CWEventMask, &swa);
/* Make sure window manager installs our colormap */
(void) XSetWMColormapWindows(dpy, window, &window, 1);
#elif defined(TOGL_WGL)
window = Tk_AttachHWND(tkwin, hwnd);
#elif defined(TOGL_AGL)
{
TkWindow *winPtr = (TkWindow *) tkwin;
window = TkpMakeWindow(winPtr, parent);
}
#endif
#if TOGL_USE_OVERLAY
if (togl->OverlayFlag) {
if (SetupOverlay(togl) == TCL_ERROR) {
fprintf(stderr, "Warning: couldn't setup overlay.\n");
togl->OverlayFlag = False;
}
}
#endif
/* Request the X window to be displayed */
(void) XMapWindow(dpy, window);
#if defined(TOGL_AGL)
if (togl->ShareContext && FindTogl(togl, togl->ShareContext)) {
/* share OpenGL context with existing Togl widget */
Togl *shareWith = FindTogl(togl, togl->ShareContext);
assert(shareWith);
assert(shareWith->aglCtx);
togl->aglCtx = shareWith->aglCtx;
togl->contextTag = shareWith->contextTag;
togl->VisInfo = shareWith->VisInfo;
visinfo = togl->VisInfo;
} else {
AGLContext shareCtx = NULL;
if (togl->PixelFormat) {
/* fill in RgbaFlag, DoubleFlag, and Stereo */
fmt = (AGLPixelFormat) togl->PixelFormat;
GLint has_rgba, has_doublebuf, has_stereo;
if (aglDescribePixelFormat(fmt, AGL_RGBA, &has_rgba)
&& aglDescribePixelFormat(fmt, AGL_DOUBLEBUFFER,
&has_doublebuf)
&& aglDescribePixelFormat(fmt, AGL_STEREO, &has_stereo)) {
togl->RgbaFlag = (has_rgba ? True : False);
togl->DoubleFlag = (has_doublebuf ? True : False);
togl->Stereo =
(has_stereo ? TOGL_STEREO_NATIVE : TOGL_STEREO_NONE);
} else {
Tcl_SetResult(togl->Interp,
TCL_STUPID "failed querying pixel format attributes",
TCL_STATIC);
return DUMMY_WINDOW;
}
} else {
/* Need to do this after mapping window, so MacDrawable structure
* is more completely filled in */
na = 0;
attribs[na++] = AGL_MINIMUM_POLICY;
/* ask for hardware-accelerated onscreen */
attribs[na++] = AGL_ACCELERATED;
attribs[na++] = AGL_NO_RECOVERY;
if (togl->RgbaFlag) {
/* RGB[A] mode */
attribs[na++] = AGL_RGBA;
attribs[na++] = AGL_RED_SIZE;
attribs[na++] = togl->RgbaRed;
attribs[na++] = AGL_GREEN_SIZE;
attribs[na++] = togl->RgbaGreen;
attribs[na++] = AGL_BLUE_SIZE;
attribs[na++] = togl->RgbaBlue;
if (togl->AlphaFlag) {
attribs[na++] = AGL_ALPHA_SIZE;
attribs[na++] = togl->AlphaSize;
}
} else {
/* Color index mode */
attribs[na++] = AGL_BUFFER_SIZE;
attribs[na++] = 8;
}
if (togl->DepthFlag) {
attribs[na++] = AGL_DEPTH_SIZE;
attribs[na++] = togl->DepthSize;
}
if (togl->DoubleFlag) {
attribs[na++] = AGL_DOUBLEBUFFER;
}
if (togl->StencilFlag) {
attribs[na++] = AGL_STENCIL_SIZE;
attribs[na++] = togl->StencilSize;
}
if (togl->AccumFlag) {
attribs[na++] = AGL_ACCUM_RED_SIZE;
attribs[na++] = togl->AccumRed;
attribs[na++] = AGL_ACCUM_GREEN_SIZE;
attribs[na++] = togl->AccumGreen;
attribs[na++] = AGL_ACCUM_BLUE_SIZE;
attribs[na++] = togl->AccumBlue;
if (togl->AlphaFlag) {
attribs[na++] = AGL_ACCUM_ALPHA_SIZE;
attribs[na++] = togl->AccumAlpha;
}
}
if (togl->AuxNumber != 0) {
attribs[na++] = AGL_AUX_BUFFERS;
attribs[na++] = togl->AuxNumber;
}
if (togl->Stereo == TOGL_STEREO_NATIVE) {
attribs[na++] = AGL_STEREO;
}
attribs[na++] = AGL_NONE;
if ((fmt = aglChoosePixelFormat(NULL, 0, attribs)) == NULL) {
Tcl_SetResult(togl->Interp,
TCL_STUPID "couldn't choose pixel format", TCL_STATIC);
return DUMMY_WINDOW;
}
}
/*
* Check whether to share lists.
*/
if (togl->ShareList) {
/* share display lists with existing togl widget */
Togl *shareWith = FindTogl(togl, togl->ShareList);
if (shareWith) {
shareCtx = shareWith->aglCtx;
togl->contextTag = shareWith->contextTag;
}
}
if ((togl->aglCtx = aglCreateContext(fmt, shareCtx)) == NULL) {
GLenum err = aglGetError();
aglDestroyPixelFormat(fmt);
if (err == AGL_BAD_MATCH)
Tcl_SetResult(togl->Interp,
TCL_STUPID "unable to share display lists"
": shared context doesn't match", TCL_STATIC);
else if (err == AGL_BAD_CONTEXT)
Tcl_SetResult(togl->Interp,
TCL_STUPID "unable to share display lists"
": bad shared context", TCL_STATIC);
else if (err == AGL_BAD_PIXELFMT)
Tcl_SetResult(togl->Interp,
TCL_STUPID "could not create rendering context"
": bad pixel format", TCL_STATIC);
else
Tcl_SetResult(togl->Interp,
TCL_STUPID "could not create rendering context"
": unknown reason", TCL_STATIC);
return DUMMY_WINDOW;
}
aglDestroyPixelFormat(fmt);
if (!aglSetDrawable(togl->aglCtx,
((MacDrawable *) (window))->toplevel->grafPtr)) {
aglDestroyContext(togl->aglCtx);
Tcl_SetResult(togl->Interp,
TCL_STUPID "couldn't set drawable", TCL_STATIC);
return DUMMY_WINDOW;
}
/* Just for portability, define the simplest visinfo */
visinfo = (XVisualInfo *) malloc(sizeof (XVisualInfo));
visinfo->visual = DefaultVisual(dpy, DefaultScreen(dpy));
visinfo->depth = visinfo->visual->bits_per_rgb;
Tk_SetWindowVisual(tkwin, visinfo->visual, visinfo->depth, cmap);
}
#endif /* TOGL_AGL */
#if defined(TOGL_X11)
/* Check for a single/double buffering snafu */
{
int dbl_flag;
if (glXGetConfig(dpy, visinfo, GLX_DOUBLEBUFFER, &dbl_flag)) {
if (!togl->DoubleFlag && dbl_flag) {
/* We requested single buffering but had to accept a */
/* double buffered visual. Set the GL draw buffer to */
/* be the front buffer to simulate single buffering. */
glDrawBuffer(GL_FRONT);
}
}
}
#endif
/* for EPS Output */
if (!togl->RgbaFlag) {
int index_size;
#if defined(TOGL_X11) || defined(TOGL_AGL)
GLint index_bits;
glGetIntegerv(GL_INDEX_BITS, &index_bits);
index_size = 1 << index_bits;
#elif defined(TOGL_WGL)
index_size = togl->CiColormapSize;
#endif /* TOGL_X11 */
if (togl->EpsMapSize != index_size) {
if (togl->EpsRedMap)
free(togl->EpsRedMap);
if (togl->EpsGreenMap)
free(togl->EpsGreenMap);
if (togl->EpsBlueMap)
free(togl->EpsBlueMap);
togl->EpsMapSize = index_size;
togl->EpsRedMap = (GLfloat *) calloc(index_size, sizeof (GLfloat));
togl->EpsGreenMap =
(GLfloat *) calloc(index_size, sizeof (GLfloat));
togl->EpsBlueMap = (GLfloat *) calloc(index_size, sizeof (GLfloat));
}
}
#ifdef HAVE_AUTOSTEREO
if (togl->Stereo == TOGL_STEREO_NATIVE) {
if (!togl->as_initialized) {
const char *autostereod;
togl->as_initialized = True;
if ((autostereod = getenv("AUTOSTEREOD")) == NULL)
autostereod = AUTOSTEREOD;
if (autostereod && *autostereod) {
if (ASInitialize(togl->display, autostereod) == Success) {
togl->ash = ASCreatedStereoWindow(dpy);
}
}
} else {
togl->ash = ASCreatedStereoWindow(dpy);
}
}
#endif
return window;
}
/*
* Togl_WorldChanged
*
* Add support for setgrid option.
*/
static void
Togl_WorldChanged(ClientData instanceData)
{
Togl *togl = (Togl *) instanceData;
Tk_GeometryRequest(togl->TkWin, togl->Width, togl->Height);
Tk_SetInternalBorder(togl->TkWin, 0);
if (togl->SetGrid > 0) {
Tk_SetGrid(togl->TkWin, togl->Width / togl->SetGrid,
togl->Height / togl->SetGrid, togl->SetGrid, togl->SetGrid);
} else {
Tk_UnsetGrid(togl->TkWin);
}
}
/*
* ToglFree
*
* Wrap the ckfree macro.
*/
static void
ToglFree(char *clientData)
{
ckfree(clientData);
}
/*
* ToglCmdDeletedProc
*
* This procedure is invoked when a widget command is deleted. If
* the widget isn't already in the process of being destroyed,
* this command destroys it.
*
* Results:
* None.
*
* Side effects:
* The widget is destroyed.
*
*----------------------------------------------------------------------
*/
static void
ToglCmdDeletedProc(ClientData clientData)
{
Togl *togl = (Togl *) clientData;
Tk_Window tkwin = togl->TkWin;
/*
* This procedure could be invoked either because the window was
* destroyed and the command was then deleted (in which case tkwin
* is NULL) or because the command was deleted, and then this procedure
* destroys the widget.
*/
if (tkwin) {
Tk_DeleteEventHandler(tkwin, ExposureMask | StructureNotifyMask,
Togl_EventProc, (ClientData) togl);
}
Tk_Preserve((ClientData) togl);
Tcl_EventuallyFree((ClientData) togl, ToglFree);
Togl_LeaveStereo(togl, togl->Stereo);
if (togl->DestroyProc) {
/* call user's cleanup code */
Togl_CallCallback(togl, togl->DestroyProc);
}
if (togl->TimerProc != NULL) {
Tcl_DeleteTimerHandler(togl->timerHandler);
togl->timerHandler = NULL;
}
if (togl->UpdatePending) {
Tcl_CancelIdleCall(Togl_Render, (ClientData) togl);
togl->UpdatePending = False;
}
#ifndef NO_TK_CURSOR
if (togl->Cursor != None) {
Tk_FreeCursor(togl->display, togl->Cursor);
togl->Cursor = None;
}
#endif
/* remove from linked list */
RemoveFromList(togl);
togl->TkWin = NULL;
if (tkwin != NULL && Tk_WindowId(tkwin) != DUMMY_WINDOW) {
#if defined(TOGL_X11)
if (togl->GlCtx) {
if (FindToglWithSameContext(togl) == NULL)
glXDestroyContext(togl->display, togl->GlCtx);
togl->GlCtx = NULL;
}
# if TOGL_USE_OVERLAY
if (togl->OverlayCtx) {
Tcl_HashEntry *entryPtr;
TkWindow *winPtr = (TkWindow *) tkwin;
if (winPtr) {
entryPtr = Tcl_FindHashEntry(&winPtr->dispPtr->winTable,
(const char *) togl->OverlayWindow);
Tcl_DeleteHashEntry(entryPtr);
}
if (FindToglWithSameOverlayContext(togl) == NULL)
glXDestroyContext(togl->display, togl->OverlayCtx);
togl->OverlayCtx = NULL;
}
# endif /* TOGL_USE_OVERLAY */
#endif
#if defined(TOGL_WGL)
if (togl->tglGLHglrc) {
if (FindToglWithSameContext(togl) == NULL)
wglDeleteContext(togl->tglGLHglrc);
togl->tglGLHglrc = NULL;
}
if (tkwin && togl->tglGLHdc) {
HWND hwnd = Tk_GetHWND(Tk_WindowId(tkwin));
ReleaseDC(hwnd, togl->tglGLHdc);
togl->tglGLHdc = NULL;
}
#endif
/* TODO: delete contexts on other platforms */
if (togl->SetGrid > 0) {
Tk_UnsetGrid(tkwin);
}
Tk_DestroyWindow(tkwin);
}
Tk_Release((ClientData) togl);
}
/*
* This gets called to handle Togl window configuration events
*/
static void
Togl_EventProc(ClientData clientData, XEvent *eventPtr)
{
Togl *togl = (Togl *) clientData;
switch (eventPtr->type) {
case Expose:
if (eventPtr->xexpose.count == 0) {
if (!togl->UpdatePending
&& eventPtr->xexpose.window == Tk_WindowId(togl->TkWin)) {
Togl_PostRedisplay(togl);
}
#if defined(TOGL_X11)
if (!togl->OverlayUpdatePending && togl->OverlayFlag
&& togl->OverlayIsMapped
&& eventPtr->xexpose.window == togl->OverlayWindow) {
Togl_PostOverlayRedisplay(togl);
}
#endif
}
break;
case ConfigureNotify:
if (togl->Width != Tk_Width(togl->TkWin)
|| togl->Height != Tk_Height(togl->TkWin)) {
togl->Width = Tk_Width(togl->TkWin);
togl->Height = Tk_Height(togl->TkWin);
(void) XResizeWindow(Tk_Display(togl->TkWin),
Tk_WindowId(togl->TkWin), togl->Width, togl->Height);
#if defined(TOGL_X11)
if (togl->OverlayFlag) {
(void) XResizeWindow(Tk_Display(togl->TkWin),
togl->OverlayWindow, togl->Width, togl->Height);
(void) XRaiseWindow(Tk_Display(togl->TkWin),
togl->OverlayWindow);
}
#endif
Togl_MakeCurrent(togl);
glViewport(0, 0, togl->Width, togl->Height);
#if defined(TOGL_X11)
if (togl->OverlayFlag) {
Togl_UseLayer(togl, TOGL_OVERLAY);
glViewport(0, 0, togl->Width, togl->Height);
Togl_UseLayer(togl, TOGL_NORMAL);
}
#endif
if (togl->ReshapeProc) {
Togl_CallCallback(togl, togl->ReshapeProc);
}
#ifndef TOGL_WGL /* causes double redisplay on Win32 platform */
Togl_PostRedisplay(togl);
#endif
}
break;
case MapNotify:
#if defined(TOGL_AGL)
{
/*
* See comment for the UnmapNotify case below.
*/
AGLDrawable d = TkMacOSXGetDrawablePort(Tk_WindowId(togl->TkWin));
aglSetDrawable(togl->aglCtx, d);
}
#endif
break;
case UnmapNotify:
#if defined(TOGL_AGL)
{
/*
* For Mac OS X Aqua, Tk subwindows are not implemented as
* separate Aqua windows. They are just different regions of
* a single Aqua window. To unmap them they are just not drawn.
* Have to disconnect the AGL context otherwise they will continue
* to be displayed directly by Aqua.
*/
aglSetDrawable(togl->aglCtx, NULL);
}
#endif
break;
case DestroyNotify:
if (togl->TkWin != NULL) {
if (togl->SetGrid > 0) {
Tk_UnsetGrid(togl->TkWin);
}
(void) Tcl_DeleteCommandFromToken(togl->Interp, togl->widgetCmd);
}
break;
default:
/* nothing */
;
}
}
void
Togl_PostRedisplay(Togl *togl)
{
if (!togl->UpdatePending) {
togl->UpdatePending = True;
Tk_DoWhenIdle(Togl_Render, (ClientData) togl);
}
}
Bool
Togl_UpdatePending(const Togl *togl)
{
return togl->UpdatePending;
}
void
Togl_SwapBuffers(const Togl *togl)
{
if (togl->DoubleFlag) {
#if defined(TOGL_WGL)
int res = SwapBuffers(togl->tglGLHdc);
if (!res) {
ErrorExit(TEXT("SwapBuffers"));
}
#elif defined(TOGL_X11)
glXSwapBuffers(Tk_Display(togl->TkWin), Tk_WindowId(togl->TkWin));
#elif defined(TOGL_AGL)
aglSwapBuffers(togl->aglCtx);
#endif
} else {
glFlush();
}
}
const char *
Togl_Ident(const Togl *togl)
{
return togl->Ident;
}
int
Togl_Width(const Togl *togl)
{
return togl->Width;
}
int
Togl_Height(const Togl *togl)
{
return togl->Height;
}
Tcl_Interp *
Togl_Interp(const Togl *togl)
{
return togl->Interp;
}
Tk_Window
Togl_TkWin(const Togl *togl)
{
return togl->TkWin;
}
const char *
Togl_CommandName(const Togl *togl)
{
return Tcl_GetCommandName(togl->Interp, togl->widgetCmd);
}
int
Togl_ContextTag(const Togl *togl)
{
return togl->contextTag;
}
#if defined(TOGL_X11)
/*
* A replacement for XAllocColor. This function should never
* fail to allocate a color. When XAllocColor fails, we return
* the nearest matching color. If we have to allocate many colors
* this function isn't too efficient; the XQueryColors() could be
* done just once.
* Written by Michael Pichler, Brian Paul, Mark Kilgard
* Input: dpy - X display
* cmap - X colormap
* cmapSize - size of colormap
* In/Out: color - the XColor struct
* Output: exact - 1=exact color match, 0=closest match
*/
static void
noFaultXAllocColor(Display *dpy, Colormap cmap, int cmapSize,
XColor *color, int *exact)
{
XColor *ctable, subColor;
int i, bestmatch;
double mindist; /* 3*2^16^2 exceeds long int precision. */
/* First try just using XAllocColor. */
if (XAllocColor(dpy, cmap, color)) {
*exact = 1;
return;
}
/* Retrieve color table entries. */
/* XXX alloca candidate. */
ctable = (XColor *) ckalloc(cmapSize * sizeof (XColor));
for (i = 0; i < cmapSize; i++) {
ctable[i].pixel = i;
}
(void) XQueryColors(dpy, cmap, ctable, cmapSize);
/* Find best match. */
bestmatch = -1;
mindist = 0;
for (i = 0; i < cmapSize; i++) {
double dr = (double) color->red - (double) ctable[i].red;
double dg = (double) color->green - (double) ctable[i].green;
double db = (double) color->blue - (double) ctable[i].blue;
double dist = dr * dr + dg * dg + db * db;
if (bestmatch < 0 || dist < mindist) {
bestmatch = i;
mindist = dist;
}
}
/* Return result. */
subColor.red = ctable[bestmatch].red;
subColor.green = ctable[bestmatch].green;
subColor.blue = ctable[bestmatch].blue;
ckfree((char *) ctable);
/* Try to allocate the closest match color. This should only fail if the
* cell is read/write. Otherwise, we're incrementing the cell's reference
* count. */
if (!XAllocColor(dpy, cmap, &subColor)) {
/* do this to work around a problem reported by Frank Ortega */
subColor.pixel = (unsigned long) bestmatch;
subColor.red = ctable[bestmatch].red;
subColor.green = ctable[bestmatch].green;
subColor.blue = ctable[bestmatch].blue;
subColor.flags = DoRed | DoGreen | DoBlue;
}
*color = subColor;
}
#elif defined(TOGL_WGL)
static UINT
Win32AllocColor(const Togl *togl, float red, float green, float blue)
{
/* Modified version of XAllocColor emulation of Tk. - returns index,
* instead of color itself - allocates logical palette entry even for
* non-palette devices */
TkWinColormap *cmap = (TkWinColormap *) Tk_Colormap(togl->TkWin);
UINT index;
COLORREF newColor, closeColor;
PALETTEENTRY entry, closeEntry;
int isNew, refCount;
Tcl_HashEntry *entryPtr;
entry.peRed = (unsigned char) (red * 255 + .5);
entry.peGreen = (unsigned char) (green * 255 + .5);
entry.peBlue = (unsigned char) (blue * 255 + .5);
entry.peFlags = 0;
/*
* Find the nearest existing palette entry.
*/
newColor = RGB(entry.peRed, entry.peGreen, entry.peBlue);
index = GetNearestPaletteIndex(cmap->palette, newColor);
GetPaletteEntries(cmap->palette, index, 1, &closeEntry);
closeColor = RGB(closeEntry.peRed, closeEntry.peGreen, closeEntry.peBlue);
/*
* If this is not a duplicate and colormap is not full, allocate a new entry.
*/
if (newColor != closeColor) {
if (cmap->size == (unsigned int) togl->CiColormapSize) {
entry = closeEntry;
} else {
cmap->size++;
ResizePalette(cmap->palette, cmap->size);
index = cmap->size - 1;
SetPaletteEntries(cmap->palette, index, 1, &entry);
SelectPalette(togl->tglGLHdc, cmap->palette, TRUE);
RealizePalette(togl->tglGLHdc);
}
}
newColor = PALETTERGB(entry.peRed, entry.peGreen, entry.peBlue);
entryPtr = Tcl_CreateHashEntry(&cmap->refCounts,
(CONST char *) newColor, &isNew);
if (isNew) {
refCount = 1;
} else {
refCount = ((int) Tcl_GetHashValue(entryPtr)) + 1;
}
Tcl_SetHashValue(entryPtr, (ClientData) refCount);
/* for EPS output */
togl->EpsRedMap[index] = (GLfloat) (entry.peRed / 255.0);
togl->EpsGreenMap[index] = (GLfloat) (entry.peGreen / 255.0);
togl->EpsBlueMap[index] = (GLfloat) (entry.peBlue / 255.0);
return index;
}
static void
Win32FreeColor(const Togl *togl, unsigned long index)
{
TkWinColormap *cmap = (TkWinColormap *) Tk_Colormap(togl->TkWin);
COLORREF cref;
UINT count, refCount;
PALETTEENTRY entry, *entries;
Tcl_HashEntry *entryPtr;
if (index >= cmap->size) {
panic("Tried to free a color that isn't allocated.");
}
GetPaletteEntries(cmap->palette, index, 1, &entry);
cref = PALETTERGB(entry.peRed, entry.peGreen, entry.peBlue);
entryPtr = Tcl_FindHashEntry(&cmap->refCounts, (CONST char *) cref);
if (!entryPtr) {
panic("Tried to free a color that isn't allocated.");
}
refCount = (int) Tcl_GetHashValue(entryPtr) - 1;
if (refCount == 0) {
count = cmap->size - index;
entries = (PALETTEENTRY *) ckalloc(sizeof (PALETTEENTRY) * count);
GetPaletteEntries(cmap->palette, index + 1, count, entries);
SetPaletteEntries(cmap->palette, index, count, entries);
SelectPalette(togl->tglGLHdc, cmap->palette, TRUE);
RealizePalette(togl->tglGLHdc);
ckfree((char *) entries);
cmap->size--;
Tcl_DeleteHashEntry(entryPtr);
} else {
Tcl_SetHashValue(entryPtr, (ClientData) refCount);
}
}
static void
Win32SetColor(const Togl *togl,
unsigned long index, float red, float green, float blue)
{
TkWinColormap *cmap = (TkWinColormap *) Tk_Colormap(togl->TkWin);
PALETTEENTRY entry;
entry.peRed = (unsigned char) (red * 255 + .5);
entry.peGreen = (unsigned char) (green * 255 + .5);
entry.peBlue = (unsigned char) (blue * 255 + .5);
entry.peFlags = 0;
SetPaletteEntries(cmap->palette, index, 1, &entry);
SelectPalette(togl->tglGLHdc, cmap->palette, TRUE);
RealizePalette(togl->tglGLHdc);
/* for EPS output */
togl->EpsRedMap[index] = (GLfloat) (entry.peRed / 255.0);
togl->EpsGreenMap[index] = (GLfloat) (entry.peGreen / 255.0);
togl->EpsBlueMap[index] = (GLfloat) (entry.peBlue / 255.0);
}
#endif /* TOGL_X11 */
unsigned long
Togl_AllocColor(const Togl *togl, float red, float green, float blue)
{
if (togl->RgbaFlag) {
(void) fprintf(stderr,
"Error: Togl_AllocColor illegal in RGBA mode.\n");
return 0;
}
/* TODO: maybe not... */
if (togl->PrivateCmapFlag) {
(void) fprintf(stderr,
"Error: Togl_AllocColor illegal with private colormap\n");
return 0;
}
#if defined(TOGL_X11)
{
XColor xcol;
int exact;
xcol.red = (short) (red * 65535.0);
xcol.green = (short) (green * 65535.0);
xcol.blue = (short) (blue * 65535.0);
noFaultXAllocColor(Tk_Display(togl->TkWin), Tk_Colormap(togl->TkWin),
Tk_Visual(togl->TkWin)->map_entries, &xcol, &exact);
/* for EPS output */
togl->EpsRedMap[xcol.pixel] = (float) xcol.red / 65535.0;
togl->EpsGreenMap[xcol.pixel] = (float) xcol.green / 65535.0;
togl->EpsBlueMap[xcol.pixel] = (float) xcol.blue / 65535.0;
return xcol.pixel;
}
#elif defined(TOGL_WGL)
return Win32AllocColor(togl, red, green, blue);
#elif defined(TOGL_AGL)
/* still need to implement this on Mac... */
return 0;
#endif
}
void
Togl_FreeColor(const Togl *togl, unsigned long pixel)
{
if (togl->RgbaFlag) {
(void) fprintf(stderr, "Error: Togl_FreeColor illegal in RGBA mode.\n");
return;
}
/* TODO: maybe not... */
if (togl->PrivateCmapFlag) {
(void) fprintf(stderr,
"Error: Togl_FreeColor illegal with private colormap\n");
return;
}
#if defined(TOGL_X11)
(void) XFreeColors(Tk_Display(togl->TkWin), Tk_Colormap(togl->TkWin),
&pixel, 1, 0);
#elif defined(TOGL_WGL)
Win32FreeColor(togl, pixel);
#endif
}
void
Togl_SetColor(const Togl *togl,
unsigned long index, float red, float green, float blue)
{
if (togl->RgbaFlag) {
(void) fprintf(stderr, "Error: Togl_SetColor illegal in RGBA mode.\n");
return;
}
if (!togl->PrivateCmapFlag) {
(void) fprintf(stderr,
"Error: Togl_SetColor requires a private colormap\n");
return;
}
#if defined(TOGL_X11)
{
XColor xcol;
xcol.pixel = index;
xcol.red = (short) (red * 65535.0);
xcol.green = (short) (green * 65535.0);
xcol.blue = (short) (blue * 65535.0);
xcol.flags = DoRed | DoGreen | DoBlue;
(void) XStoreColor(Tk_Display(togl->TkWin), Tk_Colormap(togl->TkWin),
&xcol);
/* for EPS output */
togl->EpsRedMap[xcol.pixel] = (float) xcol.red / 65535.0;
togl->EpsGreenMap[xcol.pixel] = (float) xcol.green / 65535.0;
togl->EpsBlueMap[xcol.pixel] = (float) xcol.blue / 65535.0;
}
#elif defined(TOGL_WGL)
Win32SetColor(togl, index, red, green, blue);
#endif
}
#if TOGL_USE_FONTS == 1
# include "toglFont.c"
#else
Tcl_Obj *
Togl_LoadBitmapFont(const Togl *togl, const char *fontname)
{
return NULL;
}
int
Togl_UnloadBitmapFont(const Togl *togl, Tcl_Obj *bitmapfont)
{
return TCL_OK;
}
int
Togl_WriteObj(const Togl *togl, const Tcl_Obj *toglfont, const Tcl_Obj *obj)
{
return -1;
}
int
Togl_WriteChars(const Togl *togl, const Tcl_Obj *toglfont, const char *str,
int len)
{
return -1;
}
#endif /* TOGL_USE_FONTS */
/*
* Overlay functions
*/
void
Togl_UseLayer(Togl *togl, int layer)
{
if (layer == TOGL_NORMAL) {
#if defined(TOGL_WGL)
int res = wglMakeCurrent(togl->tglGLHdc, togl->tglGLHglrc);
if (!res) {
ErrorExit(TEXT("wglMakeCurrent"));
}
#elif defined(TOGL_X11)
(void) glXMakeCurrent(Tk_Display(togl->TkWin),
Tk_WindowId(togl->TkWin), togl->GlCtx);
#elif defined(TOGL_AGL)
(void) aglSetCurrentContext(togl->aglCtx);
#endif /* TOGL_WGL */
} else if (layer == TOGL_OVERLAY && togl->OverlayWindow) {
#if defined(TOGL_WGL)
int res = wglMakeCurrent(togl->tglGLHdc, togl->tglGLOverlayHglrc);
if (!res) {
ErrorExit(TEXT("wglMakeCurrent overlay"));
}
#elif defined(TOGL_X11)
(void) glXMakeCurrent(Tk_Display(togl->TkWin),
togl->OverlayWindow, togl->OverlayCtx);
#elif defined(TOGL_AGL)
#endif /* TOGL_WGL */
} else {
/* error */
}
}
void
Togl_ShowOverlay(Togl *togl)
{
#if defined(TOGL_X11) /* not yet implemented on Windows */
if (togl->OverlayWindow) {
(void) XMapWindow(Tk_Display(togl->TkWin), togl->OverlayWindow);
(void) XInstallColormap(Tk_Display(togl->TkWin), togl->OverlayCmap);
togl->OverlayIsMapped = True;
}
#endif
}
void
Togl_HideOverlay(Togl *togl)
{
if (togl->OverlayWindow && togl->OverlayIsMapped) {
(void) XUnmapWindow(Tk_Display(togl->TkWin), togl->OverlayWindow);
togl->OverlayIsMapped = False;
}
}
void
Togl_PostOverlayRedisplay(Togl *togl)
{
if (!togl->OverlayUpdatePending
&& togl->OverlayWindow && togl->OverlayDisplayProc) {
Tk_DoWhenIdle(Togl_RenderOverlay, (ClientData) togl);
togl->OverlayUpdatePending = True;
}
}
int
Togl_ExistsOverlay(const Togl *togl)
{
return togl->OverlayFlag;
}
int
Togl_GetOverlayTransparentValue(const Togl *togl)
{
return togl->OverlayTransparentPixel;
}
int
Togl_IsMappedOverlay(const Togl *togl)
{
return togl->OverlayFlag && togl->OverlayIsMapped;
}
unsigned long
Togl_AllocColorOverlay(const Togl *togl, float red, float green, float blue)
{
#if defined(TOGL_X11) /* not yet implemented on Windows */
if (togl->OverlayFlag && togl->OverlayCmap) {
XColor xcol;
xcol.red = (short) (red * 65535.0);
xcol.green = (short) (green * 65535.0);
xcol.blue = (short) (blue * 65535.0);
if (!XAllocColor(Tk_Display(togl->TkWin), togl->OverlayCmap, &xcol))
return (unsigned long) -1;
return xcol.pixel;
}
#endif /* TOGL_X11 */
return (unsigned long) -1;
}
void
Togl_FreeColorOverlay(const Togl *togl, unsigned long pixel)
{
#if defined(TOGL_X11) /* not yet implemented on Windows */
if (togl->OverlayFlag && togl->OverlayCmap) {
(void) XFreeColors(Tk_Display(togl->TkWin), togl->OverlayCmap, &pixel,
1, 0);
}
#endif /* TOGL_X11 */
}
/*
* User client data
*/
ClientData
Togl_GetClientData(const Togl *togl)
{
return togl->Client_Data;
}
void
Togl_SetClientData(Togl *togl, ClientData clientData)
{
togl->Client_Data = clientData;
}
#ifdef MESA_COLOR_HACK
/*
* Let's know how many free colors do we have
*/
# define RLEVELS 5
# define GLEVELS 9
# define BLEVELS 5
/* to free dithered_rgb_colormap pixels allocated by Mesa */
static unsigned long *ToglMesaUsedPixelCells = NULL;
static int ToglMesaUsedFreeCells = 0;
static int
get_free_color_cells(Display *display, int screen, Colormap colormap)
{
if (!ToglMesaUsedPixelCells) {
XColor xcol;
int i;
int colorsfailed, ncolors = XDisplayCells(display, screen);
long r, g, b;
ToglMesaUsedPixelCells =
(unsigned long *) ckalloc(ncolors * sizeof (unsigned long));
/* Allocate X colors and initialize color_table[], red_table[], etc */
/* de Mesa 2.1: xmesa1.c setup_dithered_(...) */
i = colorsfailed = 0;
for (r = 0; r < RLEVELS; r++)
for (g = 0; g < GLEVELS; g++)
for (b = 0; b < BLEVELS; b++) {
int exact;
xcol.red = (r * 65535) / (RLEVELS - 1);
xcol.green = (g * 65535) / (GLEVELS - 1);
xcol.blue = (b * 65535) / (BLEVELS - 1);
noFaultXAllocColor(display, colormap, ncolors,
&xcol, &exact);
ToglMesaUsedPixelCells[i++] = xcol.pixel;
if (!exact) {
colorsfailed++;
}
}
ToglMesaUsedFreeCells = i;
XFreeColors(display, colormap, ToglMesaUsedPixelCells,
ToglMesaUsedFreeCells, 0x00000000);
}
return ToglMesaUsedFreeCells;
}
static void
free_default_color_cells(Display *display, Colormap colormap)
{
if (ToglMesaUsedPixelCells) {
XFreeColors(display, colormap, ToglMesaUsedPixelCells,
ToglMesaUsedFreeCells, 0x00000000);
ckfree((char *) ToglMesaUsedPixelCells);
ToglMesaUsedPixelCells = NULL;
ToglMesaUsedFreeCells = 0;
}
}
#endif
/*
* Original stereo code contributed by Ben Evans (Ben.Evans@anusf.anu.edu.au)
* and was based on SGI's /usr/share/src/OpenGL/teach/stereo/glwstereo.c,
* which is identical to the 1997/12/1 glwstereo.c code in the CrystalEyes
* Software Development Kit.
*/
int
Togl_NumEyes(const Togl *togl)
{
if (togl->Stereo > TOGL_STEREO_ONE_EYE_MAX)
return 2;
return 1;
}
/* call instead of glDrawBuffer */
void
Togl_DrawBuffer(Togl *togl, GLenum mode)
{
if (togl->Stereo <= TOGL_STEREO_ONE_EYE_MAX) {
/* Only drawing a single eye */
if (togl->currentStereoBuffer != STEREO_BUFFER_NONE) {
glViewport(0, 0, togl->Width, togl->Height);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
togl->currentStereoBuffer = STEREO_BUFFER_NONE;
}
switch (mode) {
case GL_FRONT:
case GL_BACK:
case GL_FRONT_AND_BACK:
break;
case GL_LEFT:
case GL_FRONT_LEFT:
case GL_RIGHT:
case GL_FRONT_RIGHT:
mode = GL_FRONT;
break;
case GL_BACK_LEFT:
case GL_BACK_RIGHT:
mode = GL_BACK;
break;
default:
break;
}
glDrawBuffer(mode);
#ifdef STEREO_I_H
if (togl->Stereo == TOGL_STEREO_NVIDIA_CON) {
if (togl->pStereoI) {
togl->pStereoI->SetSeparation((float) togl->EyeSeparation);
togl->pStereoI->SetConvergence((float) togl->Convergence);
}
}
#endif
return;
}
/* called once for each eye */
switch (mode) {
case GL_FRONT:
case GL_BACK:
case GL_FRONT_AND_BACK:
/*
** Simultaneous drawing to both left and right buffers isn't
** really possible if we don't have a stereo capable visual.
** For now just fall through and use the left buffer.
*/
case GL_LEFT:
case GL_FRONT_LEFT:
case GL_BACK_LEFT:
togl->currentStereoBuffer = STEREO_BUFFER_LEFT;
break;
case GL_RIGHT:
case GL_FRONT_RIGHT:
case GL_BACK_RIGHT:
togl->currentStereoBuffer = STEREO_BUFFER_RIGHT;
break;
default:
break;
}
if (togl->Stereo != TOGL_STEREO_NATIVE) {
switch (mode) {
default:
mode = GL_FRONT;
break;
case GL_BACK:
case GL_BACK_LEFT:
case GL_BACK_RIGHT:
mode = GL_BACK;
break;
}
}
switch (togl->Stereo) {
default:
break;
#ifdef __sgi
case TOGL_STEREO_SGIOLDSTYLE:
glXWaitGL(); /* sync with GL command stream before calling X
*/
XSGISetStereoBuffer(togl->display, Tk_WindowId(togl->TkWin),
togl->currentStereoBuffer);
glXWaitX(); /* sync with X command stream before calling GL
*/
break;
#endif
case TOGL_STEREO_ANAGLYPH:
if (togl->currentStereoBuffer == STEREO_BUFFER_LEFT)
glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);
else
glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
break;
case TOGL_STEREO_CROSS_EYE:
if (togl->currentStereoBuffer == STEREO_BUFFER_LEFT)
glViewport(togl->Width / 2 + 1, 0, togl->Width / 2, togl->Height);
else
glViewport(0, 0, togl->Width / 2, togl->Height);
break;
case TOGL_STEREO_WALL_EYE:
if (togl->currentStereoBuffer == STEREO_BUFFER_LEFT)
glViewport(0, 0, togl->Width / 2, togl->Height);
else
glViewport(togl->Width / 2 + 1, 0, togl->Width / 2, togl->Height);
break;
case TOGL_STEREO_DTI:
if (togl->currentStereoBuffer == STEREO_BUFFER_LEFT)
glViewport(0, 0, togl->Width / 2, togl->Height);
else
glViewport(togl->Width / 2 + 1, 0, togl->Width / 2, togl->Height);
break;
}
glDrawBuffer(mode);
}
/* call instead of glClear */
void
Togl_Clear(const Togl *togl, GLbitfield mask)
{
switch (togl->Stereo) {
default:
break;
case TOGL_STEREO_CROSS_EYE:
case TOGL_STEREO_WALL_EYE:
case TOGL_STEREO_DTI:
if (togl->currentStereoBuffer != STEREO_BUFFER_LEFT) {
/* Since glViewport does not affect what is cleared (unlike
* glScissor), only clear in left eye */
return;
}
}
#if 0
/* only needed if we wish to support multi-eye clears */
if (togl->Stereo > TOGL_STEREO_ONE_EYE_MAX) {
GLenum drawBuffer = togl->currentDrawBuffer;
switch (drawBuffer) {
case GL_FRONT:
Togl_DrawBuffer(togl, GL_FRONT_RIGHT);
glClear(mask);
Togl_DrawBuffer(togl, drawBuffer);
break;
case GL_BACK:
Togl_DrawBuffer(togl, GL_BACK_RIGHT);
glClear(mask);
Togl_DrawBuffer(togl, drawBuffer);
break;
case GL_FRONT_AND_BACK:
Togl_DrawBuffer(togl, GL_RIGHT);
glClear(mask);
Togl_DrawBuffer(togl, drawBuffer);
break;
case GL_LEFT:
case GL_FRONT_LEFT:
case GL_BACK_LEFT:
case GL_RIGHT:
case GL_FRONT_RIGHT:
case GL_BACK_RIGHT:
default:
break;
}
}
#endif
glClear(mask);
}
void
Togl_Frustum(const Togl *togl, GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar)
{
GLdouble eyeOffset = 0, eyeShift = 0;
if (togl->Stereo == TOGL_STEREO_LEFT_EYE
|| togl->currentStereoBuffer == STEREO_BUFFER_LEFT)
eyeOffset = -togl->EyeSeparation / 2; /* for left eye */
else if (togl->Stereo == TOGL_STEREO_RIGHT_EYE
|| togl->currentStereoBuffer == STEREO_BUFFER_RIGHT)
eyeOffset = togl->EyeSeparation / 2; /* for right eye */
eyeShift = (togl->Convergence - zNear) * (eyeOffset / togl->Convergence);
/* compenstate for altered viewports */
switch (togl->Stereo) {
default:
break;
case TOGL_STEREO_SGIOLDSTYLE:
case TOGL_STEREO_DTI:
/* squished image is expanded, nothing needed */
break;
case TOGL_STEREO_CROSS_EYE:
case TOGL_STEREO_WALL_EYE:{
GLdouble delta = (top - bottom) / 2;
top += delta;
bottom -= delta;
break;
}
}
glFrustum(left + eyeShift, right + eyeShift, bottom, top, zNear, zFar);
glTranslated(-eyeShift, 0, 0);
}
void
Togl_Ortho(const Togl *togl, GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar)
{
/* TODO: debug this */
GLdouble eyeOffset = 0, eyeShift = 0;
if (togl->currentStereoBuffer == STEREO_BUFFER_LEFT)
eyeOffset = -togl->EyeSeparation / 2; /* for left eye */
else if (togl->currentStereoBuffer == STEREO_BUFFER_RIGHT)
eyeOffset = togl->EyeSeparation / 2; /* for right eye */
eyeShift = (togl->Convergence - zNear) * (eyeOffset / togl->Convergence);
/* compenstate for altered viewports */
switch (togl->Stereo) {
default:
break;
case TOGL_STEREO_SGIOLDSTYLE:
case TOGL_STEREO_DTI:
/* squished image is expanded, nothing needed */
break;
case TOGL_STEREO_CROSS_EYE:
case TOGL_STEREO_WALL_EYE:{
GLdouble delta = (top - bottom) / 2;
top += delta;
bottom -= delta;
break;
}
}
glOrtho(left + eyeShift, right + eyeShift, bottom, top, zNear, zFar);
glTranslated(-eyeShift, 0, 0);
}
int
Togl_GetToglFromObj(Tcl_Interp *interp, Tcl_Obj *obj, Togl **toglPtr)
{
Tcl_Command toglCmd;
Tcl_CmdInfo info;
toglCmd = Tcl_GetCommandFromObj(interp, obj);
if (Tcl_GetCommandInfoFromToken(toglCmd, &info) == 0
|| info.objProc != Togl_ObjWidget) {
Tcl_AppendResult(interp, "expected togl command argument", NULL);
return TCL_ERROR;
}
*toglPtr = (Togl *) info.objClientData;
return TCL_OK;
}
int
Togl_GetToglFromName(Tcl_Interp *interp, const char *cmdName, Togl **toglPtr)
{
Tcl_CmdInfo info;
if (Tcl_GetCommandInfo(interp, cmdName, &info) == 0
|| info.objProc != Togl_ObjWidget) {
Tcl_AppendResult(interp, "expected togl command argument", NULL);
return TCL_ERROR;
}
*toglPtr = (Togl *) info.objClientData;
return TCL_OK;
}
static int ObjectIsEmpty(Tcl_Obj *objPtr);
/*
*----------------------------------------------------------------------
*
* GetStereo -
*
* Converts an internal int into a a Tcl string obj.
*
* Results:
* Tcl_Obj containing the string representation of the stereo value.
*
* Side effects:
* Creates a new Tcl_Obj.
*
*----------------------------------------------------------------------
*/
static Tcl_Obj *
GetStereo(ClientData clientData, Tk_Window tkwin, char *recordPtr,
int internalOffset)
/* recordPtr is a pointer to widget record. */
/* internalOffset is the offset within *recordPtr containing the stereo
* value. */
{
int stereo = *(int *) (recordPtr + internalOffset);
const char *name = "unknown";
switch (stereo) {
case TOGL_STEREO_NONE:
name = "";
break;
case TOGL_STEREO_LEFT_EYE:
name = "left eye";
break;
case TOGL_STEREO_RIGHT_EYE:
name = "right eye";
break;
case TOGL_STEREO_NVIDIA_CON:
name = "nvidia consumer stereo";
break;
case TOGL_STEREO_NATIVE:
name = "native";
break;
case TOGL_STEREO_SGIOLDSTYLE:
name = "sgioldstyle";
break;
case TOGL_STEREO_ANAGLYPH:
name = "anaglyph";
break;
case TOGL_STEREO_CROSS_EYE:
name = "cross-eye";
break;
case TOGL_STEREO_WALL_EYE:
name = "wall-eye";
break;
case TOGL_STEREO_DTI:
name = "dti";
break;
}
return Tcl_NewStringObj(name, -1);
}
/*
*----------------------------------------------------------------------
*
* SetStereo --
*
* Converts a Tcl_Obj representing a widgets stereo into an
* integer value.
*
* Results:
* Standard Tcl result.
*
* Side effects:
* May store the integer value into the internal representation
* pointer. May change the pointer to the Tcl_Obj to NULL to indicate
* that the specified string was empty and that is acceptable.
*
*----------------------------------------------------------------------
*/
static int
SetStereo(ClientData clientData, Tcl_Interp *interp, Tk_Window tkwin,
Tcl_Obj **value, char *recordPtr, int internalOffset,
char *oldInternalPtr, int flags)
/* interp is the current interp; may be used for errors. */
/* tkwin is the Window for which option is being set. */
/* value is a pointer to the pointer to the value object. We use a pointer
* to the pointer because we may need to return a value (NULL). */
/* recordPtr is a pointer to storage for the widget record. */
/* internalOffset is the offset within *recordPtr at which the internal
* value is to be stored. */
/* oldInternalPtr is a pointer to storage for the old value. */
/* flags are the flags for the option, set Tk_SetOptions. */
{
int stereo = 0;
char *string, *internalPtr;
internalPtr = (internalOffset > 0) ? recordPtr + internalOffset : NULL;
if (flags & TK_OPTION_NULL_OK && ObjectIsEmpty(*value)) {
*value = NULL;
} else {
/*
* Convert the stereo specifier into an integer value.
*/
string = Tcl_GetString(*value);
if (strcmp(string, "") == 0 || strcasecmp(string, "none") == 0
|| strcasecmp(string, "false") == 0) {
stereo = TOGL_STEREO_NONE;
} else if (strcasecmp(string, "native") == 0
|| strcasecmp(string, "true") == 0) {
stereo = TOGL_STEREO_NATIVE;
/* check if available when creating visual */
} else if (strcasecmp(string, "left eye") == 0) {
stereo = TOGL_STEREO_LEFT_EYE;
} else if (strcasecmp(string, "right eye") == 0) {
stereo = TOGL_STEREO_RIGHT_EYE;
} else if (strcasecmp(string, "nvidia consumer stereo") == 0) {
stereo = TOGL_STEREO_NVIDIA_CON;
} else if (strcasecmp(string, "sgioldstyle") == 0) {
stereo = TOGL_STEREO_SGIOLDSTYLE;
} else if (strcasecmp(string, "anaglyph") == 0) {
stereo = TOGL_STEREO_ANAGLYPH;
} else if (strcasecmp(string, "cross-eye") == 0) {
stereo = TOGL_STEREO_CROSS_EYE;
} else if (strcasecmp(string, "wall-eye") == 0) {
stereo = TOGL_STEREO_WALL_EYE;
} else if (strcasecmp(string, "dti") == 0) {
stereo = TOGL_STEREO_DTI;
} else {
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "bad stereo value \"",
Tcl_GetString(*value), "\"", NULL);
return TCL_ERROR;
}
}
if (internalPtr != NULL) {
*((int *) oldInternalPtr) = *((int *) internalPtr);
*((int *) internalPtr) = stereo;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
* RestoreStereo --
*
* Restore a stereo option value from a saved value.
*
* Results:
* None.
*
* Side effects:
* Restores the old value.
*
*----------------------------------------------------------------------
*/
static void
RestoreStereo(ClientData clientData, Tk_Window tkwin, char *internalPtr,
char *oldInternalPtr)
{
*(int *) internalPtr = *(int *) oldInternalPtr;
}
/*
*----------------------------------------------------------------------
*
* ObjectIsEmpty --
*
* This procedure tests whether the string value of an object is
* empty.
*
* Results:
* The return value is 1 if the string value of objPtr has length
* zero, and 0 otherwise.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
ObjectIsEmpty(Tcl_Obj *objPtr)
/* objPtr = Object to test. May be NULL. */
{
int length;
if (objPtr == NULL) {
return 1;
}
if (objPtr->bytes != NULL) {
return (objPtr->length == 0);
}
Tcl_GetStringFromObj(objPtr, &length);
return (length == 0);
}
|