OpenWalnut 1.2.5

WGEShaderDefine.h

00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WGESHADERDEFINE_H
00026 #define WGESHADERDEFINE_H
00027 
00028 #include <string>
00029 
00030 #include <boost/shared_ptr.hpp>
00031 #include <boost/lexical_cast.hpp>
00032 
00033 #include "WGEShaderPreprocessor.h"
00034 
00035 /**
00036  * This class is able to provide arbitrary values as define statements in GLSL code.
00037  */
00038 template< typename ValueType = bool >
00039 class WGEShaderDefine: public WGEShaderPreprocessor
00040 {
00041 public:
00042     /**
00043      * Shared pointer for this class.
00044      */
00045     typedef boost::shared_ptr< WGEShaderDefine< ValueType > > SPtr;
00046 
00047     /**
00048      * A const shared pointer for this class.
00049      */
00050     typedef boost::shared_ptr< const WGEShaderDefine< ValueType > > ConstSPtr;
00051 
00052     /**
00053      * Constructs a define with a given name and initial value.
00054      *
00055      * \param name name of the define
00056      * \param value the initial value.
00057      */
00058     WGEShaderDefine( std::string name, ValueType value = ValueType( 0 ) );
00059 
00060     /**
00061      * Destructor.
00062      */
00063     virtual ~WGEShaderDefine();
00064 
00065     /**
00066      * Process the whole code. It is not allowed to modify some internal state in this function because it might be called by several shaders.
00067      *
00068      * \param code the code to process
00069      * \param file the filename of the shader currently processed. Should be used for debugging output.
00070      *
00071      * \return the resulting new code
00072      */
00073     virtual std::string process( const std::string& file, const std::string& code ) const;
00074 
00075     /**
00076      * Returns the name of the define.
00077      *
00078      * \return the name
00079      */
00080     std::string getName() const;
00081 
00082     /**
00083      * Returns the current value.
00084      *
00085      * \return the current value
00086      */
00087     const ValueType& getValue() const;
00088 
00089     /**
00090      * Sets the new value for this define. Causes an reload of all associated shaders.
00091      *
00092      * \param value the new value.
00093      */
00094     void setValue( const ValueType& value );
00095 
00096 protected:
00097 
00098 private:
00099 
00100     /**
00101      * The name of the define.
00102      */
00103     std::string m_name;
00104 
00105     /**
00106      * The value of the define as a string.
00107      */
00108     ValueType m_value;
00109 };
00110 
00111 template< typename ValueType >
00112 WGEShaderDefine< ValueType >::WGEShaderDefine( std::string name, ValueType value ):
00113     WGEShaderPreprocessor(),
00114     m_name( name ),
00115     m_value( value )
00116 {
00117     // initialize
00118 }
00119 
00120 template< typename ValueType >
00121 WGEShaderDefine< ValueType >::~WGEShaderDefine()
00122 {
00123     // cleanup
00124 }
00125 
00126 template< typename ValueType >
00127 std::string WGEShaderDefine< ValueType >::process( const std::string& /*file*/, const std::string& code ) const
00128 {
00129     if( !getActive() )
00130     {
00131         return code;
00132     }
00133 
00134     return "#define " + getName() + " " + boost::lexical_cast< std::string >( getValue() ) + "\n" + code;
00135 }
00136 
00137 template< typename ValueType >
00138 std::string WGEShaderDefine< ValueType >::getName() const
00139 {
00140     return m_name;
00141 }
00142 
00143 template< typename ValueType >
00144 const ValueType& WGEShaderDefine< ValueType >::getValue() const
00145 {
00146     return m_value;
00147 }
00148 
00149 template< typename ValueType >
00150 void WGEShaderDefine< ValueType >::setValue( const ValueType& value )
00151 {
00152     m_value = value;
00153     updated();
00154 }
00155 
00156 ///////////////////////////////////////////////////////////////////////////////
00157 // Some typedefs
00158 ///////////////////////////////////////////////////////////////////////////////
00159 
00160 typedef WGEShaderDefine< bool > WGEShaderDefineSwitch;
00161 
00162 #endif  // WGESHADERDEFINE_H
00163 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends