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 #include <boost/shared_ptr.hpp>
32 
33 #include "../../common/WStringUtils.h"
34 
35 #include "WGEShaderPreprocessor.h"
36 
37 /**
38  * This class is able to provide arbitrary values as define statements in GLSL code.
39  */
40 template< typename ValueType = bool >
42 {
43 public:
44  /**
45  * Shared pointer for this class.
46  */
47  typedef boost::shared_ptr< WGEShaderDefine< ValueType > > SPtr;
48 
49  /**
50  * A const shared pointer for this class.
51  */
52  typedef boost::shared_ptr< const WGEShaderDefine< ValueType > > ConstSPtr;
53 
54  /**
55  * Constructs a define with a given name and initial value.
56  *
57  * \param name name of the define
58  * \param value the initial value.
59  */
60  WGEShaderDefine( std::string name, ValueType value = ValueType( 0 ) );
61 
62  /**
63  * Destructor.
64  */
65  virtual ~WGEShaderDefine();
66 
67  /**
68  * Process the whole code. It is not allowed to modify some internal state in this function because it might be called by several shaders.
69  *
70  * \param code the code to process
71  * \param file the filename of the shader currently processed. Should be used for debugging output.
72  *
73  * \return the resulting new code
74  */
75  virtual std::string process( const std::string& file, const std::string& code ) const;
76 
77  /**
78  * Returns the name of the define.
79  *
80  * \return the name
81  */
82  std::string getName() const;
83 
84  /**
85  * Returns the current value.
86  *
87  * \return the current value
88  */
89  const ValueType& getValue() const;
90 
91  /**
92  * Sets the new value for this define. Causes an reload of all associated shaders.
93  *
94  * \param value the new value.
95  */
96  void setValue( const ValueType& value );
97 
98 protected:
99 private:
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  return "#define " + getName() + " " + string_utils::toString( getValue() ) + "\n" + code;
134 }
135 
136 template< typename ValueType >
138 {
139  return m_name;
140 }
141 
142 template< typename ValueType >
144 {
145  return m_value;
146 }
147 
148 template< typename ValueType >
150 {
151  m_value = value;
152  updated();
153 }
154 
155 ///////////////////////////////////////////////////////////////////////////////
156 // Some typedefs
157 ///////////////////////////////////////////////////////////////////////////////
158 
160 
161 #endif // WGESHADERDEFINE_H
162