]> git.mxchange.org Git - simgear.git/commitdiff
OSG Reader and Writer for BTG files
authortimoore <timoore>
Sun, 29 Jul 2007 22:32:58 +0000 (22:32 +0000)
committertimoore <timoore>
Sun, 29 Jul 2007 22:32:58 +0000 (22:32 +0000)
This is part of a somewhat long road towards terrain database paging using
OSG's database pager thread.

simgear/scene/model/model.cxx
simgear/scene/tgdb/Makefile.am
simgear/scene/tgdb/SGReaderWriterBTG.cxx [new file with mode: 0644]
simgear/scene/tgdb/SGReaderWriterBTG.hxx [new file with mode: 0644]
simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx [new file with mode: 0644]
simgear/scene/tgdb/userdata.cxx

index 1d278f7ee63faeb0647185340c8f7c057a6a6bd4..4a2349be603cac35bf1aa7153c83d789577647fa 100644 (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;
index 21ab3fb85029ad984ecfe63fd2285a35a09a65c4..e66569893b5fc0e7401690f7b542761c08fb2bc7 100644 (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)
diff --git a/simgear/scene/tgdb/SGReaderWriterBTG.cxx b/simgear/scene/tgdb/SGReaderWriterBTG.cxx
new file mode 100644 (file)
index 0000000..daa7f70
--- /dev/null
@@ -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;
+}
+
+
diff --git a/simgear/scene/tgdb/SGReaderWriterBTG.hxx b/simgear/scene/tgdb/SGReaderWriterBTG.hxx
new file mode 100644 (file)
index 0000000..20070b2
--- /dev/null
@@ -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
+    
diff --git a/simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx b/simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx
new file mode 100644 (file)
index 0000000..91a7c2a
--- /dev/null
@@ -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
index 52f0a6f87726478b9712b069f88efb58b6416d20..9e2b3f1fa1ad273e9ab649f64bf48d13a106570e 100644 (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;