From 7aa6fd479da920a6eb95f4b7da2010906928b619 Mon Sep 17 00:00:00 2001 From: timoore Date: Sun, 29 Jul 2007 22:32:58 +0000 Subject: [PATCH] OSG Reader and Writer for BTG files This is part of a somewhat long road towards terrain database paging using OSG's database pager thread. --- simgear/scene/model/model.cxx | 11 ++- simgear/scene/tgdb/Makefile.am | 2 + simgear/scene/tgdb/SGReaderWriterBTG.cxx | 67 +++++++++++++++++++ simgear/scene/tgdb/SGReaderWriterBTG.hxx | 35 ++++++++++ .../scene/tgdb/SGReaderWriterBTGOptions.hxx | 53 +++++++++++++++ simgear/scene/tgdb/userdata.cxx | 12 +++- 6 files changed, 177 insertions(+), 3 deletions(-) create mode 100644 simgear/scene/tgdb/SGReaderWriterBTG.cxx create mode 100644 simgear/scene/tgdb/SGReaderWriterBTG.hxx create mode 100644 simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx diff --git a/simgear/scene/model/model.cxx b/simgear/scene/model/model.cxx index 1d278f7e..4a2349be 100644 --- a/simgear/scene/model/model.cxx +++ b/simgear/scene/model/model.cxx @@ -207,6 +207,15 @@ public: readNode(const std::string& fileName, const osgDB::ReaderWriter::Options* opt) { + osgDB::Registry* registry = osgDB::Registry::instance(); + osgDB::ReaderWriter::ReadResult res; + // The BTG loader automatically looks for ".btg.gz" if a file with + // the .btg extension doesn't exist. Also, we don't want to add + // nodes, run the optimizer, etc. on the btg model.So, let it do + // its thing. + if (osgDB::equalCaseInsensitive(osgDB::getFileExtension(fileName), "btg")) { + return registry->readNodeImplementation(fileName, opt); + } std::string absFileName = osgDB::findDataFile(fileName); if (!osgDB::fileExists(absFileName)) { SG_LOG(SG_IO, SG_ALERT, "Cannot find model file \"" @@ -214,8 +223,6 @@ public: return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND; } - osgDB::Registry* registry = osgDB::Registry::instance(); - osgDB::ReaderWriter::ReadResult res; res = registry->readNodeImplementation(absFileName, opt); if (!res.validNode()) return res; diff --git a/simgear/scene/tgdb/Makefile.am b/simgear/scene/tgdb/Makefile.am index 21ab3fb8..e6656989 100644 --- a/simgear/scene/tgdb/Makefile.am +++ b/simgear/scene/tgdb/Makefile.am @@ -14,6 +14,7 @@ include_HEADERS = \ SGDirectionalLightBin.hxx \ SGLightBin.hxx \ SGOceanTile.hxx \ + SGReaderWriterBTGOptions.hxx \ SGTexturedTriangleBin.hxx \ SGTriangleBin.hxx \ SGVertexArrayBin.hxx @@ -24,6 +25,7 @@ libsgtgdb_a_SOURCES = \ pt_lights.cxx \ userdata.cxx \ SGOceanTile.cxx \ + SGReaderWriterBTG.cxx \ SGVasiDrawable.cxx INCLUDES = -I$(top_srcdir) diff --git a/simgear/scene/tgdb/SGReaderWriterBTG.cxx b/simgear/scene/tgdb/SGReaderWriterBTG.cxx new file mode 100644 index 00000000..daa7f70e --- /dev/null +++ b/simgear/scene/tgdb/SGReaderWriterBTG.cxx @@ -0,0 +1,67 @@ +// Copyright (C) 2007 Tim Moore timoore@redhat.com +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + +#include +#include +#include "SGReaderWriterBTGOptions.hxx" +#include "SGReaderWriterBTG.hxx" +#include "obj.hxx" + +const char* SGReaderWriterBTG::className() const +{ + return "BTG Database reader"; +} + +bool SGReaderWriterBTG::acceptsExtension(const std::string& extension) const +{ + return (osgDB::equalCaseInsensitive(extension, "gz") + || osgDB::equalCaseInsensitive(extension, "btg")); +} + +osgDB::ReaderWriter::ReadResult +SGReaderWriterBTG::readNode(const std::string& fileName, + const osgDB::ReaderWriter::Options* options) const +{ + std::string ext = osgDB::getLowerCaseFileExtension(fileName); + if(!acceptsExtension(ext)) + return ReadResult::FILE_NOT_HANDLED; + if (osgDB::equalCaseInsensitive(ext, "gz")) { + std::string btgFileName = osgDB::getNameLessExtension(fileName); + if (!acceptsExtension( + osgDB::getLowerCaseFileExtension(btgFileName))) { + return ReadResult::FILE_NOT_HANDLED; + } + } + SGMaterialLib* matlib = 0; + bool calcLights = false; + bool useRandomObjects = false; + const SGReaderWriterBTGOptions* btgOptions + = dynamic_cast(options); + if (btgOptions) { + matlib = btgOptions->getMatlib(); + calcLights = btgOptions->getCalcLights(); + useRandomObjects = btgOptions->getUseRandomObjects(); + } + osg::Node* result = SGLoadBTG(fileName, matlib, calcLights, + useRandomObjects); + if (result) + return result; + else + return ReadResult::FILE_NOT_HANDLED; +} + + diff --git a/simgear/scene/tgdb/SGReaderWriterBTG.hxx b/simgear/scene/tgdb/SGReaderWriterBTG.hxx new file mode 100644 index 00000000..20070b24 --- /dev/null +++ b/simgear/scene/tgdb/SGReaderWriterBTG.hxx @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2006-2007 Tim Moore timoore@redhat.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ +#ifndef SGREADERWRITERBTG_HXX +#define SGREADERWRITERBTG_HXX 1 +#include + +class SGReaderWriterBTG : public osgDB::ReaderWriter { +public: + virtual const char* className() const; + + virtual bool acceptsExtension(const std::string& extension) const; + + virtual ReadResult readNode(const std::string& fileName, + const osgDB::ReaderWriter::Options* options) + const; +}; +#endif + diff --git a/simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx b/simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx new file mode 100644 index 00000000..91a7c2ad --- /dev/null +++ b/simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx @@ -0,0 +1,53 @@ +// Copyright (C) 2007 Tim Moore timoore@redhat.com +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// +#ifndef SGREADERWRITERBTGOPTIONS_HXX +#define SGREADERWRITERBTGOPTIONS_HXX 1 + +#include +#include +class SGReaderWriterBTGOptions : public osgDB::ReaderWriter::Options { +public: + SGReaderWriterBTGOptions() {} + SGReaderWriterBTGOptions(const std::string& str): + osgDB::ReaderWriter::Options(str), + _matlib(0), _calcLights(false), _useRandomObjects(false) + {} + + SGReaderWriterBTGOptions(const SGReaderWriterBTGOptions& options, + const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): + osgDB::ReaderWriter::Options(options, copyop), + _matlib(options._matlib), _calcLights(options._calcLights), + _useRandomObjects(options._useRandomObjects) + { + } + SGMaterialLib* getMatlib() const { return _matlib; } + void setMatlib (SGMaterialLib* matlib) { _matlib = matlib; } + bool getCalcLights() const { return _calcLights; } + void setCalcLights(bool calcLights) { _calcLights = calcLights; } + bool getUseRandomObjects() const { return _useRandomObjects; } + void setUseRandomObjects(bool useRandomObjects) + { + _useRandomObjects = useRandomObjects; + } +protected: + virtual ~SGReaderWriterBTGOptions() {} + SGMaterialLib* _matlib; + bool _calcLights; + bool _useRandomObjects; + +}; +#endif diff --git a/simgear/scene/tgdb/userdata.cxx b/simgear/scene/tgdb/userdata.cxx index 52f0a6f8..9e2b3f1f 100644 --- a/simgear/scene/tgdb/userdata.cxx +++ b/simgear/scene/tgdb/userdata.cxx @@ -25,6 +25,8 @@ # include #endif +#include + #include #include #include @@ -33,7 +35,7 @@ #include #include "userdata.hxx" - +#include "SGReaderWriterBTG.hxx" // the following are static values needed by the runtime object // loader. However, the loading is done via a call back so these @@ -47,6 +49,14 @@ static string model_root = ""; static SGPropertyNode *root_props = NULL; static double sim_time_sec = 0.0; +// Because BTG files are now loaded through the osgDB::Registry, there +// are no symbols referenced by FlightGear in this library other than +// sgUserDataInit. But the libraries are all statically linked, so +// none of the other object files in this library would be included in +// the executable! Sticking the static proxy here forces the BTG code +// to be sucked in. +osgDB::RegisterReaderWriterProxy g_readerWriter_BTG_Proxy; + void sgUserDataInit( SGModelLib *m, const string &r, SGPropertyNode *p, double t ) { _inited = true; -- 2.39.5