Panda3D
 All Classes Functions Variables Enumerations
texturePool.h
1 // Filename: texturePool.h
2 // Created by: drose (26Apr00)
3 // Updated by: fperazzi, PandaSE(29Apr10) (added load_2d_texture_array)
4 //
5 ////////////////////////////////////////////////////////////////////
6 //
7 // PANDA 3D SOFTWARE
8 // Copyright (c) Carnegie Mellon University. All rights reserved.
9 //
10 // All use of this software is subject to the terms of the revised BSD
11 // license. You should have received a copy of this license along
12 // with this source code in a file named "LICENSE."
13 //
14 ////////////////////////////////////////////////////////////////////
15 
16 #ifndef TEXTUREPOOL_H
17 #define TEXTUREPOOL_H
18 
19 #include "pandabase.h"
20 #include "texture.h"
21 #include "filename.h"
22 #include "config_gobj.h"
23 #include "loaderOptions.h"
24 #include "pmutex.h"
25 #include "pmap.h"
26 #include "textureCollection.h"
27 
28 class TexturePoolFilter;
29 class BamCache;
30 class BamCacheRecord;
31 
32 ////////////////////////////////////////////////////////////////////
33 // Class : TexturePool
34 // Description : This is the preferred interface for loading textures
35 // from image files. It unifies all references to the
36 // same filename, so that multiple models that reference
37 // the same textures don't waste texture memory
38 // unnecessarily.
39 ////////////////////////////////////////////////////////////////////
40 class EXPCL_PANDA_GOBJ TexturePool {
41 PUBLISHED:
42  INLINE static bool has_texture(const Filename &filename);
43  INLINE static bool verify_texture(const Filename &filename);
44  BLOCKING INLINE static Texture *load_texture(const Filename &filename,
45  int primary_file_num_channels = 0,
46  bool read_mipmaps = false,
47  const LoaderOptions &options = LoaderOptions());
48  BLOCKING INLINE static Texture *load_texture(const Filename &filename,
49  const Filename &alpha_filename,
50  int primary_file_num_channels = 0,
51  int alpha_file_channel = 0,
52  bool read_mipmaps = false,
53  const LoaderOptions &options = LoaderOptions());
54  BLOCKING INLINE static Texture *load_3d_texture(const Filename &filename_pattern,
55  bool read_mipmaps = false,
56  const LoaderOptions &options = LoaderOptions());
57  BLOCKING INLINE static Texture *load_2d_texture_array(const Filename &filename_pattern,
58  bool read_mipmaps = false,
59  const LoaderOptions &options = LoaderOptions());
60  BLOCKING INLINE static Texture *load_cube_map(const Filename &filename_pattern,
61  bool read_mipmaps = false,
62  const LoaderOptions &options = LoaderOptions());
63 
64  INLINE static Texture *get_normalization_cube_map(int size);
65  INLINE static Texture *get_alpha_scale_map();
66 
67  INLINE static void add_texture(Texture *texture);
68  INLINE static void release_texture(Texture *texture);
69  INLINE static void release_all_textures();
70  INLINE static void rehash();
71 
72  INLINE static int garbage_collect();
73 
74  INLINE static void list_contents(ostream &out);
75  INLINE static void list_contents();
76 
77  INLINE static Texture *find_texture(const string &name);
78  INLINE static TextureCollection find_all_textures(const string &name = "*");
79 
80  INLINE static void set_fake_texture_image(const Filename &filename);
81  INLINE static void clear_fake_texture_image();
82  INLINE static bool has_fake_texture_image();
83  INLINE static const Filename &get_fake_texture_image();
84  INLINE static PT(Texture) make_texture(const string &extension);
85 
86  static void write(ostream &out);
87 
88 public:
89  typedef Texture::MakeTextureFunc MakeTextureFunc;
90  void register_texture_type(MakeTextureFunc *func, const string &extensions);
91  void register_filter(TexturePoolFilter *filter);
92 
93  MakeTextureFunc *get_texture_type(const string &extension) const;
94  void write_texture_types(ostream &out, int indent_level) const;
95 
96  static TexturePool *get_global_ptr();
97 
98 private:
99  TexturePool();
100 
101  bool ns_has_texture(const Filename &orig_filename);
102  Texture *ns_load_texture(const Filename &orig_filename,
103  int primary_file_num_channels,
104  bool read_mipmaps,
105  const LoaderOptions &options);
106  Texture *ns_load_texture(const Filename &orig_filename,
107  const Filename &orig_alpha_filename,
108  int primary_file_num_channels,
109  int alpha_file_channel,
110  bool read_mipmaps,
111  const LoaderOptions &options);
112  Texture *ns_load_3d_texture(const Filename &filename_pattern,
113  bool read_mipmaps,
114  const LoaderOptions &options);
115  Texture *ns_load_2d_texture_array(const Filename &filename_pattern,
116  bool read_mipmaps,
117  const LoaderOptions &options);
118  Texture *ns_load_cube_map(const Filename &filename_pattern,
119  bool read_mipmaps,
120  const LoaderOptions &options);
121  Texture *ns_get_normalization_cube_map(int size);
122  Texture *ns_get_alpha_scale_map();
123 
124  void ns_add_texture(Texture *texture);
125  void ns_release_texture(Texture *texture);
126  void ns_release_all_textures();
127  int ns_garbage_collect();
128  void ns_list_contents(ostream &out) const;
129  Texture *ns_find_texture(const string &name) const;
130  TextureCollection ns_find_all_textures(const string &name) const;
131  PT(Texture) ns_make_texture(const string &extension) const;
132 
133  void resolve_filename(Filename &new_filename, const Filename &orig_filename,
134  bool read_mipmaps, const LoaderOptions &options);
135 
136  void try_load_cache(PT(Texture) &tex, BamCache *cache,
137  const Filename &filename, PT(BamCacheRecord) &record,
138  bool &compressed_cache_record,
139  const LoaderOptions &options);
140  void report_texture_unreadable(const Filename &filename) const;
141 
142  // Methods to invoke a TexturePoolFilter.
143  PT(Texture) pre_load(const Filename &orig_filename,
144  const Filename &orig_alpha_filename,
145  int primary_file_num_channels,
146  int alpha_file_channel,
147  bool read_mipmaps, const LoaderOptions &options);
148  PT(Texture) post_load(Texture *tex);
149 
150  void load_filters();
151 
152  static TexturePool *_global_ptr;
153 
154  Mutex _lock;
156  Textures _textures; // indexed by fullpath
158  RelpathLookup _relpath_lookup;
159 
160  Filename _fake_texture_image;
161 
162  PT(Texture) _normalization_cube_map;
163  PT(Texture) _alpha_scale_map;
164 
166  TypeRegistry _type_registry;
167 
169  FilterRegistry _filter_registry;
170 };
171 
172 #include "texturePool.I"
173 
174 #endif
175 
176 
Specifies parameters that may be passed to the loader.
Definition: loaderOptions.h:26
This class maintains a cache of Bam and/or Txo objects generated from model files and texture images ...
Definition: bamCache.h:47
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:75
A standard mutex, or mutual exclusion lock.
Definition: pmutex.h:44
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
An instance of this class is written to the front of a Bam or Txo file to make the file a cached inst...
Manages a list of Texture objects, as returned by TexturePool::find_all_textures().
This is an abstract base class, a placeholder for any number of different classes that may wish to im...
This is the preferred interface for loading textures from image files.
Definition: texturePool.h:40