Tulip  5.4.0
Large graphs analysis and drawing
GlTextureManager.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
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 #ifndef TULIP_BUILD_GL_TEX_LOADER
26 #include <tulip/TulipException.h>
27 #endif
28 #include <tulip/OpenGlIncludes.h>
29 
30 #include <set>
31 #include <unordered_map>
32 #include <string>
33 
34 namespace tlp {
35 
36 struct GlTexture {
37  GLuint *id;
38  int height;
39  int width;
40  unsigned int spriteNumber;
41 };
42 
43 /**
44  * \brief Class to load textures
45  */
46 class TLP_GL_SCOPE GlTextureLoader {
47 public:
48  /**
49  * Load a texture from a file
50  * in the default implementation only bmp, jpeg and png files
51  * can be loaded.
52  * Return false if an error occurs
53  */
54 #ifdef TULIP_BUILD_GL_TEX_LOADER
55  virtual bool loadTexture(const std::string &filename, GlTexture &texture);
56 #else
57  virtual bool loadTexture(const std::string &filename, GlTexture &texture) = 0;
58 #endif
59  virtual ~GlTextureLoader() {}
60 };
61 
62 /**
63  * class to load/store textures need by OpenGL rendering
64  */
65 class TLP_GL_SCOPE GlTextureManager {
66 
67  typedef std::unordered_map<std::string, GlTexture> TextureMap;
68 
69 public:
70  /**
71  * Return texture info (id, width and height) for the given name
72  */
73  static GlTexture getTextureInfo(const std::string &);
74 
75  /**
76  * Check if a texture fo the given name exists in the current context
77  */
78  static bool existsTexture(const std::string &filename);
79  /**
80  * Load texture with given name
81  */
82  static bool loadTexture(const std::string &);
83  /**
84  * Remove texture with given name
85  */
86  static void deleteTexture(const std::string &);
87  /**
88  * Begin a new texture with given name
89  */
90  static void beginNewTexture(const std::string &);
91  /**
92  * Activate a texture with given name
93  */
94  static bool activateTexture(const std::string &, unsigned int);
95  /**
96  * Activate a texture with given name
97  */
98  static bool activateTexture(const std::string &);
99  /**
100  * Disable texture with given name
101  */
102  static void deactivateTexture();
103  /**
104  * Set animationStep for next textures (for next activateTexture)
105  */
106  static inline void setAnimationFrame(unsigned int id) {
107  animationFrame = id;
108  }
109  /**
110  * Get animationStep of next textures
111  */
112  static inline unsigned int getAnimationFrame() {
113  return animationFrame;
114  }
115  /**
116  * Clear vector of textures with error
117  */
118  static inline void clearErrorVector() {
119  texturesWithError.clear();
120  }
121  /**
122  * Remove an entry of vector of textures with error
123  */
124  static inline void removeEntryOfErrorVector(const std::string &name) {
125  texturesWithError.erase(name);
126  }
127 
128  /**
129  * Register an external texture is GlTextureManager
130  */
131  static void registerExternalTexture(const std::string &textureName, const GLuint textureId);
132 
133  /**
134  * Get Texture loader
135  */
136  static inline GlTextureLoader *getTextureLoader() {
137 #ifdef TULIP_BUILD_GL_TEX_LOADER
138  return loader ? loader : (loader = new GlTextureLoader());
139 #else
140  if (!loader)
141  throw TulipException("GlTextureLoader Error: no texture loader found");
142  return loader;
143 #endif
144  }
145 
146  /**
147  * Set Texture loader
148  */
149  static void setTextureLoader(GlTextureLoader *texLoader) {
150  if (loader)
151  delete loader;
152 
153  loader = texLoader;
154  }
155 
156  static void deleteAllTextures();
157 
158 private:
159  static GlTextureLoader *loader;
160 
161  static TextureMap texturesMap;
162  static std::set<std::string> texturesWithError;
163 
164  static unsigned int animationFrame;
165 };
166 } // namespace tlp
167 
168 #endif // Tulip_GLTEXTUREMANAGER_H
169 ///@endcond