From 8d3723c91fe56e63edaeb498e9f71e7db5f7629b Mon Sep 17 00:00:00 2001 From: Richard Harrison Date: Mon, 4 Dec 2017 07:33:45 +0100 Subject: [PATCH] Prevent nullptr exception in treenodes. Happened to me once or twice mainly during reposition probably as scenery changed whilst still loading. --- simgear/scene/tgdb/TreeBin.cxx | 48 ++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/simgear/scene/tgdb/TreeBin.cxx b/simgear/scene/tgdb/TreeBin.cxx index 7c6bc990..ee8f4b39 100644 --- a/simgear/scene/tgdb/TreeBin.cxx +++ b/simgear/scene/tgdb/TreeBin.cxx @@ -202,33 +202,41 @@ void addTreeToLeafGeode(Geode* geode, const SGVec3f& p, const SGVec3f& t) Vec3 pos = toOsg(p); Vec3 ter = toOsg(t); unsigned int numDrawables = geode->getNumDrawables(); - Geometry* geom - = static_cast(geode->getDrawable(numDrawables - 1)); + Geometry* geom = static_cast(geode->getDrawable(numDrawables - 1)); Vec3Array* posArray = static_cast(geom->getColorArray()); Vec3Array* tnormalArray = NULL; - if (use_tree_shadows || use_tree_normals) - {tnormalArray = static_cast(geom->getSecondaryColorArray());} - if (posArray->size() - >= static_cast(geom->getVertexArray())->size()) { - Vec3Array* paramsArray - = static_cast(geom->getNormalArray()); + + if (use_tree_shadows || use_tree_normals) { + tnormalArray = static_cast(geom->getSecondaryColorArray()); + } + + if (posArray->size() >= static_cast(geom->getVertexArray())->size()) { + Vec3Array* paramsArray = static_cast(geom->getNormalArray()); Vec3 params = (*paramsArray)[0]; geom = createTreeGeometry(params.x(), params.y(), params.z()); posArray = static_cast(geom->getColorArray()); - if (use_tree_shadows || use_tree_normals) - {tnormalArray = static_cast(geom->getSecondaryColorArray());} + + if (use_tree_shadows || use_tree_normals) { + tnormalArray = static_cast(geom->getSecondaryColorArray()); + } geode->addDrawable(geom); } - posArray->insert(posArray->end(), 4, pos); - if (use_tree_shadows || use_tree_normals) - {tnormalArray->insert(tnormalArray->end(),4,ter);} - size_t numVerts = posArray->size(); - int imax = 2; - if (use_tree_shadows) {imax = 3;} - for (int i = 0; i < imax; ++i) { - DrawArrays* primSet - = static_cast(geom->getPrimitiveSet(i)); - primSet->setCount(numVerts); + + if (tnormalArray && (use_tree_shadows || use_tree_normals)) + tnormalArray->insert(tnormalArray->end(), 4, ter); + + if (posArray) + { + posArray->insert(posArray->end(), 4, pos); + + size_t numVerts = posArray->size(); + int imax = 2; + if (use_tree_shadows) { imax = 3; } + for (int i = 0; i < imax; ++i) { + DrawArrays* primSet = static_cast(geom->getPrimitiveSet(i)); + if(primSet != nullptr) + primSet->setCount(numVerts); + } } }