Panda3D
texturePool.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file texturePool.I
10  * @author drose
11  * @date 2000-04-26
12  * @author fperazzi, PandaSE
13  * @date 2010-04-29
14  */
15 
16 /**
17  * Returns true if the texture has ever been loaded, false otherwise.
18  */
19 INLINE bool TexturePool::
20 has_texture(const Filename &filename) {
21  return get_global_ptr()->ns_has_texture(filename);
22 }
23 
24 /**
25  * Loads the given filename up into a texture, if it has not already been
26  * loaded, and returns true to indicate success, or false to indicate failure.
27  * If this returns true, it is guaranteed that a subsequent call to
28  * load_texture() with the same texture name will return a valid Texture
29  * pointer.
30  */
31 INLINE bool TexturePool::
32 verify_texture(const Filename &filename) {
33  return load_texture(filename) != nullptr;
34 }
35 
36 /**
37  * Loads the given filename up into a texture, if it has not already been
38  * loaded, and returns the new texture. If a texture with the same filename
39  * was previously loaded, returns that one instead. If the texture file
40  * cannot be found, returns NULL.
41  *
42  * If read_mipmaps is true, the filename should contain a hash mark ('#'),
43  * which will be filled in with the mipmap level number; and the texture will
44  * be defined with a series of images, one for each mipmap level.
45  */
46 INLINE Texture *TexturePool::
47 load_texture(const Filename &filename, int primary_file_num_channels,
48  bool read_mipmaps, const LoaderOptions &options) {
49  return get_global_ptr()->ns_load_texture(filename, primary_file_num_channels,
50  read_mipmaps, options);
51 }
52 
53 /**
54  * Loads the given filename up into a texture, if it has not already been
55  * loaded, and returns the new texture. If a texture with the same filename
56  * was previously loaded, returns that one instead. If the texture file
57  * cannot be found, returns NULL.
58  *
59  * If read_mipmaps is true, both filenames should contain a hash mark ('#'),
60  * which will be filled in with the mipmap level number; and the texture will
61  * be defined with a series of images, two for each mipmap level.
62  */
63 INLINE Texture *TexturePool::
64 load_texture(const Filename &filename, const Filename &alpha_filename,
65  int primary_file_num_channels, int alpha_file_channel,
66  bool read_mipmaps, const LoaderOptions &options) {
67  return get_global_ptr()->ns_load_texture(filename, alpha_filename,
68  primary_file_num_channels,
69  alpha_file_channel,
70  read_mipmaps, options);
71 }
72 
73 /**
74  * Loads a 3-D texture that is specified with a series of n pages, all
75  * numbered in sequence, and beginning with index 0. The filename should
76  * include a sequence of one or more hash characters ("#") which will be
77  * filled in with the index number of each level.
78  *
79  * If read_mipmaps is true, the filename should contain an additional hash
80  * mark. The first hash mark will be filled in with the mipmap level number,
81  * and the second with the index number of each 3-d level.
82  */
83 INLINE Texture *TexturePool::
84 load_3d_texture(const Filename &filename_pattern, bool read_mipmaps,
85  const LoaderOptions &options) {
86  return get_global_ptr()->ns_load_3d_texture(filename_pattern, read_mipmaps,
87  options);
88 }
89 
90 /**
91  * Loads a 2-D texture array that is specified with a series of n pages, all
92  * numbered in sequence, and beginning with index 0. The filename should
93  * include a sequence of one or more hash characters ("#") which will be
94  * filled in with the index number of each level.
95  *
96  * If read_mipmaps is true, the filename should contain an additional hash
97  * mark. The first hash mark will be filled in with the mipmap level number,
98  * and the second with the index number of each 2-d level.
99  */
100 INLINE Texture *TexturePool::
101 load_2d_texture_array(const Filename &filename_pattern, bool read_mipmaps,
102  const LoaderOptions &options) {
103  return get_global_ptr()->ns_load_2d_texture_array(filename_pattern, read_mipmaps,
104  options);
105 }
106 
107 /**
108  * Loads a cube map texture that is specified with a series of 6 pages,
109  * numbered 0 through 5. The filename should include a sequence of one or
110  * more hash characters ("#") which will be filled in with the index number of
111  * each pagee.
112  *
113  * If read_mipmaps is true, the filename should contain an additional hash
114  * mark. The first hash mark will be filled in with the mipmap level number,
115  * and the second with the face number, 0 through 5.
116  */
117 INLINE Texture *TexturePool::
118 load_cube_map(const Filename &filename_pattern, bool read_mipmaps,
119  const LoaderOptions &options) {
120  return get_global_ptr()->ns_load_cube_map(filename_pattern, read_mipmaps,
121  options);
122 }
123 
124 /**
125  * Returns a standard Texture object that has been created with
126  * Texture::generate_normalization_cube_map(). This Texture may be shared by
127  * any application code requiring a normalization cube map. It will be at
128  * least as large as the specified size, though it may be larger.
129  */
130 INLINE Texture *TexturePool::
132  return get_global_ptr()->ns_get_normalization_cube_map(size);
133 }
134 
135 /**
136  * Returns a standard Texture object that has been created with
137  * Texture::generate_alpha_scale_map().
138  *
139  * This Texture object is used internally by Panda to apply an alpha scale to
140  * an object (instead of munging its vertices) when
141  * gsg->get_alpha_scale_via_texture() returns true.
142  */
143 INLINE Texture *TexturePool::
145  return get_global_ptr()->ns_get_alpha_scale_map();
146 }
147 
148 /**
149  * Adds the indicated already-loaded texture to the pool. The texture must
150  * have a filename set for its name. The texture will always replace any
151  * previously-loaded texture in the pool that had the same filename.
152  */
153 INLINE void TexturePool::
154 add_texture(Texture *texture) {
155  get_global_ptr()->ns_add_texture(texture);
156 }
157 
158 /**
159  * Removes the indicated texture from the pool, indicating it will never be
160  * loaded again; the texture may then be freed. If this function is never
161  * called, a reference count will be maintained on every texture every loaded,
162  * and textures will never be freed.
163  *
164  * The texture's name should not have been changed during its lifetime, or
165  * this function may fail to locate it in the pool.
166  */
167 INLINE void TexturePool::
169  get_global_ptr()->ns_release_texture(texture);
170 }
171 
172 /**
173  * Releases all textures in the pool and restores the pool to the empty state.
174  */
175 INLINE void TexturePool::
177  get_global_ptr()->ns_release_all_textures();
178 }
179 
180 /**
181  * Should be called when the model-path changes, to blow away the cache of
182  * texture pathnames found along the model-path.
183  */
184 INLINE void TexturePool::
186  get_global_ptr()->_relpath_lookup.clear();
187 }
188 
189 /**
190  * Releases only those textures in the pool that have a reference count of
191  * exactly 1; i.e. only those textures that are not being used outside of the
192  * pool. Returns the number of textures released.
193  */
194 INLINE int TexturePool::
196  return get_global_ptr()->ns_garbage_collect();
197 }
198 
199 /**
200  * Lists the contents of the texture pool to the indicated output stream.
201  */
202 INLINE void TexturePool::
203 list_contents(std::ostream &out) {
204  get_global_ptr()->ns_list_contents(out);
205 }
206 
207 /**
208  * Lists the contents of the texture pool to cout
209  */
210 INLINE void TexturePool::
212  get_global_ptr()->ns_list_contents(std::cout);
213 }
214 
215 /**
216  * Returns the first texture found in the pool that matches the indicated name
217  * (which may contain wildcards). Returns the texture if it is found, or NULL
218  * if it is not.
219  */
220 INLINE Texture *TexturePool::
221 find_texture(const std::string &name) {
222  return get_global_ptr()->ns_find_texture(name);
223 }
224 
225 /**
226  * Returns the set of all textures found in the pool that match the indicated
227  * name (which may contain wildcards).
228  */
230 find_all_textures(const std::string &name) {
231  return get_global_ptr()->ns_find_all_textures(name);
232 }
233 
234 /**
235  * Sets a bogus filename that will be loaded in lieu of any textures requested
236  * from this point on.
237  */
238 INLINE void TexturePool::
240  get_global_ptr()->_fake_texture_image = filename;
241 }
242 
243 /**
244  * Restores normal behavior of loading the textures actually requested.
245  */
246 INLINE void TexturePool::
248  set_fake_texture_image(std::string());
249 }
250 
251 /**
252  * Returns true if fake_texture_image mode has been enabled, false if we are
253  * in the normal mode.
254  */
255 INLINE bool TexturePool::
257  return !get_fake_texture_image().empty();
258 }
259 
260 /**
261  * Returns the filename that was specified with a previous call to
262  * set_fake_texture_image().
263  */
264 INLINE const Filename &TexturePool::
266  return get_global_ptr()->_fake_texture_image;
267 }
268 
269 /**
270  * Creates a new Texture object of the appropriate type for the indicated
271  * filename extension, according to the types that have been registered via
272  * register_texture_type().
273  */
274 PT(Texture) TexturePool::
275 make_texture(const std::string &extension) {
276  return get_global_ptr()->ns_make_texture(extension);
277 }
278 
279 /**
280  * Defines relative ordering between LookupKey instances.
281  */
282 INLINE bool TexturePool::LookupKey::
283 operator < (const LookupKey &other) const {
284  if (_fullpath != other._fullpath) {
285  return _fullpath < other._fullpath;
286  }
287  if (_alpha_fullpath != other._alpha_fullpath) {
288  return _alpha_fullpath < other._alpha_fullpath;
289  }
290  if (_primary_file_num_channels != other._primary_file_num_channels) {
291  return _primary_file_num_channels < other._primary_file_num_channels;
292  }
293  if (_alpha_file_channel != other._alpha_file_channel) {
294  return _alpha_file_channel < other._alpha_file_channel;
295  }
296  return _texture_type < other._texture_type;
297 }
static void list_contents()
Lists the contents of the texture pool to cout.
Definition: texturePool.I:211
static void clear_fake_texture_image()
Restores normal behavior of loading the textures actually requested.
Definition: texturePool.I:247
static Texture * load_cube_map(const Filename &filename_pattern, bool read_mipmaps=false, const LoaderOptions &options=LoaderOptions())
Loads a cube map texture that is specified with a series of 6 pages, numbered 0 through 5.
Definition: texturePool.I:118
static int garbage_collect()
Releases only those textures in the pool that have a reference count of exactly 1; i....
Definition: texturePool.I:195
Specifies parameters that may be passed to the loader.
Definition: loaderOptions.h:23
static void release_texture(Texture *texture)
Removes the indicated texture from the pool, indicating it will never be loaded again; the texture ma...
Definition: texturePool.I:168
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:71
static void rehash()
Should be called when the model-path changes, to blow away the cache of texture pathnames found along...
Definition: texturePool.I:185
static bool has_fake_texture_image()
Returns true if fake_texture_image mode has been enabled, false if we are in the normal mode.
Definition: texturePool.I:256
PT(Texture) TexturePool
Creates a new Texture object of the appropriate type for the indicated filename extension,...
Definition: texturePool.I:274
static bool verify_texture(const Filename &filename)
Loads the given filename up into a texture, if it has not already been loaded, and returns true to in...
Definition: texturePool.I:32
static Texture * get_alpha_scale_map()
Returns a standard Texture object that has been created with Texture::generate_alpha_scale_map().
Definition: texturePool.I:144
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
static const Filename & get_fake_texture_image()
Returns the filename that was specified with a previous call to set_fake_texture_image().
Definition: texturePool.I:265
static void set_fake_texture_image(const Filename &filename)
Sets a bogus filename that will be loaded in lieu of any textures requested from this point on.
Definition: texturePool.I:239
static Texture * get_normalization_cube_map(int size)
Returns a standard Texture object that has been created with Texture::generate_normalization_cube_map...
Definition: texturePool.I:131
static bool has_texture(const Filename &filename)
Returns true if the texture has ever been loaded, false otherwise.
Definition: texturePool.I:20
Manages a list of Texture objects, as returned by TexturePool::find_all_textures().
static Texture * load_texture(const Filename &filename, int primary_file_num_channels=0, bool read_mipmaps=false, const LoaderOptions &options=LoaderOptions())
Loads the given filename up into a texture, if it has not already been loaded, and returns the new te...
Definition: texturePool.I:47
static TexturePool * get_global_ptr()
Initializes and/or returns the global pointer to the one TexturePool object in the system.
static void add_texture(Texture *texture)
Adds the indicated already-loaded texture to the pool.
Definition: texturePool.I:154
static TextureCollection find_all_textures(const std::string &name="*")
Returns the set of all textures found in the pool that match the indicated name (which may contain wi...
Definition: texturePool.I:230
static void release_all_textures()
Releases all textures in the pool and restores the pool to the empty state.
Definition: texturePool.I:176
static Texture * load_3d_texture(const Filename &filename_pattern, bool read_mipmaps=false, const LoaderOptions &options=LoaderOptions())
Loads a 3-D texture that is specified with a series of n pages, all numbered in sequence,...
Definition: texturePool.I:84
static Texture * load_2d_texture_array(const Filename &filename_pattern, bool read_mipmaps=false, const LoaderOptions &options=LoaderOptions())
Loads a 2-D texture array that is specified with a series of n pages, all numbered in sequence,...
Definition: texturePool.I:101
static Texture * find_texture(const std::string &name)
Returns the first texture found in the pool that matches the indicated name (which may contain wildca...
Definition: texturePool.I:221