]> git.mxchange.org Git - flightgear.git/blobdiff - src/Cockpit/od_gauge.cxx
Positioned/Cache tweaks to support PoIs.
[flightgear.git] / src / Cockpit / od_gauge.cxx
index c6be269e4e5e5e3074c4b1d0d562f92914c92d36..4f569acb1af97733c67d753b7d51335301c99108 100644 (file)
@@ -36,6 +36,7 @@
 #include <osg/Camera>
 #include <osg/Geode>
 #include <osg/NodeVisitor>
+#include <osg/Material>
 #include <osg/Matrix>
 #include <osg/PolygonMode>
 #include <osg/ShadeModel>
 #include <simgear/scene/material/EffectGeode.hxx>
 #include <simgear/scene/util/RenderConstants.hxx>
 
+#include <Canvas/FGCanvasSystemAdapter.hxx>
 #include <Main/globals.hxx>
-#include <Viewer/renderer.hxx>
 #include <Scenery/scenery.hxx>
 #include "od_gauge.hxx"
 
 #include <cassert>
 
-//------------------------------------------------------------------------------
-FGODGauge::FGODGauge():
-  _size_x( -1 ),
-  _size_y( -1 ),
-  _view_width( -1 ),
-  _view_height( -1 ),
-  _use_image_coords( false ),
-  _use_stencil( false ),
-  _use_mipmapping( false ),
-  _coverage_samples( 0 ),
-  _color_samples( 0 ),
-  rtAvailable( false )
-{
-}
+static simgear::canvas::SystemAdapterPtr system_adapter(
+  new canvas::FGCanvasSystemAdapter
+);
 
 //------------------------------------------------------------------------------
-FGODGauge::~FGODGauge()
-{
-  if( camera.valid() )
-    globals->get_renderer()->removeCamera(camera.get());
-}
-
-//------------------------------------------------------------------------------
-void FGODGauge::setSize(int size_x, int size_y)
-{
-  _size_x = size_x;
-  _size_y = size_y < 0 ? size_x : size_y;
-
-  if( texture.valid() )
-    texture->setTextureSize(_size_x, _size_x);
-}
-
-//----------------------------------------------------------------------------
-void FGODGauge::setViewSize(int width, int height)
+FGODGauge::FGODGauge()
 {
-  _view_width = width;
-  _view_height = height < 0 ? width : height;
-
-  if( camera )
-    updateCoordinateFrame();
+  setSystemAdapter(system_adapter);
 }
 
 //------------------------------------------------------------------------------
-void FGODGauge::useImageCoords(bool use)
-{
-  if( use == _use_image_coords )
-    return;
-
-  _use_image_coords = use;
-
-  if( texture )
-    updateCoordinateFrame();
-}
-
-//------------------------------------------------------------------------------
-void FGODGauge::useStencil(bool use)
-{
-  if( use == _use_stencil )
-    return;
-
-  _use_stencil = use;
-
-  if( texture )
-    updateStencil();
-}
-
-//------------------------------------------------------------------------------
-void FGODGauge::setSampling( bool mipmapping,
-                             int coverage_samples,
-                             int color_samples )
-{
-  if(    _use_mipmapping == mipmapping
-      && _coverage_samples == coverage_samples
-      && _color_samples == color_samples )
-    return;
-
-  _use_mipmapping = mipmapping;
-
-  if( color_samples > coverage_samples )
-  {
-    SG_LOG
-    (
-      SG_GL,
-      SG_WARN,
-      "FGODGauge::setSampling: color_samples > coverage_samples not allowed!"
-    );
-    color_samples = coverage_samples;
-  }
-
-  _coverage_samples = coverage_samples;
-  _color_samples = color_samples;
-
-  updateSampling();
-}
-
-//------------------------------------------------------------------------------
-void FGODGauge::setRender(bool render)
-{
-  // Only the far camera should trigger this texture to be rendered.
-  camera->setNodeMask(render ? simgear::BACKGROUND_BIT : 0);
-}
-
-//------------------------------------------------------------------------------
-bool FGODGauge::serviceable(void) 
-{
-  return rtAvailable;
-}
-
-//------------------------------------------------------------------------------
-void FGODGauge::allocRT(osg::NodeCallback* camera_cull_callback)
-{
-  camera = new osg::Camera;
-  camera->setDataVariance(osg::Object::DYNAMIC);
-  camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
-  camera->setRenderOrder(osg::Camera::PRE_RENDER);
-  camera->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f , 0.0f));
-  camera->setClearStencil(0);
-  camera->setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT,
-                                             osg::Camera::FRAME_BUFFER );
-
-  if( camera_cull_callback )
-    camera->setCullCallback(camera_cull_callback);
-
-  setRender(true);
-  updateCoordinateFrame();
-  updateStencil();
-
-  osg::StateSet* stateSet = camera->getOrCreateStateSet();
-  stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
-  stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
-  stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
-  stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
-  stateSet->setAttributeAndModes(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,
-          osg::PolygonMode::FILL),
-          osg::StateAttribute::ON);
-  stateSet->setAttributeAndModes(new osg::AlphaFunc(osg::AlphaFunc::GREATER,
-          0.0f),
-          osg::StateAttribute::ON);
-  stateSet->setAttribute(new osg::ShadeModel(osg::ShadeModel::FLAT));
-  stateSet->setAttributeAndModes(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA,
-          osg::BlendFunc::ONE_MINUS_SRC_ALPHA),
-          osg::StateAttribute::ON);
-  if( !texture )
-  {
-    texture = new osg::Texture2D;
-    texture->setTextureSize(_size_x, _size_y);
-    texture->setInternalFormat(GL_RGBA);
-  }
-
-  updateSampling();
-
-  globals->get_renderer()->addCamera(camera.get(), false);
-  rtAvailable = true;
-}
-
-//------------------------------------------------------------------------------
-void FGODGauge::updateCoordinateFrame()
-{
-  assert( camera );
-
-  if( _view_width < 0 )
-    _view_width = _size_x;
-  if( _view_height < 0 )
-    _view_height = _size_y;
-
-  camera->setViewport(0, 0, _size_x, _size_y);
-
-  if( _use_image_coords )
-    camera->setProjectionMatrix(
-      osg::Matrix::ortho2D(0, _view_width, _view_height, 0)
-    );
-  else
-    camera->setProjectionMatrix(
-      osg::Matrix::ortho2D( -_view_width/2.,  _view_width/2.,
-                            -_view_height/2., _view_height/2. )
-    );
-}
-
-//------------------------------------------------------------------------------
-void FGODGauge::updateStencil()
+FGODGauge::~FGODGauge()
 {
-  assert( camera );
-
-  GLbitfield mask = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
-
-  if( _use_stencil)
-  {
-    camera->attach( osg::Camera::PACKED_DEPTH_STENCIL_BUFFER,
-                     GL_DEPTH_STENCIL_EXT );
-    mask |= GL_STENCIL_BUFFER_BIT;
-  }
-  else
-  {
-    camera->detach(osg::Camera::PACKED_DEPTH_STENCIL_BUFFER);
-  }
-
-  camera->setClearMask(mask);
-}
 
-//------------------------------------------------------------------------------
-void FGODGauge::updateSampling()
-{
-  assert( camera );
-  assert( texture );
-
-  texture->setFilter(
-    osg::Texture2D::MIN_FILTER,
-    _use_mipmapping ? osg::Texture2D::LINEAR_MIPMAP_LINEAR
-                    : osg::Texture2D::LINEAR
-  );
-  camera->attach(
-    osg::Camera::COLOR_BUFFER,
-    texture.get(),
-    0, 0,
-    _use_mipmapping,
-    _coverage_samples,
-    _color_samples
-  );
 }
 
 /**
@@ -282,6 +79,9 @@ class ReplaceStaticTextureVisitor:
 {
   public:
 
+    typedef osg::ref_ptr<osg::Group> GroupPtr;
+    typedef osg::ref_ptr<osg::Material> MaterialPtr;
+
     ReplaceStaticTextureVisitor( const char* name,
                                  osg::Texture2D* new_texture ):
         osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
@@ -289,7 +89,7 @@ class ReplaceStaticTextureVisitor:
         _new_texture(new_texture)
     {}
 
-    ReplaceStaticTextureVisitor( const SGPropertyNode* placement,
+    ReplaceStaticTextureVisitor( SGPropertyNode* placement,
                                  osg::Texture2D* new_texture,
                                  osg::NodeCallback* cull_callback = 0 ):
         osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
@@ -298,6 +98,7 @@ class ReplaceStaticTextureVisitor:
         ),
         _node_name( placement->getStringValue("node") ),
         _parent_name( placement->getStringValue("parent") ),
+        _node(placement),
         _new_texture(new_texture),
         _cull_callback(cull_callback)
     {
@@ -317,7 +118,7 @@ class ReplaceStaticTextureVisitor:
      * Get a list of groups which have been inserted into the scene graph to
      * replace the given texture
      */
-    canvas::Placements& getPlacements()
+    simgear::canvas::Placements& getPlacements()
     {
       return _placements;
     }
@@ -383,7 +184,7 @@ class ReplaceStaticTextureVisitor:
 
         // insert a new group between the geode an it's parent which overrides
         // the texture
-        osg::ref_ptr<osg::Group> group = new osg::Group;
+        GroupPtr group = new osg::Group;
         group->setName("canvas texture group");
         group->addChild(eg);
         parent->removeChild(eg);
@@ -392,16 +193,16 @@ class ReplaceStaticTextureVisitor:
         if( _cull_callback )
           group->setCullCallback(_cull_callback);
 
-        _placements.push_back(
-          canvas::PlacementPtr(new ObjectPlacement(group))
-        );
-
         osg::StateSet* stateSet = group->getOrCreateStateSet();
         stateSet->setTextureAttribute( unit, _new_texture,
                                              osg::StateAttribute::OVERRIDE );
         stateSet->setTextureMode( unit, GL_TEXTURE_2D,
                                         osg::StateAttribute::ON );
 
+        _placements.push_back( simgear::canvas::PlacementPtr(
+          new ObjectPlacement(_node, group)
+        ));
+
         SG_LOG
         (
           SG_GL,
@@ -418,12 +219,52 @@ class ReplaceStaticTextureVisitor:
   protected:
 
     class ObjectPlacement:
-      public canvas::Placement
+      public simgear::canvas::Placement
     {
       public:
-        ObjectPlacement(osg::ref_ptr<osg::Group> group):
+
+        ObjectPlacement( SGPropertyNode* node,
+                         GroupPtr group ):
+          Placement(node),
           _group(group)
-        {}
+        {
+          // TODO make more generic and extendable for more properties
+          if( node->hasValue("emission") )
+            setEmission( node->getFloatValue("emission") );
+        }
+
+        virtual bool childChanged(SGPropertyNode* node)
+        {
+          if( node->getParent() != _node )
+            return false;
+
+          if( node->getNameString() == "emission" )
+            setEmission( node->getFloatValue() );
+          else
+            return false;
+
+          return true;
+        }
+
+        void setEmission(float emit)
+        {
+          emit = SGMiscf::clip(emit, 0, 1);
+
+          if( !_material )
+          {
+            _material = new osg::Material;
+            _material->setColorMode(osg::Material::OFF);
+            _material->setDataVariance(osg::Object::DYNAMIC);
+            _group->getOrCreateStateSet()
+                  ->setAttribute(_material, ( osg::StateAttribute::ON
+                                            | osg::StateAttribute::OVERRIDE ) );
+          }
+
+          _material->setEmission(
+            osg::Material::FRONT_AND_BACK,
+            osg::Vec4(emit, emit, emit, emit)
+          );
+        }
 
         /**
          * Remove placement from the scene
@@ -444,22 +285,26 @@ class ReplaceStaticTextureVisitor:
         }
 
       private:
-        osg::ref_ptr<osg::Group> _group;
+        GroupPtr            _group;
+        MaterialPtr         _material;
     };
 
     std::string _tex_name,      ///<! Name of texture to be replaced
                 _node_name,     ///<! Only replace if node name matches
                 _parent_name;   ///<! Only replace if any parent node matches
                                 ///   given name (all the tree upwards)
+
+    SGPropertyNode_ptr  _node;
     osg::Texture2D     *_new_texture;
     osg::NodeCallback  *_cull_callback;
 
-    canvas::Placements _placements;
+    simgear::canvas::Placements _placements;
 };
 
 //------------------------------------------------------------------------------
-canvas::Placements FGODGauge::set_texture( const char* name,
-                                           osg::Texture2D* new_texture )
+simgear::canvas::Placements
+FGODGauge::set_texture( const char* name,
+                        osg::Texture2D* new_texture )
 {
   osg::Group* root = globals->get_scenery()->get_aircraft_branch();
   ReplaceStaticTextureVisitor visitor(name, new_texture);
@@ -468,9 +313,10 @@ canvas::Placements FGODGauge::set_texture( const char* name,
 }
 
 //------------------------------------------------------------------------------
-canvas::Placements FGODGauge::set_texture( const SGPropertyNode* placement,
-                                           osg::Texture2D* new_texture,
-                                           osg::NodeCallback* cull_callback )
+simgear::canvas::Placements
+FGODGauge::set_texture( SGPropertyNode* placement,
+                        osg::Texture2D* new_texture,
+                        osg::NodeCallback* cull_callback )
 {
   osg::Group* root = globals->get_scenery()->get_aircraft_branch();
   ReplaceStaticTextureVisitor visitor(placement, new_texture, cull_callback);