From: Thomas Geymayer Date: Wed, 6 Jun 2012 22:11:10 +0000 (+0200) Subject: Allow filling paths and do some clean up/fixing. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=569042acbdfba967960a25779c2da3a60d0fab23;p=flightgear.git Allow filling paths and do some clean up/fixing. --- diff --git a/src/Canvas/CMakeLists.txt b/src/Canvas/CMakeLists.txt index 325bd791d..6927c04a4 100644 --- a/src/Canvas/CMakeLists.txt +++ b/src/Canvas/CMakeLists.txt @@ -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 diff --git a/src/Canvas/ShivaVG/src/CMakeLists.txt b/src/Canvas/ShivaVG/src/CMakeLists.txt index 4d7acf539..63dcdef78 100644 --- a/src/Canvas/ShivaVG/src/CMakeLists.txt +++ b/src/Canvas/ShivaVG/src/CMakeLists.txt @@ -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 diff --git a/src/Canvas/ShivaVG/src/shVgu.c b/src/Canvas/ShivaVG/src/shVgu.c index f42e819b1..30cb35e26 100644 --- a/src/Canvas/ShivaVG/src/shVgu.c +++ b/src/Canvas/ShivaVG/src/shVgu.c @@ -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; diff --git a/src/Canvas/canvas.cxx b/src/Canvas/canvas.cxx index e82c08123..f1a6ef983 100644 --- a/src/Canvas/canvas.cxx +++ b/src/Canvas/canvas.cxx @@ -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(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(_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(_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); } //------------------------------------------------------------------------------ diff --git a/src/Canvas/canvas.hxx b/src/Canvas/canvas.hxx index 9094825d4..25f87012d 100644 --- a/src/Canvas/canvas.hxx +++ b/src/Canvas/canvas.hxx @@ -23,7 +23,7 @@ #include #include -#include +#include #include namespace canvas @@ -88,6 +88,7 @@ class Canvas: _color_dirty; FGODGauge _texture; + std::auto_ptr _root_group; SGPropertyNode_ptr _node; std::vector _color_background; @@ -97,9 +98,6 @@ class Canvas: std::vector _dirty_placements; std::vector _placements; - // TODO replace auto_ptr with unique_ptr as soon as C++11 is used! - std::vector > _groups; - void setStatusFlags(unsigned int flags, bool set = true); void clearPlacements(int index); void clearPlacements(); diff --git a/src/Canvas/elements/element.cxx b/src/Canvas/elements/element.cxx index a27ad6197..1695449aa 100644 --- a/src/Canvas/elements/element.cxx +++ b/src/Canvas/elements/element.cxx @@ -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 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 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 diff --git a/src/Canvas/elements/element.hxx b/src/Canvas/elements/element.hxx index 3c9730e66..642060434 100644 --- a/src/Canvas/elements/element.hxx +++ b/src/Canvas/elements/element.hxx @@ -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 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 diff --git a/src/Canvas/elements/group.cxx b/src/Canvas/elements/group.cxx index 67f5d58f2..ebbe3e6f7 100644 --- a/src/Canvas/elements/group.cxx +++ b/src/Canvas/elements/group.cxx @@ -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; diff --git a/src/Canvas/elements/path.cxx b/src/Canvas/elements/path.cxx index e3694928b..14a385ce5 100644 --- a/src/Canvas/elements/path.cxx +++ b/src/Canvas/elements/path.cxx @@ -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 _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 diff --git a/src/Canvas/property_helper.hxx b/src/Canvas/property_helper.hxx index 8e14e5548..429cc636e 100644 --- a/src/Canvas/property_helper.hxx +++ b/src/Canvas/property_helper.hxx @@ -76,7 +76,7 @@ namespace canvas void linkColorNodes( const char* name, SGPropertyNode* parent, std::vector& nodes, - const osg::Vec4& def = osg::Vec4(0,0,0,0) ); + const osg::Vec4& def = osg::Vec4(0,0,0,1) ); } // namespace canvas diff --git a/src/Main/CMakeLists.txt b/src/Main/CMakeLists.txt index 73c899b6c..2a541e304 100644 --- a/src/Main/CMakeLists.txt +++ b/src/Main/CMakeLists.txt @@ -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}