PICCANTE  0.4
The hottest HDR imaging library!
sampler_random_m.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_POINT_SAMPLERS_SAMPLER_RANDOM_M_HPP
19 #define PIC_GL_POINT_SAMPLERS_SAMPLER_RANDOM_M_HPP
20 
21 #include "../../base.hpp"
22 
23 #include "../../gl/image.hpp"
24 
25 #include "../../point_samplers/sampler_random_m.hpp"
26 
27 namespace pic {
28 
33 {
34  GLenum err = glGetError();
35 
36  if(err != GL_NO_ERROR) {
37  printf("----------- %d\n", err);
38  }
39 }
40 
44 template <unsigned int N>
45 class MRSamplersGL: public MRSamplers<N>
46 {
47 protected:
48  GLuint texture;
50  int width, height;
51 
52 public:
53  int nSamples;
54 
64  int nSamplers): MRSamplers<N>(type, window, nSamples, nLevels, nSamplers)
65  {
66  texture = 0;
67  }
68 
74  void updateGL(Vec<N, int> window, int nSamples);
75 
80  GLuint getTexture()
81  {
82  return texture;
83  }
84 
90  {
91  ImageGL *ret = new ImageGL(texture, GL_TEXTURE_2D);
92 
93  return ret;
94  }
95 
101  {
102  ImageGL *ret = new ImageGL(levelsRtexture, GL_TEXTURE_2D);
103 
104  return ret;
105  }
106 
111  GLuint generateTexture();
112 
118  {
119  return levelsRtexture;
120  }
121 
126  GLuint generateLevelsRTexture();
127 };
128 
129 template <unsigned int N> void MRSamplersGL<N>::updateGL(Vec<N, int> window,
130  int nSamples)
131 {
132  if(texture == -1) {
133  return;
134  }
135 
136 #ifdef PIC_DEBUG
137  printf("window: %d %d\n", window[0], window[1]);
138 #endif
139 
140  if(!this->update(window, nSamples)) {
141  return;
142  }
143 
144  glDeleteTextures(1, &texture);
145  generateTexture();
146 
147  this->oldSamples = nSamples;
148  this->oldWindow = window;
149 }
150 
151 template <unsigned int N> GLuint MRSamplersGL<N>::generateTexture()
152 {
153  if(this->nSamplers <= 0) {
154  texture = 0;
155 
156 #ifdef PIC_DEBUG
157  printf("No samplers in MRSamplersGL.\n");
158 #endif
159  nSamples = -1;
160  return 0;
161  }
162 
163  nSamples = int(this->samplers[0]->samplesR.size());
164 
165  for(int i = 1; i < this->nSamplers; i++) {
166  nSamples = MIN(nSamples, int(this->samplers[i]->samplesR.size()));
167  }
168 
169 #ifdef PIC_DEBUG
170  printf("Samples in samplers: %d %d\n", nSamples / N, N);
171 #endif
172 
173  //Create the buffer in the main memory
174  int *buffer = new int [this->nSamplers * (nSamples / N) * 4];
175 
176  int ind = 0;
177 
178  for(int i = 0; i < this->nSamplers; i++) {
179  for(int j = 0; j < nSamples; j += N) {
180  int ind2 = ind * 4;
181 
182  for(int k = 0; k < 4; k++) {
183  buffer[ind2 + k] = 0;
184  }
185 
186  for(int k = 0; k < N; k++) {
187  buffer[ind2 + k] = this->samplers[i]->samplesR[j + k];
188  }
189 
190  buffer[ind2 + N] = this->samplers[i]->samplesR[j] *
191  this->samplers[i]->samplesR[j] +
192  this->samplers[i]->samplesR[j + 1] * this->samplers[i]->samplesR[j + 1];
193  ind++;
194  }
195  }
196 
197  //Transfer the buffer into graphics card's memory
198  glGenTextures(1, &texture);
199  glBindTexture(GL_TEXTURE_2D, texture);
200  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
201  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
202  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
203  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
204  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
205 
206  width = nSamples / N;
207  height = this->nSamplers;
208 
209  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32I, width, height, 0,
210  GL_RGBA_INTEGER, GL_INT, buffer);
211 
212  //nSamples = nSamples>>1;
213  //glGenerateMipmap(GL_TEXTURE_2D);
214 
215  glBindTexture(GL_TEXTURE_2D, 0);
216 
217  //release memory from the buffer
218  delete[] buffer;
219  return texture;
220 }
221 
222 template <unsigned int N> GLuint MRSamplersGL<N>::generateLevelsRTexture()
223 {
224  //Create the buffer in the main memory
225  int *buffer = new int [this->nSamplers * this->nLevels];
226  int ind = 0;
227 
228  for(int i = 0; i < this->nSamplers; i++) {
229  for(int j = 0; j < this->nLevels; j++) {
230  buffer[ind] = this->samplers[i]->levelsR[j];
231  printf("%d ", buffer[ind]);
232  ind++;
233  }
234 
235  printf("\n");
236  }
237 
238  //Transfer the buffer into graphics card's memory
239  glGenTextures(1, &levelsRtexture);
240  glBindTexture(GL_TEXTURE_2D, levelsRtexture);
241  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
242  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
243  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
244  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
245  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
246 
247  glTexImage2D(GL_TEXTURE_2D, 0, GL_R32I, this->nLevels, this->nSamplers, 0,
248  GL_RED_INTEGER, GL_INT, buffer);
249 
250  glBindTexture(GL_TEXTURE_2D, 0);
251 
252  //release memory from the buffer
253  delete[] buffer;
254  return levelsRtexture;
255 }
256 
257 } // end namespace pic
258 
259 #endif /* PIC_GL_POINT_SAMPLERS_SAMPLER_RANDOM_M_HPP */
260 
ImageGL * getImage()
getImage
Definition: sampler_random_m.hpp:89
SAMPLER_TYPE type
Definition: display.hpp:41
GLuint generateTexture()
generateTexture
The MRSamplers class.
Definition: sampler_random_m.hpp:34
PIC_INLINE void glGetPrintError()
glGetPrintError
Definition: sampler_random_m.hpp:32
The ImageGL class.
Definition: image.hpp:42
int height
Definition: sampler_random_m.hpp:50
int nLevels
Definition: display.hpp:44
int nSamplers
Definition: display.hpp:38
void updateGL(Vec< N, int > window, int nSamples)
updateGL
GLuint texture
Definition: sampler_random_m.hpp:48
The MRSamplersGL class.
Definition: sampler_random_m.hpp:45
SAMPLER_TYPE
Definition: point_samplers.hpp:51
#define PIC_INLINE
Definition: base.hpp:33
#define MIN(a, b)
Definition: math.hpp:69
Definition: bilateral_separation.hpp:25
MRSamplersGL(SAMPLER_TYPE type, Vec< N, int > window, int nSamples, int nLevels, int nSamplers)
MRSamplersGL.
Definition: sampler_random_m.hpp:63
GLuint getTexture()
getTexture
Definition: sampler_random_m.hpp:80
The Vec class.
Definition: vec.hpp:35
int nSamples
Definition: sampler_random_m.hpp:53
GLuint levelsRtexture
Definition: sampler_random_m.hpp:49
ImageGL * getImageLevelsR()
getImageLevelsR
Definition: sampler_random_m.hpp:100
GLuint getLevelsRTexture()
getLevelsRTexture
Definition: sampler_random_m.hpp:117
int width
Definition: sampler_random_m.hpp:50
GLuint generateLevelsRTexture()
generateLevelsRTexture