Panda3D

texturePool.h

00001 // Filename: texturePool.h
00002 // Created by:  drose (26Apr00)
00003 // Updated by: fperazzi, PandaSE(29Apr10) (added load_2d_texture_array)
00004 //
00005 ////////////////////////////////////////////////////////////////////
00006 //
00007 // PANDA 3D SOFTWARE
00008 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00009 //
00010 // All use of this software is subject to the terms of the revised BSD
00011 // license.  You should have received a copy of this license along
00012 // with this source code in a file named "LICENSE."
00013 //
00014 ////////////////////////////////////////////////////////////////////
00015 
00016 #ifndef TEXTUREPOOL_H
00017 #define TEXTUREPOOL_H
00018 
00019 #include "pandabase.h"
00020 #include "texture.h"
00021 #include "filename.h"
00022 #include "config_gobj.h"
00023 #include "loaderOptions.h"
00024 #include "pmutex.h"
00025 #include "pmap.h"
00026 #include "textureCollection.h"
00027 
00028 class TexturePoolFilter;
00029 class BamCache;
00030 class BamCacheRecord;
00031 
00032 ////////////////////////////////////////////////////////////////////
00033 //       Class : TexturePool
00034 // Description : This is the preferred interface for loading textures
00035 //               from image files.  It unifies all references to the
00036 //               same filename, so that multiple models that reference
00037 //               the same textures don't waste texture memory
00038 //               unnecessarily.
00039 ////////////////////////////////////////////////////////////////////
00040 class EXPCL_PANDA_GOBJ TexturePool {
00041 PUBLISHED:
00042   INLINE static bool has_texture(const Filename &filename);
00043   INLINE static bool verify_texture(const Filename &filename);
00044   INLINE static Texture *load_texture(const Filename &filename, 
00045                                       int primary_file_num_channels = 0,
00046                                       bool read_mipmaps = false,
00047                                       const LoaderOptions &options = LoaderOptions());
00048   INLINE static Texture *load_texture(const Filename &filename,
00049                                       const Filename &alpha_filename, 
00050                                       int primary_file_num_channels = 0,
00051                                       int alpha_file_channel = 0,
00052                                       bool read_mipmaps = false,
00053                                       const LoaderOptions &options = LoaderOptions());
00054   INLINE static Texture *load_3d_texture(const Filename &filename_pattern,
00055                                          bool read_mipmaps = false,
00056                                          const LoaderOptions &options = LoaderOptions());
00057   INLINE static Texture *load_2d_texture_array(const Filename &filename_pattern,
00058                                                bool read_mipmaps = false,
00059                                                const LoaderOptions &options = LoaderOptions());
00060   INLINE static Texture *load_cube_map(const Filename &filename_pattern,
00061                                        bool read_mipmaps = false,
00062                                        const LoaderOptions &options = LoaderOptions());
00063 
00064   INLINE static Texture *get_normalization_cube_map(int size);
00065   INLINE static Texture *get_alpha_scale_map();
00066 
00067   INLINE static void add_texture(Texture *texture);
00068   INLINE static void release_texture(Texture *texture);
00069   INLINE static void release_all_textures();
00070   INLINE static void rehash();
00071 
00072   INLINE static int garbage_collect();
00073 
00074   INLINE static void list_contents(ostream &out);
00075   INLINE static void list_contents();
00076 
00077   INLINE static Texture *find_texture(const string &name);
00078   INLINE static TextureCollection find_all_textures(const string &name = "*");
00079 
00080   INLINE static void set_fake_texture_image(const Filename &filename);
00081   INLINE static void clear_fake_texture_image();
00082   INLINE static bool has_fake_texture_image();
00083   INLINE static const Filename &get_fake_texture_image();
00084   INLINE static PT(Texture) make_texture(const string &extension);
00085 
00086   static void write(ostream &out);
00087 
00088 public:
00089   typedef Texture::MakeTextureFunc MakeTextureFunc;
00090   void register_texture_type(MakeTextureFunc *func, const string &extensions);
00091   void register_filter(TexturePoolFilter *filter);
00092   
00093   MakeTextureFunc *get_texture_type(const string &extension) const;
00094   void write_texture_types(ostream &out, int indent_level) const;
00095 
00096   static TexturePool *get_global_ptr();
00097 
00098 private:
00099   TexturePool();
00100 
00101   bool ns_has_texture(const Filename &orig_filename);
00102   Texture *ns_load_texture(const Filename &orig_filename, 
00103                            int primary_file_num_channels,
00104                            bool read_mipmaps,
00105                            const LoaderOptions &options);
00106   Texture *ns_load_texture(const Filename &orig_filename, 
00107                            const Filename &orig_alpha_filename, 
00108                            int primary_file_num_channels,
00109                            int alpha_file_channel,
00110                            bool read_mipmaps,
00111                            const LoaderOptions &options);
00112   Texture *ns_load_3d_texture(const Filename &filename_pattern,
00113                               bool read_mipmaps,
00114                               const LoaderOptions &options);
00115   Texture *ns_load_2d_texture_array(const Filename &filename_pattern,
00116                                     bool read_mipmaps,
00117                                     const LoaderOptions &options);
00118   Texture *ns_load_cube_map(const Filename &filename_pattern,
00119                             bool read_mipmaps,
00120                             const LoaderOptions &options);
00121   Texture *ns_get_normalization_cube_map(int size);
00122   Texture *ns_get_alpha_scale_map();
00123 
00124   void ns_add_texture(Texture *texture);
00125   void ns_release_texture(Texture *texture);
00126   void ns_release_all_textures();
00127   int ns_garbage_collect();
00128   void ns_list_contents(ostream &out) const;
00129   Texture *ns_find_texture(const string &name) const;
00130   TextureCollection ns_find_all_textures(const string &name) const;
00131   PT(Texture) ns_make_texture(const string &extension) const;
00132 
00133   void resolve_filename(Filename &new_filename, const Filename &orig_filename,
00134                         bool read_mipmaps, const LoaderOptions &options);
00135 
00136   void try_load_cache(PT(Texture) &tex, BamCache *cache, 
00137                       const Filename &filename, PT(BamCacheRecord) &record, 
00138                       bool &compressed_cache_record,
00139                       const LoaderOptions &options);
00140   void report_texture_unreadable(const Filename &filename) const;
00141 
00142   // Methods to invoke a TexturePoolFilter.
00143   PT(Texture) pre_load(const Filename &orig_filename, 
00144                        const Filename &orig_alpha_filename,
00145                        int primary_file_num_channels,
00146                        int alpha_file_channel,
00147                        bool read_mipmaps, const LoaderOptions &options);
00148   PT(Texture) post_load(Texture *tex);
00149 
00150   void load_filters();
00151 
00152   static TexturePool *_global_ptr;
00153 
00154   Mutex _lock;
00155   typedef pmap<Filename, PT(Texture)> Textures;
00156   Textures _textures;  // indexed by fullpath
00157   typedef pmap<Filename, Filename> RelpathLookup;
00158   RelpathLookup _relpath_lookup;
00159 
00160   Filename _fake_texture_image;
00161 
00162   PT(Texture) _normalization_cube_map;
00163   PT(Texture) _alpha_scale_map;
00164 
00165   typedef pmap<string, MakeTextureFunc *> TypeRegistry;
00166   TypeRegistry _type_registry;
00167 
00168   typedef pvector<TexturePoolFilter *> FilterRegistry;
00169   FilterRegistry _filter_registry;
00170 };
00171 
00172 #include "texturePool.I"
00173 
00174 #endif
00175 
00176 
 All Classes Functions Variables Enumerations