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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "ProjectileDrawer.h"
#include "Game/Camera.h"
#include "Game/GlobalUnsynced.h"
#include "Game/LoadScreen.h"
#include "Lua/LuaParser.h"
#include "Map/MapInfo.h"
#include "Rendering/GroundFlash.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/ShadowHandler.h"
#include "Rendering/UnitDrawer.h"
#include "Rendering/Env/ISky.h"
#include "Rendering/GL/FBO.h"
#include "Rendering/GL/VertexArray.h"
#include "Rendering/Shaders/Shader.h"
#include "Rendering/Textures/Bitmap.h"
#include "Rendering/Textures/ColorMap.h"
#include "Rendering/Textures/S3OTextureHandler.h"
#include "Rendering/Textures/TextureAtlas.h"
#include "Rendering/Models/ModelRenderContainer.h"
#include "Sim/Misc/LosHandler.h"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/Projectiles/ExplosionGenerator.h"
#include "Sim/Projectiles/Projectile.h"
#include "Sim/Projectiles/ProjectileHandler.h"
#include "Sim/Projectiles/PieceProjectile.h"
#include "Rendering/Env/Particles/Classes/FlyingPiece.h"
#include "Sim/Projectiles/WeaponProjectiles/WeaponProjectile.h"
#include "Sim/Weapons/WeaponDefHandler.h"
#include "Sim/Weapons/WeaponDef.h"
#include "System/EventHandler.h"
#include "System/Exceptions.h"
#include "System/Log/ILog.h"
#include "System/SafeUtil.h"
#include "System/StringUtil.h"
CProjectileDrawer* projectileDrawer = nullptr;
CProjectileDrawer::CProjectileDrawer(): CEventClient("[CProjectileDrawer]", 123456, false) {
eventHandler.AddClient(this);
loadscreen->SetLoadMessage("Creating Projectile Textures");
textureAtlas = new CTextureAtlas(); textureAtlas->SetName("ProjectileTextureAtlas");
groundFXAtlas = new CTextureAtlas(); groundFXAtlas->SetName("ProjectileEffectsAtlas");
LuaParser resourcesParser("gamedata/resources.lua", SPRING_VFS_MOD_BASE, SPRING_VFS_ZIP);
LuaParser mapResParser("gamedata/resources_map.lua", SPRING_VFS_MAP_BASE, SPRING_VFS_ZIP);
resourcesParser.Execute();
const LuaTable& resTable = resourcesParser.GetRoot();
const LuaTable& resGraphicsTable = resTable.SubTable("graphics");
const LuaTable& resProjTexturesTable = resGraphicsTable.SubTable("projectileTextures");
const LuaTable& resSmokeTexturesTable = resGraphicsTable.SubTable("smoke");
const LuaTable& resGroundFXTexturesTable = resGraphicsTable.SubTable("groundfx");
// used to block resources_map.* from overriding any of
// resources.lua:{projectile, smoke, groundfx}textures,
// as well as various defaults (repulsegfxtexture, etc)
spring::unordered_set<std::string> blockedTexNames;
ParseAtlasTextures(true, resProjTexturesTable, blockedTexNames, textureAtlas);
ParseAtlasTextures(true, resGroundFXTexturesTable, blockedTexNames, groundFXAtlas);
int smokeTexCount = -1;
{
// get the smoke textures, hold the count in 'smokeTexCount'
if (resSmokeTexturesTable.IsValid()) {
for (smokeTexCount = 0; true; smokeTexCount++) {
const std::string& tex = resSmokeTexturesTable.GetString(smokeTexCount + 1, "");
if (tex.empty()) {
break;
}
const std::string texName = "bitmaps/" + tex;
const std::string smokeName = "ismoke" + IntToString(smokeTexCount, "%02i");
textureAtlas->AddTexFromFile(smokeName, texName);
blockedTexNames.insert(StringToLower(smokeName));
}
} else {
// setup the defaults
for (smokeTexCount = 0; smokeTexCount < 12; smokeTexCount++) {
const std::string smokeNum = IntToString(smokeTexCount, "%02i");
const std::string smokeName = "ismoke" + smokeNum;
const std::string texName = "bitmaps/smoke/smoke" + smokeNum + ".tga";
textureAtlas->AddTexFromFile(smokeName, texName);
blockedTexNames.insert(StringToLower(smokeName));
}
}
if (smokeTexCount <= 0) {
// this needs to be an exception, other code
// assumes at least one smoke-texture exists
throw content_error("missing smoke textures");
}
}
{
// shield-texture memory
std::array<char, 4 * perlinTexSize * perlinTexSize> perlinTexMem;
perlinTexMem.fill(70);
textureAtlas->AddTexFromMem("perlintex", perlinTexSize, perlinTexSize, CTextureAtlas::RGBA32, &perlinTexMem[0]);
blockedTexNames.insert("perlintex");
}
blockedTexNames.insert("flare");
blockedTexNames.insert("explo");
blockedTexNames.insert("explofade");
blockedTexNames.insert("heatcloud");
blockedTexNames.insert("laserend");
blockedTexNames.insert("laserfalloff");
blockedTexNames.insert("randdots");
blockedTexNames.insert("smoketrail");
blockedTexNames.insert("wake");
blockedTexNames.insert("flame");
blockedTexNames.insert("sbtrailtexture");
blockedTexNames.insert("missiletrailtexture");
blockedTexNames.insert("muzzleflametexture");
blockedTexNames.insert("repulsetexture");
blockedTexNames.insert("dguntexture");
blockedTexNames.insert("flareprojectiletexture");
blockedTexNames.insert("sbflaretexture");
blockedTexNames.insert("missileflaretexture");
blockedTexNames.insert("beamlaserflaretexture");
blockedTexNames.insert("bubbletexture");
blockedTexNames.insert("geosquaretexture");
blockedTexNames.insert("gfxtexture");
blockedTexNames.insert("projectiletexture");
blockedTexNames.insert("repulsegfxtexture");
blockedTexNames.insert("sphereparttexture");
blockedTexNames.insert("torpedotexture");
blockedTexNames.insert("wrecktexture");
blockedTexNames.insert("plasmatexture");
if (mapResParser.Execute()) {
// allow map-specified atlas textures (for gaia-projectiles and ground-flashes)
const LuaTable& mapResTable = mapResParser.GetRoot();
const LuaTable& mapResGraphicsTable = mapResTable.SubTable("graphics");
const LuaTable& mapResProjTexturesTable = mapResGraphicsTable.SubTable("projectileTextures");
const LuaTable& mapResGroundFXTexturesTable = mapResGraphicsTable.SubTable("groundfx");
ParseAtlasTextures(false, mapResProjTexturesTable, blockedTexNames, textureAtlas);
ParseAtlasTextures(false, mapResGroundFXTexturesTable, blockedTexNames, groundFXAtlas);
}
if (!textureAtlas->Finalize())
LOG_L(L_ERROR, "Could not finalize projectile-texture atlas. Use fewer/smaller textures.");
flaretex = &textureAtlas->GetTexture("flare");
explotex = &textureAtlas->GetTexture("explo");
explofadetex = &textureAtlas->GetTexture("explofade");
heatcloudtex = &textureAtlas->GetTexture("heatcloud");
laserendtex = &textureAtlas->GetTexture("laserend");
laserfallofftex = &textureAtlas->GetTexture("laserfalloff");
randdotstex = &textureAtlas->GetTexture("randdots");
smoketrailtex = &textureAtlas->GetTexture("smoketrail");
waketex = &textureAtlas->GetTexture("wake");
perlintex = &textureAtlas->GetTexture("perlintex");
flametex = &textureAtlas->GetTexture("flame");
for (int i = 0; i < smokeTexCount; i++) {
const std::string smokeName = "ismoke" + IntToString(i, "%02i");
const AtlasedTexture* smokeTex = &textureAtlas->GetTexture(smokeName);
smoketex.push_back(smokeTex);
}
sbtrailtex = &textureAtlas->GetTextureWithBackup("sbtrailtexture", "smoketrail" );
missiletrailtex = &textureAtlas->GetTextureWithBackup("missiletrailtexture", "smoketrail" );
muzzleflametex = &textureAtlas->GetTextureWithBackup("muzzleflametexture", "explo" );
repulsetex = &textureAtlas->GetTextureWithBackup("repulsetexture", "explo" );
dguntex = &textureAtlas->GetTextureWithBackup("dguntexture", "flare" );
flareprojectiletex = &textureAtlas->GetTextureWithBackup("flareprojectiletexture", "flare" );
sbflaretex = &textureAtlas->GetTextureWithBackup("sbflaretexture", "flare" );
missileflaretex = &textureAtlas->GetTextureWithBackup("missileflaretexture", "flare" );
beamlaserflaretex = &textureAtlas->GetTextureWithBackup("beamlaserflaretexture", "flare" );
bubbletex = &textureAtlas->GetTextureWithBackup("bubbletexture", "circularthingy");
geosquaretex = &textureAtlas->GetTextureWithBackup("geosquaretexture", "circularthingy");
gfxtex = &textureAtlas->GetTextureWithBackup("gfxtexture", "circularthingy");
projectiletex = &textureAtlas->GetTextureWithBackup("projectiletexture", "circularthingy");
repulsegfxtex = &textureAtlas->GetTextureWithBackup("repulsegfxtexture", "circularthingy");
sphereparttex = &textureAtlas->GetTextureWithBackup("sphereparttexture", "circularthingy");
torpedotex = &textureAtlas->GetTextureWithBackup("torpedotexture", "circularthingy");
wrecktex = &textureAtlas->GetTextureWithBackup("wrecktexture", "circularthingy");
plasmatex = &textureAtlas->GetTextureWithBackup("plasmatexture", "circularthingy");
if (!groundFXAtlas->Finalize())
LOG_L(L_ERROR, "Could not finalize groundFX texture atlas. Use fewer/smaller textures.");
groundflashtex = &groundFXAtlas->GetTexture("groundflash");
groundringtex = &groundFXAtlas->GetTexture("groundring");
seismictex = &groundFXAtlas->GetTexture("seismic");
for (int a = 0; a < 4; ++a) {
perlinBlend[a] = 0.0f;
}
{
glGenTextures(8, perlinBlendTex);
for (int a = 0; a < 8; ++a) {
glBindTexture(GL_TEXTURE_2D, perlinBlendTex[a]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, perlinBlendTexSize,perlinBlendTexSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
}
}
perlinTexObjects = 0;
drawPerlinTex = false;
if (perlinFB.IsValid()) {
// we never refresh the full texture (just the perlin part). So we need to reload it then.
perlinFB.reloadOnAltTab = true;
perlinFB.Bind();
perlinFB.AttachTexture(textureAtlas->GetTexID());
drawPerlinTex = perlinFB.CheckStatus("PERLIN");
perlinFB.Unbind();
}
for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
modelRenderers[modelType] = IModelRenderContainer::GetInstance(modelType);
}
}
CProjectileDrawer::~CProjectileDrawer() {
eventHandler.RemoveClient(this);
glDeleteTextures(8, perlinBlendTex);
spring::SafeDelete(textureAtlas);
spring::SafeDelete(groundFXAtlas);
for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
spring::SafeDelete(modelRenderers[modelType]);
}
renderProjectiles.clear();
}
void CProjectileDrawer::ParseAtlasTextures(
const bool blockTextures,
const LuaTable& textureTable,
spring::unordered_set<std::string>& blockedTextures,
CTextureAtlas* texAtlas
) {
std::vector<std::string> subTables;
spring::unordered_map<std::string, std::string> texturesMap;
textureTable.GetMap(texturesMap);
textureTable.GetKeys(subTables);
for (auto texturesMapIt = texturesMap.begin(); texturesMapIt != texturesMap.end(); ++texturesMapIt) {
const std::string textureName = StringToLower(texturesMapIt->first);
// no textures added to this atlas are allowed
// to be overwritten later by other textures of
// the same name
if (blockTextures)
blockedTextures.insert(textureName);
if (blockTextures || (blockedTextures.find(textureName) == blockedTextures.end()))
texAtlas->AddTexFromFile(texturesMapIt->first, "bitmaps/" + texturesMapIt->second);
}
texturesMap.clear();
for (size_t i = 0; i < subTables.size(); i++) {
const LuaTable& textureSubTable = textureTable.SubTable(subTables[i]);
if (!textureSubTable.IsValid())
continue;
textureSubTable.GetMap(texturesMap);
for (auto texturesMapIt = texturesMap.begin(); texturesMapIt != texturesMap.end(); ++texturesMapIt) {
const std::string textureName = StringToLower(texturesMapIt->first);
if (blockTextures)
blockedTextures.insert(textureName);
if (blockTextures || (blockedTextures.find(textureName) == blockedTextures.end()))
texAtlas->AddTexFromFile(texturesMapIt->first, "bitmaps/" + texturesMapIt->second);
}
texturesMap.clear();
}
}
void CProjectileDrawer::LoadWeaponTextures() {
// post-process the synced weapon-defs to set unsynced fields
// (this requires CWeaponDefHandler to have been initialized)
for (WeaponDef& wd: weaponDefHandler->weaponDefs) {
wd.visuals.texture1 = nullptr;
wd.visuals.texture2 = nullptr;
wd.visuals.texture3 = nullptr;
wd.visuals.texture4 = nullptr;
if (wd.type == "Cannon") {
wd.visuals.texture1 = plasmatex;
} else if (wd.type == "AircraftBomb") {
wd.visuals.texture1 = plasmatex;
} else if (wd.type == "Shield") {
wd.visuals.texture1 = perlintex;
} else if (wd.type == "Flame") {
wd.visuals.texture1 = flametex;
if (wd.visuals.colorMap == nullptr) {
wd.visuals.colorMap = CColorMap::LoadFromDefString(
"1.0 1.0 1.0 0.1 "
"0.025 0.025 0.025 0.10 "
"0.0 0.0 0.0 0.0"
);
}
} else if (wd.type == "MissileLauncher") {
wd.visuals.texture1 = missileflaretex;
wd.visuals.texture2 = missiletrailtex;
} else if (wd.type == "TorpedoLauncher") {
wd.visuals.texture1 = plasmatex;
} else if (wd.type == "LaserCannon") {
wd.visuals.texture1 = laserfallofftex;
wd.visuals.texture2 = laserendtex;
} else if (wd.type == "BeamLaser") {
if (wd.largeBeamLaser) {
wd.visuals.texture1 = &textureAtlas->GetTexture("largebeam");
wd.visuals.texture2 = laserendtex;
wd.visuals.texture3 = &textureAtlas->GetTexture("muzzleside");
wd.visuals.texture4 = beamlaserflaretex;
} else {
wd.visuals.texture1 = laserfallofftex;
wd.visuals.texture2 = laserendtex;
wd.visuals.texture3 = beamlaserflaretex;
}
} else if (wd.type == "LightningCannon") {
wd.visuals.texture1 = laserfallofftex;
} else if (wd.type == "EmgCannon") {
wd.visuals.texture1 = plasmatex;
} else if (wd.type == "StarburstLauncher") {
wd.visuals.texture1 = sbflaretex;
wd.visuals.texture2 = sbtrailtex;
wd.visuals.texture3 = explotex;
} else {
wd.visuals.texture1 = plasmatex;
wd.visuals.texture2 = plasmatex;
}
// override the textures if we have specified names for them
if (!wd.visuals.texNames[0].empty()) { wd.visuals.texture1 = &textureAtlas->GetTexture(wd.visuals.texNames[0]); }
if (!wd.visuals.texNames[1].empty()) { wd.visuals.texture2 = &textureAtlas->GetTexture(wd.visuals.texNames[1]); }
if (!wd.visuals.texNames[2].empty()) { wd.visuals.texture3 = &textureAtlas->GetTexture(wd.visuals.texNames[2]); }
if (!wd.visuals.texNames[3].empty()) { wd.visuals.texture4 = &textureAtlas->GetTexture(wd.visuals.texNames[3]); }
if (!wd.visuals.ptrailExpGenTag.empty()) {
// these can only be custom EG's so prefix is not required game-side
wd.ptrailExplosionGeneratorID = explGenHandler->LoadGeneratorID(CEG_PREFIX_STRING + wd.visuals.ptrailExpGenTag);
}
if (!wd.visuals.impactExpGenTag.empty()) {
wd.impactExplosionGeneratorID = explGenHandler->LoadGeneratorID(wd.visuals.impactExpGenTag);
}
if (!wd.visuals.bounceExpGenTag.empty()) {
wd.bounceExplosionGeneratorID = explGenHandler->LoadGeneratorID(wd.visuals.bounceExpGenTag);
}
}
}
void CProjectileDrawer::DrawProjectiles(int modelType, bool drawReflection, bool drawRefraction)
{
auto& projectileBin = modelRenderers[modelType]->GetProjectileBinMutable();
for (auto binIt = projectileBin.cbegin(); binIt != projectileBin.cend(); ++binIt) {
CUnitDrawer::BindModelTypeTexture(modelType, binIt->first);
DrawProjectilesSet(binIt->second, drawReflection, drawRefraction);
}
DrawFlyingPieces(modelType);
}
void CProjectileDrawer::DrawProjectilesSet(const std::vector<CProjectile*>& projectiles, bool drawReflection, bool drawRefraction)
{
for (CProjectile* p: projectiles) {
DrawProjectileNow(p, drawReflection, drawRefraction);
}
}
bool CProjectileDrawer::CanDrawProjectile(const CProjectile* pro, const CSolidObject* owner)
{
auto& th = teamHandler;
auto& lh = losHandler;
return (gu->spectatingFullView || (owner != nullptr && th->Ally(owner->allyteam, gu->myAllyTeam)) || lh->InLos(pro, gu->myAllyTeam));
}
void CProjectileDrawer::DrawProjectileNow(CProjectile* pro, bool drawReflection, bool drawRefraction)
{
pro->drawPos = pro->GetDrawPos(globalRendering->timeOffset);
if (!CanDrawProjectile(pro, pro->owner()))
return;
if (drawRefraction && (pro->drawPos.y > pro->GetDrawRadius()) /*!pro->IsInWater()*/)
return;
if (drawReflection && !CUnitDrawer::ObjectVisibleReflection(pro->drawPos, camera->GetPos(), pro->GetDrawRadius()))
return;
const CCamera* cam = CCamera::GetActiveCamera();
if (!cam->InView(pro->drawPos, pro->GetDrawRadius()))
return;
DrawProjectileModel(pro);
if (pro->drawSorted) {
pro->SetSortDist(cam->ProjectedDistance(pro->pos));
zSortedProjectiles.push_back(pro);
} else {
unsortedProjectiles.push_back(pro);
}
}
void CProjectileDrawer::DrawProjectilesShadow(int modelType)
{
auto& projectileBin = modelRenderers[modelType]->GetProjectileBinMutable();
for (auto binIt = projectileBin.cbegin(); binIt != projectileBin.cend(); ++binIt) {
DrawProjectilesSetShadow(binIt->second);
}
DrawFlyingPieces(modelType);
}
void CProjectileDrawer::DrawProjectilesSetShadow(const std::vector<CProjectile*>& projectiles)
{
for (CProjectile* p: projectiles) {
DrawProjectileShadow(p);
}
}
void CProjectileDrawer::DrawProjectileShadow(CProjectile* p)
{
if (CanDrawProjectile(p, p->owner())) {
const CCamera* cam = CCamera::GetActiveCamera();
if (!cam->InView(p->drawPos, p->GetDrawRadius()))
return;
// if this returns false, then projectile is
// neither weapon nor piece, or has no model
if (DrawProjectileModel(p))
return;
if (!p->castShadow)
return;
// don't need to z-sort in the shadow pass
p->Draw(projectileDrawer->fxVA);
}
}
void CProjectileDrawer::DrawProjectilesMiniMap()
{
CVertexArray* lines = GetVertexArray();
CVertexArray* points = GetVertexArray();
lines->Initialize();
points->Initialize();
for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
const auto& projectileBin = modelRenderers[modelType]->GetProjectileBin();
if (projectileBin.empty())
continue;
for (auto binIt = projectileBin.cbegin(); binIt != projectileBin.cend(); ++binIt) {
const auto& projectileSet = binIt->second;
lines->EnlargeArrays(projectileSet.size() * 2, 0, VA_SIZE_C);
points->EnlargeArrays(projectileSet.size(), 0, VA_SIZE_C);
for (CProjectile* p: projectileSet) {
if (!CanDrawProjectile(p, p->owner()))
continue;
p->DrawOnMinimap(*lines, *points);
}
}
}
lines->DrawArrayC(GL_LINES);
points->DrawArrayC(GL_POINTS);
if (!renderProjectiles.empty()) {
lines->Initialize();
lines->EnlargeArrays(renderProjectiles.size() * 2, 0, VA_SIZE_C);
points->Initialize();
points->EnlargeArrays(renderProjectiles.size(), 0, VA_SIZE_C);
for (CProjectile* p: renderProjectiles) {
if (!CanDrawProjectile(p, p->owner()))
continue;
p->DrawOnMinimap(*lines, *points);
}
lines->DrawArrayC(GL_LINES);
points->DrawArrayC(GL_POINTS);
}
}
void CProjectileDrawer::DrawFlyingPieces(int modelType)
{
const FlyingPieceContainer& container = projectileHandler->flyingPieces[modelType];
if (container.empty())
return;
glPushAttrib(GL_POLYGON_BIT);
glDisable(GL_CULL_FACE);
const FlyingPiece* last = nullptr;
for (const FlyingPiece& fp: container) {
const bool noLosTst = gu->spectatingFullView || teamHandler->AlliedTeams(gu->myTeam, fp.GetTeam());
const bool inAirLos = noLosTst || losHandler->InAirLos(fp.GetPos(), gu->myAllyTeam);
if (!inAirLos)
continue;
if (!camera->InView(fp.GetPos(), fp.GetRadius()))
continue;
fp.Draw(last);
last = &fp;
}
if (last != nullptr)
last->EndDraw();
glPopAttrib();
}
void CProjectileDrawer::Draw(bool drawReflection, bool drawRefraction) {
glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT);
glDisable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glDepthMask(GL_TRUE);
sky->SetupFog();
zSortedProjectiles.clear();
unsortedProjectiles.clear();
{
unitDrawer->SetupOpaqueDrawing(false);
for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
unitDrawer->PushModelRenderState(modelType);
DrawProjectiles(modelType, drawReflection, drawRefraction);
unitDrawer->PopModelRenderState(modelType);
}
unitDrawer->ResetOpaqueDrawing(false);
// note: model-less projectiles are NOT drawn by this call but
// only z-sorted (if the projectiles indicate they want to be)
DrawProjectilesSet(renderProjectiles, drawReflection, drawRefraction);
std::sort(zSortedProjectiles.begin(), zSortedProjectiles.end(), zSortCmp);
fxVA = GetVertexArray();
fxVA->Initialize();
// collect the alpha-translucent particle effects in fxVA
for (CProjectile* p: zSortedProjectiles) {
p->Draw(fxVA);
}
for (CProjectile* p: unsortedProjectiles) {
p->Draw(fxVA);
}
}
glEnable(GL_BLEND);
glDisable(GL_FOG);
if (fxVA->drawIndex() > 0) {
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glColor4f(1.0f, 1.0f, 1.0f, 0.2f);
glAlphaFunc(GL_GREATER, 0.0f);
glEnable(GL_ALPHA_TEST);
glDepthMask(GL_FALSE);
// send event after the default state has been set, allows overriding
// it for specific cases such as proper blending with depth-aware fog
// (requires mask=true and func=always)
eventHandler.DrawWorldPreParticles();
textureAtlas->BindTexture();
fxVA->DrawArrayTC(GL_QUADS);
} else {
eventHandler.DrawWorldPreParticles();
}
glPopAttrib();
}
void CProjectileDrawer::DrawShadowPass()
{
Shader::IProgramObject* po =
shadowHandler->GetShadowGenProg(CShadowHandler::SHADOWGEN_PROGRAM_PROJECTILE);
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_TEXTURE_2D);
po->Enable();
fxVA = GetVertexArray();
fxVA->Initialize();
{
for (int modelType = MODELTYPE_3DO; modelType < MODELTYPE_OTHER; modelType++) {
DrawProjectilesShadow(modelType);
}
// draw the model-less projectiles
DrawProjectilesSetShadow(renderProjectiles);
}
if (fxVA->drawIndex() > 0) {
glEnable(GL_TEXTURE_2D);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glAlphaFunc(GL_GREATER, 0.3f);
glEnable(GL_ALPHA_TEST);
glShadeModel(GL_SMOOTH);
// glDisable(GL_CULL_FACE);
textureAtlas->BindTexture();
fxVA->DrawArrayTC(GL_QUADS);
}
po->Disable();
glShadeModel(GL_FLAT);
glPopAttrib();
}
bool CProjectileDrawer::DrawProjectileModel(const CProjectile* p)
{
if (!(p->weapon || p->piece) || (p->model == nullptr))
return false;
if (p->weapon) {
// weapon-projectile
const CWeaponProjectile* wp = static_cast<const CWeaponProjectile*>(p);
unitDrawer->SetTeamColour(wp->GetTeamID());
glPushMatrix();
glMultMatrixf(wp->GetTransformMatrix(wp->GetProjectileType() == WEAPON_MISSILE_PROJECTILE));
if (!(/*p->luaDraw &&*/ eventHandler.DrawProjectile(p))) {
wp->model->DrawStatic();
}
glPopMatrix();
} else {
// piece-projectile
const CPieceProjectile* pp = static_cast<const CPieceProjectile*>(p);
unitDrawer->SetTeamColour(pp->GetTeamID());
glPushMatrix();
glTranslatef3(pp->drawPos);
glRotatef(pp->GetDrawAngle(), pp->spinVec.x, pp->spinVec.y, pp->spinVec.z);
if (!(/*p->luaDraw &&*/ eventHandler.DrawProjectile(p))) {
if (pp->explFlags & PF_Recursive) {
pp->omp->DrawStatic();
} else {
glCallList(pp->dispList);
}
}
glPopMatrix();
}
return true;
}
void CProjectileDrawer::DrawGroundFlashes()
{
GroundFlashContainer& gfc = projectileHandler->groundFlashes;
if (gfc.empty())
return;
static constexpr GLfloat black[] = {0.0f, 0.0f, 0.0f, 0.0f};
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glActiveTexture(GL_TEXTURE0);
groundFXAtlas->BindTexture();
glEnable(GL_TEXTURE_2D);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.01f);
glPolygonOffset(-20, -1000);
glEnable(GL_POLYGON_OFFSET_FILL);
glFogfv(GL_FOG_COLOR, black);
gfVA = GetVertexArray();
gfVA->Initialize();
gfVA->EnlargeArrays(8 * gfc.size(), 0, VA_SIZE_TC);
bool depthTest = true;
bool depthMask = false;
for (CGroundFlash* gf: gfc) {
const bool inLos = gf->alwaysVisible || gu->spectatingFullView || losHandler->InAirLos(gf, gu->myAllyTeam);
if (!inLos)
continue;
if (!camera->InView(gf->pos, gf->size))
continue;
if (depthTest != gf->depthTest) {
gfVA->DrawArrayTC(GL_QUADS);
gfVA->Initialize();
if ((depthTest = gf->depthTest)) {
glEnable(GL_DEPTH_TEST);
} else {
glDisable(GL_DEPTH_TEST);
}
}
if (depthMask != gf->depthMask) {
gfVA->DrawArrayTC(GL_QUADS);
gfVA->Initialize();
if ((depthMask = gf->depthMask)) {
glDepthMask(GL_TRUE);
} else {
glDepthMask(GL_FALSE);
}
}
gf->Draw(gfVA);
}
gfVA->DrawArrayTC(GL_QUADS);
glFogfv(GL_FOG_COLOR, sky->fogColor);
glDisable(GL_POLYGON_OFFSET_FILL);
glDisable(GL_ALPHA_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
}
void CProjectileDrawer::UpdateTextures() {
if (perlinTexObjects > 0 && drawPerlinTex)
UpdatePerlin();
}
void CProjectileDrawer::UpdatePerlin() {
perlinFB.Bind();
glViewport(perlintex->xstart * (textureAtlas->GetSize()).x, perlintex->ystart * (textureAtlas->GetSize()).y, perlinTexSize, perlinTexSize);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, 1, 0, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
glEnable(GL_TEXTURE_2D);
glDisable(GL_ALPHA_TEST);
glDisable(GL_FOG);
unsigned char col[4];
float time = globalRendering->lastFrameTime * gs->speedFactor * 0.003f;
float speed = 1.0f;
float size = 1.0f;
CVertexArray* va = GetVertexArray();
va->CheckInitSize(4 * VA_SIZE_TC);
for (int a = 0; a < 4; ++a) {
perlinBlend[a] += time * speed;
if (perlinBlend[a] > 1) {
unsigned int temp = perlinBlendTex[a * 2];
perlinBlendTex[a * 2 ] = perlinBlendTex[a * 2 + 1];
perlinBlendTex[a * 2 + 1] = temp;
GenerateNoiseTex(perlinBlendTex[a * 2 + 1]);
perlinBlend[a] -= 1;
}
float tsize = 8.0f / size;
if (a == 0)
glDisable(GL_BLEND);
for (int b = 0; b < 4; ++b)
col[b] = int((1.0f - perlinBlend[a]) * 16 * size);
glBindTexture(GL_TEXTURE_2D, perlinBlendTex[a * 2]);
va->Initialize();
va->AddVertexQTC(ZeroVector, 0, 0, col);
va->AddVertexQTC( UpVector, 0, tsize, col);
va->AddVertexQTC( XYVector, tsize, tsize, col);
va->AddVertexQTC( RgtVector, tsize, 0, col);
va->DrawArrayTC(GL_QUADS);
if (a == 0)
glEnable(GL_BLEND);
for (int b = 0; b < 4; ++b)
col[b] = int(perlinBlend[a] * 16 * size);
glBindTexture(GL_TEXTURE_2D, perlinBlendTex[a * 2 + 1]);
va->Initialize();
va->AddVertexQTC(ZeroVector, 0, 0, col);
va->AddVertexQTC( UpVector, 0, tsize, col);
va->AddVertexQTC( XYVector, tsize, tsize, col);
va->AddVertexQTC( RgtVector, tsize, 0, col);
va->DrawArrayTC(GL_QUADS);
speed *= 0.6f;
size *= 2;
}
perlinFB.Unbind();
glViewport(globalRendering->viewPosX, 0, globalRendering->viewSizeX, globalRendering->viewSizeY);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void CProjectileDrawer::GenerateNoiseTex(unsigned int tex)
{
std::array<unsigned char, 4 * perlinBlendTexSize * perlinBlendTexSize> mem;
for (int a = 0; a < perlinBlendTexSize * perlinBlendTexSize; ++a) {
const unsigned char rnd = int(std::max(0.0f, guRNG.NextFloat() * 555.0f - 300.0f));
mem[a * 4 + 0] = rnd;
mem[a * 4 + 1] = rnd;
mem[a * 4 + 2] = rnd;
mem[a * 4 + 3] = rnd;
}
glBindTexture(GL_TEXTURE_2D, tex);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, perlinBlendTexSize, perlinBlendTexSize, GL_RGBA, GL_UNSIGNED_BYTE, &mem[0]);
}
void CProjectileDrawer::RenderProjectileCreated(const CProjectile* p)
{
if (p->model) {
modelRenderers[MDL_TYPE(p)]->AddProjectile(p);
} else {
renderProjectiles.push_back(const_cast<CProjectile*>(p));
}
}
void CProjectileDrawer::RenderProjectileDestroyed(const CProjectile* const p)
{
if (p->model) {
modelRenderers[MDL_TYPE(p)]->DelProjectile(p);
} else {
auto it = std::find(renderProjectiles.begin(), renderProjectiles.end(), const_cast<CProjectile*>(p));
assert(it != renderProjectiles.end());
renderProjectiles.erase(it);
}
}
|