PICCANTE  0.4
The hottest HDR imaging library!
filter_scatter.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_GL_FILTERING_FILTER_SCATTER_HPP
19 #define PIC_GL_FILTERING_FILTER_SCATTER_HPP
20 
21 #include "../../base.hpp"
22 #include "../../util/std_util.hpp"
23 #include "../../gl/filtering/filter.hpp"
24 
25 namespace pic {
26 
33 {
34 protected:
35 
36  GLfloat *vertex_array;
38  GLuint vbo, vao;
39 
45  void generateVertexArray(int width, int height);
46 
50  void initShaders();
51 
55  void FragmentShader();
56 
57  float s_S, s_R, mul_E;
58 
59 public:
60 
68  FilterGLScatter(float s_S, float s_R, int width, int height);
69 
71 
75  void releaseAux()
76  {
78 
79  if(vbo != 0) {
80  glDeleteBuffers(1, &vbo);
81  vbo = 0;
82  }
83 
84  if(vao != 0) {
85  glDeleteVertexArrays(1, &vao);
86  vao = 0;
87  }
88  }
89 
95  void update(float s_S, float s_R);
96 
103  ImageGL *Process(ImageGLVec imgIn, ImageGL *imgOut);
104 };
105 
106 PIC_INLINE FilterGLScatter::FilterGLScatter(float s_S, float s_R, int width, int height)
107 {
108  this->s_S = s_S;
109  this->s_R = s_R;
110 
111  vertex_array = NULL;
112 
113  generateVertexArray(width, height);
114 
115  FragmentShader();
116  initShaders();
117 }
118 
120 {
121  release();
122 }
123 
125 {
127 
128  vertex_array = new GLfloat[2 * width * height];
129  nVertex_array = width * height;
130 
131  int index = 0;
132 
133  for(int i = 0; i < height; i++) {
134  float i_f = float(i);
135 
136  for(int j = 0; j < width; j++) {
137  vertex_array[index++] = float(j);
138  vertex_array[index++] = i_f;
139  }
140  }
141 
142  //Vertex Buffer Object
143  glGenBuffers(1, &vbo);
144  glBindBuffer(GL_ARRAY_BUFFER, vbo);
145  glBufferData(GL_ARRAY_BUFFER, 2 * nVertex_array * sizeof(GLfloat), vertex_array,
146  GL_STATIC_DRAW);
147  glBindBuffer(GL_ARRAY_BUFFER, 0);
148 
149  //Vertex Array Object
150  glGenVertexArrays(1, &vao);
151  glBindVertexArray(vao);
152  glBindBuffer(GL_ARRAY_BUFFER, vbo);
153 
154  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
155 
156  glEnableVertexAttribArray(0);
157  glBindVertexArray(0);
158  glDisableVertexAttribArray(0);
159  glBindBuffer(GL_ARRAY_BUFFER, 0);
160 }
161 
163 {
165 
166  uniform sampler2D u_tex;
167  uniform float s_S;
168  uniform float mul_E;
169 
170  layout(location = 0) in vec2 a_position;
171 
172  flat out vec4 v2g_color;
173  flat out int v2g_layer;
174 
175  void main(void) {
176  //Texture Fetch
177  vec4 data = texelFetch(u_tex, ivec2(a_position), 0);
178 
179  //Output coordinate
180  vec2 coord = vec2(a_position) / vec2(textureSize(u_tex, 0) - ivec2(1));
181  coord = coord * 2.0 - vec2(1.0);
182 
183  v2g_color = vec4(data.xyz, 1.0);
184  v2g_layer = int(floor(dot(data.xyz, vec3(1.0)) * mul_E));
185 
186  gl_Position = vec4(coord, 0.0, 1.0);
187  }
188  );
189 
191 
192  layout(points) in;
193  layout(points, max_vertices = 1) out;
194 
195  flat in vec4 v2g_color[1];
196  flat in int v2g_layer[1];
197  flat out vec4 g2f_color;
198 
199  void main(void) {
200  g2f_color = v2g_color[0];
201  gl_Layer = v2g_layer[0];
202 
203  gl_PointSize = 1.0;
204  gl_Position = gl_in[0].gl_Position;
205  EmitVertex();
206 
207  EndPrimitive();
208  }
209  );
210 
212  flat in vec4 g2f_color;
213  layout(location = 0) out vec4 f_color;
214 
215  void main(void) {
216  f_color = g2f_color;
217  }
218  );
219 }
220 
222 {
224 
225  update(s_S, s_R);
226 }
227 
228 PIC_INLINE void FilterGLScatter::update(float s_S, float s_R)
229 {
230  this->s_S = s_S;
231  this->s_R = s_R;
232 
233  mul_E = s_R / 3.0f;
234 
235  #ifdef PIC_DEBUG
236  printf("Rate S: %f Rate R: %f Mul E: %f\n", s_S, s_R, mul_E);
237  #endif
238 
239  technique.bind();
240  technique.setUniform1i("u_tex", 0);
241  technique.setUniform1f("s_S", s_S);
242  technique.setUniform1f("mul_E", mul_E);
243  technique.unbind();
244 }
245 
247 {
248  if(imgIn.size() < 1 && imgIn[0] == NULL) {
249  return imgOut;
250  }
251 
252  int width, height, range;
253  width = int(ceilf(float(imgIn[0]->width) * s_S));
254  height = int(ceilf(float(imgIn[0]->height) * s_S));
255  range = int(ceilf(s_R));
256 
257  if(imgOut == NULL) {
258  imgOut = new ImageGL(range + 1, width + 1, height + 1,
259  imgIn[0]->channels + 1, IMG_GPU, GL_TEXTURE_3D);
260  }
261 
262  if(fbo == NULL) {
263  fbo = new Fbo();
264  fbo->create(width + 1, height + 1, range + 1, false, imgOut->getTexture());
265  }
266 
267  //Rendering
268  fbo->bind();
269 
270  glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
271  imgOut->getTexture(), 0);
272 
273  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
274  glClear(GL_COLOR_BUFFER_BIT);
275 
276  glViewport(0, 0, (GLsizei)width, (GLsizei)height);
277 
278  //Shaders
279  technique.bind();
280 
281  //Textures
282  glActiveTexture(GL_TEXTURE0);
283  imgIn[0]->bindTexture();
284 
285  glEnable(GL_BLEND);
286  glBlendFunc(GL_ONE, GL_ONE);
287 
288  glBindVertexArray(vao);
289  glDrawArrays(GL_POINTS, 0, nVertex_array);
290  glBindVertexArray(0);
291 
292  glDisable(GL_BLEND);
293 
294  //Fbo
295  fbo->unbind();
296 
297  //Shaders
298  technique.unbind();
299 
300  //Textures
301  glActiveTexture(GL_TEXTURE0);
302  imgIn[0]->unBindTexture();
303 
304  return imgOut;
305 }
306 
307 } // end namespace pic
308 
309 #endif /* PIC_GL_FILTERING_FILTER_SCATTER_HPP */
310 
The FilterGLScatter class implement the bilateral grid approximation of the bilateral filter...
Definition: filter_scatter.hpp:32
TechniqueGL technique
Definition: display.hpp:45
The Fbo class.
Definition: display.hpp:32
float s_R
Definition: filter_scatter.hpp:57
bool create(int width, int height, bool bDepth)
create
Definition: display.hpp:207
void bind()
bind
Definition: display.hpp:189
float mul_E
Definition: filter_scatter.hpp:57
#define MAKE_STRING(input_string)
ImageGL * Process(ImageGLVec imgIn, ImageGL *imgOut)
Process.
void releaseAux()
releaseAux
Definition: filter_scatter.hpp:75
FilterGLScatter(float s_S, float s_R, int width, int height)
FilterGLScatter.
T * delete_vec_s(T *data)
delete_vec_s
Definition: std_util.hpp:138
void initShaders()
initShaders
The ImageGL class.
Definition: image.hpp:42
GLuint getTexture() const
getTexture
Definition: display.hpp:369
GLuint vao
Definition: filter_scatter.hpp:38
void setUniform1f(const char *name_uniform, float value0)
SetUniform1f.
Definition: display.hpp:247
void generateVertexArray(int width, int height)
generateVertexArray
Fbo * fbo
Definition: display.hpp:39
void bind()
bind
Definition: display.hpp:427
The FilterGL class.
Definition: filter.hpp:35
#define PIC_INLINE
Definition: base.hpp:33
The ImageGL class.
Definition: display.hpp:42
bool initStandard(std::string version_number, std::string vertex_shader_source, std::string fragment_shader_source, std::string name)
initStandard
Definition: display.hpp:114
GLfloat * vertex_array
Definition: filter_scatter.hpp:36
float s_S
Definition: filter_scatter.hpp:57
int nVertex_array
Definition: filter_scatter.hpp:37
void FragmentShader()
FragmentShader.
void unbind()
unbind
Definition: display.hpp:197
std::string vertex_source
Definition: display.hpp:57
Definition: bilateral_separation.hpp:25
void unbind()
unbind
Definition: display.hpp:442
std::vector< ImageGL * > ImageGLVec
ImageGLVec an std::vector of pic::ImageGL.
Definition: display.hpp:32
std::string geometry_source
Definition: display.hpp:57
std::string fragment_source
Definition: display.hpp:57
Definition: image.hpp:37
std::vector< ImageGL * > ImageGLVec
ImageGLVec an std::vector of pic::ImageGL.
Definition: image_vec.hpp:32
void release()
release
Definition: display.hpp:85
void setUniform1i(const char *name_uniform, int value0)
SetUniform.
Definition: display.hpp:236
void update(float s_S, float s_R)
update
GLuint vbo
Definition: filter_scatter.hpp:38