1 // Copyright (C) 2007 Tim Moore timoore@redhat.com
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License as
5 // published by the Free Software Foundation; either version 2 of the
6 // License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but
9 // WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17 
18 #ifdef HAVE_CONFIG_H
19 #  include <simgear_config.h>
20 #endif
21 
22 #include <osgDB/FileNameUtils>
23 #include <osgDB/Registry>
24 
25 #include <simgear/debug/ErrorReportingCallback.hxx>
26 #include <simgear/scene/model/ModelRegistry.hxx>
27 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
28 #include <simgear/structure/exception.hxx>
29 
30 #include "SGReaderWriterBTG.hxx"
31 #include "obj.hxx"
32 
33 using namespace simgear;
34 
SGReaderWriterBTG()35 SGReaderWriterBTG::SGReaderWriterBTG()
36 {
37     supportsExtension("btg", "SimGear btg database format");
38     // supportsExtension("btg.gz", "SimGear btg database format");
39 }
40 
~SGReaderWriterBTG()41 SGReaderWriterBTG::~SGReaderWriterBTG()
42 {
43 }
44 
className() const45 const char* SGReaderWriterBTG::className() const
46 {
47     return "BTG Database reader";
48 }
49 
50 bool
acceptsExtension(const std::string & extension) const51 SGReaderWriterBTG::acceptsExtension(const std::string& extension) const
52 {
53     // trick the osg extensions match algorithm to accept btg.gz files.
54     if (osgDB::convertToLowerCase(extension) == "gz")
55         return true;
56     return osgDB::ReaderWriter::acceptsExtension(extension);
57 }
58 
59 osgDB::ReaderWriter::ReadResult
readNode(const std::string & fileName,const osgDB::Options * options) const60 SGReaderWriterBTG::readNode(const std::string& fileName,
61                             const osgDB::Options* options) const
62 {
63     const SGReaderWriterOptions* sgOptions;
64     sgOptions = dynamic_cast<const SGReaderWriterOptions*>(options);
65     osg::Node* result = NULL;
66     simgear::ErrorReportContext ec{"btg", fileName};
67     try {
68         result = SGLoadBTG(fileName, sgOptions);
69         if (!result)
70             return ReadResult::FILE_NOT_HANDLED;
71     } catch (sg_exception& e) {
72         simgear::reportFailure(simgear::LoadFailure::BadData, simgear::ErrorCode::BTGLoad,
73                                "Failed to load BTG file:" + e.getFormattedMessage(),
74                                e.getLocation());
75         return ReadResult::ERROR_IN_READING_FILE;
76     } catch (std::bad_alloc&) {
77         simgear::reportFailure(simgear::LoadFailure::OutOfMemory, simgear::ErrorCode::BTGLoad,
78                                "Out of memory loading BTG:" + fileName, sg_location{fileName});
79         return ReadResult::ERROR_IN_READING_FILE;
80     }
81 
82     return result;
83 }
84 
85 
86 typedef ModelRegistryCallback<DefaultProcessPolicy, NoCachePolicy,
87                               NoOptimizePolicy,
88                               NoSubstitutePolicy, BuildGroupBVHPolicy>
89 BTGCallback;
90 
91 namespace
92 {
93 ModelRegistryCallbackProxy<BTGCallback> g_btgCallbackProxy("btg");
94 }
95