Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 2148 lines (1671 sloc) 60.553 kb
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */


#include "UnitDrawer.h"
#include "UnitDrawerState.hpp"

#include "Game/Camera.h"
#include "Game/CameraHandler.h"
#include "Game/GameHelper.h"
#include "Game/GameSetup.h"
#include "Game/GlobalUnsynced.h"
#include "Game/Players/Player.h"
#include "Game/UI/MiniMap.h"
#include "Lua/LuaMaterial.h"
#include "Lua/LuaUnitMaterial.h"
#include "Map/BaseGroundDrawer.h"
#include "Map/Ground.h"
#include "Map/MapInfo.h"
#include "Map/ReadMap.h"

#include "Rendering/Env/ISky.h"
#include "Rendering/Env/IWater.h"
#include "Rendering/Env/CubeMapHandler.h"
#include "Rendering/FarTextureHandler.h"
#include "Rendering/Fonts/glFont.h"
#include "Rendering/GL/glExtra.h"
#include "Rendering/GL/VertexArray.h"
#include "Rendering/Env/IGroundDecalDrawer.h"
#include "Rendering/IconHandler.h"
#include "Rendering/ShadowHandler.h"
#include "Rendering/Shaders/Shader.h"
#include "Rendering/Textures/Bitmap.h"
#include "Rendering/Textures/3DOTextureHandler.h"
#include "Rendering/Textures/S3OTextureHandler.h"
#include "Rendering/Models/WorldObjectModelRenderer.h"

#include "Sim/Features/Feature.h"
#include "Sim/Misc/LosHandler.h"
#include "Sim/Misc/RadarHandler.h"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/Projectiles/ExplosionGenerator.h"
#include "Sim/Units/CommandAI/BuilderCAI.h"
#include "Sim/Units/Groups/Group.h"
#include "Sim/Units/UnitDef.h"
#include "Sim/Units/UnitDefHandler.h"
#include "Sim/Units/Unit.h"
#include "Sim/Units/UnitHandler.h"
#include "Sim/Units/UnitTypes/Building.h"
#include "Sim/Units/UnitTypes/TransportUnit.h"
#include "Sim/Weapons/Weapon.h"

#include "System/Config/ConfigHandler.h"
#include "System/EventHandler.h"
#include "System/Log/ILog.h"
#include "System/myMath.h"
#include "System/Platform/Watchdog.h"
#include "System/TimeProfiler.h"
#include "System/Util.h"

#define UNIT_SHADOW_ALPHA_MASKING

CUnitDrawer* unitDrawer;

CONFIG(int, UnitLodDist).defaultValue(1000);
CONFIG(int, UnitIconDist).defaultValue(200);
CONFIG(float, UnitTransparency).defaultValue(0.7f);

CONFIG(int, MaxDynamicModelLights)
.defaultValue(1)
.minimumValue(0);

CONFIG(bool, AdvUnitShading).defaultValue(true).safemodeValue(false).description("Determines whether specular highlights and other lighting effects are rendered for units.");
CONFIG(bool, AllowDeferredModelRendering).defaultValue(false).safemodeValue(false);

CONFIG(float, LODScale).defaultValue(1.0f);
CONFIG(float, LODScaleShadow).defaultValue(1.0f);
CONFIG(float, LODScaleReflection).defaultValue(1.0f);
CONFIG(float, LODScaleRefraction).defaultValue(1.0f);

static bool LUA_DRAWING = false; // FIXME
static float UNIT_GLOBAL_LOD_FACTOR = 1.0f;

inline static void SetUnitGlobalLODFactor(float value)
{
UNIT_GLOBAL_LOD_FACTOR = (value * camera->GetLPPScale());
}

static float GetLODFloat(const string& name)
{
// NOTE: the inverse of the value is used
const float value = configHandler->GetFloat(name);
if (value <= 0.0f) {
return 1.0f;
}
return (1.0f / value);
}


CUnitDrawer::CUnitDrawer(): CEventClient("[CUnitDrawer]", 271828, false)
{
eventHandler.AddClient(this);

SetUnitDrawDist((float)configHandler->GetInt("UnitLodDist"));
SetUnitIconDist((float)configHandler->GetInt("UnitIconDist"));

LODScale = GetLODFloat("LODScale");
LODScaleShadow = GetLODFloat("LODScaleShadow");
LODScaleReflection = GetLODFloat("LODScaleReflection");
LODScaleRefraction = GetLODFloat("LODScaleRefraction");

unitAmbientColor = mapInfo->light.unitAmbientColor;
unitSunColor = mapInfo->light.unitSunColor;

cloakAlpha = std::max(0.11f, std::min(1.0f, 1.0f - configHandler->GetFloat("UnitTransparency")));
cloakAlpha1 = std::min(1.0f, cloakAlpha + 0.1f);
cloakAlpha2 = std::min(1.0f, cloakAlpha + 0.2f);
cloakAlpha3 = std::min(1.0f, cloakAlpha + 0.4f);

// load unit explosion generators and decals
for (size_t unitDefID = 1; unitDefID < unitDefHandler->unitDefs.size(); unitDefID++) {
UnitDef* ud = unitDefHandler->unitDefs[unitDefID];

for (unsigned int n = 0; n < ud->modelCEGTags.size(); n++) {
ud->SetModelExplosionGeneratorID(n, explGenHandler->LoadGeneratorID(ud->modelCEGTags[n]));
}
for (unsigned int n = 0; n < ud->pieceCEGTags.size(); n++) {
// these can only be custom EG's so prefix is not required game-side
ud->SetPieceExplosionGeneratorID(n, explGenHandler->LoadGeneratorID(CEG_PREFIX_STRING + ud->pieceCEGTags[n]));
}
}


deadGhostBuildings.resize(MODELTYPE_OTHER);
liveGhostBuildings.resize(MODELTYPE_OTHER);

opaqueModelRenderers.resize(MODELTYPE_OTHER, NULL);
cloakedModelRenderers.resize(MODELTYPE_OTHER, NULL);

for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
opaqueModelRenderers[modelType] = IWorldObjectModelRenderer::GetInstance(modelType);
cloakedModelRenderers[modelType] = IWorldObjectModelRenderer::GetInstance(modelType);
}

unitRadarIcons.resize(teamHandler->ActiveAllyTeams());

// LH must be initialized before drawer-state is initialized
lightHandler.Init(2U, configHandler->GetInt("MaxDynamicModelLights"));
geomBuffer.SetName("UNITDRAWER-GBUFFER");

unitDrawerStateSSP = IUnitDrawerState::GetInstance(globalRendering->haveARB, globalRendering->haveGLSL);
unitDrawerStateFFP = IUnitDrawerState::GetInstance( false, false);

// also set in ::SetupForUnitDrawing, but SunChanged can be
// called first if DynamicSun is enabled --> must be non-NULL
SelectRenderState(false);

// NOTE:
// advShading can NOT change at runtime if initially false***
// (see AdvModelShadingActionExecutor), so we will always use
// unitDrawerStateFFP (in ::Draw) in that special case and it
// does not matter if unitDrawerStateSSP is initialized
// *** except for DrawCloakedUnits
advFading = GLEW_NV_vertex_program2;
advShading = (unitDrawerStateSSP->Init(this) && cubeMapHandler->Init());
drawDeferred = geomBuffer.Valid();

if (drawDeferred) {
drawDeferred &= UpdateGeometryBuffer(true);
}
}

CUnitDrawer::~CUnitDrawer()
{
eventHandler.RemoveClient(this);

unitDrawerStateSSP->Kill(); IUnitDrawerState::FreeInstance(unitDrawerStateSSP);
unitDrawerStateFFP->Kill(); IUnitDrawerState::FreeInstance(unitDrawerStateFFP);

cubeMapHandler->Free();

for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
std::set<GhostSolidObject*>& ghostSet = deadGhostBuildings[modelType];
std::set<GhostSolidObject*>::iterator ghostSetIt;

for (ghostSetIt = ghostSet.begin(); ghostSetIt != ghostSet.end(); ++ghostSetIt) {
delete *ghostSetIt;
}

deadGhostBuildings[modelType].clear();
liveGhostBuildings[modelType].clear();
}

deadGhostBuildings.clear();
liveGhostBuildings.clear();

for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
delete opaqueModelRenderers[modelType];
delete cloakedModelRenderers[modelType];
}

opaqueModelRenderers.clear();
cloakedModelRenderers.clear();

unitRadarIcons.clear();
unsortedUnits.clear();
}



void CUnitDrawer::SetUnitDrawDist(float dist)
{
unitDrawDist = dist;
unitDrawDistSqr = dist * dist;
}

void CUnitDrawer::SetUnitIconDist(float dist)
{
unitIconDist = dist;
iconLength = 750 * unitIconDist * unitIconDist;
}



void CUnitDrawer::Update()
{
{

while (!tempDrawUnits.empty() && tempDrawUnits.begin()->first < gs->frameNum - 1) {
tempDrawUnits.erase(tempDrawUnits.begin());
}
while (!tempTransparentDrawUnits.empty() && tempTransparentDrawUnits.begin()->first <= gs->frameNum) {
tempTransparentDrawUnits.erase(tempTransparentDrawUnits.begin());
}
}

eventHandler.UpdateDrawUnits();

{

drawIcon.clear();
for (std::set<CUnit*>::iterator usi = unsortedUnits.begin(); usi != unsortedUnits.end(); ++usi) {
CUnit* unit = *usi;

UpdateUnitIconState(unit);
UpdateUnitDrawPos(unit);
}
}

useDistToGroundForIcons = (camHandler->GetCurrentController()).GetUseDistToGroundForIcons();

if (useDistToGroundForIcons) {
const float3& camPos = camera->GetPos();
// use the height at the current camera position
//const float groundHeight = CGround::GetHeightAboveWater(camPos.x, camPos.z, false);
// use the middle between the highest and lowest position on the map as average
const float groundHeight = (readMap->GetCurrMinHeight() + readMap->GetCurrMaxHeight()) * 0.5f;
const float overGround = camPos.y - groundHeight;

sqCamDistToGroundForIcons = overGround * overGround;
}

if (drawDeferred) {
drawDeferred &= UpdateGeometryBuffer(false);
}
}

bool CUnitDrawer::UpdateGeometryBuffer(bool init)
{
static const bool drawDeferredAllowed = configHandler->GetBool("AllowDeferredModelRendering");

if (!drawDeferredAllowed)
return false;

return (geomBuffer.Update(init));
}






// only called by DrawOpaqueUnit
inline bool CUnitDrawer::DrawUnitLOD(CUnit* unit)
{
if (unit->lodCount > 0) {
if (unit->isCloaked) {
const LuaMatType matType = (water->DrawReflectionPass())?
LUAMAT_ALPHA_REFLECT: LUAMAT_ALPHA;
LuaUnitMaterial& unitMat = unit->luaMats[matType];
const unsigned lod = CalcUnitLOD(unit, unitMat.GetLastLOD());
unit->currentLOD = lod;
LuaUnitLODMaterial* lodMat = unitMat.GetMaterial(lod);

if ((lodMat != NULL) && lodMat->IsActive()) {
lodMat->AddUnit(unit); return true;
}
} else {
const LuaMatType matType =
(water->DrawReflectionPass()) ? LUAMAT_OPAQUE_REFLECT : LUAMAT_OPAQUE;
LuaUnitMaterial& unitMat = unit->luaMats[matType];
const unsigned lod = CalcUnitLOD(unit, unitMat.GetLastLOD());
unit->currentLOD = lod;
LuaUnitLODMaterial* lodMat = unitMat.GetMaterial(lod);

if ((lodMat != NULL) && lodMat->IsActive()) {
lodMat->AddUnit(unit); return true;
}
}
}

return false;
}

inline void CUnitDrawer::DrawOpaqueUnit(CUnit* unit, const CUnit* excludeUnit, bool drawReflection, bool drawRefraction)
{
if (unit == excludeUnit)
return;
if (unit->noDraw)
return;
if (unit->IsInVoid())
return;
if (!camera->InView(unit->drawMidPos, unit->drawRadius))
return;

if ((unit->losStatus[gu->myAllyTeam] & LOS_INLOS) || gu->spectatingFullView) {
if (drawReflection) {
float3 zeroPos;

if (unit->drawMidPos.y < 0.0f) {
zeroPos = unit->drawMidPos;
} else {
const float dif = unit->drawMidPos.y - camera->GetPos().y;
zeroPos =
camera->GetPos() * (unit->drawMidPos.y / dif) +
unit->drawMidPos * (-camera->GetPos().y / dif);
}
if (CGround::GetApproximateHeight(zeroPos.x, zeroPos.z, false) > unit->drawRadius) {
return;
}
}
else if (drawRefraction) {
if (unit->pos.y > 0.0f) {
return;
}
}

if (!unit->isIcon) {
if ((unit->pos).SqDistance(camera->GetPos()) > (unit->sqRadius * unitDrawDistSqr)) {
farTextureHandler->Queue(unit);
} else {
if (!DrawUnitLOD(unit)) {
SetTeamColour(unit->team);
DrawUnitNow(unit);
}
}
}
}
}



void CUnitDrawer::Draw(bool drawReflection, bool drawRefraction)
{
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
ISky::SetupFog();

if (drawReflection) {
SetUnitGlobalLODFactor(LODScale * LODScaleReflection);
} else if (drawRefraction) {
SetUnitGlobalLODFactor(LODScale * LODScaleRefraction);
} else {
SetUnitGlobalLODFactor(LODScale);
}

camNorm = camera->forward;
camNorm.y = -0.1f;
camNorm.ANormalize();

const CPlayer* myPlayer = gu->GetMyPlayer();
const CUnit* excludeUnit = drawReflection? NULL: myPlayer->fpsController.GetControllee();

if (drawDeferred) {
DrawDeferredPass(excludeUnit, drawReflection, drawRefraction);
}

SetupForUnitDrawing(false);

{
for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
opaqueModelRenderers[modelType]->PushRenderState();
DrawOpaqueUnits(modelType, excludeUnit, drawReflection, drawRefraction);
opaqueModelRenderers[modelType]->PopRenderState();
}
}

CleanUpUnitDrawing(false);
DrawOpaqueShaderUnits((water->DrawReflectionPass())? LUAMAT_OPAQUE_REFLECT: LUAMAT_OPAQUE, false);

farTextureHandler->Draw();
DrawUnitIcons(drawReflection);

glDisable(GL_FOG);
glDisable(GL_ALPHA_TEST);
glDisable(GL_TEXTURE_2D);
}

void CUnitDrawer::DrawDeferredPass(const CUnit* excludeUnit, bool drawReflection, bool drawRefraction)
{
if (!geomBuffer.Valid())
return;

// some water renderers use FBO's for the reflection pass
if (drawReflection)
return;
// some water renderers use FBO's for the refraction pass
if (drawRefraction)
return;

// deferred pass must be executed with GLSL shaders
// bail early if the FFP state *is going to be* selected by
// SetupForUnitDrawing and if our shader-path happens to be
// ARB instead
if (!unitDrawerStateSSP->CanEnable(this))
return;
if (!unitDrawerStateSSP->CanDrawDeferred())
return;

geomBuffer.Bind();

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// this selects the render-state (FFP, SSP); needs to be
// enabled and disabled by us (as well as ::Draw) because
// custom-shader models do not use it and we want to draw
// those deferred as well
SetupForUnitDrawing(true);

{
for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
opaqueModelRenderers[modelType]->PushRenderState();
DrawOpaqueUnits(modelType, excludeUnit, drawReflection, drawRefraction);
opaqueModelRenderers[modelType]->PopRenderState();
}
}

CleanUpUnitDrawing(true);
DrawOpaqueShaderUnits((water->DrawReflectionPass())? LUAMAT_OPAQUE_REFLECT: LUAMAT_OPAQUE, true);

geomBuffer.UnBind();

#if 0
geomBuffer.DrawDebug(geomBuffer.GetBufferTexture(GL::GeometryBuffer::ATTACHMENT_NORMTEX));
#endif
}



void CUnitDrawer::DrawOpaqueUnits(int modelType, const CUnit* excludeUnit, bool drawReflection, bool drawRefraction)
{
typedef std::set<CUnit*> UnitSet;
typedef std::map<int, UnitSet> UnitBin;

const UnitBin& unitBin = opaqueModelRenderers[modelType]->GetUnitBin();

UnitBin::const_iterator unitBinIt;
UnitSet::const_iterator unitSetIt;

for (unitBinIt = unitBin.begin(); unitBinIt != unitBin.end(); ++unitBinIt) {
if (modelType != MODELTYPE_3DO) {
texturehandlerS3O->SetS3oTexture(unitBinIt->first);
}

const UnitSet& unitSet = unitBinIt->second;

{
for (unitSetIt = unitSet.begin(); unitSetIt != unitSet.end(); ++unitSetIt) {
DrawOpaqueUnit(*unitSetIt, excludeUnit, drawReflection, drawRefraction);
}
}
}

if (modelType == MODELTYPE_3DO) {
DrawOpaqueAIUnits();
}
}

void CUnitDrawer::DrawOpaqueAIUnits()
{
// non-cloaked AI unit ghosts (FIXME: s3o's + teamcolor)
for (std::multimap<int, TempDrawUnit>::iterator ti = tempDrawUnits.begin(); ti != tempDrawUnits.end(); ++ti) {
if (camera->InView(ti->second.pos, 100)) {
glPushMatrix();
glTranslatef3(ti->second.pos);
glRotatef(ti->second.rotation * 180 / PI, 0, 1, 0);

const UnitDef* udef = ti->second.unitdef;
const S3DModel* model = udef->LoadModel();

model->DrawStatic();
glPopMatrix();
}
}
}

void CUnitDrawer::DrawUnitIcons(bool drawReflection)
{
if (!drawReflection) {
// Draw unit icons and radar blips.
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.5f);

for (std::set<CUnit*>::iterator ui = drawIcon.begin(); ui != drawIcon.end(); ++ui) {
DrawIcon(*ui, false);
}
if (!gu->spectatingFullView) {
for (std::set<CUnit*>::const_iterator ui = unitRadarIcons[gu->myAllyTeam].begin(); ui != unitRadarIcons[gu->myAllyTeam].end(); ++ui) {
DrawIcon(*ui, ((*ui)->losStatus[gu->myAllyTeam] & (LOS_PREVLOS | LOS_CONTRADAR)) != (LOS_PREVLOS | LOS_CONTRADAR));
}
}

glDisable(GL_TEXTURE_2D);
glDisable(GL_ALPHA_TEST);

glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
}





/******************************************************************************/
/******************************************************************************/

static void DrawLuaMatBins(LuaMatType type, bool deferredPass)
{
const LuaMatBinSet& bins = luaMatHandler.GetBins(type);
if (bins.empty()) {
return;
}

LUA_DRAWING = true;

glPushAttrib(GL_TEXTURE_BIT | GL_ENABLE_BIT | GL_TRANSFORM_BIT);
if (type == LUAMAT_ALPHA || type == LUAMAT_ALPHA_REFLECT) {
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.1f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
} else {
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.5f);
}

const LuaMaterial* currMat = &LuaMaterial::defMat;

LuaMatBinSet::const_iterator it;
for (it = bins.begin(); it != bins.end(); ++it) {
LuaMatBin* bin = *it;
bin->Execute(*currMat, deferredPass);
currMat = bin;

const std::vector<CUnit*>& units = bin->GetUnits();
const int count = (int)units.size();
for (int i = 0; i < count; i++) {
CUnit* unit = units[i];

const LuaUnitMaterial& unitMat = unit->luaMats[type];
const LuaUnitLODMaterial* lodMat = unitMat.GetMaterial(unit->currentLOD);
lodMat->uniforms.Execute(unit);

unitDrawer->SetTeamColour(unit->team);
unitDrawer->DrawUnitWithLists(unit, lodMat->preDisplayList, lodMat->postDisplayList);
}
}

LuaMaterial::defMat.Execute(*currMat, deferredPass);
luaMatHandler.ClearBins(type);

glPopAttrib();

LUA_DRAWING = false;
}


/******************************************************************************/

static void SetupShadowDrawing()
{
//FIXME setup face culling for s3o?

glColor3f(1.0f, 1.0f, 1.0f);
glDisable(GL_TEXTURE_2D);

glPolygonOffset(1.0f, 1.0f);
glEnable(GL_POLYGON_OFFSET_FILL);

Shader::IProgramObject* po =
shadowHandler->GetShadowGenProg(CShadowHandler::SHADOWGEN_PROGRAM_MODEL);
po->Enable();
}


static void CleanUpShadowDrawing()
{
Shader::IProgramObject* po =
shadowHandler->GetShadowGenProg(CShadowHandler::SHADOWGEN_PROGRAM_MODEL);

po->Disable();
glDisable(GL_POLYGON_OFFSET_FILL);
}


/******************************************************************************/

#define ud unitDrawer
static void SetupOpaque3DO(bool deferredPass) {
ud->SetupForUnitDrawing(deferredPass);
ud->GetOpaqueModelRenderer(MODELTYPE_3DO)->PushRenderState();
}
static void ResetOpaque3DO(bool deferredPass) {
ud->GetOpaqueModelRenderer(MODELTYPE_3DO)->PopRenderState();
ud->CleanUpUnitDrawing(deferredPass);
}
static void SetupOpaqueS3O(bool deferredPass) {
ud->SetupForUnitDrawing(deferredPass);
ud->GetOpaqueModelRenderer(MODELTYPE_S3O)->PushRenderState();
}
static void ResetOpaqueS3O(bool deferredPass) {
ud->GetOpaqueModelRenderer(MODELTYPE_S3O)->PopRenderState();
ud->CleanUpUnitDrawing(deferredPass);
}

static void SetupAlpha3DO(bool) {
ud->SetupForGhostDrawing();
ud->GetCloakedModelRenderer(MODELTYPE_3DO)->PushRenderState();
}
static void ResetAlpha3DO(bool) {
ud->GetCloakedModelRenderer(MODELTYPE_3DO)->PopRenderState();
ud->CleanUpGhostDrawing();
}
static void SetupAlphaS3O(bool) {
ud->SetupForGhostDrawing();
ud->GetCloakedModelRenderer(MODELTYPE_S3O)->PushRenderState();
}
static void ResetAlphaS3O(bool) {
ud->GetCloakedModelRenderer(MODELTYPE_S3O)->PopRenderState();
ud->CleanUpGhostDrawing();
}
#undef ud

static void SetupShadow3DO(bool) { SetupShadowDrawing(); }
static void ResetShadow3DO(bool) { CleanUpShadowDrawing(); }
static void SetupShadowS3O(bool) { SetupShadowDrawing(); }
static void ResetShadowS3O(bool) { CleanUpShadowDrawing(); }



/******************************************************************************/

void CUnitDrawer::DrawOpaqueShaderUnits(unsigned int matType, bool deferredPass)
{
luaMatHandler.setup3doShader = SetupOpaque3DO;
luaMatHandler.reset3doShader = ResetOpaque3DO;
luaMatHandler.setupS3oShader = SetupOpaqueS3O;
luaMatHandler.resetS3oShader = ResetOpaqueS3O;

DrawLuaMatBins(LuaMatType(matType), deferredPass);
}


void CUnitDrawer::DrawCloakedShaderUnits(unsigned int matType)
{
luaMatHandler.setup3doShader = SetupAlpha3DO;
luaMatHandler.reset3doShader = ResetAlpha3DO;
luaMatHandler.setupS3oShader = SetupAlphaS3O;
luaMatHandler.resetS3oShader = ResetAlphaS3O;

// FIXME: deferred shading and transparency is a PITA
DrawLuaMatBins(LuaMatType(matType), false);
}


void CUnitDrawer::DrawShadowShaderUnits(unsigned int matType)
{
luaMatHandler.setup3doShader = SetupShadow3DO;
luaMatHandler.reset3doShader = ResetShadow3DO;
luaMatHandler.setupS3oShader = SetupShadowS3O;
luaMatHandler.resetS3oShader = ResetShadowS3O;

// we neither want nor need a deferred shadow
// pass for custom- or default-shader models!
DrawLuaMatBins(LuaMatType(matType), false);
}



/******************************************************************************/
/******************************************************************************/


inline void CUnitDrawer::DrawOpaqueUnitShadow(CUnit* unit) {
// do shadow alpha-masking for S3O units only
// (3DO's need more setup than it is worth)
#ifdef UNIT_SHADOW_ALPHA_MASKING
#define S3O_TEX(model) \
texturehandlerS3O->GetS3oTex(model->textureType)
#define PUSH_SHADOW_TEXTURE_STATE(model) \
if (model->type != MODELTYPE_3DO) { \
glActiveTexture(GL_TEXTURE0); \
glEnable(GL_TEXTURE_2D); \
glBindTexture(GL_TEXTURE_2D, S3O_TEX(model)->tex2); \
}
#define POP_SHADOW_TEXTURE_STATE(model) \
if (model->type != MODELTYPE_3DO) { \
glBindTexture(GL_TEXTURE_2D, 0); \
glDisable(GL_TEXTURE_2D); \
glActiveTexture(GL_TEXTURE0); \
}
#else
#define PUSH_SHADOW_TEXTURE_STATE(model)
#define POP_SHADOW_TEXTURE_STATE(model)
#endif

const bool unitInLOS = ((unit->losStatus[gu->myAllyTeam] & LOS_INLOS) || gu->spectatingFullView);

if (unit->noDraw)
return;
if (unit->IsInVoid())
return;
// FIXME: test against the shadow projection intersection
if (!(unitInLOS && camera->InView(unit->drawMidPos, unit->drawRadius + 700.0f))) {
return;
}

const float sqDist = (unit->pos - camera->GetPos()).SqLength();
const float farLength = unit->sqRadius * unitDrawDistSqr;

if (sqDist >= farLength) { return; }
if (unit->isCloaked) { return; }
if (DrawAsIcon(unit, sqDist)) { return; }

if (unit->lodCount <= 0) {
PUSH_SHADOW_TEXTURE_STATE(unit->model);
DrawUnitNow(unit);
POP_SHADOW_TEXTURE_STATE(unit->model);
} else {
LuaUnitMaterial& unitMat = unit->luaMats[LUAMAT_SHADOW];
const unsigned lod = CalcUnitLOD(unit, unitMat.GetLastLOD());
unit->currentLOD = lod;
LuaUnitLODMaterial* lodMat = unitMat.GetMaterial(lod);

if ((lodMat != NULL) && lodMat->IsActive()) {
lodMat->AddUnit(unit);
} else {
PUSH_SHADOW_TEXTURE_STATE(unit->model);
DrawUnitNow(unit);
POP_SHADOW_TEXTURE_STATE(unit->model);
}
}

#undef PUSH_SHADOW_TEXTURE_STATE
#undef POP_SHADOW_TEXTURE_STATE
}
void CUnitDrawer::DrawOpaqueUnitsShadow(int modelType) {
typedef std::set<CUnit*> UnitSet;
typedef std::map<int, UnitSet> UnitBin;

const UnitBin& unitBin = opaqueModelRenderers[modelType]->GetUnitBin();

UnitBin::const_iterator unitBinIt;
UnitSet::const_iterator unitSetIt;

for (unitBinIt = unitBin.begin(); unitBinIt != unitBin.end(); ++unitBinIt) {
const UnitSet& unitSet = unitBinIt->second;

{
for (unitSetIt = unitSet.begin(); unitSetIt != unitSet.end(); ++unitSetIt) {
DrawOpaqueUnitShadow(*unitSetIt);
}
}
}
}

void CUnitDrawer::DrawShadowPass()
{
glColor3f(1.0f, 1.0f, 1.0f);
glPolygonOffset(1.0f, 1.0f);
glEnable(GL_POLYGON_OFFSET_FILL);

#ifdef UNIT_SHADOW_ALPHA_MASKING
glAlphaFunc(GL_GREATER, 0.5f);
glEnable(GL_ALPHA_TEST);
#endif

Shader::IProgramObject* po =
shadowHandler->GetShadowGenProg(CShadowHandler::SHADOWGEN_PROGRAM_MODEL);
po->Enable();

SetUnitGlobalLODFactor(LODScale * LODScaleShadow);

{
// 3DO's have clockwise-wound faces and
// (usually) holes, so disable backface
// culling for them
glDisable(GL_CULL_FACE);
DrawOpaqueUnitsShadow(MODELTYPE_3DO);
glEnable(GL_CULL_FACE);

for (int modelType = MODELTYPE_S3O; modelType < MODELTYPE_OTHER; modelType++) {
// note: just use DrawOpaqueUnits()? would
// save texture switches needed anyway for
// UNIT_SHADOW_ALPHA_MASKING
DrawOpaqueUnitsShadow(modelType);
}
}

po->Disable();

#ifdef UNIT_SHADOW_ALPHA_MASKING
glDisable(GL_ALPHA_TEST);
#endif

glDisable(GL_POLYGON_OFFSET_FILL);

DrawShadowShaderUnits(LUAMAT_SHADOW);
}



void CUnitDrawer::DrawIcon(CUnit* unit, bool useDefaultIcon)
{
// If the icon is to be drawn as a radar blip, we want to get the default icon.
const icon::CIconData* iconData = NULL;

if (useDefaultIcon) {
iconData = icon::iconHandler->GetDefaultIconData();
} else {
iconData = unit->unitDef->iconType.GetIconData();
}

// Calculate the icon size. It scales with:
// * The square root of the camera distance.
// * The mod defined 'iconSize' (which acts a multiplier).
// * The unit radius, depending on whether the mod defined 'radiusadjust' is true or false.
float3 pos;
if (gu->spectatingFullView) {
pos = unit->drawMidPos;
} else {
pos = unit->GetDrawErrorPos(gu->myAllyTeam);
}

// make sure icon is above ground (needed before we calculate scale below)
const float h = CGround::GetHeightReal(pos.x, pos.z, false);

if (pos.y < h) {
pos.y = h;
}

const float dist = std::min(8000.0f, fastmath::sqrt2(camera->GetPos().SqDistance(pos)));
const float iconScale = 0.4f * fastmath::sqrt2(dist); // makes far icons bigger
float scale = iconData->GetSize() * iconScale;

if (iconData->GetRadiusAdjust() && !useDefaultIcon) {
scale *= (unit->radius / WORLDOBJECT_DEFAULT_DRAWRADIUS);
}

// make sure icon is not partly under ground
if (pos.y < (h + scale)) {
pos.y = (h + scale);
}

unit->iconRadius = scale; // store the icon size so that we don't have to calculate it again

// Is the unit selected? Then draw it white.
if (unit->isSelected) {
glColor3ub(255, 255, 255);
} else {
glColor3ubv(teamHandler->Team(unit->team)->color);
}

// calculate the vertices
const float3 dy = camera->up * scale;
const float3 dx = camera->right * scale;
const float3 vn = pos - dx;
const float3 vp = pos + dx;
const float3 vnn = vn - dy;
const float3 vpn = vp - dy;
const float3 vnp = vn + dy;
const float3 vpp = vp + dy;

// Draw the icon.
iconData->Draw(vnn, vpn, vnp, vpp);
}






void CUnitDrawer::SetupForGhostDrawing() const
{
glEnable(GL_LIGHTING); // Give faded objects same appearance as regular
glLightfv(GL_LIGHT1, GL_POSITION, sky->GetLight()->GetLightDir());
glEnable(GL_LIGHT1);

SetupBasicS3OTexture0();
SetupBasicS3OTexture1(); // This also sets up the transparency

static const float cols[] = {1.0f, 1.0f, 1.0f, 1.0f};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, cols);
glColor3f(1.0f, 1.0f, 1.0f);

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);

glPushAttrib(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.1f);
glDepthMask(GL_FALSE);
}

void CUnitDrawer::CleanUpGhostDrawing() const
{
glPopAttrib();
glDisable(GL_TEXTURE_2D);

// clean up s3o drawing stuff
// reset texture1 state
CleanupBasicS3OTexture1();

// reset texture0 state
CleanupBasicS3OTexture0();

glDisable(GL_LIGHTING);
glDisable(GL_LIGHT1);
}



void CUnitDrawer::DrawCloakedUnits(bool disableAdvShading)
{
const bool oldAdvShading = advShading;
// don't use shaders if shadows are enabled (WHY?)
// also not safe: should set state instance to FFP
advShading = advShading && !disableAdvShading;

if (advShading) {
SetupForUnitDrawing(false);
glDisable(GL_ALPHA_TEST);
} else {
SetupForGhostDrawing();
}

glColor4f(1.0f, 1.0f, 1.0f, cloakAlpha);

{
for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
cloakedModelRenderers[modelType]->PushRenderState();
DrawCloakedUnitsHelper(modelType);
cloakedModelRenderers[modelType]->PopRenderState();
}

if (advShading) {
CleanUpUnitDrawing(false);
glEnable(GL_ALPHA_TEST);
} else {
CleanUpGhostDrawing();
}

advShading = oldAdvShading;

// shader rendering
DrawCloakedShaderUnits((water->DrawReflectionPass())? LUAMAT_ALPHA_REFLECT: LUAMAT_ALPHA);
}

glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}

void CUnitDrawer::DrawCloakedUnitsHelper(int modelType)
{
if (modelType == MODELTYPE_3DO) {
DrawCloakedAIUnits();
}

{
typedef std::set<CUnit*> UnitSet;
typedef std::map<int, UnitSet> UnitRenderBin;
typedef std::map<int, UnitSet>::const_iterator UnitRenderBinIt;

const UnitRenderBin& unitBin = cloakedModelRenderers[modelType]->GetUnitBin();

// cloaked units
for (UnitRenderBinIt it = unitBin.begin(); it != unitBin.end(); ++it) {
if (modelType != MODELTYPE_3DO) {
texturehandlerS3O->SetS3oTexture(it->first);
}

const UnitSet& unitSet = it->second;

for (UnitSet::const_iterator ui = unitSet.begin(); ui != unitSet.end(); ++ui) {
DrawCloakedUnit(*ui, modelType, false);
}
}
}

// living and dead ghosted buildings
DrawGhostedBuildings(modelType);
}

inline void CUnitDrawer::DrawCloakedUnit(CUnit* unit, int modelType, bool drawGhostBuildingsPass) {
if (!camera->InView(unit->drawMidPos, unit->drawRadius)) {
return;
}

const unsigned short losStatus = unit->losStatus[gu->myAllyTeam];

if (!drawGhostBuildingsPass) {
if ((losStatus & LOS_INLOS) || gu->spectatingFullView) {
if (!unit->isIcon) {
SetTeamColour(unit->team, cloakAlpha);
DrawUnitNow(unit);
}
}
} else {
// check for decoy models
const UnitDef* decoyDef = unit->unitDef->decoyDef;
const S3DModel* model = NULL;

if (decoyDef == NULL) {
model = unit->model;
} else {
model = decoyDef->LoadModel();
}

// FIXME: needs a second pass
if (model->type != modelType) {
return;
}

// ghosted enemy units
if (losStatus & LOS_CONTRADAR) {
glColor4f(0.9f, 0.9f, 0.9f, cloakAlpha2);
} else {
glColor4f(0.6f, 0.6f, 0.6f, cloakAlpha1);
}

glPushMatrix();
glTranslatef3(unit->pos);
glRotatef(unit->buildFacing * 90.0f, 0, 1, 0);

if (modelType != MODELTYPE_3DO) {
// the units in liveGhostedBuildings[modelType] are not
// sorted by textureType, but we cannot merge them with
// cloakedModelRenderers[modelType] since they are not
// actually cloaked
texturehandlerS3O->SetS3oTexture(model->textureType);
}

SetTeamColour(unit->team, (losStatus & LOS_CONTRADAR) ? cloakAlpha2 : cloakAlpha1);
model->DrawStatic();
glPopMatrix();

glColor4f(1.0f, 1.0f, 1.0f, cloakAlpha);
}
}

void CUnitDrawer::DrawCloakedAIUnits()
{
// cloaked AI unit ghosts (FIXME: S3O's need different state)
for (std::multimap<int, TempDrawUnit>::iterator ti = tempTransparentDrawUnits.begin(); ti != tempTransparentDrawUnits.end(); ++ti) {
if (camera->InView(ti->second.pos, 100)) {
glPushMatrix();
glTranslatef3(ti->second.pos);
glRotatef(ti->second.rotation * 180 / PI, 0, 1, 0);

const UnitDef* udef = ti->second.unitdef;
const S3DModel* model = udef->LoadModel();

SetTeamColour(ti->second.team, cloakAlpha);

model->DrawStatic();
glPopMatrix();
}
if (ti->second.drawBorder) {
float3 pos = ti->second.pos;
const UnitDef* unitdef = ti->second.unitdef;

SetTeamColour(ti->second.team, cloakAlpha3);

BuildInfo bi(unitdef, pos, ti->second.facing);
pos = CGameHelper::Pos2BuildPos(bi, false);

const float xsize = bi.GetXSize() * 4;
const float zsize = bi.GetZSize() * 4;

glColor4f(0.2f, 1, 0.2f, cloakAlpha3);
glDisable(GL_TEXTURE_2D);
glBegin(GL_LINE_STRIP);
glVertexf3(pos + float3( xsize, 1.0f, zsize));
glVertexf3(pos + float3(-xsize, 1.0f, zsize));
glVertexf3(pos + float3(-xsize, 1.0f, -zsize));
glVertexf3(pos + float3( xsize, 1.0f, -zsize));
glVertexf3(pos + float3( xsize, 1.0f, zsize));
glEnd();
glColor4f(1.0f, 1.0f, 1.0f, cloakAlpha);
glEnable(GL_TEXTURE_2D);
}
}
}

void CUnitDrawer::DrawGhostedBuildings(int modelType)
{
std::set<GhostSolidObject*>& deadGhostedBuildings = deadGhostBuildings[modelType];
std::set<CUnit*>& liveGhostedBuildings = liveGhostBuildings[modelType];

glColor4f(0.6f, 0.6f, 0.6f, cloakAlpha1);

// buildings that died while ghosted
for (std::set<GhostSolidObject*>::iterator it = deadGhostedBuildings.begin(); it != deadGhostedBuildings.end(); ) {
if (losHandler->InLos((*it)->pos, gu->myAllyTeam) || gu->spectatingFullView) {
// obtained LOS on the ghost of a dead building
groundDecals->GhostDestroyed(*it);

delete *it;
it = set_erase(deadGhostedBuildings, it);
} else {
if (camera->InView((*it)->pos, (*it)->model->drawRadius)) {
glPushMatrix();
glTranslatef3((*it)->pos);
glRotatef((*it)->facing * 90.0f, 0, 1, 0);

if (modelType != MODELTYPE_3DO)
texturehandlerS3O->SetS3oTexture((*it)->model->textureType);

SetTeamColour((*it)->team, cloakAlpha1);
(*it)->model->DrawStatic();
glPopMatrix();
}

++it;
}
}

if (!gu->spectatingFullView) {
for (std::set<CUnit*>::const_iterator ui = liveGhostedBuildings.begin(); ui != liveGhostedBuildings.end(); ++ui) {
if (!((*ui)->losStatus[gu->myAllyTeam] & LOS_INLOS)) // because of team switching via cheat, ghost buildings can exist for units in LOS
DrawCloakedUnit(*ui, modelType, true);
}
}
}





void CUnitDrawer::SetupForUnitDrawing(bool deferredPass)
{
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);

glAlphaFunc(GL_GREATER, 0.5f);
glEnable(GL_ALPHA_TEST);

SelectRenderState(unitDrawerStateSSP->CanEnable(this));
unitDrawerState->Enable(this, deferredPass);

// NOTE:
// when deferredPass is true we MUST be able to use unitDrawerStateSSP
// all calling code (reached from DrawDeferredPass) should ensure this
// is the case
assert(!deferredPass || unitDrawerState == unitDrawerStateSSP);
}

void CUnitDrawer::CleanUpUnitDrawing(bool deferredPass) const
{
glDisable(GL_CULL_FACE);
glDisable(GL_ALPHA_TEST);

unitDrawerState->Disable(this, deferredPass);
}



void CUnitDrawer::SetTeamColour(int team, float alpha) const
{
// need this because we can be called by no-team projectiles
if (teamHandler->IsValidTeam(team)) {
unitDrawerState->SetTeamColor(team, alpha);
}
}



/**
* Set up the texture environment in texture unit 0
* to give an S3O texture its team-colour.
*
* Also:
* - call SetBasicTeamColour to set the team colour to transform to.
* - Replace the output alpha channel. If not, only the team-coloured bits will show, if that. Or something.
*/
void CUnitDrawer::SetupBasicS3OTexture0()
{
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);

// RGB = Texture * (1 - Alpha) + Teamcolor * Alpha
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_INTERPOLATE_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_CONSTANT_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_ONE_MINUS_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);

// ALPHA = Ignore
}

/**
* This sets the first texture unit to GL_MODULATE the colours from the
* first texture unit with the current glColor.
*
* Normal S3O drawing sets the color to full white; translucencies
* use this setup to 'tint' the drawn model.
*
* - Leaves glActivateTextureARB at the first unit.
* - This doesn't tinker with the output alpha, either.
*/
void CUnitDrawer::SetupBasicS3OTexture1()
{
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);

// RGB = Primary Color * Previous
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PRIMARY_COLOR_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);

// ALPHA = Current alpha * Alpha mask
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PRIMARY_COLOR_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);
}


void CUnitDrawer::CleanupBasicS3OTexture1()
{
// reset texture1 state
glActiveTexture(GL_TEXTURE1);
glDisable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_RGB_ARB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
}

void CUnitDrawer::CleanupBasicS3OTexture0()
{
// reset texture0 state
glActiveTexture(GL_TEXTURE0);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_RGB_ARB, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
}



/**
* Draw one unit.
*
* Used for drawing the view of the controlled unit.
*
* Note: does all the GL state setting for that one unit, so you might want
* something else for drawing many units.
*/
void CUnitDrawer::DrawIndividual(CUnit* unit)
{
const bool origDebug = globalRendering->drawdebug;
globalRendering->drawdebug = false;

LuaUnitLODMaterial* lodMat = NULL;

if (unit->lodCount > 0) {
const LuaMatType matType = (water->DrawReflectionPass())?
LUAMAT_OPAQUE_REFLECT : LUAMAT_OPAQUE;
LuaUnitMaterial& unitMat = unit->luaMats[matType];
lodMat = unitMat.GetMaterial(unit->currentLOD);
}

if (lodMat && lodMat->IsActive()) {
SetUnitGlobalLODFactor(LODScale);

luaMatHandler.setup3doShader = SetupOpaque3DO;
luaMatHandler.reset3doShader = ResetOpaque3DO;
luaMatHandler.setupS3oShader = SetupOpaqueS3O;
luaMatHandler.resetS3oShader = ResetOpaqueS3O;

const LuaMaterial& mat = (LuaMaterial&) *lodMat->matref.GetBin();

// NOTE: doesn't make sense to supported deferred mode for this?
mat.Execute(LuaMaterial::defMat, false);

lodMat->uniforms.Execute(unit);

SetTeamColour(unit->team);
DrawUnitRawWithLists(unit, lodMat->preDisplayList, lodMat->postDisplayList);

LuaMaterial::defMat.Execute(mat, false);
} else {
SetupForUnitDrawing(false);
opaqueModelRenderers[MDL_TYPE(unit)]->PushRenderState();

if (MDL_TYPE(unit) != MODELTYPE_3DO) {
texturehandlerS3O->SetS3oTexture(TEX_TYPE(unit));
}

SetTeamColour(unit->team);
DrawUnitRaw(unit);

opaqueModelRenderers[MDL_TYPE(unit)]->PopRenderState();
CleanUpUnitDrawing(false);
}

globalRendering->drawdebug = origDebug;
}


/**
* Draw one unit,
* - with depth-buffering(!) and lighting DISABLED,
* - 'tinted' by the current glColor, *including* alpha.
*
* Used for drawing building orders.
*
* Note: does all the GL state setting for that one unit, so you might want
* something else for drawing many translucent units.
*/
void CUnitDrawer::DrawBuildingSample(const UnitDef* unitdef, int team, float3 pos, int facing)
{
const S3DModel* model = unitdef->LoadModel();

/* From SetupForGhostDrawing. */
glPushAttrib(GL_TEXTURE_BIT | GL_ENABLE_BIT);

assert(teamHandler->IsValidTeam(team));
IUnitDrawerState::SetBasicTeamColor(team, 1.0f);
SetupBasicS3OTexture0();

switch (model->type) {
case MODELTYPE_3DO: {
texturehandler3DO->Set3doAtlases();
} break;
case MODELTYPE_S3O:
case MODELTYPE_OBJ:
case MODELTYPE_ASS: {
texturehandlerS3O->SetS3oTexture(model->textureType);
} break;
default: {
} break;
}

SetupBasicS3OTexture1();

/* Use the alpha given by glColor for the outgoing alpha.
(Might need to change this if we ever have transparent bits on units?)
*/
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PRIMARY_COLOR_ARB);

glActiveTexture(GL_TEXTURE0);

/* From SetupForGhostDrawing. */
glDepthMask(GL_FALSE);
glDisable(GL_CULL_FACE); /* Leave out face culling, as 3DO and 3DO translucents does. */
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

/* Push out the polygons. */
glPushMatrix();
glTranslatef3(pos);
glRotatef(facing * 90.0f, 0, 1, 0);

model->DrawStatic();
glPopMatrix();

// reset texture1 state
CleanupBasicS3OTexture1();

/* Also reset the alpha generation. */
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);

// reset texture0 state
CleanupBasicS3OTexture0();

/* From CleanUpGhostDrawing. */
glPopAttrib();
glDisable(GL_TEXTURE_2D);
glDepthMask(GL_TRUE);
}

// used by LuaOpenGL::DrawUnitShape only
void CUnitDrawer::DrawUnitDef(const UnitDef* unitDef, int team)
{
const S3DModel* model = unitDef->LoadModel();

glPushAttrib(GL_TEXTURE_BIT | GL_ENABLE_BIT);
glEnable(GL_TEXTURE_2D);

// get the team-coloured texture constructed by texunit 0
assert(teamHandler->IsValidTeam(team));
IUnitDrawerState::SetBasicTeamColor(team, 1.0f);
SetupBasicS3OTexture0();

switch (model->type) {
case MODELTYPE_3DO: {
texturehandler3DO->Set3doAtlases();
} break;
case MODELTYPE_S3O: {
texturehandlerS3O->SetS3oTexture(model->textureType);
} break;
default: {
} break;
}

// tint it with the current glColor in texunit 1
SetupBasicS3OTexture1();

// use the alpha given by glColor for the outgoing alpha.
// (might need to change this if we ever have transparent bits on units?)
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PRIMARY_COLOR_ARB);

glActiveTexture(GL_TEXTURE0);
model->DrawStatic();

// reset texture1 state
CleanupBasicS3OTexture1();

// also reset the alpha generation
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);

// reset texture0 state
CleanupBasicS3OTexture0();

glPopAttrib();
}



void CUnitDrawer::DrawUnitBeingBuilt(CUnit* unit)
{
if (shadowHandler->inShadowPass) {
if (unit->buildProgress > (2.0f / 3.0f)) {
DrawUnitModel(unit);
}
return;
}

const float start = std::max(unit->model->mins.y, -unit->model->height);
const float height = unit->model->height;

glEnable(GL_CLIP_PLANE0);
glEnable(GL_CLIP_PLANE1);

const float col = math::fabs(128.0f - ((gs->frameNum * 4) & 255)) / 255.0f + 0.5f;
const unsigned char* tcol = teamHandler->Team(unit->team)->color;
// frame line-color
const float3 fc = (!globalRendering->teamNanospray)?
unit->unitDef->nanoColor:
float3(tcol[0] / 255.0f, tcol[1] / 255.0f, tcol[2] / 255.0f);

glColorf3(fc * col);

// render wireframe with FFP
unitDrawerState->DisableShaders(this);
unitDrawerState->DisableTextures(this);


// first stage: wireframe model
//
// Both clip planes move up. Clip plane 0 is the upper bound of the model,
// clip plane 1 is the lower bound. In other words, clip plane 0 makes the
// wireframe/flat color/texture appear, and clip plane 1 then erases the
// wireframe/flat color laten on.

const double plane0[4] = {0, -1, 0, start + height * (unit->buildProgress * 3)};
const double plane1[4] = {0, 1, 0, -start - height * (unit->buildProgress * 10 - 9)};
glClipPlane(GL_CLIP_PLANE0, plane0);
glClipPlane(GL_CLIP_PLANE1, plane1);

if (!globalRendering->atiHacks) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
DrawUnitModel(unit);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
} else {
// some ATi mobility cards/drivers dont like clipping wireframes...
glDisable(GL_CLIP_PLANE0);
glDisable(GL_CLIP_PLANE1);

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
DrawUnitModel(unit);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glEnable(GL_CLIP_PLANE0);
glEnable(GL_CLIP_PLANE1);
}

// Flat-colored model
if (unit->buildProgress > (1.0f / 3.0f)) {
glColorf3(fc * (1.5f - col));
const double plane0[4] = {0, -1, 0, start + height * (unit->buildProgress * 3 - 1)};
const double plane1[4] = {0, 1, 0, -start - height * (unit->buildProgress * 3 - 2)};
glClipPlane(GL_CLIP_PLANE0, plane0);
glClipPlane(GL_CLIP_PLANE1, plane1);

DrawUnitModel(unit);
}

glDisable(GL_CLIP_PLANE1);

unitDrawerState->EnableTextures(this);
unitDrawerState->EnableShaders(this);

// second stage: texture-mapped model
if (unit->buildProgress > (2.0f / 3.0f)) {
if (globalRendering->atiHacks) {
glDisable(GL_CLIP_PLANE0);

glPolygonOffset(1.0f, 1.0f);
glEnable(GL_POLYGON_OFFSET_FILL);
DrawUnitModel(unit);
glDisable(GL_POLYGON_OFFSET_FILL);
} else {
const double plane0[4] = {0, -1, 0 , start + height * (unit->buildProgress * 3 - 2)};
glClipPlane(GL_CLIP_PLANE0, plane0);

glPolygonOffset(1.0f, 1.0f);
glEnable(GL_POLYGON_OFFSET_FILL);
DrawUnitModel(unit);
glDisable(GL_POLYGON_OFFSET_FILL);
}
}

glDisable(GL_CLIP_PLANE0);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}



inline void CUnitDrawer::DrawUnitModel(CUnit* unit) {
if (unit->luaDraw && eventHandler.DrawUnit(unit)) {
return;
}

if (unit->lodCount <= 0) {
unit->localModel->Draw();
} else {
unit->localModel->DrawLOD(unit->currentLOD);
}
}


void CUnitDrawer::DrawUnitNow(CUnit* unit)
{
/*
// this interferes with Lua material management
if (unit->alphaThreshold != 0.1f) {
glPushAttrib(GL_COLOR_BUFFER_BIT);
glAlphaFunc(GL_GREATER, unit->alphaThreshold);
}
*/

glPushMatrix();
glMultMatrixf(unit->GetTransformMatrix());

if (!unit->beingBuilt || !unit->unitDef->showNanoFrame) {
DrawUnitModel(unit);
} else {
DrawUnitBeingBuilt(unit);
}
glPopMatrix();

/*
if (unit->alphaThreshold != 0.1f) {
glPopAttrib();
}
*/
}


void CUnitDrawer::DrawUnitWithLists(CUnit* unit, unsigned int preList, unsigned int postList)
{
glPushMatrix();
glMultMatrixf(unit->GetTransformMatrix());

if (preList != 0) {
glCallList(preList);
}

if (!unit->beingBuilt || !unit->unitDef->showNanoFrame) {
DrawUnitModel(unit);
} else {
DrawUnitBeingBuilt(unit);
}

if (postList != 0) {
glCallList(postList);
}
glPopMatrix();
}


void CUnitDrawer::DrawUnitRaw(CUnit* unit)
{
glPushMatrix();
glMultMatrixf(unit->GetTransformMatrix());
DrawUnitModel(unit);
glPopMatrix();
}


void CUnitDrawer::DrawUnitRawModel(CUnit* unit)
{
if (unit->lodCount <= 0) {
unit->localModel->Draw();
} else {
unit->localModel->DrawLOD(unit->currentLOD);
}
}


void CUnitDrawer::DrawUnitRawWithLists(CUnit* unit, unsigned int preList, unsigned int postList)
{
glPushMatrix();
glMultMatrixf(unit->GetTransformMatrix());

if (preList != 0) {
glCallList(preList);
}

DrawUnitModel(unit);

if (postList != 0) {
glCallList(postList);
}

glPopMatrix();
}

inline void CUnitDrawer::UpdateUnitIconState(CUnit* unit) {
const unsigned short losStatus = unit->losStatus[gu->myAllyTeam];

// reset
unit->isIcon = false;

if ((losStatus & LOS_INLOS) || gu->spectatingFullView) {
unit->isIcon = DrawAsIcon(unit, (unit->pos - camera->GetPos()).SqLength());

} else if ((losStatus & LOS_PREVLOS) && (losStatus & LOS_CONTRADAR)) {
if (gameSetup->ghostedBuildings && unit->unitDef->IsImmobileUnit()) {
unit->isIcon = DrawAsIcon(unit, (unit->pos - camera->GetPos()).SqLength());
}
}

if (unit->isIcon && !unit->noDraw && !unit->IsInVoid())
drawIcon.insert(unit);
}

inline void CUnitDrawer::UpdateUnitDrawPos(CUnit* u) {
const CTransportUnit* trans = u->GetTransporter();

if (trans != NULL) {
u->drawPos = u->pos + (trans->speed * globalRendering->timeOffset);
} else {
u->drawPos = u->pos + (u->speed * globalRendering->timeOffset);
}

u->drawMidPos = u->drawPos + (u->midPos - u->pos);
}



bool CUnitDrawer::DrawAsIcon(const CUnit* unit, const float sqUnitCamDist) const {

const float sqIconDistMult = unit->unitDef->iconType->GetDistanceSqr();
const float realIconLength = iconLength * sqIconDistMult;
bool asIcon = false;

if (useDistToGroundForIcons) {
asIcon = (sqCamDistToGroundForIcons > realIconLength);
} else {
asIcon = (sqUnitCamDist > realIconLength);
}

return asIcon;
}




// visualize if a unit can be built at specified position
bool CUnitDrawer::ShowUnitBuildSquare(const BuildInfo& buildInfo)
{
return ShowUnitBuildSquare(buildInfo, std::vector<Command>());
}

bool CUnitDrawer::ShowUnitBuildSquare(const BuildInfo& buildInfo, const std::vector<Command>& commands)
{
//TODO: make this a lua callin!
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_TEXTURE_2D);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

CFeature* feature = NULL;

std::vector<float3> buildableSquares; // buildable squares
std::vector<float3> featureSquares; // occupied squares
std::vector<float3> illegalSquares; // non-buildable squares

const float3& pos = buildInfo.pos;
const int x1 = pos.x - (buildInfo.GetXSize() * 0.5f * SQUARE_SIZE);
const int x2 = x1 + (buildInfo.GetXSize() * SQUARE_SIZE);
const int z1 = pos.z - (buildInfo.GetZSize() * 0.5f * SQUARE_SIZE);
const int z2 = z1 + (buildInfo.GetZSize() * SQUARE_SIZE);
const float h = CGameHelper::GetBuildHeight(pos, buildInfo.def, false);

const bool canBuild = !!CGameHelper::TestUnitBuildSquare(
buildInfo,
feature,
-1,
false,
&buildableSquares,
&featureSquares,
&illegalSquares,
&commands
);

if (canBuild) {
glColor4f(0.0f, 0.9f, 0.0f, 0.7f);
} else {
glColor4f(0.9f, 0.8f, 0.0f, 0.7f);
}

CVertexArray* va = GetVertexArray();
va->Initialize();
va->EnlargeArrays(buildableSquares.size() * 4, 0, VA_SIZE_0);

for (unsigned int i = 0; i < buildableSquares.size(); i++) {
va->AddVertexQ0(buildableSquares[i] );
va->AddVertexQ0(buildableSquares[i] + float3(SQUARE_SIZE, 0, 0));
va->AddVertexQ0(buildableSquares[i] + float3(SQUARE_SIZE, 0, SQUARE_SIZE));
va->AddVertexQ0(buildableSquares[i] + float3( 0, 0, SQUARE_SIZE));
}
va->DrawArray0(GL_QUADS);


glColor4f(0.9f, 0.8f, 0.0f, 0.7f);
va = GetVertexArray();
va->Initialize();
va->EnlargeArrays(featureSquares.size() * 4, 0, VA_SIZE_0);

for (unsigned int i = 0; i < featureSquares.size(); i++) {
va->AddVertexQ0(featureSquares[i] );
va->AddVertexQ0(featureSquares[i] + float3(SQUARE_SIZE, 0, 0));
va->AddVertexQ0(featureSquares[i] + float3(SQUARE_SIZE, 0, SQUARE_SIZE));
va->AddVertexQ0(featureSquares[i] + float3( 0, 0, SQUARE_SIZE));
}
va->DrawArray0(GL_QUADS);


glColor4f(0.9f, 0.0f, 0.0f, 0.7f);
va = GetVertexArray();
va->Initialize();
va->EnlargeArrays(illegalSquares.size() * 4, 0, VA_SIZE_0);

for (unsigned int i = 0; i < illegalSquares.size(); i++) {
va->AddVertexQ0(illegalSquares[i]);
va->AddVertexQ0(illegalSquares[i] + float3(SQUARE_SIZE, 0, 0));
va->AddVertexQ0(illegalSquares[i] + float3(SQUARE_SIZE, 0, SQUARE_SIZE));
va->AddVertexQ0(illegalSquares[i] + float3( 0, 0, SQUARE_SIZE));
}
va->DrawArray0(GL_QUADS);


if (h < 0.0f) {
const unsigned char s[4] = { 0, 0, 255, 128 }; // start color
const unsigned char e[4] = { 0, 128, 255, 255 }; // end color

va = GetVertexArray();
va->Initialize();
va->EnlargeArrays(8, 0, VA_SIZE_C);
va->AddVertexQC(float3(x1, h, z1), s); va->AddVertexQC(float3(x1, 0.f, z1), e);
va->AddVertexQC(float3(x1, h, z2), s); va->AddVertexQC(float3(x1, 0.f, z2), e);
va->AddVertexQC(float3(x2, h, z2), s); va->AddVertexQC(float3(x2, 0.f, z2), e);
va->AddVertexQC(float3(x2, h, z1), s); va->AddVertexQC(float3(x2, 0.f, z1), e);
va->DrawArrayC(GL_LINES);

va = GetVertexArray();
va->Initialize();
va->AddVertexQC(float3(x1, 0.0f, z1), e);
va->AddVertexQC(float3(x1, 0.0f, z2), e);
va->AddVertexQC(float3(x2, 0.0f, z2), e);
va->AddVertexQC(float3(x2, 0.0f, z1), e);
va->DrawArrayC(GL_LINE_LOOP);
}

glEnable(GL_DEPTH_TEST );
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// glDisable(GL_BLEND);

return canBuild;
}



inline const icon::CIconData* GetUnitIcon(const CUnit* unit) {
const unsigned short losStatus = unit->losStatus[gu->myAllyTeam];
const unsigned short prevMask = (LOS_PREVLOS | LOS_CONTRADAR);

const UnitDef* unitDef = unit->unitDef;
const icon::CIconData* iconData = NULL;

// use the unit's custom icon if we can currently see it,
// or have seen it before and did not lose contact since
const bool unitVisible = ((losStatus & (LOS_INLOS | LOS_INRADAR)) && ((losStatus & prevMask) == prevMask));
const bool customIcon = (minimap->UseUnitIcons() && (unitVisible || gu->spectatingFullView));

if (customIcon) {
iconData = unitDef->iconType.GetIconData();
} else {
if (losStatus & LOS_INRADAR) {
iconData = icon::iconHandler->GetDefaultIconData();
}
}

return iconData;
}

inline float GetUnitIconScale(const CUnit* unit) {
float scale = unit->myIcon->GetSize();

if (!minimap->UseUnitIcons())
return scale;
if (!unit->myIcon->GetRadiusAdjust())
return scale;

const unsigned short losStatus = unit->losStatus[gu->myAllyTeam];
const unsigned short prevMask = (LOS_PREVLOS | LOS_CONTRADAR);
const bool unitVisible = ((losStatus & LOS_INLOS) || ((losStatus & LOS_INRADAR) && ((losStatus & prevMask) == prevMask)));

if ((unitVisible || gu->spectatingFullView)) {
scale *= (unit->radius / WORLDOBJECT_DEFAULT_DRAWRADIUS);
}

return scale;
}


void CUnitDrawer::DrawUnitMiniMapIcon(const CUnit* unit, CVertexArray* va) const {
if (unit->noMinimap)
return;
if (unit->myIcon == NULL)
return;
if (unit->IsInVoid())
return;

const unsigned char defaultColor[4] = {255, 255, 255, 255};
const unsigned char* color = &defaultColor[0];

if (!unit->isSelected) {
if (minimap->UseSimpleColors()) {
if (unit->team == gu->myTeam) {
color = minimap->GetMyTeamIconColor();
} else if (teamHandler->Ally(gu->myAllyTeam, unit->allyteam)) {
color = minimap->GetAllyTeamIconColor();
} else {
color = minimap->GetEnemyTeamIconColor();
}
} else {
color = teamHandler->Team(unit->team)->color;
}
}

const float iconScale = GetUnitIconScale(unit);
const float3& iconPos = (!gu->spectatingFullView) ?
unit->GetDrawErrorPos(gu->myAllyTeam):
unit->drawMidPos;

const float iconSizeX = (iconScale * minimap->GetUnitSizeX());
const float iconSizeY = (iconScale * minimap->GetUnitSizeY());

const float x0 = iconPos.x - iconSizeX;
const float x1 = iconPos.x + iconSizeX;
const float y0 = iconPos.z - iconSizeY;
const float y1 = iconPos.z + iconSizeY;

unit->myIcon->DrawArray(va, x0, y0, x1, y1, color);
}

// TODO:
// UnitDrawer::DrawIcon was half-duplicate of MiniMap::DrawUnit&co
// the latter has been replaced by this, do the same for the former
// (mini-map icons and real-map radar icons are the same anyway)
void CUnitDrawer::DrawUnitMiniMapIcons() const {
std::map<icon::CIconData*, std::set<const CUnit*> >::const_iterator iconIt;
std::set<const CUnit*>::const_iterator unitIt;

CVertexArray* va = GetVertexArray();

for (iconIt = unitsByIcon.begin(); iconIt != unitsByIcon.end(); ++iconIt) {
const icon::CIconData* icon = iconIt->first;
const std::set<const CUnit*>& units = iconIt->second;

if (icon == NULL)
continue;
if (units.empty())
continue;

va->Initialize();
va->EnlargeArrays(units.size() * 4, 0, VA_SIZE_2DTC);
icon->BindTexture();

for (unitIt = units.begin(); unitIt != units.end(); ++unitIt) {
assert((*unitIt)->myIcon == icon);
DrawUnitMiniMapIcon(*unitIt, va);
}

va->DrawArray2dTC(GL_QUADS);
}
}

void CUnitDrawer::UpdateUnitMiniMapIcon(const CUnit* unit, bool forced, bool killed) {
icon::CIconData* oldIcon = unit->myIcon;
icon::CIconData* newIcon = const_cast<icon::CIconData*>(GetUnitIcon(unit));

CUnit* u = const_cast<CUnit*>(unit);

if (!killed) {
if ((oldIcon != newIcon) || forced) {
unitsByIcon[oldIcon].erase(u);
unitsByIcon[newIcon].insert(u);
}
} else {
unitsByIcon[oldIcon].erase(u);
}

u->myIcon = killed? NULL: newIcon;
}



void CUnitDrawer::RenderUnitCreated(const CUnit* u, int cloaked) {
CUnit* unit = const_cast<CUnit*>(u);
texturehandlerS3O->UpdateDraw();

if (u->model != NULL) {
if (cloaked) {
cloakedModelRenderers[MDL_TYPE(u)]->AddUnit(u);
} else {
opaqueModelRenderers[MDL_TYPE(u)]->AddUnit(u);
}
}

UpdateUnitMiniMapIcon(u, false, false);
unsortedUnits.insert(unit);
}


void CUnitDrawer::RenderUnitDestroyed(const CUnit* unit) {
CUnit* u = const_cast<CUnit*>(unit);

if ((dynamic_cast<CBuilding*>(u) != NULL) && gameSetup->ghostedBuildings &&
!(u->losStatus[gu->myAllyTeam] & (LOS_INLOS | LOS_CONTRADAR)) &&
(u->losStatus[gu->myAllyTeam] & (LOS_PREVLOS)) && !gu->spectatingFullView
) {
// FIXME -- adjust decals for decoys? gets weird?
const UnitDef* decoyDef = u->unitDef->decoyDef;
S3DModel* gbModel = (decoyDef == NULL) ? u->model : decoyDef->LoadModel();

GhostSolidObject* gb = new GhostSolidObject();
gb->pos = u->pos;
gb->model = gbModel;
gb->decal = NULL;
gb->facing = u->buildFacing;
gb->dir = u->frontdir;
gb->team = u->team;

deadGhostBuildings[gbModel->type].insert(gb);

groundDecals->GhostCreated(u, gb);
}

if (u->model) {
// renderer unit cloak state may not match sim (because of MT) - erase from both renderers to be sure
cloakedModelRenderers[MDL_TYPE(u)]->DelUnit(u);
opaqueModelRenderers[MDL_TYPE(u)]->DelUnit(u);
}

unsortedUnits.erase(u);
liveGhostBuildings[MDL_TYPE(u)].erase(u);

// remove the icon for all ally-teams
for (std::vector<std::set<CUnit*> >::iterator it = unitRadarIcons.begin(); it != unitRadarIcons.end(); ++it) {
(*it).erase(u);
}
UpdateUnitMiniMapIcon(unit, false, true);
SetUnitLODCount(u, 0);
}


void CUnitDrawer::RenderUnitCloakChanged(const CUnit* unit, int cloaked) {
CUnit* u = const_cast<CUnit*>(unit);

if (u->model) {
if (cloaked) {
cloakedModelRenderers[MDL_TYPE(u)]->AddUnit(u);
opaqueModelRenderers[MDL_TYPE(u)]->DelUnit(u);
} else {
opaqueModelRenderers[MDL_TYPE(u)]->AddUnit(u);
cloakedModelRenderers[MDL_TYPE(u)]->DelUnit(u);
}
}
}


void CUnitDrawer::RenderUnitLOSChanged(const CUnit* unit, int allyTeam, int newStatus) {
CUnit* u = const_cast<CUnit*>(unit);

if (newStatus & LOS_INLOS) {
if (allyTeam == gu->myAllyTeam) {
if (gameSetup->ghostedBuildings && unit->unitDef->IsImmobileUnit()) {
liveGhostBuildings[MDL_TYPE(unit)].erase(u);
}
}
unitRadarIcons[allyTeam].erase(u);
} else {
if (newStatus & LOS_PREVLOS) {
if (allyTeam == gu->myAllyTeam) {
if (gameSetup->ghostedBuildings && unit->unitDef->IsImmobileUnit()) {
liveGhostBuildings[MDL_TYPE(unit)].insert(u);
}
}
}

if (newStatus & LOS_INRADAR) {
unitRadarIcons[allyTeam].insert(u);
} else {
unitRadarIcons[allyTeam].erase(u);
}
}

UpdateUnitMiniMapIcon(unit, false, false);
}






unsigned int CUnitDrawer::CalcUnitLOD(const CUnit* unit, unsigned int lastLOD)
{
if (lastLOD == 0) { return 0; }

const float3 diff = (unit->pos - camera->GetPos());
const float dist = diff.dot(camera->forward);
const float lpp = std::max(0.0f, dist * UNIT_GLOBAL_LOD_FACTOR);

for (/* no-op */; lastLOD != 0; lastLOD--) {
if (lpp > unit->lodLengths[lastLOD]) {
break;
}
}

return lastLOD;
}

// unused
unsigned int CUnitDrawer::CalcUnitShadowLOD(const CUnit* unit, unsigned int lastLOD)
{
return CalcUnitLOD(unit, lastLOD); // FIXME

// FIXME -- the more 'correct' method
if (lastLOD == 0) { return 0; }

// FIXME: fix it, cap it for shallow shadows?
const float3& sun = sky->GetLight()->GetLightDir();
const float3 diff = (camera->GetPos() - unit->pos);
const float dot = diff.dot(sun);
const float3 gap = diff - (sun * dot);
const float lpp = std::max(0.0f, gap.Length() * UNIT_GLOBAL_LOD_FACTOR);

for (/* no-op */; lastLOD != 0; lastLOD--) {
if (lpp > unit->lodLengths[lastLOD]) {
break;
}
}

return lastLOD;
}

void CUnitDrawer::SetUnitLODCount(CUnit* unit, unsigned int count)
{
const unsigned int oldCount = unit->lodCount;

unit->lodCount = count;
unit->lodLengths.resize(count);
unit->localModel->SetLODCount(count);

for (unsigned int i = oldCount; i < count; i++) {
unit->lodLengths[i] = -1.0f;
}

for (int m = 0; m < LUAMAT_TYPE_COUNT; m++) {
unit->luaMats[m].SetLODCount(count);
}
}

void CUnitDrawer::PlayerChanged(int playerNum) {
if (playerNum != gu->myPlayerNum)
return;

std::map<icon::CIconData*, std::set<const CUnit*> >::iterator iconIt;
std::set<CUnit*>::const_iterator unitIt;

for (iconIt = unitsByIcon.begin(); iconIt != unitsByIcon.end(); ++iconIt) {
(iconIt->second).clear();
}

for (unitIt = unsortedUnits.begin(); unitIt != unsortedUnits.end(); ++unitIt) {
// force an erase (no-op) followed by an insert
UpdateUnitMiniMapIcon(*unitIt, true, false);
}
}

void CUnitDrawer::SunChanged(const float3& sunDir) {
unitDrawerState->UpdateCurrentShader(this, sky->GetLight());
}
Something went wrong with that request. Please try again.