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
+28 -20
View File
@@ -202,33 +202,41 @@ 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);
} }
posArray->insert(posArray->end(), 4, pos);
if (use_tree_shadows || use_tree_normals) if (tnormalArray && (use_tree_shadows || use_tree_normals))
{tnormalArray->insert(tnormalArray->end(),4,ter);} tnormalArray->insert(tnormalArray->end(), 4, ter);
size_t numVerts = posArray->size();
int imax = 2; if (posArray)
if (use_tree_shadows) {imax = 3;} {
for (int i = 0; i < imax; ++i) { posArray->insert(posArray->end(), 4, pos);
DrawArrays* primSet
= static_cast<DrawArrays*>(geom->getPrimitiveSet(i)); size_t numVerts = posArray->size();
primSet->setCount(numVerts); int imax = 2;
if (use_tree_shadows) { imax = 3; }
for (int i = 0; i < imax; ++i) {
DrawArrays* primSet = static_cast<DrawArrays*>(geom->getPrimitiveSet(i));
if(primSet != nullptr)
primSet->setCount(numVerts);
}
} }
} }