OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WGEShaderDefine.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WGESHADERDEFINE_H
26 #define WGESHADERDEFINE_H
27 
28 #include <string>
29 
30 #include <boost/shared_ptr.hpp>
31 #include <boost/lexical_cast.hpp>
32 
33 #include "WGEShaderPreprocessor.h"
34 
35 /**
36  * This class is able to provide arbitrary values as define statements in GLSL code.
37  */
38 template< typename ValueType = bool >
40 {
41 public:
42  /**
43  * Shared pointer for this class.
44  */
45  typedef boost::shared_ptr< WGEShaderDefine< ValueType > > SPtr;
46 
47  /**
48  * A const shared pointer for this class.
49  */
50  typedef boost::shared_ptr< const WGEShaderDefine< ValueType > > ConstSPtr;
51 
52  /**
53  * Constructs a define with a given name and initial value.
54  *
55  * \param name name of the define
56  * \param value the initial value.
57  */
58  WGEShaderDefine( std::string name, ValueType value = ValueType( 0 ) );
59 
60  /**
61  * Destructor.
62  */
63  virtual ~WGEShaderDefine();
64 
65  /**
66  * Process the whole code. It is not allowed to modify some internal state in this function because it might be called by several shaders.
67  *
68  * \param code the code to process
69  * \param file the filename of the shader currently processed. Should be used for debugging output.
70  *
71  * \return the resulting new code
72  */
73  virtual std::string process( const std::string& file, const std::string& code ) const;
74 
75  /**
76  * Returns the name of the define.
77  *
78  * \return the name
79  */
80  std::string getName() const;
81 
82  /**
83  * Returns the current value.
84  *
85  * \return the current value
86  */
87  const ValueType& getValue() const;
88 
89  /**
90  * Sets the new value for this define. Causes an reload of all associated shaders.
91  *
92  * \param value the new value.
93  */
94  void setValue( const ValueType& value );
95 
96 protected:
97 
98 private:
99 
100  /**
101  * The name of the define.
102  */
103  std::string m_name;
104 
105  /**
106  * The value of the define as a string.
107  */
109 };
110 
111 template< typename ValueType >
114  m_name( name ),
115  m_value( value )
116 {
117  // initialize
118 }
119 
120 template< typename ValueType >
122 {
123  // cleanup
124 }
125 
126 template< typename ValueType >
127 std::string WGEShaderDefine< ValueType >::process( const std::string& /*file*/, const std::string& code ) const
128 {
129  if( !getActive() )
130  {
131  return code;
132  }
133 
134  return "#define " + getName() + " " + boost::lexical_cast< std::string >( getValue() ) + "\n" + code;
135 }
136 
137 template< typename ValueType >
139 {
140  return m_name;
141 }
142 
143 template< typename ValueType >
145 {
146  return m_value;
147 }
148 
149 template< typename ValueType >
151 {
152  m_value = value;
153  updated();
154 }
155 
156 ///////////////////////////////////////////////////////////////////////////////
157 // Some typedefs
158 ///////////////////////////////////////////////////////////////////////////////
159 
161 
162 #endif // WGESHADERDEFINE_H
163