/*
* This source file is part of the osgOcean library
* 
* Copyright (C) 2009 Kim Bale
* Copyright (C) 2009 The University of Hull, UK
* 
* This program 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 3 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.
* http://www.gnu.org/copyleft/lesser.txt.
*/

#pragma once
#include <osgOcean/Export>
#include <osg/Program>

#include <map>
#include <sstream>

namespace osgOcean
{
    
    class OSGOCEAN_EXPORT ShaderManager
    {
    private:
        typedef std::map<const std::string, std::string> GlobalDefinitions;
        GlobalDefinitions _globalDefinitions;
        bool _shadersEnabled;

    public:
        static ShaderManager& instance();

        /** Set a definition that will be added as a #define to the top of every 
         *  shader loaded through subsequent calls to createProgram() .
         */
        template<typename T>
        void setGlobalDefinition(const std::string& name, const T& value)
        {
            std::ostringstream valStr;
            valStr << value;
            _globalDefinitions[name] = valStr.str();
        }

        /** Get the value of a global definition that was previously set using
         *  setGlobalDefinition().
         */
        std::string getGlobalDefiniton(const std::string& name);

        /** Creates a shader program. Will try to load the filenames first, and if 
         *  not found will fall back to using the given shader source strings.
         */
        osg::Program* createProgram( const std::string& name, 
                                     const std::string& vertexFilename, 
                                     const std::string& fragmentFilename, 
                                     const std::string& vertexSrc, 
                                     const std::string& fragmentSrc );

        /// Check if shaders are globally enabled or not.
        bool areShadersEnabled() const { return _shadersEnabled; }
        /// Globally enable or disable shaders for osgOcean.
        void enableShaders(bool enable) { _shadersEnabled = enable; }

    private:
        // private so clients can't call it, they have to call instance().
        ShaderManager();

        // No definition, copy and assignment is illegal.
        ShaderManager(const ShaderManager&);
        ShaderManager& operator=(const ShaderManager&);

        std::string buildGlobalDefinitionsList(const std::string& name);
    };
}
