SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLGLUniform.h
Go to the documentation of this file.
1 /**
2  * \file SLGLUniform.h
3  * \date July 2014
4  * \authors Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #ifndef SLGLUNIFORM_H
11 #define SLGLUNIFORM_H
12 
13 #include <SL.h>
14 #include <SLEventHandler.h>
15 
16 //-----------------------------------------------------------------------------
17 //! Template for a single GLSL uniform variable.
18 /*! Class for GLSL uniform variables that change per frame. An SLGLProgram
19 holds a list of this type of uniform variables that are applied within the
20 beginUse method.
21 */
22 template<class T>
24 {
25 public:
27  const SLchar* name,
28  T value,
29  T inc = 0.0f,
30  T min = 0.0f,
31  T max = 0.0f,
32  SLKey keyInc = K_none)
33  {
34  _name = name;
35  _value = value;
36  _min = min;
37  _max = max;
38  _inc = inc;
39  _type = type;
40  _keyInc = keyInc;
41  }
42 
44  function<T(void)> getFunc)
45  {
46  _name = name;
47  _type = UT_lambda;
48  getValue = getFunc;
49  }
50 
51  const SLchar* name() { return _name.c_str(); }
52 
53  /*!
54  calculates the current value & returns it.
55  This method is called on every frame.
56  */
57  T value()
58  {
59  if (_type == UT_const)
60  return _value;
61 
62  if (_type == UT_lambda)
63  return getValue();
64 
65  if (_type == UT_incDec)
66  {
67  if ((_inc > 0.0f && _value >= _max) ||
68  (_inc < 0.0f && _value <= _min)) _inc *= -1;
69  _value += _inc;
70  return _value;
71  }
72 
73  if (_type == UT_incInc)
74  {
75  if (_inc > 0 && _value >= _max) _value = _min;
76  if (_inc < 0 && _value <= _min) _value = _max;
77  _value += _inc;
78  return _value;
79  }
80 
81  if (_type == UT_inc)
82  {
83  _value += _inc;
84  return _value;
85  }
86 
87  if (_type == UT_random)
88  {
89  _value = _min + ((T)rand() / (T)RAND_MAX) * (_max - _min);
90  return _value;
91  }
92 
93  if (_type == UT_seconds)
94  {
95  _value = (T)clock() / CLOCKS_PER_SEC;
96  return _value;
97  }
98  else
99  return _value;
100  }
101 
102  //! Key press event-handler
103  SLbool onKeyPress(const SLKey key, const SLKey mod) override
104  {
105  if (_keyInc != K_none)
106  {
107  if (key == _keyInc)
108  {
109  if (mod == K_none)
110  {
111  if (_value < _max)
112  {
113  _value += _inc;
114  // std::cout << "Uniform: " << _name.c_str() << " = " << _value << std::endl;
115  return true;
116  }
117  else if (_inc == _max) // Toggle between min & max
118  {
119  _value = _min;
120  // std::cout << "Uniform: " << _name.c_str() << " = " << _value << std::endl;
121  return true;
122  }
123  }
124  else if (mod == K_shift)
125  {
126  if (_value > _min)
127  {
128  _value -= _inc;
129  // std::cout << "Uniform: " << _name.c_str() << " = " << _value << std::endl;
130  return true;
131  }
132  }
133  }
134  }
135  return false;
136  }
137 
138 private:
139  SLstring _name; //!< Name of the variable
140  T _value; //!< Current value
141  T _max; //!< Max. value for IncInc, IncDec & random types
142  T _min; //!< Min. value for IncInc, IncDec & random types
143  T _inc; //!< Increment value for IncInc, IncDec & Inc types
144  SLUniformType _type; //!< Uniform1f type
145  SLKey _keyInc; //!< keyboard key incrementing const values
146  function<T(void)> getValue; //!< lambda getter function
147 };
148 //-----------------------------------------------------------------------------
152 //-----------------------------------------------------------------------------
153 //! STL vector of SLGLShaderUniform1f pointers
154 typedef vector<SLGLUniform1f*> SLVUniform1f;
155 typedef vector<SLGLUniform1i*> SLVUniform1i;
156 //-----------------------------------------------------------------------------
157 #endif
char SLchar
Definition: SL.h:162
bool SLbool
Definition: SL.h:175
string SLstring
Definition: SL.h:158
SLUniformType
Type definition for GLSL uniform1f variables that change per frame.
Definition: SLEnums.h:232
@ UT_seconds
seconds since the process has started
Definition: SLEnums.h:238
@ UT_incDec
never ending loop from min to max and max to min
Definition: SLEnums.h:234
@ UT_inc
never ending increment
Definition: SLEnums.h:236
@ UT_incInc
never ending loop from min to max
Definition: SLEnums.h:235
@ UT_random
random values between min and max
Definition: SLEnums.h:237
@ UT_const
constant value
Definition: SLEnums.h:233
@ UT_lambda
lambda getter function
Definition: SLEnums.h:239
SLKey
Keyboard key codes enumeration.
Definition: SLEnums.h:16
@ K_none
Definition: SLEnums.h:17
@ K_shift
Definition: SLEnums.h:62
SLGLUniform< SLint > SLGLUniform1i
Definition: SLGLUniform.h:150
SLGLUniform< SLuint > SLGLUniform1u
Definition: SLGLUniform.h:151
vector< SLGLUniform1f * > SLVUniform1f
STL vector of SLGLShaderUniform1f pointers.
Definition: SLGLUniform.h:154
SLGLUniform< SLfloat > SLGLUniform1f
Definition: SLGLUniform.h:149
vector< SLGLUniform1i * > SLVUniform1i
Definition: SLGLUniform.h:155
Virtual Eventhandler class.
Template for a single GLSL uniform variable.
Definition: SLGLUniform.h:24
T _min
Min. value for IncInc, IncDec & random types.
Definition: SLGLUniform.h:142
SLstring _name
Name of the variable.
Definition: SLGLUniform.h:139
SLbool onKeyPress(const SLKey key, const SLKey mod) override
Key press event-handler.
Definition: SLGLUniform.h:103
T _max
Max. value for IncInc, IncDec & random types.
Definition: SLGLUniform.h:141
SLGLUniform(SLUniformType type, const SLchar *name, T value, T inc=0.0f, T min=0.0f, T max=0.0f, SLKey keyInc=K_none)
Definition: SLGLUniform.h:26
SLKey _keyInc
keyboard key incrementing const values
Definition: SLGLUniform.h:145
function< T(void)> getValue
lambda getter function
Definition: SLGLUniform.h:146
SLGLUniform(const SLchar *name, function< T(void)> getFunc)
Definition: SLGLUniform.h:43
T _value
Current value.
Definition: SLGLUniform.h:140
SLUniformType _type
Uniform1f type.
Definition: SLGLUniform.h:144
const SLchar * name()
Definition: SLGLUniform.h:51
T _inc
Increment value for IncInc, IncDec & Inc types.
Definition: SLGLUniform.h:143
T mod(T a, T b)
Definition: Utils.h:250