]> git.mxchange.org Git - flightgear.git/commitdiff
Allow filling paths and do some clean up/fixing.
authorThomas Geymayer <tomgey@gmail.com>
Wed, 6 Jun 2012 22:11:10 +0000 (00:11 +0200)
committerJames Turner <zakalawe@mac.com>
Sun, 17 Jun 2012 20:14:00 +0000 (21:14 +0100)
src/Canvas/CMakeLists.txt
src/Canvas/ShivaVG/src/CMakeLists.txt
src/Canvas/ShivaVG/src/shVgu.c
src/Canvas/canvas.cxx
src/Canvas/canvas.hxx
src/Canvas/elements/element.cxx
src/Canvas/elements/element.hxx
src/Canvas/elements/group.cxx
src/Canvas/elements/path.cxx
src/Canvas/property_helper.hxx
src/Main/CMakeLists.txt

index 325bd791d820893c4bc2aa727eb005f842d35328..6927c04a4b510d5b03ba4984dcecbec376e19b12 100644 (file)
@@ -19,7 +19,7 @@ set(HEADERS
   elements/text.hxx
   property_helper.hxx
 )
-    
+
 flightgear_component(Canvas "${SOURCES}" "${HEADERS}")
 
 add_subdirectory(ShivaVG/src)
\ No newline at end of file
index 4d7acf539add8a818307e9209ff6e211bf971ad1..63dcdef78820879ada63f52e07752f65ba36816a 100644 (file)
@@ -1,12 +1,14 @@
-#SET(LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}/lib")
+include(CheckIncludeFile)
+check_include_file("inttypes.h" HAVE_INTTYPES_H)
 
-ADD_DEFINITIONS(
-    -DVG_API_EXPORT
-    -DHAVE_INTTYPES_H
-)
+if(HAVE_INTTYPES_H)
+  add_definitions(-DHAVE_INTTYPES_H)
+endif()
+add_definitions(-DVG_API_EXPORT)
 
 INCLUDE_DIRECTORIES(
-    ${OPENGL_INCLUDE_DIR}
+  ${OPENGL_INCLUDE_DIR}
+  ../include
 )
 
 SET(INCROOT ../include/VG)
@@ -43,11 +45,4 @@ TARGET_LINK_LIBRARIES(
     ShivaVG
     ${OPENGL_gl_LIBRARY}
     ${OPENGL_glu_LIBRARY}
-)
-
-#INSTALL(
-#    TARGETS OpenVG
-#    RUNTIME DESTINATION bin COMPONENT bin
-#    LIBRARY DESTINATION lib${LIB_SUFFIX} COMPONENT bin
-#    ARCHIVE DESTINATION lin${LIB_SUFFIX} COMPONENT devel
-#)
+)
\ No newline at end of file
index f42e819b1aca7f900b8d0a8bc3c27de64063d727..30cb35e26b7d90fe8878c380f09ae8d86eba59a2 100644 (file)
@@ -317,21 +317,23 @@ VGU_API_CALL VGUErrorCode vguComputeWarpQuadToSquare(VGfloat sx0, VGfloat sy0,
                                                      VGfloat *matrix)
 {
   /* Basic idea taken from the reference implementation */
+  VGfloat mat[3][3];
+  VGfloat det, det00, det01, det02;
+
   if( !matrix )
     return VGU_ILLEGAL_ARGUMENT_ERROR;
 
-  VGfloat mat[3][3];
   if(    vguComputeWarpSquareToQuad( sx0, sy0, sx1, sy1, sx2, sy2, sx3, sy3,
                                      (VGfloat*)mat )
       == VGU_BAD_WARP_ERROR )
     return VGU_BAD_WARP_ERROR;
 
   // Invert the matrix...
-  VGfloat det00 = mat[1][1]*mat[2][2] - mat[2][1]*mat[1][2];
-  VGfloat det01 = mat[2][0]*mat[1][2] - mat[1][0]*mat[2][2];
-  VGfloat det02 = mat[1][0]*mat[2][1] - mat[2][0]*mat[1][1];
+  det00 = mat[1][1]*mat[2][2] - mat[2][1]*mat[1][2];
+  det01 = mat[2][0]*mat[1][2] - mat[1][0]*mat[2][2];
+  det02 = mat[1][0]*mat[2][1] - mat[2][0]*mat[1][1];
 
-  VGfloat det = mat[0][0]*det00 + mat[0][1]*det01 + mat[0][2]*det02;
+  det = mat[0][0]*det00 + mat[0][1]*det01 + mat[0][2]*det02;
   if( det == 0.0f )
     return VGU_BAD_WARP_ERROR;
 
index e82c08123a55310c580ccda953f8dca44c48da9e..f1a6ef983e4d8a64c5539a773faead9f9a7e3b73 100644 (file)
@@ -139,6 +139,7 @@ void Canvas::reset(SGPropertyNode* node)
 
   if( _node )
   {
+    _root_group.reset( new canvas::Group(_node) );
     _node->tie
     (
       "size[0]",
@@ -198,9 +199,7 @@ void Canvas::update(double delta_time_sec)
     _texture.useStencil(true);
     _texture.allocRT(_camera_callback);
     _texture.getCamera()->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f , 1.0f));
-
-    for( size_t i = 0; i < _groups.size(); ++i )
-      _texture.getCamera()->addChild( _groups[i]->getMatrixTransform() );
+    _texture.getCamera()->addChild(_root_group->getMatrixTransform());
 
     if( _texture.serviceable() )
     {
@@ -213,8 +212,7 @@ void Canvas::update(double delta_time_sec)
     }
   }
 
-  for( size_t i = 0; i < _groups.size(); ++i )
-    _groups[i]->update(delta_time_sec);
+  _root_group->update(delta_time_sec);
 
   if( _sampling_dirty )
   {
@@ -351,19 +349,11 @@ void Canvas::childAdded( SGPropertyNode * parent,
   if( parent != _node )
     return;
 
-  if( child->getNameString() == "group" )
-  {
-    _groups.push_back
-    (
-      boost::shared_ptr<canvas::Group>(new canvas::Group(child))
-    );
-    if( _texture.serviceable() )
-      _texture.getCamera()->addChild( _groups.back()->getMatrixTransform() );
-  }
-  else if( child->getNameString() == "placement" )
-  {
+  if( child->getNameString() == "placement" )
     _dirty_placements.push_back(child);
-  }
+  else
+    static_cast<canvas::Element*>(_root_group.get())
+      ->childAdded(parent, child);
 }
 
 //------------------------------------------------------------------------------
@@ -375,10 +365,13 @@ void Canvas::childRemoved( SGPropertyNode * parent,
 
   if( child->getNameString() == "placement" )
     clearPlacements(child->getIndex());
+  else
+    static_cast<canvas::Element*>(_root_group.get())
+      ->childRemoved(parent, child);
 }
 
 //----------------------------------------------------------------------------
-void Canvas::valueChanged(SGPropertyNode * node)
+void Canvas::valueChanged(SGPropertyNode* node)
 {
   if( node->getParent()->getParent() == _node )
   {
@@ -406,6 +399,8 @@ void Canvas::valueChanged(SGPropertyNode * node)
         || node->getNameString() == "color-samples" )
       _sampling_dirty = true;
   }
+
+  _root_group->valueChanged(node);
 }
 
 //------------------------------------------------------------------------------
index 9094825d453ed1d7391762003eb39754826492c8..25f87012d3c98fafc3226093a3b6bfc90c7c4836 100644 (file)
@@ -23,7 +23,7 @@
 #include <simgear/props/props.hxx>
 #include <osg/NodeCallback>
 
-#include <boost/shared_ptr.hpp>
+#include <memory>
 #include <string>
 
 namespace canvas
@@ -88,6 +88,7 @@ class Canvas:
          _color_dirty;
 
     FGODGauge _texture;
+    std::auto_ptr<canvas::Group> _root_group;
 
     SGPropertyNode_ptr              _node;
     std::vector<SGPropertyNode_ptr> _color_background;
@@ -97,9 +98,6 @@ class Canvas:
     std::vector<SGPropertyNode*> _dirty_placements;
     std::vector<Placements> _placements;
 
-    // TODO replace auto_ptr with unique_ptr as soon as C++11 is used!
-    std::vector<boost::shared_ptr<canvas::Group> > _groups;
-
     void setStatusFlags(unsigned int flags, bool set = true);
     void clearPlacements(int index);
     void clearPlacements();
index a27ad619718494468bba9b428264d39a271cf578..1695449aa0c85d16c937ec918f0dddb7d15279d4 100644 (file)
@@ -126,53 +126,6 @@ namespace canvas
     return _transform;
   }
 
-  //----------------------------------------------------------------------------
-  Element::Element(SGPropertyNode_ptr node, uint32_t attributes_used):
-    _attributes_used( attributes_used ),
-    _attributes_dirty( attributes_used ),
-    _transform_dirty( false ),
-    _transform( new osg::MatrixTransform ),
-    _node( node ),
-    _drawable( 0 )
-  {
-    assert( _node );
-    _node->addChangeListener(this);
-
-    if( _attributes_used & COLOR )
-      linkColorNodes("color", _node, _color, osg::Vec4f(0,1,0,1));
-
-    if( _attributes_used & COLOR_FILL )
-      linkColorNodes("color-fill", _node, _color_fill);
-
-    SG_LOG
-    (
-      SG_GL,
-      SG_DEBUG,
-      "New canvas element " << node->getPath()
-    );
-  }
-
-  //----------------------------------------------------------------------------
-  void Element::setDrawable( osg::Drawable* drawable )
-  {
-    _drawable = drawable;
-    assert( _drawable );
-
-    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
-    geode->addDrawable(_drawable);
-    _transform->addChild(geode);
-
-    if( _attributes_used & BOUNDING_BOX )
-    {
-      SGPropertyNode* bb_node = _node->getChild("bounding-box", 0, true);
-      _bounding_box.resize(4);
-      _bounding_box[0] = bb_node->getChild("min-x", 0, true);
-      _bounding_box[1] = bb_node->getChild("min-y", 0, true);
-      _bounding_box[2] = bb_node->getChild("max-x", 0, true);
-      _bounding_box[3] = bb_node->getChild("max-y", 0, true);
-    }
-  }
-
   //----------------------------------------------------------------------------
   void Element::childAdded(SGPropertyNode* parent, SGPropertyNode* child)
   {
@@ -261,4 +214,51 @@ namespace canvas
     }
   }
 
+  //----------------------------------------------------------------------------
+  Element::Element(SGPropertyNode_ptr node, uint32_t attributes_used):
+    _attributes_used( attributes_used ),
+    _attributes_dirty( attributes_used ),
+    _transform_dirty( false ),
+    _transform( new osg::MatrixTransform ),
+    _node( node ),
+    _drawable( 0 )
+  {
+    assert( _node );
+    _node->addChangeListener(this);
+
+    if( _attributes_used & COLOR )
+      linkColorNodes("color", _node, _color, osg::Vec4f(0,1,0,1));
+
+    if( _attributes_used & COLOR_FILL )
+      linkColorNodes("color-fill", _node, _color_fill, osg::Vec4f(1,0,1,1));
+
+    SG_LOG
+    (
+      SG_GL,
+      SG_DEBUG,
+      "New canvas element " << node->getPath()
+    );
+  }
+
+  //----------------------------------------------------------------------------
+  void Element::setDrawable( osg::Drawable* drawable )
+  {
+    _drawable = drawable;
+    assert( _drawable );
+
+    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
+    geode->addDrawable(_drawable);
+    _transform->addChild(geode);
+
+    if( _attributes_used & BOUNDING_BOX )
+    {
+      SGPropertyNode* bb_node = _node->getChild("bounding-box", 0, true);
+      _bounding_box.resize(4);
+      _bounding_box[0] = bb_node->getChild("min-x", 0, true);
+      _bounding_box[1] = bb_node->getChild("min-y", 0, true);
+      _bounding_box[2] = bb_node->getChild("max-x", 0, true);
+      _bounding_box[3] = bb_node->getChild("max-y", 0, true);
+    }
+  }
+
 } // namespace canvas
index 3c9730e66f4d65106381b4b40e16498e600ce4fe..642060434d0c6521025f10ddd9292dd37028b8dc 100644 (file)
@@ -32,7 +32,7 @@ namespace canvas
 {
 
   class Element:
-    private SGPropertyChangeListener
+    public SGPropertyChangeListener
   {
     public:
       virtual ~Element() = 0;
@@ -46,6 +46,12 @@ namespace canvas
 
       osg::ref_ptr<osg::MatrixTransform> getMatrixTransform();
 
+      virtual void childAdded( SGPropertyNode * parent,
+                               SGPropertyNode * child );
+      virtual void childRemoved( SGPropertyNode * parent,
+                                 SGPropertyNode * child );
+      virtual void valueChanged(SGPropertyNode * child);
+
     protected:
 
       enum Attributes
@@ -93,12 +99,6 @@ namespace canvas
       osg::Drawable  *_drawable;
 
       Element(const Element&);// = delete
-
-      virtual void childAdded( SGPropertyNode * parent,
-                               SGPropertyNode * child );
-      virtual void childRemoved( SGPropertyNode * parent,
-                                 SGPropertyNode * child );
-      virtual void valueChanged(SGPropertyNode * child);
   };
 
 }  // namespace canvas
index 67f5d58f2c716dfe484beee7c5c8f4385772956c..ebbe3e6f747589295db4dc5b9e2730458976d57d 100644 (file)
@@ -58,13 +58,6 @@ namespace canvas
       element.reset( new Group(child) );
     else if( child->getNameString() == "path" )
       element.reset( new Path(child) );
-    else
-      SG_LOG
-      (
-        SG_GL,
-        SG_WARN,
-        "canvas::Group unknown child: " << child->getDisplayName()
-      );
 
     if( !element )
       return;
index e3694928b8c35ae4a6ba0d0df1d8902ab74ff487..14a385ce5f7a2939ca58994d20cded7c65725c81 100644 (file)
@@ -38,16 +38,13 @@ namespace canvas
       PathDrawable():
         _path(VG_INVALID_HANDLE),
         _paint(VG_INVALID_HANDLE),
-      _attributes_dirty(~0),
-      _stroke_width(1)
+        _paint_fill(VG_INVALID_HANDLE),
+        _attributes_dirty(~0),
+        _stroke_width(1),
+        _fill(false)
       {
         setSupportsDisplayList(false);
         setDataVariance(Object::DYNAMIC);
-
-        _paint_color[0] = 0;
-        _paint_color[1] = 1;
-        _paint_color[2] = 1;
-        _paint_color[3] = 1;
       }
 
       virtual ~PathDrawable()
@@ -92,8 +89,26 @@ namespace canvas
       void setColor(const osg::Vec4& color)
       {
         for( size_t i = 0; i < 4; ++i )
-          _paint_color[i] = color[i];
-        _attributes_dirty |= PAINT_COLOR;
+          _stroke_color[i] = color[i];
+        _attributes_dirty |= STROKE_COLOR;
+      }
+
+      /**
+       * Enable/Disable filling of the path
+       */
+      void enableFill(bool enable)
+      {
+        _fill = enable;
+      }
+
+      /**
+       * Set the line color
+       */
+      void setColorFill(const osg::Vec4& color)
+      {
+        for( size_t i = 0; i < 4; ++i )
+          _fill_color[i] = color[i];
+        _attributes_dirty |= FILL_COLOR;
       }
 
       /**
@@ -146,18 +161,13 @@ namespace canvas
         }
 
         // Initialize/Update the paint
-        if( _attributes_dirty & (PAINT_COLOR | STROKE) )
+        if( _attributes_dirty & (STROKE_COLOR | STROKE) )
         {
           if( _paint == VG_INVALID_HANDLE )
-          {
             _paint = vgCreatePaint();
-            vgSetPaint(_paint, VG_STROKE_PATH);
-          }
 
-          if( _attributes_dirty & PAINT_COLOR )
-          {
-            vgSetParameterfv(_paint, VG_PAINT_COLOR, 4, _paint_color);
-          }
+          if( _attributes_dirty & STROKE_COLOR )
+            vgSetParameterfv(_paint, VG_PAINT_COLOR, 4, _stroke_color);
           if( _attributes_dirty & STROKE )
           {
             vgSetf(VG_STROKE_LINE_WIDTH, _stroke_width);
@@ -167,15 +177,41 @@ namespace canvas
                      _stroke_dash.empty() ? 0 : &_stroke_dash[0] );
           }
 
-          _attributes_dirty &= ~(PAINT_COLOR | STROKE);
+          _attributes_dirty &= ~(STROKE_COLOR | STROKE);
+        }
+
+        // Initialize/update fill paint
+        if( _attributes_dirty & (FILL_COLOR | FILL) )
+        {
+          if( _paint_fill == VG_INVALID_HANDLE )
+            _paint_fill = vgCreatePaint();
+
+          if( _attributes_dirty & FILL_COLOR )
+            vgSetParameterfv(_paint_fill, VG_PAINT_COLOR, 4, _fill_color);
+
+          _attributes_dirty &= ~(FILL_COLOR | FILL);
+        }
+
+        // Detect draw mode
+        VGbitfield mode = 0;
+        if( _stroke_width > 0 )
+        {
+          mode |= VG_STROKE_PATH;
+          vgSetPaint(_paint, VG_STROKE_PATH);
+        }
+        if( _fill )
+        {
+          mode |= VG_FILL_PATH;
+          vgSetPaint(_paint_fill, VG_FILL_PATH);
         }
 
         // And finally draw the path
-        vgDrawPath(_path, VG_STROKE_PATH);
+        if( mode )
+          vgDrawPath(_path, mode);
 
         VGErrorCode err = vgGetError();
         if( err != VG_NO_ERROR )
-          std::cout << "vgError: " << err << std::endl;
+          SG_LOG(SG_GL, SG_ALERT, "vgError: " << err);
 
         glPopAttrib();
         glPopClientAttrib();
@@ -188,27 +224,33 @@ namespace canvas
       enum Attributes
       {
         PATH            = 0x0001,
-        PAINT_COLOR     = 0x0002,
-        STROKE          = 0x0004
+        STROKE_COLOR    = PATH << 1,
+        STROKE          = STROKE_COLOR << 1,
+        FILL_COLOR      = STROKE << 1,
+        FILL            = FILL_COLOR << 1
       };
 
       mutable VGPath    _path;
       mutable VGPaint   _paint;
+      mutable VGPaint   _paint_fill;
       mutable uint32_t  _attributes_dirty;
 
       CmdList   _cmds;
       CoordList _coords;
 
-      VGfloat               _paint_color[4];
+      VGfloat               _stroke_color[4];
       VGfloat               _stroke_width;
       std::vector<VGfloat>  _stroke_dash;
+
+      bool      _fill;
+      VGfloat   _fill_color[4];
   };
 
   bool PathDrawable::_vg_initialized = false;
 
   //----------------------------------------------------------------------------
   Path::Path(SGPropertyNode_ptr node):
-    Element(node, COLOR /*| COLOR_FILL*/), // TODO fill color
+    Element(node, COLOR | COLOR_FILL),
     _path( new PathDrawable() )
   {
     setDrawable(_path);
@@ -257,6 +299,8 @@ namespace canvas
     else if(    child->getNameString() == "stroke-width"
              || child->getNameString() == "stroke-dasharray" )
       _attributes_dirty |= STROKE;
+    else if( child->getNameString() == "fill" )
+      _path->enableFill( child->getBoolValue() );
   }
 
   //----------------------------------------------------------------------------
@@ -268,7 +312,7 @@ namespace canvas
   //----------------------------------------------------------------------------
   void Path::colorFillChanged(const osg::Vec4& color)
   {
-
+    _path->setColorFill(color);
   }
 
 } // namespace canvas
index 8e14e554809995d368c3a60feea0d6e3c5fc029a..429cc636e60c7eff782b397632fbc4e1e7b8b213 100644 (file)
@@ -76,7 +76,7 @@ namespace canvas
   void linkColorNodes( const char* name,
                        SGPropertyNode* parent,
                        std::vector<SGPropertyNode_ptr>& nodes,
-                       const osg::Vec4& def = osg::Vec4(0,0,0,0) );
+                       const osg::Vec4& def = osg::Vec4(0,0,0,1) );
 
 } // namespace canvas
 
index 73c899b6c7cf519481421e5889d5f88565c469c7..2a541e30498428c3df8a7d13e03189e22f4fe0fe 100644 (file)
@@ -64,6 +64,9 @@ if(ENABLE_JSBSIM)
     target_link_libraries(fgfs JSBSim)
 endif()
 
+include_directories(${PROJECT_SOURCE_DIR}/src/Canvas/ShivaVG/include)
+add_definitions(-DVG_API_EXPORT)
+
 target_link_libraries(fgfs
        ${SIMGEAR_LIBRARIES}
        ${OPENSCENEGRAPH_LIBRARIES}