Oolite
Loading...
Searching...
No Matches
OOCombinedEmissionMapGenerator.m
Go to the documentation of this file.
1/*
2
3OOCombinedEmissionMapGenerator.m
4
5
6Copyright (C) 2010-2013 Jens Ayton
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
29
31#import "OOColor.h"
34#import "OOTextureInternal.h"
35#import "OOTextureScaling.h"
36
37#define DUMP_COMBINER 0
38
39static OOColor* ModulateColor(OOColor* a, OOColor* b);
40static void ScaleToMatch(OOPixMap* pmA, OOPixMap* pmB);
41
42@interface OOCombinedEmissionMapGenerator (Private)
43
44- (id)initWithEmissionMapSpec:(NSDictionary*)emissionMapSpec
45 emissionColor:(OOColor*)emissionColor
46 diffuseMap:(OOTexture*)diffuseMap
47 diffuseColor:(OOColor*)diffuseColor
48 illuminationMapSpec:(NSDictionary*)illuminationMapSpec
49 illuminationColor:(OOColor*)illuminationColor
50 isCombinedMap:(BOOL)isCombinedMap
51 optionsSpecifier:(NSDictionary*)spec;
52
53- (NSString*)constructCacheKey;
54
55@end
56
58
59- (id)initWithEmissionMapSpec:(NSDictionary*)emissionMapSpec
60 emissionColor:(OOColor*)emissionColor
61 diffuseMap:(OOTexture*)diffuseMap
62 diffuseColor:(OOColor*)diffuseColor
63 illuminationMapSpec:(NSDictionary*)illuminationMapSpec
64 illuminationColor:(OOColor*)illuminationColor
65 optionsSpecifier:(NSDictionary*)spec
66{
67 return [self initWithEmissionMapSpec:emissionMapSpec
68 emissionColor:emissionColor
69 diffuseMap:diffuseMap
70 diffuseColor:diffuseColor
71 illuminationMapSpec:illuminationMapSpec
72 illuminationColor:illuminationColor
74 optionsSpecifier:spec];
75}
76
77- (id)initWithEmissionAndIlluminationMapSpec:(NSDictionary*)emissionAndIlluminationMapSpec
78 diffuseMap:(OOTexture*)diffuseMap
79 diffuseColor:(OOColor*)diffuseColor
80 emissionColor:(OOColor*)emissionColor
81 illuminationColor:(OOColor*)illuminationColor
82 optionsSpecifier:(NSDictionary*)spec
83{
84 return [self initWithEmissionMapSpec:emissionAndIlluminationMapSpec
85 emissionColor:emissionColor
86 diffuseMap:diffuseMap
87 diffuseColor:diffuseColor
89 illuminationColor:illuminationColor
91 optionsSpecifier:spec];
92}
93
94- (id)initWithEmissionMapSpec:(NSDictionary*)emissionMapSpec
95 emissionColor:(OOColor*)emissionColor
96 diffuseMap:(OOTexture*)diffuseMap
97 diffuseColor:(OOColor*)diffuseColor
98 illuminationMapSpec:(NSDictionary*)illuminationMapSpec
99 illuminationColor:(OOColor*)illuminationColor
100 isCombinedMap:(BOOL)isCombinedMap
101 optionsSpecifier:(NSDictionary*)spec
102{
103 if (emissionMapSpec == nil && illuminationMapSpec == nil) {
104 [self release];
105 return nil;
106 }
107
108 NSParameterAssert(illuminationMapSpec == nil || !isCombinedMap);
109
110 uint32_t options;
111 GLfloat anisotropy;
112 GLfloat lodBias;
113 OOInterpretTextureSpecifier(spec, NULL, &options, &anisotropy, &lodBias, YES);
114 options = OOApplyTextureOptionDefaults(options);
115
116 if ((self = [super initWithPath:@"<generated emission map>" options:options])) {
117 /* Illumination contribution is:
118 illuminationMap * illuminationColor * diffuseMap * diffuseColor
119 Since illuminationColor and diffuseColor aren't used otherwise,
120 we may as well combine them up front.
121 */
122 illuminationColor = ModulateColor(diffuseColor, illuminationColor);
123
124 if ([emissionColor isWhite])
125 emissionColor = nil;
126 if ([illuminationColor isWhite])
127 illuminationColor = nil;
128 if (!isCombinedMap && illuminationMapSpec == nil)
129 diffuseMap = nil; // Diffuse map is only used with illumination
130
131 // Insert extraShrink flag here instead of using extraOptions later because we need it in cache key too.
132 NSMutableDictionary* emissionSpec = [emissionMapSpec mutableCopy];
133 [emissionSpec oo_setBool:YES forKey:kOOTextureSpecifierExtraShrinkKey];
134 _emissionSpec = emissionSpec;
135
136 NSMutableDictionary* illuminationSpec = [illuminationMapSpec mutableCopy];
137 [illuminationSpec oo_setBool:YES forKey:kOOTextureSpecifierExtraShrinkKey];
138 _illuminationSpec = illuminationSpec;
139
140 _diffuseMap = [diffuseMap retain];
141
142 _emissionColor = [emissionColor retain];
143 _illuminationColor = [illuminationColor retain];
144 _isCombinedMap = isCombinedMap;
145
146 _textureOptions = options;
147 _anisotropy = anisotropy;
148 _lodBias = lodBias;
149
150 _cacheKey = [self constructCacheKey];
151
152 if ([OOTexture existingTextureForKey:_cacheKey] == nil) {
153 /* Extract pixmap from diffuse map. This must be done in the main
154 thread even if scheduling is fixed, because it might involve
155 reading back pixels from OpenGL.
156 */
157 if (diffuseMap != nil) {
159#ifndef NDEBUG
160 _diffuseDesc = [[diffuseMap shortDescription] copy];
161#endif
162 }
163
164 /* Extract emission and illumination pixmaps from loaders. Ideally,
165 this would be done asynchronously, but that requires dependency
166 management in OOAsyncWorkManager.
167 */
168 OOTextureDataFormat format;
169 if (_emissionSpec != nil) {
170 OOTextureLoader* emissionMapLoader = [OOTextureLoader loaderWithTextureSpecifier:_emissionSpec
172 folder:@"Textures"];
173 [emissionMapLoader getResult:&_emissionPx format:&format originalWidth:NULL originalHeight:NULL];
174#ifndef NDEBUG
175 _emissionDesc = [[emissionMapLoader shortDescription] copy];
176#endif
177 }
178 if (_illuminationSpec != nil) {
179 OOTextureLoader* illuminationMapLoader = [OOTextureLoader loaderWithTextureSpecifier:_illuminationSpec
181 folder:@"Textures"];
182 [illuminationMapLoader getResult:&_illuminationPx format:&format originalWidth:NULL originalHeight:NULL];
183#ifndef NDEBUG
184 _illuminationDesc = [[illuminationMapLoader shortDescription] copy];
185#endif
186 }
187 }
188 }
189
190 return self;
191}
192
194{
195 NSMutableString* cacheKey = [NSMutableString string];
196
197 if (_isCombinedMap)
198 [cacheKey appendString:@"combined emission and illumination map;"];
199 else if (_emissionSpec == nil)
200 [cacheKey appendString:@"illumination map;"];
201 else if (_illuminationSpec == nil)
202 [cacheKey appendString:@"emission map;"];
203 else
204 [cacheKey appendString:@"merged emission and illumination map;"];
205
206 NSString* emissionDesc = nil;
207 if (_emissionSpec != nil) {
208 emissionDesc = OOTextureCacheKeyForSpecifier(_emissionSpec);
209 [cacheKey appendFormat:@"emission:{%@}", emissionDesc];
210 if (_emissionColor != nil)
211 [cacheKey appendFormat:@"*%@", [_emissionColor rgbaDescription]];
212 [cacheKey appendString:@";"];
213 }
214
215 NSString* illuminationDesc = nil;
216 if (_isCombinedMap) {
217 illuminationDesc = [NSString stringWithFormat:@"%@:a", emissionDesc];
218 } else if (_illuminationSpec != nil) {
219 illuminationDesc = OOTextureCacheKeyForSpecifier(_illuminationSpec);
220 }
221
222 if (illuminationDesc != nil) {
223 [cacheKey appendFormat:@"illumination:{%@}*{%@}", illuminationDesc, [_diffuseMap cacheKey]];
224 if (_illuminationColor != nil)
225 [cacheKey appendFormat:@"*%@", [_illuminationColor rgbaDescription]];
226 [cacheKey appendString:@";"];
227 }
228
229 return cacheKey;
230}
231
252
253#ifndef NDEBUG
255{
256 NSMutableString* result = [NSMutableString string];
257 BOOL haveIllumination = NO;
258
259 if (_emissionDesc != nil) {
260 [result appendFormat:@"emission map: %@", _emissionDesc];
261 if (_isCombinedMap) {
262 [result appendString:@".rgb"];
263 }
264 if (_emissionColor != nil) {
265 [result appendFormat:@" * %@", [_emissionColor rgbaDescription]];
266 }
267
268 if (_isCombinedMap) {
269 [result appendFormat:@", illumination map: %@.a", _emissionDesc];
270 haveIllumination = YES;
271 }
272 }
273
274 if (_illuminationDesc != nil) {
275 if (_emissionDesc != nil)
276 [result appendString:@", "];
277 [result appendFormat:@"illumination map: %@", _illuminationDesc];
278 haveIllumination = YES;
279 }
280
281 if (haveIllumination) {
282 if (_diffuseDesc != nil) {
283 [result appendFormat:@" * %@", _diffuseDesc];
284 }
285 if (_illuminationColor != nil) {
286 [result appendFormat:@" * %@", [_illuminationColor rgbaDescription]];
287 }
288 }
289
290 return result;
291}
292#endif
293
294- (uint32_t)textureOptions
295{
296 return _textureOptions;
297}
298
299- (GLfloat)anisotropy
300{
301 return _anisotropy;
302}
303
304- (GLfloat)lodBias
305{
306 return _lodBias;
307}
308
309- (NSString*)cacheKey
310{
311 return _cacheKey;
312}
313
315{
316 OOPixMap illuminationPx = kOONullPixMap;
317 BOOL haveEmission = NO, haveIllumination = NO, haveDiffuse = NO;
318
319#if DUMP_COMBINER
320 static unsigned sTexID = 0;
321 unsigned texID = ++sTexID, dumpCount = 0;
322
323#define DUMP(pm, label) OODumpPixMap(pm, [NSString stringWithFormat:@"lightmap %u.%u - %@", texID, ++dumpCount, label]);
324#else
325#define DUMP(pm, label) \
326 do { \
327 } while (0)
328#endif
329
330 haveEmission = !OOIsNullPixMap(_emissionPx);
331 if (haveEmission)
332 DUMP(_emissionPx, @"source emission map");
333
334 // Extract illumination component if emission_and_illumination_map.
335 if (haveEmission && _isCombinedMap && OOPixMapFormatHasAlpha(_emissionPx.format)) {
337 illuminationPx = OODuplicatePixMap(_emissionPx, 0);
338 OOExtractPixMapChannel(&illuminationPx, 3, YES);
339 haveIllumination = YES;
340 DUMP(illuminationPx, @"extracted illumination map");
341 }
342
343 // Tint emission map if necessary.
344 if (haveEmission && _emissionColor != nil) {
345 OOPixMapModulateUniform(&_emissionPx, [_emissionColor redComponent], [_emissionColor greenComponent], [_emissionColor blueComponent], 1.0);
346 DUMP(_emissionPx, @"modulated emission map");
347 }
348
350 NSAssert(!_isCombinedMap, @"OOCombinedEmissionMapGenerator configured with both illumination map and combined emission/illumination map.");
351
352 illuminationPx = _illuminationPx;
353 _illuminationPx.pixels = NULL;
354 haveIllumination = YES;
355 DUMP(illuminationPx, @"source illumination map");
356 }
357
358 // Tint illumination map if necessary.
359 if (haveIllumination && _illuminationColor != nil) {
360 OOPixMapModulateUniform(&illuminationPx, [_illuminationColor redComponent], [_illuminationColor greenComponent], [_illuminationColor blueComponent], 1.0);
361 DUMP(illuminationPx, @"modulated illumination map");
362 }
363
364 // Load diffuse map and combine with illumination map.
365 haveDiffuse = !OOIsNullPixMap(_diffusePx);
366 if (haveDiffuse)
367 DUMP(_diffusePx, @"source diffuse map");
368
369 if (haveIllumination && haveDiffuse) {
370 // Modulate illumination with diffuse map.
371 ScaleToMatch(&_diffusePx, &illuminationPx);
373 OOPixMapModulatePixMap(&illuminationPx, _diffusePx);
374 DUMP(illuminationPx, @"combined diffuse and illumination map");
375 }
377
378 if (haveIllumination) {
379 if (haveEmission) {
380 OOPixMapToRGBA(&illuminationPx);
381 OOPixMapAddPixMap(&_emissionPx, illuminationPx);
382 OOFreePixMap(&illuminationPx);
383 DUMP(_emissionPx, @"combined emission and illumination map");
384 } else if (haveIllumination) {
385 // No explicit emission map -> modulated illumination map is our only emission map.
386 _emissionPx = illuminationPx;
387 haveEmission = YES;
388 illuminationPx.pixels = NULL;
389 }
390 haveIllumination = NO; // Either way, illumination is now baked into emission.
391 }
392
393 (void)haveEmission;
394 (void)haveIllumination;
395
396 // Done: emissionPx now contains combined emission map.
399 _data = _emissionPx.pixels;
400 _width = _emissionPx.width;
401 _height = _emissionPx.height;
402 _rowBytes = _emissionPx.rowBytes;
403 _format = _emissionPx.format;
404
405 _emissionPx.pixels = NULL; // So it won't be freed by -dealloc.
406 }
407 if (_data == NULL) {
408 OOLogERR(@"texture.combinedEmissionMap.error", @"Unknown error loading %@", self);
409 }
410}
411
412@end
413
415{
416 if (a == nil)
417 return b;
418 if (b == nil)
419 return a;
420
421 OORGBAComponents ac, bc;
422 ac = [a rgbaComponents];
423 bc = [b rgbaComponents];
424
425 ac.r *= bc.r;
426 ac.g *= bc.g;
427 ac.b *= bc.b;
428 ac.a *= bc.a;
429
431}
432
433static void ScaleToMatch(OOPixMap* pmA, OOPixMap* pmB)
434{
435 NSCParameterAssert(pmA != NULL && pmB != NULL && OOIsValidPixMap(*pmA) && OOIsValidPixMap(*pmB));
436
437 OOPixMapDimension minWidth = MIN(pmA->width, pmB->width);
438 OOPixMapDimension minHeight = MIN(pmA->height, pmB->height);
439
440 if (pmA->width != minWidth || pmA->height != minHeight) {
441 *pmA = OOScalePixMap(*pmA, minWidth, minHeight, NO);
442 }
443 if (pmB->width != minWidth || pmB->height != minHeight) {
444 *pmB = OOScalePixMap(*pmB, minWidth, minHeight, NO);
445 }
446}
#define DUMP(pm, label)
static OOColor * ModulateColor(OOColor *a, OOColor *b)
static void ScaleToMatch(OOPixMap *pmA, OOPixMap *pmB)
#define DESTROY(x)
#define OOLogERR(class, format,...)
Definition OOLogging.h:125
#define MIN(A, B)
Definition OOMaths.h:91
BOOL OOPixMapModulateUniform(OOPixMap *ioPixMap, float f0, float f1, float f2, float f3)
BOOL OOExtractPixMapChannel(OOPixMap *ioPixMap, uint8_t channelIndex, BOOL compactWhenDone)
BOOL OOPixMapAddPixMap(OOPixMap *ioDstPixMap, OOPixMap otherPixMap)
BOOL OOPixMapModulatePixMap(OOPixMap *ioDstPixMap, OOPixMap otherPixMap)
BOOL OOPixMapToRGBA(OOPixMap *ioPixMap)
void OOFreePixMap(OOPixMap *ioPixMap)
Definition OOPixMap.m:80
uint32_t OOPixMapDimension
Definition OOPixMap.h:32
const OOPixMap kOONullPixMap
Definition OOPixMap.m:30
OOINLINE BOOL OOIsNullPixMap(OOPixMap pixMap)
Definition OOPixMap.h:53
BOOL OOIsValidPixMap(OOPixMap pixMap)
Definition OOPixMap.m:39
BOOL OOPixMapFormatHasAlpha(OOPixMapFormat format) PURE_FUNC
Definition OOPixMap.m:228
OOPixMap OODuplicatePixMap(OOPixMap srcPixMap, size_t desiredSize)
Definition OOPixMap.m:89
OOINLINE void OOCompactPixMap(OOPixMap *ioPixMap)
Definition OOPixMap.h:91
return nil
OOPixMap OOScalePixMap(OOPixMap srcPixMap, OOPixMapDimension dstWidth, OOPixMapDimension dstHeight, BOOL leaveSpaceForMipMaps)
OOTextureFlags OOApplyTextureOptionDefaults(OOTextureFlags options)
Definition OOTexture.m:819
OOPixMapFormat OOTextureDataFormat
Definition OOTexture.h:107
BOOL OOInterpretTextureSpecifier(id specifier, NSString **outName, OOTextureFlags *outOptions, float *outAnisotropy, float *outLODBias, BOOL ignoreExtract)
Definition OOTexture.m:648
NSString * OOTextureCacheKeyForSpecifier(id specifier)
Definition OOTexture.m:875
NSString * rgbaDescription()
Definition OOColor.m:477
OOColor * colorWithRGBAComponents:(OORGBAComponents components)
Definition OOColor.m:119
OORGBAComponents rgbaComponents()
Definition OOColor.m:354
id initWithEmissionMapSpec:emissionColor:diffuseMap:diffuseColor:illuminationMapSpec:illuminationColor:isCombinedMap:optionsSpecifier:(NSDictionary *emissionMapSpec, [emissionColor] OOColor *emissionColor, [diffuseMap] OOTexture *diffuseMap, [diffuseColor] OOColor *diffuseColor, [illuminationMapSpec] NSDictionary *illuminationMapSpec, [illuminationColor] OOColor *illuminationColor, [isCombinedMap] BOOL isCombinedMap, [optionsSpecifier] NSDictionary *spec)
OOTextureDataFormat _format
id loaderWithTextureSpecifier:extraOptions:folder:(id specifier,[extraOptions] uint32_t extraOptions,[folder] NSString *folder)
BOOL getResult:format:originalWidth:originalHeight:(OOPixMap *result,[format] OOTextureDataFormat *outFormat,[originalWidth] uint32_t *outWidth,[originalHeight] uint32_t *outHeight)
OOPixMap copyPixMapRepresentation()
Definition OOTexture.m:309
OOPixMapDimension height
Definition OOPixMap.h:45
void * pixels
Definition OOPixMap.h:44
OOPixMapDimension width
Definition OOPixMap.h:45