Oolite
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#pragma once
35
36#import <Foundation/Foundation.h>
37
38#import "OOOpenGL.h"
39#import "OOPixMap.h"
40#import "OOWeakReference.h"
41
43
44enum {
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
93};
94
95typedef uint32_t OOTextureFlags;
96
97#define kOOTextureDefaultAnisotropy 0.5
98#define kOOTextureDefaultLODBias -0.25
99
100enum {
102
104 kOOTextureDataGrayscale = kOOPixMapGrayscale, // GL_LUMINANCE (or GL_ALPHA with kOOTextureAlphaMask)
106};
108
110#ifdef OO_DEBUG_MONITOR_ENABLED
111@protected
112 BOOL _trace;
113#endif
114}
115
116/* Load a texture, looking in Textures directories.
117
118 NOTE: anisotropy is normalized to the range [0, 1]. 1 means as high an
119 anisotropy setting as the hardware supports.
120
121 This method may change; +textureWithConfiguration is generally more
122 appropriate.
123*/
124+ (id)textureWithName:(NSString*)name
125 inFolder:(NSString*)directory
126 options:(OOTextureFlags)options
127 anisotropy:(GLfloat)anisotropy
128 lodBias:(GLfloat)lodBias;
129
130/* Equivalent to textureWithName:name
131 inFolder:directory
132 options:kOOTextureDefaultOptions
133 anisotropy:kOOTextureDefaultAnisotropy
134 lodBias:kOOTextureDefaultLODBias
135*/
136+ (id)textureWithName:(NSString*)name
137 inFolder:(NSString*)directory;
138
139/* Load a texure, looking in Textures directories, using configuration
140 dictionary or name. (That is, configuration may be either an NSDictionary
141 or an NSString.)
142
143 Supported keys:
144 name (string, required)
145 min_filter (string, one of "default", "nearest", "linear", "mipmap")
146 max_filter (string, one of "default", "nearest", "linear")
147 noShrink (boolean)
148 repeat_s (boolean)
149 repeat_t (boolean)
150 cube_map (boolean)
151 anisotropy (real)
152 texture_LOD_bias (real)
153 extract_channel (string, one of "r", "g", "b", "a")
154 */
155+ (id)textureWithConfiguration:(id)configuration;
156+ (id)textureWithConfiguration:(id)configuration extraOptions:(OOTextureFlags)extraOptions;
157
158/* Return the "null texture", a texture object representing an empty texture.
159 Applying the null texture is equivalent to calling [OOTexture applyNone].
160*/
161+ (id)nullTexture;
162
163/* Load a texture from a generator.
164 */
165+ (id)textureWithGenerator:(OOTextureGenerator*)generator;
166
167// Load a texture from a generator with option to force an enqueue
168+ (id)textureWithGenerator:(OOTextureGenerator*)generator enqueue:(BOOL)enqueue;
169
170/* Bind the texture to the current texture unit.
171 This will block until loading is completed.
172*/
173- (void)apply;
174
175+ (void)applyNone;
176
177/* Ensure texture is loaded. This is required because setting up textures
178 inside display lists isn't allowed.
179*/
180- (void)ensureFinishedLoading;
181
182/* Check whether a texture has loaded. NOTE: this does not do the setup that
183 -ensureFinishedLoading does, so -ensureFinishedLoading is still required
184 before using the texture in a display list.
185*/
186- (BOOL)isFinishedLoading;
187
188- (NSString*)cacheKey;
189
190/* Dimensions in pixels.
191 This will block until loading is completed.
192*/
193- (NSSize)dimensions;
194
195/* Original file dimensions in pixels.
196 This will block until loading is completed.
197*/
198- (NSSize)originalDimensions;
199
200/* Check whether texture is mip-mapped.
201 This will block until loading is completed.
202*/
203- (BOOL)isMipMapped;
204
205/* Create a new pixmap with a copy of the texture data. The caller is
206 responsible for free()ing the resulting buffer.
207*/
208- (OOPixMap)copyPixMapRepresentation;
209
210/* Identify special texture types.
211 */
212- (BOOL)isRectangleTexture;
213- (BOOL)isCubeMap;
214
215/* Dimensions in texture coordinates.
216
217 If kOOTextureAllowRectTexture is set, and GL_EXT_texture_rectangle is
218 available, textures whose dimensions are not powers of two will be loaded
219 as rectangle textures. Rectangle textures use unnormalized co-ordinates;
220 that is, co-oridinates range from 0 to the actual size of the texture
221 rather than 0 to 1. Thus, for rectangle textures, -texCoordsScale returns
222 -dimensions (with the required wait for loading) for a rectangle texture.
223 For non-rectangle textures, (1, 1) is returned without delay. If the
224 texture has power-of-two dimensions, it will be loaded as a normal
225 texture.
226
227 Rectangle textures have additional limitations: kOOTextureMinFilterMipMap
228 is not supported (kOOTextureMinFilterLinear will be used instead), and
229 kOOTextureRepeatS/kOOTextureRepeatT will be ignored.
230
231 Note that 'rectangle texture' is a misnomer; non-rectangle textures may
232 be rectangular, as long as their sides are powers of two. Non-power-of-two
233 textures would be more descriptive, but this phrase is used for the
234 extension that allows 'normal' textures to have non-power-of-two sides
235 without additional restrictions. It is intended that OOTexture should
236 support this in future, but this shouldn’t affect the interface, only
237 avoid the scaling-to-power-of-two stage.
238*/
239- (NSSize)texCoordsScale;
240
241/* OpenGL texture name.
242 Not reccomended, but required for legacy TextureStore.
243*/
244- (GLint)glTextureName;
245
246// Forget all cached textures so new texture objects will reload.
247+ (void)clearCache;
248
249// Called by OOGraphicsResetManager as necessary.
250+ (void)rebindAllTextures;
251
252#ifdef OO_DEBUG_MONITOR_ENABLED
253- (void)setTrace:(BOOL)trace;
254
255+ (NSArray*)cachedTexturesByAge;
256+ (NSSet*)allTextures;
257
258- (size_t)dataSize;
259
260- (NSString*)name;
261#endif
262
263@end
264
265@interface NSDictionary (OOTextureConveniences)
266- (NSDictionary*)oo_textureSpecifierForKey:(id)key defaultName:(NSString*)name;
267@end
268
269@interface NSArray (OOTextureConveniences)
270- (NSDictionary*)oo_textureSpecifierAtIndex:(unsigned)index defaultName:(NSString*)name;
271@end
272
273NSDictionary* OOTextureSpecFromObject(id object, NSString* defaultName);
274
276
277BOOL OOCubeMapsAvailable(void);
278
279/* OOInterpretTextureSpecifier()
280
281 Interpret a texture specifier (string or dictionary). All out parameters
282 may be NULL.
283*/
284BOOL OOInterpretTextureSpecifier(id specifier, NSString** outName, OOTextureFlags* outOptions, float* outAnisotropy, float* outLODBias, BOOL ignoreExtract);
285
286/* OOMakeTextureSpecifier()
287
288 Create a texture specifier.
289
290 If internal is used, an optimized form unsuitable for serialization may be
291 used.
292*/
293NSDictionary* OOMakeTextureSpecifier(NSString* name, OOTextureFlags options, float anisotropy, float lodBias, BOOL internal);
294
295/* OOApplyTextureOptionDefaults()
296
297 Replace all default/automatic options with their current default values.
298*/
300
301// Texture specifier keys.
302extern NSString* const kOOTextureSpecifierNameKey;
303extern NSString* const kOOTextureSpecifierSwizzleKey;
304extern NSString* const kOOTextureSpecifierMinFilterKey;
305extern NSString* const kOOTextureSpecifierMagFilterKey;
306extern NSString* const kOOTextureSpecifierNoShrinkKey;
307extern NSString* const kOOTextureSpecifierExtraShrinkKey;
308extern NSString* const kOOTextureSpecifierRepeatSKey;
309extern NSString* const kOOTextureSpecifierRepeatTKey;
310extern NSString* const kOOTextureSpecifierCubeMapKey;
311extern NSString* const kOOTextureSpecifierAnisotropyKey;
312extern NSString* const kOOTextureSpecifierLODBiasKey;
313
314// Keys not used in texture setup, but put in specific texture specifiers to simplify plists.
315extern NSString* const kOOTextureSpecifierModulateColorKey;
316extern NSString* const kOOTextureSpecifierIlluminationModeKey;
317extern NSString* const kOOTextureSpecifierSelfColorKey;
318extern NSString* const kOOTextureSpecifierScaleFactorKey;
319extern NSString* const kOOTextureSpecifierBindingKey;
OOPixMapFormat
Definition OOPixMap.h:36
@ kOOPixMapInvalidFormat
Definition OOPixMap.h:37
@ kOOPixMapGrayscale
Definition OOPixMap.h:38
@ kOOPixMapRGBA
Definition OOPixMap.h:40
@ kOOPixMapGrayscaleAlpha
Definition OOPixMap.h:39
NSString *const kOOTextureSpecifierSelfColorKey
Definition OOTexture.m:58
NSString *const kOOTextureSpecifierNoShrinkKey
Definition OOTexture.m:48
NSString *const kOOTextureSpecifierNameKey
Definition OOTexture.m:44
BOOL OOCubeMapsAvailable(void)
Definition OOTexture.m:643
uint8_t OOTextureComponentsForFormat(OOTextureDataFormat format)
Definition OOTexture.m:624
NSDictionary * OOMakeTextureSpecifier(NSString *name, OOTextureFlags options, float anisotropy, float lodBias, BOOL internal)
Definition OOTexture.m:736
NSString *const kOOTextureSpecifierRepeatTKey
Definition OOTexture.m:51
OOTextureFlags OOApplyTextureOptionDefaults(OOTextureFlags options)
Definition OOTexture.m:819
NSDictionary * OOTextureSpecFromObject(id object, NSString *defaultName)
Definition OOTexture.m:602
NSString *const kOOTextureSpecifierScaleFactorKey
Definition OOTexture.m:59
@ kOOTextureRepeatS
Definition OOTexture.h:55
@ kOOTextureAllowRectTexture
Definition OOTexture.h:57
@ kOOTextureExtractChannelA
Definition OOTexture.h:70
@ kOOTextureExtractChannelB
Definition OOTexture.h:69
@ kOOTextureFlagsAllowedForCubeMap
Definition OOTexture.h:92
@ 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 kOOTextureSpecifierBindingKey
Definition OOTexture.m:60
@ kOOTextureDataGrayscaleAlpha
Definition OOTexture.h:105
@ kOOTextureDataGrayscale
Definition OOTexture.h:104
@ kOOTextureDataInvalid
Definition OOTexture.h:101
@ kOOTextureDataRGBA
Definition OOTexture.h:103
NSString *const kOOTextureSpecifierIlluminationModeKey
Definition OOTexture.m:57
NSString *const kOOTextureSpecifierMinFilterKey
Definition OOTexture.m:46
NSString *const kOOTextureSpecifierMagFilterKey
Definition OOTexture.m:47
NSString *const kOOTextureSpecifierAnisotropyKey
Definition OOTexture.m:53
uint32_t OOTextureFlags
Definition OOTexture.h:95
NSString *const kOOTextureSpecifierRepeatSKey
Definition OOTexture.m:50
OOPixMapFormat OOTextureDataFormat
Definition OOTexture.h:107
NSString *const kOOTextureSpecifierModulateColorKey
Definition OOTexture.m:56
BOOL OOInterpretTextureSpecifier(id specifier, NSString **outName, OOTextureFlags *outOptions, float *outAnisotropy, float *outLODBias, BOOL ignoreExtract)
Definition OOTexture.m:648
NSString *const kOOTextureSpecifierCubeMapKey
Definition OOTexture.m:52
NSString *const kOOTextureSpecifierSwizzleKey
Definition OOTexture.m:45
NSString *const kOOTextureSpecifierExtraShrinkKey
Definition OOTexture.m:49
NSString *const kOOTextureSpecifierLODBiasKey
Definition OOTexture.m:54