/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
* Copyright 2008-2010 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>
*/
#ifndef OSGEARTHUTIL_SKY_NODE
#define OSGEARTHUTIL_SKY_NODE

#include <osgEarthUtil/Common>
#include <osgEarth/Map>
#include <osg/MatrixTransform>
#include <osg/Uniform>
#include <osg/Group>
#include <osg/View>

namespace osgEarth { namespace Util 
{
    using namespace osgEarth;

    /**
     * A sky model.
     */
    class OSGEARTHUTIL_EXPORT SkyNode : public osg::Group
    {
    public:
        /** Creates a new sky node based on the provided map. */        
        SkyNode( Map* map, const std::string& starFile="" );

        /** Attached this sky node to a view (placing a sky light). */
        void attach( osg::View* view, int lightNum =0 );

        /** Sets the sun's position as a unit vector. */
        void setSunPosition( const osg::Vec3& pos );

        /** Sets the sun's position as a latitude and longitude. */         
        void setSunPosition( double latitudeRad, double longitudeRad );
        
        /** Sets the sun's position based on a julian date. */
        void setDateTime( int year, int month, int date, double hoursUTC );

        /** The minimum brightness for non-sunlit areas. */
        void setAmbientBrigtness( float value );
        float getAmbientBrightness() const { return _ambientBrightness; }

        /** Whether the stars are visible */
        void setStarsVisible( bool value ) { _starsXform->setNodeMask(value? ~0 : 0); }
        bool getStarsVisible() const { return _starsXform->getNodeMask() != 0; }

    private:
        struct StarData
        {
            std::string name;
            double right_ascension;
            double declination;
            double magnitude;
            
            StarData() { }
            StarData( std::stringstream &ss );
        };

        float _innerRadius, _outerRadius, _sunDistance, _starRadius;
        osg::Vec3f _lightPos;
        float _ambientBrightness;
        osg::ref_ptr<osg::Light> _light;
        osg::ref_ptr<osg::Uniform> _lightPosUniform;
        osg::ref_ptr<osg::MatrixTransform> _sunXform;
        osg::ref_ptr<osg::MatrixTransform> _starsXform;
        osg::ref_ptr<osg::Uniform> _starAlpha;
        osg::ref_ptr<osg::Uniform> _starPointSize;

        osg::ref_ptr< const osg::EllipsoidModel > _ellipsoidModel;

        void makeAtmosphere( const osg::EllipsoidModel* );
        void makeSun();

        void makeStars(const std::string& starFile);
        osg::Geode* buildStarGeometry(const std::vector<StarData>& stars);
        void getDefaultStars(std::vector<StarData>& out_stars);
        bool parseStarFile(const std::string& starFile, std::vector<StarData>& out_stars);
    };

} } // namespace osgEarth::Util

#endif //OSGEARTHUTIL_SKY_NODE
