From 433900fca5c3757d316a16c5dc5749b26a236df8 Mon Sep 17 00:00:00 2001 From: scrawl Date: Thu, 9 Mar 2017 03:31:30 +0100 Subject: [PATCH] Fix waiting on initial quad tree build --- components/terrain/quadtreeworld.cpp | 41 ++++++++++++++++------------ components/terrain/quadtreeworld.hpp | 8 ++++-- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/components/terrain/quadtreeworld.cpp b/components/terrain/quadtreeworld.cpp index bc4a4abc5b..66748a4aa1 100644 --- a/components/terrain/quadtreeworld.cpp +++ b/components/terrain/quadtreeworld.cpp @@ -337,34 +337,39 @@ void QuadTreeWorld::accept(osg::NodeVisitor &nv) mRootNode->getViewDataMap()->clearUnusedViews(nv.getTraversalNumber()); } -void QuadTreeWorld::loadCell(int x, int y) +void QuadTreeWorld::ensureQuadTreeBuilt() { - if (mQuadTreeBuilt && !mRootNode) { + OpenThreads::ScopedLock lock(mQuadTreeMutex); + if (mQuadTreeBuilt) + return; + if (!mQuadTreeBuilt && !mWorkItem) + { + const float minSize = 1/4.f; + mWorkItem = new BuildQuadTreeItem(mStorage, mViewDataMap.get(), minSize); + mWorkQueue->addWorkItem(mWorkItem); + } + } + + mWorkItem->waitTillDone(); + { + OpenThreads::ScopedLock lock(mQuadTreeMutex); mRootNode = mWorkItem->getRootNode(); mRootNode->setWorld(this); - mTerrainRoot->addChild(mRootNode); - mWorkItem = NULL; - } -} - -osg::ref_ptr QuadTreeWorld::cacheCell(int x, int y) -{ - if (!mQuadTreeBuilt) - { - const float minSize = 1/4.f; - mWorkItem = new BuildQuadTreeItem(mStorage, mViewDataMap.get(), minSize); - mWorkQueue->addWorkItem(mWorkItem); - - mWorkItem->waitTillDone(); - mQuadTreeBuilt = true; } - return NULL; } void QuadTreeWorld::enable(bool enabled) { + if (enabled) + { + ensureQuadTreeBuilt(); + + if (!mRootNode->getNumParents()) + mTerrainRoot->addChild(mRootNode); + } + if (mRootNode) mRootNode->setNodeMask(enabled ? ~0 : 0); } diff --git a/components/terrain/quadtreeworld.hpp b/components/terrain/quadtreeworld.hpp index bec901b096..f66426ed24 100644 --- a/components/terrain/quadtreeworld.hpp +++ b/components/terrain/quadtreeworld.hpp @@ -5,6 +5,8 @@ #include "world.hpp" +#include + namespace SceneUtil { class WorkQueue; @@ -30,12 +32,11 @@ namespace Terrain void accept(osg::NodeVisitor& nv); - virtual void loadCell(int x, int y); - virtual osg::ref_ptr cacheCell(int x, int y); - virtual void enable(bool enabled); private: + void ensureQuadTreeBuilt(); + osg::ref_ptr mRootNode; osg::ref_ptr mWorkQueue; @@ -44,6 +45,7 @@ namespace Terrain osg::ref_ptr mViewDataMap; + OpenThreads::Mutex mQuadTreeMutex; bool mQuadTreeBuilt; };