Beatmup
Beatmup::GL::VariablesBundle Class Reference

#include <variables_bundle.h>

Inheritance diagram for Beatmup::GL::VariablesBundle:
Beatmup::LockableObject Beatmup::Object Beatmup::ImageShader

Classes

class  MatrixParameter
 

Public Member Functions

void clear ()
 Removes all stored variables. More...
 
void setInteger (std::string name, int value)
 Sets a scalar integer uniform value. More...
 
void setInteger (std::string name, int x, int y)
 
void setInteger (std::string name, int x, int y, int z)
 
void setInteger (std::string name, int x, int y, int z, int w)
 
void setFloat (std::string name, float value)
 Sets a scalar float uniform value. More...
 
void setFloat (std::string name, float x, float y)
 
void setFloat (std::string name, float x, float y, float z)
 
void setFloat (std::string name, float x, float y, float z, float w)
 
void setFloatMatrix2 (std::string name, const float matrix[4])
 Sets a float 2*2 matrix variable value. More...
 
void setFloatMatrix3 (std::string name, const float matrix[9])
 Sets a float 3*3 matrix variable value. More...
 
void setFloatMatrix4 (std::string name, const float matrix[16])
 Sets a float 4*4 matrix variable value. More...
 
void setFloatMatrix4 (std::string name, const Color::Matrix &matrix)
 Sets a float 4*4 matrix variable value from a Color::Matrix instance. More...
 
void setFloatArray (std::string name, const std::vector< float > &values)
 Sets a float array variable value. More...
 
float getFloat (const std::string &name) const
 Retrieves a value of a scalar float uniform variable by its name. More...
 
- Public Member Functions inherited from Beatmup::LockableObject
void lock ()
 
void unlock ()
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Protected Member Functions

void apply (Program &program)
 

Private Attributes

std::map< std::string, int > integers
 
std::map< std::string, float > floats
 
std::map< std::string, std::vector< float > > floatArrays
 
std::map< std::string, MatrixParameterparams
 

Detailed Description

Definition at line 33 of file variables_bundle.h.

Member Function Documentation

◆ apply()

void VariablesBundle::apply ( Program program)
protected

Definition at line 170 of file variables_bundle.cpp.

170  {
171  for (auto& var : integers)
172  program.setInteger(var.first.c_str(), var.second);
173 
174  for (auto& var : floats)
175  program.setFloat(var.first.c_str(), var.second);
176 
177  for (auto& var : params) {
178  // assign a vector
179  if (var.second.getHeight() == 1 && var.second.getCount() == 1)
180  switch (var.second.getType()) {
181  case MatrixParameter::Type::INT: {
182  GLint* data = var.second.getData<GLint>();
183  switch (var.second.getWidth()) {
184  case 2:
185  glUniform2i(program.getUniformLocation(var.first.c_str()), data[0], data[1]);
186  break;
187  case 3:
188  glUniform3i(program.getUniformLocation(var.first.c_str()), data[0], data[1], data[2]);
189  break;
190  case 4:
191  glUniform4i(program.getUniformLocation(var.first.c_str()), data[0], data[1], data[2], data[3]);
192  break;
193  default: Insanity::insanity("Invalid parameter size");
194  }
195  break;
196  }
197  case MatrixParameter::Type::FLOAT: {
198  GLfloat* data = var.second.getData<GLfloat>();
199  switch (var.second.getWidth()) {
200  case 2:
201  glUniform2f(program.getUniformLocation(var.first.c_str()), data[0], data[1]);
202  break;
203  case 3:
204  glUniform3f(program.getUniformLocation(var.first.c_str()), data[0], data[1], data[2]);
205  break;
206  case 4:
207  glUniform4f(program.getUniformLocation(var.first.c_str()), data[0], data[1], data[2], data[3]);
208  break;
209  default: Insanity::insanity("Invalid parameter size");
210  }
211  break;
212  }
213  default: Insanity::insanity("Invalid parameter type");
214  }
215  // assign an array
216  else if (var.second.getHeight() == 1)
217 #define CASE(n,t,T) case n: glUniform##n##t##v(program.getUniformLocation(var.first.c_str()), var.second.getCount(), var.second.getData<T>()); break;
218  switch (var.second.getType()) {
219  case MatrixParameter::Type::INT:
220  switch (var.second.getWidth()) {
221  CASE(1, i, GLint);
222  CASE(2, i, GLint);
223  CASE(3, i, GLint);
224  CASE(4, i, GLint);
225  default: Insanity::insanity("Invalid parameter size");
226  }
227  break;
228  case MatrixParameter::Type::FLOAT:
229  switch (var.second.getWidth()) {
230  CASE(1, f, GLfloat);
231  CASE(2, f, GLfloat);
232  CASE(3, f, GLfloat);
233  CASE(4, f, GLfloat);
234  default: Insanity::insanity("Invalid parameter size");
235  }
236  break;
237  default: Insanity::insanity("Invalid parameter type");
238  }
239  // assign a matrix
240  else if (var.second.getWidth() == var.second.getHeight())
241 #undef CASE
242 #define CASE(n) case n: glUniformMatrix##n##fv(program.getUniformLocation(var.first.c_str()), var.second.getCount(), GL_FALSE, var.second.getData<GLfloat>()); break;
243  switch (var.second.getType()) {
244  case MatrixParameter::Type::FLOAT: {
245  switch (var.second.getWidth()) {
246  CASE(2);
247  CASE(3);
248  CASE(4);
249  default: Insanity::insanity("Invalid parameter size");
250  }
251  break;
252  }
253  default: Insanity::insanity("Invalid parameter type");
254  }
255  // an error otherwise
256  else
257  Insanity::insanity("Invalid matrix size");
258  GL::GLException::check((std::string("setting uniform variable ") + var.first).c_str());
259  }
260 
261  for (auto& var : floatArrays) {
262  glUniform1fv(program.getUniformLocation(var.first.c_str()), var.second.size(), var.second.data());
263  GL::GLException::check((std::string("setting uniform variable ") + var.first).c_str());
264  }
265 }
void setInteger(const std::string &name, const int value, bool safe=false)
Assigns a value to a specific integer variable in the program.
Definition: program.cpp:308
handle_t getUniformLocation(const std::string &name)
Retrieves uniform variable location by its name.
Definition: program.cpp:288
void setFloat(const std::string &name, const float value, bool safe=false)
Assigns a value to a specific floating point variable in the program.
Definition: program.cpp:347
static void check(const std::string &info)
Definition: bgl.h:62
std::map< std::string, int > integers
std::map< std::string, MatrixParameter > params
std::map< std::string, float > floats
std::map< std::string, std::vector< float > > floatArrays
static void insanity(const char *message)
Definition: exception.h:136
#define CASE(n, t, T)

◆ clear()

void VariablesBundle::clear ( )

Removes all stored variables.

Definition at line 268 of file variables_bundle.cpp.

268  {
269  integers.clear();
270  floats.clear();
271  floatArrays.clear();
272  params.clear();
273 }

◆ setInteger() [1/4]

void VariablesBundle::setInteger ( std::string  name,
int  value 
)

Sets a scalar integer uniform value.

Parameters
nameThe uniform variable name
valueThe variable value

Definition at line 62 of file variables_bundle.cpp.

62  {
63  integers[name] = value;
64 }
return(jlong) new Beatmup jlong jstring name

◆ setInteger() [2/4]

void VariablesBundle::setInteger ( std::string  name,
int  x,
int  y 
)

Definition at line 66 of file variables_bundle.cpp.

66  {
67  MatrixParameter& param = params[name];
68  param.configure(MatrixParameter::Type::INT, 2);
69  GLint* data = param.getData<GLint>();
70  data[0] = x;
71  data[1] = y;
72 }
jobject jlong jint jint y
jobject jlong jint x

◆ setInteger() [3/4]

void VariablesBundle::setInteger ( std::string  name,
int  x,
int  y,
int  z 
)

Definition at line 74 of file variables_bundle.cpp.

74  {
75  MatrixParameter& param = params[name];
76  param.configure(MatrixParameter::Type::INT, 3);
77  GLint* data = param.getData<GLint>();
78  data[0] = x;
79  data[1] = y;
80  data[2] = z;
81 }
jlong jstring jint jint jint z

◆ setInteger() [4/4]

void VariablesBundle::setInteger ( std::string  name,
int  x,
int  y,
int  z,
int  w 
)

Definition at line 83 of file variables_bundle.cpp.

83  {
84  MatrixParameter& param = params[name];
85  param.configure(MatrixParameter::Type::INT, 4);
86  GLint* data = param.getData<GLint>();
87  data[0] = x;
88  data[1] = y;
89  data[2] = z;
90  data[3] = w;
91 }
jlong jstring jint jint jint jint w

◆ setFloat() [1/4]

void VariablesBundle::setFloat ( std::string  name,
float  value 
)

Sets a scalar float uniform value.

Parameters
nameThe uniform variable name
valueThe variable value

Definition at line 93 of file variables_bundle.cpp.

93  {
94  floats[name] = value;
95 }

◆ setFloat() [2/4]

void VariablesBundle::setFloat ( std::string  name,
float  x,
float  y 
)

Definition at line 97 of file variables_bundle.cpp.

97  {
98  MatrixParameter& param = params[name];
99  param.configure(MatrixParameter::Type::FLOAT, 2);
100  GLfloat* data = param.getData<GLfloat>();
101  data[0] = x;
102  data[1] = y;
103 }

◆ setFloat() [3/4]

void VariablesBundle::setFloat ( std::string  name,
float  x,
float  y,
float  z 
)

Definition at line 105 of file variables_bundle.cpp.

105  {
106  MatrixParameter& param = params[name];
107  param.configure(MatrixParameter::Type::FLOAT, 3);
108  GLfloat* data = param.getData<GLfloat>();
109  data[0] = x;
110  data[1] = y;
111  data[2] = z;
112 }

◆ setFloat() [4/4]

void VariablesBundle::setFloat ( std::string  name,
float  x,
float  y,
float  z,
float  w 
)

Definition at line 114 of file variables_bundle.cpp.

114  {
115  MatrixParameter& param = params[name];
116  param.configure(MatrixParameter::Type::FLOAT, 4);
117  GLfloat* data = param.getData<GLfloat>();
118  data[0] = x;
119  data[1] = y;
120  data[2] = z;
121  data[3] = w;
122 }

◆ setFloatMatrix2()

void VariablesBundle::setFloatMatrix2 ( std::string  name,
const float  matrix[4] 
)

Sets a float 2*2 matrix variable value.

Parameters
nameThe uniform variable name
matrixThe variable value

Definition at line 124 of file variables_bundle.cpp.

124  {
125  MatrixParameter& param = params[name];
126  param.configure(MatrixParameter::Type::FLOAT, 2, 2);
127  GLfloat *out = param.getData<GLfloat>();
128  for (const float* in = matrix; in < matrix + 4; ++in)
129  *out++ = *in;
130 }
JNIEnv jlong jint out

◆ setFloatMatrix3()

void VariablesBundle::setFloatMatrix3 ( std::string  name,
const float  matrix[9] 
)

Sets a float 3*3 matrix variable value.

Parameters
nameThe uniform variable name
matrixThe variable value

Definition at line 132 of file variables_bundle.cpp.

132  {
133  MatrixParameter& param = params[name];
134  param.configure(MatrixParameter::Type::FLOAT, 3, 3);
135  GLfloat *out = param.getData<GLfloat>();
136  for (const float* in = matrix; in < matrix + 9; ++in)
137  *out++ = *in;
138 }

◆ setFloatMatrix4() [1/2]

void VariablesBundle::setFloatMatrix4 ( std::string  name,
const float  matrix[16] 
)

Sets a float 4*4 matrix variable value.

Parameters
nameThe uniform variable name
matrixThe variable value

Definition at line 140 of file variables_bundle.cpp.

140  {
141  MatrixParameter& param = params[name];
142  param.configure(MatrixParameter::Type::FLOAT, 4, 4);
143  GLfloat *out = param.getData<GLfloat>();
144  for (const float* in = matrix; in < matrix + 16; ++in)
145  *out++ = *in;
146 }

◆ setFloatMatrix4() [2/2]

void VariablesBundle::setFloatMatrix4 ( std::string  name,
const Color::Matrix matrix 
)

Sets a float 4*4 matrix variable value from a Color::Matrix instance.

Parameters
nameThe uniform variable name
matrixThe color matrix to copy values from

Definition at line 148 of file variables_bundle.cpp.

148  {
149  MatrixParameter& param = params[name];
150  param.configure(MatrixParameter::Type::FLOAT, 4, 4);
151  GLfloat *out = param.getData<GLfloat>();
152  for (int x = 0; x < 4; ++x) {
153  const pixfloat4 row = matrix[x];
154  for (int y = 0; y < 4; ++y)
155  *out++ = row[y];
156  }
157 }
4-channel floating point arithmetic

◆ setFloatArray()

void VariablesBundle::setFloatArray ( std::string  name,
const std::vector< float > &  values 
)

Sets a float array variable value.

Parameters
nameThe uniform variable name
valuesThe values

Definition at line 159 of file variables_bundle.cpp.

159  {
160  floatArrays[name] = values;
161 }

◆ getFloat()

float VariablesBundle::getFloat ( const std::string &  name) const

Retrieves a value of a scalar float uniform variable by its name.

Parameters
nameThe variable name
Returns
the variable value, if defined previously, quiet NaN otherwise.

Definition at line 163 of file variables_bundle.cpp.

163  {
164  const auto& it = floats.find(name);
165  if (it == floats.cend())
166  return std::numeric_limits<float>::quiet_NaN();
167  return it->second;
168 }

Member Data Documentation

◆ integers

std::map<std::string, int> Beatmup::GL::VariablesBundle::integers
private

Definition at line 66 of file variables_bundle.h.

◆ floats

std::map<std::string, float> Beatmup::GL::VariablesBundle::floats
private

Definition at line 67 of file variables_bundle.h.

◆ floatArrays

std::map<std::string, std::vector<float> > Beatmup::GL::VariablesBundle::floatArrays
private

Definition at line 68 of file variables_bundle.h.

◆ params

std::map<std::string, MatrixParameter> Beatmup::GL::VariablesBundle::params
private

Definition at line 69 of file variables_bundle.h.


The documentation for this class was generated from the following files: