/usr/src/castle-game-engine-4.1.1/x3d/opengl/glrenderer_texture.inc is in castle-game-engine-src 4.1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | {$ifdef read_interface}
{ }
TGLTextureNode = class;
TGLTextureNodeClass = class of TGLTextureNode;
{ OpenGL handling for VRML/X3D texture node. }
TGLTextureNode = class(TResourceRenderer)
protected
{ Calculate things from TextureProperties node.
If TextureProperties = @nil, they are taken from defaults
(possibly in Attributes). }
procedure HandleTexturePropertiesCore(
TextureProperties: TTexturePropertiesNode;
out MinFilter, MagFilter: TGLint;
out Anisotropy: TGLfloat);
{ Calculate things from TextureProperties node.
If TextureProperties = @nil or not of TTexturePropertiesNode class,
they are taken from defaults (possibly in Attributes).
This is useful when interpreting VRML/X3D files,
as you have no guarantee user didn't place there some disallowed node
in "textureProperties" field. }
procedure HandleTextureProperties(
TextureProperties: TX3DNode;
out MinFilter, MagFilter: TGLint;
out Anisotropy: TGLfloat);
{ Decide if this class can handle given texture Node. }
class function IsClassForTextureNode(
ANode: TAbstractTextureNode): boolean; virtual; abstract;
{ Do the time-consuming preparations before rendering this texture.
Called from TGLRenderer.Prepare, always through our
Prepare method. Our Prepare method takes care to catch
common exceptions from this (EFramebufferError, ETextureLoadError),
convert them to OnWarning and make Unprepare to undo the preparations.
So Prepare-Unprepare work like constructor-destructor, with Unprepare
having to be prepared to finilizing incomplete instance. }
procedure PrepareCore(State: TX3DGraphTraverseState); virtual; abstract;
public
{ ANode must be TAbstractTextureNode }
constructor Create(ARenderer: TGLRenderer; ANode: TX3DNode); override;
{ Reference to handled texture node.
Never @nil.
It's guaranteed to satisfy IsClassForTextureNode method of this class. }
function TextureNode: TAbstractTextureNode;
{ Find suitable TGLTextureNode class that can best handle given Node.
Returns @nil if not found.
@italic(Descedants implementors): override IsClassForTextureNode
to be correctly recognized by this. }
class function ClassForTextureNode(
ANode: TAbstractTextureNode): TGLTextureNodeClass;
{ Do the time-consuming preparations before rendering this texture.
Calls PrepareCore. }
procedure Prepare(State: TX3DGraphTraverseState);
public
{ Bind texture for OpenGL (without enabling it).
Just like Enable, returns @false when texture node was not successfully
prepared for OpenGL. Returns @true when it was successfully bound
(caller can be sure then that given texture unit is currently active). }
function Bind(const TextureUnit: Cardinal): boolean; virtual; abstract;
{ Enables texture for OpenGL. This has to bind texture identifier
and enable proper texture state (for example:
2D, and not 3D, and not cube).
When returns @false, it means that texture node was not successfully
prepared for OpenGL, which means (we assume that you called Prepare
before Enable) that texture failed to load, required not available
OpenGL version / extension etc. Caller will then disable
the texture unit, and you don't have to generate tex coords for it.
When returns @true (success) caller can be sure that the specified
TextureUnit is currently bound (if OpenGL multitexturing
extensions are available at all). This is useful, if you want
to later adjust texture unit parameters, like
glTexEnvi(GL_TEXTURE_ENV, ...).
It's also already enabled (by glEnable(GL_TEXTURE_2D /
GL_TEXTURE_CUBE_MAP_ARB / GL_TEXTURE_3D) ). }
function Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean; virtual; abstract;
{ Sets texture state for many texture units, based on this node.
On every texture unit where something is enabled,
proper texture identifier must be bound.
Also, has to set complete glTexEnv on every enabled texture unit.
TextureUnitsCount says how many texture units can be enabled/disabled.
TextureUnitsCount does *not* take into account whether multitexturing
OpenGL extensions are available at all.
Look at GLFeatures.UseMultiTexturing for this.
Think of GLFeatures.UseMultiTexturing as capping TextureUnitsCount to 1
(still, remember to honour TextureUnitsCount = 0 case in your implementation,
even when GLFeatures.UseMultiTexturing = @true).
You have to set TexCoordsNeeded, this is the count of texture units
where some texture coordinates should be generated.
This means that all texture units above TexCoordsNeeded
(to TextureUnitsCount - 1) should be disabled by the caller (no need to
do this in EnableAll),
and there's no need to generated texture coords for them.
(
Yes, there is some small optimization missed in the definition
of TexCoordsNeeded: if some textures in the middle of
multitexture children list failed to load, but some following children
succeded, we'll generate tex coords even for the useless texture units
in the middle. We could avoid generating texture coords for them,
by changing TexCoordsNeeded into bool array.
This optimization is not considered worthy implementing for now.
)
You have to set texture state of all texture units < TexCoordsNeeded,
and only on them. }
procedure EnableAll(
const TextureUnitsCount: Cardinal;
out TexCoordsNeeded: Cardinal;
Shader: TShader); virtual; abstract;
end;
TGLTextureNodes = class(TResourceRendererList)
private
function GetItems(const Index: Integer): TGLTextureNode;
public
{ Looks for item with given ANode.
Returns -1 if not found. }
function TextureNodeIndex(ANode: TAbstractTextureNode): Integer;
{ Looks for item with given ANode.
Returns @nil if not found. }
function TextureNode(ANode: TAbstractTextureNode): TGLTextureNode;
property Items[Index: Integer]: TGLTextureNode read GetItems; default;
{ Prepare texture node, adding it to the list, if not prepared already.
Accepts multi texture or not-multi texture nodes, accepts (and ignores)
also @nil as TextureNode.
Ignore not handled node classes.
Returns created (or already existing) TGLTextureNode,
it may be useful in case of TGLMultiTextureNode.Prepare implementation.
Returns @nil if Node not suitable for TGLTextureNode (not handled,
or not really a texture node at all). }
function Prepare(State: TX3DGraphTraverseState;
ANode: TAbstractTextureNode;
ARenderer: TGLRenderer): TGLTextureNode;
{ Only bind texture node. Calls TGLTextureNode.Enable method.
If no texture renderer is prepared for this node,
returns @false. }
function Bind(ANode: TAbstractTextureNode;
const TextureUnit: Cardinal): boolean;
{ Enable and bind texture node. Calls TGLTextureNode.Enable method.
If no texture renderer is prepared for this node,
returns @false. }
function Enable(ANode: TAbstractTextureNode;
const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean;
{ Prepare / unprepare all textures inside Node.InterfaceDeclarations
SFNode / MFNode fields. }
procedure PrepareInterfaceDeclarationsTextures(const ANode: TX3DNode;
const State: TX3DGraphTraverseState;
const Renderer: TGLRenderer);
procedure UnprepareInterfaceDeclarationsTextures(const ANode: TX3DNode;
const Renderer: TGLRenderer);
end;
{ Common class for all single (not multi-texture) texture nodes.
Implements EnableAll method, by calling @link(Enable) call.
Override only @link(Enable) in descendants. }
TGLSingleTextureNode = class(TGLTextureNode)
protected
{ Determines should texture mode be suited for RGB and grayscale texture.
Default false, you should set this in descendant
(latest possibility is to set this is a successfull Enable call,
but usually you want to do it in a Prepare call).
You can set it by SetTextureRGBFromImage. }
TextureRGB: boolean;
procedure SetTextureRGBFromImage(Image: TEncodedImage);
public
procedure EnableAll(const TextureUnitsCount: Cardinal;
out TexCoordsNeeded: Cardinal; Shader: TShader); override;
end;
{ Handler for TMultiTextureNode.
This is somewhat special, in that it will use other TGLTextureNode
handlers to handle single textures inside --- but this is completely
hidden from the interface. }
TGLMultiTextureNode = class(TGLTextureNode)
private
PreparedTextureChildren: TX3DNodeList;
protected
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override;
procedure PrepareCore(State: TX3DGraphTraverseState); override;
public
{ ANode must be TMultiTextureNode }
constructor Create(ARenderer: TGLRenderer; ANode: TX3DNode); override;
destructor Destroy; override;
function TextureNode: TMultiTextureNode;
procedure Unprepare; override;
function Bind(const TextureUnit: Cardinal): boolean; override;
function Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean; override;
procedure EnableAll(const TextureUnitsCount: Cardinal;
out TexCoordsNeeded: Cardinal; Shader: TShader); override;
end;
{ Handler for TAbstractTexture2DNode with image (not a video). }
TGLImageTextureNode = class(TGLSingleTextureNode)
protected
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override;
procedure PrepareCore(State: TX3DGraphTraverseState); override;
public
GLName: TGLuint;
NormalMap, HeightMap: TGLuint;
HeightMapScale: Single;
function TextureNode: TAbstractTexture2DNode;
procedure Unprepare; override;
function Bind(const TextureUnit: Cardinal): boolean; override;
function Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean; override;
end;
TGLMovieTextureNode = class(TGLSingleTextureNode)
protected
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override;
procedure PrepareCore(State: TX3DGraphTraverseState); override;
public
GLVideo: TGLVideo;
function TextureNode: TMovieTextureNode;
procedure Unprepare; override;
function Bind(const TextureUnit: Cardinal): boolean; override;
function Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean; override;
end;
TGLRenderedTextureNode = class(TGLSingleTextureNode)
private
RenderToTexture: TGLRenderToTexture;
protected
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override;
procedure PrepareCore(State: TX3DGraphTraverseState); override;
public
GLName: TGLuint;
{ The actual decided image size and mipmap status. }
Width, Height: Cardinal;
NeedsMipmaps: boolean;
DepthMap: boolean;
function TextureNode: TRenderedTextureNode;
procedure Unprepare; override;
function Bind(const TextureUnit: Cardinal): boolean; override;
function Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean; override;
procedure Update(
const Render: TRenderFromViewFunction;
const ProjectionNear, ProjectionFar: Single;
var NeedsRestoreViewport: boolean;
CurrentViewpoint: TAbstractViewpointNode;
CameraViewKnown: boolean;
const CameraPosition, CameraDirection, CameraUp: TVector3Single);
end;
{ Common handling for texture nodes of TAbstractEnvironmentTextureNode. }
TGLCubeMapTextureNode = class(TGLSingleTextureNode)
public
GLName: TGLuint;
function TextureNode: TAbstractEnvironmentTextureNode;
{ Releases GLName by TextureCubeMap_DecReference.
Suitable for descendants tht initialize GLName by
TextureCubeMap_IncReference. }
procedure Unprepare; override;
function Bind(const TextureUnit: Cardinal): boolean; override;
function Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean; override;
end;
TGLComposedCubeMapTextureNode = class(TGLCubeMapTextureNode)
protected
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override;
procedure PrepareCore(State: TX3DGraphTraverseState); override;
public
function TextureNode: TComposedCubeMapTextureNode;
end;
TGLImageCubeMapTextureNode = class(TGLCubeMapTextureNode)
protected
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override;
procedure PrepareCore(State: TX3DGraphTraverseState); override;
public
function TextureNode: TImageCubeMapTextureNode;
end;
TGLGeneratedCubeMapTextureNode = class(TGLCubeMapTextureNode)
private
RenderToTexture: TGLRenderToTexture;
protected
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override;
procedure PrepareCore(State: TX3DGraphTraverseState); override;
public
{ The right size of the texture,
that satisfies all OpenGL cube map sizes requirements
(IsCubeMapTextureSized). }
Size: Cardinal;
{ This says if MinFilter needs mipmaps. }
NeedsMipmaps: boolean;
function TextureNode: TGeneratedCubeMapTextureNode;
procedure Unprepare; override;
procedure Update(
const Render: TRenderFromViewFunction;
const ProjectionNear, ProjectionFar: Single;
var NeedsRestoreViewport: boolean;
const CubeMiddle: TVector3Single);
end;
TGL3DTextureNode = class(TGLSingleTextureNode)
protected
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override;
procedure PrepareCore(State: TX3DGraphTraverseState); override;
public
GLName: TGLuint;
function TextureNode: TAbstractTexture3DNode;
procedure Unprepare; override;
function Bind(const TextureUnit: Cardinal): boolean; override;
function Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean; override;
end;
TGLGeneratedShadowMap = class(TGLSingleTextureNode)
private
RenderToTexture: TGLRenderToTexture;
{ VarianceShadowMaps calculated at the PrepareCore time. }
VarianceShadowMaps: boolean;
NeedsMipmaps: boolean;
protected
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override;
procedure PrepareCore(State: TX3DGraphTraverseState); override;
public
GLName: TGLuint;
{ The right size of the texture,
that satisfies all OpenGL sizes requirements. }
Size: Cardinal;
{ Do we visualize depth map, because of Attributes.VisualizeDepthMap
or compareMode = NONE. }
VisualizeDepthMap: boolean;
function TextureNode: TGeneratedShadowMapNode;
procedure Unprepare; override;
function Bind(const TextureUnit: Cardinal): boolean; override;
function Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean; override;
procedure Update(
const Render: TRenderFromViewFunction;
const ProjectionNear, ProjectionFar: Single;
var NeedsRestoreViewport: boolean;
Light: TAbstractLightNode);
{ Check would we use Variance Shadow Maps with current ARenderer
attributes and OpenGL version/extensions. }
class function ClassVarianceShadowMaps(Attributes: TRenderingAttributes): boolean;
end;
TGLShaderTexture = class(TGLSingleTextureNode)
protected
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override;
procedure PrepareCore(State: TX3DGraphTraverseState); override;
public
procedure Unprepare; override;
function Bind(const TextureUnit: Cardinal): boolean; override;
function Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean; override;
end;
{$endif read_interface}
{$ifdef read_implementation}
function TextureRepeatToGL(const VrmlTextureRepeat: boolean): TGLenum;
begin
if VrmlTextureRepeat then
Result := GL_REPEAT else
{ GL_CLAMP is useless if VRML doesn't allow to control texture border color,
and CLAMP_TO_EDGE is the more natural clamping method anyway...
Hm, but X3D specification seems to indicate that normal clamp is OpenGL's CLAMP,
and CLAMP_TO_EDGE is available by TextureProperties.boundaryMode*.
But until this will get implemented, it's much safer (and more sensible?)
to use GL_CLAMP_TO_EDGE here. }
Result := GLFeatures.CLAMP_TO_EDGE;
end;
{ TGLTextureNode ------------------------------------------------------------- }
constructor TGLTextureNode.Create(ARenderer: TGLRenderer; ANode: TX3DNode);
begin
Assert(ANode is TAbstractTextureNode, 'TGLTextureNode.Create acceps as Node only TAbstractTextureNode');
inherited;
end;
procedure TGLTextureNode.HandleTexturePropertiesCore(
TextureProperties: TTexturePropertiesNode;
out MinFilter, MagFilter: TGLint;
out Anisotropy: TGLfloat);
function StrToMinFilter(S: string): TGLint;
begin
S := UpperCase(S);
if S = 'AVG_PIXEL' then Result := GL_LINEAR else
if S = 'AVG_PIXEL_AVG_MIPMAP' then Result := GL_LINEAR_MIPMAP_LINEAR else
if S = 'AVG_PIXEL_NEAREST_MIPMAP' then Result := GL_LINEAR_MIPMAP_NEAREST else
if S = 'NEAREST_PIXEL_AVG_MIPMAP' then Result := GL_NEAREST_MIPMAP_LINEAR else
if S = 'NEAREST_PIXEL_NEAREST_MIPMAP' then Result := GL_NEAREST_MIPMAP_NEAREST else
if S = 'NEAREST_PIXEL' then Result := GL_NEAREST else
if S = 'DEFAULT' then Result := Renderer.Attributes.TextureMinFilter else
if S = 'FASTEST' then Result := GL_NEAREST else
if S = 'NICEST' then Result := GL_LINEAR_MIPMAP_LINEAR else
if S = 'NEAREST' then
begin
OnWarning(wtMajor, 'VRML/X3D', Format('"%s" is not allowed texture minification, this is an Avalon-only extension, please fix to "NEAREST_PIXEL"', [S]));
Result := GL_NEAREST;
end else
if S = 'LINEAR' then
begin
OnWarning(wtMajor, 'VRML/X3D', Format('"%s" is not allowed texture minification, this is an Avalon-only extension, please fix to "AVG_PIXEL"', [S]));
Result := GL_LINEAR;
end else
begin
Result := Renderer.Attributes.TextureMinFilter;
OnWarning(wtMajor, 'VRML/X3D', Format('Unknown texture minification filter "%s"', [S]));
end;
end;
function StrToMagFilter(S: string): TGLint;
begin
S := UpperCase(S);
if S = 'AVG_PIXEL' then Result := GL_LINEAR else
if S = 'NEAREST_PIXEL' then Result := GL_NEAREST else
if S = 'DEFAULT' then Result := Renderer.Attributes.TextureMagFilter else
if S = 'FASTEST' then Result := GL_NEAREST else
if S = 'NICEST' then Result := GL_LINEAR else
if S = 'NEAREST' then
begin
OnWarning(wtMajor, 'VRML/X3D', Format('"%s" is not allowed texture magnification, this is an Avalon-only extension, please fix to "NEAREST_PIXEL"', [S]));
Result := GL_NEAREST;
end else
if S = 'LINEAR' then
begin
OnWarning(wtMajor, 'VRML/X3D', Format('"%s" is not allowed texture magnification, this is an Avalon-only extension, please fix to "AVG_PIXEL"', [S]));
Result := GL_LINEAR;
end else
begin
Result := Renderer.Attributes.TextureMagFilter;
OnWarning(wtMajor, 'VRML/X3D', Format('Unknown texture minification filter "%s"', [S]));
end;
end;
begin { HandleTextureProperties }
if TextureProperties <> nil then
begin
MinFilter := StrToMinFilter(TextureProperties.FdMinificationFilter.Value);
MagFilter := StrToMagFilter(TextureProperties.FdMagnificationFilter.Value);
Anisotropy := TextureProperties.FdAnisotropicDegree.Value;
end else
begin
MinFilter := Renderer.Attributes.TextureMinFilter;
MagFilter := Renderer.Attributes.TextureMagFilter;
Anisotropy := 1;
end;
end { HandleTextureProperties };
procedure TGLTextureNode.HandleTextureProperties(
TextureProperties: TX3DNode;
out MinFilter, MagFilter: TGLint;
out Anisotropy: TGLfloat);
begin
if (TextureProperties = nil) or
not (TextureProperties is TTexturePropertiesNode) then
HandleTexturePropertiesCore(nil, MinFilter, MagFilter, Anisotropy) else
HandleTexturePropertiesCore(TTexturePropertiesNode(TextureProperties),
MinFilter, MagFilter, Anisotropy);
end;
class function TGLTextureNode.ClassForTextureNode(
ANode: TAbstractTextureNode): TGLTextureNodeClass;
function TryResult(C: TGLTextureNodeClass): boolean;
begin
Result := C.IsClassForTextureNode(ANode);
if Result then
ClassForTextureNode := C;
end;
begin
{ TODO: in the future, some way of registering class for this will
be done. For now, just try known final TGLTextureNode descendants. }
if not (TryResult(TGLMultiTextureNode) or
TryResult(TGLImageTextureNode) or
TryResult(TGLMovieTextureNode) or
TryResult(TGLRenderedTextureNode) or
TryResult(TGLComposedCubeMapTextureNode) or
TryResult(TGLImageCubeMapTextureNode) or
TryResult(TGLGeneratedCubeMapTextureNode) or
TryResult(TGL3DTextureNode) or
TryResult(TGLGeneratedShadowMap) or
TryResult(TGLShaderTexture) ) then
Result := nil;
end;
procedure TGLTextureNode.Prepare(State: TX3DGraphTraverseState);
begin
try
PrepareCore(State);
except
on E: EFramebufferError do
begin
Unprepare;
OnWarning(wtMinor, 'VRML/X3D', 'Framebuffer error, generated texture not possible: '
+ E.Message);
end;
on E: ETextureLoadError do
begin
Unprepare;
OnWarning(wtMinor, 'VRML/X3D', 'Cannot load texture to OpenGL: ' + E.Message);
end;
end;
end;
function TGLTextureNode.TextureNode: TAbstractTextureNode;
begin
Result := TAbstractTextureNode(inherited Node);
end;
{ TGLTextureNodes ------------------------------------------------------------ }
function TGLTextureNodes.TextureNodeIndex(ANode: TAbstractTextureNode): Integer;
begin
Result := inherited NodeIndex(ANode);
end;
function TGLTextureNodes.TextureNode(ANode: TAbstractTextureNode): TGLTextureNode;
begin
Result := TGLTextureNode(inherited Node(ANode));
end;
function TGLTextureNodes.GetItems(const Index: Integer): TGLTextureNode;
begin
Result := TGLTextureNode(inherited Items[Index]);
end;
function TGLTextureNodes.Prepare(State: TX3DGraphTraverseState;
ANode: TAbstractTextureNode;
ARenderer: TGLRenderer): TGLTextureNode;
var
GLTextureNodeClass: TGLTextureNodeClass;
begin
Result := nil;
if ANode <> nil then
begin
GLTextureNodeClass := TGLTextureNodeClass.ClassForTextureNode(ANode);
if (GLTextureNodeClass <> nil { Ignore if not handled node. }) and
(TextureNodeIndex(ANode) = -1) then
begin
Result := GLTextureNodeClass.Create(ARenderer, ANode);
Result.Prepare(State);
Add(Result);
end;
end;
end;
function TGLTextureNodes.Bind(ANode: TAbstractTextureNode;
const TextureUnit: Cardinal): boolean;
var
GLTextureNode: TGLTextureNode;
begin
GLTextureNode := TextureNode(ANode);
Result := GLTextureNode <> nil;
if Result then
Result := GLTextureNode.Bind(TextureUnit);
end;
function TGLTextureNodes.Enable(ANode: TAbstractTextureNode;
const TextureUnit: Cardinal; Shader: TShader; const Env: TTextureEnv): boolean;
var
GLTextureNode: TGLTextureNode;
begin
GLTextureNode := TextureNode(ANode);
Result := GLTextureNode <> nil;
if Result then
Result := GLTextureNode.Enable(TextureUnit, Shader, Env);
end;
procedure TGLTextureNodes.PrepareInterfaceDeclarationsTextures(
const ANode: TX3DNode;
const State: TX3DGraphTraverseState; const Renderer: TGLRenderer);
{ If TextureNode <> @nil and is a texture node, prepare it. }
procedure PrepareTexture(TextureNode: TX3DNode);
begin
if (TextureNode <> nil) and
(TextureNode is TAbstractTextureNode) then
Prepare(State, TAbstractTextureNode(TextureNode), Renderer);
end;
var
I, J: Integer;
UniformField: TX3DField;
IDecls: TX3DInterfaceDeclarationList;
begin
IDecls := ANode.InterfaceDeclarations;
if IDecls = nil then Exit; { ignore nodes without interface declararations }
for I := 0 to IDecls.Count - 1 do
begin
UniformField := IDecls.Items[I].Field;
if UniformField <> nil then
begin
if UniformField is TSFNode then
begin
PrepareTexture(TSFNode(UniformField).Value);
end else
if UniformField is TMFNode then
begin
for J := 0 to TMFNode(UniformField).Count - 1 do
PrepareTexture(TMFNode(UniformField)[J]);
end;
end;
end;
end;
procedure TGLTextureNodes.UnprepareInterfaceDeclarationsTextures(
const ANode: TX3DNode; const Renderer: TGLRenderer);
{ If TextureNode <> @nil and is a texture node, prepare it. }
procedure UnprepareTexture(TextureNode: TX3DNode);
begin
if (TextureNode <> nil) and
(TextureNode is TAbstractTextureNode) then
Renderer.GLTextureNodes.Unprepare(TAbstractTextureNode(TextureNode));
end;
var
I, J: Integer;
UniformField: TX3DField;
IDecls: TX3DInterfaceDeclarationList;
begin
IDecls := ANode.InterfaceDeclarations;
if IDecls = nil then Exit; { ignore nodes without interface declararations }
for I := 0 to IDecls.Count - 1 do
begin
UniformField := IDecls.Items[I].Field;
if UniformField <> nil then
begin
if UniformField is TSFNode then
begin
UnprepareTexture(TSFNode(UniformField).Value);
end else
if UniformField is TMFNode then
begin
for J := 0 to TMFNode(UniformField).Count - 1 do
UnprepareTexture(TMFNode(UniformField)[J]);
end;
end;
end;
end;
{ TGLSingleTextureNode ------------------------------------------------------- }
procedure TGLSingleTextureNode.SetTextureRGBFromImage(Image: TEncodedImage);
begin
TextureRGB :=
(Image is TS3TCImage) or
(Image is TRGBImage) or
(Image is TRGBAlphaImage) or
(Image is TRGBFloatImage);
end;
procedure TGLSingleTextureNode.EnableAll(const TextureUnitsCount: Cardinal;
out TexCoordsNeeded: Cardinal; Shader: TShader);
var
Env: TTextureEnv;
begin
TexCoordsNeeded := 0;
{ initialize Env using Attributes.TextureModeRGB/Grayscale }
if TextureRGB then
Env.Init(Renderer.Attributes.TextureModeRGB) else
Env.Init(Renderer.Attributes.TextureModeGrayscale);
if (TextureUnitsCount > 0) and
Enable(0, Shader, Env) then
begin
{ Here we know that Env was initialized above.
So we know that mode is simple, the same for rgb and alpha,
and can be set using GL_TEXTURE_ENV_MODE
(doesn't require using GL_COMBINE). }
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, Env.Combine[cRGB]);
TexCoordsNeeded := 1;
end;
end;
{ TGLMultiTextureNode -------------------------------------------------------- }
constructor TGLMultiTextureNode.Create(ARenderer: TGLRenderer; ANode: TX3DNode);
begin
Assert(ANode is TMultiTextureNode, 'TGLMultiTextureNode.Create acceps as Node only TMultiTextureNode');
inherited;
PreparedTextureChildren := TX3DNodeList.Create(false);
end;
destructor TGLMultiTextureNode.Destroy;
begin
FreeAndNil(PreparedTextureChildren);
inherited;
end;
class function TGLMultiTextureNode.IsClassForTextureNode(
ANode: TAbstractTextureNode): boolean;
begin
Result := ANode is TMultiTextureNode;
end;
function TGLMultiTextureNode.TextureNode: TMultiTextureNode;
begin
Result := TMultiTextureNode(inherited TextureNode);
end;
procedure TGLMultiTextureNode.PrepareCore(State: TX3DGraphTraverseState);
var
ChildTex: TX3DNode;
I: Integer;
begin
{ Prepare all children.
Fill PreparedTextureChildren along the way,
they will be used in Unprepare. }
PreparedTextureChildren.Count := 0;
for I := 0 to TextureNode.FdTexture.Count - 1 do
begin
ChildTex := TextureNode.FdTexture[I];
if (ChildTex <> nil) and
(ChildTex is TAbstractTextureNode) then
begin
if ChildTex is TMultiTextureNode then
OnWarning(wtMajor, 'VRML/X3D', 'Child of MultiTexture node cannot be another MultiTexture node') else
begin
PreparedTextureChildren.Add(ChildTex);
Renderer.GLTextureNodes.Prepare(State, TAbstractTextureNode(ChildTex), Renderer);
end;
end;
end;
end;
procedure TGLMultiTextureNode.Unprepare;
var
I: Integer;
begin
{ Below comments concerns engine before 2.0.0. After 2.0.0 this could actually
by simplified, no need for PreparedTextureChildren.
We cannot look at TextureNode.FdTexture here.
Reason: TextureNode may be already freed at the Unprepare time.
GLRenderer explicitly allows to free vrml nodes before
unpreparing them. This is used e.g. by doing TCastleSceneCore.Load
(that calls ChangedAll, that calls Unprepare, only at the end,
*after* old nodes have been already freed.)
Accessing here TextureNode.FdTexture.Count e.g. crashes on
multiple_viewports: open models/bridge_final.x3dv, then open
models/boxes.x3dv. (Note that multiple_viewports loads new scene
by simple Scene.Load.)
That's why we use PreparedTextureChildren saved.
Note: we cannot save TGLTextureNode values, as they could be freed
by something beside our control. So PreparedTextureChildren holds
only vrml nodes, passed to Renderer.GLTextureNodes.Unprepare. }
for I := 0 to PreparedTextureChildren.Count - 1 do
Renderer.GLTextureNodes.Unprepare(PreparedTextureChildren[I]);
end;
function TGLMultiTextureNode.Bind(const TextureUnit: Cardinal): boolean;
begin
{ TGLMultiTextureNode cannot set only one texture unit.
This may be called from GLSL shader, when someone will use MultiTexture
node for a shader uniform field. I don't know how this should be handled,
I guess returning failure is Ok for now. }
Result := false;
end;
function TGLMultiTextureNode.Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean;
begin
{ This should never be called. TGLMultiTextureNode cannot set only one
texture unit. }
Result := false;
end;
procedure TGLMultiTextureNode.EnableAll(const TextureUnitsCount: Cardinal;
out TexCoordsNeeded: Cardinal; Shader: TShader);
const
ColorSourceGL: array [TColorSource] of TGLint =
( GL_PRIMARY_COLOR, GL_TEXTURE, GL_CONSTANT, GL_PREVIOUS );
Argument: array [ta0..ta1] of Integer = ( 0, 1 );
var
ChildTex: TX3DNode;
I: Integer;
Success: boolean;
Env: TTextureEnv;
ModeStr, SourceStr, FunctionStr: string;
begin
{ calculate TexCoordsNeeded }
TexCoordsNeeded := Min(TextureUnitsCount, TextureNode.FdTexture.Count);
if not GLFeatures.UseMultiTexturing then
MinTo1st(TexCoordsNeeded, 1);
for I := 0 to TexCoordsNeeded - 1 do
begin
ChildTex := TextureNode.FdTexture[I];
Success := false;
if (ChildTex <> nil) and
(ChildTex is TAbstractTextureNode) then
begin
if I < TextureNode.FdMode.Count then
ModeStr := TextureNode.FdMode.Items[I] else
ModeStr := '';
if I < TextureNode.FdSource.Count then
SourceStr := TextureNode.FdSource.Items[I] else
SourceStr := '';
if I < TextureNode.FdFunction.Count then
FunctionStr := TextureNode.FdFunction.Items[I] else
FunctionStr := '';
Env.Init(ModeStr, SourceStr, FunctionStr);
if ChildTex is TMultiTextureNode then
OnWarning(wtMajor, 'VRML/X3D', 'Child of MultiTexture node cannot be another MultiTexture node') else
Success := Renderer.GLTextureNodes.Enable(
TAbstractTextureNode(ChildTex), I, Shader, Env);
{ Apply Env for fixed-function pipeline. }
if Success and GLFeatures.UseMultiTexturing { needed OpenGL exts available } then
begin
{ Set all the multitexture mode-related stuff.
Below we handle TextureNode.mode, source, color, alpha,
function fields. }
if Env.Disabled then
begin
{ When mode=OFF, turn off the texture unit. }
Renderer.DisableCurrentTexture;
end else
begin
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, Env.Combine[cRGB]);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, Env.Combine[cAlpha]);
glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE, Env.Scale[cRGB]);
glTexEnvf(GL_TEXTURE_ENV, GL_ALPHA_SCALE, Env.Scale[cAlpha]);
if Env.CurrentTextureArgument[cRGB] <> taNone then
begin
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB + Argument[Env.CurrentTextureArgument[cRGB]], GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB + Argument[Env.CurrentTextureArgument[cRGB]], GL_SRC_COLOR);
end;
if Env.CurrentTextureArgument[cAlpha] <> taNone then
begin
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA + Argument[Env.CurrentTextureArgument[cAlpha]], GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA + Argument[Env.CurrentTextureArgument[cAlpha]], GL_SRC_ALPHA);
end;
if Env.SourceArgument[cRGB] <> taNone then
begin
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB + Argument[Env.SourceArgument[cRGB]], ColorSourceGL[Env.Source[cRGB]]);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB + Argument[Env.SourceArgument[cRGB]], GL_SRC_COLOR);
end;
if Env.SourceArgument[cAlpha] <> taNone then
begin
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA + Argument[Env.SourceArgument[cAlpha]], ColorSourceGL[Env.Source[cAlpha]]);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA + Argument[Env.SourceArgument[cAlpha]], GL_SRC_ALPHA);
end;
if (Env.Combine[cRGB] = GL_INTERPOLATE) or
(Env.Combine[cAlpha] = GL_INTERPOLATE) then
begin
{ Whole source2 (both RGB and alpha) is filled by alpha from color
specified by InterpolateAlphaSource. }
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, ColorSourceGL[Env.InterpolateAlphaSource]);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, ColorSourceGL[Env.InterpolateAlphaSource]);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA);
end;
if Env.NeedsConstantColor then
begin
{ Assign constant color now, when we know it should be used. }
glTexEnvv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, Vector4Single(
TextureNode.FdColor.Value,
TextureNode.FdAlpha.Value));
end;
end;
end;
end;
if not Success then
Renderer.DisableTexture(I);
end;
end;
{ TGLImageTextureNode -------------------------------------------------------- }
class function TGLImageTextureNode.IsClassForTextureNode(
ANode: TAbstractTextureNode): boolean;
begin
Result := (ANode is TAbstractTexture2DNode) and
TAbstractTexture2DNode(ANode).IsTextureImage;
end;
function TGLImageTextureNode.TextureNode: TAbstractTexture2DNode;
begin
Result := TAbstractTexture2DNode(inherited TextureNode);
end;
procedure TGLImageTextureNode.PrepareCore(State: TX3DGraphTraverseState);
var
MinFilter, MagFilter: TGLint;
Anisotropy: TGLfloat;
TextureWrap: TTextureWrap2D;
begin
{ HandledNode already made sure IsTextureImage = @true }
HandleTexturePropertiesCore(TextureNode.TextureProperties,
MinFilter, MagFilter, Anisotropy);
TextureWrap[0] := TextureRepeatToGL(TextureNode.RepeatS);
TextureWrap[1] := TextureRepeatToGL(TextureNode.RepeatT);
SetTextureRGBFromImage(TextureNode.TextureImage);
GLName := Renderer.Cache.TextureImage_IncReference(
TextureNode.TextureImage,
TextureNode.TextureUsedFullUrl,
TextureNode,
MinFilter,
MagFilter,
Anisotropy,
TextureWrap,
TextureNode.TextureDDS);
end;
procedure TGLImageTextureNode.Unprepare;
begin
if GLName <> 0 then
begin
Renderer.Cache.TextureImage_DecReference(GLName);
GLName := 0;
end;
end;
function TGLImageTextureNode.Bind(const TextureUnit: Cardinal): boolean;
begin
Result := GLName <> 0;
if not Result then Exit;
Renderer.ActiveTexture(TextureUnit);
glBindTexture(GL_TEXTURE_2D, GLName);
end;
function TGLImageTextureNode.Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean;
begin
Result := Bind(TextureUnit);
if not Result then Exit;
Shader.EnableTexture(TextureUnit, tt2D, TextureNode, Env);
end;
{ TGLMovieTextureNode -------------------------------------------------------- }
class function TGLMovieTextureNode.IsClassForTextureNode(
ANode: TAbstractTextureNode): boolean;
begin
{ Although for most code TGLMovieTextureNode, it would be enought
to have any TAbstractTexture2DNode with IsTextureVideo = @true.
For when rendering, we'll need some TMovieTextureNode properties
to choose video frame.
Anyway, TMovieTextureNode is for now the only texture node possible
that may have IsTextureVideo = @true, so it's not a real problem for now. }
Result := (ANode is TMovieTextureNode) and
TMovieTextureNode(ANode).IsTextureVideo;
end;
function TGLMovieTextureNode.TextureNode: TMovieTextureNode;
begin
Result := TMovieTextureNode(inherited TextureNode);
end;
procedure TGLMovieTextureNode.PrepareCore(State: TX3DGraphTraverseState);
var
MinFilter, MagFilter: TGLint;
Anisotropy: TGLfloat;
TextureWrap: TTextureWrap2D;
begin
{ HandledNode already made sure IsTextureVideo = @true }
HandleTexturePropertiesCore(TextureNode.TextureProperties,
MinFilter, MagFilter, Anisotropy);
TextureWrap[0] := TextureRepeatToGL(TextureNode.RepeatS);
TextureWrap[1] := TextureRepeatToGL(TextureNode.RepeatT);
SetTextureRGBFromImage(TextureNode.TextureVideo.Items[0]);
GLVideo := Renderer.Cache.TextureVideo_IncReference(
TextureNode.TextureVideo,
TextureNode.TextureUsedFullUrl,
TextureNode,
MinFilter,
MagFilter,
Anisotropy,
TextureWrap);
end;
procedure TGLMovieTextureNode.Unprepare;
begin
if GLVideo <> nil then
Renderer.Cache.TextureVideo_DecReference(GLVideo);
end;
function TGLMovieTextureNode.Bind(const TextureUnit: Cardinal): boolean;
var
VideoTime: TFloatTime;
begin
{ Note: don't call IsTextureImage, IsTextureVideo here --- this
would cause reloading images/videos, nullifying
TCastleSceneCore.FreeResources([frTextureDataInNodes]) purpose.
Actually, it would be safe to call this for non-MovieTexture nodes,
as they should be prepared to display lists before doing
FreeResources. But for MovieTexture nodes it's forbidden,
as it's called at every frame render. }
Result := GLVideo <> nil;
if not Result then Exit;
VideoTime := TextureNode.TimeDependentNodeHandler.ElapsedTimeInCycle *
TextureNode.FdSpeed.Value;
if TextureNode.FdSpeed.Value < 0 then
VideoTime := TextureNode.Duration + VideoTime;
Renderer.ActiveTexture(TextureUnit);
glBindTexture(GL_TEXTURE_2D, GLVideo.GLTextureFromTime(VideoTime));
end;
function TGLMovieTextureNode.Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean;
begin
Result := Bind(TextureUnit);
if not Result then Exit;
Shader.EnableTexture(TextureUnit, tt2D, TextureNode, Env);
end;
{ TGLRenderedTextureNode ----------------------------------------------------- }
class function TGLRenderedTextureNode.IsClassForTextureNode(
ANode: TAbstractTextureNode): boolean;
begin
Result := ANode is TRenderedTextureNode;
end;
function TGLRenderedTextureNode.TextureNode: TRenderedTextureNode;
begin
Result := TRenderedTextureNode(inherited TextureNode);
end;
procedure TGLRenderedTextureNode.PrepareCore(State: TX3DGraphTraverseState);
var
InitialImage: TCastleImage;
MinFilter, MagFilter: TGLint;
Anisotropy: TGLfloat;
TextureWrap: TTextureWrap2D;
NodeWidth, NodeHeight: Cardinal;
begin
HandleTextureProperties(TextureNode.FdTextureProperties.Value,
MinFilter, MagFilter, Anisotropy);
{ calculate MinFilter, MagFilter, Anisotropy, NeedsMipmaps }
NeedsMipmaps := TextureMinFilterNeedsMipmaps(MinFilter);
if NeedsMipmaps and not HasGenerateMipmap then
begin
OnWarning(wtMinor, 'VRML/X3D' { This may be caused by OpenGL implementation
limits, so it may be impossible to predict by VRML author,
so it's "ignorable" warning. },
'OpenGL implementation doesn''t allow any glGenerateMipmap* version, so you cannot use mipmaps for RenderedTexture');
MinFilter := GL_LINEAR;
NeedsMipmaps := false;
end;
TextureWrap[0] := TextureRepeatToGL(TextureNode.FdRepeatS.Value);
TextureWrap[1] := TextureRepeatToGL(TextureNode.FdRepeatT.Value);
{ calculate Width, Height }
if TextureNode.FdDimensions.Items.Count - 1 >= 0 then
NodeWidth := Max(TextureNode.FdDimensions.Items[0], 0) else
NodeWidth := DefaultRenderedTextureWidth;
if TextureNode.FdDimensions.Items.Count - 1 >= 1 then
NodeHeight := Max(TextureNode.FdDimensions.Items[1], 0) else
NodeHeight := DefaultRenderedTextureHeight;
Width := NodeWidth ;
Height := NodeHeight;
if not IsTextureSized(Width, Height, false) then
begin
ResizeToTextureSize(Width, Height, false);
OnWarning(wtMinor, 'VRML/X3D' { This may be caused by OpenGL implementation
limits, so it may be impossible to predict by VRML author,
so it's "ignorable" warning. },
Format('Rendered texture size %d x %d is incorrect (texture size must be a power of two, > 0 and <= GL_MAX_TEXTURE_SIZE = %d), corrected to %d x %d',
[ NodeWidth, NodeHeight,
GLFeatures.MaxTextureSize,
Width, Height]));
end;
{ calculate DepthMap }
if TextureNode.FdDepthMap.Count > 0 then
DepthMap := TextureNode.FdDepthMap.Items[0] else
DepthMap := false;
if DepthMap then
begin
if not GLFeatures.ARB_depth_texture then
begin
OnWarning(wtMajor, 'VRML/X3D', 'Your OpenGL doesn''t support ARB_depth_texture, cannot use RenderedTexture with depthMap = TRUE');
Exit;
end;
GLName := Renderer.Cache.TextureDepth_IncReference(
TextureNode, TextureWrap, nil, Width, Height, false);
end else
begin
InitialImage := TRGBImage.Create(Width, Height);
try
SetTextureRGBFromImage(InitialImage);
{ Fill with deliberately stupid (but constant) color,
to recognize easily RenderedTexture which don't have textures
updated. }
InitialImage.Clear(Vector4Byte(255, 0, 255, 255));
GLName := Renderer.Cache.TextureImage_IncReference(
InitialImage,
'',
TextureNode,
MinFilter,
MagFilter,
Anisotropy,
TextureWrap,
nil);
{ RenderedTexture never has any normal / height map
(Hm, although it would be possible to generate some in theory
--- after all, we generate it from 3D data. Idea for the future.)
NormalMap := 0;
HeightMap := 0;
}
finally FreeAndNil(InitialImage) end;
end;
RenderToTexture := TGLRenderToTexture.Create(Width, Height);
RenderToTexture.SetTexture(GLName, GL_TEXTURE_2D);
if DepthMap then
RenderToTexture.Buffer := tbDepth else
RenderToTexture.Buffer := tbColor;
{ TODO: implementing this is possible.
- Add GLHasStencil to LoadAllExtensions to detect we have stencil buffer.
- Change TextureDepth_IncReference to create packed depth+stencil texture
when requested (when GLPackedDepthStencil and we currently GLHasStencil).
- set below RenderToTexture.Stencil := GLHasStencil; }
RenderToTexture.Stencil := not DepthMap;
RenderToTexture.GLContextOpen;
end;
procedure TGLRenderedTextureNode.Unprepare;
begin
FreeAndNil(RenderToTexture);
if GLName <> 0 then
begin
if DepthMap then
Renderer.Cache.TextureDepth_DecReference(GLName) else
Renderer.Cache.TextureImage_DecReference(GLName);
GLName := 0;
end;
end;
function TGLRenderedTextureNode.Bind(const TextureUnit: Cardinal): boolean;
begin
Result := GLName <> 0;
if not Result then Exit;
Renderer.ActiveTexture(TextureUnit);
glBindTexture(GL_TEXTURE_2D, GLName);
end;
function TGLRenderedTextureNode.Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean;
begin
Result := Bind(TextureUnit);
if not Result then Exit;
Shader.EnableTexture(TextureUnit, tt2D, TextureNode, Env);
end;
procedure TGLRenderedTextureNode.Update(
const Render: TRenderFromViewFunction;
const ProjectionNear, ProjectionFar: Single;
var NeedsRestoreViewport: boolean;
CurrentViewpoint: TAbstractViewpointNode;
CameraViewKnown: boolean;
const CameraPosition, CameraDirection, CameraUp: TVector3Single);
var
Viewpoint: TAbstractViewpointNode;
function GetProjectionMatrix: TMatrix4Single;
{ This somewhat follows the logic similar to TCastleScene.GLProjectionCore:
we have to calculate projection, given Viewpoint node.
We have to calculate things a little differently, e.g. we have
no NavigationInfo (it would be bad to use scene bound NavigationInfo,
since VRML author cannot change it by any RenderedTexture field,
and adding such field could be too much...),
no knowledge of scene box (although we do have ProjectionNear / Far
already).
}
procedure DoPerspective;
var
FieldOfView: Single;
AngleOfViewX, AngleOfViewY: Single;
begin
if (Viewpoint <> nil) and
(Viewpoint is TViewpointNode) then
FieldOfView := TViewpointNode(Viewpoint).FdFieldOfView.Value else
if (Viewpoint <> nil) and
(Viewpoint is TPerspectiveCameraNode_1) then
FieldOfView := TPerspectiveCameraNode_1(Viewpoint).FdHeightAngle.Value else
FieldOfView := DefaultViewpointFieldOfView;
AngleOfViewX := RadToDeg(TViewpointNode.ViewpointAngleOfView(
FieldOfView, Width / Height));
AngleOfViewY := AdjustViewAngleDegToAspectRatio(
AngleOfViewX, Height / Width);
Result := PerspectiveProjMatrixDeg(AngleOfViewY, Width / Height,
ProjectionNear, ProjectionFar);
end;
procedure DoOrthographic;
var
FieldOfView: TSingleList;
FinalFieldOfView: TVector4Single;
begin
{ default VRML/X3D fov }
FinalFieldOfView[0] := -1;
FinalFieldOfView[1] := -1;
FinalFieldOfView[2] := 1;
FinalFieldOfView[3] := 1;
{ update left / right / bottom / top using OrthoViewpoint.fieldOfView }
if (Viewpoint <> nil) and
(Viewpoint is TOrthoViewpointNode) then
begin
FieldOfView := TOrthoViewpointNode(Viewpoint).FdFieldOfView.Items;
if FieldOfView.Count > 0 then FinalFieldOfView[0] := FieldOfView.Items[0];
if FieldOfView.Count > 1 then FinalFieldOfView[1] := FieldOfView.Items[1];
if FieldOfView.Count > 2 then FinalFieldOfView[2] := FieldOfView.Items[2];
if FieldOfView.Count > 3 then FinalFieldOfView[3] := FieldOfView.Items[3];
end else
if (Viewpoint <> nil) and
(Viewpoint is TOrthographicCameraNode_1) then
begin
FinalFieldOfView[0] := -TOrthographicCameraNode_1(Viewpoint).FdHeight.Value / 2;
FinalFieldOfView[1] := -TOrthographicCameraNode_1(Viewpoint).FdHeight.Value / 2;
FinalFieldOfView[2] := TOrthographicCameraNode_1(Viewpoint).FdHeight.Value / 2;
FinalFieldOfView[3] := TOrthographicCameraNode_1(Viewpoint).FdHeight.Value / 2;
end;
TOrthoViewpointNode.AspectFieldOfView(FinalFieldOfView, Width / Height);
Result := OrthoProjMatrix(
{ Beware: order of OrthoViewpoint.fieldOfView (and FinalFieldOfView)
is different than typical OpenGL and our OrthoProjMatrix. }
FinalFieldOfView[0],
FinalFieldOfView[2],
FinalFieldOfView[1],
FinalFieldOfView[3],
ProjectionNear, ProjectionFar);
end;
var
ProjectionType: TProjectionType;
begin
if Viewpoint <> nil then
ProjectionType := Viewpoint.ProjectionType else
ProjectionType := ptPerspective;
case ProjectionType of
ptPerspective: DoPerspective;
ptOrthographic: DoOrthographic;
else EInternalError.Create('TGLRenderedTextureNode.Update-ProjectionType?');
end;
end;
procedure GetRenderedTextureCamera(out Pos, Dir, Up: TVector3Single);
var
{ It's returned by Viewpoint.GetView, but we'll ignore it. }
GravityUp: TVector3Single;
procedure GetFromCurrent;
begin
if CameraViewKnown then
begin
Pos := CameraPosition;
Dir := CameraDirection;
Up := CameraUp;
end else
if CurrentViewpoint <> nil then
CurrentViewpoint.GetView(Pos, Dir, Up, GravityUp) else
begin
{ If all else fails (no viewpoint node bound, not known current
camera settings) then use defaults. }
Pos := DefaultX3DCameraPosition[cvVrml2_X3d];
Dir := DefaultX3DCameraDirection;
Up := DefaultX3DCameraUp;
end;
end;
begin
if Viewpoint = CurrentViewpoint then
GetFromCurrent else
begin
{ Viewpoint gets assigned something different than CurrentViewpoint
only when it's non-nil. }
Assert(Viewpoint <> nil);
Viewpoint.GetView(Pos, Dir, Up, GravityUp);
end;
end;
var
CameraMatrix, CameraRotationMatrix, ProjectionMatrix: TMatrix4Single;
Pos, Dir, Up: TVector3Single;
Scene: TX3DEventsEngine;
begin
if GLName = 0 then Exit;
{ calculate Viewpoint }
if (TextureNode.FdViewpoint.Value <> nil) and
(TextureNode.FdViewpoint.Value is TAbstractViewpointNode) then
Viewpoint := TAbstractViewpointNode(TextureNode.FdViewpoint.Value) else
Viewpoint := CurrentViewpoint;
ProjectionMatrix := GetProjectionMatrix;
TextureNode.EventProjection.Send(ProjectionMatrix);
GetRenderedTextureCamera(Pos, Dir, Up);
CameraMatrix := LookDirMatrix(Pos, Dir, Up);
CameraRotationMatrix := LookDirMatrix(ZeroVector3Single, Dir, Up);
TextureNode.EventViewing.Send(CameraMatrix);
TextureNode.EventRendering.Send(true);
RenderingCamera.Target := rfOffScreen;
RenderingCamera.FromMatrix(CameraMatrix, CameraRotationMatrix, ProjectionMatrix,
Viewpoint);
RenderToTexture.RenderBegin;
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glPushMatrix;
glLoadMatrix(ProjectionMatrix);
glMatrixMode(GL_MODELVIEW);
Render;
glMatrixMode(GL_PROJECTION);
glPopMatrix;
glMatrixMode(GL_MODELVIEW);
RenderToTexture.RenderEnd;
{ depth maps (created by TextureDepth_IncReference, with GL_DEPTH_COMPONENT)
cannot have mipmaps regenerated. On Mesa 7.6 (ubuntu 10.4, Mesa DRI Intel
on "domek") glGenerateMipmapEXT even causes "invalid operation" OpenGL
error (test e.g. view3dscene on rendered_texture.x3dv). }
if NeedsMipmaps and (not DepthMap) then
RenderToTexture.GenerateMipmap;
{ We want to call IncreaseTimeTick now, otherwise our rendering.Send(false)
event would be always rejected (as the rendering.Send(true) already
traveled through through the same route at the same time,
so new event would be rejected as it can be a signal that we have
a cycle in routes.) }
Scene := TextureNode.Scene;
if Scene <> nil then
Scene.IncreaseTimeTick;
TextureNode.EventRendering.Send(false);
NeedsRestoreViewport := true;
end;
{ TGLCubeMapTextureNode ------------------------------------------------------ }
function TGLCubeMapTextureNode.TextureNode: TAbstractEnvironmentTextureNode;
begin
Result := TAbstractEnvironmentTextureNode(inherited TextureNode);
end;
procedure TGLCubeMapTextureNode.Unprepare;
begin
if GLName <> 0 then
begin
Renderer.Cache.TextureCubeMap_DecReference(GLName);
GLName := 0;
end;
end;
function TGLCubeMapTextureNode.Bind(const TextureUnit: Cardinal): boolean;
begin
Result := GLName <> 0;
if not Result then Exit;
Renderer.ActiveTexture(TextureUnit);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, GLName);
end;
function TGLCubeMapTextureNode.Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean;
begin
Result := Bind(TextureUnit);
if not Result then Exit;
Shader.EnableTexture(TextureUnit, ttCubeMap, TextureNode, Env);
end;
{ TGLComposedCubeMapTextureNode ---------------------------------------------- }
class function TGLComposedCubeMapTextureNode.IsClassForTextureNode(
ANode: TAbstractTextureNode): boolean;
begin
Result := ANode is TComposedCubeMapTextureNode;
end;
function TGLComposedCubeMapTextureNode.TextureNode: TComposedCubeMapTextureNode;
begin
Result := TComposedCubeMapTextureNode(inherited TextureNode);
end;
procedure TGLComposedCubeMapTextureNode.PrepareCore(State: TX3DGraphTraverseState);
var
MinFilter, MagFilter: TGLint;
Anisotropy: TGLfloat;
BackRot, FrontRot, LeftRot, RightRot: TCastleImage;
begin
if not GLFeatures.TextureCubeMap then
begin
OnWarning(wtMajor, 'VRML/X3D', 'Your OpenGL doesn''t support ARB_texture_cube_map, cannot use CubeMapTexture');
Exit;
end;
if not TextureNode.LoadSides then
begin
OnWarning(wtMajor, 'VRML/X3D', 'Not all sides of a CubeMapTexture are correctly set and loaded, cannot use cube map');
Exit;
end;
HandleTextureProperties(TextureNode.FdTextureProperties.Value,
MinFilter, MagFilter, Anisotropy);
try
{ To match expected orientation for OpenGL, we have to rotate images.
(source images are oriented as for VRML Background.)
We safely cast them to TCastleImage below, SideLoaded above checked
that they are indeed of TCastleImage class. }
BackRot := (TAbstractTexture2DNode(TextureNode.FdBack .Value).TextureImage as TCastleImage).MakeRotated(2);
FrontRot := (TAbstractTexture2DNode(TextureNode.FdFront.Value).TextureImage as TCastleImage).MakeRotated(2);
LeftRot := (TAbstractTexture2DNode(TextureNode.FdLeft .Value).TextureImage as TCastleImage).MakeRotated(2);
RightRot := (TAbstractTexture2DNode(TextureNode.FdRight.Value).TextureImage as TCastleImage).MakeRotated(2);
SetTextureRGBFromImage(RightRot);
GLName := Renderer.Cache.TextureCubeMap_IncReference(
TextureNode,
MinFilter, MagFilter, Anisotropy,
{ positive x } RightRot,
{ negative x } LeftRot,
{ positive y } TAbstractTexture2DNode(TextureNode.FdTop .Value).TextureImage as TCastleImage,
{ negative y } TAbstractTexture2DNode(TextureNode.FdBottom.Value).TextureImage as TCastleImage,
{ positive z } BackRot,
{ negative z } FrontRot,
nil);
finally
FreeAndNil(BackRot);
FreeAndNil(FrontRot);
FreeAndNil(LeftRot);
FreeAndNil(RightRot);
end;
end;
{ TGLImageCubeMapTextureNode ------------------------------------------------- }
class function TGLImageCubeMapTextureNode.IsClassForTextureNode(
ANode: TAbstractTextureNode): boolean;
begin
Result := ANode is TImageCubeMapTextureNode;
end;
function TGLImageCubeMapTextureNode.TextureNode: TImageCubeMapTextureNode;
begin
Result := TImageCubeMapTextureNode(inherited TextureNode);
end;
procedure TGLImageCubeMapTextureNode.PrepareCore(State: TX3DGraphTraverseState);
var
MinFilter, MagFilter: TGLint;
Anisotropy: TGLfloat;
DDS: TDDSImage;
begin
if not GLFeatures.TextureCubeMap then
begin
OnWarning(wtMajor, 'VRML/X3D', 'Your OpenGL doesn''t support ARB_texture_cube_map, cannot use CubeMapTexture');
Exit;
end;
DDS := TextureNode.LoadImage;
{ If TextureNode doesn't contain anything useful, just exit.
TextureNode.LoadImage already did necessary OnWarnings. }
if DDS = nil then Exit;
try
HandleTextureProperties(TextureNode.FdTextureProperties.Value,
MinFilter, MagFilter, Anisotropy);
{ TODO: this is a quick and dirty method:
- We call LoadImage each time, while load calls should
be minimized (to avoid loading image many times, but also
to avoid making repeated warnings in case image fails).
Should be cached, like for 2D texture nodes.
- We do not use cube map mipmaps stored inside DDS file.
}
SetTextureRGBFromImage(DDS.CubeMapImage(dcsPositiveX));
GLName := Renderer.Cache.TextureCubeMap_IncReference(
TextureNode,
MinFilter, MagFilter, Anisotropy,
DDS.CubeMapImage(dcsPositiveX),
DDS.CubeMapImage(dcsNegativeX),
{ Swap meaning of positive/negative Y faces from DDS,
see TDDSCubeMapSide for explanation. }
DDS.CubeMapImage(dcsNegativeY),
DDS.CubeMapImage(dcsPositiveY),
DDS.CubeMapImage(dcsPositiveZ),
DDS.CubeMapImage(dcsNegativeZ),
DDS);
finally FreeAndNil(DDS); end;
end;
{ TGLGeneratedCubeMapTextureNode --------------------------------------------- }
class function TGLGeneratedCubeMapTextureNode.IsClassForTextureNode(
ANode: TAbstractTextureNode): boolean;
begin
Result := ANode is TGeneratedCubeMapTextureNode;
end;
function TGLGeneratedCubeMapTextureNode.TextureNode: TGeneratedCubeMapTextureNode;
begin
Result := TGeneratedCubeMapTextureNode(inherited TextureNode);
end;
procedure TGLGeneratedCubeMapTextureNode.PrepareCore(State: TX3DGraphTraverseState);
var
MinFilter, MagFilter: TGLint;
Anisotropy: TGLfloat;
InitialImage: TCastleImage;
begin
if not GLFeatures.TextureCubeMap then
begin
OnWarning(wtMajor, 'VRML/X3D', 'Your OpenGL doesn''t support ARB_texture_cube_map, cannot use CubeMapTexture');
Exit;
end;
HandleTextureProperties(TextureNode.FdTextureProperties.Value,
MinFilter, MagFilter, Anisotropy);
{ calculate MinFilter, MagFilter, Anisotropy, NeedsMipmaps }
NeedsMipmaps := TextureMinFilterNeedsMipmaps(MinFilter);
if NeedsMipmaps and not HasGenerateMipmap then
begin
OnWarning(wtMinor, 'VRML/X3D' { This may be caused by OpenGL implementation
limits, so it may be impossible to predict by VRML author,
so it's "ignorable" warning. },
'OpenGL implementation doesn''t allow any glGenerateMipmap* version, so you cannot use mipmaps for GeneratedCubeMapTexture');
MinFilter := GL_LINEAR;
NeedsMipmaps := false;
end;
{ calculate Size }
Size := Max(TextureNode.FdSize.Value, 0);
if not IsCubeMapTextureSized(Size) then
begin
Size := ResizeToCubeMapTextureSize(Size);
OnWarning(wtMinor, 'VRML/X3D' { This may be caused by OpenGL implementation
limits, so it may be impossible to predict by VRML author,
so it's "ignorable" warning. },
Format('Cube map texture size %d is incorrect (cube map texture size must be a power of two, > 0 and <= GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB = %d), corrected to %d',
[ TextureNode.FdSize.Value,
GLFeatures.MaxCubeMapTextureSizeARB,
Size]));
end;
InitialImage := TRGBImage.Create(Size, Size);
try
{ Fill with deliberately stupid (but constant) color,
to recognize easily GeneratedCubeMapTexture which don't have textures
updated. }
if not GLVersion.BuggyGenerateCubeMap then
InitialImage.Clear(Vector4Byte(255, 0, 255, 255))
else
InitialImage.Clear(Vector4Byte(237, 237, 237, 255)); { when buggy, use some reasonable color }
SetTextureRGBFromImage(InitialImage);
GLName := Renderer.Cache.TextureCubeMap_IncReference(
TextureNode,
MinFilter, MagFilter, Anisotropy,
InitialImage, InitialImage,
InitialImage, InitialImage,
InitialImage, InitialImage,
nil);
finally FreeAndNil(InitialImage) end;
RenderToTexture := TGLRenderToTexture.Create(Size, Size);
RenderToTexture.SetTexture(GLName, GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB);
RenderToTexture.GLContextOpen;
{ Workaround for NVidia GeForce FX 5200 bug:
(Confirmed it's needed on Linux, both 32 and 64bit, Kambi's "kocury".
Confirmed it's *not* needed on Radeon (Linux 32bit, fglrx, Kambi's chantal).)
Although TextureCubeMap_IncReference (glTextureCubeMap inside) already
called initial GenerateMipmap (for our InitialImage), it's not enough.
It seems that assigning texture to FBO destroys it's mipmaps (at least
their contents).
So you have to call GenerateMipmap *after* RenderToTexture.GLContextInit
before showing this texture, to recreate mipmaps.
To see bug, comment below, and run view3scene on
demo_models/x3d/cubemap_generated_recursive.x3dv
Before pressing [space] (which triggers Update method), teapots
(or any other objects, reproducible with various IndexedFaceSets)
will have seemingly random mipmaps (base level 0 is Ok, RGB(255,0,255),
but mipmaps are seemingly filled with random garbage).
Line below fixes it at negligible cost (we'll generate mipmaps at loading
one more time than necessary). Other fix that works is to move
RenderToTexture.GLContextOpen to first Update call, but this obfuscates code. }
if GLVersion.VendorNvidia and NeedsMipmaps then
begin
RenderToTexture.CompleteTextureTarget := GL_TEXTURE_CUBE_MAP_ARB;
RenderToTexture.GenerateMipmap;
end;
end;
procedure TGLGeneratedCubeMapTextureNode.Unprepare;
begin
FreeAndNil(RenderToTexture);
inherited;
end;
procedure TGLGeneratedCubeMapTextureNode.Update(
const Render: TRenderFromViewFunction;
const ProjectionNear, ProjectionFar: Single;
var NeedsRestoreViewport: boolean;
const CubeMiddle: TVector3Single);
begin
if GLName = 0 then Exit;
if GLVersion.BuggyGenerateCubeMap then Exit;
GLCaptureCubeMapTexture(GLName, Size,
CubeMiddle,
Render, ProjectionNear, ProjectionFar,
RenderToTexture);
if NeedsMipmaps then
RenderToTexture.GenerateMipmap;
NeedsRestoreViewport := true;
end;
{ TGL3DTextureNode ----------------------------------------------------------- }
class function TGL3DTextureNode.IsClassForTextureNode(
ANode: TAbstractTextureNode): boolean;
begin
Result := ANode is TAbstractTexture3DNode;
end;
function TGL3DTextureNode.TextureNode: TAbstractTexture3DNode;
begin
Result := TAbstractTexture3DNode(inherited TextureNode);
end;
procedure TGL3DTextureNode.PrepareCore(State: TX3DGraphTraverseState);
var
MinFilter, MagFilter: TGLint;
Anisotropy: TGLfloat;
TextureWrap: TTextureWrap3D;
begin
if GLFeatures.Texture3D = gsNone then
begin
OnWarning(wtMajor, 'VRML/X3D', 'Your OpenGL doesn''t support 3D textures, cannot use Texture3D nodes');
Exit;
end;
TextureNode.TextureLoaded := true;
{ If TextureImage doesn't contain anything useful, just exit.
Setting TextureLoaded already did necessary OnWarnings. }
if TextureNode.TextureImage = nil then Exit;
HandleTextureProperties(TextureNode.FdTextureProperties.Value,
MinFilter, MagFilter, Anisotropy);
{ calculate TextureWrap }
TextureWrap[0] := TextureRepeatToGL(TextureNode.FdRepeatS.Value);
TextureWrap[1] := TextureRepeatToGL(TextureNode.FdRepeatT.Value);
TextureWrap[2] := TextureRepeatToGL(TextureNode.FdRepeatR.Value);
SetTextureRGBFromImage(TextureNode.TextureImage);
GLName := Renderer.Cache.Texture3D_IncReference(
TextureNode, MinFilter, MagFilter, Anisotropy,
TextureWrap, TextureNode.TextureImage, TextureNode.TextureDDS);
end;
procedure TGL3DTextureNode.Unprepare;
begin
if GLName <> 0 then
begin
Renderer.Cache.Texture3D_DecReference(GLName);
GLName := 0;
end;
end;
function TGL3DTextureNode.Bind(const TextureUnit: Cardinal): boolean;
begin
Result := GLName <> 0;
if not Result then Exit;
Renderer.ActiveTexture(TextureUnit);
glBindTexture(GL_TEXTURE_3D, GLName);
end;
function TGL3DTextureNode.Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean;
begin
Result := Bind(TextureUnit);
if not Result then Exit;
Shader.EnableTexture(TextureUnit, tt3D, TextureNode, Env);
end;
{ TGLGeneratedShadowMap ------------------------------------------------------ }
class function TGLGeneratedShadowMap.IsClassForTextureNode(
ANode: TAbstractTextureNode): boolean;
begin
Result := ANode is TGeneratedShadowMapNode;
end;
function TGLGeneratedShadowMap.TextureNode: TGeneratedShadowMapNode;
begin
Result := TGeneratedShadowMapNode(inherited TextureNode);
end;
class function TGLGeneratedShadowMap.ClassVarianceShadowMaps(
Attributes: TRenderingAttributes): boolean;
begin
Result :=
(Attributes.ShadowSampling = ssVarianceShadowMaps) and
(TGLSLProgram.ClassSupport <> gsNone) and
GLFeatures.TextureFloat;
end;
procedure TGLGeneratedShadowMap.PrepareCore(State: TX3DGraphTraverseState);
var
TextureWrap: TTextureWrap2D;
MinFilter: TGLint;
begin
{ Float texture extensions: there is also NV_float_buffer,
which is the only way for a float texture on old nvidia GPUs.
But it's only for NV_texture_rectangle, without bilinear filtering
or mipmapping, so it's not useful for VSM at all. }
VarianceShadowMaps := ClassVarianceShadowMaps(Renderer.Attributes);
if Log then
WritelnLog('Shadows', Format('Variance Shadow Maps used: %s (Reasons: Attributes.Shadow sampling = %s, GLSL support = %s, texture_float support = %s)',
[ BoolToStr[VarianceShadowMaps],
ShadowSamplingNames[Renderer.Attributes.ShadowSampling],
GLSupportNames[TGLSLProgram.ClassSupport],
BoolToStr[GLFeatures.TextureFloat] ]));
{ TODO: fix TextureNode.FdSize.Value if needed }
Size := TextureNode.FdSize.Value;
VisualizeDepthMap := (TextureNode.FdCompareMode.Value = 'NONE') or
Renderer.Attributes.VisualizeDepthMap;
{ At least in case of float textures for VSM, CLAMP_TO_EDGE is needed
(instead of standard clamp to border). Didn't see any difference
for depth textures. }
TextureWrap[0] := GLFeatures.CLAMP_TO_EDGE;
TextureWrap[1] := GLFeatures.CLAMP_TO_EDGE;
{ calculate MinFilter, NeedsMipmaps }
if VarianceShadowMaps and HasGenerateMipmap then
begin
NeedsMipmaps := true;
MinFilter := GL_LINEAR_MIPMAP_LINEAR;
end else
begin
NeedsMipmaps := false;
MinFilter := GL_LINEAR;
end;
if VarianceShadowMaps then
begin
GLName := Renderer.Cache.TextureFloat_IncReference(
TextureNode, MinFilter, GL_LINEAR, TextureWrap, Size, Size, true);
end else
begin
if not GLFeatures.ARB_depth_texture then
begin
OnWarning(wtMajor, 'VRML/X3D', 'Your OpenGL doesn''t support ARB_depth_texture, cannot use GeneratedShadowMap nodes');
Exit;
end;
GLName := Renderer.Cache.TextureDepth_IncReference(
TextureNode, TextureWrap, TextureNode.FdCompareMode, Size, Size,
VisualizeDepthMap);
end;
RenderToTexture := TGLRenderToTexture.Create(Size, Size);
RenderToTexture.SetTexture(GLName, GL_TEXTURE_2D);
if VarianceShadowMaps then
RenderToTexture.Buffer := tbColor else
RenderToTexture.Buffer := tbDepth;
RenderToTexture.Stencil := false;
RenderToTexture.GLContextOpen;
end;
procedure TGLGeneratedShadowMap.Unprepare;
begin
FreeAndNil(RenderToTexture);
if GLName <> 0 then
begin
if VarianceShadowMaps then
Renderer.Cache.TextureFloat_DecReference(GLName) else
Renderer.Cache.TextureDepth_DecReference(GLName);
GLName := 0;
end;
end;
function TGLGeneratedShadowMap.Bind(const TextureUnit: Cardinal): boolean;
begin
Result := GLName <> 0;
if not Result then Exit;
Renderer.ActiveTexture(TextureUnit);
glBindTexture(GL_TEXTURE_2D, GLName);
end;
function TGLGeneratedShadowMap.Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean;
begin
Result := Bind(TextureUnit);
if not Result then Exit;
Shader.EnableTexture(TextureUnit, tt2DShadow, TextureNode, Env,
Size, TextureNode.Light, VisualizeDepthMap);
end;
procedure TGLGeneratedShadowMap.Update(
const Render: TRenderFromViewFunction;
const ProjectionNear, ProjectionFar: Single;
var NeedsRestoreViewport: boolean;
Light: TAbstractLightNode);
var
ProjectionMatrix: TMatrix4Single;
SavedDepthRange: TDepthRange;
begin
if GLName = 0 then Exit;
{ Render view for shadow map }
ProjectionMatrix := Light.ProjectionMatrix;
if VarianceShadowMaps then
RenderingCamera.Target := rtVarianceShadowMap else
RenderingCamera.Target := rtShadowMap;
RenderingCamera.FromMatrix(
Light.ModelviewMatrix,
Light.ModelviewRotationMatrix,
ProjectionMatrix, nil);
RenderToTexture.RenderBegin;
glViewport(0, 0, Size, Size);
glMatrixMode(GL_PROJECTION);
glPushMatrix;
glLoadMatrix(ProjectionMatrix);
glMatrixMode(GL_MODELVIEW);
{ fix glDepthRange in case was changed by TPlayer.RenderOnTop }
SavedDepthRange := DepthRange;
DepthRange := drFull;
glPushAttrib(GL_POLYGON_BIT);
{ enable polygon offset for everything (whole scene).
VarianceShadowMaps notes: can offset be avoided in this case?
Practice (on "ATI Mobility Radeon HD 4300 Series") shows that
offset is still needed for VSM. Without offset, VSM has some noise,
and sunny_street demos have noise on walls. With offset, VSM is perfect. }
glEnable(GL_POLYGON_OFFSET_FILL); { saved by GL_POLYGON_BIT }
glEnable(GL_POLYGON_OFFSET_LINE); { saved by GL_POLYGON_BIT }
glEnable(GL_POLYGON_OFFSET_POINT); { saved by GL_POLYGON_BIT }
glPolygonOffset(TextureNode.FdScale.Value, TextureNode.FdBias.Value); { saved by GL_POLYGON_BIT }
Render;
glPopAttrib;
DepthRange := SavedDepthRange;
glMatrixMode(GL_PROJECTION);
glPopMatrix;
glMatrixMode(GL_MODELVIEW);
RenderToTexture.RenderEnd;
if NeedsMipmaps then
RenderToTexture.GenerateMipmap;
NeedsRestoreViewport := true;
end;
{ TGLShaderTexture ------------------------------------------------------ }
class function TGLShaderTexture.IsClassForTextureNode(
ANode: TAbstractTextureNode): boolean;
begin
Result := ANode is TShaderTextureNode;
end;
procedure TGLShaderTexture.PrepareCore(State: TX3DGraphTraverseState);
begin
{ no need to do anything }
end;
procedure TGLShaderTexture.Unprepare;
begin
{ no need to do anything }
end;
function TGLShaderTexture.Bind(const TextureUnit: Cardinal): boolean;
begin
Result := true;
{ no need to actually do anything }
{ TODO: ShaderTexture should not need to increase the TextureUnit.
It's not loaded to OpenGL, so it doesn't take the multitexture slot.
For now we increase it, only to pass texture coords to it. }
end;
function TGLShaderTexture.Enable(const TextureUnit: Cardinal;
Shader: TShader; const Env: TTextureEnv): boolean;
begin
Result := Bind(TextureUnit);
if not Result then Exit;
Shader.EnableTexture(TextureUnit, ttShader, TextureNode, Env);
end;
{$endif read_implementation}
|