Carna  Version 3.0.1
ShaderUniform.h
1 /*
2  * Copyright (C) 2010 - 2015 Leonid Kostrykin
3  *
4  * Chair of Medical Engineering (mediTEC)
5  * RWTH Aachen University
6  * Pauwelsstr. 20
7  * 52074 Aachen
8  * Germany
9  *
10  */
11 
12 #ifndef SHADERUNIFORM_H_6014714286
13 #define SHADERUNIFORM_H_6014714286
14 
15 #include <Carna/Carna.h>
16 #include <Carna/base/math.h>
17 
24 namespace Carna
25 {
26 
27 namespace base
28 {
29 
30 
31 
32 // ----------------------------------------------------------------------------------
33 // ShaderUniformType
34 // ----------------------------------------------------------------------------------
35 
40 template< typename ValueType >
42 {
46  typedef ValueType UniformType;
47 };
48 
49 
54 template< >
56 {
62 };
63 
64 
65 
66 // ----------------------------------------------------------------------------------
67 // ShaderUniformBase
68 // ----------------------------------------------------------------------------------
69 
76 class CARNA_LIB ShaderUniformBase
77 {
78 
79 public:
80 
84  ShaderUniformBase( const std::string& name );
85 
89  virtual ~ShaderUniformBase();
90 
94  std::string name;
95 
100  bool upload() const;
101 
102 protected:
103 
107  virtual void uploadTo( int location ) const = 0;
108 
109 private:
110 
111  const static int NULL_UNIFORM_LOCATION = -1;
112 
113  int location( const ShaderProgram& ) const;
114 
115 }; // ShaderUniformBase
116 
117 
118 
119 // ----------------------------------------------------------------------------------
120 // uploadUniform
121 // ----------------------------------------------------------------------------------
122 
127 void CARNA_LIB uploadUniform( int location, const int value );
128 
129 void CARNA_LIB uploadUniform( int location, const unsigned int value );
130 
131 void CARNA_LIB uploadUniform( int location, const float value );
132 
133 void CARNA_LIB uploadUniform( int location, const math::Vector2f& value );
134 
135 void CARNA_LIB uploadUniform( int location, const math::Vector3f& value );
136 
137 void CARNA_LIB uploadUniform( int location, const math::Vector4f& value );
138 
139 void CARNA_LIB uploadUniform( int location, const math::Matrix3f& value );
140 
141 void CARNA_LIB uploadUniform( int location, const math::Matrix4f& value );
142 
149 // ----------------------------------------------------------------------------------
150 // ShaderUniform
151 // ----------------------------------------------------------------------------------
152 
165 template< typename ValueType >
167 {
168 
169 public:
170 
171  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
172 
176  typedef ValueType Value;
177 
180  ShaderUniform( const std::string& name, const ValueType& value );
181 
184  ValueType value;
185 
186 protected:
187 
188  virtual void uploadTo( int location ) const override;
189 
190 }; // ShaderUniform
191 
192 
193 template< typename ValueType >
194 ShaderUniform< ValueType >::ShaderUniform( const std::string& name, const ValueType& value )
195  : ShaderUniformBase( name )
196  , value( value )
197 {
198 }
199 
200 
201 template< typename ValueType >
202 void ShaderUniform< ValueType >::uploadTo( int location ) const
203 {
204  typedef typename ShaderUniformType< ValueType >::UniformType UniformType;
205  uploadUniform( location, static_cast< const UniformType& >( value ) );
206 }
207 
208 
209 
210 } // namespace Carna :: base
211 
212 } // namespace Carna
213 
214 #endif // SHADERUNIFORM_H_6014714286
Defines Carna::base::math namespace and CARNA_FOR_VECTOR3UI.
Eigen::Matrix< float, 2, 1 > Vector2f
Defines vector.
Definition: math.h:197
math::Vector4f UniformType
Maps the ShaderUniform value type Color to uploaded type math::Vector4f.
Definition: ShaderUniform.h:61
Maps ShaderUniform value types to actually uploaded data types. This general case maps T to T...
Definition: ShaderUniform.h:41
ValueType UniformType
Maps ShaderUniform value types to itself.
Definition: ShaderUniform.h:46
Eigen::Matrix< float, 4, 1 > Vector4f
Defines vector.
Definition: math.h:195
ValueType Value
Holds the accepted value type.
Type-independent abstract ShaderUniform base class.
Definition: ShaderUniform.h:76
Eigen::Matrix< float, 3, 1 > Vector3f
Defines vector.
Definition: math.h:196
Maintains an OpenGL shader program. Realizes the RAII-idiom.
Definition: ShaderProgram.h:47
virtual void uploadTo(int location) const override
Uploads this uniform to location.
ShaderUniform(const std::string &name, const ValueType &value)
Represents a color. Objects from this class are copyable and assignable.
Definition: Color.h:41
Eigen::Matrix< float, 4, 4, Eigen::ColMajor > Matrix4f
Defines matrix.
Definition: math.h:193
std::string name
Holds the name of this uniform.
Definition: ShaderUniform.h:94
Implements ShaderUniformBase class for particular ValueType.
Eigen::Matrix< float, 3, 3, Eigen::ColMajor > Matrix3f
Defines matrix.
Definition: math.h:194