1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-03-28 23:06:41 +00:00

Use sorted vector for ViewData::contains implementation

This commit is contained in:
elsid 2022-10-05 01:05:09 +02:00
parent bfa303f73b
commit 6b8ed4c64f
No known key found for this signature in database
GPG key ID: 4DE04C198CBA7625
3 changed files with 43 additions and 8 deletions

View file

@ -378,6 +378,8 @@ namespace Terrain
if (vd->hasChanged()) if (vd->hasChanged())
{ {
vd->buildNodeIndex();
unsigned int ourVertexLod = getVertexLod(entry.mNode, mVertexLodMod); unsigned int ourVertexLod = getVertexLod(entry.mNode, mVertexLodMod);
// have to recompute the lodFlags in case a neighbour has changed LOD. // have to recompute the lodFlags in case a neighbour has changed LOD.
unsigned int lodFlags = getLodFlags(entry.mNode, ourVertexLod, mVertexLodMod, vd); unsigned int lodFlags = getLodFlags(entry.mNode, ourVertexLod, mVertexLodMod, vd);
@ -499,7 +501,7 @@ namespace Terrain
updateWaterCullingView(mHeightCullCallback, vd, static_cast<osgUtil::CullVisitor*>(&nv), updateWaterCullingView(mHeightCullCallback, vd, static_cast<osgUtil::CullVisitor*>(&nv),
mStorage->getCellWorldSize(), !isGridEmpty()); mStorage->getCellWorldSize(), !isGridEmpty());
vd->setChanged(false); vd->resetChanged();
double referenceTime = nv.getFrameStamp() ? nv.getFrameStamp()->getReferenceTime() : 0.0; double referenceTime = nv.getFrameStamp() ? nv.getFrameStamp()->getReferenceTime() : 0.0;
if (referenceTime != 0.0) if (referenceTime != 0.0)
@ -579,6 +581,7 @@ namespace Terrain
loadRenderingNode(entry, vd, cellWorldSize, grid, true); loadRenderingNode(entry, vd, cellWorldSize, grid, true);
if (pass == 0) if (pass == 0)
reporter.addProgress(entry.mNode->getSize()); reporter.addProgress(entry.mNode->getSize());
vd->removeNodeFromIndex(entry.mNode);
entry.mNode = nullptr; // Clear node lest we break the neighbours search for the next pass entry.mNode = nullptr; // Clear node lest we break the neighbours search for the next pass
} }
} }

View file

@ -2,6 +2,8 @@
#include "quadtreenode.hpp" #include "quadtreenode.hpp"
#include <algorithm>
namespace Terrain namespace Terrain
{ {
@ -25,6 +27,7 @@ namespace Terrain
mViewPoint = other.mViewPoint; mViewPoint = other.mViewPoint;
mActiveGrid = other.mActiveGrid; mActiveGrid = other.mActiveGrid;
mWorldUpdateRevision = other.mWorldUpdateRevision; mWorldUpdateRevision = other.mWorldUpdateRevision;
mNodes = other.mNodes;
} }
void ViewData::add(QuadTreeNode* node) void ViewData::add(QuadTreeNode* node)
@ -36,7 +39,10 @@ namespace Terrain
ViewDataEntry& entry = mEntries[index]; ViewDataEntry& entry = mEntries[index];
if (entry.set(node)) if (entry.set(node))
{
mChanged = true; mChanged = true;
mNodes.clear();
}
} }
void ViewData::setViewPoint(const osg::Vec3f& viewPoint) void ViewData::setViewPoint(const osg::Vec3f& viewPoint)
@ -56,6 +62,7 @@ namespace Terrain
// reset index for next frame // reset index for next frame
mNumEntries = 0; mNumEntries = 0;
mChanged = false; mChanged = false;
mNodes.clear();
} }
void ViewData::clear() void ViewData::clear()
@ -66,6 +73,7 @@ namespace Terrain
mLastUsageTimeStamp = 0; mLastUsageTimeStamp = 0;
mChanged = false; mChanged = false;
mHasViewPoint = false; mHasViewPoint = false;
mNodes.clear();
} }
bool ViewData::suitableToUse(const osg::Vec4i& activeGrid) const bool ViewData::suitableToUse(const osg::Vec4i& activeGrid) const
@ -73,12 +81,30 @@ namespace Terrain
return hasViewPoint() && activeGrid == mActiveGrid && getNumEntries(); return hasViewPoint() && activeGrid == mActiveGrid && getNumEntries();
} }
bool ViewData::contains(QuadTreeNode* node) const bool ViewData::contains(const QuadTreeNode* node) const
{ {
for (unsigned int i = 0; i < mNumEntries; ++i) return std::binary_search(mNodes.begin(), mNodes.end(), node);
if (mEntries[i].mNode == node) }
return true;
return false; void ViewData::buildNodeIndex()
{
if (!mNodes.empty())
return;
mNodes.reserve(mNumEntries);
for (std::size_t i = 0; i < mNumEntries; ++i)
if (const QuadTreeNode* node = mEntries[i].mNode)
mNodes.push_back(node);
std::sort(mNodes.begin(), mNodes.end());
}
void ViewData::removeNodeFromIndex(const QuadTreeNode* node)
{
const auto it = std::lower_bound(mNodes.begin(), mNodes.end(), node);
if (it == mNodes.end() || *it != node)
return;
mNodes.erase(it);
} }
ViewDataEntry::ViewDataEntry() ViewDataEntry::ViewDataEntry()

View file

@ -39,7 +39,7 @@ namespace Terrain
void clear(); void clear();
bool contains(QuadTreeNode* node) const; bool contains(const QuadTreeNode* node) const;
void copyFrom(const ViewData& other); void copyFrom(const ViewData& other);
@ -53,7 +53,7 @@ namespace Terrain
/// @note Such changes may necessitate a revalidation of cached mRenderingNodes elsewhere depending /// @note Such changes may necessitate a revalidation of cached mRenderingNodes elsewhere depending
/// on the parameters that affect the creation of mRenderingNode. /// on the parameters that affect the creation of mRenderingNode.
bool hasChanged() const { return mChanged; } bool hasChanged() const { return mChanged; }
void setChanged(bool changed) { mChanged = changed; } void resetChanged() { mChanged = false; }
bool hasViewPoint() const { return mHasViewPoint; } bool hasViewPoint() const { return mHasViewPoint; }
@ -67,6 +67,7 @@ namespace Terrain
mActiveGrid = grid; mActiveGrid = grid;
mEntries.clear(); mEntries.clear();
mNumEntries = 0; mNumEntries = 0;
mNodes.clear();
} }
} }
const osg::Vec4i& getActiveGrid() const { return mActiveGrid; } const osg::Vec4i& getActiveGrid() const { return mActiveGrid; }
@ -74,8 +75,13 @@ namespace Terrain
unsigned int getWorldUpdateRevision() const { return mWorldUpdateRevision; } unsigned int getWorldUpdateRevision() const { return mWorldUpdateRevision; }
void setWorldUpdateRevision(int updateRevision) { mWorldUpdateRevision = updateRevision; } void setWorldUpdateRevision(int updateRevision) { mWorldUpdateRevision = updateRevision; }
void buildNodeIndex();
void removeNodeFromIndex(const QuadTreeNode* node);
private: private:
std::vector<ViewDataEntry> mEntries; std::vector<ViewDataEntry> mEntries;
std::vector<const QuadTreeNode*> mNodes;
unsigned int mNumEntries; unsigned int mNumEntries;
double mLastUsageTimeStamp; double mLastUsageTimeStamp;
bool mChanged; bool mChanged;