Canvas view camera element: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
m (→‎Roadmap: add original proof-of-concept patches for future reference (should probably use bugman's collapsible template?))
(15 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
|image      = cvcanvas-777.jpeg|Demonstration of multiple view windows in flightgear
|started    = 11/2020
|name        = Canvas Camera Views
|name        = Canvas Camera Views
|description = Rendering arbitrary views to a texture/Canvas
|description = Rendering arbitrary scene views to a texture/Canvas
|status      = planned/prototype
|status      = experimental prototype
|maintainers = none
|maintainers = none
|developers  = none
|developers  = Cgdae,  Icecode
}}
}}


Line 11: Line 27:
Several aircraft developers have manifested their interest in being able to render scene views to a texture (RTT) {{Wikipedia|Render target}} and use it inside cockpits as mirrors, external cameras (so called tail cams) and other uses.
Several aircraft developers have manifested their interest in being able to render scene views to a texture (RTT) {{Wikipedia|Render target}} and use it inside cockpits as mirrors, external cameras (so called tail cams) and other uses.


With the [[Compositor]] framework it's possible to configure custom render pipelines in XML space, which together with Effect schemes allow complete control over how a rendered scene looks. A synthetic terrain can be rendered by assigning an Effect scheme to a <tt>scene</tt> type pass, which textures all terrain with a brown checkerboard pattern and paints the sky in a solid blue color. All other objects are ignored and aren't rendered, saving a lot of computation time. Custom render distances independent from the main LOD settings can also be used to further optimize the performance of the display.
With the [[Compositor]] framework it's possible to configure custom render pipelines in XML space, which together with Effect schemes allow complete control over how a rendered scene looks. A synthetic terrain can be rendered by assigning an Effect scheme to a <tt>scene</tt> type pass, which textures all terrain with a brown checkerboard pattern and paints the sky in a solid blue color. All other objects are ignored and aren't rendered, saving a lot of computation time. Custom render distances independent from the main LOD settings can also be used to further optimize the performance of the display.<ref>https://forum.flightgear.org/viewtopic.php?f=71&t=23929&p=318025&#p317946</ref>


== Use Cases ==
== Use Cases ==
Line 22: Line 38:


== Proof of Concept ==
== Proof of Concept ==
{{Note|This is based on code originally provided by F-JJTH, and subsequently reworked by Icecode GL and Hooray back in 2016}}
{{Main article|Hackathon Proposal: CompositeViewer and Canvas}}
 
<!--
{{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 249: Line 271:


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


</syntaxhighlight>
</syntaxhighlight>


 
fgdata
<syntaxhighlight lang="diff">diff --git a/Nasal/canvas/api.nas b/Nasal/canvas/api.nas
<syntaxhighlight lang="diff">
diff --git a/Nasal/canvas/api.nas b/Nasal/canvas/api.nas
index 3f7f5a17..433a41f9 100644
index 3f7f5a17..433a41f9 100644
--- a/Nasal/canvas/api.nas
--- a/Nasal/canvas/api.nas
Line 410: Line 435:
  # Canvas
  # Canvas
</syntaxhighlight>
</syntaxhighlight>
-->


== Roadmap ==
== Roadmap ==
Line 427: Line 453:


=== Create a Canvas Element subclass {{Pending}} ===
=== Create a Canvas Element subclass {{Pending}} ===
{{Main article|Canvas_Development#Adding_a_new_Element}}
{{See also|Talk:Hackathon Proposal: CompositeViewer and Canvas}}


This derived class would require the following configuration:
This derived class would require the following configuration:
Line 433: Line 459:
* [[View manager|View]] to use. This could be one of the "main" ones (i.e. the ones on the main property tree), or a locally-defined one that is only known to this Canvas Element (think FLIR).
* [[View manager|View]] to use. This could be one of the "main" ones (i.e. the ones on the main property tree), or a locally-defined one that is only known to this Canvas Element (think FLIR).
* Scene graph to use. By default the main scene graph would be used, but an arbitrary XML file can be loaded to [[Howto:Extending Canvas to support rendering 3D models|render a custom model]]. A typical use-case would be instruments that need to [[Shuttle ADI ball|manipulate/render a 3D object]] (for which we have working code, too)
* Scene graph to use. By default the main scene graph would be used, but an arbitrary XML file can be loaded to [[Howto:Extending Canvas to support rendering 3D models|render a custom model]]. A typical use-case would be instruments that need to [[Shuttle ADI ball|manipulate/render a 3D object]] (for which we have working code, too)
* Several optimization/miscellaneous parameters like framerate cap, etc.
* Several optimization/miscellaneous parameters like framerate cap, node masks, LOD etc. <ref>https://forum.flightgear.org/viewtopic.php?f=71&t=32845&p=318027&hilit=canvas+draw+masks#p318028</ref>


Simplifying a lot, this Canvas Element would be an aggregation of a Compositor instance, a View and a pointer to a <tt>osg::Group</tt> representing the scene graph to render. This setup could be replicated in {{flightgear file|src/Viewer/fg_os_osgviewer.cxx}}, with the difference of ignoring Canvas and using native windowing features from OSG.
Simplifying a lot, this Canvas Element would be an aggregation of a Compositor instance, a View and a pointer to a <tt>osg::Group</tt> representing the scene graph to render. This setup could be replicated in {{flightgear file|src/Viewer/fg_os_osgviewer.cxx}}, with the difference of ignoring Canvas and using native windowing features from OSG.
Line 442: Line 468:
* {{forum link|t=32846|text=Canvas::View Development}}
* {{forum link|t=32846|text=Canvas::View Development}}
* {{forum link|t=23929|text=Gear View in Cockpit}}
* {{forum link|t=23929|text=Gear View in Cockpit}}
* {{forum link|t=32845|text=Canvas::View element: performance/optimizations}}
* {{forum link|t=20057|text=Rear-View mirror}}
* {{forum link|t=20057|text=Rear-View mirror}}
* {{forum link|t=18905|text=Progress on synthetic terrain}}
* {{forum link|t=18905|text=Progress on synthetic terrain}}
* {{forum link|t=17184|text=Instruments with heightmaps}}
* {{forum link|t=17184|text=Instruments with heightmaps}}
== References ==
{{Appendix}}


[[Category:Canvas Element Proposals]]
[[Category:Canvas Element Proposals]]

Revision as of 17:52, 16 November 2020

Cquote1.png It would make sense to integrate all efforts (CompositeViewer, Canvas and Compositor) to create a Canvas Element that can render an 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.


— Fernando García Liñán  (2020-08-20 14:27:58). Independent view windows.
(powered by Instant-Cquotes)
Cquote2.png
Canvas Camera Views
Cvcanvas-777.jpeg
Started in 11/2020
Description Rendering arbitrary scene views to a texture/Canvas
Maintainer(s) none
Contributor(s) Cgdae, Icecode
Status experimental prototype

Background

Several aircraft developers have manifested their interest in being able to render scene views to a texture (RTT) Render target This is a link to a Wikipedia article and use it inside cockpits as mirrors, external cameras (so called tail cams) and other uses.

With the Compositor framework it's possible to configure custom render pipelines in XML space, which together with Effect schemes allow complete control over how a rendered scene looks. A synthetic terrain can be rendered by assigning an Effect scheme to a scene type pass, which textures all terrain with a brown checkerboard pattern and paints the sky in a solid blue color. All other objects are ignored and aren't rendered, saving a lot of computation time. Custom render distances independent from the main LOD settings can also be used to further optimize the performance of the display.[1]

Use Cases

  • Tail Cams (gear view)
  • Mirrors
  • Dynamic in-sim view configuration
  • On demand creation of views and windows (e.g. FGCamera previews)
  • Missile/payload views
  • Prototyping/testing HUDs or PFDs requiring synthetic terrain to work properly

Proof of Concept

1rightarrow.png See Hackathon Proposal: CompositeViewer and Canvas for the main article about this subject.


Roadmap

Use CompositeViewer Pending Pending

Currently FlightGear uses only one instance of osg::Viewer, which is used by CameraGroup to manage the slave cameras. Supporting CompositeViewer would require modifying flightgear/src/Viewer/fg_os_osgviewer.cxx and creating some kind of wrapper class that manages the CompositeViewer instance and assigns a CameraGroup to each osg::View. It's also important to note that currently all FG subsystems assume there is a single instance of CameraGroup.

Standardize the View manager Pending Pending

The first step would be to port the view code to SimGear so it can be used and known by the Compositor and Canvas. The view manager (flightgear/src/Viewer/viewmgr.cxx) currently has some hardcoded assumptions, so it would either need to be rewritten to remove them or a new interface for the Views could be created specifically for the Canvas Camera View.

In FlightGear, the Canvas system (which resides in SimGear) is integrated using the equivalent of a FGCanvasSystemAdapter, which provides all FG APIs to SimGear and makes the Canvas system available inside FG: flightgear/src/Canvas/FGCanvasSystemAdapter.cxx

Instead of using tied properties, the adapted view code would either used propertyObject<> or the propertyBasedMgr abstraction, so that the corresponding canvas element can continue to use well-known conventions to manipulate views: (flightgear/src/Viewer/view.cxx)

Create a Canvas Element subclass Pending Pending

This derived class would require the following configuration:

  • Compositor to use.
  • View to use. This could be one of the "main" ones (i.e. the ones on the main property tree), or a locally-defined one that is only known to this Canvas Element (think FLIR).
  • Scene graph to use. By default the main scene graph would be used, but an arbitrary XML file can be loaded to render a custom model. A typical use-case would be instruments that need to manipulate/render a 3D object (for which we have working code, too)
  • Several optimization/miscellaneous parameters like framerate cap, node masks, LOD etc. [2]

Simplifying a lot, this Canvas Element would be an aggregation of a Compositor instance, a View and a pointer to a osg::Group representing the scene graph to render. This setup could be replicated in flightgear/src/Viewer/fg_os_osgviewer.cxx, with the difference of ignoring Canvas and using native windowing features from OSG.

Related

Forum topics

References

References