Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
glfTexture.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2008, 2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include "glfTexture.hh"
26 #include "glfException.hh"
27 
28 using namespace BIAS;
29 
30 namespace BIAS {
31  BIASOpenGLFramework_EXPORT void SwapGLObjects(glfTexture& A, glfTexture& B)
32  {
33  if(A.id_ == 0) {
34  GLF_THROW_EXCEPTION("First Swap partner has no GL-Object assigned!");
35  }
36  if(B.id_ == 0) {
37  GLF_THROW_EXCEPTION("Second Swap partner has no GL-Object assigned!");
38  }
39  if(A.target_!= B.target_) {
40  GLF_THROW_EXCEPTION("swap partners relate to different targets!");
41  }
42  GLuint swapCache = A.id_;
43  A.id_ = B.id_;
44  B.id_ = swapCache;
45  }
46 }
47 
49 glfTexture::operator=(const glfTexture& b)
50 {
51  id_ = 0;
52  target_ = b.target_;
55  return *this;
56 }
57 
59 {
60  (*this) = t;
61 }
62 
63 glfTexture::glfTexture(GLenum target)
64 {
65  id_ = 0;
66  target_ = target;
67  textureNr_ = GL_TEXTURE0;
68  enlargeToPot_ = false;
69 }
70 
72 {
73  if (id_ != 0)
74  {
75  glDeleteTextures(1, &id_);
76  }
77 }
78 
79 void
81 {
82  if (id_ == 0)
83  {
84  glGenTextures(1, &id_);
85  }
86 }
87 
88 void
89 glfTexture::SetMinFilter(GLenum minFilter)
90 {
91  Bind();
92  glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, (GLint) minFilter);
93  GLF_THROW_ON_OPENGL_ERROR;
94 }
95 
96 void
97 glfTexture::SetMagFilter(GLenum magFilter)
98 {
99  Bind();
100  glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, (GLint) magFilter);
101  GLF_THROW_ON_OPENGL_ERROR;
102 }
103 
104 void
105 glfTexture::SetWrapS(GLenum wrapS)
106 {
107  Bind();
108  glTexParameteri(target_, GL_TEXTURE_WRAP_S, (GLint) wrapS);
109  GLF_THROW_ON_OPENGL_ERROR;
110 }
111 
112 void
113 glfTexture::SetWrapT(GLenum wrapT)
114 {
115  Bind();
116  glTexParameteri(target_, GL_TEXTURE_WRAP_T, (GLint) wrapT);
117  GLF_THROW_ON_OPENGL_ERROR;
118 }
119 
120 void
121 glfTexture::SetWrapR(GLenum wrapR)
122 {
123  Bind();
124  glTexParameteri(target_, GL_TEXTURE_WRAP_R, (GLint) wrapR);
125  GLF_THROW_ON_OPENGL_ERROR;
126 }
127 
128 void
129 glfTexture::SetGenerateMipMap(bool generateMipMap)
130 {
131  Bind();
132  glTexParameteri(target_, GL_GENERATE_MIPMAP, (GLint) generateMipMap);
133  GLF_THROW_ON_OPENGL_ERROR;
134 }
135 
136 void
138 {
139  Bind();
140  glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, (GLint) baseLevel);
141  GLF_THROW_ON_OPENGL_ERROR;
142 }
143 
144 void
146 {
147  Bind();
148  glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, (GLint) maxLevel);
149  GLF_THROW_ON_OPENGL_ERROR;
150 }
151 
152 void
153 glfTexture::SetBorderColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
154 {
155  Bind();
156  GLfloat rgba[4] =
157  { r, g, b, a };
158  glTexParameterfv(target_, GL_TEXTURE_BORDER_COLOR, rgba);
159  GLF_THROW_ON_OPENGL_ERROR;
160 }
161 
162 void
163 glfTexture::EnlargeToPowerOfTwoIfRequired(bool enlargeToPotIfRequired)
164 {
165  bool supportNonPot = false;
166 
167 #ifdef BIAS_HAVE_GLEW
168  if (GLEW_ARB_texture_non_power_of_two)
169  {
170  supportNonPot = true;
171  }
172 #endif
173 
174  if (supportNonPot)
175  {
176  enlargeToPot_ = false;
177  }
178  else
179  {
180  enlargeToPot_ = enlargeToPotIfRequired;
181  }
182 }
183 
184 int
186 {
187  GLint TUs = 0;
188  glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &TUs);\
189 
190  return static_cast<int> (TUs);
191 }
192 
193 void
195 {
196  if (!glewIsSupported("GL_EXT_framebuffer_object"))
197  {
198  GLF_THROW_EXCEPTION(
199  "GL_EXT_framebuffer_object is not supported but required "
200  "for glfTexture::GenerateMipMap");
201  }
202  Bind();
203  glGenerateMipmapEXT(target_);
204 }
205 
206 void
207 glfTexture::SetTextureNr(GLint textureNr)
208 {
210  if ((textureNr >= GL_TEXTURE0) && (textureNr < (GL_TEXTURE0
211  + MAX_TEXTURE_UNITS_)))
212  {
213  textureNr_ = textureNr;
214  }
215 #ifdef BIAS_DEBUG
216  else
217  {
218  BIASWARN("Invalid texture unit! Valid range is GL_TEXTURE0 - GL_TEXTURE"<<MAX_TEXTURE_UNITS_-1);
219  }
220 #endif
221 }
222 
223 void
225 {
227  GLint textureNrGl = textureNr + GL_TEXTURE0;
228  if ((textureNrGl >= GL_TEXTURE0) && (textureNrGl < (GL_TEXTURE0
229  + MAX_TEXTURE_UNITS_)))
230  {
231  textureNr_ = textureNrGl;
232  }
233 #ifdef BIAS_DEBUG
234  else
235  {
236  BIASWARN("Invalid texture unit! Valid range is 0 - "<<MAX_TEXTURE_UNITS_-1);
237  }
238 #endif
239 }
240 
241 void
243 {
244  BIASASSERT(id_ != 0);
245  glBindTexture(target_, id_);
246 }
247 
248 void
250 {
251  BIASASSERT(id_ != 0);
252 
253  glActiveTexture(textureNr_);
254 
255  glBindTexture(target_, id_);
256 }
257 
258 void
259 glfTexture::BindTU(int number) const
260 {
261  BIASASSERT(id_ != 0);
262 
263  glActiveTexture(GL_TEXTURE0+number);
264 
265  glBindTexture(target_, id_);
266 }
267 
268 int
270 {
271  int result = 1;
272  while (result < i)
273  {
274  result <<= 1;
275  }
276  return result;
277 }
278 
279 // void
280 // glfTexture::
281 // GetFormatForInternalFormat(GLenum internalFormat, GLenum& format,
282 // int& numChannels)
283 // {
284 // switch (internalFormat)
285 // {
286 // #ifdef GL_VERSION_3_0
287 // case GL_R32F:
288 // format = GL_RED;
289 // numChannels = 1;
290 // break;
291 // #endif
292 
293 // case GL_ALPHA:
294 // case GL_ALPHA4:
295 // case GL_ALPHA8:
296 // case GL_ALPHA12:
297 // case GL_ALPHA16:
298 // case GL_COMPRESSED_ALPHA:
299 // case GL_INTENSITY:
300 // case GL_INTENSITY4:
301 // case GL_INTENSITY8:
302 // case GL_INTENSITY12:
303 // case GL_INTENSITY16:
304 // case GL_COMPRESSED_INTENSITY:
305 // format = GL_ALPHA;
306 // numChannels = 1;
307 // break;
308 
309 // case GL_LUMINANCE:
310 // case GL_LUMINANCE4:
311 // case GL_LUMINANCE8:
312 // case GL_LUMINANCE12:
313 // case GL_LUMINANCE16:
314 // //case GL_SLUMINANCE:
315 // //case GL_SLUMINANCE8:
316 // case GL_COMPRESSED_LUMINANCE:
317 // case GL_LUMINANCE16F_ARB:
318 // case GL_LUMINANCE32F_ARB:
319 // //format = GL_LUMINANCE;
320 // format = GL_RED;
321 // numChannels = 1;
322 // break;
323 
324 // case GL_LUMINANCE_ALPHA:
325 // case GL_LUMINANCE4_ALPHA4:
326 // case GL_LUMINANCE6_ALPHA2:
327 // case GL_LUMINANCE8_ALPHA8:
328 // case GL_LUMINANCE12_ALPHA4:
329 // case GL_LUMINANCE12_ALPHA12:
330 // case GL_LUMINANCE16_ALPHA16:
331 // //case GL_SLUMINANCE_ALPHA:
332 // //case GL_SLUMINANCE8_ALPHA8:
333 // case GL_COMPRESSED_LUMINANCE_ALPHA:
334 // case GL_LUMINANCE_ALPHA16F_ARB:
335 // case GL_LUMINANCE_ALPHA32F_ARB:
336 // format = GL_LUMINANCE_ALPHA;
337 // numChannels = 2;
338 // break;
339 
340 // case GL_R3_G3_B2:
341 // case GL_RGB:
342 // case GL_RGB4:
343 // case GL_RGB5:
344 // case GL_RGB8:
345 // case GL_RGB10:
346 // case GL_RGB12:
347 // case GL_RGB16:
348 // //case GL_SRGB:
349 // //case GL_SRGB8:
350 // case GL_COMPRESSED_RGB:
351 // case GL_RGB16F_ARB:
352 // case GL_RGB32F_ARB:
353 // format = GL_RGB;
354 // numChannels = 3;
355 // break;
356 
357 // case GL_RGBA:
358 // case GL_RGBA2:
359 // case GL_RGBA4:
360 // case GL_RGB5_A1:
361 // case GL_RGBA8:
362 // case GL_RGB10_A2:
363 // case GL_RGBA12:
364 // case GL_RGBA16:
365 // //case GL_SRGB_ALPHA:
366 // //case GL_SRGB8_ALPHA8:
367 // case GL_COMPRESSED_RGBA:
368 // case GL_RGBA16F_ARB:
369 // case GL_RGBA32F_ARB:
370 // format = GL_RGBA;
371 // numChannels = 4;
372 // break;
373 
374 // case GL_DEPTH_COMPONENT:
375 // case GL_DEPTH_COMPONENT16:
376 // case GL_DEPTH_COMPONENT24:
377 // case GL_DEPTH_COMPONENT32:
378 // format = GL_DEPTH_COMPONENT;
379 // numChannels = 1;
380 // break;
381 // case GL_DEPTH24_STENCIL8_EXT:
382 // format = GL_DEPTH_STENCIL_EXT;
383 // numChannels = 2;
384 // break;
385 
386 // default:
387 // GLF_THROW_EXCEPTION("Unknown or invalid internal texture format")
388 // ;
389 // break;
390 // }
391 // }
392 
393 // GLenum
394 // glfTexture::GetPixelTypeForStorageType(ImageBase::EStorageType storageType)
395 // {
396 // switch (storageType)
397 // {
398 // case ImageBase::ST_invalid:
399 // GLF_THROW_EXCEPTION("Invalid storage type")
400 // ;
401 // break;
402 // case ImageBase::ST_unsignedchar:
403 // return GL_UNSIGNED_BYTE;
404 // case ImageBase::ST_char:
405 // return GL_BYTE;
406 // case ImageBase::ST_unsignedshortint:
407 // return GL_UNSIGNED_SHORT;
408 // case ImageBase::ST_shortint:
409 // return GL_SHORT;
410 // case ImageBase::ST_unsignedint:
411 // return GL_UNSIGNED_INT;
412 // case ImageBase::ST_int:
413 // return GL_INT;
414 // case ImageBase::ST_float:
415 // return GL_FLOAT;
416 // case ImageBase::ST_double:
417 // GLF_THROW_EXCEPTION("Storage type 'double' is not supported by OpenGL")
418 // ;
419 // break;
420 // default:
421 // GLF_THROW_EXCEPTION("Unknown storage type")
422 // ;
423 // break;
424 // }
425 // }
426 
427 // GLenum
428 // glfTexture::GetFormatForColorModel(ImageBase::EColorModel colorModel)
429 // {
430 // switch (colorModel)
431 // {
432 // case ImageBase::CM_Grey:
433 // case ImageBase::CM_Depth:
434 // case ImageBase::CM_Disparity:
435 // return GL_LUMINANCE;
436 // case ImageBase::CM_GreyA:
437 // return GL_LUMINANCE_ALPHA;
438 // case ImageBase::CM_RGB:
439 // return GL_RGB;
440 // case ImageBase::CM_RGBA:
441 // return GL_RGBA;
442 // case ImageBase::CM_BGR:
443 // return GL_BGR;
444 // case ImageBase::CM_BGRA:
445 // return GL_BGRA;
446 // case ImageBase::CM_LUV:
447 // case ImageBase::CM_LAB:
448 // return GL_RGB;
449 // default:
450 // GLF_THROW_EXCEPTION("Color model " << colorModel << " is not supported by OpenGL")
451 // ;
452 // }
453 // }
454 
455 // GLenum
456 // glfTexture::ProposeInternalFormat(ImageBase::EStorageType storageType,
457 // ImageBase::EColorModel colorModel)
458 // {
459 
460 // if (storageType == ImageBase::ST_float)
461 // {
462 // switch (colorModel)
463 // {
464 // case ImageBase::CM_Grey:
465 // case ImageBase::CM_Depth:
466 // case ImageBase::CM_Disparity:
467 // return GL_LUMINANCE32F_ARB;
468 // case ImageBase::CM_GreyA:
469 // return GL_LUMINANCE_ALPHA32F_ARB;
470 // case ImageBase::CM_RGB:
471 // case ImageBase::CM_LUV:
472 // case ImageBase::CM_LAB:
473 // case ImageBase::CM_BGR:
474 // return GL_RGB32F_ARB;
475 // case ImageBase::CM_RGBA:
476 // case ImageBase::CM_BGRA:
477 // return GL_RGBA32F_ARB;
478 
479 // default:
480 // break; // suppress warning
481 // }
482 // }
483 
484 // if (storageType == ImageBase::ST_unsignedchar || storageType
485 // == ImageBase::ST_unsignedshortint || storageType
486 // == ImageBase::ST_unsignedint)
487 // {
488 // switch (colorModel)
489 // {
490 // case ImageBase::CM_Grey:
491 // return GL_LUMINANCE;
492 // case ImageBase::CM_GreyA:
493 // return GL_LUMINANCE_ALPHA;
494 // case ImageBase::CM_RGB:
495 // case ImageBase::CM_BGR:
496 // return GL_RGB;
497 // case ImageBase::CM_RGBA:
498 // case ImageBase::CM_BGRA:
499 // return GL_RGBA;
500 // default:
501 // break; // suppress warning
502 // }
503 // }
504 
505 // GLF_THROW_EXCEPTION("Could not find appropriate OpenGL pixel format for "
506 // "storage type " << storageType << " and color model "
507 // << colorModel);
508 
509 // }
510 
void SetBorderColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
Sets the border color for the texture to be used.
Definition: glfTexture.cpp:153
static int NextPowerOfTwo(int i)
Returns the smallest power of two that is greater than i.
Definition: glfTexture.cpp:269
void Create()
Creates the texture but does not upload any data yet.
Definition: glfTexture.cpp:80
void Bind() const
Binds the texture.
Definition: glfTexture.cpp:242
void EnlargeToPowerOfTwoIfRequired(bool enlargeToPotIfRequired=true)
Sets whether to enlarge each texture dimension to the next power of two, if non-power-of-two textures...
Definition: glfTexture.cpp:163
BIASOpenGLFramework_EXPORT void SwapGLObjects(glfTexture &A, glfTexture &B)
Definition: glfTexture.cpp:31
void SetMaxLevel(int maxLevel)
Sets the maximum mip map level to be used.
Definition: glfTexture.cpp:145
void SetWrapS(GLenum wrapS)
Sets the wrapping mode for the 1st texture coordinate.
Definition: glfTexture.cpp:105
virtual ~glfTexture()
Definition: glfTexture.cpp:71
void BindTU() const
Binds the texture.
Definition: glfTexture.cpp:249
void SetMinFilter(GLenum minFilter)
Sets the minifying function.
Definition: glfTexture.cpp:89
void SetTextureNrInUniformFormat(int textureNr)
Sets the OpenGL texture number (0 - MaxTextureUnit-1).
Definition: glfTexture.cpp:224
static int GetMaxSupportedTextureUnits()
Computes a pixel format that can be used with glGetTexImage for an internal format of a texture...
Definition: glfTexture.cpp:185
void SetGenerateMipMap(bool generateMipMap)
Sets whether to automatically generate mipmaps when the first mipmap is uploaded. ...
Definition: glfTexture.cpp:129
void SetWrapT(GLenum wrapT)
Sets the wrapping mode for the 2nd texture coordinate.
Definition: glfTexture.cpp:113
void SetBaseLevel(int baseLevel)
Sets the minimum (base) mip map level to be used.
Definition: glfTexture.cpp:137
void SetWrapR(GLenum wrapR)
Sets the wrapping mode for the 3rd texture coordinate.
Definition: glfTexture.cpp:121
void GenerateMipMap()
Explicitly generates the mipmaps of the texture in hardware.
Definition: glfTexture.cpp:194
Base class for textures.
Definition: glfTexture.hh:42
void SetTextureNr(GLint textureNr)
Sets the OpenGL texture number (GL_TEXTURE0 - GL_TEXTURE[MaxTextureUnit-1]).
Definition: glfTexture.cpp:207
glfTexture(GLenum target)
Definition: glfTexture.cpp:63
void SetMagFilter(GLenum magFilter)
Sets the magnification function.
Definition: glfTexture.cpp:97