PICCANTE  0.4
The hottest HDR imaging library!
technique.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_UTIL_GL_TECHNIQUE_HPP
19 #define PIC_UTIL_GL_TECHNIQUE_HPP
20 
21 #include <string>
22 
23 #include "../../util/std_util.hpp"
24 #include "../../util/gl/program.hpp"
25 
26 namespace pic {
27 
32 {
33 protected:
34  std::vector<ProgramGL*> shaders;
36 
42  GLuint getLocation(const char *name)
43  {
44  return glGetUniformLocation(main.getObject(), name);
45  }
46 
47 public:
48 
53  {
54 
55  }
56 
58  {
59  stdVectorClear<ProgramGL>(shaders);
60  }
61 
62  bool isValid()
63  {
64  return main.bCompiled;
65  }
66 
67  bool init( std::string version_number,
68  std::string vertex_shader_source,
69  std::string fragment_shader_source)
70  {
71  ProgramGL *vss = new ProgramGL(version_number, "", vertex_shader_source, GL_VERTEX_SHADER);
72  ProgramGL *fss = new ProgramGL(version_number, "", fragment_shader_source, GL_FRAGMENT_SHADER);
73  shaders.push_back(vss);
74  shaders.push_back(fss);
75 
76  return main.initProgram(shaders);
77  }
78 
79  bool init( std::string version_number,
80  std::string vertex_shader_source,
81  std::string fragment_shader_source,
82  std::string geomety_shader_source)
83  {
84  ProgramGL *vss = new ProgramGL(version_number, "", vertex_shader_source, GL_VERTEX_SHADER);
85  ProgramGL *gss = new ProgramGL(version_number, "", geomety_shader_source, GL_GEOMETRY_SHADER);
86  ProgramGL *fss = new ProgramGL(version_number, "", fragment_shader_source, GL_FRAGMENT_SHADER);
87 
88  shaders.push_back(vss);
89  shaders.push_back(gss);
90  shaders.push_back(fss);
91 
92  return main.initProgram(shaders);
93  }
94 
95  bool initCompute(std::string version_number,
96  std::string compute_shader_source)
97  {
98  #ifdef OPEN_GL_4_30
99  ProgramGL *css = new ProgramGL(version_number, "", compute_shader_source, GL_COMPUTE_SHADER);
100  shaders.push_back(css);
101  #endif
102 
103  return main.initProgram(shaders);
104  }
105 
114  bool initStandard( std::string version_number,
115  std::string vertex_shader_source,
116  std::string fragment_shader_source,
117  std::string name)
118  {
119  bool bCheck = this->init(version_number,
120  vertex_shader_source,
121  fragment_shader_source);
122 
123  #ifdef PIC_DEBUG
124  this->printLog(name);
125  #endif
126 
127  if (bCheck) {
128  this->bind();
129  this->setAttributeIndex("a_position", 0);
130  this->setOutputFragmentShaderIndex("f_color", 0);
131  this->link();
132  this->unbind();
133  }
134 
135  return bCheck;
136  }
137 
147  bool initStandard( std::string version_number,
148  std::string vertex_shader_source,
149  std::string fragment_shader_source,
150  std::string geometry_shader_source,
151  std::string name)
152  {
153  bool bCheck = this->init(version_number,
154  vertex_shader_source,
155  fragment_shader_source,
156  geometry_shader_source);
157 
158  #ifdef PIC_DEBUG
159  this->printLog(name);
160  #endif
161  if (bCheck) {
162  this->bind();
163  this->setAttributeIndex("a_position", 0);
164  this->setOutputFragmentShaderIndex("f_color", 0);
165  this->link();
166  this->unbind();
167  }
168 
169  return bCheck;
170  }
171 
176  void printLog(std::string name)
177  {
178  printf("\nLog for: %s\n", name.c_str());
179  for(auto i = 0; i < shaders.size(); i++) {
180  shaders[i]->printLog();
181  }
182 
183  main.printLog();
184  }
185 
189  void bind()
190  {
191  glUseProgram(main.getObject());
192  }
193 
197  void unbind()
198  {
199  glUseProgram(0);
200  }
201 
205  void link()
206  {
207  glLinkProgram(main.getObject());
208  }
209 
215  void setOutputFragmentShaderIndex(const char *fragment_output_color_name, unsigned int index)
216  {
217  glBindFragDataLocation(main.getObject(), GLuint(index), fragment_output_color_name);
218  }
219 
225  void setAttributeIndex(const char *attribute_name, unsigned int index)
226  {
227  glBindAttribLocation(main.getObject(), GLuint(index), attribute_name);
228  }
229 
230 
236  void setUniform1i(const char *name_uniform, int value0)
237  {
238  glUniform1i(getLocation(name_uniform),
239  GLint(value0));
240  }
241 
247  void setUniform1f(const char *name_uniform, float value0)
248  {
249  glUniform1f(getLocation(name_uniform),
250  GLfloat(value0));
251  }
252 
259  void setUniform2f(const char *name_uniform, float value0, float value1)
260  {
261  glUniform2f(getLocation(name_uniform),
262  GLfloat(value0),
263  GLfloat(value1));
264  }
265 
273  void setUniform3f(const char *name_uniform, float value0, float value1, float value2)
274  {
275  glUniform3f(getLocation(name_uniform),
276  GLfloat(value0),
277  GLfloat(value1),
278  GLfloat(value2));
279  }
280 
289  void setUniform4f(const char *name_uniform, float value0, float value1, float value2, float value3)
290  {
291  glUniform4f(getLocation(name_uniform),
292  GLfloat(value0),
293  GLfloat(value1),
294  GLfloat(value2),
295  GLfloat(value3));
296  }
297 
304  void setUniform3x3(const char *name_uniform, const float *matrix, bool bTranspose)
305  {
306  glUniformMatrix3fv(getLocation(name_uniform),
307  GLsizei(1),
308  bTranspose ? GL_TRUE : GL_FALSE,
309  (const GLfloat*)(matrix));
310  }
311 
318  void setUniform4x4(const char *name_uniform, const float *matrix, bool bTranspose)
319  {
320  glUniformMatrix4fv(getLocation(name_uniform),
321  GLsizei(1),
322  bTranspose ? GL_TRUE : GL_FALSE,
323  (const GLfloat*)(matrix));
324  }
325 
331  void setUniform3fv(const char *name_uniform, const float *value)
332  {
333  glUniform3fv(getLocation(name_uniform),
334  GLsizei(1),
335  (const GLfloat *)value);
336  }
337 
343  void setUniform4fv(const char *name_uniform, const float *value)
344  {
345  glUniform4fv(getLocation(name_uniform),
346  GLsizei(1),
347  (const GLfloat *)value);
348  }
349 
350  #ifdef OPEN_GL_4_30
351  void setSSBOIndex(const char *ssbo_name, unsigned int index)
352  {
353  GLunit block_index = 0;
354  block_index = glGetProgramResourceIndex(main, GL_SHADER_STORAGE_BLOCK, ssbo_name);
355  glShaderStorageBlockBinding(program, block_index, index);
356  }
357  #endif
358 };
359 
360 } // end namespace pic
361 
362 #endif /* PIC_UTIL_GL_TECHNIQUE_HPP */
363 
void setOutputFragmentShaderIndex(const char *fragment_output_color_name, unsigned int index)
setOutputFragmentShaderIndex
Definition: technique.hpp:215
bool initCompute(std::string version_number, std::string compute_shader_source)
Definition: technique.hpp:95
TechniqueGL()
TechniqueGL.
Definition: technique.hpp:52
~TechniqueGL()
Definition: technique.hpp:57
void setUniform1f(const char *name_uniform, float value0)
SetUniform1f.
Definition: technique.hpp:247
void unbind()
unbind
Definition: technique.hpp:197
void link()
link
Definition: technique.hpp:205
void setUniform2f(const char *name_uniform, float value0, float value1)
setUniform
Definition: technique.hpp:259
GLuint getObject()
getObject
Definition: program.hpp:163
bool initStandard(std::string version_number, std::string vertex_shader_source, std::string fragment_shader_source, std::string name)
initStandard
Definition: technique.hpp:114
Definition: program.hpp:27
bool init(std::string version_number, std::string vertex_shader_source, std::string fragment_shader_source)
Definition: technique.hpp:67
GLuint getLocation(const char *name)
getLocation
Definition: technique.hpp:42
The TechniqueGL class.
Definition: technique.hpp:31
void setUniform3fv(const char *name_uniform, const float *value)
setUniform3
Definition: technique.hpp:331
bool initStandard(std::string version_number, std::string vertex_shader_source, std::string fragment_shader_source, std::string geometry_shader_source, std::string name)
initStandard
Definition: technique.hpp:147
void setUniform4fv(const char *name_uniform, const float *value)
setUniform4
Definition: technique.hpp:343
void setUniform3f(const char *name_uniform, float value0, float value1, float value2)
setUniform
Definition: technique.hpp:273
void printLog(std::string name)
printLog
Definition: technique.hpp:176
void setUniform3x3(const char *name_uniform, const float *matrix, bool bTranspose)
setUniform3x3
Definition: technique.hpp:304
ProgramGL main
Definition: technique.hpp:35
void printLog()
printLog
Definition: program.hpp:239
void setAttributeIndex(const char *attribute_name, unsigned int index)
setAttributeIndex
Definition: technique.hpp:225
void setUniform4f(const char *name_uniform, float value0, float value1, float value2, float value3)
setUniform4f
Definition: technique.hpp:289
void setUniform4x4(const char *name_uniform, const float *matrix, bool bTranspose)
setUniform4x4
Definition: technique.hpp:318
Definition: bilateral_separation.hpp:25
bool init(std::string version_number, std::string vertex_shader_source, std::string fragment_shader_source, std::string geomety_shader_source)
Definition: technique.hpp:79
bool isValid()
Definition: technique.hpp:62
std::vector< ProgramGL * > shaders
Definition: technique.hpp:34
void setUniform1i(const char *name_uniform, int value0)
SetUniform.
Definition: technique.hpp:236
void bind()
bind
Definition: technique.hpp:189
bool bCompiled
Definition: program.hpp:109
bool initProgram(std::vector< ProgramGL *> &shaders)
initProgram
Definition: program.hpp:173