Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
texture.hpp
Go to the documentation of this file.
1 
23 #ifndef __TEXTURE__
24 #define __TEXTURE__
25 
27 #include <iostream>
28 #include <GL/glew.h>
29 #include <memory>
30 #include <Eigen/Eigen>
31 
32 using namespace std;
33 
34 namespace Tucano
35 {
36 
41 class Texture {
42 private:
43 
45  GLuint tex_id;
46 
48  GLenum tex_type;
49 
52 
54  int width;
55 
57  int height;
58 
60  int depth;
61 
63  GLenum format;
64 
66  GLenum pixel_type;
67 
69  int lod;
70 
72  int unit;
73 
75  int num_samples = 1;
76 
78  std::shared_ptr < GLuint > texID_sptr;
79 
80 public:
81 
85  Texture (void)
86  {
87  glGenTextures(1, &tex_id);
88 
89  texID_sptr = std::shared_ptr < GLuint > (
90  new GLuint (tex_id),
91  [] (GLuint *p) {
92  glDeleteTextures(1, p);
93  delete p;
94  }
95  );
96  }
97 
102  int getWidth (void)
103  {
104  return width;
105  }
106 
111  int getHeight (void)
112  {
113  return height;
114  }
115 
120  Eigen::Vector2i getDimensions (void)
121  {
122  return Eigen::Vector2i(width, height);
123  }
124 
129  void setNumSamples (int num)
130  {
131  num_samples = num;
132  }
133 
146  GLuint create (GLenum type, GLenum int_format, int w, int h, GLenum fmt, GLenum pix_type, const GLvoid* data = NULL, int dpt = 256)
147  {
148  tex_type = type;
149  internal_format = int_format;
150  width = w;
151  height = h;
152  format = fmt;
153  pixel_type = pix_type;
154  lod = 0;
155  depth = dpt;
156 
157  // create new texture
158  glBindTexture(tex_type, *texID_sptr);
159  if(tex_type == GL_TEXTURE_2D || tex_type == GL_TEXTURE_RECTANGLE)
160  {
161  glTexImage2D(tex_type, lod, internal_format, width, height, 0, format, pixel_type, data);
162  }
163  else if(tex_type == GL_TEXTURE_2D_MULTISAMPLE)
164  {
165  glTexImage2DMultisample(tex_type, num_samples, internal_format, width, height, GL_TRUE);
166  }
167  else if (tex_type == GL_TEXTURE_3D)
168  {
169  glTexImage3D(tex_type, lod, internal_format, width, height, depth, 0, format, pixel_type, data);
170  }
171  else if (tex_type == GL_TEXTURE_1D)
172  {
173  glTexImage1D(tex_type, lod, internal_format, width, 0, format, pixel_type, data);
174  }
175 
176  // default parameters
177  setTexParameters();
178  glBindTexture(tex_type, 0);
179 
180  #ifdef TUCANODEBUG
181  Tucano::Misc::errorCheckFunc(__FILE__, __LINE__, "texture create");
182  #endif
183 
184  return *texID_sptr;
185  }
186 
196  GLuint create (int w, int h, const GLvoid* data = NULL)
197  {
198  return create (GL_TEXTURE_2D, GL_RGBA32F, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data, 0);
199  }
200 
201 
205  /*void destroy (void)
206  {
207  if (tex_id != 0) {
208  glDeleteTextures(1, &tex_id);
209  }
210  tex_id = 0;
211  }*/
212 
221  void setTexParameters (GLenum wraps = GL_CLAMP, GLenum wrapt = GL_CLAMP, GLenum magfilter = GL_NEAREST, GLenum minfilter = GL_NEAREST)
222  {
223  // multisampled textures are not filtered, setting filter parameters might result in glerror
224  if (num_samples == 1)
225  {
226  glTexParameteri(tex_type, GL_TEXTURE_WRAP_S, wraps);
227  glTexParameteri(tex_type, GL_TEXTURE_WRAP_T, wrapt);
228  glTexParameteri(tex_type, GL_TEXTURE_MAG_FILTER, magfilter);
229  glTexParameteri(tex_type, GL_TEXTURE_MIN_FILTER, minfilter);
230  }
231  }
232 
244  void setTexParametersMipMap (int maxlevel, int baselevel = 0, GLenum wraps = GL_CLAMP, GLenum wrapt = GL_CLAMP, GLenum magfilter = GL_NEAREST, GLenum minfilter = GL_NEAREST_MIPMAP_NEAREST)
245  {
246  glTexParameteri(tex_type, GL_TEXTURE_MIN_FILTER, minfilter);
247  glTexParameteri(tex_type, GL_TEXTURE_MAG_FILTER, magfilter);
248  glTexParameteri(tex_type, GL_TEXTURE_WRAP_S, wraps);
249  glTexParameteri(tex_type, GL_TEXTURE_WRAP_T, wrapt);
250  glTexParameteri(tex_type, GL_TEXTURE_BASE_LEVEL, baselevel );
251  glTexParameteri(tex_type, GL_TEXTURE_MAX_LEVEL, maxlevel );
252 
253  glGenerateMipmap(tex_type);
254  }
255 
256 
263  void update (const GLvoid* data)
264  {
265  glBindTexture(tex_type, *texID_sptr);
266  if(tex_type == GL_TEXTURE_2D || tex_type == GL_TEXTURE_RECTANGLE) {
267  glTexSubImage2D(tex_type, lod, 0, 0, width, height, format, pixel_type, data);
268  }
269  else if (tex_type == GL_TEXTURE_3D) {
270  glTexSubImage3D(tex_type, lod, 0, 0, 0, width, height, depth, format, pixel_type, data);
271  }
272  else if (tex_type == GL_TEXTURE_1D) {
273  glTexSubImage1D(tex_type, lod, 0, width, format, pixel_type, data);
274  }
275  glBindTexture(tex_type,0);
276 
277  }
278 
285  void bind (int texture_unit)
286  {
287  unit = texture_unit;
288  texManager.bindTexture(tex_type, *texID_sptr, texture_unit);
289  }
290 
296  int bind (void)
297  {
298  unit = texManager.bindTexture(tex_type, *texID_sptr);
299  return unit;
300  }
301 
309  int bindImageRW (void)
310  {
311  unit = texManager.bindTexture(tex_type, *texID_sptr);
312  if (unit != -1)
313  {
314  glBindImageTexture(unit, *texID_sptr, 0, false, 0, GL_READ_WRITE, internal_format);
315  }
316  return unit;
317  }
318 
326  void bindImageRW (int texture_unit)
327  {
328  unit = texture_unit;
329  texManager.bindTexture(tex_type, *texID_sptr, texture_unit);
330  glBindImageTexture(texture_unit, *texID_sptr, 0, false, 0, GL_READ_WRITE, internal_format);
331  }
332 
339  int bindImageFormatRW (GLenum fmt)
340  {
341  unit = texManager.bindTexture(tex_type, *texID_sptr);
342 
343  if (unit != -1)
344  {
345  glBindImageTexture(unit, *texID_sptr, 0, false, 0, GL_READ_WRITE, fmt);
346  }
347  return unit;
348  }
349 
357  void bindImageFormatRW (int texture_unit, GLenum fmt)
358  {
359  unit = texture_unit;
360  texManager.bindTexture(tex_type, *texID_sptr, texture_unit);
361  glBindImageTexture(texture_unit, *texID_sptr, 0, false, 0, GL_READ_WRITE, fmt);
362  }
363 
367  void unbind (void)
368  {
369  texManager.unbindTextureID(tex_type, *texID_sptr);
370  unit = -1;
371  }
372 
377  GLuint texID (void) const
378  {
379  return *texID_sptr;
380  }
381 
386  int textureUnit (void) const
387  {
388  return unit;
389  }
390 };
391 
392 }
393 
394 #endif
GLenum pixel_type
Type of pixel channel (ex. GL_FLOAT, GL_UNSIGNED_BYTE ...).
Definition: texture.hpp:66
GLenum internal_format
Internal format (ex. GL_RGBA, GL_RGBA32F ...).
Definition: texture.hpp:51
int bind(void)
Binds the texture to the first free texture unit and returns the unit used. Gets the first free unit ...
Definition: texture.hpp:296
Definition: bufferobject.hpp:34
#define texManager
Defines our unique instance of this singleton class.
Definition: texturemanager.hpp:5
void bind(int texture_unit)
Binds the texture to a given unit. Note that if there is another texture already binded to this unit...
Definition: texture.hpp:285
void setTexParameters(GLenum wraps=GL_CLAMP, GLenum wrapt=GL_CLAMP, GLenum magfilter=GL_NEAREST, GLenum minfilter=GL_NEAREST)
Deletes the texture.
Definition: texture.hpp:221
GLuint create(int w, int h, const GLvoid *data=NULL)
Creates a texture object with typical default parameters and returns its handler. ...
Definition: texture.hpp:196
int width
Width in pixels.
Definition: texture.hpp:54
int height
Height in pixels.
Definition: texture.hpp:57
int unit
Texture unit this texture is occupying (if any).
Definition: texture.hpp:72
int getWidth(void)
Returns the texture width.
Definition: texture.hpp:102
void errorCheckFunc(std::string file, int line, std::string message="")
GL error check method.
Definition: misc.hpp:53
int bindImageFormatRW(GLenum fmt)
Binds the texture as an image texture with a given format. Binds with READ and WRITE access with a gi...
Definition: texture.hpp:339
GLuint tex_id
ID handler to texture.
Definition: texture.hpp:45
int textureUnit(void) const
Returns the texture unit this texture is bound to.
Definition: texture.hpp:386
void bindImageRW(int texture_unit)
Binds the texture as an image texture to a given unit. Image textures are used for example for atomic...
Definition: texture.hpp:326
std::shared_ptr< GLuint > texID_sptr
shared pointer for maintaing texture id
Definition: texture.hpp:78
void bindImageFormatRW(int texture_unit, GLenum fmt)
Binds the texture as an image texture to a given texture unit with a given internal format...
Definition: texture.hpp:357
Eigen::Vector2i getDimensions(void)
Returns the texture dimensions.
Definition: texture.hpp:120
void setTexParametersMipMap(int maxlevel, int baselevel=0, GLenum wraps=GL_CLAMP, GLenum wrapt=GL_CLAMP, GLenum magfilter=GL_NEAREST, GLenum minfilter=GL_NEAREST_MIPMAP_NEAREST)
Sets texture parameters. Sets the Wrap S and T, Min and Mag filter, and MipMap parameters. Also generates the MipMap at the end.
Definition: texture.hpp:244
GLenum tex_type
Type of texture (ex. GL_TEXTURE_2D, GL_TEXTURE_3D ...).
Definition: texture.hpp:48
Texture(void)
Default Constructor.
Definition: texture.hpp:85
int getHeight(void)
Returns the texture height.
Definition: texture.hpp:111
int depth
Depth in pixels (for 3D texture).
Definition: texture.hpp:60
GLuint texID(void) const
Returns the texture handle (texture ID).
Definition: texture.hpp:377
An OpenGL texture. It can be a simple texture or an FBO texture.
Definition: texture.hpp:41
int bindImageRW(void)
Binds the texture as an image texture. Image textures are used for example for atomic counters...
Definition: texture.hpp:309
void update(const GLvoid *data)
Updates the data of the texture mantaining all other parameters.
Definition: texture.hpp:263
GLuint create(GLenum type, GLenum int_format, int w, int h, GLenum fmt, GLenum pix_type, const GLvoid *data=NULL, int dpt=256)
Creates a texture object and returns its handler.
Definition: texture.hpp:146
int lod
Number of lod levels (1 for textures without lod).
Definition: texture.hpp:69
void unbind(void)
Unbinds this texture and frees the texture unit.
Definition: texture.hpp:367
GLenum format
Format of texture channels (ex. GL_RGBA).
Definition: texture.hpp:63
void setNumSamples(int num)
Sets the number of samples for multisampling.
Definition: texture.hpp:129