1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-20 02:53:51 +00:00

Optimize terrain QuadTree build

This commit is contained in:
bzzt 2019-02-20 13:37:00 +00:00 committed by Andrei Kortunov
parent 12cef51122
commit 03f23b235a
3 changed files with 17 additions and 16 deletions

View file

@ -71,16 +71,6 @@ QuadTreeNode::~QuadTreeNode()
{ {
} }
QuadTreeNode* QuadTreeNode::getParent()
{
return mParent;
}
QuadTreeNode *QuadTreeNode::getChild(unsigned int i)
{
return static_cast<QuadTreeNode*>(Group::getChild(i));
}
QuadTreeNode *QuadTreeNode::getNeighbour(Direction dir) QuadTreeNode *QuadTreeNode::getNeighbour(Direction dir)
{ {
return mNeighbours[dir]; return mNeighbours[dir];

View file

@ -35,10 +35,19 @@ namespace Terrain
QuadTreeNode(QuadTreeNode* parent, ChildDirection dir, float size, const osg::Vec2f& center); QuadTreeNode(QuadTreeNode* parent, ChildDirection dir, float size, const osg::Vec2f& center);
virtual ~QuadTreeNode(); virtual ~QuadTreeNode();
QuadTreeNode* getParent(); inline QuadTreeNode* getParent() { return mParent; }
inline QuadTreeNode* getChild(unsigned int i) { return static_cast<QuadTreeNode*>(Group::getChild(i)); }
inline unsigned int getNumChildren() const { return _children.size(); }
QuadTreeNode* getChild(unsigned int i); // osg::Group::addChild() does a lot of unrelated stuff, but we just really want to add a child node.
using osg::Group::getNumChildren; void addChild(QuadTreeNode* child)
{
// QuadTree node should not contain more than 4 child nodes.
// Reserve enough space if this node is supposed to have child nodes.
_children.reserve(4);
_children.push_back(child);
child->addParent(this);
};
/// Returns our direction relative to the parent node, or Root if we are the root node. /// Returns our direction relative to the parent node, or Root if we are the root node.
ChildDirection getDirection() { return mDirection; } ChildDirection getDirection() { return mDirection; }

View file

@ -158,9 +158,12 @@ public:
osg::BoundingBox boundingBox; osg::BoundingBox boundingBox;
for (unsigned int i=0; i<4; ++i) for (unsigned int i=0; i<4; ++i)
{ {
QuadTreeNode* child = addChild(parent, static_cast<ChildDirection>(i), halfSize); osg::ref_ptr<QuadTreeNode> child = addChild(parent, static_cast<ChildDirection>(i), halfSize);
if (child) if (child)
{
boundingBox.expandBy(child->getBoundingBox()); boundingBox.expandBy(child->getBoundingBox());
parent->addChild(child);
}
} }
if (!boundingBox.valid()) if (!boundingBox.valid())
@ -169,7 +172,7 @@ public:
parent->setBoundingBox(boundingBox); parent->setBoundingBox(boundingBox);
} }
QuadTreeNode* addChild(QuadTreeNode* parent, ChildDirection direction, float size) osg::ref_ptr<QuadTreeNode> addChild(QuadTreeNode* parent, ChildDirection direction, float size)
{ {
float halfSize = size/2.f; float halfSize = size/2.f;
osg::Vec2f center; osg::Vec2f center;
@ -194,7 +197,6 @@ public:
osg::ref_ptr<QuadTreeNode> node = new QuadTreeNode(parent, direction, size, center); osg::ref_ptr<QuadTreeNode> node = new QuadTreeNode(parent, direction, size, center);
node->setLodCallback(parent->getLodCallback()); node->setLodCallback(parent->getLodCallback());
node->setViewDataMap(mViewDataMap); node->setViewDataMap(mViewDataMap);
parent->addChild(node);
if (node->getSize() > mMinSize) if (node->getSize() > mMinSize)
{ {