PICCANTE  0.4
The hottest HDR imaging library!
filter_conv_1d.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_CONV_1D_HPP
19 #define PIC_GL_FILTERING_FILTER_CONV_1D_HPP
20 
21 #include "../../base.hpp"
22 #include "../../gl/filtering/filter_1d.hpp"
23 
24 namespace pic {
25 
30 {
31 protected:
32 
36  void FragmentShader();
37 
38 public:
39 
44 
51  FilterGLConv1D(ImageGL *weights, int direction, GLenum target);
52 
54 
60  {
61  int kernelSize = 0;
62  int halfKernelSize = 0;
63 
64  if(weights != NULL) {
65  kernelSize = weights->width;
66  halfKernelSize = kernelSize >> 1;
67  }
68 
69  technique.setUniform1i("u_weights", 1);
70  technique.setUniform1i("halfKernelSize", halfKernelSize);
71  technique.setUniform1i("kernelSize", kernelSize);
72  }
73 };
74 
76  GLenum target): FilterGL1D(direction, target)
77 {
78  this->weights = weights;
79 
81  initShaders();
82 }
83 
85 {
86  release();
87 }
88 
90 {
91  std::string fragment_source_2D = MAKE_STRING
92  (
93  uniform sampler2D u_tex;
94  uniform sampler2D u_weights;
95  uniform int iX;
96  uniform int iY;
97  uniform int halfKernelSize;
98  uniform int kernelSize;
99  out vec4 f_color;
100 
101  void main(void) {
102  vec4 color = vec4(0.0);
103  ivec2 coordsFrag = ivec2(gl_FragCoord.xy);
104  vec4 tmpCol;
105  float weight = 0.0;
106 
107  for(int i = 0; i < kernelSize; i++) {
108  //Coordinates
109  int j = i - halfKernelSize;
110  ivec2 coords = ivec2(j * iX, j * iY);
111  //Texture fetch
112  tmpCol = texelFetch(u_tex, coordsFrag.xy + coords.xy, 0);
113  //Weight
114  float tmp = texelFetch(u_weights, ivec2(i, 0), 0).x;
115  color += tmpCol * tmp;
116  weight += tmp;
117  }
118 
119  f_color = vec4(color / weight);
120  }
121  );
122 
123  std::string fragment_source_3D = MAKE_STRING
124  (
125  uniform sampler3D u_tex;
126  uniform sampler2D u_weights;
127  uniform int slice;
128  uniform int iX;
129  uniform int iY;
130  uniform int iZ;
131  uniform int halfKernelSize;
132  uniform int kernelSize;
133  out vec4 f_color;
134 
135  void main(void) {
136  vec4 color = vec4(0.0);
137  ivec3 coordsFrag = ivec3(ivec2(gl_FragCoord.xy), slice);
138  vec4 tmpCol;
139  float weight = 0.0;
140 
141  for(int i = 0; i < kernelSize; i++) {
142  //Coordinates
143  int j = i - halfKernelSize;
144  ivec3 coords = coordsFrag.xyz + ivec3(j * iX, j * iY, j * iZ);
145  //Texture fetch
146  tmpCol = texelFetch(u_tex, coords.xyz, 0);
147  //Weight
148  float tmp = texelFetch(u_weights, ivec2(i, 0), 0).x;
149  color += tmpCol * tmp;
150  weight += tmp;
151  }
152 
153  f_color = vec4(color / weight);
154  }
155  );
156 
157  switch(target) {
158  case GL_TEXTURE_2D: {
159  fragment_source = fragment_source_2D;
160  }
161  break;
162 
163  case GL_TEXTURE_3D: {
164  fragment_source = fragment_source_3D;
165  }
166  break;
167  }
168 }
169 
170 } // end namespace pic
171 
172 #endif /* PIC_GL_FILTERING_FILTER_CONV_1D_HPP */
173 
TechniqueGL technique
Definition: display.hpp:45
ImageGL * weights
Definition: display.hpp:35
int slice
Definition: display.hpp:37
The FilterGL1D class.
Definition: filter_1d.hpp:32
virtual void initShaders()
initShaders
Definition: filter_1d.hpp:175
GLenum target
Definition: display.hpp:47
#define MAKE_STRING(input_string)
The ImageGL class.
Definition: image.hpp:42
The FilterGLConv1D class.
Definition: filter_conv_1d.hpp:29
#define PIC_INLINE
Definition: base.hpp:33
The ImageGL class.
Definition: display.hpp:42
int width
Definition: filter_radial_basis_function.hpp:80
void FragmentShader()
FragmentShader.
Definition: bilateral_separation.hpp:25
FilterGLConv1D()
FilterGLConv1D.
std::string fragment_source
Definition: display.hpp:57
The FilterGL1D class.
Definition: display.hpp:32
void setUniformAux()
setUniformAux
Definition: filter_conv_1d.hpp:59
void release()
release
Definition: display.hpp:85
void setUniform1i(const char *name_uniform, int value0)
SetUniform.
Definition: display.hpp:236