Prevent nullptr exception in treenodes.

Happened to me once or twice mainly during reposition probably as scenery changed whilst still loading.
This commit is contained in:
Richard Harrison
2017-12-04 07:33:45 +01:00
parent 7dcc359110
commit 8d3723c91f
+22 -14
View File
@@ -202,35 +202,43 @@ void addTreeToLeafGeode(Geode* geode, const SGVec3f& p, const SGVec3f& t)
Vec3 pos = toOsg(p); Vec3 pos = toOsg(p);
Vec3 ter = toOsg(t); Vec3 ter = toOsg(t);
unsigned int numDrawables = geode->getNumDrawables(); unsigned int numDrawables = geode->getNumDrawables();
Geometry* geom Geometry* geom = static_cast<Geometry*>(geode->getDrawable(numDrawables - 1));
= static_cast<Geometry*>(geode->getDrawable(numDrawables - 1));
Vec3Array* posArray = static_cast<Vec3Array*>(geom->getColorArray()); Vec3Array* posArray = static_cast<Vec3Array*>(geom->getColorArray());
Vec3Array* tnormalArray = NULL; Vec3Array* tnormalArray = NULL;
if (use_tree_shadows || use_tree_normals)
{tnormalArray = static_cast<Vec3Array*>(geom->getSecondaryColorArray());} if (use_tree_shadows || use_tree_normals) {
if (posArray->size() tnormalArray = static_cast<Vec3Array*>(geom->getSecondaryColorArray());
>= static_cast<Vec3Array*>(geom->getVertexArray())->size()) { }
Vec3Array* paramsArray
= static_cast<Vec3Array*>(geom->getNormalArray()); if (posArray->size() >= static_cast<Vec3Array*>(geom->getVertexArray())->size()) {
Vec3Array* paramsArray = static_cast<Vec3Array*>(geom->getNormalArray());
Vec3 params = (*paramsArray)[0]; Vec3 params = (*paramsArray)[0];
geom = createTreeGeometry(params.x(), params.y(), params.z()); geom = createTreeGeometry(params.x(), params.y(), params.z());
posArray = static_cast<Vec3Array*>(geom->getColorArray()); posArray = static_cast<Vec3Array*>(geom->getColorArray());
if (use_tree_shadows || use_tree_normals)
{tnormalArray = static_cast<Vec3Array*>(geom->getSecondaryColorArray());} if (use_tree_shadows || use_tree_normals) {
tnormalArray = static_cast<Vec3Array*>(geom->getSecondaryColorArray());
}
geode->addDrawable(geom); geode->addDrawable(geom);
} }
if (tnormalArray && (use_tree_shadows || use_tree_normals))
tnormalArray->insert(tnormalArray->end(), 4, ter);
if (posArray)
{
posArray->insert(posArray->end(), 4, pos); posArray->insert(posArray->end(), 4, pos);
if (use_tree_shadows || use_tree_normals)
{tnormalArray->insert(tnormalArray->end(),4,ter);}
size_t numVerts = posArray->size(); size_t numVerts = posArray->size();
int imax = 2; int imax = 2;
if (use_tree_shadows) { imax = 3; } if (use_tree_shadows) { imax = 3; }
for (int i = 0; i < imax; ++i) { for (int i = 0; i < imax; ++i) {
DrawArrays* primSet DrawArrays* primSet = static_cast<DrawArrays*>(geom->getPrimitiveSet(i));
= static_cast<DrawArrays*>(geom->getPrimitiveSet(i)); if(primSet != nullptr)
primSet->setCount(numVerts); primSet->setCount(numVerts);
} }
} }
}
typedef std::map<std::string, osg::observer_ptr<Effect> > EffectMap; typedef std::map<std::string, osg::observer_ptr<Effect> > EffectMap;