Tulip  4.8.0
Better Visualization Through Research
 All Classes Files Functions Variables Enumerations Enumerator Properties Groups Pages
GlTextureManager.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef Tulip_GLTEXTUREMANAGER_H
22 #define Tulip_GLTEXTUREMANAGER_H
23 
24 #include <tulip/tulipconf.h>
25 
26 #if defined(_MSC_VER)
27 #include <Windows.h>
28 #endif
29 
30 #if defined(__APPLE__)
31 #include <OpenGL/gl.h>
32 #else
33 #include <GL/gl.h>
34 #endif
35 
36 #include <set>
37 #include <map>
38 #include <string>
39 
40 #include <stdint.h>
41 
42 namespace tlp {
43 
44 struct GlTexture {
45  GLuint *id;
46  int height;
47  int width;
48  unsigned int spriteNumber;
49 };
50 
51 /**
52  * \brief Class to load textures
53  */
54 class TLP_GL_SCOPE GlTextureLoader {
55 public:
56  /**
57  * Load a texture from a file
58  * in the default implementation only bmp, jpeg and png files
59  * can be loaded.
60  * Return false if an error occurs
61  */
62  virtual bool loadTexture(const std::string& filename, GlTexture& texture);
63 
64  virtual ~GlTextureLoader() {}
65 };
66 
67 /** \brief Class to manage textures
68  * Singleton class to load/store textures need by OpenGL rendering
69  */
70 class TLP_GL_SCOPE GlTextureManager {
71 
72  typedef std::map<std::string,GlTexture> TextureUnit;
73  typedef std::map<uintptr_t,TextureUnit> ContextAndTextureMap;
74 
75 public:
76 
77  /**
78  * Return the texture manager singleton, il singleton doesn't exist this function create it
79  */
80  static GlTextureManager &getInst() {
81  if(!inst)
82  inst=new GlTextureManager();
83 
84  return *inst;
85  }
86 
87  /**
88  * Change the current OpenGl context (each OpenGl window have a different OpenGl context)
89  */
90  void changeContext(uintptr_t context);
91  /**
92  * Remove all textures of an OpenGl context and remove this context
93  */
94  void removeContext(uintptr_t context);
95 
96  /**
97  * Return texture info (id, width and height) for the given name
98  */
99  GlTexture getTextureInfo(const std::string&);
100 
101  /**
102  * Check if a texture fo the given name exists in the current context
103  */
104  bool existsTexture(const std::string& filename);
105  /**
106  * Load texture with given name
107  */
108  bool loadTexture(const std::string&);
109  /**
110  * Remove texture with given name
111  */
112  void deleteTexture(const std::string &);
113  /**
114  * Begin a new texture with given name
115  */
116  void beginNewTexture(const std::string&);
117  /**
118  * Activate a texture with given name
119  */
120  bool activateTexture(const std::string&,unsigned int);
121  /**
122  * Activate a texture with given name
123  */
124  bool activateTexture(const std::string&);
125  /**
126  * Disable texture with given name
127  */
128  void desactivateTexture();
129  /**
130  * Set animationStep for next textures (for next activateTexture)
131  */
132  void setAnimationFrame(unsigned int id) {
133  animationFrame=id;
134  }
135  /**
136  * Get animationStep of next textures
137  */
138  unsigned int getAnimationFrame() {
139  return animationFrame;
140  }
141  /**
142  * Clear vector of textures with error
143  */
144  void clearErrorVector() {
145  texturesWithError.clear();
146  }
147  /**
148  * Remove an entry of vector of textures with error
149  */
150  void removeEntryOfErrorVector(const std::string &name) {
151  texturesWithError.erase(name);
152  }
153 
154 
155  /**
156  * Register an external texture is GlTextureManager
157  */
158  void registerExternalTexture(const std::string &textureName, const GLuint textureId);
159 
160  /**
161  * Get Texture loader
162  */
163  static GlTextureLoader* getTextureLoader() {
164  return loader ? loader : (loader = new GlTextureLoader());
165  }
166 
167  /**
168  * Set Texture loader
169  */
170  static void setTextureLoader(GlTextureLoader* texLoader) {
171  if (loader)
172  delete loader;
173 
174  loader = texLoader;
175  }
176 
177 private:
178 
179  /**
180  * empty private constructor for singleton
181  */
182  GlTextureManager();
183 
184  static GlTextureManager* inst;
185  static GlTextureLoader* loader;
186 
187  uintptr_t currentContext;
188 
189  ContextAndTextureMap texturesMap;
190  std::set<std::string> texturesWithError;
191 
192  unsigned int animationFrame;
193 
194 };
195 
196 }
197 
198 #endif // Tulip_GLTEXTUREMANAGER_H
199 ///@endcond