20,741
edits
m (→Approach) |
|||
| Line 79: | Line 79: | ||
These are already the main building blocks required for loading a 3D model from disk ([[$FG_ROOT]]) and getting an <code>osg::Node*</code> in return. | These are already the main building blocks required for loading a 3D model from disk ([[$FG_ROOT]]) and getting an <code>osg::Node*</code> in return. | ||
Next, we need to add this as a method to FGCanvasSystemAdapter to expose this FlightGear-specific API to the Canvas subsystem | Next, we need to add this as a method to FGCanvasSystemAdapter to expose this FlightGear-specific API to the Canvas subsystem living in SimGear: | ||
<syntaxhighlight lang="diff"> | |||
diff --git a/simgear/canvas/canvas_fwd.hxx b/simgear/canvas/canvas_fwd.hxx | |||
index 3b1b464..15ccd57 100644 | |||
--- a/simgear/canvas/canvas_fwd.hxx | |||
+++ b/simgear/canvas/canvas_fwd.hxx | |||
@@ -23,8 +23,10 @@ | |||
#include <simgear/structure/SGWeakPtr.hxx> | |||
#include <osg/ref_ptr> | |||
+#include <osg/Node> | |||
#include <osgText/Font> | |||
+ | |||
#include <boost/function.hpp> | |||
#include <boost/shared_ptr.hpp> | |||
#include <boost/weak_ptr.hpp> | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="diff"> | |||
diff --git a/simgear/canvas/CanvasSystemAdapter.hxx b/simgear/canvas/CanvasSystemAdapter.hxx | |||
index 2492bb9..48f2894 100644 | |||
--- a/simgear/canvas/CanvasSystemAdapter.hxx | |||
+++ b/simgear/canvas/CanvasSystemAdapter.hxx | |||
@@ -38,6 +38,7 @@ namespace canvas | |||
virtual void addCamera(osg::Camera* camera) const = 0; | |||
virtual void removeCamera(osg::Camera* camera) const = 0; | |||
virtual osg::Image* getImage(const std::string& path) const = 0; | |||
+ virtual osg::Node* getModel(const std::string& path) const = 0; | |||
virtual SGSubsystem* getSubsystem(const std::string& name) const = 0; | |||
virtual HTTP::Client* getHTTPClient() const = 0; | |||
}; | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="diff"> | |||
diff --git a/src/Canvas/FGCanvasSystemAdapter.hxx b/src/Canvas/FGCanvasSystemAdapter.hxx | |||
index c43f793..6b7ad82 100644 | |||
--- a/src/Canvas/FGCanvasSystemAdapter.hxx | |||
+++ b/src/Canvas/FGCanvasSystemAdapter.hxx | |||
@@ -21,6 +21,8 @@ | |||
#include <simgear/canvas/CanvasSystemAdapter.hxx> | |||
+#include <simgear/scene/model/modellib.hxx> | |||
+ | |||
namespace canvas | |||
{ | |||
class FGCanvasSystemAdapter: | |||
@@ -31,6 +33,7 @@ namespace canvas | |||
virtual void addCamera(osg::Camera* camera) const; | |||
virtual void removeCamera(osg::Camera* camera) const; | |||
virtual osg::Image* getImage(const std::string& path) const; | |||
+ virtual osg::Node* getModel(const std::string& path) const; | |||
virtual SGSubsystem* getSubsystem(const std::string& name) const; | |||
virtual simgear::HTTP::Client* getHTTPClient() const; | |||
}; | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="diff"> | |||
diff --git a/src/Canvas/FGCanvasSystemAdapter.cxx b/src/Canvas/FGCanvasSystemAdapter.cxx | |||
index 95b6b65..627ab18 100644 | |||
--- a/src/Canvas/FGCanvasSystemAdapter.cxx | |||
+++ b/src/Canvas/FGCanvasSystemAdapter.cxx | |||
@@ -75,7 +75,44 @@ namespace canvas | |||
if( globals->get_renderer() ) | |||
globals->get_renderer()->removeCamera(camera); | |||
} | |||
+ //---------------------------------------------------------------------------- | |||
+ osg::Node* FGCanvasSystemAdapter::getModel(const std::string& path) const | |||
+ { | |||
+ const char *model_path = "Models/Geometry/glider.ac"; | |||
+ if( SGPath(path).isAbsolute() ) | |||
+ { | |||
+ const char* valid_path = fgValidatePath(path.c_str(), false); | |||
+ if( valid_path ) | |||
+ try { | |||
+ std::string fullPath = simgear::SGModelLib::findDataFile(valid_path); | |||
+ osg::Node * object = simgear::SGModelLib::loadDeferredModel(fullPath, globals->get_props()); | |||
+ return object; | |||
+ } catch (const sg_throwable& t) { | |||
+ SG_LOG(SG_IO, SG_ALERT, "Error loading " << model_path << ":\n " << t.getFormattedMessage() << t.getOrigin()); | |||
+ return 0; | |||
+ } // error loading from absolute path | |||
+ SG_LOG(SG_IO, SG_ALERT, "canvas::Model: reading '" << path << "' denied"); | |||
+ } // absolute path handling | |||
+ else | |||
+ { | |||
+ SGPath tpath = globals->resolve_resource_path(path); | |||
+ if( !tpath.isNull() ) | |||
+ try { | |||
+ std::string fullPath = simgear::SGModelLib::findDataFile(path.c_str()); | |||
+ osg::Node * object = simgear::SGModelLib::loadDeferredModel(fullPath, globals->get_props()); | |||
+ return object; | |||
+ } catch (const sg_throwable& t) { | |||
+ SG_LOG(SG_IO, SG_ALERT, "Error loading " << model_path << ":\n " << t.getFormattedMessage() << t.getOrigin()); | |||
+ return 0; | |||
+ } // error loading from relative path | |||
+ | |||
+ SG_LOG(SG_IO, SG_ALERT, "canvas::Model: No such model: '" << path << "'"); | |||
+ } // relative path handling | |||
+ | |||
+ return 0; | |||
+ | |||
+ } | |||
</syntaxhighlight> | </syntaxhighlight> | ||