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.
This commit is contained in:
timoore 2007-07-29 22:32:58 +00:00
parent 3fcc16c1a0
commit 7aa6fd479d
6 changed files with 177 additions and 3 deletions

View File

@ -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;

View File

@ -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)

View File

@ -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 <osgDB/FileNameUtils>
#include <osgDB/Registry>
#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<const SGReaderWriterBTGOptions*>(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;
}

View File

@ -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 <osgDB/Registry>
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

View File

@ -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 <osgDB/ReaderWriter>
#include <simgear/scene/tgdb/obj.hxx>
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

View File

@ -25,6 +25,8 @@
# include <simgear_config.h>
#endif
#include <osgDB/Registry>
#include <simgear/sg_inlines.h>
#include <simgear/math/point3d.hxx>
#include <simgear/math/sg_geodesy.hxx>
@ -33,7 +35,7 @@
#include <simgear/scene/material/matmodel.hxx>
#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<SGReaderWriterBTG> g_readerWriter_BTG_Proxy;
void sgUserDataInit( SGModelLib *m, const string &r,
SGPropertyNode *p, double t ) {
_inited = true;