Panda3D

texturePool.h

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