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
|
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004-2016 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
#ifndef __VCG_GL_MESH_ATTRIBUTES_FEEDER
#define __VCG_GL_MESH_ATTRIBUTES_FEEDER
#include <vector>
#include <map>
#include <algorithm>
#include <stdexcept>
#include <climits>
#include <string>
#include <wrap/gl/space.h>
#include <wrap/gl/math.h>
#include <vcg/space/color4.h>
#include <vcg/math/matrix44.h>
#include<wrap/system/memory_info.h>
#include <wrap/gl/gl_mesh_attributes_info.h>
namespace vcg
{
struct RenderingModalityGLOptions
{
bool _perbbox_enabled;
bool _perbbox_fixed_color_enabled;
bool _perpoint_fixed_color_enabled;
bool _perwire_fixed_color_enabled;
bool _persolid_fixed_color_enabled;
Color4b _perbbox_fixed_color;
Color4b _perpoint_fixed_color;
Color4b _perwire_fixed_color;
Color4b _persolid_fixed_color;
bool _perbbox_mesh_color_enabled;
bool _perpoint_mesh_color_enabled;
bool _perwire_mesh_color_enabled;
bool _persolid_mesh_color_enabled;
bool _perpoint_noshading;
bool _perwire_noshading;
bool _persolid_noshading;
bool _perpoint_dot_enabled;
float _perpoint_pointsize;
bool _perpoint_pointsmooth_enabled;
bool _perpoint_pointattenuation_enabled;
float _perwire_wirewidth;
RenderingModalityGLOptions()
{
_perbbox_enabled = false;
_perbbox_fixed_color_enabled = true;
_perpoint_fixed_color_enabled = false;
_perwire_fixed_color_enabled = true;
_persolid_fixed_color_enabled = true;
_perbbox_fixed_color = vcg::Color4b(Color4b::White);
_perpoint_fixed_color = vcg::Color4b(Color4b::White);
_perwire_fixed_color = Color4b(Color4b::DarkGray);
_persolid_fixed_color = vcg::Color4b(Color4b::White);
_perbbox_mesh_color_enabled = false;
_perpoint_mesh_color_enabled = false;
_perwire_mesh_color_enabled = false;
_persolid_mesh_color_enabled = false;
_perpoint_dot_enabled = false;
_perpoint_noshading = false;
_perwire_noshading = true;
_persolid_noshading = false;
_perpoint_pointsize = 3.0f;
_perpoint_pointsmooth_enabled = false;
_perpoint_pointattenuation_enabled = true;
_perwire_wirewidth = 1.0f;
}
RenderingModalityGLOptions(const RenderingModalityGLOptions& opts)
{
copyData(opts);
}
virtual ~RenderingModalityGLOptions()
{
}
RenderingModalityGLOptions& operator=(const RenderingModalityGLOptions& opts)
{
copyData(opts);
return (*this);
}
private:
void copyData(const RenderingModalityGLOptions& opts)
{
_perbbox_enabled = opts._perbbox_enabled;
_perpoint_dot_enabled = opts._perpoint_dot_enabled;
_perpoint_pointsize = opts._perpoint_pointsize;
_perpoint_pointsmooth_enabled = opts._perpoint_pointsmooth_enabled;
_perpoint_pointattenuation_enabled = opts._perpoint_pointattenuation_enabled;
_perbbox_fixed_color_enabled = opts._perbbox_fixed_color_enabled;
_perpoint_fixed_color_enabled = opts._perpoint_fixed_color_enabled;
_perwire_fixed_color_enabled = opts._perwire_fixed_color_enabled;
_persolid_fixed_color_enabled = opts._persolid_fixed_color_enabled;
_perbbox_mesh_color_enabled = opts._perbbox_mesh_color_enabled;
_perpoint_mesh_color_enabled = opts._perpoint_mesh_color_enabled;
_perwire_mesh_color_enabled = opts._perwire_mesh_color_enabled;
_persolid_mesh_color_enabled = opts._persolid_mesh_color_enabled;
_perbbox_fixed_color = opts._perbbox_fixed_color;
_perpoint_fixed_color = opts._perpoint_fixed_color;
_perwire_fixed_color = opts._perwire_fixed_color;
_persolid_fixed_color = opts._persolid_fixed_color;
_perpoint_noshading = opts._perpoint_noshading;
_perwire_noshading = opts._perwire_noshading;
_persolid_noshading = opts._persolid_noshading;
_perwire_wirewidth = opts._perwire_wirewidth;
}
};
template<typename GL_OPTIONS_DERIVED_TYPE = RenderingModalityGLOptions>
class PerViewData : public GLMeshAttributesInfo
{
public:
typedef GL_OPTIONS_DERIVED_TYPE GLOptionsType;
PerViewData()
:_pmmask(), _intatts(PR_ARITY), _glopts(NULL)
{
reset();
}
PerViewData(const PerViewData<GL_OPTIONS_DERIVED_TYPE>& dt)
:_pmmask(dt._pmmask), _intatts(dt._intatts), _glopts(NULL)
{
if (dt._glopts != NULL)
_glopts = new GL_OPTIONS_DERIVED_TYPE(*(dt._glopts));
}
~PerViewData()
{
_intatts.clear();
delete _glopts;
}
PerViewData& operator=(const PerViewData<GL_OPTIONS_DERIVED_TYPE>& dt)
{
_pmmask = dt._pmmask;
_intatts = dt._intatts;
if (dt._glopts != NULL)
_glopts = new GL_OPTIONS_DERIVED_TYPE(*(dt._glopts));
return (*this);
}
bool set(PRIMITIVE_MODALITY pm, const RendAtts& atts)
{
size_t pmind(pm);
if (pm >= _intatts.size())
return false;
//_pmmask.set(pm);
_intatts[pmind] = InternalRendAtts(atts, pm);
_pmmask.set(size_t(pm), _intatts[pmind][INT_ATT_NAMES::ATT_VERTPOSITION]);
return true;
}
bool set(PRIMITIVE_MODALITY pm, ATT_NAMES att, bool onoff)
{
size_t pmind(pm);
if (pm >= _intatts.size())
return false;
_intatts[pmind][att] = onoff;
_pmmask.set(size_t(pm), _intatts[pmind][INT_ATT_NAMES::ATT_VERTPOSITION]);
if (_pmmask.test(size_t(pm)))
_intatts[pmind].setIndexingIfNeeded(pm);
return true;
}
bool set(PRIMITIVE_MODALITY pm, bool onoff)
{
return set(pm, INT_ATT_NAMES::ATT_VERTPOSITION, onoff);
}
void set(const GL_OPTIONS_DERIVED_TYPE& opts)
{
delete _glopts;
_glopts = new GL_OPTIONS_DERIVED_TYPE(opts);
}
bool isPrimitiveActive(PRIMITIVE_MODALITY pm) const
{
if (pm == PR_ARITY)
return false;
return (_pmmask.test(pm) && _intatts[size_t(pm)][INT_ATT_NAMES::ATT_VERTPOSITION]);
}
PRIMITIVE_MODALITY_MASK getPrimitiveModalityMask() const
{
return _pmmask;
}
bool get(PRIMITIVE_MODALITY pm, RendAtts& atts) const
{
size_t pmind(pm);
if (pm >= _intatts.size())
return false;
atts = _intatts[pmind];
return true;
}
bool get(GL_OPTIONS_DERIVED_TYPE& opts) const
{
if (_glopts == NULL)
return false;
opts = (*_glopts);
return true;
}
void reset(bool deleteglopts = true)
{
_pmmask.reset();
for (typename PerRendModData::iterator it = _intatts.begin(); it != _intatts.end(); ++it)
it->reset();
if (deleteglopts)
{
delete _glopts;
_glopts = 0;
}
}
void serialize(std::string& str) const
{
str.append(_pmmask.to_string());
for (typename PerRendModData::const_iterator it = _intatts.begin(); it != _intatts.end(); ++it)
{
std::string s;
it->serialize(s);
str.append(s);
}
std::string s;
_glopts->serialize(s);
str.append(s);
}
bool deserialize(const std::string& str)
{
std::string::size_type pos = 0;
std::string token[6];
token[0] = str.substr(pos, _pmmask.size());
if (token[0].length() < _pmmask.size())
return false;
int i = 1;
pos = _pmmask.size();
for (typename PerRendModData::iterator it = _intatts.begin(); it != _intatts.end(); ++it, i++)
{
token[i] = str.substr(pos, InternalRendAtts::AttName::enumArity());
if (token[i].length() < InternalRendAtts::AttName::enumArity())
return false;
pos = pos + InternalRendAtts::AttName::enumArity();
}
if (_glopts != NULL)
{
std::string tmp;
size_t size = _glopts->serialize(tmp);
token[i] = str.substr(pos, size);
if (token[i].length() < size)
return false;
}
_pmmask = PRIMITIVE_MODALITY_MASK(token[0]);
i = 1;
for (typename PerRendModData::iterator it = _intatts.begin(); it != _intatts.end(); ++it, i++)
it->deserialize(token[i]);
if (_glopts != NULL)
_glopts->deserialize(token[i]);
return true;
}
protected:
template<typename MESH_TYPE, typename UNIQUE_VIEW_ID_TYPE, typename XX_GL_OPTIONS_DERIVED_TYPE> friend class NotThreadSafeGLMeshAttributesMultiViewerBOManager;
typedef std::vector<InternalRendAtts> PerRendModData;
PRIMITIVE_MODALITY_MASK _pmmask;
PerRendModData _intatts;
GL_OPTIONS_DERIVED_TYPE* _glopts;
};
/****************************************************WARNING!!!!!!!!!!!!!!!!!*********************************************************************************************/
//You must inherit from NotThreadSafeGLMeshAttributesMultiViewerBOManager, providing thread safe mechanisms, in order to use the bo facilities exposed by the class.
//In wrap/qt/qt_thread_safe_memory_rendering.h you will find a ready to use class based on QtConcurrency module.
/*************************************************************************************************************************************************************************/
template<typename MESH_TYPE, typename UNIQUE_VIEW_ID_TYPE = unsigned int, typename GL_OPTIONS_DERIVED_TYPE = RenderingModalityGLOptions>
class NotThreadSafeGLMeshAttributesMultiViewerBOManager : public GLMeshAttributesInfo
{
public:
typedef PerViewData<GL_OPTIONS_DERIVED_TYPE> PVData;
protected:
/****************************************************WARNING!!!!!!!!!!!!!!!!!*********************************************************************************************/
//You must inherit from NotThreadSafeGLMeshAttributesMultiViewerBOManager, providing thread safe mechanisms, in order to use the bo facilities exposed by the class.
//In wrap/qt/qt_thread_safe_memory_rendering.h you will find a ready to use class based on QtConcurrency module.
/*************************************************************************************************************************************************************************/
NotThreadSafeGLMeshAttributesMultiViewerBOManager(/*const*/ MESH_TYPE& mesh, MemoryInfo& meminfo, size_t perbatchprimitives)
:_mesh(mesh), _gpumeminfo(meminfo), _bo(INT_ATT_NAMES::enumArity(), NULL), _currallocatedboatt(), _borendering(false), _perbatchprim(perbatchprimitives), _chunkmap(), _edge(), _meshverticeswhenedgeindiceswerecomputed(0), _meshtriangleswhenedgeindiceswerecomputed(0), _tr(), _debugmode(false), _loginfo(), _meaningfulattsperprimitive(PR_ARITY, InternalRendAtts())
{
_tr.SetIdentity();
_bo[INT_ATT_NAMES::ATT_VERTPOSITION] = new GLBufferObject(3, GL_FLOAT, GL_VERTEX_ARRAY, GL_ARRAY_BUFFER);
_bo[INT_ATT_NAMES::ATT_VERTNORMAL] = new GLBufferObject(3, GL_FLOAT, GL_NORMAL_ARRAY, GL_ARRAY_BUFFER);
_bo[INT_ATT_NAMES::ATT_FACENORMAL] = new GLBufferObject(3, GL_FLOAT, GL_NORMAL_ARRAY, GL_ARRAY_BUFFER);
_bo[INT_ATT_NAMES::ATT_VERTCOLOR] = new GLBufferObject(4, GL_UNSIGNED_BYTE, GL_COLOR_ARRAY, GL_ARRAY_BUFFER);
_bo[INT_ATT_NAMES::ATT_FACECOLOR] = new GLBufferObject(4, GL_UNSIGNED_BYTE, GL_COLOR_ARRAY, GL_ARRAY_BUFFER);
/*MESHCOLOR has not a buffer object associated with it. It's just a call to glColor3f. it's anyway added to the _bo arrays for sake of coherence*/
//_bo[INT_ATT_NAMES::ATT_FIXEDCOLOR] = NULL;
_bo[INT_ATT_NAMES::ATT_VERTTEXTURE] = new GLBufferObject(2, GL_FLOAT, GL_TEXTURE_COORD_ARRAY, GL_ARRAY_BUFFER);
_bo[INT_ATT_NAMES::ATT_WEDGETEXTURE] = new GLBufferObject(2, GL_FLOAT, GL_TEXTURE_COORD_ARRAY, GL_ARRAY_BUFFER);
_bo[INT_ATT_NAMES::ATT_VERTINDICES] = new GLBufferObject(3, GL_UNSIGNED_INT, GL_ELEMENT_ARRAY_BUFFER);
_bo[INT_ATT_NAMES::ATT_EDGEINDICES] = new GLBufferObject(2, GL_UNSIGNED_INT, GL_ELEMENT_ARRAY_BUFFER);
initMeaningfulAttsMask();
}
~NotThreadSafeGLMeshAttributesMultiViewerBOManager()
{
_edge.clear();
for (size_t ii = 0; ii < _bo.size(); ++ii)
delete _bo[ii];
_bo.clear();
}
/*MeshAttributesUpdate will force the buffer allocation only of the bo rendered at least by one viewer. */
/*If a filter add to a mesh, for instance, a per vertex color attribute that was not previously rendered, the meshAttributesUpdate() will ignore the attribute until a viewer require explicitly to render the per-vertex-color, too*/
/*In order to do it, please, call the setPerViewRendAtts() setting up for at least one the existing viewer (or adding a new viewer) in the RendAtts reqatts parameter the reqatts[ATT_VERTCOLOR] to true value. */
void meshAttributesUpdated(bool hasmeshconnectivitychanged, const RendAtts& changedrendatts)
{
InternalRendAtts tobeupdated(changedrendatts);
tobeupdated[INT_ATT_NAMES::ATT_VERTINDICES] = hasmeshconnectivitychanged;
tobeupdated[INT_ATT_NAMES::ATT_EDGEINDICES] = hasmeshconnectivitychanged;
for (unsigned int ii = 0; ii < INT_ATT_NAMES::enumArity(); ++ii)
{
INT_ATT_NAMES boname(ii);
if (_bo[boname] != NULL)
_bo[boname]->_isvalid = (_bo[boname]->_isvalid) && !(tobeupdated[boname]);
}
}
bool getPerViewInfo(UNIQUE_VIEW_ID_TYPE viewid, PVData& data) const
{
typename ViewsMap::const_iterator it = _perviewreqatts.find(viewid);
if (it == _perviewreqatts.end())
return false;
data = it->second;
return true;
}
void setPerViewInfo(UNIQUE_VIEW_ID_TYPE viewid, const PVData& data)
{
///cleanup stage...if an attribute impossible for a primitive modality is still here (it should not be...) we change the required atts into the view
PVData copydt(data);
for (PRIMITIVE_MODALITY pm = PRIMITIVE_MODALITY(0); pm < PR_ARITY; pm = next(pm))
copydt._intatts[pm] = InternalRendAtts::intersectionSet(copydt._intatts[size_t(pm)], _meaningfulattsperprimitive[size_t(pm)]);
_perviewreqatts[viewid] = copydt;
}
void setPerAllViewsInfo(const PVData& data)
{
///cleanup stage...if an attribute impossible for a primitive modality is still here (it should not be...) we change the required atts into the view
PVData copydt(data);
for (PRIMITIVE_MODALITY pm = PRIMITIVE_MODALITY(0); pm < PR_ARITY; pm = next(pm))
copydt._intatts[pm] = InternalRendAtts::intersectionSet(copydt._intatts[size_t(pm)], _meaningfulattsperprimitive[size_t(pm)]);
for (typename ViewsMap::iterator it = _perviewreqatts.begin(); it != _perviewreqatts.end(); ++it)
it->second = copydt;
}
bool removeView(UNIQUE_VIEW_ID_TYPE viewid)
{
typename ViewsMap::iterator it = _perviewreqatts.find(viewid);
if (it == _perviewreqatts.end())
return false;
_perviewreqatts.erase(viewid);
return true;
}
void removeAllViews()
{
_perviewreqatts.clear();
}
void draw(UNIQUE_VIEW_ID_TYPE viewid, const std::vector<GLuint>& textid = std::vector<GLuint>()) const
{
typename ViewsMap::const_iterator it = _perviewreqatts.find(viewid);
if (it == _perviewreqatts.end())
return;
const PVData& dt = it->second;
//const InternalRendAtts& atts = it->second._intatts;
drawFun(dt, textid);
}
void drawAllocatedAttributesSubset(UNIQUE_VIEW_ID_TYPE viewid, const PVData& dt, const std::vector<GLuint>& textid = std::vector<GLuint>()) const
{
typename ViewsMap::const_iterator it = _perviewreqatts.find(viewid);
if (it == _perviewreqatts.end())
return;
PVData tmp = dt;
if (!(_currallocatedboatt[INT_ATT_NAMES::ATT_VERTPOSITION]))
{
for (PRIMITIVE_MODALITY pm = PRIMITIVE_MODALITY(0); pm < PR_ARITY; pm = next(pm))
{
tmp._pmmask[size_t(pm)] = 0;
tmp._intatts[size_t(pm)] = InternalRendAtts();
}
}
else
{
for (PRIMITIVE_MODALITY pm = PRIMITIVE_MODALITY(0); pm < PR_ARITY; pm = next(pm))
{
tmp._intatts[size_t(pm)] = InternalRendAtts::intersectionSet(tmp._intatts[size_t(pm)], _meaningfulattsperprimitive[size_t(pm)]);
tmp._intatts[size_t(pm)] = InternalRendAtts::intersectionSet(tmp._intatts[size_t(pm)], _currallocatedboatt);
}
}
drawFun(dt, textid);
}
bool isBORenderingAvailable() const
{
return _borendering;
}
bool manageBuffers()
{
InternalRendAtts tobeallocated;
InternalRendAtts tobedeallocated;
InternalRendAtts tobeupdated;
bool correctlyallocated = false;
bool arebuffersok = checkBuffersAllocationStatus(tobeallocated, tobedeallocated, tobeupdated);
if (!arebuffersok)
correctlyallocated = manageAndFeedBuffersIfNeeded(tobeallocated, tobedeallocated, tobeupdated);
if (_debugmode)
debug(tobeallocated, tobedeallocated, tobeupdated);
return (arebuffersok || correctlyallocated);
}
void setGLOptions(UNIQUE_VIEW_ID_TYPE viewid, const GL_OPTIONS_DERIVED_TYPE& opts)
{
typename ViewsMap::iterator it = _perviewreqatts.find(viewid);
if (it == _perviewreqatts.end())
return;
it->second.set(opts);
}
void setTrMatrix(const vcg::Matrix44<typename MESH_TYPE::ScalarType>& tr)
{
_tr = tr;
}
void setDebugMode(bool isdebug)
{
_debugmode = isdebug;
}
void getLog(DebugInfo& info)
{
info.reset();
info._tobedeallocated = _loginfo._tobedeallocated;
info._tobeallocated = _loginfo._tobeallocated;
info._tobeupdated = _loginfo._tobeupdated;
info._currentlyallocated = _loginfo._currentlyallocated;
info._perviewdata = _loginfo._perviewdata;
_loginfo.reset();
}
private:
void initMeaningfulAttsMask()
{
_meaningfulattsperprimitive[PR_POINTS][INT_ATT_NAMES::ATT_VERTPOSITION] = true;
_meaningfulattsperprimitive[PR_POINTS][INT_ATT_NAMES::ATT_VERTNORMAL] = true;
_meaningfulattsperprimitive[PR_POINTS][INT_ATT_NAMES::ATT_VERTCOLOR] = true;
//_meaningfulattsperprimitive[PR_POINTS][INT_ATT_NAMES::ATT_FIXEDCOLOR] = true;
_meaningfulattsperprimitive[PR_POINTS][INT_ATT_NAMES::ATT_VERTTEXTURE] = true;
_meaningfulattsperprimitive[PR_WIREFRAME_EDGES][INT_ATT_NAMES::ATT_VERTPOSITION] = true;
_meaningfulattsperprimitive[PR_WIREFRAME_EDGES][INT_ATT_NAMES::ATT_VERTNORMAL] = true;
_meaningfulattsperprimitive[PR_WIREFRAME_EDGES][INT_ATT_NAMES::ATT_VERTCOLOR] = true;
//_meaningfulattsperprimitive[PR_WIREFRAME_EDGES][INT_ATT_NAMES::ATT_FIXEDCOLOR] = true;
_meaningfulattsperprimitive[PR_WIREFRAME_EDGES][INT_ATT_NAMES::ATT_EDGEINDICES] = true;
_meaningfulattsperprimitive[PR_WIREFRAME_TRIANGLES][INT_ATT_NAMES::ATT_VERTPOSITION] = true;
_meaningfulattsperprimitive[PR_WIREFRAME_TRIANGLES][INT_ATT_NAMES::ATT_VERTNORMAL] = true;
_meaningfulattsperprimitive[PR_WIREFRAME_TRIANGLES][INT_ATT_NAMES::ATT_VERTCOLOR] = true;
//_meaningfulattsperprimitive[PR_WIREFRAME_TRIANGLES][INT_ATT_NAMES::ATT_FIXEDCOLOR] = true;
_meaningfulattsperprimitive[PR_WIREFRAME_TRIANGLES][INT_ATT_NAMES::ATT_VERTINDICES] = true;
_meaningfulattsperprimitive[PR_SOLID][INT_ATT_NAMES::ATT_VERTPOSITION] = true;
_meaningfulattsperprimitive[PR_SOLID][INT_ATT_NAMES::ATT_VERTNORMAL] = true;
_meaningfulattsperprimitive[PR_SOLID][INT_ATT_NAMES::ATT_FACENORMAL] = true;
_meaningfulattsperprimitive[PR_SOLID][INT_ATT_NAMES::ATT_VERTCOLOR] = true;
_meaningfulattsperprimitive[PR_SOLID][INT_ATT_NAMES::ATT_FACECOLOR] = true;
//_meaningfulattsperprimitive[PR_SOLID][INT_ATT_NAMES::ATT_FIXEDCOLOR] = true;
_meaningfulattsperprimitive[PR_SOLID][INT_ATT_NAMES::ATT_VERTTEXTURE] = true;
_meaningfulattsperprimitive[PR_SOLID][INT_ATT_NAMES::ATT_WEDGETEXTURE] = true;
_meaningfulattsperprimitive[PR_SOLID][INT_ATT_NAMES::ATT_VERTINDICES] = true;
}
bool hasMeshAttribute(INT_ATT_NAMES attname) const
{
switch (attname)
{
case(INT_ATT_NAMES::ATT_VERTPOSITION):
return true;
case(INT_ATT_NAMES::ATT_VERTNORMAL):
return vcg::tri::HasPerVertexNormal(_mesh);
case(INT_ATT_NAMES::ATT_FACENORMAL):
return vcg::tri::HasPerFaceNormal(_mesh);
case(INT_ATT_NAMES::ATT_VERTCOLOR):
return vcg::tri::HasPerVertexColor(_mesh);
case(INT_ATT_NAMES::ATT_FACECOLOR):
return vcg::tri::HasPerFaceColor(_mesh);
/*case(INT_ATT_NAMES::ATT_FIXEDCOLOR):
return true;*/
case(INT_ATT_NAMES::ATT_VERTTEXTURE):
return vcg::tri::HasPerVertexTexCoord(_mesh);
case(INT_ATT_NAMES::ATT_WEDGETEXTURE):
return vcg::tri::HasPerWedgeTexCoord(_mesh);
case(INT_ATT_NAMES::ATT_VERTINDICES):
return (_mesh.VN() != 0) && (_mesh.FN() != 0);
case(INT_ATT_NAMES::ATT_EDGEINDICES):
return vcg::tri::HasPerVertexFlags(_mesh) || ((_mesh.VN() != 0) && (_mesh.FN() == 0) && (_mesh.EN() == 0));
default:
return false;
}
return false;
}
bool checkBuffersAllocationStatus(InternalRendAtts& tobeallocated, InternalRendAtts& tobedeallocated, InternalRendAtts& tobeupdated) const
{
bool somethingtodo = false;
tobedeallocated.reset();
tobedeallocated.reset();
tobeupdated.reset();
//bool thereisreplicatedview = isThereAReplicatedPipelineView();
InternalRendAtts meaningfulrequiredbyatleastoneview;
InternalRendAtts probabilyuseless;
for (typename ViewsMap::const_iterator it = _perviewreqatts.begin(); it != _perviewreqatts.end(); ++it)
{
for (PRIMITIVE_MODALITY pm = PRIMITIVE_MODALITY(0); pm < PR_ARITY; pm = next(pm))
{
//If a primitive_modality is not rendered (== no att_VERTPOSITION) all the referred attributes by this view can be eventually deallocated IF they are not used
//by some other rendered primitive
//the vertindices is, as usual, a different case
if (it->second._intatts[size_t(pm)][INT_ATT_NAMES::ATT_VERTPOSITION])
meaningfulrequiredbyatleastoneview = InternalRendAtts::unionSet(meaningfulrequiredbyatleastoneview, it->second._intatts[size_t(pm)]);
else
probabilyuseless = InternalRendAtts::unionSet(probabilyuseless, it->second._intatts[size_t(pm)]);
}
}
bool thereisreplicatedview = InternalRendAtts::replicatedPipelineNeeded(meaningfulrequiredbyatleastoneview);
meaningfulrequiredbyatleastoneview[INT_ATT_NAMES::ATT_VERTINDICES] &= !thereisreplicatedview;
InternalRendAtts reallyuseless = InternalRendAtts::complementSet(probabilyuseless, meaningfulrequiredbyatleastoneview);
bool switchreplicatedindexed = (!InternalRendAtts::replicatedPipelineNeeded(_currallocatedboatt) && thereisreplicatedview) || (InternalRendAtts::replicatedPipelineNeeded(_currallocatedboatt) && !thereisreplicatedview);
/*in some way the vertices number changed. If i use the indexed pipeline i have to deallocate/allocate/update the vertex indices*/
bool numvertchanged = boExpectedSize(INT_ATT_NAMES::ATT_VERTPOSITION, thereisreplicatedview) != _bo[INT_ATT_NAMES::ATT_VERTPOSITION]->_size;
bool vertindforcedupdate = numvertchanged && meaningfulrequiredbyatleastoneview[INT_ATT_NAMES::ATT_VERTINDICES];
InternalRendAtts probablytoallocate = InternalRendAtts::complementSet(meaningfulrequiredbyatleastoneview, _currallocatedboatt);
InternalRendAtts probablytodeallocate = InternalRendAtts::complementSet(_currallocatedboatt, meaningfulrequiredbyatleastoneview);
for (unsigned int ii = 0; ii < INT_ATT_NAMES::enumArity(); ++ii)
{
INT_ATT_NAMES boname(ii);
if (_bo[boname] != NULL)
{
bool hasmeshattribute = hasMeshAttribute(boname);
bool isvalid = (_bo[boname]->_isvalid);
bool notempty = (_bo[boname]->_size > 0);
if (boname != INT_ATT_NAMES::ATT_EDGEINDICES)
{
size_t sz = boExpectedSize(boname, thereisreplicatedview);
tobedeallocated[boname] = (notempty && !hasmeshattribute) ||
(notempty && probablytodeallocate[boname]) ||
(notempty && reallyuseless[boname]) ||
(notempty && (_bo[boname]->_size != sz) && meaningfulrequiredbyatleastoneview[boname]) ||
(notempty && (boname == INT_ATT_NAMES::ATT_VERTINDICES) && (vertindforcedupdate));
tobeallocated[boname] = (hasmeshattribute && (sz > 0) && (sz != _bo[boname]->_size) && meaningfulrequiredbyatleastoneview[boname]) ||
(hasmeshattribute && (sz > 0) && probablytoallocate[boname]) ||
(hasmeshattribute && (boname == INT_ATT_NAMES::ATT_VERTINDICES) && (vertindforcedupdate));
tobeupdated[boname] = tobeallocated[boname] || (hasmeshattribute && (sz > 0) && !(isvalid) && meaningfulrequiredbyatleastoneview[boname]);
}
else
{
bool meshchanged = ((size_t(_mesh.FN()) != _meshtriangleswhenedgeindiceswerecomputed) || (size_t(_mesh.VN()) != _meshverticeswhenedgeindiceswerecomputed));
tobedeallocated[INT_ATT_NAMES::ATT_EDGEINDICES] = (notempty && !hasmeshattribute) ||
(notempty && !meaningfulrequiredbyatleastoneview[INT_ATT_NAMES::ATT_EDGEINDICES]) ||
(notempty && !(isvalid) && meshchanged);
tobeallocated[INT_ATT_NAMES::ATT_EDGEINDICES] = (hasmeshattribute && meaningfulrequiredbyatleastoneview[INT_ATT_NAMES::ATT_EDGEINDICES] && !(isvalid) && (meshchanged)) ||
(hasmeshattribute && meaningfulrequiredbyatleastoneview[INT_ATT_NAMES::ATT_EDGEINDICES] && !(isvalid) && !(_currallocatedboatt[INT_ATT_NAMES::ATT_EDGEINDICES]));
tobeupdated[INT_ATT_NAMES::ATT_EDGEINDICES] = tobeallocated[INT_ATT_NAMES::ATT_EDGEINDICES] ||
(hasmeshattribute && !(isvalid) && meaningfulrequiredbyatleastoneview[INT_ATT_NAMES::ATT_EDGEINDICES]) ||
(hasmeshattribute && switchreplicatedindexed && meaningfulrequiredbyatleastoneview[INT_ATT_NAMES::ATT_EDGEINDICES]);
}
}
somethingtodo = somethingtodo || tobeallocated[boname] || tobedeallocated[boname] || tobeupdated[boname];
}
return !(somethingtodo);
}
bool manageAndFeedBuffersIfNeeded(const InternalRendAtts& tobeallocated, const InternalRendAtts& tobedeallocated, const InternalRendAtts& tobeupdated)
{
if (tobeupdated[INT_ATT_NAMES::ATT_EDGEINDICES])
updateEdgeVertIndVector();
bool immediatemode = !(buffersMemoryManagementFunction(tobeallocated, tobedeallocated, tobeupdated));
bool replicated = isThereAReplicatedPipelineView();
if (immediatemode)
return false;
bool somethingtoupdate = false;
for (unsigned int hh = 0; hh < INT_ATT_NAMES::enumArity(); ++hh)
somethingtoupdate = somethingtoupdate || tobeupdated[hh];
if (somethingtoupdate)
{
if (replicated)
{
InternalRendAtts attributestobeupdated(tobeupdated);
//WARNING!In case we have to update the wedgetexture bo maybe (not always!) we must update also the other buffer already in memory
//cause the wedgetexture pipeline force a change in the order of the triangles in GPU.
//they are now ordered by the texture seam and not more by the triangle index!
if (tobeupdated[INT_ATT_NAMES::ATT_WEDGETEXTURE])
attributestobeupdated = _currallocatedboatt;
updateBuffersReplicatedPipeline(attributestobeupdated);
}
else
updateBuffersIndexedPipeline(tobeupdated);
glFinish();
}
return true;
}
void updateEdgeVertIndVector()
{
_edge.clear();
fillUniqueEdgeVector(_mesh, _edge);
_meshverticeswhenedgeindiceswerecomputed = _mesh.VN();
_meshtriangleswhenedgeindiceswerecomputed = _mesh.FN();
}
bool buffersMemoryManagementFunction(const InternalRendAtts& tobeallocated, const InternalRendAtts& tobedeallocated, const InternalRendAtts& tobeupdated)
{
//GLenum err = glGetError();
bool replicated = isThereAReplicatedPipelineView();
std::ptrdiff_t newallocatedmem = bufferObjectsMemoryRequired(tobeallocated);
std::ptrdiff_t deallocatedmem = bufferObjectsMemoryRequired(tobedeallocated);
ptrdiff_t zero = 0;
std::ptrdiff_t changedsize = std::max(zero, newallocatedmem - deallocatedmem);
//std::ptrdiff_t bomemoryrequiredbymesh = bufferObjectsMemoryRequired(_currallocatedboatt) - deallocatedmem + newallocatedmem;
unsigned int ii = 0;
for (typename std::vector<GLBufferObject*>::iterator it = _bo.begin(); it != _bo.end(); ++it)
{
INT_ATT_NAMES boname(ii);
//size_t sz = boExpectedSize(boname,replicated);
//size_t dim = boExpectedDimension(boname,replicated);
if (tobedeallocated[boname])
bufferDeAllocationRequested(boname);
++ii;
}
if (!_gpumeminfo.isAdditionalMemoryAvailable(changedsize))
{
std::cout << "no additional memory available!!! memory required: " << changedsize << std::endl;
ii = 0;
for (typename std::vector<GLBufferObject*>::iterator it = _bo.begin(); it != _bo.end(); ++it)
{
INT_ATT_NAMES boname(ii);
size_t sz(boExpectedSize(boname, replicated));
//there are already valid mesh attributes properly allocated in memory but there is not enough gpu memory for the remaining mesh.
//we have to deallocate the previously allocated mesh attributes
if ((*it != NULL) && ((sz == (*it)->_size)))
bufferDeAllocationRequested(boname);
++ii;
}
_borendering = false;
return false;
}
else
{
bool failedallocation = false;
unsigned int ii = 0;
typename std::vector<GLBufferObject*>::iterator it = _bo.begin();
while ((it != _bo.end()) && (!failedallocation))
{
INT_ATT_NAMES boname(ii);
GLBufferObject* cbo = _bo[ii];
if (tobeallocated[boname])
{
cbo->_size = boExpectedSize(boname, replicated);
std::ptrdiff_t dim = boExpectedDimension(boname, replicated);
glGenBuffers(1, &cbo->_bohandle);
glBindBuffer(cbo->_target, cbo->_bohandle);
//we call glGetError BEFORE the glBufferData function in order to clean the error flag
GLenum err = glGetError();
//assert(err == GL_NO_ERROR);
glBufferData(cbo->_target, dim, NULL, GL_STATIC_DRAW);
err = glGetError();
//even if there according the MemoryInfo subclass there is enough space we were not able to allocate an attribute buffer object. We have to deallocate all the bos related to this mesh
failedallocation = (err == GL_OUT_OF_MEMORY) || (!_gpumeminfo.isAdditionalMemoryAvailable(dim));
if (!failedallocation)
{
//setBufferPointerEnableClientState(boname);
setBufferPointer(boname);
_gpumeminfo.acquiredMemory(dim);
}
cbo->_isvalid = !failedallocation;
_borendering = !failedallocation;
glBindBuffer(cbo->_target, 0);
_currallocatedboatt[boname] = !failedallocation;
}
else
{
//the arity of the attribute contained in the bo didn't change so i can use the old space without reallocating it
if (cbo != NULL)
cbo->_isvalid = cbo->_isvalid || tobeupdated[boname];
}
++it;
++ii;
}
if (failedallocation)
buffersDeAllocationRequested(_currallocatedboatt);
_borendering = !failedallocation;
}
return _borendering;
}
bool updateBuffersIndexedPipeline(const InternalRendAtts& attributestobeupdated)
{
_chunkmap.clear();
size_t vn = _mesh.VN();
size_t tn = _mesh.FN();
size_t facechunk = std::min(size_t(tn), _perbatchprim);
size_t vertexchunk = std::min(size_t(vn), _perbatchprim);
std::vector<vcg::Point3f> pv; //position vector
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTPOSITION])
pv.resize(vertexchunk);
std::vector<vcg::Point3f> nv; //per vertex normal vector
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTNORMAL])
nv.resize(vertexchunk);
std::vector<vcg::Color4b> cv; // Per vertex color vector
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTCOLOR])
cv.resize(vertexchunk);
std::vector<float> tv; // per vertex texture coord vector
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTTEXTURE])
tv.resize(vertexchunk * 2);
size_t chunkingpu = 0;
for (size_t i = 0; i < vn; ++i)
{
size_t chunkindex = i % vertexchunk;
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTPOSITION])
pv[chunkindex].Import(_mesh.vert[i].cP());
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTNORMAL])
{
nv[chunkindex].Import(_mesh.vert[i].cN());
nv[chunkindex].Normalize();
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTCOLOR])
{
cv[chunkindex] = _mesh.vert[i].cC();
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTTEXTURE])
{
tv[chunkindex * 2 + 0] = _mesh.vert[i].cT().U();
tv[chunkindex * 2 + 1] = _mesh.vert[i].cT().V();
}
if ((i == vn - 1) || (chunkindex == vertexchunk - 1))
{
size_t chunksize = vertexchunk;
if (i == vn - 1)
chunksize = chunkindex + 1;
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTPOSITION])
{
GLBufferObject* buffobj = _bo[INT_ATT_NAMES::ATT_VERTPOSITION];
glBindBuffer(GL_ARRAY_BUFFER, buffobj->_bohandle);
glBufferSubData(GL_ARRAY_BUFFER, chunkingpu * vertexchunk * buffobj->_components * buffobj->getSizeOfGLType(), buffobj->_components * buffobj->getSizeOfGLType() * chunksize, &pv[0]);
//std::vector<vcg::Point3f> tmppv; //position vector
//if (attributestobeupdated[GLMeshAttributesInfo::ATT_VERTPOSITION])
// tmppv.resize(vertexchunk);
//glGetBufferSubData(GL_ARRAY_BUFFER,0,buffobj->_components * buffobj->getSizeOfGLType() * chunksize,&tmppv[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTNORMAL])
{
GLBufferObject* buffobj = _bo[INT_ATT_NAMES::ATT_VERTNORMAL];
glBindBuffer(GL_ARRAY_BUFFER, buffobj->_bohandle);
glBufferSubData(GL_ARRAY_BUFFER, chunkingpu * vertexchunk * buffobj->_components * buffobj->getSizeOfGLType(), buffobj->_components * buffobj->getSizeOfGLType() * chunksize, &nv[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTCOLOR])
{
GLBufferObject* buffobj = _bo[INT_ATT_NAMES::ATT_VERTCOLOR];
glBindBuffer(GL_ARRAY_BUFFER, buffobj->_bohandle);
glBufferSubData(GL_ARRAY_BUFFER, chunkingpu * vertexchunk * buffobj->_components * buffobj->getSizeOfGLType(), buffobj->_components * buffobj->getSizeOfGLType() * chunksize, &cv[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTTEXTURE])
{
GLBufferObject* buffobj = _bo[INT_ATT_NAMES::ATT_VERTTEXTURE];
glBindBuffer(GL_ARRAY_BUFFER, buffobj->_bohandle);
glBufferSubData(GL_ARRAY_BUFFER, chunkingpu * vertexchunk * buffobj->_components * buffobj->getSizeOfGLType(), buffobj->_components * buffobj->getSizeOfGLType() * chunksize, &tv[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
glFinish();
++chunkingpu;
}
}
pv.clear();
nv.clear();
cv.clear();
tv.clear();
chunkingpu = 0;
std::vector<GLuint> ti(facechunk * 3);
for (size_t i = 0; i < tn; ++i)
{
size_t chunkindex = i % facechunk;
ti[chunkindex * 3 + 0] = GLuint(vcg::tri::Index(_mesh, _mesh.face[i].V(0)));
ti[chunkindex * 3 + 1] = GLuint(vcg::tri::Index(_mesh, _mesh.face[i].V(1)));
ti[chunkindex * 3 + 2] = GLuint(vcg::tri::Index(_mesh, _mesh.face[i].V(2)));
if ((i == tn - 1) || (chunkindex == facechunk - 1))
{
size_t chunksize = facechunk;
if (i == tn - 1)
chunksize = chunkindex + 1;
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTINDICES])
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _bo[INT_ATT_NAMES::ATT_VERTINDICES]->_bohandle);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, chunkingpu * facechunk * _bo[INT_ATT_NAMES::ATT_VERTINDICES]->_components * _bo[INT_ATT_NAMES::ATT_VERTINDICES]->getSizeOfGLType(), _bo[INT_ATT_NAMES::ATT_VERTINDICES]->_components * _bo[INT_ATT_NAMES::ATT_VERTINDICES]->getSizeOfGLType() * chunksize, &ti[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
++chunkingpu;
}
}
if ((attributestobeupdated[INT_ATT_NAMES::ATT_EDGEINDICES]) && (_edge.size() > 0))
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _bo[INT_ATT_NAMES::ATT_EDGEINDICES]->_bohandle);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, _bo[INT_ATT_NAMES::ATT_EDGEINDICES]->_components * _edge.size() * _bo[INT_ATT_NAMES::ATT_EDGEINDICES]->getSizeOfGLType(), &_edge[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
return true;
}
bool isThereAReplicatedPipelineView() const
{
bool replicated = false;
for (typename ViewsMap::const_iterator it = _perviewreqatts.begin(); it != _perviewreqatts.end(); ++it)
{
//There is a replicated pipeline only if the att[ATT_VERTPOSITION] is true, otherwise is a spurious replicated view
for (size_t pm = 0; pm < size_t(PR_ARITY); ++pm)
replicated = replicated || (InternalRendAtts::replicatedPipelineNeeded(it->second._intatts[pm]) && (it->second._pmmask.test(pm)));
}
return replicated;
}
bool isThereAnEdgesView() const
{
bool isthereaquadview = false;
for (typename ViewsMap::const_iterator it = _perviewreqatts.begin(); it != _perviewreqatts.end(); ++it)
isthereaquadview = (it->second._intatts[size_t(PR_WIREFRAME_EDGES)][INT_ATT_NAMES::ATT_VERTPOSITION]) || isthereaquadview;
return isthereaquadview;
}
bool updateBuffersReplicatedPipeline(const InternalRendAtts& attributestobeupdated)
{
size_t tn = _mesh.fn;
size_t facechunk = std::min(size_t(tn), _perbatchprim);
std::vector<vcg::Point3f> rpv; //position vector
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTPOSITION])
rpv.resize(facechunk * 3);
std::vector<vcg::Point3f> rnv; //per vertex normal vector
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTNORMAL])
rnv.resize(facechunk * 3);
std::vector<vcg::Point3f> rfnv; //per face normal vector
if (attributestobeupdated[INT_ATT_NAMES::ATT_FACENORMAL])
rfnv.resize(facechunk * 3);
std::vector<vcg::Color4b> rcv; // Per vertex color vector
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTCOLOR])
rcv.resize(facechunk * 3);
std::vector<vcg::Color4b> rfcv; // Per vertex color vector
if (attributestobeupdated[INT_ATT_NAMES::ATT_FACECOLOR])
rfcv.resize(facechunk * 3);
std::vector<float> rtv; // per vertex texture coord vector
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTTEXTURE])
rtv.resize(facechunk * 3 * 2);
std::vector<float> rwtv; // per wedge texture coord vector
if (attributestobeupdated[INT_ATT_NAMES::ATT_WEDGETEXTURE])
rwtv.resize(facechunk * 3 * 2);
size_t chunkingpu = 0;
//it's a map containing for each texture seams n a vector of all the triangle index ranges having n has texture seam
//Suppose that in a mesh we have
//TXS_0{t0,t1,t2,t3}, TXS_4{t4,t5},TXS_0{t6},TXS_-1{t7,t8,t9},TXS_4{t10,t11}
//so chunkMap will contain
// -1 -> [<t7,t9>]
// 0 -> [<t0,t3>,<t6,t6>]
// 4 -> [<t4,t5>,<t10,t11>]
//
//if the map has no-texture coords at all in order to unify the code we fill the ChunkMap with texture seam -1 and a single triangle range going from face_0 to face_n-1
if (attributestobeupdated[INT_ATT_NAMES::ATT_WEDGETEXTURE] || attributestobeupdated[INT_ATT_NAMES::ATT_VERTTEXTURE])
{
_chunkmap.clear();
if (attributestobeupdated[INT_ATT_NAMES::ATT_WEDGETEXTURE])
fillchunkMap();
else
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTTEXTURE])
_chunkmap[0].push_back(std::make_pair(0, tn - 1));
}
//default case: no texture is required to be rendered but a non texture attribute has to be updated
//we have to init the _chunkmap with just one entry (-1...that means no texture) referring all the triangles in the mesh
if ((!_currallocatedboatt[INT_ATT_NAMES::ATT_VERTTEXTURE] && !_currallocatedboatt[INT_ATT_NAMES::ATT_WEDGETEXTURE]) &&
(attributestobeupdated[INT_ATT_NAMES::ATT_VERTPOSITION] ||
attributestobeupdated[INT_ATT_NAMES::ATT_VERTNORMAL] || attributestobeupdated[INT_ATT_NAMES::ATT_FACENORMAL] ||
attributestobeupdated[INT_ATT_NAMES::ATT_VERTCOLOR] || attributestobeupdated[INT_ATT_NAMES::ATT_FACECOLOR]))
{
_chunkmap.clear();
_chunkmap[-1].push_back(std::make_pair(0, tn - 1));
}
int t = 0;
if (attributestobeupdated[INT_ATT_NAMES::ATT_WEDGETEXTURE] || attributestobeupdated[INT_ATT_NAMES::ATT_VERTTEXTURE])
{
_texindnumtriangles.clear();
_texindnumtriangles.resize(_chunkmap.size());
}
std::vector<GLuint> vpatlas;
if (attributestobeupdated[INT_ATT_NAMES::ATT_EDGEINDICES])
vpatlas.resize(_mesh.VN(), UINT_MAX);
int faceind = 0;
size_t chunkindex = faceind;
GLuint triangles = 0;
for (ChunkMap::const_iterator mit = _chunkmap.begin(); mit != _chunkmap.end(); ++mit)
{
for (ChunkVector::const_iterator cit = mit->second.begin(); cit != mit->second.end(); ++cit)
{
for (size_t indf = cit->first; indf <= cit->second; ++indf)
{
chunkindex = faceind % facechunk;
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTPOSITION])
{
rpv[chunkindex * 3 + 0].Import(_mesh.face[indf].V(0)->P());
rpv[chunkindex * 3 + 1].Import(_mesh.face[indf].V(1)->P());
rpv[chunkindex * 3 + 2].Import(_mesh.face[indf].V(2)->P());
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTNORMAL])
{
rnv[chunkindex * 3 + 0].Import(_mesh.face[indf].V(0)->N().Normalize());
rnv[chunkindex * 3 + 1].Import(_mesh.face[indf].V(1)->N().Normalize());
rnv[chunkindex * 3 + 2].Import(_mesh.face[indf].V(2)->N().Normalize());
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_FACENORMAL])
{
rfnv[chunkindex * 3 + 0].Import(_mesh.face[indf].N().Normalize());
rfnv[chunkindex * 3 + 1].Import(_mesh.face[indf].N().Normalize());
rfnv[chunkindex * 3 + 2].Import(_mesh.face[indf].N().Normalize());
}
if ((attributestobeupdated[INT_ATT_NAMES::ATT_VERTCOLOR]))
{
rcv[chunkindex * 3 + 0] = _mesh.face[indf].V(0)->C();
rcv[chunkindex * 3 + 1] = _mesh.face[indf].V(1)->C();
rcv[chunkindex * 3 + 2] = _mesh.face[indf].V(2)->C();
}
if ((attributestobeupdated[INT_ATT_NAMES::ATT_FACECOLOR]))
{
rfcv[chunkindex * 3 + 0] = _mesh.face[indf].C();
rfcv[chunkindex * 3 + 1] = _mesh.face[indf].C();
rfcv[chunkindex * 3 + 2] = _mesh.face[indf].C();
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTTEXTURE])
{
rtv[chunkindex * 6 + 0] = float(_mesh.face[indf].V(0)->T().U());
rtv[chunkindex * 6 + 1] = float(_mesh.face[indf].V(0)->T().V());
rtv[chunkindex * 6 + 2] = float(_mesh.face[indf].V(1)->T().U());
rtv[chunkindex * 6 + 3] = float(_mesh.face[indf].V(1)->T().V());
rtv[chunkindex * 6 + 4] = float(_mesh.face[indf].V(2)->T().U());
rtv[chunkindex * 6 + 5] = float(_mesh.face[indf].V(2)->T().V());
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_WEDGETEXTURE])
{
rwtv[chunkindex * 6 + 0] = float(_mesh.face[indf].WT(0).U());
rwtv[chunkindex * 6 + 1] = float(_mesh.face[indf].WT(0).V());
rwtv[chunkindex * 6 + 2] = float(_mesh.face[indf].WT(1).U());
rwtv[chunkindex * 6 + 3] = float(_mesh.face[indf].WT(1).V());
rwtv[chunkindex * 6 + 4] = float(_mesh.face[indf].WT(2).U());
rwtv[chunkindex * 6 + 5] = float(_mesh.face[indf].WT(2).V());
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_EDGEINDICES])
{
for (int ii = 0; ii < 3; ++ii)
{
size_t v = vcg::tri::Index(_mesh, _mesh.face[indf].V(ii));
if (vpatlas[v] == UINT_MAX)
vpatlas[v] = faceind * 3 + ii;
}
}
if ((faceind == int(tn - 1)) || (chunkindex == size_t(facechunk - 1) ))
{
size_t chunksize = facechunk;
if (faceind == int(tn - 1))
chunksize = chunkindex + 1;
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTPOSITION])
{
GLBufferObject* buffobj = _bo[INT_ATT_NAMES::ATT_VERTPOSITION];
glBindBuffer(GL_ARRAY_BUFFER, buffobj->_bohandle);
glBufferSubData(GL_ARRAY_BUFFER, chunkingpu * facechunk * 3 * buffobj->_components * buffobj->getSizeOfGLType(), 3 * buffobj->_components * buffobj->getSizeOfGLType() * chunksize, &rpv[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTNORMAL])
{
GLBufferObject* buffobj = _bo[INT_ATT_NAMES::ATT_VERTNORMAL];
glBindBuffer(GL_ARRAY_BUFFER, buffobj->_bohandle);
glBufferSubData(GL_ARRAY_BUFFER, chunkingpu * facechunk * 3 * buffobj->_components * buffobj->getSizeOfGLType(), 3 * buffobj->_components * buffobj->getSizeOfGLType() * chunksize, &rnv[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_FACENORMAL])
{
GLBufferObject* buffobj = _bo[INT_ATT_NAMES::ATT_FACENORMAL];
glBindBuffer(GL_ARRAY_BUFFER, buffobj->_bohandle);
glBufferSubData(GL_ARRAY_BUFFER, chunkingpu * facechunk * 3 * buffobj->_components * buffobj->getSizeOfGLType(), 3 * buffobj->_components * buffobj->getSizeOfGLType() * chunksize, &rfnv[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTCOLOR])
{
GLBufferObject* buffobj = _bo[INT_ATT_NAMES::ATT_VERTCOLOR];
glBindBuffer(GL_ARRAY_BUFFER, buffobj->_bohandle);
glBufferSubData(GL_ARRAY_BUFFER, chunkingpu * facechunk * 3 * buffobj->_components * buffobj->getSizeOfGLType(), 3 * buffobj->_components * buffobj->getSizeOfGLType() * chunksize, &rcv[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_FACECOLOR])
{
GLBufferObject* buffobj = _bo[INT_ATT_NAMES::ATT_FACECOLOR];
glBindBuffer(GL_ARRAY_BUFFER, buffobj->_bohandle);
glBufferSubData(GL_ARRAY_BUFFER, chunkingpu * facechunk * 3 * buffobj->_components * buffobj->getSizeOfGLType(), 3 * buffobj->_components * buffobj->getSizeOfGLType() * chunksize, &rfcv[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_VERTTEXTURE])
{
GLBufferObject* buffobj = _bo[INT_ATT_NAMES::ATT_VERTTEXTURE];
glBindBuffer(GL_ARRAY_BUFFER, buffobj->_bohandle);
glBufferSubData(GL_ARRAY_BUFFER, chunkingpu * facechunk * 3 * buffobj->_components * buffobj->getSizeOfGLType(), 3 * buffobj->_components * buffobj->getSizeOfGLType() * chunksize, &rtv[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_WEDGETEXTURE])
{
GLBufferObject* buffobj = _bo[INT_ATT_NAMES::ATT_WEDGETEXTURE];
glBindBuffer(GL_ARRAY_BUFFER, buffobj->_bohandle);
glBufferSubData(GL_ARRAY_BUFFER, chunkingpu * facechunk * 3 * buffobj->_components * buffobj->getSizeOfGLType(), 3 * buffobj->_components * buffobj->getSizeOfGLType() * chunksize, &rwtv[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
++chunkingpu;
}
++faceind;
}
triangles += cit->second - cit->first + 1;
}
if ((attributestobeupdated[INT_ATT_NAMES::ATT_EDGEINDICES]) && (_edge.size() > 0))
{
for (typename std::vector<EdgeVertInd>::iterator it = _edge.begin(); it != _edge.end(); ++it)
{
it->_v[0] = vpatlas[it->_v[0]];
it->_v[1] = vpatlas[it->_v[1]];
}
GLBufferObject* buffobj = _bo[INT_ATT_NAMES::ATT_EDGEINDICES];
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffobj->_bohandle);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, buffobj->_components * buffobj->getSizeOfGLType() * _edge.size(), &_edge[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
if (attributestobeupdated[INT_ATT_NAMES::ATT_WEDGETEXTURE] || attributestobeupdated[INT_ATT_NAMES::ATT_VERTTEXTURE])
_texindnumtriangles[t] = std::make_pair(mit->first, triangles);
++t;
}
//return (k != tn)
// throw MeshLabException("Mesh has not been properly partitioned");
return true;
}
void buffersDeAllocationRequested(const InternalRendAtts& rq)
{
for (unsigned int ii = 0; ii < INT_ATT_NAMES::enumArity(); ++ii)
{
INT_ATT_NAMES boname(ii);
if ((_bo[ii] != NULL) && (rq[boname]))
bufferDeAllocationRequested(boname);
}
}
void bufferDeAllocationRequested(INT_ATT_NAMES att)
{
unsigned int ind(att);
if ((ind < 0) || (ind >= (unsigned int)_bo.size()))
return;
GLBufferObject* bobj = _bo[ind];
if (bobj == NULL)
return;
if ((att != INT_ATT_NAMES::ATT_VERTINDICES) && (att != INT_ATT_NAMES::ATT_EDGEINDICES) /*&& (att != INT_ATT_NAMES::ATT_FIXEDCOLOR)*/)
{
glDisableClientState(bobj->_clientstatetag);
}
//glBufferData(bobj->_target, sizeof(vcg::Point3f)*_primitivebatch, 0, GL_DYNAMIC_DRAW);
glDeleteBuffers(1, &(bobj->_bohandle));
glFlush();
glFinish();
if (bobj->_size > 0)
//we don't use dim cause dim is the value that is going to be allocated, instead use (*it)->_size * (*it)->getSizeOfGLType() is the value already in the buffer
_gpumeminfo.releasedMemory(bobj->_size * bobj->getSizeOfGLType());
bobj->_isvalid = false;
bobj->_size = 0;
_currallocatedboatt[att] = false;
}
std::ptrdiff_t bufferObjectsMemoryRequired(const InternalRendAtts& rqatt) const
{
bool replicated = InternalRendAtts::replicatedPipelineNeeded(rqatt);
std::ptrdiff_t result(0);
for (unsigned int ii = 0; ii < INT_ATT_NAMES::enumArity(); ++ii)
{
INT_ATT_NAMES nm(ii);
if (rqatt[nm])
result += (std::ptrdiff_t) boExpectedDimension(nm, replicated);
}
return result;
}
//expected number of cells should have the required bo
//generateindex is true when i have a triangle based mesh
// is false when i have a point based mesh
size_t boExpectedSize(INT_ATT_NAMES name, bool replicatedpipeline) const
{
try
{
GLBufferObject& cbo = *_bo.at((unsigned int)name);
size_t vertnum(_mesh.VN());
size_t facenum(_mesh.FN());
switch ((unsigned int)name)
{
case(INT_ATT_NAMES::ATT_VERTPOSITION):
case(INT_ATT_NAMES::ATT_VERTNORMAL):
case(INT_ATT_NAMES::ATT_VERTCOLOR):
case(INT_ATT_NAMES::ATT_VERTTEXTURE):
{
if (replicatedpipeline)
return facenum * 3 * cbo._components;
else
return vertnum * cbo._components;
}
case(INT_ATT_NAMES::ATT_FACENORMAL):
case(INT_ATT_NAMES::ATT_FACECOLOR):
case(INT_ATT_NAMES::ATT_WEDGETEXTURE):
{
if (replicatedpipeline)
return facenum * 3 * cbo._components;
else
return 0;
}
case(INT_ATT_NAMES::ATT_VERTINDICES):
{
if (replicatedpipeline)
return 0;
else
return facenum * cbo._components;
}
case(INT_ATT_NAMES::ATT_EDGEINDICES):
{
return _edge.size() * cbo._components;
}
default: break;
}
}
catch (std::out_of_range& /*exc*/)
{
return 0;
}
return 0;
}
size_t boExpectedDimension(INT_ATT_NAMES name, bool replicatedpipeline) const
{
try
{
size_t sz = boExpectedSize(name, replicatedpipeline);
unsigned int ind = (unsigned int)name;
GLBufferObject* cbo = _bo.at(ind);
if (cbo == NULL)
return 0;
else
return sz * cbo->getSizeOfGLType();
}
catch (std::out_of_range& /*exc*/)
{
return 0;
}
return 0;
}
void drawFun(const PVData& dt, const std::vector<GLuint>& textid = std::vector<GLuint>()) const
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glMultMatrix(_tr);
if ((dt._glopts != NULL) && (dt._glopts->_perbbox_enabled))
drawBBox(dt._glopts);
if (dt.isPrimitiveActive(PR_SOLID))
{
bool somethingmore = dt.isPrimitiveActive(PR_WIREFRAME_EDGES) || dt.isPrimitiveActive(PR_WIREFRAME_TRIANGLES) || dt.isPrimitiveActive(PR_POINTS);
if (somethingmore)
{
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0, 1);
}
drawFilledTriangles(dt._intatts[size_t(PR_SOLID)], dt._glopts, textid);
if (somethingmore)
glDisable(GL_POLYGON_OFFSET_FILL);
}
if (dt.isPrimitiveActive(PR_WIREFRAME_EDGES) || dt.isPrimitiveActive(PR_WIREFRAME_TRIANGLES))
{
//InternalRendAtts tmpatts = atts;
bool pointstoo = dt.isPrimitiveActive(PR_POINTS);
if (pointstoo)
{
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0, 1);
}
bool solidtoo = dt.isPrimitiveActive(PR_SOLID);
/*EDGE | TRI | DRAW
---------------------------------
TRUE TRUE EDGE
TRUE FALSE EDGE
FALSE TRUE TRI
FALSE FALSE NOTHING */
if (dt.isPrimitiveActive(PR_WIREFRAME_EDGES))
drawEdges(dt._intatts[size_t(PR_WIREFRAME_EDGES)], dt._glopts);
else
{
if (dt.isPrimitiveActive(PR_WIREFRAME_TRIANGLES))
{
drawWiredTriangles(dt._intatts[size_t(PR_WIREFRAME_TRIANGLES)], dt._glopts, textid);
}
}
if (pointstoo || solidtoo)
glDisable(GL_POLYGON_OFFSET_FILL);
}
if (dt.isPrimitiveActive(PR_POINTS))
drawPoints(dt._intatts[size_t(PR_POINTS)], dt._glopts, textid);
glPopMatrix();
glPopAttrib();
glFlush();
glFinish();
}
void drawFilledTriangles(const InternalRendAtts& req, const GL_OPTIONS_DERIVED_TYPE* glopts, const std::vector<GLuint>& textureindex = std::vector<GLuint>()) const
{
if (_mesh.VN() == 0)
return;
glPushAttrib(GL_ALL_ATTRIB_BITS);
bool isgloptsvalid = (glopts != NULL);
if (isgloptsvalid && glopts->_persolid_noshading)
glDisable(GL_LIGHTING);
else
if ((!isgloptsvalid) || (req[INT_ATT_NAMES::ATT_VERTNORMAL]) || (req[INT_ATT_NAMES::ATT_FACENORMAL]))
{
glEnable(GL_LIGHTING);
}
glEnable(GL_COLOR_MATERIAL);
if ((isgloptsvalid) && (glopts->_persolid_fixed_color_enabled))
glColor(glopts->_persolid_fixed_color);
else
{
if ((isgloptsvalid) && (glopts->_persolid_mesh_color_enabled))
glColor(_mesh.C());
else
{
if ((req[INT_ATT_NAMES::ATT_VERTCOLOR]) || (req[INT_ATT_NAMES::ATT_FACECOLOR]))
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
else
glColor(vcg::Color4b(vcg::Color4b::LightGray));
}
}
if (isBORenderingAvailable())
drawTrianglesBO(req, textureindex);
else
drawTrianglesIM(req, textureindex);
glPopAttrib();
}
void drawWiredTriangles(const InternalRendAtts& req, const GL_OPTIONS_DERIVED_TYPE* glopts, const std::vector<GLuint>& textureindex = std::vector<GLuint>()) const
{
if (_mesh.VN() == 0)
return;
glPushAttrib(GL_ALL_ATTRIB_BITS);
bool isgloptsvalid = (glopts != NULL);
if (isgloptsvalid && glopts->_perwire_noshading)
glDisable(GL_LIGHTING);
else
if ((!isgloptsvalid) || (req[INT_ATT_NAMES::ATT_VERTNORMAL]))
{
glEnable(GL_LIGHTING);
}
glEnable(GL_COLOR_MATERIAL);
if ((isgloptsvalid) && (glopts->_perwire_fixed_color_enabled))
glColor(glopts->_perwire_fixed_color);
else
{
if ((isgloptsvalid) && (glopts->_perwire_mesh_color_enabled))
glColor(_mesh.C());
else
{
if (req[INT_ATT_NAMES::ATT_VERTCOLOR])
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
else
glColor(vcg::Color4b(vcg::Color4b::DarkGray));
}
}
float linewidth = 1.0f;
if (isgloptsvalid)
linewidth = glopts->_perwire_wirewidth;
glLineWidth(linewidth);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
if (isBORenderingAvailable())
drawTrianglesBO(req, textureindex);
else
drawTrianglesIM(req, textureindex);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glPopAttrib();
}
void drawTrianglesBO(const InternalRendAtts& req, const std::vector<GLuint>& textureindex = std::vector<GLuint>()) const
{
updateClientState(req);
bool replicated = InternalRendAtts::replicatedPipelineNeeded(_currallocatedboatt);
if (replicated)
{
//qDebug("Replicated drawing");
int firsttriangleoffset = 0;
if (!req[INT_ATT_NAMES::ATT_VERTTEXTURE] && !req[INT_ATT_NAMES::ATT_WEDGETEXTURE])
{
glDisable(GL_TEXTURE_2D);
glDrawArrays(GL_TRIANGLES, 0, _mesh.fn * 3);
}
else
{
glEnable(GL_TEXTURE_2D);
for (std::vector< std::pair<short, GLuint> >::const_iterator it = _texindnumtriangles.begin(); it != _texindnumtriangles.end(); ++it)
{
if ((int(it->first) != -1) && (size_t(it->first) < textureindex.size()))
glBindTexture(GL_TEXTURE_2D, textureindex[it->first]);
else
glBindTexture(GL_TEXTURE_2D, 0);
glDrawArrays(GL_TRIANGLES, firsttriangleoffset, it->second * 3 - firsttriangleoffset);
firsttriangleoffset = it->second * 3;
}
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
}
else
{
if (req[INT_ATT_NAMES::ATT_VERTTEXTURE])
{
if (textureindex.size() > 0)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureindex[0]);
}
}
else
glDisable(GL_TEXTURE_2D);
if (_bo[INT_ATT_NAMES::ATT_VERTINDICES]->_isvalid)
{
//qDebug("Indexed drawing");
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _bo[INT_ATT_NAMES::ATT_VERTINDICES]->_bohandle);
glDrawElements(GL_TRIANGLES, GLsizei(_mesh.FN() * _bo[INT_ATT_NAMES::ATT_VERTINDICES]->_components), GL_UNSIGNED_INT, NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
InternalRendAtts tmp;
updateClientState(tmp);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void drawTrianglesIM(const InternalRendAtts& req, const std::vector<GLuint>& textureindex = std::vector<GLuint>()) const
{
if (_mesh.fn == 0)
return;
bool vn = req[INT_ATT_NAMES::ATT_VERTNORMAL] && vcg::tri::HasPerVertexNormal(_mesh);
bool fn = req[INT_ATT_NAMES::ATT_FACENORMAL] && vcg::tri::HasPerFaceNormal(_mesh);
bool vc = req[INT_ATT_NAMES::ATT_VERTCOLOR] && vcg::tri::HasPerVertexColor(_mesh);
bool fc = req[INT_ATT_NAMES::ATT_FACECOLOR] && vcg::tri::HasPerFaceColor(_mesh);
bool vt = req[INT_ATT_NAMES::ATT_VERTTEXTURE] && vcg::tri::HasPerVertexTexCoord(_mesh);
bool wt = req[INT_ATT_NAMES::ATT_WEDGETEXTURE] && vcg::tri::HasPerWedgeTexCoord(_mesh);
//typename MESHTYPE::FaceContainer::iterator fp;
typename MESH_TYPE::FaceIterator fi = _mesh.face.begin();
short curtexname = -1;
if (wt)
{
curtexname = (*fi).WT(0).n();
if ((curtexname >= 0) && (curtexname < (int)textureindex.size()))
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureindex[curtexname]);
}
else
{
glDisable(GL_TEXTURE_2D);
}
}
if (vt && !textureindex.empty()) // in the case of per vertex tex coord we assume that we have a SINGLE texture.
{
curtexname = 0;
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureindex[curtexname]);
}
glBegin(GL_TRIANGLES);
while (fi != _mesh.face.end())
{
typename MESH_TYPE::FaceType & f = *fi;
if (!f.IsD())
{
if (wt)
if (f.WT(0).n() != curtexname)
{
curtexname = (*fi).WT(0).n();
glEnd();
if (curtexname >= 0)
{
glEnable(GL_TEXTURE_2D);
if (!textureindex.empty())
glBindTexture(GL_TEXTURE_2D, textureindex[curtexname]);
}
else
{
glDisable(GL_TEXTURE_2D);
}
glBegin(GL_TRIANGLES);
}
if (fn)
glNormal(f.cN());
if (vn)
glNormal(f.V(0)->cN());
if (fc)
glColor(f.C());
if (vc)
glColor(f.V(0)->C());
if (vt)
glTexCoord(f.V(0)->T().P());
if (wt)
glTexCoord(f.WT(0).t(0));
glVertex(f.V(0)->P());
if (vn)
glNormal(f.V(1)->cN());
if (vc)
glColor(f.V(1)->C());
if (vt)
glTexCoord(f.V(1)->T().P());
if (wt)
glTexCoord(f.WT(1).t(0));
glVertex(f.V(1)->P());
if (vn)
glNormal(f.V(2)->cN());
if (vc)
glColor(f.V(2)->C());
if (vt)
glTexCoord(f.V(2)->T().P());
if (wt)
glTexCoord(f.WT(2).t(0));
glVertex(f.V(2)->P());
}
++fi;
}
glEnd();
}
void drawPoints(const InternalRendAtts& req, GL_OPTIONS_DERIVED_TYPE* glopts, const std::vector<GLuint>& textureindex = std::vector<GLuint>()) const
{
if (_mesh.VN() == 0)
return;
glPushAttrib(GL_ALL_ATTRIB_BITS);
bool isgloptsvalid = (glopts != NULL);
if ((isgloptsvalid && glopts->_perpoint_noshading) || (isgloptsvalid && glopts->_perpoint_dot_enabled))
glDisable(GL_LIGHTING);
else
if ((!isgloptsvalid) || req[INT_ATT_NAMES::ATT_VERTNORMAL])
{
glEnable(GL_LIGHTING);
}
glEnable(GL_COLOR_MATERIAL);
if ((isgloptsvalid) && ((glopts->_perpoint_fixed_color_enabled) || (glopts->_perpoint_mesh_color_enabled))) {
if (glopts->_perpoint_fixed_color_enabled)
glColor(glopts->_perpoint_fixed_color);
else
glColor(_mesh.C());
}
if (req[INT_ATT_NAMES::ATT_VERTCOLOR])
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
if (req[INT_ATT_NAMES::ATT_VERTTEXTURE])
{
glEnable(GL_TEXTURE_2D);
if (textureindex.size() > 0)
glBindTexture(GL_TEXTURE_2D, textureindex[0]);
else
glBindTexture(GL_TEXTURE_2D, 0);
}
else
glDisable(GL_TEXTURE_2D);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _bo[GLMeshAttributesInfo::ATT_VERTINDEX]->_bohandle);
if (glopts != NULL)
{
if (!glopts->_perpoint_dot_enabled)
glPointSize(glopts->_perpoint_pointsize);
if ((glopts->_perpoint_pointsmooth_enabled) || (glopts->_perpoint_dot_enabled))
glEnable(GL_POINT_SMOOTH);
else
glDisable(GL_POINT_SMOOTH);
if (glopts->_perpoint_pointattenuation_enabled)
{
vcg::Matrix44<typename MESH_TYPE::ScalarType> mat;
glGetv(GL_MODELVIEW_MATRIX, mat);
vcg::Point3<typename MESH_TYPE::ScalarType> c = _mesh.bbox.Center();
float camDist = (float)Norm(mat*c);
float quadratic[] = { 0.0f, 0.0f, 1.0f / (camDist*camDist) , 0.0f };
glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, quadratic);
glPointParameterf(GL_POINT_SIZE_MAX, 16.0f);
glPointParameterf(GL_POINT_SIZE_MIN, 1.0f);
}
else
{
float quadratic[] = { 1.0f, 0.0f, 0.0f };
glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, quadratic);
float pointsize = 1.0f;
if (isgloptsvalid)
pointsize = glopts->_perpoint_pointsize;
glPointSize(pointsize);
}
if (glopts->_perpoint_dot_enabled)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthRange(0.0, 0.9999);
glDepthFunc(GL_LEQUAL);
glPointSize(glopts->_perpoint_pointsize + 0.5);
}
}
if (isBORenderingAvailable())
drawPointsBO(req);
else
drawPointsIM(req);
if ((glopts != NULL) && (glopts->_perpoint_dot_enabled))
{
float psize = 0.0001f;
if ((glopts->_perpoint_pointsize - 1) > 0)
psize = (glopts->_perpoint_pointsize - 1);
glPointSize(psize);
if (isBORenderingAvailable())
drawPointsBO(req);
else
drawPointsIM(req);
}
glPopAttrib();
}
void drawPointsBO(const InternalRendAtts& req) const
{
size_t pointsnum = _mesh.VN();
if (InternalRendAtts::replicatedPipelineNeeded(_currallocatedboatt))
pointsnum = _mesh.FN() * 3;
updateClientState(req);
glDrawArrays(GL_POINTS, 0, GLsizei(pointsnum));
/*disable all client state buffers*/
InternalRendAtts tmp;
updateClientState(tmp);
}
void drawPointsIM(const InternalRendAtts& req) const
{
bool vn = req[INT_ATT_NAMES::ATT_VERTNORMAL] && vcg::tri::HasPerVertexNormal(_mesh);
bool vc = req[INT_ATT_NAMES::ATT_VERTCOLOR] && vcg::tri::HasPerVertexColor(_mesh);
bool vt = req[INT_ATT_NAMES::ATT_VERTTEXTURE] && vcg::tri::HasPerVertexTexCoord(_mesh);
glBegin(GL_POINTS);
for (typename MESH_TYPE::VertexIterator vi = _mesh.vert.begin(); vi != _mesh.vert.end(); ++vi)
{
if (!(*vi).IsD())
{
if (vn) glNormal((*vi).cN());
if (vc) glColor((*vi).C());
if (vt) glTexCoord((*vi).T().P());
glVertex((*vi).P());
}
}
glEnd();
}
void drawEdges(const InternalRendAtts& req, GL_OPTIONS_DERIVED_TYPE* glopts) const
{
if (_mesh.VN() == 0)
return;
glPushAttrib(GL_ALL_ATTRIB_BITS);
bool isgloptsvalid = (glopts != NULL);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
if (isgloptsvalid && glopts->_perwire_noshading)
glDisable(GL_LIGHTING);
else
if ((!isgloptsvalid) || (req[INT_ATT_NAMES::ATT_VERTNORMAL]))
{
glEnable(GL_LIGHTING);
}
bool colordefinedenabled = (isgloptsvalid) && ((glopts->_perwire_fixed_color_enabled) || (glopts->_perwire_mesh_color_enabled));
if (!(isgloptsvalid) || colordefinedenabled)
{
vcg::Color4b tmpcol = vcg::Color4b(vcg::Color4b::DarkGray);
if (colordefinedenabled)
{
if (glopts->_perwire_fixed_color_enabled)
tmpcol = glopts->_perwire_fixed_color;
else
tmpcol = _mesh.cC();
}
glColor(tmpcol);
}
glDisable(GL_TEXTURE_2D);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _bo[GLMeshAttributesInfo::ATT_VERTINDEX]->_bohandle);
float linewidth = 1.0f;
if (isgloptsvalid)
linewidth = glopts->_perwire_wirewidth;
glLineWidth(linewidth);
if (isBORenderingAvailable())
drawEdgesBO(req);
else
drawEdgesIM(req);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
/*disable all client state buffers*/
glPopAttrib();
}
void drawEdgesBO(const InternalRendAtts& req) const
{
if (_bo[INT_ATT_NAMES::ATT_EDGEINDICES]->_isvalid)
{
//qDebug("Indexed drawing");
updateClientState(req);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _bo[INT_ATT_NAMES::ATT_EDGEINDICES]->_bohandle);
glDrawElements(GL_LINES, GLsizei(_edge.size() * _bo[INT_ATT_NAMES::ATT_EDGEINDICES]->_components), GL_UNSIGNED_INT, NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
InternalRendAtts tmp;
updateClientState(tmp);
}
}
void drawEdgesIM(const InternalRendAtts& req) const
{
typename MESH_TYPE::FaceIterator fi = _mesh.face.begin();
bool vn = req[INT_ATT_NAMES::ATT_VERTNORMAL] && vcg::tri::HasPerVertexNormal(_mesh);
bool vc = req[INT_ATT_NAMES::ATT_VERTCOLOR] && vcg::tri::HasPerVertexColor(_mesh);
glBegin(GL_LINES);
while (fi != _mesh.face.end())
{
typename MESH_TYPE::FaceType & f = *fi;
if (!f.IsD())
{
if (!f.IsF(0))
{
if (vn) glNormal(f.V(0)->cN());
if (vc) glColor(f.V(0)->C());
glVertex(f.V(0)->P());
if (vn) glNormal(f.V(1)->cN());
if (vc) glColor(f.V(1)->C());
glVertex(f.V(1)->P());
}
if (!f.IsF(1))
{
if (vn) glNormal(f.V(1)->cN());
if (vc) glColor(f.V(1)->C());
glVertex(f.V(1)->P());
if (vn) glNormal(f.V(2)->cN());
if (vc) glColor(f.V(2)->C());
glVertex(f.V(2)->P());
}
if (!f.IsF(2))
{
if (vn) glNormal(f.V(2)->cN());
if (vc) glColor(f.V(2)->C());
glVertex(f.V(2)->P());
if (vn) glNormal(f.V(0)->cN());
if (vc) glColor(f.V(0)->C());
glVertex(f.V(0)->P());
}
}
++fi;
}
glEnd();
}
void drawBBox(GL_OPTIONS_DERIVED_TYPE* glopts) const
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
bool isgloptsvalid = (glopts != NULL);
glDisable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
if ((isgloptsvalid) && (glopts->_perbbox_fixed_color_enabled))
glColor(glopts->_perbbox_fixed_color);
else
{
if ((isgloptsvalid) && (glopts->_perbbox_mesh_color_enabled))
glColor(_mesh.C());
else
glColor(vcg::Color4b(vcg::Color4b::White));
}
if (isBORenderingAvailable())
drawBBoxBO();
else
drawBBoxIM();
glPopAttrib();
}
void drawBBoxBO() const
{
vcg::Box3<typename MESH_TYPE::ScalarType>& b = _mesh.bbox;
GLuint bbhandle;
glGenBuffers(1, &bbhandle);
std::vector<vcg::Point3f> bbox(12 * 2);
//0
bbox[0] = vcg::Point3f((float)b.min[0], (float)b.min[1], (float)b.min[2]);
bbox[1] = vcg::Point3f((float)b.max[0], (float)b.min[1], (float)b.min[2]);
//1
bbox[2] = vcg::Point3f((float)b.max[0], (float)b.min[1], (float)b.min[2]);
bbox[3] = vcg::Point3f((float)b.max[0], (float)b.max[1], (float)b.min[2]);
//2
bbox[4] = vcg::Point3f((float)b.max[0], (float)b.max[1], (float)b.min[2]);
bbox[5] = vcg::Point3f((float)b.min[0], (float)b.max[1], (float)b.min[2]);
//3
bbox[6] = vcg::Point3f((float)b.min[0], (float)b.max[1], (float)b.min[2]);
bbox[7] = vcg::Point3f((float)b.min[0], (float)b.min[1], (float)b.min[2]);
//4
bbox[8] = vcg::Point3f((float)b.min[0], (float)b.min[1], (float)b.min[2]);
bbox[9] = vcg::Point3f((float)b.min[0], (float)b.min[1], (float)b.max[2]);
//5
bbox[10] = vcg::Point3f((float)b.min[0], (float)b.min[1], (float)b.max[2]);
bbox[11] = vcg::Point3f((float)b.max[0], (float)b.min[1], (float)b.max[2]);
//6
bbox[12] = vcg::Point3f((float)b.max[0], (float)b.min[1], (float)b.max[2]);
bbox[13] = vcg::Point3f((float)b.max[0], (float)b.min[1], (float)b.min[2]);
//7
bbox[14] = vcg::Point3f((float)b.max[0], (float)b.min[1], (float)b.max[2]);
bbox[15] = vcg::Point3f((float)b.max[0], (float)b.max[1], (float)b.max[2]);
//8
bbox[16] = vcg::Point3f((float)b.max[0], (float)b.max[1], (float)b.max[2]);
bbox[17] = vcg::Point3f((float)b.max[0], (float)b.max[1], (float)b.min[2]);
//9
bbox[18] = vcg::Point3f((float)b.max[0], (float)b.max[1], (float)b.max[2]);
bbox[19] = vcg::Point3f((float)b.min[0], (float)b.max[1], (float)b.max[2]);
//10
bbox[20] = vcg::Point3f((float)b.min[0], (float)b.max[1], (float)b.max[2]);
bbox[21] = vcg::Point3f((float)b.min[0], (float)b.min[1], (float)b.max[2]);
//11
bbox[22] = vcg::Point3f((float)b.min[0], (float)b.max[1], (float)b.max[2]);
bbox[23] = vcg::Point3f((float)b.min[0], (float)b.max[1], (float)b.min[2]);
glBindBuffer(GL_ARRAY_BUFFER, bbhandle);
glBufferData(GL_ARRAY_BUFFER, 12 * 2 * sizeof(vcg::Point3f), &(bbox[0]), GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_LINES, 0, 24);
glDisableClientState(GL_VERTEX_ARRAY);
glDeleteBuffers(1, &bbhandle);
}
void drawBBoxIM() const
{
vcg::Box3<typename MESH_TYPE::ScalarType>& b = _mesh.bbox;
glBegin(GL_LINE_STRIP);
glVertex3f((float)b.min[0], (float)b.min[1], (float)b.min[2]);
glVertex3f((float)b.max[0], (float)b.min[1], (float)b.min[2]);
glVertex3f((float)b.max[0], (float)b.max[1], (float)b.min[2]);
glVertex3f((float)b.min[0], (float)b.max[1], (float)b.min[2]);
glVertex3f((float)b.min[0], (float)b.min[1], (float)b.min[2]);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3f((float)b.min[0], (float)b.min[1], (float)b.max[2]);
glVertex3f((float)b.max[0], (float)b.min[1], (float)b.max[2]);
glVertex3f((float)b.max[0], (float)b.max[1], (float)b.max[2]);
glVertex3f((float)b.min[0], (float)b.max[1], (float)b.max[2]);
glVertex3f((float)b.min[0], (float)b.min[1], (float)b.max[2]);
glEnd();
glBegin(GL_LINES);
glVertex3f((float)b.min[0], (float)b.min[1], (float)b.min[2]);
glVertex3f((float)b.min[0], (float)b.min[1], (float)b.max[2]);
glVertex3f((float)b.max[0], (float)b.min[1], (float)b.min[2]);
glVertex3f((float)b.max[0], (float)b.min[1], (float)b.max[2]);
glVertex3f((float)b.max[0], (float)b.max[1], (float)b.min[2]);
glVertex3f((float)b.max[0], (float)b.max[1], (float)b.max[2]);
glVertex3f((float)b.min[0], (float)b.max[1], (float)b.min[2]);
glVertex3f((float)b.min[0], (float)b.max[1], (float)b.max[2]);
glEnd();
}
void updateClientState(const InternalRendAtts& req) const
{
int ii = 0;
for (typename std::vector<GLBufferObject*>::const_iterator it = _bo.begin(); it != _bo.end(); ++it)
{
INT_ATT_NAMES boname(ii);
if ((boname != INT_ATT_NAMES::ATT_VERTINDICES) && (boname != INT_ATT_NAMES::ATT_EDGEINDICES) /*&& (boname != INT_ATT_NAMES::ATT_FIXEDCOLOR)*/)
{
if (req[boname] && _currallocatedboatt[boname] && (*it != NULL))
{
glBindBuffer((*it)->_target, (*it)->_bohandle);
setBufferPointer(boname);
glEnableClientState((*it)->_clientstatetag);
glBindBuffer((*it)->_target, 0);
}
else
{
glBindBuffer((*it)->_target, (*it)->_bohandle);
disableClientState(boname, req);
glBindBuffer((*it)->_target, 0);
}
}
++ii;
}
}
void setBufferPointer(INT_ATT_NAMES boname) const
{
unsigned int ii = boname;
if (ii >= INT_ATT_NAMES::enumArity())
return;
GLBufferObject* cbo = _bo[ii];
if (cbo == NULL)
return;
switch (ii)
{
case(INT_ATT_NAMES::ATT_VERTPOSITION):
{
glVertexPointer(GLint(cbo->_components), cbo->_gltype, GLsizei(0), 0);
break;
}
case(INT_ATT_NAMES::ATT_VERTNORMAL):
case(INT_ATT_NAMES::ATT_FACENORMAL):
{
glNormalPointer(cbo->_gltype, GLsizei(0), 0);
break;
}
case(INT_ATT_NAMES::ATT_VERTCOLOR):
case(INT_ATT_NAMES::ATT_FACECOLOR):
{
glColorPointer(GLint(cbo->_components), cbo->_gltype, GLsizei(0), 0);
break;
}
case(INT_ATT_NAMES::ATT_VERTTEXTURE):
case(INT_ATT_NAMES::ATT_WEDGETEXTURE):
{
glTexCoordPointer(GLint(cbo->_components), cbo->_gltype, GLsizei(0), 0);
break;
}
default: break;
}
}
void disableClientState(INT_ATT_NAMES boname, const RendAtts& req) const
{
if (boname >= INT_ATT_NAMES::enumArity())
return;
switch (boname)
{
case(INT_ATT_NAMES::ATT_VERTPOSITION):
{
glDisableClientState(GL_VERTEX_ARRAY);
break;
}
case(INT_ATT_NAMES::ATT_VERTNORMAL):
case(INT_ATT_NAMES::ATT_FACENORMAL):
{
if (!req[INT_ATT_NAMES::ATT_VERTNORMAL] && !req[INT_ATT_NAMES::ATT_FACENORMAL])
glDisableClientState(GL_NORMAL_ARRAY);
break;
}
case(INT_ATT_NAMES::ATT_VERTCOLOR):
case(INT_ATT_NAMES::ATT_FACECOLOR):
{
if (!req[INT_ATT_NAMES::ATT_VERTCOLOR] && !req[INT_ATT_NAMES::ATT_FACECOLOR])
glDisableClientState(GL_COLOR_ARRAY);
break;
}
case(INT_ATT_NAMES::ATT_VERTTEXTURE):
case(INT_ATT_NAMES::ATT_WEDGETEXTURE):
{
if (!req[INT_ATT_NAMES::ATT_VERTTEXTURE] && !req[INT_ATT_NAMES::ATT_WEDGETEXTURE])
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
break;
}
default:
{
break;
}
}
}
void fillchunkMap()
{
_chunkmap.clear();
if (!vcg::tri::HasPerWedgeTexCoord(_mesh) || _mesh.face.size() == 0)
return;
typename MESH_TYPE::FaceIterator infrange = _mesh.face.begin();
short texind = _mesh.face[0].WT(0).N();
for (typename MESH_TYPE::FaceIterator fit = _mesh.face.begin(); fit != _mesh.face.end(); ++fit)
{
bool last = (fit == (_mesh.face.end() - 1));
if (fit->WT(0).N() != texind || last)
{
int lowind = std::distance(_mesh.face.begin(), infrange);
int topind = std::distance(_mesh.face.begin(), fit) - 1;
if (last)
topind++;
_chunkmap[texind].push_back(std::make_pair((GLuint)lowind, (GLuint)topind));
infrange = fit;
texind = fit->WT(0).N();
}
}
}
void debug(const InternalRendAtts& tobeallocated, const InternalRendAtts& tobedeallocated, const InternalRendAtts& tobeupdated)
{
_loginfo.reset();
_loginfo._tobedeallocated = std::string("to_be_deallocated: ");
_loginfo._tobeallocated = std::string("to_be_allocated: ");
_loginfo._tobeupdated = std::string("to_be_updated: ");
std::string truestring("true");
std::string falsestring("false");
for (unsigned int ii = 0; ii < INT_ATT_NAMES::enumArity(); ++ii)
{
std::string deallocres(falsestring);
if (tobedeallocated[ii])
deallocres = truestring;
_loginfo._tobedeallocated += deallocres + " ";
std::string allocres(falsestring);
if (tobeallocated[ii])
allocres = truestring;
_loginfo._tobeallocated += allocres + " ";
std::string upres(falsestring);
if (tobeupdated[ii])
upres = truestring;
_loginfo._tobeupdated += upres + " ";
}
_loginfo._tobedeallocated = std::string("[") + _loginfo._tobedeallocated + std::string("]");
_loginfo._tobeallocated = std::string("[") + _loginfo._tobeallocated + std::string("]");
_loginfo._tobeupdated = std::string("[") + _loginfo._tobeupdated + std::string("]");
int hh = 0;
for (typename ViewsMap::const_iterator it = _perviewreqatts.begin(); it != _perviewreqatts.end(); ++it)
{
std::stringstream tmpstream;
tmpstream << "view_" << hh << ":\n";
for (size_t pm = 0; pm < size_t(PR_ARITY); ++pm)
{
tmpstream << DebugInfo::primitiveName(pm) << " ";
for (unsigned int ii = 0; ii < INT_ATT_NAMES::enumArity(); ++ii)
{
std::string res = falsestring;
if (it->second._intatts[pm][ii])
res = truestring;
tmpstream << "att[" << ii << "]=" << res << " ";
}
tmpstream << std::endl;
}
_loginfo._perviewdata.push_back(tmpstream.str());
++hh;
}
std::stringstream tmpstream;
tmpstream << "currently_allocated: ";
for (unsigned int ii = 0; ii < INT_ATT_NAMES::enumArity(); ++ii)
{
std::string res = falsestring;
if (_currallocatedboatt[ii])
res = truestring;
tmpstream << "att[" << ii << "]=" << res << " ";
}
_loginfo._currentlyallocated = tmpstream.str();
}
class EdgeVertInd
{
public:
GLuint _v[2]; // the two Vertex indices are ordered!
EdgeVertInd() {}
EdgeVertInd(const MESH_TYPE& m, typename MESH_TYPE::FacePointer pf, const int nz) { this->set(m, pf, nz); }
EdgeVertInd(const MESH_TYPE& m, typename MESH_TYPE::EdgePointer pe, const int nz) { this->set(m, pe, nz); }
void set(const MESH_TYPE& m, typename MESH_TYPE::FacePointer pf, const int nz)
{
assert(pf != 0);
assert(nz >= 0);
assert(nz < pf->VN());
_v[0] = GLuint(vcg::tri::Index(m, pf->V(nz)));;
_v[1] = GLuint(vcg::tri::Index(m, pf->V(pf->Next(nz))));
assert(_v[0] != _v[1]); // The face pointed by 'f' is Degenerate (two coincident vertexes)
if (_v[0] > _v[1])
std::swap(_v[0], _v[1]);
}
void set(const MESH_TYPE& m, typename MESH_TYPE::EdgePointer pe, const int nz)
{
assert(pe != 0);
assert(nz >= 0);
assert(nz < 2);
_v[0] = GLuint(vcg::tri::Index(m, pe->V(nz)));;
_v[1] = GLuint(vcg::tri::Index(m, pe->V((nz + 1) % 2)));
assert(_v[0] != _v[1]); // The face pointed by 'f' is Degenerate (two coincident vertexes)
if (_v[0] > _v[1])
std::swap(_v[0], _v[1]);
}
inline bool operator<(const EdgeVertInd& pe) const
{
if (_v[0] < pe._v[0])
return true;
else if (_v[0] > pe._v[0])
return false;
else
return _v[1] < pe._v[1];
}
inline bool operator==(const EdgeVertInd & pe) const
{
return _v[0] == pe._v[0] && _v[1] == pe._v[1];
}
};
static void fillEdgeVector(MESH_TYPE &m, std::vector<EdgeVertInd> &edgeVec, bool includeFauxEdge = true)
{
if (m.FN() > 0)
{
edgeVec.reserve(m.FN() * 3);
for (typename MESH_TYPE::FaceIterator fi = m.face.begin(); fi != m.face.end(); ++fi)
if (!(*fi).IsD())
for (int j = 0; j < (*fi).VN(); ++j)
if (includeFauxEdge || !(*fi).IsF(j))
edgeVec.push_back(EdgeVertInd(m, &*fi, j));
}
else
if ((m.VN() > 0) && (m.EN() > 0))
{
edgeVec.reserve(m.EN() * 2);
for (typename MESH_TYPE::EdgeIterator ei = m.edge.begin(); ei != m.edge.end(); ++ei)
if (!(*ei).IsD())
for (int j = 0; j < 2; ++j)
edgeVec.push_back(EdgeVertInd(m, &*ei, j));
}
}
static void fillUniqueEdgeVector(MESH_TYPE &m, std::vector<EdgeVertInd> &edgeVec)
{
fillEdgeVector(m, edgeVec, false);
std::sort(edgeVec.begin(), edgeVec.end());
typename std::vector<EdgeVertInd>::iterator newEnd = std::unique(edgeVec.begin(), edgeVec.end());
edgeVec.resize(newEnd - edgeVec.begin());
}
struct GLBufferObject
{
GLBufferObject(size_t components, GLenum gltype, GLenum clientstatetag, GLenum target)
:_size(0), _components(components), _isvalid(false), _gltype(gltype), _target(target), _clientstatetag(clientstatetag), _bohandle(0)
{
}
GLBufferObject(size_t components, GLenum gltype, GLenum target)
:_size(0), _components(components), _isvalid(false), _gltype(gltype), _target(target), _clientstatetag(), _bohandle(0)
{
}
size_t getSizeOfGLType() const
{
switch (_gltype)
{
case(GL_FLOAT):
return sizeof(GLfloat);
case(GL_INT):
return sizeof(GLint);
case(GL_UNSIGNED_INT):
return sizeof(GLuint);
case(GL_UNSIGNED_BYTE):
return sizeof(GLubyte);
}
return 0;
}
size_t _size;
const size_t _components;
bool _isvalid;
const GLenum _gltype;
const GLenum _target;
/*WARNING!!!!!!!!!!!!!!!!! In openGL INDEX BO doesn't require to be enabled/disabled so has NOT a valid tag associated.
In this case the client state tag remains not initialized and it's not meaningful */
const GLenum _clientstatetag;
/**********************************************************************************/
GLuint _bohandle;
};
//ideally this should be const. I'm not yet sure if VCGLib will allow me to declare it as constant
MESH_TYPE& _mesh;
MemoryInfo& _gpumeminfo;
/*The buffer objects used for the rendering operation. They are shared among all the views*/
std::vector<GLBufferObject*> _bo;
typedef std::map< UNIQUE_VIEW_ID_TYPE, PVData > ViewsMap;
///*_perviewreqatts contains a map of the requested atts by each single view. it's maintained for the actual rendering step*/
ViewsMap _perviewreqatts;
/*_currboatt contains the union of all the requested attributes by each single view on the scene. At the end it represents the BOs allocated in the GPU memory*/
/* WARNING!!!! The currently allocated BOs are the union of all the BOs requested to be visualized in the _perviewreqatts plus, possibly, the edgeindex bo (depending by the kind of per view render primitive modality that is requested) and the vertexindex bo (depending of the solid rendering modality, per-face/per-vertex)*/
/* The EdgeIndex bo is allocated only if one of the requested rendering modality is PR_WIREFRAME_EDGES or PR_WIREFRAME_EDGES. If we have PR_SOLID the glPolygonMode function is used for rendering the triangle wireframe view*/
InternalRendAtts _currallocatedboatt;
bool _borendering;
size_t _perbatchprim;
/*Additional structures used for per wedge texturing modality*/
typedef std::vector< std::pair< GLuint, GLuint > > ChunkVector;
typedef std::map< short, ChunkVector > ChunkMap;
std::vector< std::pair<short, GLuint> > _texindnumtriangles;
ChunkMap _chunkmap;
//Horrible waste of memory space...but computing the list of edges is too much expensive...we must minimize it!
std::vector<EdgeVertInd> _edge;
size_t _meshverticeswhenedgeindiceswerecomputed;
size_t _meshtriangleswhenedgeindiceswerecomputed;
//vcg::GLOptions _glopts;
vcg::Matrix44<typename MESH_TYPE::ScalarType> _tr;
bool _debugmode;
DebugInfo _loginfo;
std::vector<InternalRendAtts> _meaningfulattsperprimitive;
};
}
#endif
|