Canvas view camera element: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{FGCquote
  |It would make sense to integrate all efforts ([[CompositeViewer Support|CompositeViewer]], [[Canvas]] and [[Compositor]]) to create a [[Canvas_Development#Elements|Canvas Element]] that can render an [[Canvas_Development#Supporting_Cameras|arbitrary view]].
Some time ago I wrote what I think are the required steps to get something like cockpit mirrors working: [[#Roadmap]]
The main showstopper was having [[CompositeViewer Support]], but since that effort is underway, all the parts are there for someone willing to dive in.
  |{{cite web |url=https://sourceforge.net/p/flightgear/mailman/message/37089780/
    |title=<nowiki>Independent view windows</nowiki>
    |author=<nowiki>Fernando García Liñán </nowiki>
    |date=<nowiki>2020-08-20 14:27:58</nowiki>
  }}
}}
{{infobox subsystem
{{infobox subsystem
|name        = Canvas Camera Views
|name        = Canvas Camera Views
Line 22: Line 35:


== Proof of Concept ==
== Proof of Concept ==
{{Note|This is inspired by the long standing idea to render camera views to a texture for use as tailcams, gear views etc <ref>https://forum.flightgear.org/viewtopic.php?f=71&t=23929</ref>, it's based on code originally provided by F-JJTH<ref>https://forum.flightgear.org/viewtopic.php?f=71&t=23929#p317448</ref>, and subsequently reworked by Icecode GL and Hooray to integrate it with the Canvas system back in 2017<ref>https://forum.flightgear.org/viewtopic.php?f=47&t=32846&p=318046</ref>}}
{{Note|This is inspired by the long standing idea to render camera views to a texture for use as tailcams, gear views etc <ref>https://forum.flightgear.org/viewtopic.php?f=71&t=23929</ref>, it's based on code originally provided by F-JJTH<ref>https://forum.flightgear.org/viewtopic.php?f=71&t=23929#p317448</ref>, and subsequently reworked by Icecode GL and Hooray to integrate it with the Canvas system back in 2017<ref>https://forum.flightgear.org/viewtopic.php?f=47&t=32846&p=318046</ref>
At the time, the main issue was that the new view element needed to be explicitly updated, but that should no longer be the case thanks to James' work on the setPixel() API <ref>https://sourceforge.net/p/flightgear/mailman/message/36926486/</ref> which also means that sc::Image now has a <code>dirtyPixels()</code> API which we can reuse <ref>https://sourceforge.net/p/flightgear/simgear/ci/ad3621e23b4056e1be7e8d9eb8dd4d513455add8/</ref>
Due to the adoption of the [[Compositor]] framework, this will probably need to reworked to be properly integrated.
Furthermore, beginning in mid 2020 Julian Smith has started working on optional [[CompositeViewer Support]] so that independent scene views can be rendered.  }}


SimGear:
SimGear:
Line 251: Line 267:
<syntaxhighlight lang="diff">     
<syntaxhighlight lang="diff">     
diff --git a/src/Canvas/FGCanvasSystemAdapter.cxx b/src/Canvas/FGCanvasSystemAdapter.cxx
diff --git a/src/Canvas/FGCanvasSystemAdapter.cxx b/src/Canvas/FGCanvasSystemAdapter.cxx
    index 72b20747e..9e04dbd14 100644
index 72b20747e..9e04dbd14 100644
    --- a/src/Canvas/FGCanvasSystemAdapter.cxx
--- a/src/Canvas/FGCanvasSystemAdapter.cxx
    +++ b/src/Canvas/FGCanvasSystemAdapter.cxx
+++ b/src/Canvas/FGCanvasSystemAdapter.cxx
    @@ -23,9 +23,18 @@
@@ -23,9 +23,18 @@
    #include <Network/HTTPClient.hxx>
#include <Network/HTTPClient.hxx>
    #include <Viewer/renderer.hxx>
#include <Viewer/renderer.hxx>
   
 
    +
+
    +#include <Viewer/view.hxx>
+#include <Viewer/view.hxx>
    +#include <Viewer/viewmgr.hxx>
+#include <Viewer/viewmgr.hxx>
    +#include <simgear/scene/util/OsgMath.hxx>
+#include <simgear/scene/util/OsgMath.hxx>
    +
+
    +
+
    +
+
    #include <osgDB/ReadFile>
#include <osgDB/ReadFile>
    #include <stdexcept>
#include <stdexcept>
   
 
    +#include <simgear/scene/model/modellib.hxx>
+#include <simgear/scene/model/modellib.hxx>
    +
+
    namespace canvas
namespace canvas
    {
{
      //----------------------------------------------------------------------------
//----------------------------------------------------------------------------
    @@ -64,9 +73,9 @@ namespace canvas
@@ -64,9 +73,9 @@ namespace canvas
      }
}
   
 
      //----------------------------------------------------------------------------
//----------------------------------------------------------------------------
    -  void FGCanvasSystemAdapter::addCamera(osg::Camera* camera) const
-  void FGCanvasSystemAdapter::addCamera(osg::Camera* camera) const
    +  void FGCanvasSystemAdapter::addCamera(osg::Camera* camera, bool useSceneData) const
+  void FGCanvasSystemAdapter::addCamera(osg::Camera* camera, bool useSceneData) const
      {
{
    -    globals->get_renderer()->addCamera(camera, false);
-    globals->get_renderer()->addCamera(camera, false);
    +    globals->get_renderer()->addCamera(camera, useSceneData);
+    globals->get_renderer()->addCamera(camera, useSceneData);
      }
}
   
 
      //----------------------------------------------------------------------------
//----------------------------------------------------------------------------
    @@ -100,6 +109,46 @@ namespace canvas
@@ -100,6 +109,46 @@ namespace canvas
        return 0;
return 0;
      }
}
   
 
    +    //----------------------------------------------------------------------------
+    //----------------------------------------------------------------------------
    +    // From http://wiki.flightgear.org/Howto:Extending_Canvas_to_support_rendering_3D_models#Extending_FGCanvasSystemAdapter
+    // From http://wiki.flightgear.org/Howto:Extending_Canvas_to_support_rendering_3D_models#Extending_FGCanvasSystemAdapter
    +    osg::Node* FGCanvasSystemAdapter::getModel(const std::string& path) const
+    osg::Node* FGCanvasSystemAdapter::getModel(const std::string& path) const
    +    {
+    {
    +        const char *model_path = "Models/Geometry/glider.ac";
+        const char *model_path = "Models/Geometry/glider.ac";
    +        SGPath p(SGPath::fromUtf8(path));
+        SGPath p(SGPath::fromUtf8(path));
    +
+
    +        if( p.isAbsolute() )
+        if( p.isAbsolute() )
    +        {
+        {
    +            SGPath valid_path = fgValidatePath(p, false);
+            SGPath valid_path = fgValidatePath(p, false);
    +            if( !valid_path.isNull() )
+            if( !valid_path.isNull() )
    +            try {
+            try {
    +                std::string fullPath = simgear::SGModelLib::findDataFile(valid_path.local8BitStr());
+                std::string fullPath = simgear::SGModelLib::findDataFile(valid_path.local8BitStr());
    +                osg::Node * object = simgear::SGModelLib::loadDeferredModel(fullPath, globals->get_props());
+                osg::Node * object = simgear::SGModelLib::loadDeferredModel(fullPath, globals->get_props());
    +                return object;
+                return object;
    +            } catch (const sg_throwable& t) {
+            } catch (const sg_throwable& t) {
    +                SG_LOG(SG_IO, SG_ALERT, "Error loading " << model_path << ":\n  " << t.getFormattedMessage() << t.getOrigin());
+                SG_LOG(SG_IO, SG_ALERT, "Error loading " << model_path << ":\n  " << t.getFormattedMessage() << t.getOrigin());
    +                return 0;
+                return 0;
    +            } // error loading from absolute path
+            } // error loading from absolute path
    +            SG_LOG(SG_IO, SG_ALERT, "canvas::Model: reading '" << path << "' denied");
+            SG_LOG(SG_IO, SG_ALERT, "canvas::Model: reading '" << path << "' denied");
    +        } // absolute path handling
+        } // absolute path handling
    +        else
+        else
    +        {
+        {
    +            SGPath tpath = globals->resolve_resource_path(path);
+            SGPath tpath = globals->resolve_resource_path(path);
    +            if( !tpath.isNull() )
+            if( !tpath.isNull() )
    +            try {
+            try {
    +                std::string fullPath = simgear::SGModelLib::findDataFile(path.c_str());
+                std::string fullPath = simgear::SGModelLib::findDataFile(path.c_str());
    +                osg::Node * object = simgear::SGModelLib::loadDeferredModel(fullPath, globals->get_props());
+                osg::Node * object = simgear::SGModelLib::loadDeferredModel(fullPath, globals->get_props());
    +                return object;
+                return object;
    +            } catch (const sg_throwable& t) {
+            } catch (const sg_throwable& t) {
    +                SG_LOG(SG_IO, SG_ALERT, "Error loading " << model_path << ":\n  " << t.getFormattedMessage() << t.getOrigin());
+                SG_LOG(SG_IO, SG_ALERT, "Error loading " << model_path << ":\n  " << t.getFormattedMessage() << t.getOrigin());
    +                return 0;
+                return 0;
    +            } // error loading from relative path
+            } // error loading from relative path
    +
+
    +            SG_LOG(SG_IO, SG_ALERT, "canvas::Model: No such model: '" << path << "'");
+            SG_LOG(SG_IO, SG_ALERT, "canvas::Model: No such model: '" << path << "'");
    +        } // relative path handling
+        } // relative path handling
    +
+
    +        return 0;
+        return 0;
    +    }
+    }
    +
+
      //----------------------------------------------------------------------------
//----------------------------------------------------------------------------
      SGSubsystem*
SGSubsystem*
      FGCanvasSystemAdapter::getSubsystem(const std::string& name) const
FGCanvasSystemAdapter::getSubsystem(const std::string& name) const
    @@ -121,4 +170,16 @@ namespace canvas
@@ -121,4 +170,16 @@ namespace canvas
        return 0;
return 0;
      }
}
   
 
    +  osg::Matrix FGCanvasSystemAdapter::getViewMatrix(int view_number, double dt) const
+  osg::Matrix FGCanvasSystemAdapter::getViewMatrix(int view_number, double dt) const
    +  {
+  {
    +        flightgear::View *view = globals->get_viewmgr()->get_view(view_number);
+        flightgear::View *view = globals->get_viewmgr()->get_view(view_number);
    +        view->update(dt);
+        view->update(dt);
    +
+
    +        osg::Vec3f position( toOsg(view->getViewPosition()) );
+        osg::Vec3f position( toOsg(view->getViewPosition()) );
    +        osg::Quat orientation( toOsg(view->getViewOrientation()) );
+        osg::Quat orientation( toOsg(view->getViewOrientation()) );
    +        osg::Matrix viewMatrix( osg::Matrix::translate(-position) * osg::Matrix::rotate(orientation.inverse()) );
+        osg::Matrix viewMatrix( osg::Matrix::translate(-position) * osg::Matrix::rotate(orientation.inverse()) );
    +
+
    +        return viewMatrix;
+        return viewMatrix;
    +  }
+  }
    +
+
    }
}
    diff --git a/src/Canvas/FGCanvasSystemAdapter.hxx b/src/Canvas/FGCanvasSystemAdapter.hxx
diff --git a/src/Canvas/FGCanvasSystemAdapter.hxx b/src/Canvas/FGCanvasSystemAdapter.hxx
    index 4c1fd6210..24ececb41 100644
index 4c1fd6210..24ececb41 100644
    --- a/src/Canvas/FGCanvasSystemAdapter.hxx
--- a/src/Canvas/FGCanvasSystemAdapter.hxx
    +++ b/src/Canvas/FGCanvasSystemAdapter.hxx
+++ b/src/Canvas/FGCanvasSystemAdapter.hxx
    @@ -28,11 +28,13 @@ namespace canvas
@@ -28,11 +28,13 @@ namespace canvas
      {
{
        public:
public:
          virtual simgear::canvas::FontPtr getFont(const std::string& name) const;
virtual simgear::canvas::FontPtr getFont(const std::string& name) const;
    -      virtual void addCamera(osg::Camera* camera) const;
-      virtual void addCamera(osg::Camera* camera) const;
    +      virtual void addCamera(osg::Camera* camera, bool useSceneData = false) const;
+      virtual void addCamera(osg::Camera* camera, bool useSceneData = false) const;
          virtual void removeCamera(osg::Camera* camera) const;
virtual void removeCamera(osg::Camera* camera) const;
          virtual osg::ref_ptr<osg::Image> getImage(const std::string& path) const;
virtual osg::ref_ptr<osg::Image> getImage(const std::string& path) const;
    +      virtual osg::Node* getModel(const std::string& path) const;
+      virtual osg::Node* getModel(const std::string& path) const;
          virtual SGSubsystem* getSubsystem(const std::string& name) const;
virtual SGSubsystem* getSubsystem(const std::string& name) const;
          virtual simgear::HTTP::Client* getHTTPClient() const;
virtual simgear::HTTP::Client* getHTTPClient() const;
    +      virtual osg::Matrix getViewMatrix(int view_number, double dt) const;
+      virtual osg::Matrix getViewMatrix(int view_number, double dt) const;
      };
};
    }
}
   
 
    diff --git a/src/Viewer/renderer.cxx b/src/Viewer/renderer.cxx
diff --git a/src/Viewer/renderer.cxx b/src/Viewer/renderer.cxx
    index d9e8f351f..b9b3fc6a0 100644
index d9e8f351f..b9b3fc6a0 100644
    --- a/src/Viewer/renderer.cxx
--- a/src/Viewer/renderer.cxx
    +++ b/src/Viewer/renderer.cxx
+++ b/src/Viewer/renderer.cxx
    @@ -1845,6 +1845,8 @@ FGRenderer::setEventHandler(FGEventHandler* eventHandler_)
@@ -1845,6 +1845,8 @@ FGRenderer::setEventHandler(FGEventHandler* eventHandler_)
    void
void
    FGRenderer::addCamera(osg::Camera* camera, bool useSceneData)
FGRenderer::addCamera(osg::Camera* camera, bool useSceneData)
    {
{
    +    if (useSceneData)
+    if (useSceneData)
    +        camera->addChild(globals->get_scenery()->get_scene_graph());
+        camera->addChild(globals->get_scenery()->get_scene_graph());
        _viewerSceneRoot->addChild(camera);
_viewerSceneRoot->addChild(camera);
    }
}
 


</syntaxhighlight>
</syntaxhighlight>


 
fgdata
<syntaxhighlight lang="diff">
<syntaxhighlight lang="diff">
diff --git a/Nasal/canvas/api.nas b/Nasal/canvas/api.nas
diff --git a/Nasal/canvas/api.nas b/Nasal/canvas/api.nas

Navigation menu