Skip to content

Commit

Permalink
Do not store a LOD callback in the every QuadTreeNode
Browse files Browse the repository at this point in the history
  • Loading branch information
bzzt authored and akortunov committed Jun 5, 2019
1 parent a61c0aa commit e4ba6ec
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 30 deletions.
16 changes: 3 additions & 13 deletions components/terrain/quadtreenode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void QuadTreeNode::initNeighbours()
getChild(i)->initNeighbours();
}

void QuadTreeNode::traverse(ViewData* vd, const osg::Vec3f& viewPoint, float maxDist)
void QuadTreeNode::traverse(ViewData* vd, const osg::Vec3f& viewPoint, LodCallback* lodCallback, float maxDist)
{
if (!hasValidBounds())
return;
Expand All @@ -117,14 +117,14 @@ void QuadTreeNode::traverse(ViewData* vd, const osg::Vec3f& viewPoint, float max
if (dist > maxDist)
return;

bool stopTraversal = (mLodCallback->isSufficientDetail(this, dist)) || !getNumChildren();
bool stopTraversal = (lodCallback->isSufficientDetail(this, dist)) || !getNumChildren();

if (stopTraversal)
vd->add(this, true);
else
{
for (unsigned int i=0; i<getNumChildren(); ++i)
getChild(i)->traverse(vd, viewPoint, maxDist);
getChild(i)->traverse(vd, viewPoint, lodCallback, maxDist);
}
}

Expand Down Expand Up @@ -167,16 +167,6 @@ void QuadTreeNode::intersect(ViewData* vd, TerrainLineIntersector* intersector)
}
}

void QuadTreeNode::setLodCallback(LodCallback *lodCallback)
{
mLodCallback = lodCallback;
}

LodCallback *QuadTreeNode::getLodCallback()
{
return mLodCallback;
}

void QuadTreeNode::setBoundingBox(const osg::BoundingBox &boundingBox)
{
mBoundingBox = boundingBox;
Expand Down
12 changes: 2 additions & 10 deletions components/terrain/quadtreenode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,15 @@ namespace Terrain
/// center in cell coordinates
const osg::Vec2f& getCenter() const;

/// Optimized version of traverse() that doesn't incur the overhead of NodeVisitor double-dispatch or fetching the various variables.
/// Note this doesn't do any culling.
void traverse(ViewData* vd, const osg::Vec3f& viewPoint, float maxDist);
/// Traverse nodes according to LOD selection.
void traverse(ViewData* vd, const osg::Vec3f& viewPoint, LodCallback* lodCallback, float maxDist);

/// Traverse to a specific node and add only that node.
void traverseTo(ViewData* vd, float size, const osg::Vec2f& center);

/// Adds all leaf nodes which intersect the line from start to end
void intersect(ViewData* vd, TerrainLineIntersector* intersector);

/// Set the Lod callback to use for determining when to stop traversing further down the quad tree.
void setLodCallback(LodCallback* lodCallback);

LodCallback* getLodCallback();

private:
QuadTreeNode* mParent;

Expand All @@ -117,8 +111,6 @@ namespace Terrain
bool mValidBounds;
float mSize;
osg::Vec2f mCenter;

osg::ref_ptr<LodCallback> mLodCallback;
};

}
Expand Down
13 changes: 6 additions & 7 deletions components/terrain/quadtreeworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ class RootNode : public QuadTreeNode
class QuadTreeBuilder
{
public:
QuadTreeBuilder(Terrain::Storage* storage, float lodFactor, float minSize)
QuadTreeBuilder(Terrain::Storage* storage, float minSize)
: mStorage(storage)
, mLodFactor(lodFactor)
, mMinX(0.f), mMaxX(0.f), mMinY(0.f), mMaxY(0.f)
, mMinSize(minSize)
{
Expand All @@ -119,7 +118,6 @@ class QuadTreeBuilder
float centerY = (mMinY+mMaxY)/2.f + (size-origSizeY)/2.f;

mRootNode = new RootNode(size, osg::Vec2f(centerX, centerY));
mRootNode->setLodCallback(new DefaultLodCallback(mLodFactor, mMinSize));
addChildren(mRootNode);

mRootNode->initNeighbours();
Expand Down Expand Up @@ -168,7 +166,6 @@ class QuadTreeBuilder
}

osg::ref_ptr<QuadTreeNode> node = new QuadTreeNode(parent, direction, size, center);
node->setLodCallback(parent->getLodCallback());

if (center.x() - halfSize > mMaxX
|| center.x() + halfSize < mMinX
Expand Down Expand Up @@ -218,6 +215,7 @@ class QuadTreeBuilder
float mMinSize;

osg::ref_ptr<RootNode> mRootNode;
osg::ref_ptr<LodCallback> mLodCallback;
};

QuadTreeWorld::QuadTreeWorld(osg::Group *parent, osg::Group *compileRoot, Resource::ResourceSystem *resourceSystem, Storage *storage, int nodeMask, int preCompileMask, int borderMask, int compMapResolution, float compMapLevel, float lodFactor, int vertexLodMod, float maxCompGeometrySize)
Expand Down Expand Up @@ -359,7 +357,7 @@ void QuadTreeWorld::accept(osg::NodeVisitor &nv)
mRootNode->traverseTo(vd, 1, osg::Vec2f(x+0.5,y+0.5));
}
else
mRootNode->traverse(vd, cv->getViewPoint(), mViewDistance);
mRootNode->traverse(vd, cv->getViewPoint(), mLodCallback, mViewDistance);
}
else
{
Expand Down Expand Up @@ -424,7 +422,8 @@ void QuadTreeWorld::ensureQuadTreeBuilt()
return;

const float minSize = 1/8.f;
QuadTreeBuilder builder(mStorage, mLodFactor, minSize);
mLodCallback = new DefaultLodCallback(mLodFactor, minSize);
QuadTreeBuilder builder(mStorage, minSize);
builder.build();

mRootNode = builder.getRootNode();
Expand Down Expand Up @@ -470,7 +469,7 @@ void QuadTreeWorld::preload(View *view, const osg::Vec3f &viewPoint, std::atomic

ViewData* vd = static_cast<ViewData*>(view);
vd->setViewPoint(viewPoint);
mRootNode->traverse(vd, viewPoint, mViewDistance);
mRootNode->traverse(vd, viewPoint, mLodCallback, mViewDistance);

for (unsigned int i=0; i<vd->getNumEntries() && !abort; ++i)
{
Expand Down
2 changes: 2 additions & 0 deletions components/terrain/quadtreeworld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Terrain
{
class RootNode;
class ViewDataMap;
class LodCallback;

/// @brief Terrain implementation that loads cells into a Quad Tree, with geometry LOD and texture LOD.
class QuadTreeWorld : public TerrainGrid // note: derived from TerrainGrid is only to render default cells (see loadCell)
Expand Down Expand Up @@ -48,6 +49,7 @@ namespace Terrain
osg::ref_ptr<RootNode> mRootNode;

osg::ref_ptr<ViewDataMap> mViewDataMap;
osg::ref_ptr<LodCallback> mLodCallback;

OpenThreads::Mutex mQuadTreeMutex;
bool mQuadTreeBuilt;
Expand Down

0 comments on commit e4ba6ec

Please sign in to comment.