/* -*-c++-*- */ /* osgEarth - Geospatial SDK for OpenSceneGraph * Copyright 2020 Pelican Mapping * http://osgearth.org * * osgEarth is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see */ #ifndef OSGEARTH_SCREEN_SPACE_LAYOUT_H #define OSGEARTH_SCREEN_SPACE_LAYOUT_H 1 #include #include #include #include #include #include #define OSGEARTH_SCREEN_SPACE_LAYOUT_BIN "osgearth_ScreenSpaceLayoutBin" namespace osgEarth { /** * Interface that exposes layout information. */ class ScreenSpaceLayoutData : public osg::Referenced { public: /** Install and return a LayoutData on a drawable. */ static ScreenSpaceLayoutData* getOrCreate(osg::Node* drawable) { if (!drawable) return 0L; ScreenSpaceLayoutData* ld = dynamic_cast(drawable->getUserData()); if (!ld) { ld = new ScreenSpaceLayoutData(); drawable->setUserData(ld); } return ld; } /** Decluttering priority - FLT_MAX means don't declutter */ void setPriority(float value) { _priority = value; } float getPriority() const { return _priority; } /** Offset from geoposition in screen pixels */ void setPixelOffset(const osg::Vec2s& value) { _pixelOffset = value; } const osg::Vec2s& getPixelOffset() const { return _pixelOffset; } //! Whether the position is fixed (cannot be moved) void setFixed(bool value) { _fixed = value; } bool getFixed() const { return _fixed; } /** World point for label rotation reference */ void setAnchorPoint(const osg::Vec3d& value) { _anchorPoint = value; } const osg::Vec3d& getAnchorPoint() const { return _anchorPoint; } /** Reference point for label rotation reference */ void setProjPoint(const osg::Vec3d& value) { _refPoint = value; } const osg::Vec3d& getProjPoint() const { return _refPoint; } //! Fixed screen space rotation angle void setRotationDegrees(float value) { _angle_deg = value; } float getRotationDegrees() const { return _angle_deg; } public: float _priority = 0.0f; osg::Vec2s _pixelOffset; osg::Vec3d _anchorPoint, _refPoint; float _angle_deg = 0.0f; bool _fixed = false; bool _unique = false; }; /** * Custom functor that compares two RenderLeaf's and returns TRUE if the left-hand one * is higher priority, otherwise FALSE. You can call setDeclutterPriorityFunctor() * to set a custom priority-sorting functor. */ struct DeclutterSortFunctor : public osg::Referenced { virtual bool operator() ( const osgUtil::RenderLeaf* lhs, const osgUtil::RenderLeaf* rhs ) const =0; virtual ~DeclutterSortFunctor() { } }; /** * Options to control the annotation decluttering engine. */ class OSGEARTH_EXPORT ScreenSpaceLayoutOptions : public ConfigOptions { public: enum Technique { TECHNIQUE_LABELS, TECHNIQUE_CALLOUTS }; ScreenSpaceLayoutOptions( const ConfigOptions& co =ConfigOptions() ) : ConfigOptions ( co ), _minAnimAlpha ( 0.35f ), _minAnimScale ( 0.45f ), _inAnimTime ( 0.00f ), //0.40f ), _outAnimTime ( 0.00f ), _sortByPriority ( false ), _sortByDistance ( true ), _snapToPixel ( false ), _maxObjects ( INT_MAX ), _renderBinNumber ( 13 ), _technique ( TECHNIQUE_LABELS ), _leaderLineMaxLen ( 60 ), _leaderLineColor ( Color::White ), _leaderLineWidth ( 1.0f ) { fromConfig(_conf); } virtual ~ScreenSpaceLayoutOptions() { } /** Alpha value of a fully-occluded object */ optional& minAnimationAlpha() { return _minAnimAlpha; } const optional& minAnimationAlpha() const { return _minAnimAlpha; } /** Scale factor of a fully-occluded object */ optional& minAnimationScale() { return _minAnimScale; } const optional& minAnimationScale() const { return _minAnimScale; } /** Time (in seconds) for an object to transition from occluded to visible */ optional& inAnimationTime() { return _inAnimTime; } const optional& inAnimationTime() const { return _inAnimTime; } /** Time (in seconds) for an object to transition from visible to occluded */ optional& outAnimationTime() { return _outAnimTime; } const optional& outAnimationTime() const { return _outAnimTime; } /** If set, activate the ScreenSpaceLayoutData priority-based sorting */ optional& sortByPriority() { return _sortByPriority; } const optional& sortByPriority() const { return _sortByPriority; } /** If set, activate the ScreenSpaceLayoutData distance-based sorting */ optional& sortByDistance() { return _sortByDistance; } const optional& sortByDistance() const { return _sortByDistance; } /** Whether to always start rendering text on a pixel boundary, thereby * minimizing filtering artifacts. */ optional& snapToPixel() { return _snapToPixel; } const optional& snapToPixel() const { return _snapToPixel; } /** Maximum number of objects to draw after sorting */ optional& maxObjects() { return _maxObjects; } const optional& maxObjects() const { return _maxObjects; } /** Render bin number to use for the screen layout */ optional& renderOrder() { return _renderBinNumber; } const optional& renderOrder() const { return _renderBinNumber; } //! Technique to use for drawing optional& technique() { return _technique; } const optional& technique() const { return _technique; } //! For callouts, the maximum length of a leader line in pixels optional& leaderLineMaxLength() { return _leaderLineMaxLen; } const optional& leaderLineMaxLength() const { return _leaderLineMaxLen; } //! For callouts, the maximum length of a leader line in pixels optional& leaderLineColor() { return _leaderLineColor; } const optional& leaderLineColor() const { return _leaderLineColor; } //! For callouts, the width of a leader line in pixels optional& leaderLineWidth() { return _leaderLineWidth; } const optional& leaderLineWidth() const { return _leaderLineWidth; } public: Config getConfig() const; protected: optional _minAnimAlpha; optional _minAnimScale; optional _inAnimTime; optional _outAnimTime; optional _sortByPriority; optional _sortByDistance; optional _snapToPixel; optional _maxObjects; optional _renderBinNumber; optional _technique; optional _leaderLineMaxLen; optional _leaderLineColor; optional _leaderLineWidth; void fromConfig( const Config& conf ); }; struct OSGEARTH_EXPORT ScreenSpaceLayout { /** * Assigns a stateset to the screen-space layout engine. * Drawables rendered while this stateset is active will be projected from * scene space to 2D screen space with optional decluttering. */ static void activate(osg::StateSet* stateSet); //, int binNum =13); /** * Deactivates the use of the screen-space layout engine for a stateset. */ static void deactivate(osg::StateSet* stateSet); /** * Enables or disables decluttering globally. */ static void setDeclutteringEnabled(bool enabled); /** * Applies the provided options to the layout engine. */ static void setOptions(const ScreenSpaceLayoutOptions& options); /** * Fetches the current layout options */ static const ScreenSpaceLayoutOptions& getOptions(); public: // advanced /** * Sets a functor to use to determine render leaf priority for declutter sorting. */ static void setSortFunctor( DeclutterSortFunctor* f ); /** * Clears a custom priority functor that was set using setDeclutterPriorityFunctor, * reverting to the default behavior (which is to sort by distance from the camera). */ static void clearSortFunctor(); // internal static bool globallyEnabled; }; } // namespace osgEarth #endif //OSGEARTH_SCREEN_SPACE_LAYOUT_H