PICCANTE  0.4
The hottest HDR imaging library!
filter_conv_2d.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_2D_HPP
19 #define PIC_GL_FILTERING_FILTER_CONV_2D_HPP
20 
21 #include "../../base.hpp"
22 
23 #include "../../gl/filtering/filter.hpp"
24 
25 namespace pic {
26 
30 class FilterGLConv2D: public FilterGL
31 {
32 protected:
33 
34  GLenum target;
35 
39  void FragmentShader();
40 
41 public:
42 
47  FilterGLConv2D(GLenum target);
48 
52  void initShaders();
53 
58  void setUniform()
59  {
60  technique.bind();
61  technique.setUniform1i("u_tex", 0);
62  technique.setUniform1i("u_weights", 1);
63 
64  if(target == GL_TEXTURE_3D || target == GL_TEXTURE_2D_ARRAY) {
65  // technique.setUniform("slice", slice);
66  }
67 
68  technique.unbind();
69  }
70 };
71 
73 {
74  this->target = target;
75 
77  initShaders();
78 }
79 
81 {
82  technique.initStandard("330", vertex_source, fragment_source, "FilterGLConv2D");
83 
84  setUniform();
85 }
86 
88 {
89  std::string fragment_source_2D = MAKE_STRING
90  (
91  uniform sampler2D u_tex;
92  uniform sampler2D u_weights;
93  out vec4 f_color;
94 
95  void main(void) {
96  ivec2 kernelSize = textureSize(u_weights, 0);
97  ivec2 halfKernelSize = kernelSize >> 1;
98 
99  ivec2 shift = ivec2(halfKernelSize.x, halfKernelSize.y);
100  ivec2 coordsFrag = ivec2(gl_FragCoord.xy) - shift;
101 
102  vec4 color = vec4(0.0);
103 
104  for(int i = 0; i < kernelSize.y; i++) {
105 
106  for(int j = 0; j < kernelSize.x; j++) {
107  //do a texture fetch
108  vec4 tmpCol = texelFetch(u_tex, coordsFrag.xy + ivec2(j, i), 0);
109 
110  //weight
111  color += tmpCol * texelFetch(u_weights, ivec2(j, i), 0);
112  }
113  }
114 
115  f_color = color;
116  }
117  );
118 
119  switch(target) {
120  case GL_TEXTURE_2D: {
121  fragment_source = fragment_source_2D;
122  }
123  break;
124 
125  case GL_TEXTURE_3D: {
126  //fragment_source = fragment_source_3D;
127  }
128  break;
129  }
130 }
131 
132 } // end namespace pic
133 
134 #endif /* PIC_GL_FILTERING_FILTER_CONV_2D_HPP */
135 
TechniqueGL technique
Definition: display.hpp:45
void FragmentShader()
FragmentShader.
void bind()
bind
Definition: display.hpp:189
#define MAKE_STRING(input_string)
void initShaders()
initShaders
GLenum target
Definition: filter_conv_2d.hpp:34
The FilterGLConv2D class.
Definition: filter_conv_2d.hpp:30
The FilterGL class.
Definition: filter.hpp:35
void setUniform()
setUniform
Definition: filter_conv_2d.hpp:58
The FilterGL class.
Definition: display.hpp:35
#define PIC_INLINE
Definition: base.hpp:33
bool initStandard(std::string version_number, std::string vertex_shader_source, std::string fragment_shader_source, std::string name)
initStandard
Definition: display.hpp:114
FilterGLConv2D(GLenum target)
FilterGLConv2D.
void unbind()
unbind
Definition: display.hpp:197
std::string vertex_source
Definition: display.hpp:57
Definition: bilateral_separation.hpp:25
std::string fragment_source
Definition: display.hpp:57
void setUniform1i(const char *name_uniform, int value0)
SetUniform.
Definition: display.hpp:236