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