PICCANTE  0.4
The hottest HDR imaging library!
quad.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_QUAD_HPP
19 #define PIC_UTIL_GL_QUAD_HPP
20 
21 namespace pic {
22 
23 #include <string>
24 
25 #include "../../util/gl/technique.hpp"
26 
30 class QuadGL
31 {
32 protected:
33  GLuint vao; //vertex array object
34  GLuint vbo[2]; //vertex buffer object
35 
36 public:
37 
39  {
40  vao = 0;
41  vbo[0] = 0;
42  vbo[1] = 0;
43  }
44 
45  QuadGL(bool bTextureCoordinates, float halfSizeX = 1.0f, float halfSizeY = 1.0f)
46  {
47  vao = 0;
48  vbo[0] = 0;
49  vbo[1] = 0;
50 
51  init(bTextureCoordinates, halfSizeX, halfSizeY);
52  }
53 
55  {
56  if(vao > 0) {
57  glDeleteBuffers(1, &vao);
58  vao = 0;
59  }
60 
61  if(vbo[0] > 0) {
62  glDeleteBuffers(1, &vbo[0]);
63 
64  vbo[0] = 0;
65  }
66 
67  if(vbo[1] > 0) {
68  glDeleteBuffers(1, &vbo[1]);
69 
70  vbo[1] = 0;
71  }
72  }
73 
78  void init(bool bTexCoordinates, float halfSizeX = 1.0f, float halfSizeY = 1.0f)
79  {
80  float *data_pos = createPosCoord(halfSizeX, halfSizeY);
81 
82  if(bTexCoordinates) {
83  float *data_tex = createTexCoord();
84 
85  //init VBO 0
86  glGenBuffers(1, &vbo[0]);
87  glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
88  glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), data_pos, GL_STATIC_DRAW);
89  glBindBuffer(GL_ARRAY_BUFFER, 0);
90 
91  //init VBO 1
92  glGenBuffers(1, &vbo[1]);
93  glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
94  glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), data_tex, GL_STATIC_DRAW);
95  glBindBuffer(GL_ARRAY_BUFFER, 0);
96 
97  //init VAO
98  glGenVertexArrays(1, &vao);
99  glBindVertexArray(vao);
100 
101  glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
102  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
103  glEnableVertexAttribArray(0);
104 
105  glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
106  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
107  glEnableVertexAttribArray(1);
108 
109  glBindVertexArray(0);
110  glDisableVertexAttribArray(0);
111  glBindBuffer(GL_ARRAY_BUFFER, 0);
112 
113  delete[] data_tex;
114  } else {
115  //init VBO
116  glGenBuffers(1, &vbo[0]);
117  glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
118  glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), data_pos, GL_STATIC_DRAW);
119  glBindBuffer(GL_ARRAY_BUFFER, 0);
120 
121  //init VAO
122  glGenVertexArrays(1, &vao);
123  glBindVertexArray(vao);
124  glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
125 
126  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
127 
128  glEnableVertexAttribArray(0);
129  glBindVertexArray(0);
130  glDisableVertexAttribArray(0);
131  glBindBuffer(GL_ARRAY_BUFFER, 0);
132  }
133 
134  delete[] data_pos;
135  }
136 
140  void Render()
141  {
142  glBindVertexArray(vao);
143  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
144  glBindVertexArray(0);
145  }
146 
152  void Render(TechniqueGL &technique, GLuint texture)
153  {
154  technique.bind();
155 
156  glEnable(GL_TEXTURE_2D);
157  glActiveTexture(GL_TEXTURE0);
158  glBindTexture(GL_TEXTURE_2D, texture);
159 
160  Render();
161 
162  glActiveTexture(GL_TEXTURE0);
163  glBindTexture(GL_TEXTURE_2D, 0);
164 
165  technique.unbind();
166  }
167 
172  static float *createPosCoord(float halfSizeX = 1.0f, float halfSizeY = 1.0f)
173  {
174  float *data = new float[8];
175 
176  data[0] = -halfSizeX;
177  data[1] = halfSizeY;
178 
179  data[2] = -halfSizeX;
180  data[3] = -halfSizeY;
181 
182  data[4] = halfSizeX;
183  data[5] = halfSizeY;
184 
185  data[6] = halfSizeX;
186  data[7] = -halfSizeY;
187  return data;
188  }
189 
194  static float *createTexCoord()
195  {
196  float *data = new float[8];
197 
198  data[0] = 0.0f;
199  data[1] = 1.0f;
200 
201  data[2] = 0.0f;
202  data[3] = 0.0f;
203 
204  data[4] = 1.0f;
205  data[5] = 1.0f;
206 
207  data[6] = 1.0f;
208  data[7] = 0.0f;
209  return data;
210  }
211 
216  static std::string getVertexProgramV3()
217  {
218  std::string vertex_program = MAKE_STRING
219  (
220  in vec3 a_position;
221 
222  void main(void) {
223  gl_Position = vec4(a_position, 1.0);
224  }
225  );
226 
227  return vertex_program;
228  }
229 
234  static std::string getVertexProgramV2()
235  {
236  std::string vertex_program = MAKE_STRING
237  (
238  in vec2 a_position;
239 
240  void main(void) {
241  gl_Position = vec4(a_position, 0.0, 1.0);
242  }
243  );
244 
245  return vertex_program;
246  }
247 
254  {
255  std::string vertex_program = MAKE_STRING
256  (
257  in vec2 a_position;
258  in vec2 a_tex_coord;
259  out vec2 v_tex_coord;
260 
261  void main(void) {
262  gl_Position = vec4(a_position, 0.0, 1.0);
263  v_tex_coord = a_tex_coord;
264  }
265  );
266 
267  return vertex_program;
268  }
269 
274  static std::string getFragmentProgram()
275  {
276 
277  std::string fragment_program = MAKE_STRING
278  (
279  uniform sampler2D u_tex;
280  out vec4 f_color;
281 
282  void main(void) {
283  ivec2 coords = ivec2(gl_FragCoord.xy);
284  f_color = vec4(texelFetch(u_tex, coords, 0).xyz, 1.0);
285  }
286  );
287 
288  return fragment_program;
289  }
290 
295  static std::string getFragmentProgramForView()
296  {
297 
298  std::string fragment_program = MAKE_STRING
299  (
300  uniform sampler2D u_tex;
301  out vec4 f_color;
302 
303  void main(void) {
304  ivec2 coords = ivec2(gl_FragCoord.xy);
305  ivec2 texSize = textureSize(u_tex, 0);
306  coords.y = texSize.y - coords.y;
307  f_color = vec4(texelFetch(u_tex, coords, 0).xyz, 1.0);
308  }
309  );
310 
311  return fragment_program;
312  }
313 
320  static void getTechnique(TechniqueGL &technique, std::string vp_src = "", std::string fp_src = "", bool bTextureCoordinates = false)
321  {
322  if(vp_src.empty() || fp_src.empty()) {
323  technique.init("330", getVertexProgramV3(), getFragmentProgram());
324  } else {
325  technique.init("330", vp_src, fp_src);
326  }
327 
328  #ifdef PIC_DEBUG
329  technique.printLog("QuadGL");
330  #endif
331 
332  technique.bind();
333  technique.setAttributeIndex("a_position", 0);
334 
335  if(bTextureCoordinates) {
336  technique.setAttributeIndex("a_tex_coord", 1);
337  }
338 
339  technique.setOutputFragmentShaderIndex("f_color", 0);
340  technique.link();
341  technique.unbind();
342 
343  technique.bind();
344  technique.setUniform1i("u_tex", 0);
345  technique.unbind();
346  }
347 
348  #ifdef PIC_DEPRECATED
349 
352  static void Draw()
353  {
354  glDisable(GL_DEPTH_TEST);
355 
356  glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
357 
358  //Rendering an aligned quad
359  glBegin(GL_TRIANGLE_STRIP);
360 
361  glVertex2f( -1.0f, 1.0f);
362  glVertex2f( -1.0f, -1.0f);
363  glVertex2f( 1.0f, 1.0f);
364  glVertex2f( 1.0f, -1.0f);
365 
366  glEnd();
367  }
368 
373  static void Draw(GLuint tex)
374  {
375  glEnable(GL_TEXTURE_2D);
376 
377  glActiveTexture(GL_TEXTURE0);
378  glBindTexture(GL_TEXTURE_2D, tex);
379 
380  glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
381 
382  //Rendering an aligned quad
383  glBegin(GL_TRIANGLE_STRIP);
384 
385  glTexCoord2f( 0.0f, 0.0f);
386  glVertex2f( -1.0f, 1.0f);
387 
388  glTexCoord2f( 0.0f, 1.0f);
389  glVertex2f( -1.0f, -1.0f);
390 
391  glTexCoord2f( 1.0f, 0.0f);
392  glVertex2f( 1.0f, 1.0f);
393 
394  glTexCoord2f( 1.0f, 1.0f);
395  glVertex2f( 1.0f, -1.0f);
396 
397  glEnd();
398 
399  glDisable(GL_TEXTURE_2D);
400  }
401 
407  static void Draw(GLuint tex, float *color)
408  {
409  glEnable(GL_TEXTURE_2D);
410  glActiveTexture(GL_TEXTURE0);
411  glBindTexture(GL_TEXTURE_2D, tex);
412 
413  if(color == NULL) {
414  glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
415  } else {
416  glColor3fv(color);
417  }
418 
419  //Rendering an aligned quad
420  glBegin(GL_TRIANGLE_STRIP);
421 
422  glTexCoord2f(0.0f, 0.0f);
423  glVertex2f( -1.0f, 1.0f);
424 
425  glTexCoord2f(0.0f, 1.0f);
426  glVertex2f( -1.0f, -1.0f);
427 
428  glTexCoord2f(1.0f, 0.0f);
429  glVertex2f( 1.0f, 1.0f);
430 
431  glTexCoord2f(1.0f, 1.0f);
432  glVertex2f( 1.0f, -1.0f);
433 
434  glEnd();
435 
436  glDisable(GL_TEXTURE_2D);
437  }
438 
446  static void Draw(GLuint texture, int width, int height, TechniqueGL &technique)
447  {
448  glFrontFace(GL_CW);
449 
450  glMatrixMode(GL_PROJECTION);
451  glLoadIdentity();
452 
453  glMatrixMode(GL_MODELVIEW);
454  glLoadIdentity();
455 
456  glDisable(GL_DEPTH_TEST);
457  glViewport(0, 0, (GLsizei)width, (GLsizei)height);
458 
459  technique.bind();
460 
461  glEnable(GL_TEXTURE_2D);
462  glActiveTexture(GL_TEXTURE0);
463  glBindTexture(GL_TEXTURE_2D, texture);
464 
465  //Rendering an aligned quad
466  glBegin(GL_TRIANGLE_STRIP);
467  glVertex2f(1.0f, -1.0f);
468 
469  glVertex2f(-1.0f, -1.0f);
470 
471  glVertex2f(1.0f, 1.0f);
472 
473  glVertex2f(-1.0f, 1.0f);
474 
475  glEnd();
476 
477  glDisable(GL_TEXTURE_2D);
478 
479  technique.unbind();
480  glEnable(GL_DEPTH_TEST);
481  }
482  #endif // end PIC_DEPRECATED
483 };
484 
485 } // end namespace pic
486 
487 #endif /* PIC_UTIL_GL_QUAD_HPP */
488 
QuadGL()
Definition: quad.hpp:38
static std::string getVertexProgramWithTexCoordinates()
getVertexProgramWithTexCoordinates creates a simple vertex program with texture coordinates as input...
Definition: quad.hpp:253
QuadGL(bool bTextureCoordinates, float halfSizeX=1.0f, float halfSizeY=1.0f)
Definition: quad.hpp:45
void init(bool bTexCoordinates, float halfSizeX=1.0f, float halfSizeY=1.0f)
init initializates the QuadGL by allocating memory on the GPU.
Definition: quad.hpp:78
void setOutputFragmentShaderIndex(const char *fragment_output_color_name, unsigned int index)
setOutputFragmentShaderIndex
Definition: technique.hpp:215
static std::string getVertexProgramV2()
getVertexProgramV2 creates a simple vertex program.
Definition: quad.hpp:234
void unbind()
unbind
Definition: technique.hpp:197
void link()
link
Definition: technique.hpp:205
#define MAKE_STRING(input_string)
void Render(TechniqueGL &technique, GLuint texture)
Render.
Definition: quad.hpp:152
bool init(std::string version_number, std::string vertex_shader_source, std::string fragment_shader_source)
Definition: technique.hpp:67
The TechniqueGL class.
Definition: technique.hpp:31
static std::string getVertexProgramV3()
getVertexProgramV3 creates a simple vertex program.
Definition: quad.hpp:216
The QuadGL class.
Definition: quad.hpp:30
static float * createPosCoord(float halfSizeX=1.0f, float halfSizeY=1.0f)
createPosCoord allocates memory for a position buffer.
Definition: quad.hpp:172
void printLog(std::string name)
printLog
Definition: technique.hpp:176
static std::string getFragmentProgram()
getFragmentProgram
Definition: quad.hpp:274
static void getTechnique(TechniqueGL &technique, std::string vp_src="", std::string fp_src="", bool bTextureCoordinates=false)
getProgram creates a simple program.
Definition: quad.hpp:320
void setAttributeIndex(const char *attribute_name, unsigned int index)
setAttributeIndex
Definition: technique.hpp:225
void Render()
Render draws a quad on screen.
Definition: quad.hpp:140
static float * createTexCoord()
createTexCoord allocates memory for a texture coordinates buffer.
Definition: quad.hpp:194
Definition: bilateral_separation.hpp:25
static std::string getFragmentProgramForView()
getFragmentProgramForView
Definition: quad.hpp:295
void setUniform1i(const char *name_uniform, int value0)
SetUniform.
Definition: technique.hpp:236
GLuint vbo[2]
Definition: quad.hpp:34
GLuint vao
Definition: quad.hpp:33
void bind()
bind
Definition: technique.hpp:189
~QuadGL()
Definition: quad.hpp:54