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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "LegacyMeshDrawer.h"
#include "Game/Camera.h"
#include "Map/SMF/SMFReadMap.h"
#include "Map/SMF/SMFGroundDrawer.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/Env/Particles/ProjectileDrawer.h"
#include "Rendering/GL/myGL.h"
#include "Rendering/GL/VertexArray.h"
#include "System/Config/ConfigHandler.h"
#include "System/FastMath.h"
#include "System/myMath.h"
#include "System/Util.h"
#define CLAMP(i) Clamp((i), 0, smfReadMap->maxHeightMapIdx)
CLegacyMeshDrawer::CLegacyMeshDrawer(CSMFReadMap* rm, CSMFGroundDrawer* gd)
: smfReadMap(rm)
, smfGroundDrawer(gd)
, viewRadius(4)
, neededLod(4)
//, waterDrawn(false)
{
}
CLegacyMeshDrawer::~CLegacyMeshDrawer()
{
}
void CLegacyMeshDrawer::DrawVertexAQ(CVertexArray* ma, int x, int y)
{
// don't send the normals as vertex attributes
// (DLOD'ed triangles mess with interpolation)
// const float3& n = readMap->vertexNormals[(y * smfReadMap->heightMapSizeX) + x];
DrawVertexAQ(ma, x, y, GetVisibleVertexHeight(y * smfReadMap->heightMapSizeX + x));
}
void CLegacyMeshDrawer::DrawVertexAQ(CVertexArray* ma, int x, int y, float height)
{
//if (waterDrawn && height < 0.0f) {
// height *= 2.0f;
//}
ma->AddVertexQ0(x * SQUARE_SIZE, height, y * SQUARE_SIZE);
}
void CLegacyMeshDrawer::DrawGroundVertexArrayQ(CVertexArray*& ma)
{
ma->DrawArray0(GL_TRIANGLE_STRIP);
ma = GetVertexArray();
}
bool CLegacyMeshDrawer::BigTexSquareRowVisible(const CCamera* cam, int bty) const
{
const int minz = bty * smfReadMap->bigTexSize;
const int maxz = minz + smfReadMap->bigTexSize;
const float miny = readMap->GetCurrMinHeight();
const float maxy = math::fabs(cam->GetPos().y);
const float3 mins( 0, miny, minz);
const float3 maxs(smfReadMap->mapSizeX, maxy, maxz);
return (cam->InView(mins, maxs));
}
void CLegacyMeshDrawer::FindRange(const CCamera* cam, int& xs, int& xe, int y, int lod)
{
int xt0, xt1;
const std::vector<CCamera::FrustumLine> negSides = cam->GetNegFrustumSides();
const std::vector<CCamera::FrustumLine> posSides = cam->GetPosFrustumSides();
std::vector<CCamera::FrustumLine>::const_iterator fli;
for (fli = negSides.begin(); fli != negSides.end(); ++fli) {
const float xtf = fli->base + fli->dir * y;
xt0 = (int)xtf;
xt1 = (int)(xtf + fli->dir * lod);
if (xt0 > xt1)
xt0 = xt1;
xt0 = xt0 / lod * lod - lod;
if (xt0 > xs)
xs = xt0;
}
for (fli = posSides.begin(); fli != posSides.end(); ++fli) {
const float xtf = fli->base + fli->dir * y;
xt0 = (int)xtf;
xt1 = (int)(xtf + fli->dir * lod);
if (xt0 < xt1)
xt0 = xt1;
xt0 = xt0 / lod * lod + lod;
if (xt0 < xe)
xe = xt0;
}
}
void CLegacyMeshDrawer::DoDrawGroundRow(const CCamera* cam, int bty)
{
if (!BigTexSquareRowVisible(cam, bty)) {
//! skip this entire row of squares if we can't see it
return;
}
CVertexArray* ma = GetVertexArray();
bool inStrip = false;
float x0, x1;
int x,y;
int sx = 0;
int ex = smfReadMap->numBigTexX;
//! only process the necessary big squares in the x direction
const int bigSquareSizeY = bty * smfReadMap->bigSquareSize;
const std::vector<CCamera::FrustumLine> negSides = cam->GetNegFrustumSides();
const std::vector<CCamera::FrustumLine> posSides = cam->GetPosFrustumSides();
std::vector<CCamera::FrustumLine>::const_iterator fli;
for (fli = negSides.begin(); fli != negSides.end(); ++fli) {
x0 = fli->base + fli->dir * bigSquareSizeY;
x1 = x0 + fli->dir * smfReadMap->bigSquareSize;
if (x0 > x1)
x0 = x1;
x0 /= smfReadMap->bigSquareSize;
if (x0 > sx)
sx = (int) x0;
}
for (fli = posSides.begin(); fli != posSides.end(); ++fli) {
x0 = fli->base + fli->dir * bigSquareSizeY + smfReadMap->bigSquareSize;
x1 = x0 + fli->dir * smfReadMap->bigSquareSize;
if (x0 < x1)
x0 = x1;
x0 /= smfReadMap->bigSquareSize;
if (x0 < ex)
ex = (int) x0;
}
if (sx > ex)
return;
const float cx2 = cam->GetPos().x / SQUARE_SIZE;
const float cy2 = cam->GetPos().z / SQUARE_SIZE;
for (int btx = sx; btx < ex; ++btx) {
ma->Initialize();
for (int lod = 1; lod < neededLod; lod <<= 1) {
float oldcamxpart = 0.0f;
float oldcamypart = 0.0f;
const int hlod = lod >> 1;
const int dlod = lod << 1;
int cx = cx2;
int cy = cy2;
if (lod > 1) {
int cxo = (cx / hlod) * hlod;
int cyo = (cy / hlod) * hlod;
float cx2o = (cxo / lod) * lod;
float cy2o = (cyo / lod) * lod;
oldcamxpart = (cx2 - cx2o) / lod;
oldcamypart = (cy2 - cy2o) / lod;
}
cx = (cx / lod) * lod;
cy = (cy / lod) * lod;
const int ysquaremod = (cy % dlod) / lod;
const int xsquaremod = (cx % dlod) / lod;
const float camxpart = (cx2 - ((cx / dlod) * dlod)) / dlod;
const float camypart = (cy2 - ((cy / dlod) * dlod)) / dlod;
const float mcxp = 1.0f - camxpart, mcyp = 1.0f - camypart;
const float hcxp = 0.5f * camxpart, hcyp = 0.5f * camypart;
const float hmcxp = 0.5f * mcxp, hmcyp = 0.5f * mcyp;
const float mocxp = 1.0f - oldcamxpart, mocyp = 1.0f - oldcamypart;
const float hocxp = 0.5f * oldcamxpart, hocyp = 0.5f * oldcamypart;
const float hmocxp = 0.5f * mocxp, hmocyp = 0.5f * mocyp;
const int minty = bty * smfReadMap->bigSquareSize, maxty = minty + smfReadMap->bigSquareSize;
const int mintx = btx * smfReadMap->bigSquareSize, maxtx = mintx + smfReadMap->bigSquareSize;
const int minly = cy + (-viewRadius + 3 - ysquaremod) * lod;
const int maxly = cy + ( viewRadius - 1 - ysquaremod) * lod;
const int minlx = cx + (-viewRadius + 3 - xsquaremod) * lod;
const int maxlx = cx + ( viewRadius - 1 - xsquaremod) * lod;
const int xstart = std::max(minlx, mintx), xend = std::min(maxlx, maxtx);
const int ystart = std::max(minly, minty), yend = std::min(maxly, maxty);
const int vrhlod = viewRadius * hlod;
for (y = ystart; y < yend; y += lod) {
int xs = xstart;
int xe = xend;
FindRange(cam, /*inout*/ xs, /*inout*/ xe, y, lod);
// If FindRange modifies (xs, xe) to a (less then) empty range,
// continue to the next row.
// If we'd continue, nloop (below) would become negative and we'd
// allocate a vertex array with negative size. (mantis #1415)
if (xe < xs) continue;
int ylod = y + lod;
int yhlod = y + hlod;
int nloop = (xe - xs) / lod + 1;
ma->EnlargeArrays(52 * nloop);
int yhdx = y * smfReadMap->heightMapSizeX;
int ylhdx = yhdx + lod * smfReadMap->heightMapSizeX;
int yhhdx = yhdx + hlod * smfReadMap->heightMapSizeX;
for (x = xs; x < xe; x += lod) {
int xlod = x + lod;
int xhlod = x + hlod;
//! info: all triangle quads start in the top left corner
if ((lod == 1) ||
(x > cx + vrhlod) || (x < cx - vrhlod) ||
(y > cy + vrhlod) || (y < cy - vrhlod)) {
//! normal terrain (all vertices in one LOD)
if (!inStrip) {
DrawVertexAQ(ma, x, y);
DrawVertexAQ(ma, x, ylod);
inStrip = true;
}
DrawVertexAQ(ma, xlod, y);
DrawVertexAQ(ma, xlod, ylod);
} else {
//! border between 2 different LODs
if ((x >= cx + vrhlod)) {
//! lower LOD to the right
int idx1 = CLAMP(yhdx + x), idx1LOD = CLAMP(idx1 + lod), idx1HLOD = CLAMP(idx1 + hlod);
int idx2 = CLAMP(ylhdx + x), idx2LOD = CLAMP(idx2 + lod), idx2HLOD = CLAMP(idx2 + hlod);
int idx3 = CLAMP(yhhdx + x), idx3HLOD = CLAMP(idx3 + hlod);
float h1 = (GetVisibleVertexHeight(idx1) + GetVisibleVertexHeight(idx2)) * hmocxp + GetVisibleVertexHeight(idx3) * oldcamxpart;
float h2 = (GetVisibleVertexHeight(idx1) + GetVisibleVertexHeight(idx1LOD)) * hmocxp + GetVisibleVertexHeight(idx1HLOD) * oldcamxpart;
float h3 = (GetVisibleVertexHeight(idx2) + GetVisibleVertexHeight(idx1LOD)) * hmocxp + GetVisibleVertexHeight(idx3HLOD) * oldcamxpart;
float h4 = (GetVisibleVertexHeight(idx2) + GetVisibleVertexHeight(idx2LOD)) * hmocxp + GetVisibleVertexHeight(idx2HLOD) * oldcamxpart;
if (inStrip) {
ma->EndStrip();
inStrip = false;
}
DrawVertexAQ(ma, x, y);
DrawVertexAQ(ma, x, yhlod, h1);
DrawVertexAQ(ma, xhlod, y, h2);
DrawVertexAQ(ma, xhlod, yhlod, h3);
ma->EndStrip();
DrawVertexAQ(ma, x, yhlod, h1);
DrawVertexAQ(ma, x, ylod);
DrawVertexAQ(ma, xhlod, yhlod, h3);
DrawVertexAQ(ma, xhlod, ylod, h4);
ma->EndStrip();
DrawVertexAQ(ma, xhlod, ylod, h4);
DrawVertexAQ(ma, xlod, ylod);
DrawVertexAQ(ma, xhlod, yhlod, h3);
DrawVertexAQ(ma, xlod, y);
DrawVertexAQ(ma, xhlod, y, h2);
ma->EndStrip();
}
else if ((x <= cx - vrhlod)) {
//! lower LOD to the left
int idx1 = CLAMP(yhdx + x), idx1LOD = CLAMP(idx1 + lod), idx1HLOD = CLAMP(idx1 + hlod);
int idx2 = CLAMP(ylhdx + x), idx2LOD = CLAMP(idx2 + lod), idx2HLOD = CLAMP(idx2 + hlod);
int idx3 = CLAMP(yhhdx + x), idx3LOD = CLAMP(idx3 + lod), idx3HLOD = CLAMP(idx3 + hlod);
float h1 = (GetVisibleVertexHeight(idx1LOD) + GetVisibleVertexHeight(idx2LOD)) * hocxp + GetVisibleVertexHeight(idx3LOD ) * mocxp;
float h2 = (GetVisibleVertexHeight(idx1 ) + GetVisibleVertexHeight(idx1LOD)) * hocxp + GetVisibleVertexHeight(idx1HLOD) * mocxp;
float h3 = (GetVisibleVertexHeight(idx2 ) + GetVisibleVertexHeight(idx1LOD)) * hocxp + GetVisibleVertexHeight(idx3HLOD) * mocxp;
float h4 = (GetVisibleVertexHeight(idx2 ) + GetVisibleVertexHeight(idx2LOD)) * hocxp + GetVisibleVertexHeight(idx2HLOD) * mocxp;
if (inStrip) {
ma->EndStrip();
inStrip = false;
}
DrawVertexAQ(ma, xlod, yhlod, h1);
DrawVertexAQ(ma, xlod, y);
DrawVertexAQ(ma, xhlod, yhlod, h3);
DrawVertexAQ(ma, xhlod, y, h2);
ma->EndStrip();
DrawVertexAQ(ma, xlod, ylod);
DrawVertexAQ(ma, xlod, yhlod, h1);
DrawVertexAQ(ma, xhlod, ylod, h4);
DrawVertexAQ(ma, xhlod, yhlod, h3);
ma->EndStrip();
DrawVertexAQ(ma, xhlod, y, h2);
DrawVertexAQ(ma, x, y);
DrawVertexAQ(ma, xhlod, yhlod, h3);
DrawVertexAQ(ma, x, ylod);
DrawVertexAQ(ma, xhlod, ylod, h4);
ma->EndStrip();
}
if ((y >= cy + vrhlod)) {
//! lower LOD above
int idx1 = yhdx + x, idx1LOD = CLAMP(idx1 + lod), idx1HLOD = CLAMP(idx1 + hlod);
int idx2 = ylhdx + x, idx2LOD = CLAMP(idx2 + lod);
int idx3 = yhhdx + x, idx3LOD = CLAMP(idx3 + lod), idx3HLOD = CLAMP(idx3 + hlod);
float h1 = (GetVisibleVertexHeight(idx1 ) + GetVisibleVertexHeight(idx1LOD)) * hmocyp + GetVisibleVertexHeight(idx1HLOD) * oldcamypart;
float h2 = (GetVisibleVertexHeight(idx1 ) + GetVisibleVertexHeight(idx2 )) * hmocyp + GetVisibleVertexHeight(idx3 ) * oldcamypart;
float h3 = (GetVisibleVertexHeight(idx2 ) + GetVisibleVertexHeight(idx1LOD)) * hmocyp + GetVisibleVertexHeight(idx3HLOD) * oldcamypart;
float h4 = (GetVisibleVertexHeight(idx2LOD) + GetVisibleVertexHeight(idx1LOD)) * hmocyp + GetVisibleVertexHeight(idx3LOD ) * oldcamypart;
if (inStrip) {
ma->EndStrip();
inStrip = false;
}
DrawVertexAQ(ma, x, y);
DrawVertexAQ(ma, x, yhlod, h2);
DrawVertexAQ(ma, xhlod, y, h1);
DrawVertexAQ(ma, xhlod, yhlod, h3);
DrawVertexAQ(ma, xlod, y);
DrawVertexAQ(ma, xlod, yhlod, h4);
ma->EndStrip();
DrawVertexAQ(ma, x, yhlod, h2);
DrawVertexAQ(ma, x, ylod);
DrawVertexAQ(ma, xhlod, yhlod, h3);
DrawVertexAQ(ma, xlod, ylod);
DrawVertexAQ(ma, xlod, yhlod, h4);
ma->EndStrip();
}
else if ((y <= cy - vrhlod)) {
//! lower LOD beneath
int idx1 = CLAMP(yhdx + x), idx1LOD = CLAMP(idx1 + lod);
int idx2 = CLAMP(ylhdx + x), idx2LOD = CLAMP(idx2 + lod), idx2HLOD = CLAMP(idx2 + hlod);
int idx3 = CLAMP(yhhdx + x), idx3LOD = CLAMP(idx3 + lod), idx3HLOD = CLAMP(idx3 + hlod);
float h1 = (GetVisibleVertexHeight(idx2 ) + GetVisibleVertexHeight(idx2LOD)) * hocyp + GetVisibleVertexHeight(idx2HLOD) * mocyp;
float h2 = (GetVisibleVertexHeight(idx1 ) + GetVisibleVertexHeight(idx2 )) * hocyp + GetVisibleVertexHeight(idx3 ) * mocyp;
float h3 = (GetVisibleVertexHeight(idx2 ) + GetVisibleVertexHeight(idx1LOD)) * hocyp + GetVisibleVertexHeight(idx3HLOD) * mocyp;
float h4 = (GetVisibleVertexHeight(idx2LOD) + GetVisibleVertexHeight(idx1LOD)) * hocyp + GetVisibleVertexHeight(idx3LOD ) * mocyp;
if (inStrip) {
ma->EndStrip();
inStrip = false;
}
DrawVertexAQ(ma, x, yhlod, h2);
DrawVertexAQ(ma, x, ylod);
DrawVertexAQ(ma, xhlod, yhlod, h3);
DrawVertexAQ(ma, xhlod, ylod, h1);
DrawVertexAQ(ma, xlod, yhlod, h4);
DrawVertexAQ(ma, xlod, ylod);
ma->EndStrip();
DrawVertexAQ(ma, xlod, yhlod, h4);
DrawVertexAQ(ma, xlod, y);
DrawVertexAQ(ma, xhlod, yhlod, h3);
DrawVertexAQ(ma, x, y);
DrawVertexAQ(ma, x, yhlod, h2);
ma->EndStrip();
}
}
}
if (inStrip) {
ma->EndStrip();
inStrip = false;
}
} //for (y = ystart; y < yend; y += lod)
const int yst = std::max(ystart - lod, minty);
const int yed = std::min(yend + lod, maxty);
int nloop = (yed - yst) / lod + 1;
if (nloop > 0)
ma->EnlargeArrays(8 * nloop);
//! rita yttre begr?snings yta mot n?ta lod
if (maxlx < maxtx && maxlx >= mintx) {
x = maxlx;
int xlod = x + lod;
for (y = yst; y < yed; y += lod) {
DrawVertexAQ(ma, x, y);
DrawVertexAQ(ma, x, y + lod);
if (y % dlod) {
const int idx1 = CLAMP((y ) * smfReadMap->heightMapSizeX + x), idx1LOD = CLAMP(idx1 + lod);
const int idx2 = CLAMP((y + lod) * smfReadMap->heightMapSizeX + x), idx2LOD = CLAMP(idx2 + lod);
const int idx3 = CLAMP((y - lod) * smfReadMap->heightMapSizeX + x), idx3LOD = CLAMP(idx3 + lod);
const float h = (GetVisibleVertexHeight(idx3LOD) + GetVisibleVertexHeight(idx2LOD)) * hmcxp + GetVisibleVertexHeight(idx1LOD) * camxpart;
DrawVertexAQ(ma, xlod, y, h);
DrawVertexAQ(ma, xlod, y + lod);
} else {
const int idx1 = CLAMP((y ) * smfReadMap->heightMapSizeX + x), idx1LOD = CLAMP(idx1 + lod);
const int idx2 = CLAMP((y + lod) * smfReadMap->heightMapSizeX + x), idx2LOD = CLAMP(idx2 + lod);
const int idx3 = CLAMP((y + dlod) * smfReadMap->heightMapSizeX + x), idx3LOD = CLAMP(idx3 + lod);
const float h = (GetVisibleVertexHeight(idx1LOD) + GetVisibleVertexHeight(idx3LOD)) * hmcxp + GetVisibleVertexHeight(idx2LOD) * camxpart;
DrawVertexAQ(ma, xlod, y);
DrawVertexAQ(ma, xlod, y + lod, h);
}
ma->EndStrip();
}
}
if (minlx > mintx && minlx < maxtx) {
x = minlx - lod;
int xlod = x + lod;
for (y = yst; y < yed; y += lod) {
if (y % dlod) {
int idx1 = CLAMP((y ) * smfReadMap->heightMapSizeX + x);
int idx2 = CLAMP((y + lod) * smfReadMap->heightMapSizeX + x);
int idx3 = CLAMP((y - lod) * smfReadMap->heightMapSizeX + x);
float h = (GetVisibleVertexHeight(idx3) + GetVisibleVertexHeight(idx2)) * hcxp + GetVisibleVertexHeight(idx1) * mcxp;
DrawVertexAQ(ma, x, y, h);
DrawVertexAQ(ma, x, y + lod);
} else {
int idx1 = CLAMP((y ) * smfReadMap->heightMapSizeX + x);
int idx2 = CLAMP((y + lod) * smfReadMap->heightMapSizeX + x);
int idx3 = CLAMP((y + dlod) * smfReadMap->heightMapSizeX + x);
float h = (GetVisibleVertexHeight(idx1) + GetVisibleVertexHeight(idx3)) * hcxp + GetVisibleVertexHeight(idx2) * mcxp;
DrawVertexAQ(ma, x, y);
DrawVertexAQ(ma, x, y + lod, h);
}
DrawVertexAQ(ma, xlod, y);
DrawVertexAQ(ma, xlod, y + lod);
ma->EndStrip();
}
}
if (maxly < maxty && maxly > minty) {
y = maxly;
int xs = std::max(xstart - lod, mintx);
int xe = std::min(xend + lod, maxtx);
FindRange(cam, xs, xe, y, lod);
if (xs < xe) {
x = xs;
int ylod = y + lod;
int nloop = (xe - xs) / lod + 2; //! one extra for if statment
int ylhdx = (y + lod) * smfReadMap->heightMapSizeX;
ma->EnlargeArrays(2 * nloop);
if (x % dlod) {
int idx2 = CLAMP(ylhdx + x), idx2PLOD = CLAMP(idx2 + lod), idx2MLOD = CLAMP(idx2 - lod);
float h = (GetVisibleVertexHeight(idx2MLOD) + GetVisibleVertexHeight(idx2PLOD)) * hmcyp + GetVisibleVertexHeight(idx2) * camypart;
DrawVertexAQ(ma, x, y);
DrawVertexAQ(ma, x, ylod, h);
} else {
DrawVertexAQ(ma, x, y);
DrawVertexAQ(ma, x, ylod);
}
for (x = xs; x < xe; x += lod) {
if (x % dlod) {
DrawVertexAQ(ma, x + lod, y);
DrawVertexAQ(ma, x + lod, ylod);
} else {
int idx2 = CLAMP(ylhdx + x), idx2PLOD = CLAMP(idx2 + lod), idx2PLOD2 = CLAMP(idx2 + dlod);
float h = (GetVisibleVertexHeight(idx2PLOD2) + GetVisibleVertexHeight(idx2)) * hmcyp + GetVisibleVertexHeight(idx2PLOD) * camypart;
DrawVertexAQ(ma, x + lod, y);
DrawVertexAQ(ma, x + lod, ylod, h);
}
}
ma->EndStrip();
}
}
if (minly > minty && minly < maxty) {
y = minly - lod;
int xs = std::max(xstart - lod, mintx);
int xe = std::min(xend + lod, maxtx);
FindRange(cam, xs, xe, y, lod);
if (xs < xe) {
x = xs;
int ylod = y + lod;
int yhdx = y * smfReadMap->heightMapSizeX;
int nloop = (xe - xs) / lod + 2; //! one extra for if statment
ma->EnlargeArrays(2 * nloop);
if (x % dlod) {
int idx1 = CLAMP(yhdx + x), idx1PLOD = CLAMP(idx1 + lod), idx1MLOD = CLAMP(idx1 - lod);
float h = (GetVisibleVertexHeight(idx1MLOD) + GetVisibleVertexHeight(idx1PLOD)) * hcyp + GetVisibleVertexHeight(idx1) * mcyp;
DrawVertexAQ(ma, x, y, h);
DrawVertexAQ(ma, x, ylod);
} else {
DrawVertexAQ(ma, x, y);
DrawVertexAQ(ma, x, ylod);
}
for (x = xs; x < xe; x+= lod) {
if (x % dlod) {
DrawVertexAQ(ma, x + lod, y);
DrawVertexAQ(ma, x + lod, ylod);
} else {
int idx1 = CLAMP(yhdx + x), idx1PLOD = CLAMP(idx1 + lod), idx1PLOD2 = CLAMP(idx1 + dlod);
float h = (GetVisibleVertexHeight(idx1PLOD2) + GetVisibleVertexHeight(idx1)) * hcyp + GetVisibleVertexHeight(idx1PLOD) * mcyp;
DrawVertexAQ(ma, x + lod, y, h);
DrawVertexAQ(ma, x + lod, ylod);
}
}
ma->EndStrip();
}
}
} //for (int lod = 1; lod < neededLod; lod <<= 1)
smfGroundDrawer->SetupBigSquare(btx, bty);
DrawGroundVertexArrayQ(ma);
}
}
void CLegacyMeshDrawer::UpdateLODParams(const DrawPass::e& drawPass)
{
// Get current ground detail (draw pass dependent)
viewRadius = smfGroundDrawer->GetGroundDetail(drawPass);
// Take FOV into account
viewRadius = int(viewRadius * fastmath::apxsqrt(45.0f / camera->GetVFOV()));
// Clamp it to mapsize dependent minimum, else we get holes in the terrain
viewRadius = std::max(std::max(smfReadMap->numBigTexY, smfReadMap->numBigTexX) + 1, viewRadius);
// we need a multiple of 2
viewRadius += (viewRadius & 1);
// Compute count of LODs needed/visible
neededLod = std::max(1, int((globalRendering->viewRange * 0.125f) / viewRadius) << 1);
neededLod = std::min(neededLod, std::min(mapDims.mapx, mapDims.mapy));
}
void CLegacyMeshDrawer::DrawMesh(const DrawPass::e& drawPass)
{
if (drawPass == DrawPass::Shadow) {
DrawShadowMesh();
return;
}
UpdateLODParams(drawPass);
CCamera* cam = CCamera::GetActiveCamera();
cam->GetFrustumSides(readMap->GetCurrMinHeight() - 100.0f, readMap->GetCurrMaxHeight() + 100.0f, SQUARE_SIZE);
const int camBigTexY = Clamp(int(cam->GetPos().z / (smfReadMap->bigSquareSize * SQUARE_SIZE)), 0, smfReadMap->numBigTexY - 1);
// try to render "front to back" (so start with the bigtex rows closest to camera)
for (int bty = camBigTexY; bty >= 0; --bty) {
DoDrawGroundRow(cam, bty);
}
for (int bty = camBigTexY + 1; bty < smfReadMap->numBigTexY; ++bty) {
DoDrawGroundRow(cam, bty);
}
}
void CLegacyMeshDrawer::DoDrawGroundShadowLOD(int nlod) {
CVertexArray* ma = GetVertexArray();
ma->Initialize();
bool inStrip = false;
int x,y;
int lod = 1 << nlod;
float cx2 = camera->GetPos().x / SQUARE_SIZE;
float cy2 = camera->GetPos().z / SQUARE_SIZE;
float oldcamxpart = 0.0f;
float oldcamypart = 0.0f;
int hlod = lod >> 1;
int dlod = lod << 1;
int cx = (int)cx2;
int cy = (int)cy2;
if (lod > 1) {
int cxo = (cx / hlod) * hlod;
int cyo = (cy / hlod) * hlod;
float cx2o = (cxo / lod) * lod;
float cy2o = (cyo / lod) * lod;
oldcamxpart = (cx2 - cx2o) / lod;
oldcamypart = (cy2 - cy2o) / lod;
}
cx = (cx / lod) * lod;
cy = (cy / lod) * lod;
const int ysquaremod = (cy % dlod) / lod;
const int xsquaremod = (cx % dlod) / lod;
const float camxpart = (cx2 - (cx / dlod) * dlod) / dlod;
const float camypart = (cy2 - (cy / dlod) * dlod) / dlod;
const int minty = 0, maxty = mapDims.mapy;
const int mintx = 0, maxtx = mapDims.mapx;
const int minly = cy + (-viewRadius + 3 - ysquaremod) * lod, maxly = cy + ( viewRadius - 1 - ysquaremod) * lod;
const int minlx = cx + (-viewRadius + 3 - xsquaremod) * lod, maxlx = cx + ( viewRadius - 1 - xsquaremod) * lod;
const int xstart = std::max(minlx, mintx), xend = std::min(maxlx, maxtx);
const int ystart = std::max(minly, minty), yend = std::min(maxly, maxty);
const int lhdx = lod * smfReadMap->heightMapSizeX;
const int hhdx = hlod * smfReadMap->heightMapSizeX;
const int dhdx = dlod * smfReadMap->heightMapSizeX;
const float mcxp = 1.0f - camxpart, mcyp = 1.0f - camypart;
const float hcxp = 0.5f * camxpart, hcyp = 0.5f * camypart;
const float hmcxp = 0.5f * mcxp, hmcyp = 0.5f * mcyp;
const float mocxp = 1.0f - oldcamxpart, mocyp = 1.0f - oldcamypart;
const float hocxp = 0.5f * oldcamxpart, hocyp = 0.5f * oldcamypart;
const float hmocxp = 0.5f * mocxp, hmocyp = 0.5f * mocyp;
const int vrhlod = viewRadius * hlod;
for (y = ystart; y < yend; y += lod) {
int xs = xstart;
int xe = xend;
if (xe < xs) continue;
int ylod = y + lod;
int yhlod = y + hlod;
int ydx = y * smfReadMap->heightMapSizeX;
int nloop = (xe - xs) / lod + 1;
ma->EnlargeArrays(52 * nloop);
for (x = xs; x < xe; x += lod) {
int xlod = x + lod;
int xhlod = x + hlod;
if ((lod == 1) ||
(x > cx + vrhlod) || (x < cx - vrhlod) ||
(y > cy + vrhlod) || (y < cy - vrhlod)) {
if (!inStrip) {
DrawVertexAQ(ma, x, y );
DrawVertexAQ(ma, x, ylod);
inStrip = true;
}
DrawVertexAQ(ma, xlod, y );
DrawVertexAQ(ma, xlod, ylod);
}
else { //! inre begr?sning mot f?eg?nde lod
int yhdx=ydx+x;
int ylhdx=yhdx+lhdx;
int yhhdx=yhdx+hhdx;
if ( x>= cx + vrhlod) {
const float h1 = (GetVisibleVertexHeight(yhdx ) + GetVisibleVertexHeight(ylhdx )) * hmocxp + GetVisibleVertexHeight(yhhdx ) * oldcamxpart;
const float h2 = (GetVisibleVertexHeight(yhdx ) + GetVisibleVertexHeight(yhdx+lod )) * hmocxp + GetVisibleVertexHeight(yhdx+hlod ) * oldcamxpart;
const float h3 = (GetVisibleVertexHeight(ylhdx) + GetVisibleVertexHeight(yhdx+lod )) * hmocxp + GetVisibleVertexHeight(yhhdx+hlod) * oldcamxpart;
const float h4 = (GetVisibleVertexHeight(ylhdx) + GetVisibleVertexHeight(ylhdx+lod)) * hmocxp + GetVisibleVertexHeight(ylhdx+hlod) * oldcamxpart;
if(inStrip){
ma->EndStrip();
inStrip=false;
}
DrawVertexAQ(ma, x,y);
DrawVertexAQ(ma, x,yhlod,h1);
DrawVertexAQ(ma, xhlod,y,h2);
DrawVertexAQ(ma, xhlod,yhlod,h3);
ma->EndStrip();
DrawVertexAQ(ma, x,yhlod,h1);
DrawVertexAQ(ma, x,ylod);
DrawVertexAQ(ma, xhlod,yhlod,h3);
DrawVertexAQ(ma, xhlod,ylod,h4);
ma->EndStrip();
DrawVertexAQ(ma, xhlod,ylod,h4);
DrawVertexAQ(ma, xlod,ylod);
DrawVertexAQ(ma, xhlod,yhlod,h3);
DrawVertexAQ(ma, xlod,y);
DrawVertexAQ(ma, xhlod,y,h2);
ma->EndStrip();
}
if (x <= cx - vrhlod) {
const float h1 = (GetVisibleVertexHeight(yhdx+lod) + GetVisibleVertexHeight(ylhdx+lod)) * hocxp + GetVisibleVertexHeight(yhhdx+lod ) * mocxp;
const float h2 = (GetVisibleVertexHeight(yhdx ) + GetVisibleVertexHeight(yhdx+lod )) * hocxp + GetVisibleVertexHeight(yhdx+hlod ) * mocxp;
const float h3 = (GetVisibleVertexHeight(ylhdx ) + GetVisibleVertexHeight(yhdx+lod )) * hocxp + GetVisibleVertexHeight(yhhdx+hlod) * mocxp;
const float h4 = (GetVisibleVertexHeight(ylhdx ) + GetVisibleVertexHeight(ylhdx+lod)) * hocxp + GetVisibleVertexHeight(ylhdx+hlod) * mocxp;
if(inStrip){
ma->EndStrip();
inStrip=false;
}
DrawVertexAQ(ma, xlod,yhlod,h1);
DrawVertexAQ(ma, xlod,y);
DrawVertexAQ(ma, xhlod,yhlod,h3);
DrawVertexAQ(ma, xhlod,y,h2);
ma->EndStrip();
DrawVertexAQ(ma, xlod,ylod);
DrawVertexAQ(ma, xlod,yhlod,h1);
DrawVertexAQ(ma, xhlod,ylod,h4);
DrawVertexAQ(ma, xhlod,yhlod,h3);
ma->EndStrip();
DrawVertexAQ(ma, xhlod,y,h2);
DrawVertexAQ(ma, x,y);
DrawVertexAQ(ma, xhlod,yhlod,h3);
DrawVertexAQ(ma, x,ylod);
DrawVertexAQ(ma, xhlod,ylod,h4);
ma->EndStrip();
}
if (y >= cy + vrhlod) {
const float h1 = (GetVisibleVertexHeight(yhdx ) + GetVisibleVertexHeight(yhdx+lod)) * hmocyp + GetVisibleVertexHeight(yhdx+hlod ) * oldcamypart;
const float h2 = (GetVisibleVertexHeight(yhdx ) + GetVisibleVertexHeight(ylhdx )) * hmocyp + GetVisibleVertexHeight(yhhdx ) * oldcamypart;
const float h3 = (GetVisibleVertexHeight(ylhdx ) + GetVisibleVertexHeight(yhdx+lod)) * hmocyp + GetVisibleVertexHeight(yhhdx+hlod) * oldcamypart;
const float h4 = (GetVisibleVertexHeight(ylhdx+lod) + GetVisibleVertexHeight(yhdx+lod)) * hmocyp + GetVisibleVertexHeight(yhhdx+lod ) * oldcamypart;
if(inStrip){
ma->EndStrip();
inStrip=false;
}
DrawVertexAQ(ma, x,y);
DrawVertexAQ(ma, x,yhlod,h2);
DrawVertexAQ(ma, xhlod,y,h1);
DrawVertexAQ(ma, xhlod,yhlod,h3);
DrawVertexAQ(ma, xlod,y);
DrawVertexAQ(ma, xlod,yhlod,h4);
ma->EndStrip();
DrawVertexAQ(ma, x,yhlod,h2);
DrawVertexAQ(ma, x,ylod);
DrawVertexAQ(ma, xhlod,yhlod,h3);
DrawVertexAQ(ma, xlod,ylod);
DrawVertexAQ(ma, xlod,yhlod,h4);
ma->EndStrip();
}
if (y <= cy - vrhlod) {
const float h1 = (GetVisibleVertexHeight(ylhdx ) + GetVisibleVertexHeight(ylhdx+lod)) * hocyp + GetVisibleVertexHeight(ylhdx+hlod) * mocyp;
const float h2 = (GetVisibleVertexHeight(yhdx ) + GetVisibleVertexHeight(ylhdx )) * hocyp + GetVisibleVertexHeight(yhhdx ) * mocyp;
const float h3 = (GetVisibleVertexHeight(ylhdx ) + GetVisibleVertexHeight(yhdx+lod )) * hocyp + GetVisibleVertexHeight(yhhdx+hlod) * mocyp;
const float h4 = (GetVisibleVertexHeight(ylhdx+lod) + GetVisibleVertexHeight(yhdx+lod )) * hocyp + GetVisibleVertexHeight(yhhdx+lod ) * mocyp;
if (inStrip) {
ma->EndStrip();
inStrip = false;
}
DrawVertexAQ(ma, x,yhlod,h2);
DrawVertexAQ(ma, x,ylod);
DrawVertexAQ(ma, xhlod,yhlod,h3);
DrawVertexAQ(ma, xhlod,ylod,h1);
DrawVertexAQ(ma, xlod,yhlod,h4);
DrawVertexAQ(ma, xlod,ylod);
ma->EndStrip();
DrawVertexAQ(ma, xlod,yhlod,h4);
DrawVertexAQ(ma, xlod,y);
DrawVertexAQ(ma, xhlod,yhlod,h3);
DrawVertexAQ(ma, x,y);
DrawVertexAQ(ma, x,yhlod,h2);
ma->EndStrip();
}
}
}
if (inStrip) {
ma->EndStrip();
inStrip=false;
}
}
int yst = std::max(ystart - lod, minty);
int yed = std::min(yend + lod, maxty);
int nloop = (yed - yst) / lod + 1;
ma->EnlargeArrays(8 * nloop);
//!rita yttre begr?snings yta mot n?ta lod
if (maxlx < maxtx && maxlx >= mintx) {
x = maxlx;
const int xlod = x + lod;
for (y = yst; y < yed; y += lod) {
DrawVertexAQ(ma, x, y );
DrawVertexAQ(ma, x, y + lod);
const int yhdx = y * smfReadMap->heightMapSizeX + x;
if (y % dlod) {
const float h = (GetVisibleVertexHeight(yhdx - lhdx + lod) + GetVisibleVertexHeight(yhdx + lhdx + lod)) * hmcxp + GetVisibleVertexHeight(yhdx+lod) * camxpart;
DrawVertexAQ(ma, xlod, y, h);
DrawVertexAQ(ma, xlod, y + lod);
} else {
const float h = (GetVisibleVertexHeight(yhdx+lod) + GetVisibleVertexHeight(yhdx+dhdx+lod)) * hmcxp + GetVisibleVertexHeight(yhdx+lhdx+lod) * camxpart;
DrawVertexAQ(ma, xlod,y);
DrawVertexAQ(ma, xlod,y+lod,h);
}
ma->EndStrip();
}
}
if (minlx > mintx && minlx < maxtx) {
x = minlx - lod;
const int xlod = x + lod;
for(y = yst; y < yed; y += lod) {
int yhdx = y * smfReadMap->heightMapSizeX + x;
if(y%dlod){
const float h = (GetVisibleVertexHeight(yhdx-lhdx) + GetVisibleVertexHeight(yhdx+lhdx)) * hcxp + GetVisibleVertexHeight(yhdx) * mcxp;
DrawVertexAQ(ma, x,y,h);
DrawVertexAQ(ma, x,y+lod);
} else {
const float h = (GetVisibleVertexHeight(yhdx) + GetVisibleVertexHeight(yhdx+dhdx)) * hcxp + GetVisibleVertexHeight(yhdx+lhdx) * mcxp;
DrawVertexAQ(ma, x,y);
DrawVertexAQ(ma, x,y+lod,h);
}
DrawVertexAQ(ma, xlod,y);
DrawVertexAQ(ma, xlod,y+lod);
ma->EndStrip();
}
}
if (maxly < maxty && maxly > minty) {
y = maxly;
const int xs = std::max(xstart -lod, mintx);
const int xe = std::min(xend + lod, maxtx);
if (xs < xe) {
x = xs;
const int ylod = y + lod;
const int ydx = y * smfReadMap->heightMapSizeX;
const int nloop = (xe - xs) / lod + 2; //! two extra for if statment
ma->EnlargeArrays(2 * nloop);
if (x % dlod) {
const int ylhdx = ydx + x + lhdx;
const float h = (GetVisibleVertexHeight(ylhdx-lod) + GetVisibleVertexHeight(ylhdx+lod)) * hmcyp + GetVisibleVertexHeight(ylhdx) * camypart;
DrawVertexAQ(ma, x, y);
DrawVertexAQ(ma, x, ylod, h);
} else {
DrawVertexAQ(ma, x, y);
DrawVertexAQ(ma, x, ylod);
}
for (x = xs; x < xe; x += lod) {
if (x % dlod) {
DrawVertexAQ(ma, x + lod, y);
DrawVertexAQ(ma, x + lod, ylod);
} else {
DrawVertexAQ(ma, x+lod,y);
const int ylhdx = ydx + x + lhdx;
const float h = (GetVisibleVertexHeight(ylhdx+dlod) + GetVisibleVertexHeight(ylhdx)) * hmcyp + GetVisibleVertexHeight(ylhdx+lod) * camypart;
DrawVertexAQ(ma, x+lod,ylod,h);
}
}
ma->EndStrip();
}
}
if (minly > minty && minly < maxty) {
y = minly - lod;
const int xs = std::max(xstart - lod, mintx);
const int xe = std::min(xend + lod, maxtx);
if (xs < xe) {
x = xs;
const int ylod = y + lod;
const int ydx = y * smfReadMap->heightMapSizeX;
const int nloop = (xe - xs) / lod + 2; //! two extra for if statment
ma->EnlargeArrays(2 * nloop);
if (x % dlod) {
const int yhdx = ydx + x;
const float h = (GetVisibleVertexHeight(yhdx-lod) + GetVisibleVertexHeight(yhdx + lod)) * hcyp + GetVisibleVertexHeight(yhdx) * mcyp;
DrawVertexAQ(ma, x, y, h);
DrawVertexAQ(ma, x, ylod);
} else {
DrawVertexAQ(ma, x, y);
DrawVertexAQ(ma, x, ylod);
}
for (x = xs; x < xe; x += lod) {
if (x % dlod) {
DrawVertexAQ(ma, x + lod, y);
DrawVertexAQ(ma, x + lod, ylod);
} else {
const int yhdx = ydx + x;
const float h = (GetVisibleVertexHeight(yhdx+dlod) + GetVisibleVertexHeight(yhdx)) * hcyp + GetVisibleVertexHeight(yhdx+lod) * mcyp;
DrawVertexAQ(ma, x + lod, y, h);
DrawVertexAQ(ma, x + lod, ylod);
}
}
ma->EndStrip();
}
}
DrawGroundVertexArrayQ(ma);
}
void CLegacyMeshDrawer::DrawShadowMesh()
{
{ // profiler scope
const int NUM_LODS = 4;
{
for (int nlod = 0; nlod < NUM_LODS + 1; ++nlod) {
DoDrawGroundShadowLOD(nlod);
}
}
}
}
|