Ticket #2431: patch_optimization.patch
File patch_optimization.patch, 9.9 KB (added by , 10 years ago) |
---|
-
source/graphics/Frustum.cpp
185 185 return true; 186 186 } 187 187 188 bool CFrustum::operator==(const CFrustum& other) const 189 { 190 for(ssize_t i = 0; i < MAX_NUM_FRUSTUM_PLANES; i++) 191 { 192 if(m_aPlanes[i] != other.m_aPlanes[i]) 193 return false; 194 } 195 return true; 196 } 188 197 198 bool CFrustum::operator!=(const CFrustum& other) const 199 { 200 for(ssize_t i = 0; i < MAX_NUM_FRUSTUM_PLANES; i++) 201 { 202 if(m_aPlanes[i] != other.m_aPlanes[i]) 203 return true; 204 } 205 return false; 206 } 207 208 -
source/graphics/Frustum.h
60 60 CPlane& operator[](size_t idx) { return m_aPlanes[idx]; } 61 61 const CPlane& operator[](size_t idx) const { return m_aPlanes[idx]; } 62 62 63 bool operator==(const CFrustum& other) const; 64 bool operator!=(const CFrustum& other) const; 63 65 public: 64 66 //make the planes public for ease of use 65 67 CPlane m_aPlanes[MAX_NUM_FRUSTUM_PLANES]; -
source/graphics/GameView.cpp
15 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 16 */ 17 17 18 #include <algorithm> 19 #include <vector> 20 #include <iterator> 21 18 22 #include "precompiled.h" 19 23 20 24 #include "GameView.h" … … 351 355 orientation->Translate(m->PosX.GetValue(), m->PosY.GetValue(), m->PosZ.GetValue()); 352 356 } 353 357 358 //only internal use for std::generate 359 class CoordsGenerator 360 { 361 public: 362 CoordsGenerator(const ssize_t& num_patches); 363 ~CoordsGenerator(){} 364 365 std::pair<ssize_t,ssize_t> operator()(); 366 private: 367 ssize_t x_; 368 ssize_t y_; 369 const ssize_t& num_patches_; 370 }; 371 372 CoordsGenerator::CoordsGenerator(const ssize_t& num_patches): 373 num_patches_(num_patches),x_(0),y_(0) 374 { 375 } 376 377 std::pair<ssize_t,ssize_t> CoordsGenerator::operator()() 378 { 379 std::pair<ssize_t,ssize_t> res; 380 res.first = x_; 381 res.second = y_; 382 383 x_++; 384 if(x_ >= num_patches_) 385 { 386 x_ = 0; 387 y_++; 388 } 389 return res; 390 } 391 354 392 CGameView::CGameView(CGame *pGame): 355 393 m(new CGameViewImpl(pGame)) 356 394 { … … 501 539 /////////////////////////////////////////////////////////// 502 540 // This callback is part of the Scene interface 503 541 // Submit all objects visible in the given frustum 542 504 543 void CGameView::EnumerateObjects(const CFrustum& frustum, SceneCollector* c) 505 544 { 506 545 { 507 546 PROFILE3("submit terrain"); 508 547 509 CTerrain* pTerrain = m->Game->GetWorld()->GetTerrain(); 510 const ssize_t patchesPerSide = pTerrain->GetPatchesPerSide(); 548 static CTerrain* pTerrain = 0; 511 549 512 // find out which patches will be drawn 513 for (ssize_t j=0; j<patchesPerSide; j++) { 514 for (ssize_t i=0; i<patchesPerSide; i++) { 515 CPatch* patch=pTerrain->GetPatch(i,j); // can't fail 550 float waterHeight = 0.0f; 551 CPatch* patch = 0; 552 const CVector3D zero = CVector3D(0,0,0); 516 553 517 // If the patch is underwater, calculate a bounding box that also contains the water plane 554 static std::vector<std::pair<ssize_t,ssize_t> > coords; 555 556 //checking if terrain pointer needs to get updated 557 //if so, update also the patch coordinates 558 if(pTerrain != m->Game->GetWorld()->GetTerrain()) 559 { 560 pTerrain = m->Game->GetWorld()->GetTerrain(); 561 const ssize_t patchesPerSide = pTerrain->GetPatchesPerSide(); 562 coords.clear(); 563 coords.resize(patchesPerSide*patchesPerSide); 564 CoordsGenerator gen(patchesPerSide); 565 std::generate(coords.begin(),coords.end(),gen); 566 } 567 568 static std::vector<CPatch*> patches_to_draw; 569 static CFrustum lastView = CFrustum(); 570 std::vector<CPatch*>::iterator patch_iterator; 571 std::vector<std::pair<ssize_t,ssize_t> >::iterator coord; 572 ssize_t x; 573 ssize_t y; 574 575 //checking if current view is the same than last frame 576 //if not, update the patches to draw 577 if(lastView != frustum) 578 { 579 lastView = frustum; 580 std::vector<CPatch*>::iterator it; 581 582 //deactivating last patches 583 while(patches_to_draw.size() != 0) 584 { 585 it = patches_to_draw.end() - 1; 586 if((*it) != NULL) 587 (*it)->setDrawState(false); 588 patches_to_draw.pop_back(); 589 } 590 patches_to_draw.reserve(pTerrain->GetPatchesPerSide()*pTerrain->GetPatchesPerSide()); 591 592 //deciding which patches to draw 593 for(coord = coords.begin(); coord < coords.end(); coord++) 594 { 595 x = coord->first; 596 y = coord->second; 597 598 patch = pTerrain->GetPatch(x,y); 518 599 CBoundingBoxAligned bounds = patch->GetWorldBounds(); 519 float waterHeight = g_Renderer.GetWaterManager()->m_WaterHeight + 0.001f; 520 if(bounds[1].Y < waterHeight) { 521 bounds[1].Y = waterHeight; 522 } 600 waterHeight = g_Renderer.GetWaterManager()->m_WaterHeight + 0.001f; 601 bounds[1].Y = waterHeight*(bounds[1].Y >= waterHeight) + bounds[1].Y*(bounds[1].Y < waterHeight); 523 602 524 if (!m->Culling || frustum.IsBoxVisible (CVector3D(0,0,0), bounds)) { 525 //c->Submit(patch); 603 if (!m->Culling || frustum.IsBoxVisible (zero, bounds)) { 526 604 527 605 // set the renderstate for this patch 528 patch ->setDrawState(true);606 patches_to_draw.push_back(patch); 529 607 530 608 // set the renderstate for the neighbors 531 609 CPatch *nPatch; 532 610 533 nPatch = pTerrain->GetPatch(i-1,j-1); 534 if(nPatch) nPatch->setDrawState(true); 611 nPatch = pTerrain->GetPatch(x-1,y-1); 612 patch_iterator = std::find(patches_to_draw.begin(),patches_to_draw.end(),nPatch); 613 if(nPatch && patch_iterator == patches_to_draw.end()) 614 { 615 patches_to_draw.push_back(nPatch); 616 nPatch->setDrawState(true); 617 } 535 618 536 nPatch = pTerrain->GetPatch(i,j-1); 537 if(nPatch) nPatch->setDrawState(true); 619 nPatch = pTerrain->GetPatch(x,y-1); 620 patch_iterator = std::find(patches_to_draw.begin(),patches_to_draw.end(),nPatch); 621 if(nPatch && patch_iterator == patches_to_draw.end()) 622 { 623 patches_to_draw.push_back(nPatch); 624 nPatch->setDrawState(true); 625 } 538 626 539 nPatch = pTerrain->GetPatch(i+1,j-1); 540 if(nPatch) nPatch->setDrawState(true); 627 nPatch = pTerrain->GetPatch(x+1,y-1); 628 patch_iterator = std::find(patches_to_draw.begin(),patches_to_draw.end(),nPatch); 629 if(nPatch && patch_iterator == patches_to_draw.end()) 630 { 631 patches_to_draw.push_back(nPatch); 632 nPatch->setDrawState(true); 633 } 541 634 542 nPatch = pTerrain->GetPatch(i-1,j); 543 if(nPatch) nPatch->setDrawState(true); 635 nPatch = pTerrain->GetPatch(x-1,y); 636 patch_iterator = std::find(patches_to_draw.begin(),patches_to_draw.end(),nPatch); 637 if(nPatch && patch_iterator == patches_to_draw.end()) 638 { 639 patches_to_draw.push_back(nPatch); 640 nPatch->setDrawState(true); 641 } 544 642 545 nPatch = pTerrain->GetPatch(i+1,j); 546 if(nPatch) nPatch->setDrawState(true); 643 nPatch = pTerrain->GetPatch(x+1,y); 644 patch_iterator = std::find(patches_to_draw.begin(),patches_to_draw.end(),nPatch); 645 if(nPatch && patch_iterator == patches_to_draw.end()) 646 { 647 patches_to_draw.push_back(nPatch); 648 nPatch->setDrawState(true); 649 } 547 650 548 nPatch = pTerrain->GetPatch(i-1,j+1); 549 if(nPatch) nPatch->setDrawState(true); 651 nPatch = pTerrain->GetPatch(x-1,y+1); 652 patch_iterator = std::find(patches_to_draw.begin(),patches_to_draw.end(),nPatch); 653 if(nPatch && patch_iterator == patches_to_draw.end()) 654 { 655 patches_to_draw.push_back(nPatch); 656 nPatch->setDrawState(true); 657 } 550 658 551 nPatch = pTerrain->GetPatch(i,j+1); 552 if(nPatch) nPatch->setDrawState(true); 659 nPatch = pTerrain->GetPatch(x,y+1); 660 patch_iterator = std::find(patches_to_draw.begin(),patches_to_draw.end(),nPatch); 661 if(nPatch && patch_iterator == patches_to_draw.end()) 662 { 663 patches_to_draw.push_back(nPatch); 664 nPatch->setDrawState(true); 665 } 553 666 554 nPatch = pTerrain->GetPatch(i+1,j+1); 555 if(nPatch) nPatch->setDrawState(true); 667 nPatch = pTerrain->GetPatch(x+1,y+1); 668 patch_iterator = std::find(patches_to_draw.begin(),patches_to_draw.end(),nPatch); 669 if(nPatch && patch_iterator == patches_to_draw.end()) 670 { 671 patches_to_draw.push_back(nPatch); 672 nPatch->setDrawState(true); 673 } 556 674 } 557 675 } 558 676 } 677 678 //submit the patches that are activated 679 for(patch_iterator = patches_to_draw.begin();patch_iterator < patches_to_draw.end();patch_iterator++) 680 c->Submit(*patch_iterator); 559 681 560 // draw the patches 561 for (ssize_t j=0; j<patchesPerSide; j++) 562 { 563 for (ssize_t i=0; i<patchesPerSide; i++) 564 { 565 CPatch* patch=pTerrain->GetPatch(i,j); // can't fail 566 if(patch->getDrawState() == true) 567 { 568 c->Submit(patch); 569 patch->setDrawState(false); 570 } 571 } 682 m->Game->GetSimulation2()->RenderSubmit(*c, frustum, m->Culling); 572 683 } 573 }574 575 m->Game->GetSimulation2()->RenderSubmit(*c, frustum, m->Culling);576 684 } 577 685 578 686 … … 1164 1272 } 1165 1273 1166 1274 return IN_PASS; 1167 } 1275 } 1276 No newline at end of file -
source/maths/Plane.cpp
133 133 *intsect = start - (direction * (DistanceToPlane (start)/dot)); 134 134 return true; 135 135 } 136 137 bool CPlane::operator!=(const CPlane& other) const 138 { 139 return m_Norm != other.m_Norm || m_Dist != other.m_Dist; 140 } 141 142 bool CPlane::operator==(const CPlane& other) const 143 { 144 return m_Norm == other.m_Norm && m_Dist == other.m_Dist; 145 } -
source/maths/Plane.h
63 63 bool FindLineSegIntersection (const CVector3D &start, const CVector3D &end, CVector3D *intsect); 64 64 bool FindRayIntersection (const CVector3D &start, const CVector3D &direction, CVector3D *intsect); 65 65 66 bool operator!=(const CPlane& other) const; 67 bool operator==(const CPlane& other) const; 68 66 69 public: 67 70 CVector3D m_Norm; //normal vector of the plane 68 71 float m_Dist; //Plane distance (ie D in the plane eq.)