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:
26  /**
27  * @brief Constructs a custom uniform variable
28  * @param type Type of uniform variable
29  * @param name Name of uniform variable
30  * @param value Current value
31  * @param inc Increment value
32  * @param min Minimal value
33  * @param max Maximal value
34  * @param keyInc Key F1-F10 to increment the value decrement with SHIFT key
35  */
37  const SLchar* name,
38  T value,
39  T inc = 0.0f,
40  T min = 0.0f,
41  T max = 0.0f,
42  SLKey keyInc = K_none)
43  {
44  _name = name;
45  _value = value;
46  _min = min;
47  _max = max;
48  _inc = inc;
49  _type = type;
50  _keyInc = keyInc;
51 
52  if (_keyInc != K_none && !(_keyInc >= K_F1 && _keyInc <= K_F10))
53  SL_EXIT_MSG("Please use keys F1-F10 for uniform variables.");
54  }
55 
57  function<T(void)> getFunc)
58  {
59  _name = name;
60  _type = UT_lambda;
61  getValue = getFunc;
62  }
63 
64  const SLchar* name() { return _name.c_str(); }
65 
66  /*!
67  calculates the current value & returns it.
68  This method is called on every frame.
69  */
70  T value()
71  {
72  if (_type == UT_const)
73  return _value;
74 
75  if (_type == UT_lambda)
76  return getValue();
77 
78  if (_type == UT_incDec)
79  {
80  if ((_inc > 0.0f && _value >= _max) ||
81  (_inc < 0.0f && _value <= _min)) _inc *= -1;
82  _value += _inc;
83  return _value;
84  }
85 
86  if (_type == UT_incInc)
87  {
88  if (_inc > 0 && _value >= _max) _value = _min;
89  if (_inc < 0 && _value <= _min) _value = _max;
90  _value += _inc;
91  return _value;
92  }
93 
94  if (_type == UT_inc)
95  {
96  _value += _inc;
97  return _value;
98  }
99 
100  if (_type == UT_random)
101  {
102  _value = _min + ((T)rand() / (T)RAND_MAX) * (_max - _min);
103  return _value;
104  }
105 
106  if (_type == UT_seconds)
107  {
108  _value = (T)clock() / CLOCKS_PER_SEC;
109  return _value;
110  }
111  else
112  return _value;
113  }
114 
115  //! Key press event-handler
116  SLbool onKeyPress(const SLKey key, const SLKey mod) override
117  {
118  if (_keyInc != K_none)
119  {
120  if (key == _keyInc)
121  {
122  if (mod == K_none)
123  {
124  if (_value < _max)
125  {
126  _value += _inc;
127  SL_LOG("Uniform: %s = %5.4f", _name.c_str(),(float)_value);
128  return true;
129  }
130  else if (_inc == _max) // Toggle between min & max
131  {
132  _value = _min;
133  SL_LOG("Uniform: %s = %5.4f", _name.c_str(),(float)_value);
134  return true;
135  }
136  }
137  else if (mod == K_shift)
138  {
139  if (_value > _min)
140  {
141  _value -= _inc;
142  SL_LOG("Uniform: %s = %5.4f", _name.c_str(),(float)_value);
143  return true;
144  }
145  }
146  }
147  }
148  return false;
149  }
150 
151 private:
152  SLstring _name; //!< Name of the variable
153  T _value; //!< Current value
154  T _max; //!< Max. value for IncInc, IncDec & random types
155  T _min; //!< Min. value for IncInc, IncDec & random types
156  T _inc; //!< Increment value for IncInc, IncDec & Inc types
157  SLUniformType _type; //!< Uniform1f type
158  SLKey _keyInc; //!< keyboard key incrementing const values
159  function<T(void)> getValue; //!< lambda getter function
160 };
161 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
166 //! STL vector of SLGLShaderUniform1f pointers
167 typedef vector<SLGLUniform1f*> SLVUniform1f;
168 typedef vector<SLGLUniform1i*> SLVUniform1i;
169 //-----------------------------------------------------------------------------
170 #endif
#define SL_LOG(...)
Definition: SL.h:233
char SLchar
Definition: SL.h:162
bool SLbool
Definition: SL.h:175
#define SL_EXIT_MSG(message)
Definition: SL.h:240
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_F1
Definition: SLEnums.h:49
@ K_none
Definition: SLEnums.h:17
@ K_shift
Definition: SLEnums.h:62
@ K_F10
Definition: SLEnums.h:58
SLGLUniform< SLint > SLGLUniform1i
Definition: SLGLUniform.h:163
SLGLUniform< SLuint > SLGLUniform1u
Definition: SLGLUniform.h:164
vector< SLGLUniform1f * > SLVUniform1f
STL vector of SLGLShaderUniform1f pointers.
Definition: SLGLUniform.h:167
SLGLUniform< SLfloat > SLGLUniform1f
Definition: SLGLUniform.h:162
vector< SLGLUniform1i * > SLVUniform1i
Definition: SLGLUniform.h:168
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:155
SLstring _name
Name of the variable.
Definition: SLGLUniform.h:152
SLbool onKeyPress(const SLKey key, const SLKey mod) override
Key press event-handler.
Definition: SLGLUniform.h:116
T _max
Max. value for IncInc, IncDec & random types.
Definition: SLGLUniform.h:154
SLGLUniform(SLUniformType type, const SLchar *name, T value, T inc=0.0f, T min=0.0f, T max=0.0f, SLKey keyInc=K_none)
Constructs a custom uniform variable.
Definition: SLGLUniform.h:36
SLKey _keyInc
keyboard key incrementing const values
Definition: SLGLUniform.h:158
function< T(void)> getValue
lambda getter function
Definition: SLGLUniform.h:159
SLGLUniform(const SLchar *name, function< T(void)> getFunc)
Definition: SLGLUniform.h:56
T _value
Current value.
Definition: SLGLUniform.h:153
SLUniformType _type
Uniform1f type.
Definition: SLGLUniform.h:157
const SLchar * name()
Definition: SLGLUniform.h:64
T _inc
Increment value for IncInc, IncDec & Inc types.
Definition: SLGLUniform.h:156
T mod(T a, T b)
Definition: Utils.h:250