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