API to reset the shared tree geometry

This commit is contained in:
James Turner 2020-08-14 15:40:47 +01:00
parent e659ad3872
commit b874201806
2 changed files with 36 additions and 15 deletions

View File

@ -151,14 +151,26 @@ Geometry* makeSharedTreeGeometry(int numQuads)
return result; return result;
} }
ref_ptr<Geometry> sharedTreeGeometry; static std::mutex static_sharedGeometryMutex;
static ref_ptr<Geometry> sharedTreeGeometry;
void clearSharedTreeGeometry()
{
std::lock_guard<std::mutex> g(static_sharedGeometryMutex);
sharedTreeGeometry = {};
}
Geometry* createTreeGeometry(float width, float height, int varieties) Geometry* createTreeGeometry(float width, float height, int varieties)
{ {
Geometry* quadGeom = nullptr;
{
std::lock_guard<std::mutex> g(static_sharedGeometryMutex);
if (!sharedTreeGeometry) if (!sharedTreeGeometry)
sharedTreeGeometry = makeSharedTreeGeometry(1600); sharedTreeGeometry = makeSharedTreeGeometry(1600);
Geometry* quadGeom = simgear::clone(sharedTreeGeometry.get(), quadGeom = simgear::clone(sharedTreeGeometry.get(),
CopyOp::SHALLOW_COPY); CopyOp::SHALLOW_COPY);
}
Vec3Array* params = new Vec3Array; Vec3Array* params = new Vec3Array;
params->push_back(Vec3(width, height, (float)varieties)); params->push_back(Vec3(width, height, (float)varieties));
quadGeom->setNormalArray(params); quadGeom->setNormalArray(params);

View File

@ -35,11 +35,14 @@ namespace simgear
{ {
class TreeBin { class TreeBin {
public: public:
~TreeBin() = default;
struct Tree { struct Tree {
SGVec3f position; SGVec3f position;
SGVec3f tnormal; SGVec3f tnormal;
Tree(const SGVec3f& p, const SGVec3f& t) : position(p),tnormal(t) Tree(const SGVec3f& p, const SGVec3f& t) : position(p), tnormal(t)
{ } {
}
}; };
typedef std::vector<Tree> TreeList; typedef std::vector<Tree> TreeList;
@ -55,19 +58,25 @@ public:
{ _trees.push_back(t); } { _trees.push_back(t); }
void insert(const SGVec3f& p, const SGVec3f& tnorm) void insert(const SGVec3f& p, const SGVec3f& tnorm)
{insert(Tree(p,tnorm));} {
_trees.emplace_back(p, tnorm);
}
unsigned getNumTrees() const unsigned getNumTrees() const
{ return _trees.size(); } { return _trees.size(); }
const Tree& getTree(unsigned i) const const Tree& getTree(unsigned i) const
{ return _trees[i]; } {
assert(i < _trees.size());
return _trees.at(i);
}
TreeList _trees; TreeList _trees;
~TreeBin() {
_trees.clear();
}
}; };
void clearSharedTreeGeometry();
typedef std::list<TreeBin*> SGTreeBinList; typedef std::list<TreeBin*> SGTreeBinList;
osg::Group* createForest(SGTreeBinList& forestList, const osg::Matrix& transform, osg::Group* createForest(SGTreeBinList& forestList, const osg::Matrix& transform,