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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOpenGLContextDevice2D.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkOpenGLContextDevice2D.h"
#include "vtkAbstractContextBufferId.h"
#include "vtkBrush.h"
#include "vtkFloatArray.h"
#include "vtkImageData.h"
#include "vtkMath.h"
#include "vtkMathTextUtilities.h"
#include "vtkMatrix3x3.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLError.h"
#include "vtkOpenGLIndexBufferObject.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLRenderer.h"
#include "vtkOpenGLShaderCache.h"
#include "vtkOpenGLTexture.h"
#include "vtkOpenGLVertexArrayObject.h"
#include "vtkOpenGLVertexBufferObject.h"
#include "vtkPen.h"
#include "vtkPoints2D.h"
#include "vtkRect.h"
#include "vtkShaderProgram.h"
#include "vtkSmartPointer.h"
#include "vtkTextProperty.h"
#include "vtkTextRendererStringToImage.h"
#include "vtkTexture.h"
#include "vtkTextureUnitManager.h"
#include "vtkTransform.h"
#include "vtkVector.h"
#include "vtkViewport.h"
#include "vtkWindow.h"
#include "vtkOpenGLHelper.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLContextDevice2DPrivate.h"
#include <algorithm>
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkOpenGLContextDevice2D)
//-----------------------------------------------------------------------------
vtkOpenGLContextDevice2D::vtkOpenGLContextDevice2D()
{
this->Renderer = 0;
this->InRender = false;
this->TextRenderer = vtkTextRendererStringToImage::New();
this->Storage = new vtkOpenGLContextDevice2D::Private;
this->RenderWindow = NULL;
this->MaximumMarkerCacheSize = 20;
this->ProjectionMatrix = vtkTransform::New();
this->ModelMatrix = vtkTransform::New();
this->VBO = new vtkOpenGLHelper;
this->VCBO = new vtkOpenGLHelper;
this->VTBO = new vtkOpenGLHelper;
this->SBO = new vtkOpenGLHelper;
this->SCBO = new vtkOpenGLHelper;
}
//-----------------------------------------------------------------------------
vtkOpenGLContextDevice2D::~vtkOpenGLContextDevice2D()
{
delete this->VBO;
this->VBO = 0;
delete this->VCBO;
this->VCBO = 0;
delete this->SBO;
this->SBO = 0;
delete this->SCBO;
this->SCBO = 0;
delete this->VTBO;
this->VTBO = 0;
while (!this->MarkerCache.empty())
{
this->MarkerCache.back().Value->Delete();
this->MarkerCache.pop_back();
}
this->TextRenderer->Delete();
this->ProjectionMatrix->Delete();
this->ModelMatrix->Delete();
delete this->Storage;
}
vtkMatrix4x4 *vtkOpenGLContextDevice2D::GetProjectionMatrix()
{
return this->ProjectionMatrix->GetMatrix();
}
vtkMatrix4x4 *vtkOpenGLContextDevice2D::GetModelMatrix()
{
return this->ModelMatrix->GetMatrix();
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::Begin(vtkViewport* viewport)
{
vtkOpenGLClearErrorMacro();
// Need the actual pixel size of the viewport - ask OpenGL.
GLint vp[4];
glGetIntegerv(GL_VIEWPORT, vp);
this->Storage->Offset.Set(static_cast<int>(vp[0]),
static_cast<int>(vp[1]));
this->Storage->Dim.Set(static_cast<int>(vp[2]),
static_cast<int>(vp[3]));
// push a 2D matrix on the stack
this->ProjectionMatrix->Push();
this->ProjectionMatrix->Identity();
this->PushMatrix();
this->ModelMatrix->Identity();
double offset = 0.5;
double xmin = offset;
double xmax = vp[2]+offset-1.0;
double ymin = offset;
double ymax = vp[3]+offset-1.0;
double znear = -2000;
double zfar = 2000;
double matrix[4][4];
vtkMatrix4x4::Identity(*matrix);
matrix[0][0] = 2/(xmax - xmin);
matrix[1][1] = 2/(ymax - ymin);
matrix[2][2] = -2/(zfar - znear);
matrix[0][3] = -(xmin + xmax)/(xmax - xmin);
matrix[1][3] = -(ymin + ymax)/(ymax - ymin);
matrix[2][3] = -(znear + zfar)/(zfar - znear);
this->ProjectionMatrix->SetMatrix(*matrix);
// Store the previous state before changing it
this->Storage->SaveGLState();
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
this->Renderer = vtkRenderer::SafeDownCast(viewport);
this->RenderWindow = vtkOpenGLRenderWindow::SafeDownCast(this->Renderer->GetRenderWindow());
this->RenderWindow->GetShaderCache()->ReleaseCurrentShader();
// Enable simple line, point and polygon antialiasing if multisampling is on.
if (this->Renderer->GetRenderWindow()->GetMultiSamples())
{
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
}
this->InRender = true;
vtkOpenGLCheckErrorMacro("failed after Begin");
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::End()
{
if (!this->InRender)
{
return;
}
this->ProjectionMatrix->Pop();
this->PopMatrix();
vtkOpenGLClearErrorMacro();
// Restore the GL state that we changed
this->Storage->RestoreGLState();
// Disable simple line, point and polygon antialiasing if multisampling is on.
if (this->Renderer->GetRenderWindow()->GetMultiSamples())
{
glDisable(GL_LINE_SMOOTH);
glDisable(GL_POLYGON_SMOOTH);
}
this->RenderWindow = NULL;
this->InRender = false;
vtkOpenGLCheckErrorMacro("failed after End");
}
// ----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::BufferIdModeBegin(
vtkAbstractContextBufferId *bufferId)
{
assert("pre: not_yet" && !this->GetBufferIdMode());
assert("pre: bufferId_exists" && bufferId!=0);
vtkOpenGLClearErrorMacro();
this->BufferId=bufferId;
// Save OpenGL state.
this->Storage->SaveGLState(true);
int lowerLeft[2];
int usize, vsize;
this->Renderer->GetTiledSizeAndOrigin(&usize,&vsize,lowerLeft,lowerLeft+1);
// push a 2D matrix on the stack
this->ProjectionMatrix->Push();
this->ProjectionMatrix->Identity();
this->PushMatrix();
this->ModelMatrix->Identity();
double xmin = 0.5;
double xmax = usize+0.5;
double ymin = 0.5;
double ymax = vsize+0.5;
double znear = -1;
double zfar = 1;
double matrix[4][4];
vtkMatrix4x4::Identity(*matrix);
matrix[0][0] = 2/(xmax - xmin);
matrix[1][1] = 2/(ymax - ymin);
matrix[2][2] = -2/(zfar - znear);
matrix[0][3] = -(xmin + xmax)/(xmax - xmin);
matrix[1][3] = -(ymin + ymax)/(ymax - ymin);
matrix[2][3] = -(znear + zfar)/(zfar - znear);
this->ProjectionMatrix->SetMatrix(*matrix);
glDrawBuffer(GL_BACK_LEFT);
glClearColor(0.0,0.0,0.0,0.0); // id=0 means no hit, just background
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_ALPHA_TEST);
glDisable(GL_STENCIL_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
vtkOpenGLCheckErrorMacro("failed after BufferIdModeBegin");
assert("post: started" && this->GetBufferIdMode());
}
// ----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::BufferIdModeEnd()
{
assert("pre: started" && this->GetBufferIdMode());
vtkOpenGLClearErrorMacro();
// Assume the renderer has been set previously during rendering (sse Begin())
int lowerLeft[2];
int usize, vsize;
this->Renderer->GetTiledSizeAndOrigin(&usize,&vsize,lowerLeft,lowerLeft+1);
this->BufferId->SetValues(lowerLeft[0],lowerLeft[1]);
this->ProjectionMatrix->Pop();
this->PopMatrix();
this->Storage->RestoreGLState(true);
this->BufferId=0;
vtkOpenGLCheckErrorMacro("failed after BufferIdModeEnd");
assert("post: done" && !this->GetBufferIdMode());
}
void vtkOpenGLContextDevice2D::SetMatrices(vtkShaderProgram *prog)
{
prog->SetUniformMatrix("WCDCMatrix",
this->ProjectionMatrix->GetMatrix());
prog->SetUniformMatrix("MCWCMatrix",
this->ModelMatrix->GetMatrix());
}
void vtkOpenGLContextDevice2D::BuildVBO(
vtkOpenGLHelper *cellBO,
float *f, int nv,
unsigned char *colors, int nc,
float *tcoords)
{
int stride = 2;
int cOffset = 0;
int tOffset = 0;
if (colors)
{
cOffset = stride;
stride++;
}
if (tcoords)
{
tOffset = stride;
stride += 2;
}
std::vector<float> va;
va.resize(nv*stride);
vtkucfloat c;
for (int i = 0; i < nv; i++)
{
va[i*stride] = f[i*2];
va[i*stride+1] = f[i*2+1];
if (colors)
{
c.c[0] = colors[nc*i];
c.c[1] = colors[nc*i+1];
c.c[2] = colors[nc*i+2];
if (nc == 4)
{
c.c[3] = colors[nc*i+3];
}
else
{
c.c[3] = 255;
}
va[i*stride+cOffset] = c.f;
}
if (tcoords)
{
va[i*stride+tOffset] = tcoords[i*2];
va[i*stride+tOffset+1] = tcoords[i*2+1];
}
}
// upload the data
cellBO->IBO->Upload(va, vtkOpenGLBufferObject::ArrayBuffer);
cellBO->VAO->Bind();
if (!cellBO->VAO->AddAttributeArray(
cellBO->Program, cellBO->IBO,
"vertexMC", 0,
sizeof(float)*stride,
VTK_FLOAT, 2, false))
{
vtkErrorMacro(<< "Error setting vertexMC in shader VAO.");
}
if (colors)
{
if (!cellBO->VAO->AddAttributeArray(
cellBO->Program, cellBO->IBO,
"vertexScalar", sizeof(float)*cOffset,
sizeof(float)*stride,
VTK_UNSIGNED_CHAR, 4, true))
{
vtkErrorMacro(<< "Error setting vertexScalar in shader VAO.");
}
}
if (tcoords)
{
if (!cellBO->VAO->AddAttributeArray(
cellBO->Program, cellBO->IBO,
"tcoordMC", sizeof(float)*tOffset,
sizeof(float)*stride,
VTK_FLOAT, 2, false))
{
vtkErrorMacro(<< "Error setting tcoordMC in shader VAO.");
}
}
cellBO->VAO->Bind();
}
void vtkOpenGLContextDevice2D::ReadyVBOProgram()
{
if (!this->VBO->Program)
{
this->VBO->Program =
this->RenderWindow->GetShaderCache()->ReadyShaderProgram(
// vertex shader
"//VTK::System::Dec\n"
"attribute vec2 vertexMC;\n"
"uniform mat4 WCDCMatrix;\n"
"uniform mat4 MCWCMatrix;\n"
"void main() {\n"
"vec4 vertex = vec4(vertexMC.xy, 0.0, 1.0);\n"
"gl_Position = vertex*MCWCMatrix*WCDCMatrix; }\n",
// fragment shader
"//VTK::System::Dec\n"
"//VTK::Output::Dec\n"
"uniform vec4 vertexColor;\n"
"void main() { gl_FragData[0] = vertexColor; }",
// geometry shader
"");
}
else
{
this->RenderWindow->GetShaderCache()->ReadyShaderProgram(this->VBO->Program);
}
}
void vtkOpenGLContextDevice2D::ReadyVCBOProgram()
{
if (!this->VCBO->Program)
{
this->VCBO->Program =
this->RenderWindow->GetShaderCache()->ReadyShaderProgram(
// vertex shader
"//VTK::System::Dec\n"
"attribute vec2 vertexMC;\n"
"attribute vec4 vertexScalar;\n"
"uniform mat4 WCDCMatrix;\n"
"uniform mat4 MCWCMatrix;\n"
"varying vec4 vertexColor;\n"
"void main() {\n"
"vec4 vertex = vec4(vertexMC.xy, 0.0, 1.0);\n"
"vertexColor = vertexScalar;\n"
"gl_Position = vertex*MCWCMatrix*WCDCMatrix; }\n",
// fragment shader
"//VTK::System::Dec\n"
"//VTK::Output::Dec\n"
"varying vec4 vertexColor;\n"
"void main() { gl_FragData[0] = vertexColor; }",
// geometry shader
"");
}
else
{
this->RenderWindow->GetShaderCache()->ReadyShaderProgram(this->VCBO->Program);
}
}
void vtkOpenGLContextDevice2D::ReadyVTBOProgram()
{
if (!this->VTBO->Program)
{
this->VTBO->Program =
this->RenderWindow->GetShaderCache()->ReadyShaderProgram(
// vertex shader
"//VTK::System::Dec\n"
"attribute vec2 vertexMC;\n"
"attribute vec2 tcoordMC;\n"
"uniform mat4 WCDCMatrix;\n"
"uniform mat4 MCWCMatrix;\n"
"varying vec2 tcoord;\n"
"void main() {\n"
"vec4 vertex = vec4(vertexMC.xy, 0.0, 1.0);\n"
"tcoord = tcoordMC;\n"
"gl_Position = vertex*MCWCMatrix*WCDCMatrix; }\n",
// fragment shader
"//VTK::System::Dec\n"
"//VTK::Output::Dec\n"
"varying vec2 tcoord;\n"
"uniform sampler2D texture1;\n"
"void main() { gl_FragData[0] = texture2D(texture1, tcoord); }",
// geometry shader
"");
}
else
{
this->RenderWindow->GetShaderCache()->ReadyShaderProgram(this->VTBO->Program);
}
}
void vtkOpenGLContextDevice2D::ReadySBOProgram()
{
if (!this->SBO->Program)
{
this->SBO->Program =
this->RenderWindow->GetShaderCache()->ReadyShaderProgram(
// vertex shader
"//VTK::System::Dec\n"
"attribute vec2 vertexMC;\n"
"uniform mat4 WCDCMatrix;\n"
"uniform mat4 MCWCMatrix;\n"
"void main() {\n"
"vec4 vertex = vec4(vertexMC.xy, 0.0, 1.0);\n"
"gl_Position = vertex*MCWCMatrix*WCDCMatrix; }\n",
// fragment shader
"//VTK::System::Dec\n"
"//VTK::Output::Dec\n"
"uniform vec4 vertexColor;\n"
"uniform sampler2D texture1;\n"
"void main() { gl_FragData[0] = vertexColor*texture2D(texture1, gl_PointCoord); }",
// geometry shader
"");
}
else
{
this->RenderWindow->GetShaderCache()->ReadyShaderProgram(this->SBO->Program);
}
}
void vtkOpenGLContextDevice2D::ReadySCBOProgram()
{
if (!this->SCBO->Program)
{
this->SCBO->Program =
this->RenderWindow->GetShaderCache()->ReadyShaderProgram(
// vertex shader
"//VTK::System::Dec\n"
"attribute vec2 vertexMC;\n"
"attribute vec4 vertexScalar;\n"
"uniform mat4 WCDCMatrix;\n"
"uniform mat4 MCWCMatrix;\n"
"varying vec4 vertexColor;\n"
"void main() {\n"
"vec4 vertex = vec4(vertexMC.xy, 0.0, 1.0);\n"
"vertexColor = vertexScalar;\n"
"gl_Position = vertex*MCWCMatrix*WCDCMatrix; }\n",
// fragment shader
"//VTK::System::Dec\n"
"//VTK::Output::Dec\n"
"varying vec4 vertexColor;\n"
"uniform sampler2D texture1;\n"
"void main() { gl_FragData[0] = vertexColor*texture2D(texture1, gl_PointCoord); }",
// geometry shader
"");
}
else
{
this->RenderWindow->GetShaderCache()->ReadyShaderProgram(this->SCBO->Program);
}
}
namespace
{
void copyColors(std::vector<unsigned char> &newColors,
unsigned char *colors, int nc)
{
for (int j = 0; j < nc; j++)
{
newColors.push_back(colors[j]);
}
}
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::DrawPoly(float *f, int n, unsigned char *colors,
int nc)
{
assert("f must be non-null" && f != NULL);
assert("n must be greater than 0" && n > 0);
if (this->Pen->GetLineType() == vtkPen::NO_PEN)
{
return;
}
vtkOpenGLClearErrorMacro();
this->SetLineType(this->Pen->GetLineType());
vtkOpenGLHelper *cbo = 0;
if (colors)
{
this->ReadyVCBOProgram();
cbo = this->VCBO;
}
else
{
this->ReadyVBOProgram();
cbo = this->VBO;
cbo->Program->SetUniform4uc("vertexColor",
this->Pen->GetColor());
}
this->SetMatrices(cbo->Program);
if (this->Pen->GetWidth() > 1.0)
{
double *scale = this->ModelMatrix->GetScale();
// convert to triangles and draw, this is because
// OpenGL no longer supports wide lines directly
float hwidth = this->Pen->GetWidth()/2.0;
std::vector<float> newVerts;
std::vector<unsigned char> newColors;
for (int i = 0; i < n-1; i++)
{
// for each line segment draw two triangles
// start by computing the direction
vtkVector2f dir((f[i*2+2] - f[i*2])*scale[0],
(f[i*2+3] - f[i*2+1])*scale[1]);
vtkVector2f norm(-dir.GetY(),dir.GetX());
norm.Normalize();
norm.SetX(hwidth*norm.GetX()/scale[0]);
norm.SetY(hwidth*norm.GetY()/scale[1]);
newVerts.push_back(f[i*2]+norm.GetX());
newVerts.push_back(f[i*2+1]+norm.GetY());
newVerts.push_back(f[i*2]-norm.GetX());
newVerts.push_back(f[i*2+1]-norm.GetY());
newVerts.push_back(f[i*2+2]-norm.GetX());
newVerts.push_back(f[i*2+3]-norm.GetY());
newVerts.push_back(f[i*2]+norm.GetX());
newVerts.push_back(f[i*2+1]+norm.GetY());
newVerts.push_back(f[i*2+2]-norm.GetX());
newVerts.push_back(f[i*2+3]-norm.GetY());
newVerts.push_back(f[i*2+2]+norm.GetX());
newVerts.push_back(f[i*2+3]+norm.GetY());
if (colors)
{
copyColors(newColors, colors+i*nc, nc);
copyColors(newColors, colors+i*nc, nc);
copyColors(newColors, colors+(i+1)*nc, nc);
copyColors(newColors, colors+i*nc, nc);
copyColors(newColors, colors+(i+1)*nc, nc);
copyColors(newColors, colors+(i+1)*nc, nc);
}
}
this->BuildVBO(cbo, &(newVerts[0]), newVerts.size()/2,
colors ? &(newColors[0]) : NULL, nc, NULL);
glDrawArrays(GL_TRIANGLES, 0, newVerts.size()/2);
}
else
{
this->SetLineWidth(this->Pen->GetWidth());
this->BuildVBO(cbo, f, n, colors, nc, NULL);
glDrawArrays(GL_LINE_STRIP, 0, n);
this->SetLineWidth(1.0);
}
// free everything
cbo->ReleaseGraphicsResources(this->RenderWindow);
vtkOpenGLCheckErrorMacro("failed after DrawPoly");
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::DrawLines(float *f, int n, unsigned char *colors,
int nc)
{
assert("f must be non-null" && f != NULL);
assert("n must be greater than 0" && n > 0);
if (this->Pen->GetLineType() == vtkPen::NO_PEN)
{
return;
}
vtkOpenGLClearErrorMacro();
this->SetLineType(this->Pen->GetLineType());
vtkOpenGLHelper *cbo = 0;
if (colors)
{
this->ReadyVCBOProgram();
cbo = this->VCBO;
}
else
{
this->ReadyVBOProgram();
cbo = this->VBO;
cbo->Program->SetUniform4uc("vertexColor",
this->Pen->GetColor());
}
this->SetMatrices(cbo->Program);
if (this->Pen->GetWidth() > 1.0)
{
double *scale = this->ModelMatrix->GetScale();
// convert to triangles and draw, this is because
// OpenGL no longer supports wide lines directly
float hwidth = this->Pen->GetWidth()/2.0;
std::vector<float> newVerts;
std::vector<unsigned char> newColors;
for (int i = 0; i < n-1; i += 2)
{
// for each line segment draw two triangles
// start by computing the direction
vtkVector2f dir((f[i*2+2] - f[i*2])*scale[0],
(f[i*2+3] - f[i*2+1])*scale[1]);
vtkVector2f norm(-dir.GetY(),dir.GetX());
norm.Normalize();
norm.SetX(hwidth*norm.GetX()/scale[0]);
norm.SetY(hwidth*norm.GetY()/scale[1]);
newVerts.push_back(f[i*2]+norm.GetX());
newVerts.push_back(f[i*2+1]+norm.GetY());
newVerts.push_back(f[i*2]-norm.GetX());
newVerts.push_back(f[i*2+1]-norm.GetY());
newVerts.push_back(f[i*2+2]-norm.GetX());
newVerts.push_back(f[i*2+3]-norm.GetY());
newVerts.push_back(f[i*2]+norm.GetX());
newVerts.push_back(f[i*2+1]+norm.GetY());
newVerts.push_back(f[i*2+2]-norm.GetX());
newVerts.push_back(f[i*2+3]-norm.GetY());
newVerts.push_back(f[i*2+2]+norm.GetX());
newVerts.push_back(f[i*2+3]+norm.GetY());
if (colors)
{
copyColors(newColors, colors+i*nc, nc);
copyColors(newColors, colors+i*nc, nc);
copyColors(newColors, colors+(i+1)*nc, nc);
copyColors(newColors, colors+i*nc, nc);
copyColors(newColors, colors+(i+1)*nc, nc);
copyColors(newColors, colors+(i+1)*nc, nc);
}
}
this->BuildVBO(cbo, &(newVerts[0]), newVerts.size()/2,
colors ? &(newColors[0]) : NULL, nc, NULL);
glDrawArrays(GL_TRIANGLES, 0, newVerts.size()/2);
}
else
{
this->SetLineWidth(this->Pen->GetWidth());
this->BuildVBO(cbo, f, n, colors, nc, NULL);
glDrawArrays(GL_LINES, 0, n);
this->SetLineWidth(1.0);
}
// free everything
cbo->ReleaseGraphicsResources(this->RenderWindow);
vtkOpenGLCheckErrorMacro("failed after DrawLines");
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::DrawPoints(float *f, int n, unsigned char *c,
int nc)
{
vtkOpenGLClearErrorMacro();
vtkOpenGLHelper *cbo = 0;
if (c)
{
this->ReadyVCBOProgram();
cbo = this->VCBO;
}
else
{
this->ReadyVBOProgram();
cbo = this->VBO;
cbo->Program->SetUniform4uc("vertexColor",
this->Pen->GetColor());
}
this->SetPointSize(this->Pen->GetWidth());
this->BuildVBO(cbo, f, n, c, nc, NULL);
this->SetMatrices(cbo->Program);
glDrawArrays(GL_POINTS, 0, n);
// free everything
cbo->ReleaseGraphicsResources(this->RenderWindow);
vtkOpenGLCheckErrorMacro("failed after DrawPoints");
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::DrawPointSprites(vtkImageData *sprite,
float *points, int n,
unsigned char *colors,
int nc_comps)
{
vtkOpenGLClearErrorMacro();
if (points && n > 0)
{
this->SetPointSize(this->Pen->GetWidth());
vtkOpenGLHelper *cbo = 0;
if (colors)
{
this->ReadySCBOProgram();
cbo = this->SCBO;
}
else
{
this->ReadySBOProgram();
cbo = this->SBO;
cbo->Program->SetUniform4uc("vertexColor",
this->Pen->GetColor());
}
this->BuildVBO(cbo, points, n, colors, nc_comps, NULL);
this->SetMatrices(cbo->Program);
if (sprite)
{
if (!this->Storage->SpriteTexture)
{
this->Storage->SpriteTexture = vtkTexture::New();
}
int properties = this->Brush->GetTextureProperties();
this->Storage->SpriteTexture->SetInputData(sprite);
this->Storage->SpriteTexture->SetRepeat(properties & vtkContextDevice2D::Repeat);
this->Storage->SpriteTexture->SetInterpolate(properties & vtkContextDevice2D::Linear);
this->Storage->SpriteTexture->Render(this->Renderer);
int tunit = vtkOpenGLTexture::SafeDownCast(
this->Storage->SpriteTexture)->GetTextureUnit();
cbo->Program->SetUniformi("texture1", tunit);
}
// We can actually use point sprites here
if (!vtkOpenGLRenderWindow::GetContextSupportsOpenGL32())
{
glEnable(GL_POINT_SPRITE);
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
}
glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT);
glDrawArrays(GL_POINTS, 0, n);
// free everything
cbo->ReleaseGraphicsResources(this->RenderWindow);
if (!vtkOpenGLRenderWindow::GetContextSupportsOpenGL32())
{
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_FALSE);
glDisable(GL_POINT_SPRITE);
}
if (sprite)
{
this->Storage->SpriteTexture->PostRender(this->Renderer);
}
}
else
{
vtkWarningMacro(<< "Points supplied without a valid image or pointer.");
}
vtkOpenGLCheckErrorMacro("failed after DrawPointSprites");
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::DrawMarkers(int shape, bool highlight,
float *points, int n,
unsigned char *colors, int nc_comps)
{
// Get a point sprite for the shape
vtkImageData *sprite = this->GetMarker(shape, this->Pen->GetWidth(),
highlight);
this->DrawPointSprites(sprite, points, n, colors, nc_comps);
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::DrawQuad(float *f, int n)
{
if (!f || n <= 0)
{
vtkWarningMacro(<< "Points supplied that were not of type float.");
return;
}
// convert quads to triangles
std::vector<float> tverts;
int numTVerts = 6*n/4;
tverts.resize(numTVerts*2);
int offset[6] = {0,1,2,0,2,3};
for (int i = 0; i < numTVerts; i++)
{
int index = 2*(4*(i/6)+offset[i%6]);
tverts[i*2] = f[index];
tverts[i*2+1] = f[index+1];
}
this->CoreDrawTriangles(tverts);
}
void vtkOpenGLContextDevice2D::CoreDrawTriangles(
std::vector<float> &tverts)
{
vtkOpenGLClearErrorMacro();
float* texCoord = 0;
vtkOpenGLHelper *cbo = 0;
if (this->Brush->GetTexture())
{
this->ReadyVTBOProgram();
cbo = this->VTBO;
this->SetTexture(this->Brush->GetTexture(),
this->Brush->GetTextureProperties());
this->Storage->Texture->Render(this->Renderer);
texCoord = this->Storage->TexCoords(&(tverts[0]), tverts.size()/2);
int tunit = vtkOpenGLTexture::SafeDownCast(this->Storage->Texture)->GetTextureUnit();
cbo->Program->SetUniformi("texture1", tunit);
}
else
{
this->ReadyVBOProgram();
cbo = this->VBO;
}
cbo->Program->SetUniform4uc("vertexColor",
this->Brush->GetColor());
this->BuildVBO(cbo, &(tverts[0]), tverts.size()/2, NULL, 0, texCoord);
this->SetMatrices(cbo->Program);
glDrawArrays(GL_TRIANGLES, 0, tverts.size()/2);
// free everything
cbo->ReleaseGraphicsResources(this->RenderWindow);
if (this->Storage->Texture)
{
this->Storage->Texture->PostRender(this->Renderer);
delete [] texCoord;
}
vtkOpenGLCheckErrorMacro("failed after DrawQuad");
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::DrawQuadStrip(float *f, int n)
{
if (!f || n <= 0)
{
vtkWarningMacro(<< "Points supplied that were not of type float.");
return;
}
// convert quad strips to triangles
std::vector<float> tverts;
int numTVerts = 3*(n-2);
tverts.resize(numTVerts*2);
int offset[6] = {0,1,3,0,3,2};
for (int i = 0; i < numTVerts; i++)
{
int index = 2*(2*(i/6)+offset[i%6]);
tverts[i*2] = f[index];
tverts[i*2+1] = f[index+1];
}
this->CoreDrawTriangles(tverts);
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::DrawPolygon(float *f, int n)
{
if (!f || n <= 0)
{
vtkWarningMacro(<< "Points supplied that were not of type float.");
return;
}
// convert polygon to triangles
std::vector<float> tverts;
int numTVerts = 3*(n-2);
tverts.resize(numTVerts*2);
for (int i = 0; i < n-2; i++)
{
tverts.push_back(f[0]);
tverts.push_back(f[1]);
tverts.push_back(f[i*2+2]);
tverts.push_back(f[i*2+3]);
tverts.push_back(f[i*2+4]);
tverts.push_back(f[i*2+5]);
}
this->CoreDrawTriangles(tverts);
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::DrawEllipseWedge(float x, float y, float outRx,
float outRy, float inRx,
float inRy, float startAngle,
float stopAngle)
{
assert("pre: positive_outRx" && outRx>=0.0f);
assert("pre: positive_outRy" && outRy>=0.0f);
assert("pre: positive_inRx" && inRx>=0.0f);
assert("pre: positive_inRy" && inRy>=0.0f);
assert("pre: ordered_rx" && inRx<=outRx);
assert("pre: ordered_ry" && inRy<=outRy);
if(outRy==0.0f && outRx==0.0f)
{
// we make sure maxRadius will never be null.
return;
}
int iterations=this->GetNumberOfArcIterations(outRx,outRy,startAngle,
stopAngle);
// step in radians.
double step =
vtkMath::RadiansFromDegrees(stopAngle-startAngle)/(iterations);
// step have to be lesser or equal to maxStep computed inside
// GetNumberOfIterations()
double rstart=vtkMath::RadiansFromDegrees(startAngle);
// the A vertices (0,2,4,..) are on the inner side
// the B vertices (1,3,5,..) are on the outer side
// (A and B vertices terms come from triangle strip definition in
// OpenGL spec)
// we are iterating counterclockwise
// convert polygon to triangles
std::vector<float> tverts;
int numTVerts = 6*iterations;
tverts.resize(numTVerts*2);
int offset[6] = {0,1,3,0,3,2};
for (int i = 0; i < numTVerts; i++)
{
int index = i/6 + offset[i%6]/2;
double radiusX = offset[i%6]%2 ? outRx : inRx;
double radiusY = offset[i%6]%2 ? outRy : inRy;
double a=rstart+index*step;
tverts.push_back(radiusX * cos(a) + x);
tverts.push_back(radiusY * sin(a) + y);
}
this->CoreDrawTriangles(tverts);
}
// ----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::DrawEllipticArc(float x, float y, float rX,
float rY, float startAngle,
float stopAngle)
{
assert("pre: positive_rX" && rX>=0);
assert("pre: positive_rY" && rY>=0);
if(rX==0.0f && rY==0.0f)
{
// we make sure maxRadius will never be null.
return;
}
vtkOpenGLClearErrorMacro();
int iterations = this->GetNumberOfArcIterations(rX, rY, startAngle, stopAngle);
float *p = new float[2*(iterations+1)];
// step in radians.
double step =
vtkMath::RadiansFromDegrees(stopAngle-startAngle)/(iterations);
// step have to be lesser or equal to maxStep computed inside
// GetNumberOfIterations()
double rstart=vtkMath::RadiansFromDegrees(startAngle);
// we are iterating counterclockwise
for(int i = 0; i <= iterations; ++i)
{
double a=rstart+i*step;
p[2*i ] = rX * cos(a) + x;
p[2*i+1] = rY * sin(a) + y;
}
this->DrawPolygon(p,iterations+1);
this->DrawPoly(p,iterations+1);
delete[] p;
vtkOpenGLCheckErrorMacro("failed after DrawEllipseArc");
}
// ----------------------------------------------------------------------------
int vtkOpenGLContextDevice2D::GetNumberOfArcIterations(float rX,
float rY,
float startAngle,
float stopAngle)
{
assert("pre: positive_rX" && rX>=0.0f);
assert("pre: positive_rY" && rY>=0.0f);
assert("pre: not_both_null" && (rX>0.0 || rY>0.0));
// 1.0: pixel precision. 0.5 (subpixel precision, useful with multisampling)
double error = 4.0; // experience shows 4.0 is visually enough.
// The tessellation is the most visible on the biggest radius.
double maxRadius;
if(rX >= rY)
{
maxRadius = rX;
}
else
{
maxRadius = rY;
}
if(error > maxRadius)
{
// to make sure the argument of asin() is in a valid range.
error = maxRadius;
}
// Angle of a sector so that its chord is `error' pixels.
// This is will be our maximum angle step.
double maxStep = 2.0 * asin(error / (2.0 * maxRadius));
// ceil because we want to make sure we don't underestimate the number of
// iterations by 1.
return static_cast<int>(
ceil(vtkMath::RadiansFromDegrees(stopAngle - startAngle) / maxStep));
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::AlignText(double orientation, float width,
float height, float *p)
{
// Special case multiples of 90 as no transformation is required...
if (orientation > -0.0001 && orientation < 0.0001)
{
switch (this->TextProp->GetJustification())
{
case VTK_TEXT_LEFT:
break;
case VTK_TEXT_CENTERED:
p[0] -= floor(width / 2.0);
break;
case VTK_TEXT_RIGHT:
p[0] -= width;
break;
}
switch (this->TextProp->GetVerticalJustification())
{
case VTK_TEXT_BOTTOM:
break;
case VTK_TEXT_CENTERED:
p[1] -= floor(height / 2.0);
break;
case VTK_TEXT_TOP:
p[1] -= height;
break;
}
}
else if (orientation > 89.9999 && orientation < 90.0001)
{
switch (this->TextProp->GetJustification())
{
case VTK_TEXT_LEFT:
break;
case VTK_TEXT_CENTERED:
p[1] -= floor(height / 2.0);
break;
case VTK_TEXT_RIGHT:
p[1] -= height;
break;
}
switch (this->TextProp->GetVerticalJustification())
{
case VTK_TEXT_TOP:
break;
case VTK_TEXT_CENTERED:
p[0] -= floor(width / 2.0);
break;
case VTK_TEXT_BOTTOM:
p[0] -= width;
break;
}
}
else if (orientation > 179.9999 && orientation < 180.0001)
{
switch (this->TextProp->GetJustification())
{
case VTK_TEXT_RIGHT:
break;
case VTK_TEXT_CENTERED:
p[0] -= floor(width / 2.0);
break;
case VTK_TEXT_LEFT:
p[0] -= width;
break;
}
switch (this->TextProp->GetVerticalJustification())
{
case VTK_TEXT_TOP:
break;
case VTK_TEXT_CENTERED:
p[1] -= floor(height / 2.0);
break;
case VTK_TEXT_BOTTOM:
p[1] -= height;
break;
}
}
else if (orientation > 269.9999 && orientation < 270.0001)
{
switch (this->TextProp->GetJustification())
{
case VTK_TEXT_LEFT:
break;
case VTK_TEXT_CENTERED:
p[1] -= floor(height / 2.0);
break;
case VTK_TEXT_RIGHT:
p[1] -= height;
break;
}
switch (this->TextProp->GetVerticalJustification())
{
case VTK_TEXT_BOTTOM:
break;
case VTK_TEXT_CENTERED:
p[0] -= floor(width / 2.0);
break;
case VTK_TEXT_TOP:
p[0] -= width;
break;
}
}
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::DrawString(float *point,
const vtkStdString &string)
{
this->DrawString(point, vtkUnicodeString::from_utf8(string));
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::ComputeStringBounds(const vtkStdString &string,
float bounds[4])
{
this->ComputeStringBounds(vtkUnicodeString::from_utf8(string), bounds);
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::DrawString(float *point,
const vtkUnicodeString &string)
{
vtkOpenGLClearErrorMacro();
double *mv = this->ModelMatrix->GetMatrix()->Element[0];
float xScale = mv[0];
float yScale = mv[5];
float p[] = { std::floor(point[0] * xScale) / xScale,
std::floor(point[1] * yScale) / yScale };
// TODO this currently ignores vtkContextScene::ScaleTiles. Not sure how to
// get at that from here, but this is better than ignoring scaling altogether.
// TODO Also, FreeType supports anisotropic DPI. Might be needed if the
// tileScale isn't homogeneous, but we'll need to update the textrenderer API
// and see if MPL/mathtext can support it.
int tileScale[2];
this->RenderWindow->GetTileScale(tileScale);
int dpi = this->RenderWindow->GetDPI() * std::max(tileScale[0], tileScale[1]);
// Cache rendered text strings
vtkTextureImageCache<UTF16TextPropertyKey>::CacheData &cache =
this->Storage->TextTextureCache.GetCacheData(
UTF16TextPropertyKey(this->TextProp, string, dpi));
vtkImageData* image = cache.ImageData;
if (image->GetNumberOfPoints() == 0 && image->GetNumberOfCells() == 0)
{
int textDims[2];
if (!this->TextRenderer->RenderString(this->TextProp, string, dpi, image,
textDims))
{
return;
}
cache.TextWidth = textDims[0];
cache.TextHeight = textDims[1];
}
vtkTexture* texture = cache.Texture;
texture->Render(this->Renderer);
int imgDims[3];
image->GetDimensions(imgDims);
float width = cache.TextWidth / xScale;
float height = cache.TextHeight / yScale;
float xw = cache.TextWidth / static_cast<float>(imgDims[0]);
float xh = cache.TextHeight / static_cast<float>(imgDims[1]);
this->AlignText(this->TextProp->GetOrientation(), width, height, p);
float points[] = { p[0] , p[1],
p[0] + width, p[1],
p[0] + width, p[1] + height,
p[0] , p[1],
p[0] + width, p[1] + height,
p[0] , p[1] + height };
float texCoord[] = { 0.0f, 0.0f,
xw, 0.0f,
xw, xh,
0.0f, 0.0f,
xw, xh,
0.0f, xh };
vtkOpenGLClearErrorMacro();
vtkOpenGLHelper *cbo = 0;
this->ReadyVTBOProgram();
cbo = this->VTBO;
int tunit = vtkOpenGLTexture::SafeDownCast(texture)->GetTextureUnit();
cbo->Program->SetUniformi("texture1", tunit);
this->BuildVBO(cbo, points, 6, NULL, 0, texCoord);
this->SetMatrices(cbo->Program);
glDrawArrays(GL_TRIANGLES, 0, 6);
// free everything
cbo->ReleaseGraphicsResources(this->RenderWindow);
texture->PostRender(this->Renderer);
vtkOpenGLCheckErrorMacro("failed after DrawString");
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::ComputeStringBounds(const vtkUnicodeString &string,
float bounds[4])
{
// TODO this currently ignores vtkContextScene::ScaleTiles. Not sure how to
// get at that from here, but this is better than ignoring scaling altogether.
// TODO Also, FreeType supports anisotropic DPI. Might be needed if the
// tileScale isn't homogeneous, but we'll need to update the textrenderer API
// and see if MPL/mathtext can support it.
int tileScale[2];
this->RenderWindow->GetTileScale(tileScale);
int dpi = this->RenderWindow->GetDPI() * std::max(tileScale[0], tileScale[1]);
vtkVector2i box = this->TextRenderer->GetBounds(this->TextProp, string, dpi);
// Check for invalid bounding box
if (box[0] == VTK_INT_MIN || box[0] == VTK_INT_MAX ||
box[1] == VTK_INT_MIN || box[1] == VTK_INT_MAX)
{
bounds[0] = static_cast<float>(0);
bounds[1] = static_cast<float>(0);
bounds[2] = static_cast<float>(0);
bounds[3] = static_cast<float>(0);
return;
}
double *mv = this->ModelMatrix->GetMatrix()->Element[0];
float xScale = mv[0];
float yScale = mv[5];
bounds[0] = static_cast<float>(0);
bounds[1] = static_cast<float>(0);
bounds[2] = static_cast<float>(box.GetX() / xScale);
bounds[3] = static_cast<float>(box.GetY() / yScale);
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::DrawMathTextString(float point[2],
const vtkStdString &string)
{
vtkMathTextUtilities *mathText = vtkMathTextUtilities::GetInstance();
if (!mathText || !mathText->IsAvailable())
{
vtkWarningMacro(<<"MathText is not available to parse string "
<< string.c_str() << ". Install matplotlib and enable "
"python to use MathText.");
return;
}
vtkOpenGLClearErrorMacro();
float p[] = { std::floor(point[0]), std::floor(point[1]) };
// TODO this currently ignores vtkContextScene::ScaleTiles. Not sure how to
// get at that from here, but this is better than ignoring scaling altogether.
// TODO Also, FreeType supports anisotropic DPI. Might be needed if the
// tileScale isn't homogeneous, but we'll need to update the textrenderer API
// and see if MPL/mathtext can support it.
int tileScale[2];
this->RenderWindow->GetTileScale(tileScale);
int dpi = this->RenderWindow->GetDPI() * std::max(tileScale[0], tileScale[1]);
// Cache rendered text strings
vtkTextureImageCache<UTF8TextPropertyKey>::CacheData &cache =
this->Storage->MathTextTextureCache.GetCacheData(
UTF8TextPropertyKey(this->TextProp, string, dpi));
vtkImageData* image = cache.ImageData;
if (image->GetNumberOfPoints() == 0 && image->GetNumberOfCells() == 0)
{
int textDims[2];
if (!mathText->RenderString(string.c_str(), image, this->TextProp, dpi,
textDims))
{
return;
}
cache.TextWidth = textDims[0];
cache.TextHeight = textDims[1];
}
vtkTexture* texture = cache.Texture;
texture->Render(this->Renderer);
double *mv = this->ModelMatrix->GetMatrix()->Element[0];
float xScale = mv[0];
float yScale = mv[5];
int imgDims[3];
image->GetDimensions(imgDims);
float width = cache.TextWidth / xScale;
float height = cache.TextHeight / yScale;
float xw = cache.TextWidth / static_cast<float>(imgDims[0]);
float xh = cache.TextHeight / static_cast<float>(imgDims[1]);
this->AlignText(this->TextProp->GetOrientation(), width, height, p);
float points[] = { p[0] , p[1],
p[0] + width, p[1],
p[0] + width, p[1] + height,
p[0] , p[1],
p[0] + width, p[1] + height,
p[0] , p[1] + height };
float texCoord[] = { 0.0f, 0.0f,
xw, 0.0f,
xw, xh,
0.0f, 0.0f,
xw, xh,
0.0f, xh };
vtkOpenGLClearErrorMacro();
vtkOpenGLHelper *cbo = 0;
this->ReadyVTBOProgram();
cbo = this->VTBO;
int tunit = vtkOpenGLTexture::SafeDownCast(texture)->GetTextureUnit();
cbo->Program->SetUniformi("texture1", tunit);
this->BuildVBO(cbo, points, 6, NULL, 0, texCoord);
this->SetMatrices(cbo->Program);
glDrawArrays(GL_TRIANGLES, 0, 6);
// free everything
cbo->ReleaseGraphicsResources(this->RenderWindow);
texture->PostRender(this->Renderer);
vtkOpenGLCheckErrorMacro("failed after DrawMathTexString");
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::DrawImage(float p[2], float scale,
vtkImageData *image)
{
vtkOpenGLClearErrorMacro();
this->SetTexture(image);
this->Storage->Texture->Render(this->Renderer);
int *extent = image->GetExtent();
float points[] = { p[0] , p[1],
p[0]+scale*extent[1]+1.0f, p[1],
p[0]+scale*extent[1]+1.0f, p[1]+scale*extent[3]+1.0f,
p[0] , p[1],
p[0]+scale*extent[1]+1.0f, p[1]+scale*extent[3]+1.0f,
p[0] , p[1]+scale*extent[3]+1.0f };
float texCoord[] = { 0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f };
vtkOpenGLClearErrorMacro();
vtkOpenGLHelper *cbo = 0;
this->ReadyVTBOProgram();
cbo = this->VTBO;
int tunit = vtkOpenGLTexture::SafeDownCast(
this->Storage->Texture)->GetTextureUnit();
cbo->Program->SetUniformi("texture1", tunit);
cerr << "doing image\n";
if (this->Storage->Texture->GetTransform())
{
cerr << "have a transform\n";
}
this->BuildVBO(cbo, points, 6, NULL, 0, texCoord);
this->SetMatrices(cbo->Program);
glDrawArrays(GL_TRIANGLES, 0, 6);
// free everything
cbo->ReleaseGraphicsResources(this->RenderWindow);
this->Storage->Texture->PostRender(this->Renderer);
vtkOpenGLCheckErrorMacro("failed after DrawImage");
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::DrawImage(const vtkRectf& pos,
vtkImageData *image)
{
vtkVector2f tex(1.0, 1.0);
GLuint index = 0;
if (this->Storage->PowerOfTwoTextures)
{
index = this->Storage->TextureFromImage(image, tex);
}
else
{
index = this->Storage->TextureFromImage(image, tex);
}
float points[] = {
pos.GetX() , pos.GetY(),
pos.GetX() + pos.GetWidth(), pos.GetY(),
pos.GetX() + pos.GetWidth(), pos.GetY() + pos.GetHeight(),
pos.GetX() , pos.GetY(),
pos.GetX() + pos.GetWidth(), pos.GetY() + pos.GetHeight(),
pos.GetX() , pos.GetY() + pos.GetHeight() };
float texCoord[] = { 0.0f, 0.0f,
tex[0], 0.0f,
tex[0], tex[1],
0.0f, 0.0f,
tex[0], tex[1],
0.0f, tex[1]};
vtkOpenGLHelper *cbo = 0;
this->ReadyVTBOProgram();
cbo = this->VTBO;
int tunit = this->RenderWindow->GetTextureUnitManager()->Allocate();
if (tunit < 0)
{
vtkErrorMacro("Hardware does not support the number of textures defined.");
return;
}
glActiveTexture(GL_TEXTURE0 + tunit);
cbo->Program->SetUniformi("texture1", tunit);
this->BuildVBO(cbo, points, 6, NULL, 0, texCoord);
this->SetMatrices(cbo->Program);
glDrawArrays(GL_TRIANGLES, 0, 6);
this->RenderWindow->GetTextureUnitManager()->Free(tunit);
// free everything
cbo->ReleaseGraphicsResources(this->RenderWindow);
glDeleteTextures(1, &index);
vtkOpenGLCheckErrorMacro("failed after DrawImage");
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::SetColor4(unsigned char *)
{
vtkErrorMacro("color cannot be set this way\n");
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::SetColor(unsigned char *)
{
vtkErrorMacro("color cannot be set this way\n");
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::SetTexture(vtkImageData* image, int properties)
{
if (image == NULL)
{
if (this->Storage->Texture)
{
this->Storage->Texture->Delete();
this->Storage->Texture = 0;
}
return;
}
if (this->Storage->Texture == NULL)
{
this->Storage->Texture = vtkTexture::New();
}
this->Storage->Texture->SetInputData(image);
this->Storage->TextureProperties = properties;
this->Storage->Texture->SetRepeat(properties & vtkContextDevice2D::Repeat);
this->Storage->Texture->SetInterpolate(properties & vtkContextDevice2D::Linear);
this->Storage->Texture->EdgeClampOn();
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::SetPointSize(float size)
{
glPointSize(size);
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::SetLineWidth(float width)
{
glLineWidth(width);
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::SetLineType(int type)
{
if (type == vtkPen::SOLID_LINE || type == vtkPen::NO_PEN)
{
return;
}
vtkWarningMacro(<< "Line Stipples are no longer supported");
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::MultiplyMatrix(vtkMatrix3x3 *m)
{
// We must construct a 4x4 matrix from the 3x3 matrix for OpenGL
double *M = m->GetData();
double matrix[16];
matrix[0] = M[0];
matrix[1] = M[1];
matrix[2] = 0.0;
matrix[3] = M[2];
matrix[4] = M[3];
matrix[5] = M[4];
matrix[6] = 0.0;
matrix[7] = M[5];
matrix[8] = 0.0;
matrix[9] = 0.0;
matrix[10] = 1.0;
matrix[11] = 0.0;
matrix[12] = M[6];
matrix[13] = M[7];
matrix[14] = 0.0;
matrix[15] = M[8];
this->ModelMatrix->Concatenate(matrix);
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::SetMatrix(vtkMatrix3x3 *m)
{
// We must construct a 4x4 matrix from the 3x3 matrix for OpenGL
double *M = m->GetData();
double matrix[16];
matrix[0] = M[0];
matrix[1] = M[1];
matrix[2] = 0.0;
matrix[3] = M[2];
matrix[4] = M[3];
matrix[5] = M[4];
matrix[6] = 0.0;
matrix[7] = M[5];
matrix[8] = 0.0;
matrix[9] = 0.0;
matrix[10] = 1.0;
matrix[11] = 0.0;
matrix[12] = M[6];
matrix[13] = M[7];
matrix[14] = 0.0;
matrix[15] = M[8];
this->ModelMatrix->SetMatrix(matrix);
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::GetMatrix(vtkMatrix3x3 *m)
{
assert("pre: non_null" && m != NULL);
// We must construct a 4x4 matrix from the 3x3 matrix for OpenGL
double *M = m->GetData();
double *matrix = this->ModelMatrix->GetMatrix()->Element[0];
M[0] = matrix[0];
M[1] = matrix[1];
M[2] = matrix[3];
M[3] = matrix[4];
M[4] = matrix[5];
M[5] = matrix[7];
M[6] = matrix[12];
M[7] = matrix[13];
M[8] = matrix[15];
m->Modified();
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::PushMatrix()
{
this->ModelMatrix->Push();
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::PopMatrix()
{
this->ModelMatrix->Pop();
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::SetClipping(int *dim)
{
// Check the bounds, and clamp if necessary
GLint vp[4] = { this->Storage->Offset.GetX(), this->Storage->Offset.GetY(),
this->Storage->Dim.GetX(),this->Storage->Dim.GetY()};
if (dim[0] > 0 && dim[0] < vp[2] )
{
vp[0] += dim[0];
}
if (dim[1] > 0 && dim[1] < vp[3])
{
vp[1] += dim[1];
}
if (dim[2] > 0 && dim[2] < vp[2])
{
vp[2] = dim[2];
}
if (dim[3] > 0 && dim[3] < vp[3])
{
vp[3] = dim[3];
}
glScissor(vp[0], vp[1], vp[2], vp[3]);
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::EnableClipping(bool enable)
{
if (enable)
{
glEnable(GL_SCISSOR_TEST);
}
else
{
glDisable(GL_SCISSOR_TEST);
}
}
//-----------------------------------------------------------------------------
bool vtkOpenGLContextDevice2D::SetStringRendererToFreeType()
{
// FreeType is the only choice - nothing to do here
return true;
}
//-----------------------------------------------------------------------------
bool vtkOpenGLContextDevice2D::SetStringRendererToQt()
{
// The Qt based strategy is not available
return false;
}
//----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::ReleaseGraphicsResources(vtkWindow *window)
{
this->VBO->ReleaseGraphicsResources(window);
this->VCBO->ReleaseGraphicsResources(window);
this->SBO->ReleaseGraphicsResources(window);
this->SCBO->ReleaseGraphicsResources(window);
this->VTBO->ReleaseGraphicsResources(window);
if (this->Storage->Texture)
{
this->Storage->Texture->ReleaseGraphicsResources(window);
}
if (this->Storage->SpriteTexture)
{
this->Storage->SpriteTexture->ReleaseGraphicsResources(window);
}
this->Storage->TextTextureCache.ReleaseGraphicsResources(window);
this->Storage->MathTextTextureCache.ReleaseGraphicsResources(window);
}
//----------------------------------------------------------------------------
bool vtkOpenGLContextDevice2D::HasGLSL()
{
return true;
}
//-----------------------------------------------------------------------------
vtkImageData *vtkOpenGLContextDevice2D::GetMarker(int shape, int size,
bool highlight)
{
// Generate the cache key for this marker
vtkTypeUInt64 key = highlight ? (1U << 31) : 0U;
key |= static_cast<vtkTypeUInt16>(shape);
key <<= 32;
key |= static_cast<vtkTypeUInt32>(size);
// Try to find the marker in the cache
std::list<vtkMarkerCacheObject>::iterator match =
std::find(this->MarkerCache.begin(), this->MarkerCache.end(), key);
// Was it in the cache?
if (match != this->MarkerCache.end())
{
// Yep -- move it to the front and return the data.
if (match == this->MarkerCache.begin())
{
return match->Value;
}
else
{
vtkMarkerCacheObject result = *match;
this->MarkerCache.erase(match);
this->MarkerCache.push_front(result);
return result.Value;
}
}
// Nope -- we'll need to generate it. Create the image data:
vtkMarkerCacheObject result;
result.Key = key;
result.Value = this->GenerateMarker(shape, size, highlight);
// If there was an issue generating the marker, just return NULL.
if (!result.Value)
{
vtkErrorMacro(<<"Error generating marker: shape,size: "
<< shape << "," << size)
return NULL;
}
// Check the current cache size.
while (this->MarkerCache.size() >
static_cast<size_t>(this->MaximumMarkerCacheSize - 1) &&
!this->MarkerCache.empty())
{
this->MarkerCache.back().Value->Delete();
this->MarkerCache.pop_back();
}
// Add to the cache
this->MarkerCache.push_front(result);
return result.Value;
}
//-----------------------------------------------------------------------------
vtkImageData *vtkOpenGLContextDevice2D::GenerateMarker(int shape, int width,
bool highlight)
{
// Set up the image data, if highlight then the mark shape is different
vtkImageData *result = vtkImageData::New();
result->SetExtent(0, width - 1, 0, width - 1, 0, 0);
result->AllocateScalars(VTK_UNSIGNED_CHAR, 4);
unsigned char* image =
static_cast<unsigned char*>(result->GetScalarPointer());
memset(image, 0, width * width * 4);
// Generate the marker image at the required size
switch (shape)
{
case VTK_MARKER_CROSS:
{
int center = (width + 1) / 2;
for (int i = 0; i < center; ++i)
{
int j = width - i - 1;
memset(image + (4 * (width * i + i)), 255, 4);
memset(image + (4 * (width * i + j)), 255, 4);
memset(image + (4 * (width * j + i)), 255, 4);
memset(image + (4 * (width * j + j)), 255, 4);
if (highlight)
{
memset(image + (4 * (width * (j-1) + (i))), 255, 4);
memset(image + (4 * (width * (i+1) + (i))), 255, 4);
memset(image + (4 * (width * (i) + (i+1))), 255, 4);
memset(image + (4 * (width * (i) + (j-1))), 255, 4);
memset(image + (4 * (width * (i+1) + (j))), 255, 4);
memset(image + (4 * (width * (j-1) + (j))), 255, 4);
memset(image + (4 * (width * (j) + (j-1))), 255, 4);
memset(image + (4 * (width * (j) + (i+1))), 255, 4);
}
}
break;
}
default: // Maintaining old behavior, which produces plus for unknown shape
vtkWarningMacro(<<"Invalid marker shape: " << shape);
VTK_FALLTHROUGH;
case VTK_MARKER_PLUS:
{
int center = (width + 1) / 2;
for (int i = 0; i < center; ++i)
{
int j = width - i - 1;
int c = center - 1;
memset(image + (4 * (width * c + i)), 255, 4);
memset(image + (4 * (width * c + j)), 255, 4);
memset(image + (4 * (width * i + c)), 255, 4);
memset(image + (4 * (width * j + c)), 255, 4);
if (highlight)
{
memset(image + (4 * (width * (c - 1) + i)), 255, 4);
memset(image + (4 * (width * (c + 1) + i)), 255, 4);
memset(image + (4 * (width * (c - 1) + j)), 255, 4);
memset(image + (4 * (width * (c + 1) + j)), 255, 4);
memset(image + (4 * (width * i + (c - 1))), 255, 4);
memset(image + (4 * (width * i + (c + 1))), 255, 4);
memset(image + (4 * (width * j + (c - 1))), 255, 4);
memset(image + (4 * (width * j + (c + 1))), 255, 4);
}
}
break;
}
case VTK_MARKER_SQUARE:
{
memset(image, 255, width * width * 4);
break;
}
case VTK_MARKER_CIRCLE:
{
double r = width/2.0;
double r2 = r * r;
for (int i = 0; i < width; ++i)
{
double dx2 = (i - r) * (i - r);
for (int j = 0; j < width; ++j)
{
double dy2 = (j - r) * (j - r);
if ((dx2 + dy2) < r2)
{
memset(image + (4 * width * i) + (4 * j), 255, 4);
}
}
}
break;
}
case VTK_MARKER_DIAMOND:
{
int r = width/2;
for (int i = 0; i < width; ++i)
{
int dx = abs(i - r);
for (int j = 0; j < width; ++j)
{
int dy = abs(j - r);
if (r - dx >= dy)
{
memset(image + (4 * width * i) + (4 * j), 255, 4);
}
}
}
break;
}
}
return result;
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextDevice2D::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Renderer: ";
if (this->Renderer)
{
os << endl;
this->Renderer->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << "(none)" << endl;
}
os << indent << "Text Renderer: ";
if (this->TextRenderer)
{
os << endl;
this->TextRenderer->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << "(none)" << endl;
}
os << indent << "MaximumMarkerCacheSize: "
<< this->MaximumMarkerCacheSize << endl;
os << indent << "MarkerCache: " << this->MarkerCache.size()
<< " entries." << endl;
}
|