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