Tulip  4.6.0
Better Visualization Through Research
library/tulip-ogl/include/tulip/GlTextureManager.h
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 ///@cond DOXYGEN_HIDDEN
00020 
00021 #ifndef Tulip_GLTEXTUREMANAGER_H
00022 #define Tulip_GLTEXTUREMANAGER_H
00023 
00024 #include <tulip/tulipconf.h>
00025 
00026 #if defined(_MSC_VER)
00027 #include <Windows.h>
00028 #endif
00029 
00030 #if defined(__APPLE__)
00031 #include <OpenGL/gl.h>
00032 #else
00033 #include <GL/gl.h>
00034 #endif
00035 
00036 #include <set>
00037 #include <map>
00038 #include <string>
00039 
00040 #include <stdint.h>
00041 
00042 namespace tlp {
00043 
00044 struct GlTexture {
00045   GLuint *id;
00046   int height;
00047   int width;
00048   unsigned int spriteNumber;
00049 };
00050 
00051 /**
00052  *  \brief Class to load textures
00053  */
00054 class TLP_GL_SCOPE GlTextureLoader {
00055 public:
00056   /**
00057    * Load a texture from a file
00058    * in the default implementation only bmp, jpeg and png files
00059    * can be loaded.
00060    * Return false if an error occurs
00061    */
00062   virtual bool loadTexture(const std::string& filename, GlTexture& texture);
00063 
00064   virtual ~GlTextureLoader() {}
00065 };
00066 
00067 /** \brief Class to manage textures
00068  * Singleton class to load/store textures need by OpenGL rendering
00069  */
00070 class TLP_GL_SCOPE GlTextureManager {
00071 
00072   typedef std::map<std::string,GlTexture> TextureUnit;
00073   typedef std::map<uintptr_t,TextureUnit> ContextAndTextureMap;
00074 
00075 public:
00076 
00077   /**
00078   * Return the texture manager singleton, il singleton doesn't exist this function create it
00079   */
00080   static GlTextureManager &getInst() {
00081     if(!inst)
00082       inst=new GlTextureManager();
00083 
00084     return *inst;
00085   }
00086 
00087   /**
00088    * Change the current OpenGl context (each OpenGl window have a different OpenGl context)
00089    */
00090   void changeContext(uintptr_t context);
00091   /**
00092    * Remove all textures of an OpenGl context and remove this context
00093    */
00094   void removeContext(uintptr_t context);
00095 
00096   /**
00097    * Return texture info (id, width and height) for the given name
00098    */
00099   GlTexture getTextureInfo(const std::string&);
00100 
00101   /**
00102    * Check if a texture fo the given name exists in the current context
00103    */
00104   bool existsTexture(const std::string& filename);
00105   /**
00106    * Load texture with given name
00107    */
00108   bool loadTexture(const std::string&);
00109   /**
00110    * Remove texture with given name
00111    */
00112   void deleteTexture(const std::string &);
00113   /**
00114    * Begin a new texture with given name
00115    */
00116   void beginNewTexture(const std::string&);
00117   /**
00118    * Activate a texture with given name
00119    */
00120   bool activateTexture(const std::string&,unsigned int);
00121   /**
00122    * Activate a texture with given name
00123    */
00124   bool activateTexture(const std::string&);
00125   /**
00126    * Disable texture with given name
00127    */
00128   void desactivateTexture();
00129   /**
00130    * Set animationStep for next textures (for next activateTexture)
00131    */
00132   void setAnimationFrame(unsigned int id) {
00133     animationFrame=id;
00134   }
00135   /**
00136    * Get animationStep of next textures
00137    */
00138   unsigned int getAnimationFrame() {
00139     return animationFrame;
00140   }
00141   /**
00142    * Clear vector of textures with error
00143    */
00144   void clearErrorVector() {
00145     texturesWithError.clear();
00146   }
00147   /**
00148    * Remove an entry of vector of textures with error
00149    */
00150   void removeEntryOfErrorVector(const std::string &name) {
00151     texturesWithError.erase(name);
00152   }
00153 
00154 
00155   /**
00156    * Register an external texture is GlTextureManager
00157    */
00158   void registerExternalTexture(const std::string &textureName, const GLuint textureId);
00159 
00160   /**
00161    * Get Texture loader
00162    */
00163   static GlTextureLoader* getTextureLoader() {
00164     return loader ? loader : (loader = new GlTextureLoader());
00165   }
00166 
00167   /**
00168    * Set Texture loader
00169    */
00170   static void setTextureLoader(GlTextureLoader* texLoader) {
00171     if (loader)
00172       delete loader;
00173 
00174     loader = texLoader;
00175   }
00176 
00177 private:
00178 
00179   /**
00180    * empty private constructor for singleton
00181    */
00182   GlTextureManager();
00183 
00184   static GlTextureManager* inst;
00185   static GlTextureLoader* loader;
00186 
00187   uintptr_t currentContext;
00188 
00189   ContextAndTextureMap texturesMap;
00190   std::set<std::string> texturesWithError;
00191 
00192   unsigned int animationFrame;
00193 
00194 };
00195 
00196 }
00197 
00198 #endif // Tulip_GLTEXTUREMANAGER_H
00199 ///@endcond
 All Classes Files Functions Variables Enumerations Enumerator Properties