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;
}
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)
{
if (!sharedTreeGeometry)
sharedTreeGeometry = makeSharedTreeGeometry(1600);
Geometry* quadGeom = simgear::clone(sharedTreeGeometry.get(),
CopyOp::SHALLOW_COPY);
Geometry* quadGeom = nullptr;
{
std::lock_guard<std::mutex> g(static_sharedGeometryMutex);
if (!sharedTreeGeometry)
sharedTreeGeometry = makeSharedTreeGeometry(1600);
quadGeom = simgear::clone(sharedTreeGeometry.get(),
CopyOp::SHALLOW_COPY);
}
Vec3Array* params = new Vec3Array;
params->push_back(Vec3(width, height, (float)varieties));
quadGeom->setNormalArray(params);

View File

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