Tulip  4.3.0
Better Visualization Through Research
 All Classes Namespaces 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 1 and Inria Bordeaux - Sud Ouest
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 namespace tlp {
41 
42 struct GlTexture {
43  GLuint *id;
44  int height;
45  int width;
46  unsigned int spriteNumber;
47 };
48 
49 /** \brief Class to manage textures
50  * Singleton class to load/store textures need by OpenGL rendering
51  */
52 class TLP_GL_SCOPE GlTextureManager {
53 
54  typedef std::map<std::string,GlTexture> TextureUnit;
55  typedef std::map<unsigned long,TextureUnit> ContextAndTextureMap;
56 
57 public:
58 
59  /**
60  * Create the texture manager singleton
61  */
62  static void createInst();
63  /**
64  * Return the texture manager singleton, il singleton doesn't exist this function create it
65  */
66  static GlTextureManager &getInst() {
67  if(!inst)
68  inst=new GlTextureManager();
69 
70  return *inst;
71  }
72 
73  /**
74  * Change the current OpenGl context (each OpenGl window have a different OpenGl context)
75  */
76  void changeContext(unsigned long context);
77  /**
78  * Remove all textures of an OpenGl context and remove this context
79  */
80  void removeContext(unsigned long context);
81 
82  /**
83  * Return texture info (id, width and height) for the given name
84  */
85  GlTexture getTextureInfo(const std::string&);
86 
87  /**
88  * Check if a texture fo the given name exists in the current context
89  */
90  bool existsTexture(const std::string& filename);
91  /**
92  * Load texture with given name
93  */
94  bool loadTexture(const std::string&);
95  /**
96  * Remove texture with given name
97  */
98  void deleteTexture(const std::string &);
99  /**
100  * Begin a new texture with given name
101  */
102  void beginNewTexture(const std::string&);
103  /**
104  * Activate a texture with given name
105  */
106  bool activateTexture(const std::string&,unsigned int);
107  /**
108  * Activate a texture with given name
109  */
110  bool activateTexture(const std::string&);
111  /**
112  * Disable texture with given name
113  */
114  void desactivateTexture();
115  /**
116  * Set animationStep for next textures (for next activateTexture)
117  */
118  void setAnimationFrame(unsigned int id) {
119  animationFrame=id;
120  }
121  /**
122  * Get animationStep of next textures
123  */
124  unsigned int getAnimationFrame() {
125  return animationFrame;
126  }
127  /**
128  * Clear vector of textures with error
129  */
130  void clearErrorVector() {
131  texturesWithError.clear();
132  }
133  /**
134  * Remove an entry of vector of textures with error
135  */
136  void removeEntryOfErrorVector(const std::string &name) {
137  texturesWithError.erase(name);
138  }
139 
140 
141  /**
142  * Register an external texture is GlTextureManager
143  */
144  void registerExternalTexture(const std::string &textureName, const GLuint textureId);
145 
146 private:
147 
148  /**
149  * empty private constructor for singleton
150  */
151  GlTextureManager();
152 
153  static GlTextureManager* inst;
154 
155  unsigned long currentContext;
156 
157  ContextAndTextureMap texturesMap;
158  std::set<std::string> texturesWithError;
159 
160  unsigned int animationFrame;
161 
162 };
163 
164 }
165 
166 #endif // Tulip_GLTEXTUREMANAGER_H
167 ///@endcond