PICCANTE  0.4
The hottest HDR imaging library!
buffer_allocation.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_BUFFER_ALLOCATION_HPP
19 #define PIC_UTIL_GL_BUFFER_ALLOCATION_HPP
20 
21 #include "../../base.hpp"
22 
23 #include "../../util/string.hpp"
24 #include "../../util/gl/quad.hpp"
25 
26 namespace pic {
27 
37 PIC_INLINE GLuint generateTexture2DGL(int width, int height, int channels, float *data = NULL, bool mipmap = false)
38 {
39  if(width < 1 || height < 1 || channels < 1) {
40  return 0;
41  }
42 
43  GLuint texture;
44 
45  glGenTextures(1, &texture);
46  glBindTexture(GL_TEXTURE_2D, texture);
47 
48  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
49 
50  if(mipmap) {
51  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
52  } else {
53  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
54  }
55 
56  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
57  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
58 
59  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
60 
61  int mode, modeInternalFormat;
62  getModesGL(channels, mode, modeInternalFormat);
63  glTexImage2D(GL_TEXTURE_2D, 0, modeInternalFormat, width, height, 0,
64  mode, GL_FLOAT, data);
65 
66  if(mipmap) {
67  glGenerateMipmap(GL_TEXTURE_2D);
68  }
69 
70  glBindTexture(GL_TEXTURE_2D, 0);
71 
72  return texture;
73 }
74 
84 PIC_INLINE GLuint generateTextureCubeMapGL(int width, int height, int channels, int frames, float *data = NULL)
85 {
86  if(width < 1 || height < 1 || channels < 1 || frames < 6) {
87  return 0;
88  }
89 
90  GLuint texture;
91 
92  int mode, modeInternalFormat;
93  getModesGL(channels, mode, modeInternalFormat);
94 
95  glGenTextures(1, &texture);
96  glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
97  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
98  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
99  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
100  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
101  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
102 
103  //Order Pos, Neg and X, Y, Z
104  int tstride = width * height * channels;
105 
106  for(int i = 0; i < 6; i++) {
107  glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, modeInternalFormat, width,
108  height, 0, mode, GL_FLOAT, &data[tstride * i]);
109  }
110 
111  glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
112 
113  return texture;
114 }
115 
125 PIC_INLINE GLuint generateTexture3DGL(int width, int height, int channels, int frames, float *data = NULL)
126 {
127  if(width <1 || height < 1 || channels < 1 || frames < 1) {
128  return 0;
129  }
130 
131  GLuint texture;
132 
133  int mode, modeInternalFormat;
134  getModesGL(channels, mode, modeInternalFormat);
135 
136  glGenTextures(1, &texture);
137  glBindTexture(GL_TEXTURE_3D, texture);
138  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
139  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
140  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
141  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
142  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
143 
144  glTexImage3D(GL_TEXTURE_3D, 0, modeInternalFormat, width, height, frames, 0,
145  mode, GL_FLOAT, data);
146 
147  glBindTexture(GL_TEXTURE_3D, 0);
148 
149 // for(int i=0;i<frames;i++)
150 // glTexSubImage3D(GL_TEXTURE_3D,0,0,0,i,width,height,1,mode,GL_FLOAT,&data[i*tstride]);
151 
152  return texture;
153 }
154 
164 PIC_INLINE GLuint generateTexture2DArrayGL(int width, int height, int channels, int frames, float *data = NULL)
165 {
166  if(width < 1 || height < 1 || channels < 1 || frames < 1) {
167  return 0;
168  }
169 
170  int mode, modeInternalFormat;
171  getModesGL(channels, mode, modeInternalFormat);
172 
173  GLuint texture;
174 
175  glGenTextures(1, &texture);
176  glBindTexture(GL_TEXTURE_2D_ARRAY, texture);
177  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
178  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
179  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
180  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
181 
182  glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, modeInternalFormat, width, height, frames,
183  0, mode, GL_FLOAT, data);
184 
185  glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
186 
187  return texture;
188 }
189 
197 PIC_INLINE GLuint generateTexture2DU32GL(int width, int height, int channels, int *data = NULL)
198 {
199  if(width < 1 || height < 1 || channels < 1) {
200  return 0;
201  }
202 
203  GLuint texture;
204 
205  glGenTextures(1, &texture);
206  glBindTexture(GL_TEXTURE_2D, texture);
207 
208  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
209  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
210 
211  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
212  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
213 
214  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
215 
216  int mode, modeInternalFormat;
217  getModesIntegerGL(channels, mode, modeInternalFormat);
218 
219  glTexImage2D(GL_TEXTURE_2D, 0, modeInternalFormat, width, height, 0,
220  mode, GL_INT, data);
221 
222  glBindTexture(GL_TEXTURE_2D, 0);
223 
224  return texture;
225 }
226 
227 } // end namespace pic
228 
229 #endif /* PIC_UTIL_GL_BUFFER_ALLOCATION_HPP */
void getModesIntegerGL(int channels, int &mode, int &modeInternalFormat)
getModesIntegerGL
Definition: formats.hpp:97
PIC_INLINE GLuint generateTexture2DU32GL(int width, int height, int channels, int *data=NULL)
generateTexture2DU32GL
Definition: buffer_allocation.hpp:197
PIC_INLINE GLuint generateTexture2DGL(int width, int height, int channels, float *data=NULL, bool mipmap=false)
generateTexture2DGL
Definition: buffer_allocation.hpp:37
PIC_INLINE GLuint generateTextureCubeMapGL(int width, int height, int channels, int frames, float *data=NULL)
generateTextureCubeMapGL
Definition: buffer_allocation.hpp:84
void getModesGL(int channels, int &mode, int &modeInternalFormat)
getModesGL
Definition: formats.hpp:33
PIC_INLINE GLuint generateTexture3DGL(int width, int height, int channels, int frames, float *data=NULL)
generateTexture3DGL
Definition: buffer_allocation.hpp:125
#define PIC_INLINE
Definition: base.hpp:33
Definition: bilateral_separation.hpp:25
PIC_INLINE GLuint generateTexture2DArrayGL(int width, int height, int channels, int frames, float *data=NULL)
generateTexture2DArrayGL
Definition: buffer_allocation.hpp:164