Report errors on TileDetails callback failures

This callback can do out of memory and other failures, so catch
those and report them.
This commit is contained in:
James Turner 2021-07-27 14:22:50 +01:00 committed by James Turner
parent ce9f476ddb
commit 1c30b29168

View File

@ -37,6 +37,7 @@
#include <simgear/scene/util/SGReaderWriterOptions.hxx>
#include <simgear/scene/util/OptionsReadFileCallback.hxx>
#include <simgear/scene/util/SGNodeMasks.hxx>
#include <simgear/debug/ErrorReportingCallback.hxx>
#include "SGNodeTriangles.hxx"
#include "GroundLightManager.hxx"
@ -55,9 +56,11 @@ typedef std::list<SGDirectionalLightBin> SGDirectionalLightListBin;
#define SG_SIMPLIFIER_RATIO (0.001)
#define SG_SIMPLIFIER_MAX_LENGTH (1000.0)
#define SG_SIMPLIFIER_MAX_ERROR (2000.0)
#define SG_TILE_MIN_EXPIRY (180.0)
using namespace simgear;
using ReadResult = osgDB::ReaderWriter::ReadResult;
// QuadTreeBuilder is used by Random Objects Generator
typedef std::pair<osg::Node*, int> ModelLOD;
struct MakeQuadLeaf {
@ -97,47 +100,59 @@ public:
SG_LOG( SG_TERRAIN, SG_DEBUG, "SGTileDetailsCallback::~SGTileDetailsCallback() num cbs left " << num_tdcb );
}
virtual osgDB::ReaderWriter::ReadResult readNode(
const std::string&, const osgDB::Options*)
ReadResult readNode(const std::string&, const osgDB::Options*) override
{
SGMaterialLibPtr matlib;
osg::ref_ptr<SGMaterialCache> matcache;
osg::ref_ptr<osg::Group> group = new osg::Group;
group->setDataVariance(osg::Object::STATIC);
osg::ref_ptr<osg::Group> group;
simgear::ErrorReportContext ec{"btg", _path};
// generate textured triangle list
std::vector<SGTriangleInfo> matTris;
GetNodeTriangles nodeTris(_gbs_center, &matTris);
_rootNode->accept( nodeTris );
try {
group = new osg::Group;
SGMaterialLibPtr matlib;
osg::ref_ptr<SGMaterialCache> matcache;
group->setDataVariance(osg::Object::STATIC);
// build matcache
matlib = _options->getMaterialLib();
if (matlib) {
SGGeod geodPos = SGGeod::fromCart(_gbs_center);
matcache = matlib->generateMatCache(geodPos);
}
#if 0
// TEST : See if we can regenerate landclass shapes from node
for ( unsigned int i=0; i<matTris.size(); i++ ) {
matTris[i].dumpBorder(_gbs_center);
}
#endif
// generate textured triangle list
std::vector<SGTriangleInfo> matTris;
GetNodeTriangles nodeTris(_gbs_center, &matTris);
_rootNode->accept( nodeTris );
osg::Node* node = loadTerrain();
if (node) {
group->addChild(node);
}
// build matcache
matlib = _options->getMaterialLib();
if (matlib) {
SGGeod geodPos = SGGeod::fromCart(_gbs_center);
matcache = matlib->generateMatCache(geodPos);
}
#if 0
// TEST : See if we can regenerate landclass shapes from node
for ( unsigned int i=0; i<matTris.size(); i++ ) {
matTris[i].dumpBorder(_gbs_center);
}
#endif
osg::LOD* lightLOD = generateLightingTileObjects(matTris, matcache);
if (lightLOD) {
group->addChild(lightLOD);
}
osg::Node* node = loadTerrain();
if (node) {
group->addChild(node);
}
osg::LOD* objectLOD = generateRandomTileObjects(matTris, matcache);
if (objectLOD) {
group->addChild(objectLOD);
osg::LOD* lightLOD = generateLightingTileObjects(matTris, matcache);
if (lightLOD) {
group->addChild(lightLOD);
}
osg::LOD* objectLOD = generateRandomTileObjects(matTris, matcache);
if (objectLOD) {
group->addChild(objectLOD);
}
} catch (sg_exception& sge) {
simgear::reportFailure(simgear::LoadFailure::BadData, simgear::ErrorCode::BTGLoad,
"Failed to load BTG file:" + sge.getFormattedMessage(),
sge.getLocation());
return ReadResult::ERROR_IN_READING_FILE;
} catch (std::bad_alloc &e) {
simgear::reportFailure(simgear::LoadFailure::OutOfMemory, simgear::ErrorCode::BTGLoad,
"Out of memory loading tile details", sg_location{_path});
return ReadResult::INSUFFICIENT_MEMORY_TO_LOAD;
}
return group.release();