OpenWalnut  1.4.0
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 #include <iostream>
30 
31 #ifndef Q_MOC_RUN
32 #include <boost/shared_ptr.hpp>
33 #endif
34 
35 #include "../../common/WStringUtils.h"
36 
37 #include "WGEShaderPreprocessor.h"
38 
39 /**
40  * This class is able to provide arbitrary values as define statements in GLSL code.
41  */
42 template< typename ValueType = bool >
44 {
45 public:
46  /**
47  * Shared pointer for this class.
48  */
49  typedef boost::shared_ptr< WGEShaderDefine< ValueType > > SPtr;
50 
51  /**
52  * A const shared pointer for this class.
53  */
54  typedef boost::shared_ptr< const WGEShaderDefine< ValueType > > ConstSPtr;
55 
56  /**
57  * Constructs a define with a given name and initial value.
58  *
59  * \param name name of the define
60  * \param value the initial value.
61  */
62  WGEShaderDefine( std::string name, ValueType value = ValueType( 0 ) );
63 
64  /**
65  * Destructor.
66  */
67  virtual ~WGEShaderDefine();
68 
69  /**
70  * Process the whole code. It is not allowed to modify some internal state in this function because it might be called by several shaders.
71  *
72  * \param code the code to process
73  * \param file the filename of the shader currently processed. Should be used for debugging output.
74  *
75  * \return the resulting new code
76  */
77  virtual std::string process( const std::string& file, const std::string& code ) const;
78 
79  /**
80  * Returns the name of the define.
81  *
82  * \return the name
83  */
84  std::string getName() const;
85 
86  /**
87  * Returns the current value.
88  *
89  * \return the current value
90  */
91  const ValueType& getValue() const;
92 
93  /**
94  * Sets the new value for this define. Causes an reload of all associated shaders.
95  *
96  * \param value the new value.
97  */
98  void setValue( const ValueType& value );
99 
100 protected:
101 private:
102  /**
103  * The name of the define.
104  */
105  std::string m_name;
106 
107  /**
108  * The value of the define as a string.
109  */
111 };
112 
113 template< typename ValueType >
116  m_name( name ),
117  m_value( value )
118 {
119  // initialize
120 }
121 
122 template< typename ValueType >
124 {
125  // cleanup
126 }
127 
128 template< typename ValueType >
129 std::string WGEShaderDefine< ValueType >::process( const std::string& /*file*/, const std::string& code ) const
130 {
131  if( !getActive() )
132  {
133  return code;
134  }
135  return "#define " + getName() + " " + string_utils::toString( getValue() ) + "\n" + code;
136 }
137 
138 template< typename ValueType >
140 {
141  return m_name;
142 }
143 
144 template< typename ValueType >
146 {
147  return m_value;
148 }
149 
150 template< typename ValueType >
152 {
153  m_value = value;
154  updated();
155 }
156 
157 ///////////////////////////////////////////////////////////////////////////////
158 // Some typedefs
159 ///////////////////////////////////////////////////////////////////////////////
160 
162 
163 #endif // WGESHADERDEFINE_H
164 
const ValueType & getValue() const
Returns the current value.
ValueType m_value
The value of the define as a string.
virtual ~WGEShaderDefine()
Destructor.
std::string m_name
The name of the define.
void setValue(const ValueType &value)
Sets the new value for this define.
std::string toString(const T &value)
Convert a given value to a string.
Definition: WStringUtils.h:120
WGEShaderDefine(std::string name, ValueType value=ValueType(0))
Constructs a define with a given name and initial value.
std::string getName() const
Returns the name of the define.
Base class for each preprocessing possible to shader code.
boost::shared_ptr< const WGEShaderDefine< ValueType > > ConstSPtr
A const shared pointer for this class.
This class is able to provide arbitrary values as define statements in GLSL code. ...
boost::shared_ptr< WGEShaderDefine< ValueType > > SPtr
Shared pointer for this class.
virtual std::string process(const std::string &file, const std::string &code) const
Process the whole code.