/usr/src/castle-game-engine-4.1.1/x3d/castlearraysgenerator.pas 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 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 | {
Copyright 2002-2013 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{ Generating TGeometryArrays for VRML/X3D shapes (TArraysGenerator). }
unit CastleArraysGenerator;
interface
uses CastleShapes, X3DNodes, X3DFields, CastleUtils, CastleGeometryArrays, CastleVectors;
type
TRadianceTransferFunction = function (Node: TAbstractGeometryNode;
RadianceTransfer: PVector3Single;
const RadianceTransferCount: Cardinal): TVector3Single of object;
{ Callback used by TRenderingAttributes.OnVertexColor.
Passed here VertexPosition is in local coordinates (that is,
local of this object, multiply by State.Transform to get scene coords).
VertexIndex is the direct index to Node.Coordinates. }
TVertexColorFunction = procedure (var Color: TVector3Single;
Shape: TShape; const VertexPosition: TVector3Single;
VertexIndex: Integer) of object;
{ Generate TGeometryArrays for a VRML/X3D shape. This is the basis
of our renderer: generate a TGeometryArrays for a shape,
then TGLRenderer will pass TGeometryArrays to OpenGL.
Geometry must be based on coordinates when using this,
that is TAbstractGeometryNode.Coord must return @true. }
TArraysGenerator = class
private
FShape: TShape;
FState: TX3DGraphTraverseState;
FGeometry: TAbstractGeometryNode;
FCurrentRangeNumber: Cardinal;
FCoord: TMFVec3f;
FCoordIndex: TMFLong;
{ How Geometry and State are generated from Shape.
We have to record it, to use with Shape.Normals* later. }
OverTriangulate: boolean;
protected
{ Indexes, only when Arrays.Indexes = nil but original node was indexed. }
IndexesFromCoordIndex: TLongIntList;
{ Index to Arrays. Suitable always to index Arrays.Position / Color / Normal
and other Arrays attribute arrays. Calculated in
each TAbstractCoordinateGenerator.GenerateVertex,
always call "inherited" first fro GenerateVertex overrides.
There are three cases:
1. When CoordIndex <> nil (so we have indexed node) and
Arrays.Indexes <> nil (so we can render it by indexes,
because AllowIndexed = true) then it's an index to
node coordinates. It's equivalent to CoordIndex[IndexNum],
and it can be used to index node's Coord as well as Arrays.Position
(since they are ordered the same in this case).
2. When CoordIndex <> nil (so we have indexed node) and
Arrays.Indexes = nil (so we cannot render it by indexes,
because AllowIndexed = false) then it's a number of vertex,
that is it's incremented in each TAbstractCoordinateGenerator.GenerateVertex
call.
In this case IndexesFromCoordIndex <> nil,
and Arrays attributes have the same count as IndexesFromCoordIndex.Count.
GenerateVertex must be called in exactly the same order
as IndexesFromCoordIndex were generated for this.
3. When CoordIndex = nil (so we don't have an indexed node,
also Arrays.Indexes = IndexesFromCoordIndex = nil always in this case)
then it's an index to attributes. This is the trivial case,
as Arrays attributes are then ordered just like node's Coord.
It's equal to IndexNum then.
}
ArrayIndexNum: Integer;
{ Generated TGeometryArrays instance, available inside GenerateCoordinate*. }
Arrays: TGeometryArrays;
{ Current shape properties, constant for the whole
lifetime of the generator, set in constructor.
@groupBegin }
property Shape: TShape read FShape;
property State: TX3DGraphTraverseState read FState;
property Geometry: TAbstractGeometryNode read FGeometry;
{ @groupEnd }
procedure WarningShadingProblems(
const ColorPerVertex, NormalPerVertex: boolean);
{ Coordinates, taken from Geometry.Coord.
Usually coming from (coord as Coordinate).points field.
If @nil then nothing will be rendered.
In our constructor we initialize Coord and CoordIndex
from Geometry, using TAbstractGeometryNode.Coord and
TAbstractGeometryNode.CoordIndex values. }
property Coord: TMFVec3f read FCoord;
{ Coordinate index, taken from Geometry.CoordIndex.
If @nil, then GenerateVertex (and all other
routines taking some index) will just directly index Coord
(this is useful for non-indexed geometry, like TriangleSet
instead of IndexedTriangleSet). }
property CoordIndex: TMFLong read FCoordIndex;
{ Generate arrays content for given vertex.
Given IndexNum indexes Coord, or (if CoordIndex is assigned)
indexes CoordIndex (and CoordIndex then indexes actual Coord). }
procedure GenerateVertex(IndexNum: Integer); virtual;
{ Get vertex coordinate. Returned vertex is in local coordinate space
(use State.Transform if you want to get global coordinates). }
function GetVertex(IndexNum: integer): TVector3Single;
{ Count of indexes. You can pass index between 0 and CoordCount - 1
to various methods taking an index, like GenerateVertex. }
function CoordCount: Integer;
{ Generate contents of Arrays.
These are all called only when Coord is assigned.
GenerateCoordinate can be overridden only by the class
that actually knows how to deconstruct (triangulate etc.) this node.
It must call GenerateVertex (or call GenerateCoordsRange,
that has to be then overridden to call GenerateVertex after inherited).
GenerateCoordinateBegin, GenerateCoordinateEnd will be called
before / after GenerateCoordinate. It's useful to override them for
intermediate classes in this file, that cannot triangulate,
but still want to add something before / after GenerateCoordinate.
When overriding GenerateCoordinateBegin, always call inherited
at the begin. When overriding GenerateCoordinateEnd, always call inherited
at the end.
@groupBegin }
procedure GenerateCoordinate; virtual; abstract;
procedure GenerateCoordinateBegin; virtual;
procedure GenerateCoordinateEnd; virtual;
{ @groupEnd }
{ Generate arrays content for one coordinate range (like a face).
This is not called, not used, anywhere in this base
TAbstractCoordinateGenerator class.
In descendants, it may be useful to use this, like
Geometry.MakeCoordRanges(State, @@GenerateCoordsRange).
GenerateCoordsRange is supposed to generate the parts of the mesh
between BeginIndex and EndIndex - 1 vertices.
BeginIndex and EndIndex are indexes to CoordIndex array,
if CoordIndex is assigned, or just indexes to Coord. }
procedure GenerateCoordsRange(
const RangeNumber: Cardinal;
BeginIndex, EndIndex: Integer); virtual;
{ The number of current range (like a face), equal to RangeNumber passed to
GenerateCoordsRange. Read this only while in GenerateCoordsRange.
In fact, this is just set by GenerateCoordsRange in this class
(so call @code(inherited) first when overriding it).
It's comfortable e.g. when you need RangeNumber inside GenerateVertex,
and you know that GenerateVertex will be called only from
GenerateCoordsRange. }
property CurrentRangeNumber: Cardinal read FCurrentRangeNumber;
{ If CoordIndex assigned (this VRML/X3D node is IndexedXxx)
then calculate and set IndexesFromCoordIndex here.
This is also the place to set Arrays.Primitive and Arrays.Counts. }
procedure PrepareIndexesPrimitives; virtual; abstract;
{ Called when constructing Arrays, before the Arrays.Count is set.
Descendants can override this to do stuff like Arrays.AddColor or
Arrays.AddAttribute('foo'). Descendants can also set AllowIndexed
to @false, if we can't use indexed rendering (because e.g. we have
colors per-face, which means that the same vertex position may have
different colors, which means it has to be duplicated in arrays anyway,
so there's no point in indexing). }
procedure PrepareAttributes(var AllowIndexed: boolean); virtual;
public
{ Assign these before calling GenerateArrays.
@groupBegin }
TexCoordsNeeded: Cardinal;
MaterialOpacity: Single;
FogVolumetric: boolean;
FogVolumetricDirection: TVector3Single;
FogVolumetricVisibilityStart: Single;
ShapeBumpMappingUsed: boolean;
OnRadianceTransfer: TRadianceTransferFunction;
OnVertexColor: TVertexColorFunction;
{ Do we need TGeometryArrays.Faces }
FacesNeeded: boolean;
{ @groupEnd }
constructor Create(AShape: TShape; AOverTriangulate: boolean); virtual;
{ Create and generate Arrays contents. }
function GenerateArrays: TGeometryArrays;
class function BumpMappingAllowed: boolean; virtual;
end;
TArraysGeneratorClass = class of TArraysGenerator;
{ TArraysGenerator class suitable for given geometry.
Returns @nil if not suitable generator for this node,
which means that this node cannot be rendered through TGeometryArrays. }
function GetArraysGenerator(AGeometry: TAbstractGeometryNode): TArraysGeneratorClass;
implementation
uses SysUtils, CastleLog, FGL, CastleTriangles, CastleColors,
CastleBoxes, CastleTriangulate, CastleStringUtils, CastleWarnings;
{ Copying to interleaved memory utilities ------------------------------------ }
type
EAssignInterleavedRangeError = class(Exception);
{ Copy Source contents to given Target memory. Each item in Target
is separated by the Stride bytes.
We copy CopyCount items (you usually want to pass the count of Target
data here). Source.Count must be >= CopyCount,
we check it and eventually raise EAssignInterleavedRangeError.
Warning: this is safely usable only for arrays of types that don't
require initialization / finalization. Otherwise target memory data
will be properly referenced.
@raises EAssignInterleavedRangeError When Count < CopyCount. }
procedure AssignToInterleaved(Source: TFPSList; Target: Pointer;
const Stride, CopyCount: Cardinal); forward;
{ Copy Source contents to given Target memory. Each item in Target
is separated by the Stride bytes.
We copy CopyCount items (you usually want to pass the count of Target
data here). Indexes.Count must be >= CopyCount,
we check it and eventually raise EAssignInterleavedRangeError.
Item number I is taken from Items[Indexes[I]].
All values on Indexes list must be valid (that is >= 0 and < Source.Count),
or we raise EAssignInterleavedRangeError.
Warning: this is safely usable only for arrays of types that don't
require initialization / finalization. Otherwise target memory data
will be properly referenced.
@raises(EAssignInterleavedRangeError When Indexes.Count < CopyCount,
or some index points outside of array.) }
procedure AssignToInterleavedIndexed(Source: TFPSList; Target: Pointer;
const Stride, CopyCount: Cardinal; Indexes: TLongIntList); forward;
procedure AssignToInterleaved(Source: TFPSList; Target: Pointer;
const Stride, CopyCount: Cardinal);
var
I: Integer;
SourcePtr: Pointer;
begin
if Source.Count < CopyCount then
raise EAssignInterleavedRangeError.CreateFmt('Not enough items: %d, but at least %d required',
[Source.Count, CopyCount]);
SourcePtr := Source.List;
for I := 0 to CopyCount - 1 do
begin
Move(SourcePtr^, Target^, Source.ItemSize);
PtrUInt(SourcePtr) += Source.ItemSize;
PtrUInt(Target) += Stride;
end;
end;
procedure AssignToInterleavedIndexed(Source: TFPSList; Target: Pointer;
const Stride, CopyCount: Cardinal; Indexes: TLongIntList);
var
I: Integer;
Index: LongInt;
begin
if Indexes.Count < CopyCount then
raise EAssignInterleavedRangeError.CreateFmt('Not enough items: %d, but at least %d required',
[Indexes.Count, CopyCount]);
for I := 0 to CopyCount - 1 do
begin
Index := Indexes.L[I];
if (Index < 0) or
(Index >= Source.Count) then
raise EAssignInterleavedRangeError.CreateFmt('Invalid index: %d, but we have %d items',
[Index, Source.Count]);
{ Beware to not make multiplication below (* ItemSize) using 64-bit ints.
This would cause noticeable slowdown when using AssignToInterleavedIndexed
for ArraysGenerator, that in turn affects dynamic scenes
and especially dynamic shading like radiance_transfer. }
Move(Pointer(PtrUInt(Source.List) + PtrUInt(Index) * PtrUInt(Source.ItemSize))^,
Target^, Source.ItemSize);
PtrUInt(Target) += Stride;
end;
end;
{ classes -------------------------------------------------------------------- }
type
TTextureCoordsImplementation = (
{ Texture coords not generated, because not needed by Renderer. }
tcNotGenerated,
{ All texture coords (on all texture units, for multi-texturing)
are automatically generated.
Actually we could use some other value
like tcTexIndexed (anything else than tcNotGenerated) to indicate
this, since for everything <> tcNotGenerated we actually check
TexCoordGen to know if coords should be explicit or automatic.
But using special tcAllGenerated feels cleaner. }
tcAllGenerated,
{ IndexNum is an index to TexCoordIndex, as this indexes TexCoordGen/Array[TextureUnit]. }
tcTexIndexed,
{ IndexNum is an index to CoordIndex, as this indexes TexCoordGen/Array[TextureUnit]. }
tcCoordIndexed,
{ IndexNum is a direct index to TexCoordGen/Array[TextureUnit]. }
tcNonIndexed);
{ Handle texture coordinates.
Usage:
- TexCoord is already set from TAbstractGeometryNode.TexCoord,
so just be sure it's overriden for your Geometry node class.
Don't worry, we will automatically check TexCoord class,
and do something useful with it only if we can.
When TexCoord = @nil,
then we'll generate default texture coordinates,
following VRML 2.0 / X3D IndexedFaceSet default texture coord algorithm.
(X3D spec doesn't say what happens for nodes like
IndexedTriangleSet when texture is specified (in Appearance.texture)
but texture coords are not present.
For now, we just generate texture coords for them just like for
IndexedFaceSet.)
- Set TexCoordIndex, if available. This works just like CoordIndex:
If @nil, then GenerateVertex (and all other
routines taking some index) will just directly index TexCoord
(this is useful for non-indexed geometry, like TriangleSet
instead of IndexedTriangleSet). Otherwise, they will index TexCoordIndex,
and then TexCoordIndex provides index to TexCoord.
As a special case, if TexCoordIndex is assigned but empty
(actually, just checked as "shorter than CoordIndex")
then IndexNum indexed CoordIndex. So CoordIndex acts as
TexCoordIndex in this case. This case is specially for
for IndexedFaceSet in VRML >= 2.0.
Always when TexCoordIndex is non-nil, also make sure that CoordIndex
is non-nil. When TexCoordIndex is nil, make sure that CoordIndex is nil.
(This restriction may be removed in the future,
but for now nothing needs it.)
This class takes care to generate tex coords, or use supplied
TexCoord and TexCoordIndex. At least for texture units in
TexCoordsNeeded (although we may pass some more, will be unused).
So you do not have to generate any texture coordinates
in descendants. Everything related to textures is already
handled in this class. }
TAbstractTextureCoordinateGenerator = class(TArraysGenerator)
private
TexImplementation: TTextureCoordsImplementation;
{ Source of explicit texture coordinates, for each texture unit
that has Arrays.TexCoord[I].Generation = tgExplicit.
The value of Arrays.TexCoord[I].Dimensions determines
which of these is actually used.
These arrays have always exactly the same length,
equal to Arrays.TexCoords.Count. }
TexCoordArray2d: array of TMFVec2f;
TexCoordArray3d: array of TMFVec3f;
TexCoordArray4d: array of TMFVec4f;
TexCoord: TX3DNode;
protected
TexCoordIndex: TMFLong;
{ Return texture coordinate for given vertex, identified by IndexNum.
IndexNum indexes TexCoordGen/Array[TextureUnit], or TexCoordIndex
(if TexCoordIndex assigned),
or CoordIndex (if TexCoordIndex assigned but empty, for IndexedFaceSet).
Returns @false if no texture coords are available, for given
TextureUnit.
Works in all cases when we actually render some texture.
Overloaded version with only TVector2Single just ignores the 3rd and
4th texture coordinate, working only when texture coord is
normal 2D coord. }
function GetTextureCoord(IndexNum: integer;
const TextureUnit: Cardinal; out Tex: TVector4Single): boolean;
function GetTextureCoord(IndexNum: integer;
const TextureUnit: Cardinal; out Tex: TVector2Single): boolean;
procedure PrepareAttributes(var AllowIndexed: boolean); override;
procedure GenerateVertex(IndexNum: Integer); override;
procedure GenerateCoordinateBegin; override;
public
constructor Create(AShape: TShape; AOverTriangulate: boolean); override;
end;
TMaterials1Implementation = (miOverall,
miPerVertexCoordIndexed,
miPerVertexMatIndexed,
miPerFace,
miPerFaceMatIndexed);
{ Handle per-face or per-vertex VRML 1.0 materials.
Usage:
- Just set MaterialIndex and MaterialBinding using your node's fields.
Call UpdateMat1Implementation afterwards.
- For VRML >= 2.0 nodes, you don't have to do anything.
You can just leave MaterialBinding as default BIND_DEFAULT,
but it really doesn't matter: the only effect of this class
are calls to Render_BindMaterial_1. And Render_BindMaterial_1
simply does nothing for VRML >= 2.0 geometry nodes.
Since this must have a notion of what "face" is, it assumes that
your GenerateCoordsRange constitutes rendering of a single face.
If this isn't true, then "per face" materials will not work
correctly.
Note that "per vertex" materials require smooth shading,
so you should set this in your Render. There's no way to implement
them with flat shading.
Everything related to VRML 1.0 materials is already handled in this class. }
TAbstractMaterial1Generator = class(TAbstractTextureCoordinateGenerator)
private
{ Must be set in constructor. MaterialsBegin (may someday) depend on this.
For this reason, call UpdateMat1Implementation inside descendant
constructor after changing this. }
Mat1Implementation: TMaterials1Implementation;
FaceMaterial1Color: TVector4Single;
function GetMaterial1Color(const MaterialIndex: Integer): TVector4Single;
protected
{ You can leave MaterialIndex as nil if you are sure that
MaterialBinding will not be any _INDEXED value.
Be sure to change these only inside constructor,
and call UpdateMat1Implementation afterwards. We want Mat1Implementation
ready after constructor. }
MaterialIndex: TMFLong;
MaterialBinding: Integer;
procedure UpdateMat1Implementation;
procedure PrepareAttributes(var AllowIndexed: boolean); override;
procedure GenerateVertex(IndexNum: Integer); override;
procedure GenerateCoordsRange(const RangeNumber: Cardinal;
BeginIndex, EndIndex: integer); override;
public
constructor Create(AShape: TShape; AOverTriangulate: boolean); override;
end;
{ Handle per-face or per-vertex VRML >= 2.0 colors.
- Usage: set Color or ColorRGBA (at most one of them), ColorPerVertex,
ColorIndex.
If Color and ColorRGBA = @nil, this class will not do anything.
Otherwise, colors will be used.
ColorPerVertex specifies per-vertex or per-face.
Just like for VRML 1.0, the same restrictions apply:
- if you want per-face to work, then GenerateCoordsRange must
correspond to a single face.
- if you want per-vertex to work, you must use smooth shading.
ColorIndex: if set and non-empty, then vertex IndexNum or face number will
index ColorIndex, and then ColorIndex indexes Color items.
Otherwise, for vertex we use CoordIndex (if assigned, otherwise it directly
accesses colors)
and for face we'll use just face number.
- We also handle RadianceTransfer for all X3DComposedGeometryNode
descendants. If set, and non-empty,
and OnRadianceTransfer is defined, we will use it.
We will then ignore Color, ColorRGBA, ColorPerVertex, ColorIndex
settings --- only the colors returned by OnRadianceTransfer
will be used.
- Attributes.OnVertexColor, if assigned,
will be automatically used here to calculate color for each vertex.
If this will be assigned, then the above things
(Color, ColorRGBA, ColorPerVertex, ColorIndex, RadianceTransfer)
will be ignored -- only the colors returned by OnVertexColor will
be used.
Everything related to setting VRML 2.0
material should be set in Render_MaterialsBegin, and everything
related to VRML 2.0 colors is handled in this class.
So in summary, this class takes care of everything related to
materials / colors. }
TAbstractColorGenerator = class(TAbstractMaterial1Generator)
private
RadianceTransferVertexSize: Cardinal;
RadianceTransfer: TVector3SingleList;
FaceColor: TVector4Single;
protected
Color: TMFVec3f;
ColorRGBA: TMFColorRGBA;
ColorPerVertex: boolean;
ColorIndex: TMFLong;
procedure PrepareAttributes(var AllowIndexed: boolean); override;
procedure GenerateVertex(IndexNum: integer); override;
procedure GenerateCoordsRange(const RangeNumber: Cardinal;
BeginIndex, EndIndex: Integer); override;
end;
TNormalsImplementation = (
{ Do nothing about normals (in TAbstractNormalGenerator)
class. Passing normals to OpenGL is left for descendants. }
niNone,
{ The first item of Normals specifies the one and only normal
for the whole geometry. }
niOverall,
{ Each vertex has it's normal vector, IndexNum specifies direct index
to Normals. }
niPerVertexNonIndexed,
{ Each vertex has it's normal vector, IndexNum specifies index to
CoordIndex and this is an index to Normals. }
niPerVertexCoordIndexed,
{ Each vertex has it's normal vector, IndexNum specifies index to
NormalIndex and this is an index to Normals. }
niPerVertexNormalIndexed,
{ Face number is the index to Normals. }
niPerFace,
{ Face number is the index to NormalIndex, and this indexes Normals. }
niPerFaceNormalIndexed);
{ Handle normals, both taken from user data (that is, stored in VRML file)
and generated.
Usage:
- You have to set NorImplementation in descendant.
Default value, NorImplementation = niNone, simply means that
this class does nothing and it's your responsibility to generate
and use normal vectors. See TNormalsImplementation for other meanings,
and which properties from
Normals
NormalsCcw (should always be set when setting Normals)
NormalIndex
you also have to assign to make them work.
For VRML 1.0, you most definitely want to set both Normals
and NormalIndex and then call NorImplementation :=
NorImplementationFromVRML1Binding. This should take care of
VRML 1.0 needs completely.
- If and only if NorImplementation = niNone (either you left it as
default, or NorImplementationFromVRML1Binding returned this,
or you set this...)
you have to make appropriate glNormal calls yourself.
Normals should always point from CCW (you *do not* check here FrontFaceCcw
field, as we *do not* call glFrontFace anywhere).
If NorImplementation <> niNone then we handle everything
related to normals in this class.
Note that PerVertexXxx normals require smooth shading to work Ok. }
TAbstractNormalGenerator = class(TAbstractColorGenerator)
private
{ Will be set to Normals or it's inverted version, to keep
pointing from CCW. }
CcwNormals: TVector3SingleList;
FaceNormal: TVector3Single;
function CcwNormalsSafe(const Index: Integer): TVector3Single;
protected
NormalIndex: TMFLong;
Normals: TVector3SingleList;
NormalsCcw: boolean;
{ This is calculated in constructor. Unlike similar TexImplementation
(which is calculated only in GenerateCoordinateBegin).
Reasons:
- Descendants may want to change NorImplementation. In other words,
full automatic detection only in TAbstractNormalGenerator
is not done, it's possible in descendants to explicitly change this.
- NodeLit uses this, so it must be available after creation and
before rendering. }
NorImplementation: TNormalsImplementation;
function NorImplementationFromVRML1Binding(
NormalBinding: Integer): TNormalsImplementation;
{ Returns normal vector for given vertex, identified by IndexNum
(IndexNum has the same meaning as for GenerateVertex) and FaceNumber
(since normals may be available per-face, we need to know face number
as well as vertex number).
Returns normal always from
CCW (just like we pass to OpenGL always CCW normals, since we always
assume front face = CCW).
Override this in descendants only to handle
NorImplementation = niNone case. }
procedure GetNormal(IndexNum: Integer; RangeNumber: Integer;
out N: TVector3Single); virtual;
{ If @true, then it's guaranteed that normals for the same face will
be equal. This may be useful for various optimization purposes.
Override this in descendants only to handle
NorImplementation = niNone case. The implementation in this class
just derives it from NorImplementation, and for niNone answers @false
(safer answer). }
function NormalsFlat: boolean; virtual;
procedure GenerateCoordinateBegin; override;
procedure GenerateCoordinateEnd; override;
procedure GenerateCoordsRange(const RangeNumber: Cardinal;
BeginIndex, EndIndex: Integer); override;
procedure PrepareAttributes(var AllowIndexed: boolean); override;
procedure GenerateVertex(IndexNum: Integer); override;
end;
{ Handle fog coordinate.
Descendants don't have to do anything, this just works
(using TAbstractGeometryNode.FogCoord). }
TAbstractFogGenerator = class(TAbstractNormalGenerator)
private
FogCoord: TSingleList;
protected
procedure PrepareAttributes(var AllowIndexed: boolean); override;
procedure GenerateVertex(IndexNum: Integer); override;
public
constructor Create(AShape: TShape; AOverTriangulate: boolean); override;
end;
TX3DVertexAttributeNodes = specialize TFPGObjectList<TAbstractVertexAttributeNode>;
{ Handle GLSL attributes from VRML/X3D "attrib" field.
Descendants don't have to do anything, this just works
(using TAbstractGeometryNode.Attrib). }
TAbstractShaderAttribGenerator = class(TAbstractFogGenerator)
private
Attrib: TX3DVertexAttributeNodes;
protected
procedure PrepareAttributes(var AllowIndexed: boolean); override;
procedure GenerateVertex(IndexNum: Integer); override;
public
constructor Create(AShape: TShape; AOverTriangulate: boolean); override;
destructor Destroy; override;
end;
{ Handle bump mapping.
Descendants should:
- override BumpMappingAllowed to return true,
- call CalculateTangentVectors when needed.
- Also make sure that GetNormal always
works (since it's called by CalculateTangentVectors),
so if you may use NorImplementation = niNone: be sure to override
GetNormal to return correct normal. }
TAbstractBumpMappingGenerator = class(TAbstractShaderAttribGenerator)
private
{ Helpers for bump mapping }
HasTangentVectors: boolean;
STangent, TTangent: TVector3Single;
protected
procedure GenerateVertex(IndexNum: Integer); override;
procedure PrepareAttributes(var AllowIndexed: boolean); override;
{ Update tangent vectors (HasTangentVectors, STangent, TTangent).
Without this, bump mapping will be wrong.
Give triangle indexes (like IndexNum for GenerateVertex). }
procedure CalculateTangentVectors(
const TriangleIndex1, TriangleIndex2, TriangleIndex3: Integer);
end;
{ Most complete implementation of arrays generator,
should be used to derive non-abstract renderers for nodes. }
TAbstractCompleteGenerator = TAbstractBumpMappingGenerator;
{ TArraysGenerator ------------------------------------------------------ }
constructor TArraysGenerator.Create(AShape: TShape; AOverTriangulate: boolean);
begin
inherited Create;
FShape := AShape;
OverTriangulate := AOverTriangulate;
FGeometry := FShape.Geometry(OverTriangulate);
FState := FShape.State(OverTriangulate);
Check(Geometry.Coord(State, FCoord),
'TAbstractCoordinateRenderer is only for coordinate-based nodes');
FCoordIndex := Geometry.CoordIndex;
end;
procedure TArraysGenerator.WarningShadingProblems(
const ColorPerVertex, NormalPerVertex: boolean);
const
SPerVertex: array [boolean] of string = ('per-face', 'per-vertex');
begin
OnWarning(wtMajor, 'VRML/X3D', Format(
'Colors %s and normals %s used in the same node %s. Shading results may be incorrect',
[ SPerVertex[ColorPerVertex], SPerVertex[NormalPerVertex],
Geometry.NodeTypeName]));
end;
function TArraysGenerator.GenerateArrays: TGeometryArrays;
var
AllowIndexed: boolean;
MaxIndex: Integer;
begin
Arrays := TGeometryArrays.Create;
Result := Arrays;
{ no geometry if coordinates are empty. Leave empty Arrays. }
if Coord = nil then Exit;
{ initialize stuff for generating }
IndexesFromCoordIndex := nil;
try
ArrayIndexNum := -1;
PrepareIndexesPrimitives;
{ Assert about Arrays.Counts.Sum. Note that even when
IndexesFromCoordIndex = nil, Arrays.Counts.Sum should usually be equal
to final Arrays.Count. But not always: it may not be equal
for invalid nodes, with non-complete triangle strips / fans etc.
(then Arrays.Counts will not contain all coordinates). }
Assert(
(Arrays.Counts = nil) or
(IndexesFromCoordIndex = nil) or
(Arrays.Counts.Sum = IndexesFromCoordIndex.Count) );
if IndexesFromCoordIndex <> nil then
begin
Assert(CoordIndex <> nil);
MaxIndex := IndexesFromCoordIndex.Max;
{ check do we have enough coordinates. TGeometryArrays data may be passed
quite raw to OpenGL, so this may be our last chance to check correctness
and avoid passing data that would cause OpenGL errors. }
if MaxIndex >= Coord.Count then
begin
CoordIndex.OnWarning_WrongVertexIndex(Geometry.NodeTypeName,
MaxIndex, Coord.Count);
Exit; { leave Arrays created but empty }
end;
end;
AllowIndexed := true;
PrepareAttributes(AllowIndexed);
try
if Log and LogShapes then
WritelnLog('Renderer', Format('Shape %s is rendered with indexes: %s',
[Shape.NiceName, BoolToStr[AllowIndexed]]));
if AllowIndexed or (IndexesFromCoordIndex = nil) then
begin
Arrays.Indexes := IndexesFromCoordIndex;
IndexesFromCoordIndex := nil;
Arrays.Count := Coord.Count;
AssignToInterleaved(Coord.Items, Arrays.Position, Arrays.CoordinateSize, Arrays.Count);
end else
begin
Arrays.Count := IndexesFromCoordIndex.Count;
{ Expand IndexesFromCoordIndex, to specify vertexes multiple times }
AssignToInterleavedIndexed(Coord.Items, Arrays.Position, Arrays.CoordinateSize, Arrays.Count, IndexesFromCoordIndex);
end;
GenerateCoordinateBegin;
try
GenerateCoordinate;
finally GenerateCoordinateEnd; end;
except
on E: EAssignInterleavedRangeError do
OnWarning(wtMajor, 'VRML/X3D', Format('Invalid number of items in a normal or texture coordinate array for shape "%s": %s',
[Shape.NiceName, E.Message]));
end;
finally FreeAndNil(IndexesFromCoordIndex); end;
end;
procedure TArraysGenerator.PrepareAttributes(var AllowIndexed: boolean);
begin
if Geometry is TAbstractComposedGeometryNode then
begin
Arrays.CullBackFaces := (Geometry as TAbstractComposedGeometryNode).FdSolid.Value;
Arrays.FrontFaceCcw := (Geometry as TAbstractComposedGeometryNode).FdCcw.Value;
end;
end;
procedure TArraysGenerator.GenerateCoordinateBegin;
begin
{ nothing to do in this class }
end;
procedure TArraysGenerator.GenerateCoordinateEnd;
begin
{ nothing to do in this class }
end;
procedure TArraysGenerator.GenerateVertex(IndexNum: integer);
begin
if CoordIndex <> nil then
begin
if Arrays.Indexes = nil then
Inc(ArrayIndexNum) else
ArrayIndexNum := CoordIndex.Items.L[IndexNum];
end else
ArrayIndexNum := IndexNum;
end;
function TArraysGenerator.GetVertex(IndexNum: integer): TVector3Single;
begin
{ This assertion should never fail, it's the responsibility
of the programmer. }
Assert(IndexNum < CoordCount);
if CoordIndex <> nil then
Result := Coord.ItemsSafe[CoordIndex.Items.L[IndexNum]] else
Result := Coord.Items.L[IndexNum];
end;
function TArraysGenerator.CoordCount: Integer;
begin
if CoordIndex <> nil then
Result := CoordIndex.Items.Count else
Result := Coord.Items.Count;
end;
procedure TArraysGenerator.GenerateCoordsRange(
const RangeNumber: Cardinal; BeginIndex, EndIndex: Integer);
begin
FCurrentRangeNumber := RangeNumber;
end;
class function TArraysGenerator.BumpMappingAllowed: boolean;
begin
Result := false;
end;
{ TAbstractTextureCoordinateGenerator ----------------------------------------- }
constructor TAbstractTextureCoordinateGenerator.Create(AShape: TShape; AOverTriangulate: boolean);
begin
inherited;
if not Geometry.TexCoord(State, TexCoord) then
TexCoord := nil;
end;
procedure TAbstractTextureCoordinateGenerator.PrepareAttributes(
var AllowIndexed: boolean);
{ Is a texture used on given unit, and it's 3D texture. }
function IsTexture3D(const TexUnit: Cardinal): boolean;
{ Knowing that Tex is not nil,
check is it a single (not MultiTexture) 3D texture. }
function IsSingleTexture3D(Tex: TX3DNode): boolean;
begin
Result :=
(Tex is TAbstractTexture3DNode) or
((Tex is TShaderTextureNode) and
(TShaderTextureNode(Tex).FdDefaultTexCoord.Value = 'BOUNDS3D'));
end;
var
Tex: TAbstractTextureNode;
begin
Tex := State.Texture;
Result := (
(Tex <> nil) and
( ( (TexUnit = 0) and IsSingleTexture3D(Tex) )
or
( (Tex is TMultiTextureNode) and
(TMultiTextureNode(Tex).FdTexture.Count > TexUnit) and
IsSingleTexture3D(TMultiTextureNode(Tex).FdTexture[TexUnit])
)));
end;
{ Set length of TexCoordArray* arrays. }
procedure SetTexLengths(const Count: Integer);
begin
SetLength(TexCoordArray2d, Count);
SetLength(TexCoordArray3d, Count);
SetLength(TexCoordArray4d, Count);
end;
function Bounds2DTextureGenVectors: TTextureGenerationVectors;
var
LocalBBox: TBox3D;
LocalBBoxSize: TVector3Single;
{ Setup and enable glTexGen to make automatic 2D texture coords
based on shape bounding box. On texture unit 0. }
procedure SetupCoordGen(out Gen: TVector4Single;
const Coord: integer; const GenStart, GenEnd: Single);
{ We want to map float from range
LocalBBox[0, Coord]...LocalBBox[0, Coord] + LocalBBoxSize[Coord]
to
GenStart...GenEnd.
For a 3D point V let's define S1 as
S1 = (V[Coord] - LocalBBox[0, Coord]) / LocalBBoxSize[Coord]
and so S1 is in 0..1 range, now
S = S1 * (GenEnd - GenStart) + GenStart
and so S is in GenStart...GenEnd range, like we wanted.
It remains to rewrite this to a form that we can pass to OpenGL
glTexGenfv(..., GL_OBJECT_PLANE, ...).
S = V[Coord] * (GenEnd - GenStart) / LocalBBoxSize[Coord]
- LocalBBox[0, Coord] * (GenEnd - GenStart) / LocalBBoxSize[Coord]
+ GenStart
Simple check: for GenStart = 0, GenEnd = 1 this simplifies to
S = V[Coord] / LocalBBoxSize[Coord] -
LocalBBox[0, Coord] / LocalBBoxSize[Coord]
= (V[Coord] - LocalBBox[0, Coord]) / LocalBBoxSize[Coord]
= S1
}
begin
FillChar(Gen, SizeOf(Gen), 0);
Gen[Coord] := (GenEnd - GenStart) / LocalBBoxSize[Coord];
Gen[3] :=
- LocalBBox.Data[0, Coord] * (GenEnd - GenStart) / LocalBBoxSize[Coord]
+ GenStart;
end;
var
SCoord, TCoord: integer;
begin
LocalBBox := Shape.LocalBoundingBox;
if not LocalBBox.IsEmpty then
begin
LocalBBoxSize := LocalBBox.Sizes;
Geometry.GetTextureBounds2DST(LocalBBoxSize, SCoord, TCoord);
{ Calculate TextureGen[0..1]. }
SetupCoordGen(Result[0], SCoord, 0, 1);
SetupCoordGen(Result[1], TCoord, 0, LocalBBoxSize[TCoord] / LocalBBoxSize[SCoord]);
end else
begin
{ When local bounding box is empty, set these to any sensible value }
Result[0] := Vector4Single(1, 0, 0, 0);
Result[1] := Vector4Single(0, 1, 0, 0);
end;
Result[2] := Vector4Single(0, 0, 1, 0); //< whatever, just to be defined.
end;
function Bounds3DTextureGenVectors: TTextureGenerationVectors;
var
Box: TBox3D;
XStart, YStart, ZStart, XSize, YSize, ZSize: Single;
begin
Box := Shape.LocalBoundingBox;
if not Box.IsEmpty then
begin
{ Texture S should range from 0..1 when X changes from X1 .. X2.
So S = X / (X2 - X1) - X1 / (X2 - X1).
Same for T.
For R, X3D spec says that coords go backwards, so just SwapValues. }
SwapValues(Box.Data[0][2], Box.Data[1][2]);
XStart := Box.Data[0][0];
YStart := Box.Data[0][1];
ZStart := Box.Data[0][2];
XSize := Box.Data[1][0] - Box.Data[0][0];
YSize := Box.Data[1][1] - Box.Data[0][1];
ZSize := Box.Data[1][2] - Box.Data[0][2];
Result[0] := Vector4Single(1 / XSize, 0, 0, - XStart / XSize);
Result[1] := Vector4Single(0, 1 / YSize, 0, - YStart / YSize);
Result[2] := Vector4Single(0, 0, 1 / ZSize, - ZStart / ZSize);
end else
begin
{ When local bounding box is empty, set these to any sensible value }
Result[0] := Vector4Single(1, 0, 0, 0);
Result[1] := Vector4Single(0, 1, 0, 0);
Result[2] := Vector4Single(0, 0, 1, 0);
end;
end;
{ Initialize Arrays.TexCoords and TexCoordArray2/3/4d, based on TexCoord.
If any usable tex coords are found
(that is, Arrays.TexCoords.Count <> 0 on exit) then this array
contains at least TexCoordsNeeded units. }
procedure InitializeTexCoordGenArray;
{ Add single texture coord configuration
to Arrays.TexCoord and TexCoordArray2/3/4d.
Assume that TexCoordArray2/3/4d already have required length.
Pass any TexCoord node except TMultiTextureCoordinateNode. }
procedure AddSingleTexCoord(const TextureUnit: Cardinal; TexCoord: TX3DNode);
function TexCoordGenFromString(const S: string; const IsTexture3D: boolean): TTextureCoordinateGeneration;
begin
if S = 'SPHERE' then
Result := tgSphereMap else
if S = 'COORD' then
Result := tgCoord else
if (S = 'COORD-EYE') or (S = 'CAMERASPACEPOSITION') then
Result := tgCoordEye else
if S = 'CAMERASPACENORMAL' then
Result := tgCameraSpaceNormal else
if S = 'WORLDSPACENORMAL' then
Result := tgWorldSpaceNormal else
if S = 'CAMERASPACEREFLECTIONVECTOR' then
Result := tgCameraSpaceReflectionVector else
if S = 'WORLDSPACEREFLECTIONVECTOR' then
Result := tgWorldSpaceReflectionVector else
if S = 'PROJECTION' then
Result := tgProjection else
if S = 'BOUNDS' then
begin
if IsTexture3D then
Result := tgBounds3d else
Result := tgBounds2d;
end else
if S = 'BOUNDS2D' then
Result := tgBounds2d else
if S = 'BOUNDS3D' then
Result := tgBounds3d else
begin
Result := tgCoord;
OnWarning(wtMajor, 'VRML/X3D', Format('Unsupported TextureCoordinateGenerator.mode: "%s", will use "COORD" instead',
[S]));
end;
end;
{ For tgProjection generation, calculate function that should be
later used to calculate matrix to pass to glTexGen as eye plane.
Gets projector matrices from a TextureCoordinateGenerator or
ProjectedTextureCoordinate node.
If this projector is a correct light source or viewpoint,
then get it's projection and modelview matrix and return @true.
Returns @false (and does correct OnWarning)
if it's empty or incorrect. }
function GetProjectorMatrixFunction(GeneratorNode: TX3DNode): TProjectorMatrixFunction;
var
ProjectorValue: TX3DNode; { possible ProjectorLight or ProjectorViewpoint }
begin
Result := nil;
if GeneratorNode is TTextureCoordinateGeneratorNode then
begin
ProjectorValue := TTextureCoordinateGeneratorNode(GeneratorNode).FdProjectedLight.Value;
if (ProjectorValue <> nil) and
(ProjectorValue is TAbstractLightNode) then
begin
Result := @TAbstractLightNode(ProjectorValue).GetProjectorMatrix;
end else
OnWarning(wtMajor, 'VRML/X3D', 'Using TextureCoordinateGenerator.mode = "PROJECTION", but TextureCoordinateGenerator.projectedLight is NULL or incorrect');
end else
if GeneratorNode is TProjectedTextureCoordinateNode then
begin
ProjectorValue := TProjectedTextureCoordinateNode(GeneratorNode).FdProjector.Value;
if (ProjectorValue <> nil) and
(ProjectorValue is TAbstractLightNode) then
begin
Result := @TAbstractLightNode(ProjectorValue).GetProjectorMatrix;
end else
if (ProjectorValue <> nil) and
(ProjectorValue is TAbstractX3DViewpointNode) then
begin
Result := @TAbstractX3DViewpointNode(ProjectorValue).GetProjectorMatrix;
end else
OnWarning(wtMajor, 'VRML/X3D', 'ProjectedTextureCoordinate.projector is NULL or incorrect');
end else
{ This should not actually happen (GeneratorNode passed here should be like this) }
OnWarning(wtMajor, 'VRML/X3D', 'Invalid texture generator node');
end;
begin
if TexCoord is TTextureCoordinateNode then
begin
Arrays.AddTexCoord2D(TextureUnit);
TexCoordArray2d[TextureUnit] := TTextureCoordinateNode(TexCoord).FdPoint;
end else
if TexCoord is TTextureCoordinate2Node_1 then
begin
Arrays.AddTexCoord2D(TextureUnit);
TexCoordArray2d[TextureUnit] := TTextureCoordinate2Node_1(TexCoord).FdPoint;
end else
if TexCoord is TTextureCoordinate3DNode then
begin
Arrays.AddTexCoord3D(TextureUnit);
TexCoordArray3d[TextureUnit] := TTextureCoordinate3DNode(TexCoord).FdPoint;
end else
if TexCoord is TTextureCoordinate4DNode then
begin
Arrays.AddTexCoord4D(TextureUnit);
TexCoordArray4d[TextureUnit] := TTextureCoordinate4DNode(TexCoord).FdPoint;
end else
if TexCoord is TTextureCoordinateGeneratorNode then
begin
Arrays.AddTexCoordGenerated(
TexCoordGenFromString(TTextureCoordinateGeneratorNode(TexCoord).FdMode.Value,
IsTexture3D(TextureUnit)), TextureUnit);
end else
if TexCoord is TProjectedTextureCoordinateNode then
begin
Arrays.AddTexCoordGenerated(tgProjection, TextureUnit);
end else
begin
{ dummy default }
Arrays.AddTexCoordGenerated(tgBounds2d, TextureUnit);
if TexCoord <> nil then
OnWarning(wtMajor, 'VRML/X3D', Format('Unsupported texture coordinate node: %s, inside multiple texture coordinate', [TexCoord.NodeTypeName])) else
OnWarning(wtMajor, 'VRML/X3D', 'NULL texture coordinate node');
end;
{ Calculate some Generation-specific values }
case Arrays.TexCoords[TextureUnit].Generation of
tgBounds2d: Arrays.TexCoords[TextureUnit].GenerationBoundsVector := Bounds2DTextureGenVectors;
tgBounds3d: Arrays.TexCoords[TextureUnit].GenerationBoundsVector := Bounds3DTextureGenVectors;
tgProjection: Arrays.TexCoords[TextureUnit].GenerationProjectorMatrix := GetProjectorMatrixFunction(TexCoord);
end;
end;
var
MultiTexCoord: TX3DNodeList;
I, LastCoord: Integer;
begin
if TexCoord = nil then
begin
{ Leave TexCoords.Count = 0, no OnWarning }
Exit;
end else
if TexCoord is TMultiTextureCoordinateNode then
begin
MultiTexCoord := TMultiTextureCoordinateNode(TexCoord).FdTexCoord.Items;
SetTexLengths(MultiTexCoord.Count);
for I := 0 to MultiTexCoord.Count - 1 do
AddSingleTexCoord(I, MultiTexCoord[I]);
end else
begin
SetTexLengths(1);
AddSingleTexCoord(0, TexCoord);
end;
Assert(Arrays.TexCoords.Count = Length(TexCoordArray2d));
Assert(Arrays.TexCoords.Count = Length(TexCoordArray3d));
Assert(Arrays.TexCoords.Count = Length(TexCoordArray4d));
if (Arrays.TexCoords.Count <> 0) and
(Arrays.TexCoords.Count < TexCoordsNeeded) then
begin
LastCoord := Arrays.TexCoords.Count - 1;
SetTexLengths(TexCoordsNeeded);
{ We copy tex coord LastCoord values to all following items.
This way we do what X3D spec says:
- if non-MultiTextureCoordinate is used for multitexturing,
channel 0 is replicated
- if MultiTextureCoordinate is used, but with too few items,
last channel is replicated. }
for I := LastCoord + 1 to TexCoordsNeeded - 1 do
begin
Arrays.AddTexCoordCopy(I, LastCoord);
TexCoordArray2d[I] := TexCoordArray2d[LastCoord];
TexCoordArray3d[I] := TexCoordArray3d[LastCoord];
TexCoordArray4d[I] := TexCoordArray4d[LastCoord];
end;
end;
end;
{ Setup 2D texture coordinates suitable for IndexedFaceSet without
explicit texture coords, following X3D 3D Texturing spec.
We set texture generation to tgBounds2d
on all texture units within TexCoordsNeeded.
We also set TexImplementation := tcAllGenerated. }
procedure Bounds2DTextureGen;
var
I: integer;
TexGenVectors: TTextureGenerationVectors;
begin
TexGenVectors := Bounds2DTextureGenVectors;
TexImplementation := tcAllGenerated;
SetTexLengths(TexCoordsNeeded);
for I := 0 to TexCoordsNeeded - 1 do
begin
Arrays.AddTexCoordGenerated(tgBounds2d, I);
Arrays.TexCoords[I].GenerationBoundsVector := TexGenVectors;
end;
end;
{ Setup 3D texture coordinates suitable for "primitives without
explicit texture coordinates", following X3D 3D Texturing spec.
We set tgBounds3d on all texture units within TexCoordsNeeded.
We also set TexImplementation := tcAllGenerated. }
procedure Bounds3DTextureGen;
var
I: Integer;
TexGenVectors: TTextureGenerationVectors;
begin
TexGenVectors := Bounds3DTextureGenVectors;
TexImplementation := tcAllGenerated;
SetTexLengths(TexCoordsNeeded);
for I := 0 to TexCoordsNeeded - 1 do
begin
Arrays.AddTexCoordGenerated(tgBounds3d, I);
Arrays.TexCoords[I].GenerationBoundsVector := TexGenVectors;
end;
end;
begin
inherited;
TexImplementation := tcNotGenerated;
{ Make sure they are initially empty. }
Assert(Arrays.TexCoords.Count = 0);
Assert(Length(TexCoordArray2d) = 0);
Assert(Length(TexCoordArray3d) = 0);
Assert(Length(TexCoordArray4d) = 0);
if TexCoordsNeeded > 0 then
begin
if { Original shape node is a primitive, without explicit texCoord }
Shape.OriginalGeometry.AutoGenerate3DTexCoords and
{ First texture is 3D texture }
IsTexture3D(0) then
begin
Bounds3DTextureGen;
end else
{ Handle VRML 1.0 case when TexCoord present but TexCoordIndex empty
before calling InitializeTexCoordGenArray. That's because we have
to ignore TexCoord in this case (so says VRML 1.0 spec, and it's
sensible since for VRML 1.0 there is always some last TexCoord node).
So we don't want to let InitializeTexCoordGenArray to call
any Arrays.AddTexCoord. }
if (State.ShapeNode = nil) and
(TexCoordIndex <> nil) and
(TexCoordIndex.Count < CoordIndex.Count) then
begin
Bounds2DTextureGen;
end else
begin
InitializeTexCoordGenArray;
if Arrays.TexCoords.Count > 0 then
begin
if TexCoordIndex = nil then
begin
{ This happens only for X3D non-indexed primitives:
Triangle[Fan/Strip]Set, QuadSet. Spec says that TexCoord should be
used just like Coord, so IndexNum indexes it directly. }
TexImplementation := tcNonIndexed;
Assert(CoordIndex = nil);
end else
if TexCoordIndex.Count >= CoordIndex.Count then
begin
TexImplementation := tcTexIndexed;
end else
begin
{ If TexCoord <> nil (non-zero Arrays.TexCoords.Count guarantees this)
but TexCoordIndex is empty then
- VRML 2.0 spec says that coordIndex is used
to index texture coordinates for IndexedFaceSet.
- VRML 1.0 spec says that in this case default texture
coordinates should be generated (that's because for
VRML 1.0 there is always some TexCoord <> nil,
so it cannot be used to produce different behavior).
We handled this case in code above.
- Note that this cannot happen at all for X3D primitives
like IndexedTriangle[Fan/Strip]Set, QuadSet, since they
have TexCoordIndex = CoordIndex (just taken from "index" field).
}
Assert(State.ShapeNode <> nil);
TexImplementation := tcCoordIndexed;
end;
end else
Bounds2DTextureGen;
end;
end;
if TexImplementation in [tcTexIndexed, tcNonIndexed] then
AllowIndexed := false;
end;
procedure TAbstractTextureCoordinateGenerator.GenerateCoordinateBegin;
{ Set explicit texture coordinates in Arrays,
for texture units where Arrays.TexCoords[].Generation = tgExplicit.
This is only for TexImplementation in [tcNonIndexed, tcCoordIndexed],
other TexImplementation have to be handled elsewhere. }
procedure EnableExplicitTexCoord;
var
I: Integer;
procedure Handle(const Dimensions: TTexCoordDimensions;
TexCoordArray: TFPSList);
var
A: Pointer;
begin
A := Arrays.TexCoord(Dimensions, I, 0);
if TexImplementation = tcCoordIndexed then
begin
if Arrays.Indexes <> nil then
AssignToInterleaved(TexCoordArray, A, Arrays.AttributeSize, Arrays.Count) else
AssignToInterleavedIndexed(TexCoordArray, A, Arrays.AttributeSize, Arrays.Count, IndexesFromCoordIndex);
end else
begin
Assert(TexImplementation = tcNonIndexed);
Assert(CoordIndex = nil); { tcNonIndexed happens only for non-indexed triangle/quad primitives }
Assert(Arrays.Indexes = nil);
AssignToInterleaved(TexCoordArray, A, Arrays.AttributeSize, Arrays.Count);
end;
end;
begin
for I := 0 to Arrays.TexCoords.Count - 1 do
if Arrays.TexCoords[I].Generation = tgExplicit then
case Arrays.TexCoords[I].Dimensions of
2: Handle(2, TexCoordArray2d[I].Items);
3: Handle(3, TexCoordArray3d[I].Items);
4: Handle(4, TexCoordArray4d[I].Items);
end;
end;
begin
inherited;
if TexImplementation in [tcNonIndexed, tcCoordIndexed] then
EnableExplicitTexCoord;
end;
function TAbstractTextureCoordinateGenerator.GetTextureCoord(
IndexNum: integer; const TextureUnit: Cardinal;
out Tex: TVector4Single): boolean;
function GenerateTexCoord(const TexCoord: TGeometryTexCoord): TVector4Single;
var
Vertex: TVector3Single;
begin
case TexCoord.Generation of
tgBounds2d:
begin
Vertex := GetVertex(IndexNum);
Result[0] := VectorDotProduct(Vertex, TexCoord.GenerationBoundsVector[0]);
Result[1] := VectorDotProduct(Vertex, TexCoord.GenerationBoundsVector[1]);
Result[2] := 0;
Result[3] := 1;
end;
tgBounds3d:
begin
Vertex := GetVertex(IndexNum);
Result[0] := VectorDotProduct(Vertex, TexCoord.GenerationBoundsVector[0]);
Result[1] := VectorDotProduct(Vertex, TexCoord.GenerationBoundsVector[1]);
Result[2] := VectorDotProduct(Vertex, TexCoord.GenerationBoundsVector[2]);
Result[3] := 1;
end;
tgCoord:
begin
Vertex := GetVertex(IndexNum);
Result[0] := Vertex[0];
Result[1] := Vertex[1];
Result[2] := Vertex[2];
Result[3] := 1;
end;
else OnWarning(wtMajor, 'VRML/X3D', Format('Generating on CPU texture coordinates with %d not implemented yet',
[TexCoord.Generation]));
end;
end;
{ Sets Tex to TexCoordArray*[TextureUnit][Index] value. }
procedure SetTexFromTexCoordArray(const Dimensions: TTexCoordDimensions;
const Index: Integer);
var
Tex2d: TVector2Single absolute Tex;
Tex3d: TVector3Single absolute Tex;
begin
case Dimensions of
2: Tex2d := TexCoordArray2d[TextureUnit].ItemsSafe[Index];
3: Tex3d := TexCoordArray3d[TextureUnit].ItemsSafe[Index];
4: Tex := TexCoordArray4d[TextureUnit].ItemsSafe[Index];
end;
end;
begin
Result := TexImplementation <> tcNotGenerated;
if Result then
begin
{ This assertion should never fail, it's the responsibility
of the programmer. Note that we don't need any TexCoordCount
here, since IndexNum allowed for GetTextureCoord are the same
and come from the same range as coords. }
Assert(IndexNum < CoordCount);
{ Initialize to common values }
Tex := Vector4Single(0, 0, 0, 1);
Result := TextureUnit < Arrays.TexCoords.Count;
if not Result then Exit;
if Arrays.TexCoords[TextureUnit].Generation = tgExplicit then
begin
case TexImplementation of
tcTexIndexed:
{ tcTexIndexed is set only if
TexCoordIndex.Count >= CoordIndex.Count, so the IndexNum index
is Ok for sure. That's why we don't do "ItemsSafe"
for TexCoordIndex. }
SetTexFromTexCoordArray(Arrays.TexCoords[TextureUnit].Dimensions, TexCoordIndex.Items.L[IndexNum]);
tcCoordIndexed:
{ We already checked that IndexNum < CoordCount, so the first index
is Ok for sure. }
SetTexFromTexCoordArray(Arrays.TexCoords[TextureUnit].Dimensions, CoordIndex.Items.L[IndexNum]);
tcNonIndexed:
SetTexFromTexCoordArray(Arrays.TexCoords[TextureUnit].Dimensions, IndexNum);
else raise EInternalError.Create('TAbstractTextureCoordinateGenerator.GetTextureCoord?');
end;
end else
Tex := GenerateTexCoord(Arrays.TexCoords[TextureUnit]);
end;
end;
function TAbstractTextureCoordinateGenerator.GetTextureCoord(
IndexNum: integer; const TextureUnit: Cardinal;
out Tex: TVector2Single): boolean;
var
Tex4f: TVector4Single;
begin
Result := GetTextureCoord(IndexNum, TextureUnit, Tex4f);
Tex[0] := Tex4f[0];
Tex[1] := Tex4f[1];
end;
procedure TAbstractTextureCoordinateGenerator.GenerateVertex(indexNum: integer);
procedure DoTexCoord(Index: Integer);
var
TextureUnit: Integer;
begin
Assert(Arrays.Indexes = nil);
for TextureUnit := 0 to Arrays.TexCoords.Count - 1 do
if Arrays.TexCoords[TextureUnit].Generation = tgExplicit then
case Arrays.TexCoords[TextureUnit].Dimensions of
2: Arrays.TexCoord2D(TextureUnit, ArrayIndexNum)^ := TexCoordArray2d[TextureUnit].ItemsSafe[Index];
3: Arrays.TexCoord3D(TextureUnit, ArrayIndexNum)^ := TexCoordArray3d[TextureUnit].ItemsSafe[Index];
4: Arrays.TexCoord4D(TextureUnit, ArrayIndexNum)^ := TexCoordArray4d[TextureUnit].ItemsSafe[Index];
end;
end;
begin
inherited;
if TexImplementation = tcTexIndexed then
DoTexCoord(TexCoordIndex.Items.L[IndexNum]);
end;
{ TAbstractMaterial1Generator ------------------------------------------ }
constructor TAbstractMaterial1Generator.Create(AShape: TShape; AOverTriangulate: boolean);
begin
inherited;
MaterialBinding := BIND_DEFAULT;
UpdateMat1Implementation;
end;
procedure TAbstractMaterial1Generator.UpdateMat1Implementation;
function IndexListNotEmpty(MFIndexes: TMFLong): boolean;
begin
Result :=
(MFIndexes.Count > 0) and
{ For VRML 1.0, [-1] value is default for materialIndex
and should be treated as "empty", as far as I understand
the spec. }
(not ((MFIndexes.Count = 1) and (MFIndexes.Items.L[0] = -1)));
end;
begin
{ Calculate Mat1Implementation }
Mat1Implementation := miOverall;
case MaterialBinding of
{ BIND_OVERALL, BIND_DEFAULT: take default miOverall }
BIND_PER_VERTEX:
Mat1Implementation := miPerVertexCoordIndexed;
BIND_PER_VERTEX_INDEXED:
if IndexListNotEmpty(MaterialIndex) then
Mat1Implementation := miPerVertexMatIndexed;
BIND_PER_PART, BIND_PER_FACE:
Mat1Implementation := miPerFace;
BIND_PER_PART_INDEXED, BIND_PER_FACE_INDEXED:
if IndexListNotEmpty(MaterialIndex) then
Mat1Implementation := miPerFaceMatIndexed;
end;
{ TODO: we handle all material bindings, but we handle BIND_PER_PART
and BIND_PER_PART_INDEXED wrong for IndexedLineSet. }
end;
procedure TAbstractMaterial1Generator.PrepareAttributes(var AllowIndexed: boolean);
begin
inherited;
if Mat1Implementation in
[ miPerFace, miPerFaceMatIndexed,
miPerVertexCoordIndexed, miPerVertexMatIndexed ] then
begin
Arrays.AddColor;
if Mat1Implementation in
[ miPerFace, miPerFaceMatIndexed, miPerVertexMatIndexed ] then
AllowIndexed := false;
end;
end;
function TAbstractMaterial1Generator.GetMaterial1Color(
const MaterialIndex: Integer): TVector4Single;
var
M: TMaterialNode_1;
begin
M := State.LastNodes.Material;
if M.OnlyEmissiveMaterial then
Result := M.EmissiveColor4Single(MaterialIndex) else
Result := M.DiffuseColor4Single(MaterialIndex);
end;
procedure TAbstractMaterial1Generator.GenerateVertex(IndexNum: Integer);
begin
inherited;
case Mat1Implementation of
miPerVertexCoordIndexed:
Arrays.Color(ArrayIndexNum)^ := GetMaterial1Color(CoordIndex.ItemsSafe[IndexNum]);
miPerVertexMatIndexed:
Arrays.Color(ArrayIndexNum)^ := GetMaterial1Color(MaterialIndex.ItemsSafe[IndexNum]);
miPerFace, miPerFaceMatIndexed:
Arrays.Color(ArrayIndexNum)^ := FaceMaterial1Color;
end;
end;
procedure TAbstractMaterial1Generator.GenerateCoordsRange(
const RangeNumber: Cardinal; BeginIndex, EndIndex: Integer);
begin
inherited;
case Mat1Implementation of
miPerFace:
FaceMaterial1Color := GetMaterial1Color(RangeNumber);
miPerFaceMatIndexed:
FaceMaterial1Color := GetMaterial1Color(MaterialIndex.Items.L[RangeNumber]);
end;
end;
{ TAbstractColorGenerator --------------------------------------- }
procedure TAbstractColorGenerator.PrepareAttributes(var AllowIndexed: boolean);
begin
inherited;
if Geometry is TAbstractComposedGeometryNode then
RadianceTransfer := (Geometry as TAbstractComposedGeometryNode).FdRadianceTransfer.Items;
{ calculate final RadianceTransfer:
Leave it non-nil, and calculate RadianceTransferVertexSize,
if it's useful. }
if RadianceTransfer <> nil then
begin
if (RadianceTransfer.Count <> 0) and
Assigned(OnRadianceTransfer) then
begin
if RadianceTransfer.Count mod Coord.Count <> 0 then
begin
OnWarning(wtMajor, 'VRML/X3D', 'radianceTransfer field must be emppty, or have a number of items being multiple of coods');
RadianceTransfer := nil;
end else
if RadianceTransfer.Count < Coord.Count then
begin
OnWarning(wtMajor, 'VRML/X3D', 'radianceTransfer field must be emppty, or have a number of items >= number of coods');
RadianceTransfer := nil;
end else
RadianceTransferVertexSize := RadianceTransfer.Count div Coord.Count;
end else
RadianceTransfer := nil;
end;
if Assigned(OnVertexColor) or
(RadianceTransfer <> nil) then
begin
Arrays.AddColor;
AllowIndexed := false;
end else
if (Color <> nil) or (ColorRGBA <> nil) then
begin
Arrays.AddColor;
if (ColorIndex <> nil) or (not ColorPerVertex) then
AllowIndexed := false;
end;
end;
procedure TAbstractColorGenerator.GenerateVertex(IndexNum: integer);
var
VertexColor: TVector3Single;
VertexIndex: Cardinal;
begin
inherited;
{ Implement different color per vertex here. }
if Assigned(OnVertexColor) then
begin
if CoordIndex <> nil then
VertexIndex := CoordIndex.ItemsSafe[IndexNum] else
VertexIndex := IndexNum;
{ Get vertex color, taking various possible configurations.
OnVertexColor will be able to change it. }
if (Color <> nil) and ColorPerVertex then
begin
if (ColorIndex <> nil) and (ColorIndex.Count <> 0) then
VertexColor := Color.ItemsSafe[ColorIndex.ItemsSafe[IndexNum]] else
if CoordIndex <> nil then
VertexColor := Color.ItemsSafe[CoordIndex.ItemsSafe[IndexNum]] else
VertexColor := Color.ItemsSafe[IndexNum];
end else
if (State.ShapeNode <> nil) and
(State.ShapeNode.Material <> nil) then
begin
VertexColor := State.ShapeNode.Material.FdDiffuseColor.Value;
end else
VertexColor := White3Single; { default fallback }
OnVertexColor(VertexColor, Shape, GetVertex(IndexNum), VertexIndex);
Arrays.Color(ArrayIndexNum)^ := Vector4Single(VertexColor, MaterialOpacity);
end else
if RadianceTransfer <> nil then
begin
if CoordIndex <> nil then
VertexIndex := CoordIndex.ItemsSafe[IndexNum] else
VertexIndex := IndexNum;
VertexColor := OnRadianceTransfer(Geometry,
Addr(RadianceTransfer.L[VertexIndex * RadianceTransferVertexSize]),
RadianceTransferVertexSize);
Arrays.Color(ArrayIndexNum)^ := Vector4Single(VertexColor, MaterialOpacity);
end else
if Color <> nil then
begin
if ColorPerVertex then
begin
if (ColorIndex <> nil) and (ColorIndex.Count <> 0) then
Arrays.Color(ArrayIndexNum)^ := Vector4Single(Color.ItemsSafe[ColorIndex.ItemsSafe[IndexNum]], MaterialOpacity) else
if CoordIndex <> nil then
Arrays.Color(ArrayIndexNum)^ := Vector4Single(Color.ItemsSafe[CoordIndex.ItemsSafe[IndexNum]], MaterialOpacity) else
Arrays.Color(ArrayIndexNum)^ := Vector4Single(Color.ItemsSafe[IndexNum], MaterialOpacity);
end else
Arrays.Color(ArrayIndexNum)^ := FaceColor;
end else
if ColorRGBA <> nil then
begin
if ColorPerVertex then
begin
if (ColorIndex <> nil) and (ColorIndex.Count <> 0) then
Arrays.Color(ArrayIndexNum)^ := ColorRGBA.ItemsSafe[ColorIndex.ItemsSafe[IndexNum]] else
if CoordIndex <> nil then
Arrays.Color(ArrayIndexNum)^ := ColorRGBA.ItemsSafe[CoordIndex.ItemsSafe[IndexNum]] else
Arrays.Color(ArrayIndexNum)^ := ColorRGBA.ItemsSafe[IndexNum];
end else
Arrays.Color(ArrayIndexNum)^ := FaceColor;
end;
end;
procedure TAbstractColorGenerator.GenerateCoordsRange(
const RangeNumber: Cardinal; BeginIndex, EndIndex: Integer);
begin
inherited;
{ Implement different color per face here. }
if (not Assigned(OnVertexColor)) and
(RadianceTransfer = nil) then
begin
if (Color <> nil) and (not ColorPerVertex) then
begin
if (ColorIndex <> nil) and (ColorIndex.Count <> 0) then
FaceColor := Vector4Single(Color.ItemsSafe[ColorIndex.ItemsSafe[RangeNumber]], MaterialOpacity) else
FaceColor := Vector4Single(Color.ItemsSafe[RangeNumber], MaterialOpacity);
end else
if (ColorRGBA <> nil) and (not ColorPerVertex) then
begin
if (ColorIndex <> nil) and (ColorIndex.Count <> 0) then
FaceColor := ColorRGBA.ItemsSafe[ColorIndex.ItemsSafe[RangeNumber]] else
FaceColor := ColorRGBA.ItemsSafe[RangeNumber];
end;
end;
end;
{ TAbstractNormalGenerator ----------------------------------------------------- }
procedure TAbstractNormalGenerator.PrepareAttributes(
var AllowIndexed: boolean);
begin
inherited;
if not (
{ When IndexNum for normal works exactly like for position,
then normals can be indexed. This is true in two cases:
- there is no coordIndex, and normal vectors are not indexed
- there is coordIndex, and normal vectors are indexed by coordIndex }
(NorImplementation = niPerVertexCoordIndexed) or
((NorImplementation = niPerVertexNonIndexed) and (CoordIndex = nil)) ) then
AllowIndexed := false;
end;
function TAbstractNormalGenerator.
NorImplementationFromVRML1Binding(NormalBinding: Integer): TNormalsImplementation;
begin
Result := niNone;
if (Normals = nil) or (NormalIndex = nil) then
Exit;
case NormalBinding of
BIND_DEFAULT, BIND_PER_VERTEX_INDEXED:
if (NormalIndex.Count > 0) and (NormalIndex.Items.L[0] >= 0) then
Result := niPerVertexNormalIndexed;
BIND_PER_VERTEX:
if CoordIndex <> nil then
Result := niPerVertexCoordIndexed;
BIND_OVERALL:
if Normals.Count > 0 then
Result := niOverall;
BIND_PER_PART, BIND_PER_FACE:
Result := niPerFace;
BIND_PER_PART_INDEXED, BIND_PER_FACE_INDEXED:
if (NormalIndex.Count > 0) and (NormalIndex.Items.L[0] >= 0) then
Result := niPerFaceNormalIndexed;
end;
{ If no normals are provided (for VRML 1.0, this means that last Normal
node was empty, or it's the default empty node from DefaultLastNodes scen)
then generate normals. }
if Normals.Count = 0 then
Result := niNone;
end;
function TAbstractNormalGenerator.CcwNormalsSafe(
const Index: Integer): TVector3Single;
begin
if Index < CcwNormals.Count then
Result := CcwNormals.L[Index] else
Result := ZeroVector3Single;
end;
procedure TAbstractNormalGenerator.GetNormal(
IndexNum: Integer; RangeNumber: Integer; out N: TVector3Single);
begin
case NorImplementation of
niOverall:
N := CcwNormals.L[0];
niPerVertexNonIndexed:
N := CcwNormals.L[IndexNum];
niPerVertexCoordIndexed:
N := CcwNormals.L[CoordIndex.Items.L[IndexNum]];
niPerVertexNormalIndexed:
N := CcwNormals.L[NormalIndex.ItemsSafe[IndexNum]];
niPerFace:
N := CcwNormals.L[RangeNumber];
niPerFaceNormalIndexed:
N := CcwNormals.L[NormalIndex.ItemsSafe[RangeNumber]];
else
raise EInternalError.CreateFmt('NorImplementation unknown (probably niNone, and not overridden GetNormal) in class %s',
[ClassName]);
end;
end;
function TAbstractNormalGenerator.NormalsFlat: boolean;
begin
Result := NorImplementation in [niOverall, niPerFace, niPerFaceNormalIndexed];
end;
procedure TAbstractNormalGenerator.GenerateVertex(IndexNum: Integer);
begin
inherited;
case NorImplementation of
niPerVertexNormalIndexed:
begin
Assert(Arrays.Indexes = nil);
Arrays.Normal(ArrayIndexNum)^ := CcwNormalsSafe(NormalIndex.ItemsSafe[IndexNum]);
end;
niPerVertexNonIndexed:
if CoordIndex <> nil then
begin
Assert(Arrays.Indexes = nil);
Arrays.Normal(ArrayIndexNum)^ := CcwNormalsSafe(IndexNum);
end;
niPerFace, niPerFaceNormalIndexed:
begin
Assert(Arrays.Indexes = nil);
Arrays.Normal(ArrayIndexNum)^ := FaceNormal;
end;
end;
end;
procedure TAbstractNormalGenerator.GenerateCoordinateBegin;
procedure SetAllNormals(const Value: TVector3Single);
var
N: PVector3Single;
I: Integer;
begin
N := Arrays.Normal;
for I := 0 to Arrays.Count - 1 do
begin
N^ := Value;
Arrays.IncNormal(N);
end;
end;
begin
inherited;
if Normals <> nil then
begin
if NormalsCcw then
CcwNormals := Normals else
begin
CcwNormals := TVector3SingleList.Create;
CcwNormals.AssignNegated(Normals);
end;
end;
if NorImplementation = niPerVertexCoordIndexed then
begin
if Arrays.Indexes <> nil then
AssignToInterleaved(CcwNormals, Arrays.Normal, Arrays.CoordinateSize, Arrays.Count) else
AssignToInterleavedIndexed(CcwNormals, Arrays.Normal, Arrays.CoordinateSize, Arrays.Count, IndexesFromCoordIndex);
end else
if (NorImplementation = niPerVertexNonIndexed) and (CoordIndex = nil) then
begin
Assert(Arrays.Indexes = nil);
AssignToInterleaved(CcwNormals, Arrays.Normal, Arrays.CoordinateSize, Arrays.Count);
end else
if NorImplementation = niOverall then
begin
SetAllNormals(CcwNormalsSafe(0));
end;
end;
procedure TAbstractNormalGenerator.GenerateCoordinateEnd;
begin
if (Normals <> nil) and (not NormalsCcw) then
FreeAndNil(CcwNormals);
inherited;
end;
procedure TAbstractNormalGenerator.GenerateCoordsRange(
const RangeNumber: Cardinal; BeginIndex, EndIndex: Integer);
begin
inherited;
{ FaceNormal will be actually copied to Arrays.Normal in GenerateVertex
for this face. }
case NorImplementation of
niPerFace:
FaceNormal := CcwNormalsSafe(RangeNumber);
niPerFaceNormalIndexed:
FaceNormal := CcwNormalsSafe(NormalIndex.ItemsSafe[RangeNumber]);
end;
end;
{ TAbstractFogGenerator --------------------------------- }
constructor TAbstractFogGenerator.Create(AShape: TShape; AOverTriangulate: boolean);
begin
inherited;
if Geometry.FogCoord <> nil then
FogCoord := Geometry.FogCoord.Items;
end;
procedure TAbstractFogGenerator.PrepareAttributes(
var AllowIndexed: boolean);
begin
inherited;
if (FogCoord <> nil) or FogVolumetric then
begin
Arrays.AddFogCoord;
Arrays.FogDirectValues := FogCoord <> nil;
end;
end;
procedure TAbstractFogGenerator.GenerateVertex(
IndexNum: Integer);
function GetFogCoord: Single;
begin
{ make IndexNum independent of coordIndex, always work like index to coords }
if CoordIndex <> nil then
IndexNum := CoordIndex.Items.L[IndexNum];
{ calculate Fog, based on FogCoord and IndexNum }
if IndexNum < FogCoord.Count then
Result := FogCoord.L[IndexNum] else
if FogCoord.Count <> 0 then
Result := FogCoord.Last else
Result := 0; //< some default
end;
function GetFogVolumetric: Single;
var
Position, Projected: TVector3Single;
begin
{ calculate global vertex position }
if CoordIndex <> nil then
Position := Coord.Items.L[CoordIndex.Items.L[IndexNum]] else
Position := Coord.Items.L[IndexNum];
Position := MatrixMultPoint(State.Transform, Position);
Projected := PointOnLineClosestToPoint(
ZeroVector3Single, FogVolumetricDirection, Position);
Result := VectorLen(Projected);
if not AreParallelVectorsSameDirection(
Projected, FogVolumetricDirection) then
Result := -Result;
{ Now I want
- Result = FogVolumetricVisibilityStart -> 0
- Result = FogVolumetricVisibilityStart + X -> X
(so that Result = FogVolumetricVisibilityStart +
FogVisibilityRangeScaled -> FogVisibilityRangeScaled) }
Result -= FogVolumetricVisibilityStart;
end;
procedure SetFog(const Fog: Single);
begin
{ When Result < 0 our intention is to have no fog (at least
for volumetric fog, for explicit FogCoordinate we don't know what
was user's intention).
So Result < 0 should be equivalent to Result = 0.
However, OpenGL doesn't necessarily interpret it like this.
Since factor given by glFogCoordfEXT is interpreted just like
eye distance (i.e. it's processed by appopriate linear or exp or exp2
equations), negative values may produce quite unexpected results
(unless you really look at the equations).
This is mentioned in the extension specification
[http://oss.sgi.com/projects/ogl-sample/registry/EXT/fog_coord.txt].
First is says:
* Should the specified value be used directly as the fog weighting
factor, or in place of the z input to the fog equations?
As the z input; more flexible and meets ISV requests.
... which means that what glFogCoordfEXT gives is interpreted
just like eye distance for normal fog (so it's e.g. affected
by fog linear / exp / exp2 modes, affected by fog start and end values,
etc.). Later it says:
* Should the fog coordinate be restricted to non-negative values?
Perhaps. Eye-coordinate distance of fragments will be
non-negative due to clipping. Specifying explicit negative
coordinates may result in very large computed f values, although
they are defined to be clipped after computation.
... and this is precisely why specifying negative glFogCoordfEXT
parameters is a bad idea: you don't really know what OpenGL
implementation will do. NVidia OpenGL seems to actually assume
that factor < 0 means the same as factor = 0, so my code
worked OK without clamping below
(because NVidia OpenGL was actually doing it anyway).
Mesa 3D (and Radeon, as I suspect, because similar problems
were reported for "The Castle" on Radeon) seem to just use the negative
value directly, which causes strange artifacts
(see e.g. "The Castle" gate_final.wrl VRML file).
The clamping below makes volumetric fog work
OK as expected for all OpenGL implementations.
Note: we don't limit Fog to be <= 1 (although we could
for X3D FogCoordinate). glFogCoord is like a distance from the eye,
so it may be >= 1, and GetFogVolumetric depends on it to work. }
Arrays.FogCoord(ArrayIndexNum)^ := Max(Fog, 0);
end;
begin
inherited;
if FogCoord <> nil then
SetFog(GetFogCoord) else
if FogVolumetric then
SetFog(GetFogVolumetric);
end;
{ TAbstractShaderAttribGenerator ------------------------------ }
constructor TAbstractShaderAttribGenerator.Create(AShape: TShape; AOverTriangulate: boolean);
var
A: TMFNode;
I: Integer;
begin
inherited;
A := Geometry.Attrib;
if A <> nil then
for I := 0 to A.Count - 1 do
if A[I] is TAbstractVertexAttributeNode then
begin
{ To conserve time and memory, create Attrib instance only when needed }
if Attrib = nil then
Attrib := TX3DVertexAttributeNodes.Create(false);
Attrib.Add(TAbstractVertexAttributeNode(A[I]));
end;
end;
destructor TAbstractShaderAttribGenerator.Destroy;
begin
FreeAndNil(Attrib);
inherited;
end;
procedure TAbstractShaderAttribGenerator.PrepareAttributes(var AllowIndexed: boolean);
var
I: Integer;
begin
inherited;
if Attrib <> nil then
for I := 0 to Attrib.Count - 1 do
begin
{ call Arrays.AddGLSLAttribute* }
if Attrib[I] is TFloatVertexAttributeNode then
begin
case TFloatVertexAttributeNode(Attrib[I]).FdNumComponents.Value of
1: Arrays.AddGLSLAttributeFloat(Attrib[I].FdName.Value, false);
2: Arrays.AddGLSLAttributeVector2(Attrib[I].FdName.Value, false);
3: Arrays.AddGLSLAttributeVector3(Attrib[I].FdName.Value, false);
4: Arrays.AddGLSLAttributeVector4(Attrib[I].FdName.Value, false);
else OnWarning(wtMajor, 'VRML/X3D', Format('Invalid FloatVertexAttribute.numComponents: %d (should be between 1..4)',
[TFloatVertexAttributeNode(Attrib[I]).FdNumComponents.Value]));
end;
end else
if Attrib[I] is TMatrix3VertexAttributeNode then
Arrays.AddGLSLAttributeMatrix3(Attrib[I].FdName.Value, false) else
if Attrib[I] is TMatrix4VertexAttributeNode then
Arrays.AddGLSLAttributeMatrix4(Attrib[I].FdName.Value, false) else
OnWarning(wtMajor, 'VRML/X3D', Format('Not handled vertex attribute class %s',
[Attrib[I].NodeTypeName]));
end;
end;
procedure TAbstractShaderAttribGenerator.GenerateVertex(IndexNum: Integer);
var
I: Integer;
VertexIndex: Integer;
begin
inherited;
if Attrib <> nil then
begin
if CoordIndex <> nil then
VertexIndex := CoordIndex.Items.L[IndexNum] else
VertexIndex := IndexNum;
for I := 0 to Attrib.Count - 1 do
begin
{ set Arrays.GLSLAttribute*(ArrayIndexNum).
Note we don't do some warnings here, that were already done
in PrepareAttributes. }
if Attrib[I] is TFloatVertexAttributeNode then
begin
case TFloatVertexAttributeNode(Attrib[I]).FdNumComponents.Value of
1: Arrays.GLSLAttributeFloat(Attrib[I].FdName.Value, ArrayIndexNum)^ :=
TFloatVertexAttributeNode(Attrib[I]).FdValue.ItemsSafe[VertexIndex];
2: Arrays.GLSLAttributeVector2(Attrib[I].FdName.Value, ArrayIndexNum)^ := Vector2Single(
TFloatVertexAttributeNode(Attrib[I]).FdValue.ItemsSafe[VertexIndex * 2],
TFloatVertexAttributeNode(Attrib[I]).FdValue.ItemsSafe[VertexIndex * 2 + 1]);
3: Arrays.GLSLAttributeVector3(Attrib[I].FdName.Value, ArrayIndexNum)^ := Vector3Single(
TFloatVertexAttributeNode(Attrib[I]).FdValue.ItemsSafe[VertexIndex * 3],
TFloatVertexAttributeNode(Attrib[I]).FdValue.ItemsSafe[VertexIndex * 3 + 1],
TFloatVertexAttributeNode(Attrib[I]).FdValue.ItemsSafe[VertexIndex * 3 + 2]);
4: Arrays.GLSLAttributeVector4(Attrib[I].FdName.Value, ArrayIndexNum)^ := Vector4Single(
TFloatVertexAttributeNode(Attrib[I]).FdValue.ItemsSafe[VertexIndex * 4],
TFloatVertexAttributeNode(Attrib[I]).FdValue.ItemsSafe[VertexIndex * 4 + 1],
TFloatVertexAttributeNode(Attrib[I]).FdValue.ItemsSafe[VertexIndex * 4 + 2],
TFloatVertexAttributeNode(Attrib[I]).FdValue.ItemsSafe[VertexIndex * 4 + 3]);
end;
end else
if Attrib[I] is TMatrix3VertexAttributeNode then
Arrays.GLSLAttributeMatrix3(Attrib[I].FdName.Value, ArrayIndexNum)^ :=
TMatrix3VertexAttributeNode(Attrib[I]).FdValue.ItemsSafe[VertexIndex] else
if Attrib[I] is TMatrix4VertexAttributeNode then
Arrays.GLSLAttributeMatrix4(Attrib[I].FdName.Value, ArrayIndexNum)^ :=
TMatrix4VertexAttributeNode(Attrib[I]).FdValue.ItemsSafe[VertexIndex];
end;
end;
end;
{ TAbstractBumpMappingGenerator ----------------------------------------------- }
procedure TAbstractBumpMappingGenerator.PrepareAttributes(var AllowIndexed: boolean);
begin
inherited;
if ShapeBumpMappingUsed then
Arrays.AddGLSLAttributeMatrix3('castle_tangent_to_object_space', true);
end;
procedure TAbstractBumpMappingGenerator.GenerateVertex(IndexNum: Integer);
procedure DoBumpMapping;
function TangentToObjectSpace: TMatrix3Single;
procedure SetResult(const Normal, STangent, TTangent: TVector3Single);
begin
Result[0] := STangent;
Result[1] := TTangent;
Result[2] := Normal;
end;
var
LocalSTangent, LocalTTangent: TVector3Single;
Normal: TVector3Single;
begin
GetNormal(IndexNum, CurrentRangeNumber, Normal);
if HasTangentVectors and
(Abs(VectorDotProduct(STangent, Normal)) < 0.95) and
(Abs(VectorDotProduct(TTangent, Normal)) < 0.95) then
begin
if NormalsFlat then
begin
SetResult(Normal, STangent, TTangent);
end else
begin
{ If not NormalsFlat, you want to calculate local STangent and TTangent,
I mean STangent and TTangent adjusted to current vertex (since each
vertex may have different normal on the face when not NormalsFlat).
Without doing this, you would see strange artifacts, smoothed
faces would look somewhat like flat faces. Concenptually, for smoothed
faces, whole tangent space should vary for each vertex, so Normal,
and both tangents may be different on each vertex. }
LocalSTangent := STangent;
MakeVectorsOrthoOnTheirPlane(LocalSTangent, Normal);
LocalTTangent := TTangent;
MakeVectorsOrthoOnTheirPlane(LocalTTangent, Normal);
SetResult(Normal, LocalSTangent, LocalTTangent);
end;
end else
begin
SetResult(Normal,
{ would be more correct to set LocalSTangent as anything perpendicular
to Normal, and LocalTTangent as vector product (normal, LocalSTangent) }
Vector3Single(1, 0, 0), Vector3Single(0, 1, 0));
end;
end;
begin
Arrays.GLSLAttributeMatrix3('castle_tangent_to_object_space',
ArrayIndexNum)^ := TangentToObjectSpace;
end;
begin
inherited;
if ShapeBumpMappingUsed then
DoBumpMapping;
end;
procedure TAbstractBumpMappingGenerator.CalculateTangentVectors(
const TriangleIndex1, TriangleIndex2, TriangleIndex3: Integer);
{ This procedure can change Triangle*, but only by swapping some vertexes,
so we pass Triangle* by reference instead of by value, to avoid
needless mem copying.
Returns @false if cannot be calculated. }
function CalculateTangent(IsSTangent: boolean; var Tangent: TVector3Single;
var Triangle3D: TTriangle3Single;
var TriangleTexCoord: TTriangle2Single): boolean;
var
D: TVector2Single;
LineA, LineBC, DIn3D: TVector3Single;
MiddleIndex: Integer;
FarthestDistance, NewDistance, Alpha: Single;
SearchCoord, OtherCoord: Cardinal;
begin
if ISSTangent then
SearchCoord := 0 else
SearchCoord := 1;
OtherCoord := 1 - SearchCoord;
{ choose such that 1st and 2nd points have longest distance along
OtherCoord, so 0 point is in the middle. }
{ MiddleIndex means that
MiddleIndex, (MiddleIndex + 1) mod 3 are farthest. }
MiddleIndex := 2;
FarthestDistance := Abs(TriangleTexCoord[0][OtherCoord] - TriangleTexCoord[1][OtherCoord]);
NewDistance := Abs(TriangleTexCoord[1][OtherCoord] - TriangleTexCoord[2][OtherCoord]);
if NewDistance > FarthestDistance then
begin
MiddleIndex := 0;
FarthestDistance := NewDistance;
end;
NewDistance := Abs(TriangleTexCoord[2][OtherCoord] - TriangleTexCoord[0][OtherCoord]);
if NewDistance > FarthestDistance then
begin
MiddleIndex := 1;
FarthestDistance := NewDistance;
end;
if Zero(FarthestDistance) then
Exit(false);
if MiddleIndex <> 0 then
begin
SwapValues(TriangleTexCoord[0], TriangleTexCoord[MiddleIndex]);
SwapValues(Triangle3D [0], Triangle3D [MiddleIndex]);
end;
if IsSTangent then
begin
{ we want line Y = TriangleTexCoord[0][1]. }
LineA[0] := 0;
LineA[1] := 1;
LineA[2] := -TriangleTexCoord[0][1];
end else
begin
{ we want line X = TriangleTexCoord[0][0]. }
LineA[0] := 1;
LineA[1] := 0;
LineA[2] := -TriangleTexCoord[0][0];
end;
LineBC := LineOfTwoDifferentPoints2d(
TriangleTexCoord[1], TriangleTexCoord[2]);
try
D := Lines2DIntersection(LineA, LineBC);
except
on ELinesParallel do begin Result := false; Exit; end;
end;
{ LineBC[0, 1] is vector 2D orthogonal to LineBC.
If Abs(LineBC[0]) is *smaller* then it means that B and C points
are most different on 0 coord. }
if Abs(LineBC[0]) < Abs(LineBC[1]) then
Alpha := ( D[0] - TriangleTexCoord[1][0]) /
(TriangleTexCoord[2][0] - TriangleTexCoord[1][0]) else
Alpha := ( D[1] - TriangleTexCoord[1][1]) /
(TriangleTexCoord[2][1] - TriangleTexCoord[1][1]);
DIn3D := VectorAdd(
VectorScale(Triangle3D[1], 1 - Alpha),
VectorScale(Triangle3D[2], Alpha));
if D[SearchCoord] > TriangleTexCoord[0][SearchCoord] then
Tangent := VectorSubtract(DIn3D, Triangle3D[0]) else
Tangent := VectorSubtract(Triangle3D[0], DIn3D);
NormalizeTo1st(Tangent);
Result := true;
end;
var
Triangle3D: TTriangle3Single;
TriangleTexCoord: TTriangle2Single;
begin
HasTangentVectors := false;
if ShapeBumpMappingUsed then
begin
{ calculate Triangle3D }
Triangle3D[0] := GetVertex(TriangleIndex1);
Triangle3D[1] := GetVertex(TriangleIndex2);
Triangle3D[2] := GetVertex(TriangleIndex3);
{ This is just to shut up FPC 2.2.0 warnings about
TriangleTexCoord not initialized. }
TriangleTexCoord[0][0] := 0.0;
HasTangentVectors :=
{ calculate TriangleTexCoord }
GetTextureCoord(TriangleIndex1, 0, TriangleTexCoord[0]) and
GetTextureCoord(TriangleIndex2, 0, TriangleTexCoord[1]) and
GetTextureCoord(TriangleIndex3, 0, TriangleTexCoord[2]) and
{ calculate STangent, TTangent }
CalculateTangent(true , STangent, Triangle3D, TriangleTexCoord) and
CalculateTangent(false, TTangent, Triangle3D, TriangleTexCoord) and
(Abs(VectorDotProduct(STangent, TTangent)) < 0.95);
end;
end;
{ non-abstract generators ---------------------------------------------------- }
{$I arraysgenerator_x3d_rendering.inc}
{$I arraysgenerator_x3d_geometry3d.inc}
{ global routines ------------------------------------------------------------ }
function GetArraysGenerator(AGeometry: TAbstractGeometryNode): TArraysGeneratorClass;
begin
if AGeometry is TIndexedTriangleMeshNode_1 then
Result := TTriangleStripSetGenerator else
if AGeometry is TIndexedFaceSetNode_1 then
Result := TIndexedFaceSet_1Generator else
if AGeometry is TIndexedFaceSetNode then
Result := TIndexedFaceSetGenerator else
if AGeometry is TIndexedLineSetNode_1 then
Result := TIndexedLineSet_1Generator else
if (AGeometry is TIndexedLineSetNode) or
(AGeometry is TLineSetNode) then
Result := TLineSetGenerator else
if AGeometry is TPointSetNode_1 then
Result := TPointSet_1Generator else
if AGeometry is TPointSetNode then
Result := TPointSetGenerator else
if (AGeometry is TTriangleSetNode) or
(AGeometry is TIndexedTriangleSetNode) then
Result := TTriangleSetGenerator else
if (AGeometry is TTriangleFanSetNode) or
(AGeometry is TIndexedTriangleFanSetNode) then
Result := TTriangleFanSetGenerator else
if (AGeometry is TTriangleStripSetNode) or
(AGeometry is TIndexedTriangleStripSetNode) then
Result := TTriangleStripSetGenerator else
if (AGeometry is TQuadSetNode) or
(AGeometry is TIndexedQuadSetNode) then
Result := TQuadSetGenerator else
Result := nil;
end;
end.
|