OpenWalnut  1.4.0
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 #include  <iostream>
00030 
00031 #include <boost/shared_ptr.hpp>
00032 
00033 #include "../../common/WStringUtils.h"
00034 
00035 #include "WGEShaderPreprocessor.h"
00036 
00037 /**
00038  * This class is able to provide arbitrary values as define statements in GLSL code.
00039  */
00040 template< typename ValueType = bool >
00041 class WGEShaderDefine: public WGEShaderPreprocessor
00042 {
00043 public:
00044     /**
00045      * Shared pointer for this class.
00046      */
00047     typedef boost::shared_ptr< WGEShaderDefine< ValueType > > SPtr;
00048 
00049     /**
00050      * A const shared pointer for this class.
00051      */
00052     typedef boost::shared_ptr< const WGEShaderDefine< ValueType > > ConstSPtr;
00053 
00054     /**
00055      * Constructs a define with a given name and initial value.
00056      *
00057      * \param name name of the define
00058      * \param value the initial value.
00059      */
00060     WGEShaderDefine( std::string name, ValueType value = ValueType( 0 ) );
00061 
00062     /**
00063      * Destructor.
00064      */
00065     virtual ~WGEShaderDefine();
00066 
00067     /**
00068      * Process the whole code. It is not allowed to modify some internal state in this function because it might be called by several shaders.
00069      *
00070      * \param code the code to process
00071      * \param file the filename of the shader currently processed. Should be used for debugging output.
00072      *
00073      * \return the resulting new code
00074      */
00075     virtual std::string process( const std::string& file, const std::string& code ) const;
00076 
00077     /**
00078      * Returns the name of the define.
00079      *
00080      * \return the name
00081      */
00082     std::string getName() const;
00083 
00084     /**
00085      * Returns the current value.
00086      *
00087      * \return the current value
00088      */
00089     const ValueType& getValue() const;
00090 
00091     /**
00092      * Sets the new value for this define. Causes an reload of all associated shaders.
00093      *
00094      * \param value the new value.
00095      */
00096     void setValue( const ValueType& value );
00097 
00098 protected:
00099 private:
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     return "#define " + getName() + " " + string_utils::toString( getValue() ) + "\n" + code;
00134 }
00135 
00136 template< typename ValueType >
00137 std::string WGEShaderDefine< ValueType >::getName() const
00138 {
00139     return m_name;
00140 }
00141 
00142 template< typename ValueType >
00143 const ValueType& WGEShaderDefine< ValueType >::getValue() const
00144 {
00145     return m_value;
00146 }
00147 
00148 template< typename ValueType >
00149 void WGEShaderDefine< ValueType >::setValue( const ValueType& value )
00150 {
00151     m_value = value;
00152     updated();
00153 }
00154 
00155 ///////////////////////////////////////////////////////////////////////////////
00156 // Some typedefs
00157 ///////////////////////////////////////////////////////////////////////////////
00158 
00159 typedef WGEShaderDefine< bool > WGEShaderDefineSwitch;
00160 
00161 #endif  // WGESHADERDEFINE_H
00162