Oolite 1.91.0.7646-241128-10e222e
Loading...
Searching...
No Matches
OOTexture.h
Go to the documentation of this file.
1/*
2
3OOTexture.h
4
5Load, track and manage textures. In general, this should be used through an
6OOMaterial.
7
8Note: OOTexture is abstract. The factory methods return instances of
9OOConcreteTexture, but special-case implementations are possible.
10
11
12Copyright (C) 2007-2013 Jens Ayton and contributors
13
14Permission is hereby granted, free of charge, to any person obtaining a copy
15of this software and associated documentation files (the "Software"), to deal
16in the Software without restriction, including without limitation the rights
17to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18copies of the Software, and to permit persons to whom the Software is
19furnished to do so, subject to the following conditions:
20
21The above copyright notice and this permission notice shall be included in all
22copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30SOFTWARE.
31
32*/
33
34#import <Foundation/Foundation.h>
35
36#import "OOOpenGL.h"
37#import "OOPixMap.h"
38#import "OOWeakReference.h"
39
41
42
43enum
44{
49
52
53 kOOTextureNoShrink = 0x000010UL,
55 kOOTextureRepeatS = 0x000040UL,
56 kOOTextureRepeatT = 0x000080UL,
57 kOOTextureAllowRectTexture = 0x000100UL, // Indicates that GL_TEXTURE_RECTANGLE_EXT may be used instead of GL_TEXTURE_2D. See -texCoordsScale for a discussion of rectangle textures.
58 kOOTextureNoFNFMessage = 0x000200UL, // Don't log file not found error
59 kOOTextureNeverScale = 0x000400UL, // Don't rescale texture, even if rect textures are not available. This *must not* be used for regular textures, but may be passed to OOTextureLoader when being used for other purposes.
60 kOOTextureAlphaMask = 0x000800UL, // Single-channel texture should be GL_ALPHA, not GL_LUMINANCE. No effect for multi-channel textures.
62
63 kOOTextureSRGBA = 0x002000UL,
64
67 kOOTextureExtractChannelR = 0x100000UL, // 001
68 kOOTextureExtractChannelG = 0x300000UL, // 011
69 kOOTextureExtractChannelB = 0x500000UL, // 101
70 kOOTextureExtractChannelA = 0x700000UL, // 111
71
75
77
90
95};
96
97
98typedef uint32_t OOTextureFlags;
99
100
101#define kOOTextureDefaultAnisotropy 0.5
102#define kOOTextureDefaultLODBias -0.25
103
104
105enum
106{
108
110 kOOTextureDataGrayscale = kOOPixMapGrayscale, // GL_LUMINANCE (or GL_ALPHA with kOOTextureAlphaMask)
114
115
117{
118#ifndef NDEBUG
119@protected
120 BOOL _trace;
121#endif
122}
123
124/* Load a texture, looking in Textures directories.
125
126 NOTE: anisotropy is normalized to the range [0, 1]. 1 means as high an
127 anisotropy setting as the hardware supports.
128
129 This method may change; +textureWithConfiguration is generally more
130 appropriate.
131*/
132+ (id) textureWithName:(NSString *)name
133 inFolder:(NSString *)directory
134 options:(OOTextureFlags)options
135 anisotropy:(GLfloat)anisotropy
136 lodBias:(GLfloat)lodBias;
137
138/* Equivalent to textureWithName:name
139 inFolder:directory
140 options:kOOTextureDefaultOptions
141 anisotropy:kOOTextureDefaultAnisotropy
142 lodBias:kOOTextureDefaultLODBias
143*/
144+ (id) textureWithName:(NSString *)name
145 inFolder:(NSString*)directory;
146
147/* Load a texure, looking in Textures directories, using configuration
148 dictionary or name. (That is, configuration may be either an NSDictionary
149 or an NSString.)
150
151 Supported keys:
152 name (string, required)
153 min_filter (string, one of "default", "nearest", "linear", "mipmap")
154 max_filter (string, one of "default", "nearest", "linear")
155 noShrink (boolean)
156 repeat_s (boolean)
157 repeat_t (boolean)
158 cube_map (boolean)
159 anisotropy (real)
160 texture_LOD_bias (real)
161 extract_channel (string, one of "r", "g", "b", "a")
162 */
163+ (id) textureWithConfiguration:(id)configuration;
164+ (id) textureWithConfiguration:(id)configuration extraOptions:(OOTextureFlags)extraOptions;
165
166/* Return the "null texture", a texture object representing an empty texture.
167 Applying the null texture is equivalent to calling [OOTexture applyNone].
168*/
169+ (id) nullTexture;
170
171/* Load a texture from a generator.
172*/
173+ (id) textureWithGenerator:(OOTextureGenerator *)generator;
174
175// Load a texture from a generator with option to force an enqueue
176+ (id) textureWithGenerator:(OOTextureGenerator *)generator enqueue:(BOOL) enqueue;
177
178/* Bind the texture to the current texture unit.
179 This will block until loading is completed.
180*/
181- (void) apply;
182
183+ (void) applyNone;
184
185/* Ensure texture is loaded. This is required because setting up textures
186 inside display lists isn't allowed.
187*/
188- (void) ensureFinishedLoading;
189
190/* Check whether a texture has loaded. NOTE: this does not do the setup that
191 -ensureFinishedLoading does, so -ensureFinishedLoading is still required
192 before using the texture in a display list.
193*/
194- (BOOL) isFinishedLoading;
195
196- (NSString *) cacheKey;
197
198/* Dimensions in pixels.
199 This will block until loading is completed.
200*/
201- (NSSize) dimensions;
202
203/* Original file dimensions in pixels.
204 This will block until loading is completed.
205*/
206- (NSSize) originalDimensions;
207
208/* Check whether texture is mip-mapped.
209 This will block until loading is completed.
210*/
211- (BOOL) isMipMapped;
212
213/* Create a new pixmap with a copy of the texture data. The caller is
214 responsible for free()ing the resulting buffer.
215*/
216- (OOPixMap) copyPixMapRepresentation;
217
218/* Identify special texture types.
219*/
220- (BOOL) isRectangleTexture;
221- (BOOL) isCubeMap;
222
223
224/* Dimensions in texture coordinates.
225
226 If kOOTextureAllowRectTexture is set, and GL_EXT_texture_rectangle is
227 available, textures whose dimensions are not powers of two will be loaded
228 as rectangle textures. Rectangle textures use unnormalized co-ordinates;
229 that is, co-oridinates range from 0 to the actual size of the texture
230 rather than 0 to 1. Thus, for rectangle textures, -texCoordsScale returns
231 -dimensions (with the required wait for loading) for a rectangle texture.
232 For non-rectangle textures, (1, 1) is returned without delay. If the
233 texture has power-of-two dimensions, it will be loaded as a normal
234 texture.
235
236 Rectangle textures have additional limitations: kOOTextureMinFilterMipMap
237 is not supported (kOOTextureMinFilterLinear will be used instead), and
238 kOOTextureRepeatS/kOOTextureRepeatT will be ignored.
239
240 Note that 'rectangle texture' is a misnomer; non-rectangle textures may
241 be rectangular, as long as their sides are powers of two. Non-power-of-two
242 textures would be more descriptive, but this phrase is used for the
243 extension that allows 'normal' textures to have non-power-of-two sides
244 without additional restrictions. It is intended that OOTexture should
245 support this in future, but this shouldn’t affect the interface, only
246 avoid the scaling-to-power-of-two stage.
247*/
248- (NSSize) texCoordsScale;
249
250/* OpenGL texture name.
251 Not reccomended, but required for legacy TextureStore.
252*/
253- (GLint) glTextureName;
254
255// Forget all cached textures so new texture objects will reload.
256+ (void) clearCache;
257
258// Called by OOGraphicsResetManager as necessary.
259+ (void) rebindAllTextures;
260
261#ifndef NDEBUG
262- (void) setTrace:(BOOL)trace;
263
264+ (NSArray *) cachedTexturesByAge;
265+ (NSSet *) allTextures;
266
267- (size_t) dataSize;
268
269- (NSString *) name;
270#endif
271
272@end
273
274
275@interface NSDictionary (OOTextureConveniences)
276- (NSDictionary *) oo_textureSpecifierForKey:(id)key defaultName:(NSString *)name;
277@end
278
279@interface NSArray (OOTextureConveniences)
280- (NSDictionary *) oo_textureSpecifierAtIndex:(unsigned)index defaultName:(NSString *)name;
281@end
282
283NSDictionary *OOTextureSpecFromObject(id object, NSString *defaultName);
284
285
287
288
289BOOL OOCubeMapsAvailable(void);
290
291
292/* OOInterpretTextureSpecifier()
293
294 Interpret a texture specifier (string or dictionary). All out parameters
295 may be NULL.
296*/
297BOOL OOInterpretTextureSpecifier(id specifier, NSString **outName, OOTextureFlags *outOptions, float *outAnisotropy, float *outLODBias, BOOL ignoreExtract);
298
299/* OOMakeTextureSpecifier()
300
301 Create a texture specifier.
302
303 If internal is used, an optimized form unsuitable for serialization may be
304 used.
305*/
306NSDictionary *OOMakeTextureSpecifier(NSString *name, OOTextureFlags options, float anisotropy, float lodBias, BOOL internal);
307
308/* OOApplyTextureOptionDefaults()
309
310 Replace all default/automatic options with their current default values.
311*/
313
314
315// Texture specifier keys.
316extern NSString * const kOOTextureSpecifierNameKey;
317extern NSString * const kOOTextureSpecifierSwizzleKey;
318extern NSString * const kOOTextureSpecifierMinFilterKey;
319extern NSString * const kOOTextureSpecifierMagFilterKey;
320extern NSString * const kOOTextureSpecifierNoShrinkKey;
321extern NSString * const kOOTextureSpecifierExtraShrinkKey;
322extern NSString * const kOOTextureSpecifierRepeatSKey;
323extern NSString * const kOOTextureSpecifierRepeatTKey;
324extern NSString * const kOOTextureSpecifierCubeMapKey;
325extern NSString * const kOOTextureSpecifierAnisotropyKey;
326extern NSString * const kOOTextureSpecifierLODBiasKey;
327
328// Keys not used in texture setup, but put in specific texture specifiers to simplify plists.
329extern NSString * const kOOTextureSpecifierModulateColorKey;
330extern NSString * const kOOTextureSpecifierIlluminationModeKey;
331extern NSString * const kOOTextureSpecifierSelfColorKey;
332extern NSString * const kOOTextureSpecifierScaleFactorKey;
333extern NSString * const kOOTextureSpecifierBindingKey;
OOPixMapFormat
Definition OOPixMap.h:39
@ kOOPixMapInvalidFormat
Definition OOPixMap.h:40
@ kOOPixMapGrayscale
Definition OOPixMap.h:41
@ kOOPixMapRGBA
Definition OOPixMap.h:43
@ kOOPixMapGrayscaleAlpha
Definition OOPixMap.h:42
NSString *const kOOTextureSpecifierSelfColorKey
Definition OOTexture.m:59
NSString *const kOOTextureSpecifierNoShrinkKey
Definition OOTexture.m:49
NSString *const kOOTextureSpecifierNameKey
Definition OOTexture.m:45
BOOL OOCubeMapsAvailable(void)
Definition OOTexture.m:688
uint8_t OOTextureComponentsForFormat(OOTextureDataFormat format)
Definition OOTexture.m:667
@ kOOTextureDataGrayscaleAlpha
Definition OOTexture.h:111
@ kOOTextureDataGrayscale
Definition OOTexture.h:110
@ kOOTextureDataInvalid
Definition OOTexture.h:107
@ kOOTextureDataRGBA
Definition OOTexture.h:109
NSDictionary * OOMakeTextureSpecifier(NSString *name, OOTextureFlags options, float anisotropy, float lodBias, BOOL internal)
Definition OOTexture.m:774
NSString *const kOOTextureSpecifierRepeatTKey
Definition OOTexture.m:52
OOTextureFlags OOApplyTextureOptionDefaults(OOTextureFlags options)
Definition OOTexture.m:855
NSDictionary * OOTextureSpecFromObject(id object, NSString *defaultName)
Definition OOTexture.m:647
NSString *const kOOTextureSpecifierScaleFactorKey
Definition OOTexture.m:60
NSString *const kOOTextureSpecifierBindingKey
Definition OOTexture.m:61
NSString *const kOOTextureSpecifierIlluminationModeKey
Definition OOTexture.m:58
NSString *const kOOTextureSpecifierMinFilterKey
Definition OOTexture.m:47
@ kOOTextureRepeatS
Definition OOTexture.h:55
@ kOOTextureAllowRectTexture
Definition OOTexture.h:57
@ kOOTextureExtractChannelA
Definition OOTexture.h:70
@ kOOTextureExtractChannelB
Definition OOTexture.h:69
@ kOOTextureFlagsAllowedForCubeMap
Definition OOTexture.h:93
@ kOOTextureNoFNFMessage
Definition OOTexture.h:58
@ kOOTextureNoShrink
Definition OOTexture.h:53
@ kOOTextureMinFilterDefault
Definition OOTexture.h:45
@ kOOTextureExtractChannelNone
Definition OOTexture.h:66
@ kOOTextureMagFilterMask
Definition OOTexture.h:73
@ kOOTextureExtractChannelMask
Definition OOTexture.h:65
@ kOOTextureDefinedFlags
Definition OOTexture.h:78
@ kOOTextureMinFilterLinear
Definition OOTexture.h:47
@ kOOTextureAllowCubeMap
Definition OOTexture.h:61
@ kOOTextureMinFilterMask
Definition OOTexture.h:72
@ kOOTextureSRGBA
Definition OOTexture.h:63
@ kOOTextureRepeatT
Definition OOTexture.h:56
@ kOOTextureDefaultOptions
Definition OOTexture.h:76
@ kOOTextureMinFilterMipMap
Definition OOTexture.h:48
@ kOOTextureNeverScale
Definition OOTexture.h:59
@ kOOTextureAlphaMask
Definition OOTexture.h:60
@ kOOTextureMagFilterNearest
Definition OOTexture.h:50
@ kOOTextureExtraShrink
Definition OOTexture.h:54
@ kOOTextureExtractChannelG
Definition OOTexture.h:68
@ kOOTextureFlagsMask
Definition OOTexture.h:74
@ kOOTextureFlagsAllowedForRectangleTexture
Definition OOTexture.h:91
@ kOOTextureExtractChannelR
Definition OOTexture.h:67
@ kOOTextureMagFilterLinear
Definition OOTexture.h:51
@ kOOTextureMinFilterNearest
Definition OOTexture.h:46
NSString *const kOOTextureSpecifierMagFilterKey
Definition OOTexture.m:48
NSString *const kOOTextureSpecifierAnisotropyKey
Definition OOTexture.m:54
uint32_t OOTextureFlags
Definition OOTexture.h:98
NSString *const kOOTextureSpecifierRepeatSKey
Definition OOTexture.m:51
OOPixMapFormat OOTextureDataFormat
Definition OOTexture.h:113
NSString *const kOOTextureSpecifierModulateColorKey
Definition OOTexture.m:57
BOOL OOInterpretTextureSpecifier(id specifier, NSString **outName, OOTextureFlags *outOptions, float *outAnisotropy, float *outLODBias, BOOL ignoreExtract)
Definition OOTexture.m:694
NSString *const kOOTextureSpecifierCubeMapKey
Definition OOTexture.m:53
NSString *const kOOTextureSpecifierSwizzleKey
Definition OOTexture.m:46
NSString *const kOOTextureSpecifierExtraShrinkKey
Definition OOTexture.m:50
NSString *const kOOTextureSpecifierLODBiasKey
Definition OOTexture.m:55
BOOL _trace
Definition OOTexture.h:120