Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
texturemanager.hpp
Go to the documentation of this file.
1 #ifndef __TEXTUREMANAGER__
2 #define __TEXTUREMANAGER__
3 
5 #define texManager TextureManager::Instance()
6 
7 #include <set>
8 #include <iostream>
9 #include <vector>
10 #include <GL/glew.h>
11 
12 
13 using namespace std;
14 
15 namespace Tucano
16 {
17 
25 
26 
27 
28  //Still got to think about an interesting way to set texture parameters here. Besides. got to see a way to decrease the number of parameters for the create texture method.
29 
30  //One problem: framebuffer has it's method for storing the free units. What will I do? Use the texture manager in the framebuffer?
31 
32  //I need to test the texturemanager in an application that uses more than one texture unit at a time.
33  public:
34 
38  static TextureManager &Instance (void)
39  {
40  static TextureManager _instance;
41  return _instance;
42  }
43 
47  void bindTexture (GLenum texType, GLuint texID, int texture_unit)
48  {
49  glActiveTexture(GL_TEXTURE0 + texture_unit);
50  glBindTexture(texType, texID);
51 
52  if (used_units[texture_unit] != -1)
53  {
54  cerr << "WARNING: Texture unit already used. Replacing bound texture..." << endl;
55  }
56 
57  used_units[texture_unit] = texID;
58  }
59 
64  int bindTexture (GLenum texType, GLuint texID)
65  {
66  int free_unit = getAvailableUnit();
67 
68  if (free_unit != -1)
69  {
70  glActiveTexture(GL_TEXTURE0 + free_unit);
71  glBindTexture(texType, texID);
72  used_units[free_unit] = texID;
73  }
74  else
75  {
76  cerr << "WARNING: no free texture unit found!\n";
77  }
78 
79  return free_unit;
80  }
81 
85  int getAvailableUnit (void)
86  {
87  for (int i = 0; i < max_texture_units; ++i)
88  {
89  if (used_units[i] == -1)
90  {
91  return i;
92  }
93  }
94  return -1;
95  }
96 
100  void setUnavailableUnit(int unit);
101 
103  void unbindTexture(GLenum texType, int texture_unit)
104  {
105  glActiveTexture(GL_TEXTURE0 + texture_unit);
106  glBindTexture(texType, 0);
107  used_units[texture_unit] = -1;
108  }
109 
116  void unbindTextureID(GLenum texType, GLuint texID)
117  {
118  for (int i = 0; i < max_texture_units; ++i) {
119  if (used_units[i] == (GLint)texID) {
120  glActiveTexture(GL_TEXTURE0 + i);
121  glBindTexture(texType, 0);
122  used_units[i] = -1;
123  }
124  }
125  }
126 
127  //THAT'S SOMETHING IMPORTANT. SHOULDN'T REALLY I DELETE THE INSTANCE? WON'T THERE BE A MEMORY LEAKING THERE?
129 
130 private:
131 
132  //I need to define the constructor. the copy constructor and the operator= as private in order to ensure that only one Texture Manager can be created. through the use of the Instance() method.
133 
136  // get maximum number of texture units. and set all as not used
137  glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_texture_units);
138 
139  for (int i = 0; i < max_texture_units; ++i) {
140  used_units.push_back(-1);
141  }
142  }
143 
146 
148  TextureManager& operator=(TextureManager const&);
149 
152 
155 
157  std::vector<int> used_units;
158 
159 };
160 
161 //Not sure why it is here.
162 //http://www.yolinux.com/TUTORIALS/C++Singleton.html says: "Global static pointer used to ensure a single instance of the class."
163 //Need to understand it better.
164 //static TextureManager* TextureManager::pInstance = NULL;
165 
166 
167 }
168 
169 
170 #endif
~TextureManager()
Definition: texturemanager.hpp:128
Singleton class that manages texture unit allocation.
Definition: texturemanager.hpp:24
void unbindTextureID(GLenum texType, GLuint texID)
Unbinds a texture with given ID Searches texture unit vector for the texture and frees unit...
Definition: texturemanager.hpp:116
Definition: bufferobject.hpp:34
void unbindTexture(GLenum texType, int texture_unit)
Unbinds the texture from the specific texture unit.
Definition: texturemanager.hpp:103
TextureManager(TextureManager const &)
Copy Constructor.
Definition: texturemanager.hpp:145
static TextureManager & Instance(void)
Returns the unique instance. If no instace exists, it will create one (only once).
Definition: texturemanager.hpp:38
int max_texture_units
Maximum number of texture units.
Definition: texturemanager.hpp:154
TextureManager()
Default Constructor.
Definition: texturemanager.hpp:135
int bindTexture(GLenum texType, GLuint texID)
Binds a texture to the first free texture unit and returns the allocated unit.
Definition: texturemanager.hpp:64
int getAvailableUnit(void)
Returns the first available texture unit.
Definition: texturemanager.hpp:85
static TextureManager * pInstance
Pointer to the instance of the Texture Manager object.
Definition: texturemanager.hpp:151
std::vector< int > used_units
Texture units in use. each slot holds the texture id or -1 if free.
Definition: texturemanager.hpp:157
void bindTexture(GLenum texType, GLuint texID, int texture_unit)
Binds a texture to a unit given by the user.
Definition: texturemanager.hpp:47