Oolite
Loading...
Searching...
No Matches
OODefaultShaderSynthesizer Class Reference
Inheritance diagram for OODefaultShaderSynthesizer:
Collaboration diagram for OODefaultShaderSynthesizer:

Instance Methods

(id) - initWithMaterialConfiguration:materialKey:entityName:
(BOOL) - run
(NSString *) - vertexShader
(NSString *) - fragmentShader
(NSArray *) - textureSpecifications
(NSDictionary *) - uniformSpecifications
(NSString *) - materialKey
(NSString *) - entityName
(void) - createTemporaries
(void) - destroyTemporaries
(void) - composeVertexShader
(void) - composeFragmentShader
(void) - appendVariable:ofType:withPrefix:to:
(void) - addAttribute:ofType:
(void) - addVarying:ofType:
(void) - addVertexUniform:ofType:
(void) - addFragmentUniform:ofType:
(NSString *) - defineBindingUniform:ofType:
(NSString *) - readRGBForTextureSpec:mapName:
(NSString *) - readOneChannelForTextureSpec:mapName:
(NSUInteger) - textureIDForSpec:
(void) - setUpOneTexture:
(void) - getSampleName:andSwizzleOp:forTextureSpec:
(void) - writeTextureCoordRead
(void) - writeDiffuseColorTermIfNeeded
(void) - writeDiffuseColorTerm
(void) - writeDiffuseLighting
(void) - writeLightVector
(void) - writeEyeVector
(void) - writeVertexTangentBasis
(void) - writeNormalIfNeeded
(void) - writeNormal
(void) - writeSpecularLighting
(void) - writeLightMaps
(void) - writeVertexPosition
(void) - writeTotalColor
(void) - writeFinalColorComposite
(void) - performStage:
(void) - dealloc [implementation]
(NSUInteger) - assignIDForTexture: [implementation]

Class Methods

(static void) + AppendIfNotEmpty [implementation]
(static NSString *) + GetExtractMode [implementation]
(static NSString *) + KeyFromTextureParameters [implementation]
(static NSString *) + KeyFromTextureSpec [implementation]

Private Attributes

NSDictionary * _configuration
NSString * _materialKey
NSString * _entityName
NSString * _vertexShader
NSString * _fragmentShader
NSMutableArray * _textures
NSMutableDictionary * _uniforms
NSMutableString * _attributes
NSMutableString * _varyings
NSMutableString * _vertexUniforms
NSMutableString * _fragmentUniforms
NSMutableString * _vertexHelpers
NSMutableString * _fragmentHelpers
NSMutableString * _vertexBody
NSMutableString * _fragmentPreTextures
NSMutableString * _fragmentTextureLookups
NSMutableString * _fragmentBody
NSMutableDictionary * _texturesByName
NSMutableDictionary * _textureIDs
NSHashTable * _sampledTextures
NSMutableDictionary * _uniformBindingNames
NSUInteger _usesNormalMap: 1
NSUInteger _usesDiffuseTerm: 1
NSUInteger _constZNormal: 1
NSUInteger _haveDiffuseLight: 1
NSUInteger _completed_writeFinalColorComposite: 1
NSUInteger _completed_writeDiffuseColorTerm: 1
NSUInteger _completed_writeSpecularLighting: 1
NSUInteger _completed_writeLightMaps: 1
NSUInteger _completed_writeDiffuseLighting: 1
NSUInteger _completed_writeDiffuseColorTermIfNeeded: 1
NSUInteger _completed_writeVertexPosition: 1
NSUInteger _completed_writeNormalIfNeeded: 1
NSUInteger _completed_writeLightVector: 1
NSUInteger _completed_writeEyeVector: 1
NSUInteger _completed_writeTotalColor: 1
NSUInteger _completed_writeTextureCoordRead: 1
NSUInteger _completed_writeVertexTangentBasis: 1
NSHashTable * _stagesInProgress

Detailed Description

Definition at line 54 of file OODefaultShaderSynthesizer.m.

Method Documentation

◆ addAttribute:ofType:

- (void) addAttribute: (NSString*) name
ofType: (NSString*) type 

Definition at line 431 of file OODefaultShaderSynthesizer.m.

431 :(NSString*)name ofType:(NSString*)type
432{
433 [self appendVariable:name ofType:type withPrefix:@"attribute" to:_attributes];
434}

References appendVariable:ofType:withPrefix:to:.

Referenced by writeVertexTangentBasis.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addFragmentUniform:ofType:

- (void) addFragmentUniform: (NSString*) name
ofType: (NSString*) type 

Definition at line 446 of file OODefaultShaderSynthesizer.m.

446 :(NSString*)name ofType:(NSString*)type
447{
448 [self appendVariable:name ofType:type withPrefix:@"uniform" to:_fragmentUniforms];
449}

References appendVariable:ofType:withPrefix:to:.

Referenced by assignIDForTexture:, and defineBindingUniform:ofType:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addVarying:ofType:

- (void) addVarying: (NSString*) name
ofType: (NSString*) type 

Definition at line 436 of file OODefaultShaderSynthesizer.m.

436 :(NSString*)name ofType:(NSString*)type
437{
438 [self appendVariable:name ofType:type withPrefix:@"varying" to:_varyings];
439}

References appendVariable:ofType:withPrefix:to:.

Referenced by writeEyeVector, writeLightVector, and writeTextureCoordRead.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addVertexUniform:ofType:

- (void) addVertexUniform: (NSString*) name
ofType: (NSString*) type 

Definition at line 441 of file OODefaultShaderSynthesizer.m.

441 :(NSString*)name ofType:(NSString*)type
442{
443 [self appendVariable:name ofType:type withPrefix:@"uniform" to:_vertexUniforms];
444}

References appendVariable:ofType:withPrefix:to:.

Here is the call graph for this function:

◆ AppendIfNotEmpty

+ (void) AppendIfNotEmpty (NSMutableString *) buffer
(NSString *) segment
(NSString *) name 
implementation

Definition at line 391 of file OODefaultShaderSynthesizer.m.

392{
393 if ([segment length] > 0) {
394 if ([buffer length] > 0)
395 [buffer appendString:@"\n\n"];
396 if ([name length] > 0)
397 [buffer appendFormat:@"// %@\n", name];
398 [buffer appendString:segment];
399 }
400}

Referenced by composeFragmentShader, and composeVertexShader.

Here is the caller graph for this function:

◆ appendVariable:ofType:withPrefix:to:

- (void) appendVariable: (NSString*) name
ofType: (NSString*) type
withPrefix: (NSString*) prefix
to: (NSMutableString*) buffer 

Definition at line 424 of file OODefaultShaderSynthesizer.m.

424 :(NSString*)name ofType:(NSString*)type withPrefix:(NSString*)prefix to:(NSMutableString*)buffer
425{
426 NSUInteger typeDeclLength = [prefix length] + [type length] + 1;
427 NSUInteger padding = (typeDeclLength < 20) ? (23 - typeDeclLength) / 4 : 1;
428 [buffer appendFormat:@"%@ %@%@%@;\n", prefix, type, OOTabString(padding), name];
429}

Referenced by addAttribute:ofType:, addFragmentUniform:ofType:, addVarying:ofType:, and addVertexUniform:ofType:.

Here is the caller graph for this function:

◆ assignIDForTexture:

- (NSUInteger) assignIDForTexture: (NSDictionary*) spec
implementation

Definition at line 562 of file OODefaultShaderSynthesizer.m.

562 :(NSDictionary*)spec
563{
564 NSParameterAssert(spec != nil);
565
566 // extract_channel doesn't affect uniqueness, and we don't want OOTexture to do actual extraction.
567 if ([spec objectForKey:kOOTextureSpecifierSwizzleKey] != nil) {
568 spec = [spec dictionaryByRemovingObjectForKey:kOOTextureSpecifierSwizzleKey];
569 }
570
571 NSString* texName = nil;
572 OOTextureFlags texOptions;
573 float anisotropy, lodBias;
574 if (!OOInterpretTextureSpecifier(spec, &texName, &texOptions, &anisotropy, &lodBias, YES)) {
575 // OOInterpretTextureSpecifier() will have logged something.
576 [NSException raise:NSGenericException format:@"Invalid texture specifier"];
577 }
578
579 if (texOptions & kOOTextureAllowCubeMap) {
580 // cube_map = true; fail regardless of whether actual texture qualifies.
581 OOLogERR(@"material.synthesis.error.cubeMap", @"The material \"%@\" of \"%@\" specifies a cube map texture, but doesn't have custom shaders. Cube map textures are not supported with the default shaders.", [self materialKey], [self entityName]);
582 [NSException raise:NSGenericException format:@"Invalid material"];
583 }
584
585 NSString* key = KeyFromTextureParameters(texName, texOptions, anisotropy, lodBias);
586 NSUInteger texID;
587 NSObject* existing = [_texturesByName objectForKey:key];
588 if (existing == nil) {
589 texID = [_texturesByName count];
590 NSNumber* texIDObj = [NSNumber numberWithUnsignedInteger:texID];
591 NSString* texUniform = [NSString stringWithFormat:@"uTexture%zu", texID];
592
593#ifndef NDEBUG
594 BOOL useInternalFormat = NO;
595#else
596 BOOL useInternalFormat = YES;
597#endif
598
599 [_textures addObject:OOMakeTextureSpecifier(texName, texOptions, anisotropy, lodBias, useInternalFormat)];
600 [_texturesByName setObject:spec forKey:key];
601 [_textureIDs setObject:texIDObj forKey:key];
602 [_uniforms setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"texture", @"type", texIDObj, @"value", nil]
603 forKey:texUniform];
604
605 [self addFragmentUniform:texUniform ofType:@"sampler2D"];
606 } else {
607 texID = [_textureIDs oo_unsignedIntegerForKey:texName];
608 }
609
610 return texID;
611}
#define OOLogERR(class, format,...)
Definition OOLogging.h:125
return nil
@ kOOTextureAllowCubeMap
Definition OOTexture.h:61
uint32_t OOTextureFlags
Definition OOTexture.h:95
BOOL OOInterpretTextureSpecifier(id specifier, NSString **outName, OOTextureFlags *outOptions, float *outAnisotropy, float *outLODBias, BOOL ignoreExtract)
Definition OOTexture.m:648
NSString *const kOOTextureSpecifierSwizzleKey
Definition OOTexture.m:45
static NSString * KeyFromTextureParameters(NSString *name, OOTextureFlags options, float anisotropy, float lodBias)

References addFragmentUniform:ofType:, entityName, KeyFromTextureParameters, kOOTextureAllowCubeMap, kOOTextureSpecifierSwizzleKey, materialKey, nil, OOInterpretTextureSpecifier(), and OOLogERR.

Referenced by setUpOneTexture:, and writeTextureCoordRead.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ composeFragmentShader

- (void) composeFragmentShader

Definition at line 505 of file OODefaultShaderSynthesizer.m.

506{
507 while ([_fragmentBody hasSuffix:@"\t\n"]) {
508 [_fragmentBody deleteCharactersInRange:(NSRange) { [_fragmentBody length] - 2, 2 }];
509 }
510
511 NSMutableString* fragmentShader = [NSMutableString string];
514 AppendIfNotEmpty(fragmentShader, _fragmentHelpers, @"Helper functions");
516 if ([_fragmentTextureLookups length] > 0) {
517 [fragmentShader appendString:@"\t\n\t// Texture lookups\n"];
518 [fragmentShader appendString:_fragmentTextureLookups];
519 }
520 [fragmentShader appendString:@"\t\n"];
521 [fragmentShader appendString:_fragmentBody];
522 [fragmentShader appendString:@"}"];
523
524#ifndef NDEBUG
525 _fragmentShader = [fragmentShader copy];
526#else
527 _fragmentShader = [fragmentShader retain];
528#endif
529}
static void AppendIfNotEmpty(NSMutableString *buffer, NSString *segment, NSString *name)

References _fragmentBody, _fragmentHelpers, _fragmentPreTextures, _fragmentShader, _fragmentTextureLookups, _fragmentUniforms, _varyings, AppendIfNotEmpty, composeFragmentShader, fragmentShader, and nil.

Referenced by composeFragmentShader, and run.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ composeVertexShader

- (void) composeVertexShader

Definition at line 484 of file OODefaultShaderSynthesizer.m.

485{
486 while ([_vertexBody hasSuffix:@"\t\n"]) {
487 [_vertexBody deleteCharactersInRange:(NSRange) { [_vertexBody length] - 2, 2 }];
488 }
489 [_vertexBody appendString:@"}"];
490
491 NSMutableString* vertexShader = [NSMutableString string];
495 AppendIfNotEmpty(vertexShader, _vertexHelpers, @"Helper functions");
497
498#ifndef NDEBUG
499 _vertexShader = [vertexShader copy];
500#else
501 _vertexShader = [vertexShader retain];
502#endif
503}

References _attributes, _varyings, _vertexBody, _vertexHelpers, _vertexShader, _vertexUniforms, AppendIfNotEmpty, composeVertexShader, nil, and vertexShader.

Referenced by composeVertexShader, and run.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ createTemporaries

- (void) createTemporaries

Definition at line 700 of file OODefaultShaderSynthesizer.m.

701{
702 _attributes = [[NSMutableString alloc] init];
703 _varyings = [[NSMutableString alloc] init];
704 _vertexUniforms = [[NSMutableString alloc] init];
705 _fragmentUniforms = [[NSMutableString alloc] init];
706 _vertexHelpers = [[NSMutableString alloc] init];
707 _fragmentHelpers = [[NSMutableString alloc] init];
708 _vertexBody = [[NSMutableString alloc] init];
709 _fragmentPreTextures = [[NSMutableString alloc] init];
710 _fragmentTextureLookups = [[NSMutableString alloc] init];
711 _fragmentBody = [[NSMutableString alloc] init];
712
713 _textures = [[NSMutableArray alloc] init];
714 _texturesByName = [[NSMutableDictionary alloc] init];
715 _textureIDs = [[NSMutableDictionary alloc] init];
716 _sampledTextures = NSCreateHashTable(NSIntegerHashCallBacks, 0);
717
718 _uniformBindingNames = [[NSMutableDictionary alloc] init];
719
720#ifndef NDEBUG
721 _stagesInProgress = NSCreateHashTable(NSNonOwnedPointerHashCallBacks, 0);
722#endif
723}
NSMutableDictionary * _uniformBindingNames

References _attributes, _fragmentBody, _fragmentHelpers, _fragmentPreTextures, _fragmentTextureLookups, _fragmentUniforms, _sampledTextures, _stagesInProgress, _textureIDs, _textures, _texturesByName, _uniformBindingNames, _varyings, _vertexBody, _vertexHelpers, _vertexUniforms, and createTemporaries.

Referenced by createTemporaries, and run.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dealloc

- (void) dealloc
implementation

Definition at line 314 of file OODefaultShaderSynthesizer.m.

315{
316 [self destroyTemporaries];
323
324 [super dealloc];
325}
#define DESTROY(x)

References _configuration, _entityName, _fragmentShader, _materialKey, _textures, _vertexShader, dealloc, DESTROY, and destroyTemporaries.

Referenced by dealloc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ defineBindingUniform:ofType:

- (NSString *) defineBindingUniform: (NSDictionary*) binding
ofType: (NSString*) type 

Definition at line 451 of file OODefaultShaderSynthesizer.m.

451 :(NSDictionary*)binding ofType:(NSString*)type
452{
453 NSString* name = [binding oo_stringForKey:@"binding"];
454 NSParameterAssert([name length] > 0);
455
456 NSMutableDictionary* bindingSpec = [[binding mutableCopy] autorelease];
457 if ([bindingSpec oo_stringForKey:@"type"] == nil)
458 [bindingSpec setObject:@"binding" forKey:@"type"];
459
460 // Use existing uniform if one is defined.
461 NSString* uniformName = [_uniformBindingNames objectForKey:bindingSpec];
462 if (uniformName != nil)
463 return uniformName;
464
465 // Capitalize first char of name, and prepend u.
466 unichar firstChar = toupper([name characterAtIndex:0]);
467 NSString* baseName = [NSString stringWithFormat:@"u%C%@", firstChar, [name substringFromIndex:1]];
468
469 // Ensure name is unique.
470 name = baseName;
471 unsigned idx = 1;
472 while ([_uniforms objectForKey:name] != nil) {
473 name = [NSString stringWithFormat:@"%@%u", baseName, ++idx];
474 }
475
476 [self addFragmentUniform:name ofType:type];
477
478 [_uniforms setObject:bindingSpec forKey:name];
479 [_uniformBindingNames setObject:name forKey:bindingSpec];
480
481 return name;
482}

References _uniforms, addFragmentUniform:ofType:, and nil.

Referenced by writeLightMaps.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ destroyTemporaries

- (void) destroyTemporaries

Definition at line 725 of file OODefaultShaderSynthesizer.m.

726{
737
740 if (_sampledTextures != NULL) {
741 NSFreeHashTable(_sampledTextures);
742 _sampledTextures = NULL;
743 }
744
746
747#ifndef NDEBUG
748 if (_stagesInProgress != NULL) {
749 NSFreeHashTable(_stagesInProgress);
750 _stagesInProgress = NULL;
751 }
752#endif
753}

References _attributes, _fragmentBody, _fragmentHelpers, _fragmentPreTextures, _fragmentTextureLookups, _fragmentUniforms, _sampledTextures, _stagesInProgress, _textureIDs, _texturesByName, _uniformBindingNames, _varyings, _vertexBody, _vertexHelpers, _vertexUniforms, DESTROY, and destroyTemporaries.

Referenced by dealloc, destroyTemporaries, and run.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ entityName

- (NSString *) entityName

Definition at line 384 of file OODefaultShaderSynthesizer.m.

385{
386 return _entityName;
387}

References _entityName, and entityName.

Referenced by assignIDForTexture:, entityName, readOneChannelForTextureSpec:mapName:, readRGBForTextureSpec:mapName:, writeNormalIfNeeded, and writeTextureCoordRead.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fragmentShader

- (NSString *) fragmentShader

Definition at line 332 of file OODefaultShaderSynthesizer.m.

333{
334 return _fragmentShader;
335}

References _fragmentShader, and fragmentShader.

Referenced by composeFragmentShader, fragmentShader, and OOSynthesizeMaterialShader().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetExtractMode

+ (NSString *) GetExtractMode (NSDictionary *) textureSpecifier
implementation

Definition at line 402 of file OODefaultShaderSynthesizer.m.

403{
404 NSString* result = nil;
405
406 NSString* rawMode = [textureSpecifier oo_stringForKey:kOOTextureSpecifierSwizzleKey];
407 if (rawMode != nil) {
408 NSUInteger length = [rawMode length];
409 if (1 <= length && length <= 4) {
410 static NSCharacterSet* nonRGBACharset = nil;
411 if (nonRGBACharset == nil) {
412 nonRGBACharset = [[[NSCharacterSet characterSetWithCharactersInString:@"rgba"] invertedSet] retain];
413 }
414
415 if ([rawMode rangeOfCharacterFromSet:nonRGBACharset].location == NSNotFound) {
416 result = rawMode;
417 }
418 }
419 }
420
421 return result;
422}

References nil.

Referenced by getSampleName:andSwizzleOp:forTextureSpec:, and writeTextureCoordRead.

Here is the caller graph for this function:

◆ getSampleName:andSwizzleOp:forTextureSpec:

- (void) getSampleName: (NSString**) outSampleName
andSwizzleOp: (NSString**) outSwizzleOp
forTextureSpec: (NSDictionary*) textureSpec 

Definition at line 632 of file OODefaultShaderSynthesizer.m.

632 :(NSString**)outSampleName andSwizzleOp:(NSString**)outSwizzleOp forTextureSpec:(NSDictionary*)textureSpec
633{
634 NSParameterAssert(outSampleName != NULL && outSwizzleOp != NULL && textureSpec != nil);
635
636 [self setUpOneTexture:textureSpec];
637 NSUInteger texID = [self textureIDForSpec:textureSpec];
638
639 *outSampleName = [NSString stringWithFormat:@"tex%zuSample", texID];
640 *outSwizzleOp = GetExtractMode(textureSpec);
641}
static NSString * GetExtractMode(NSDictionary *textureSpecifier)

References GetExtractMode, nil, setUpOneTexture:, and textureIDForSpec:.

Referenced by readOneChannelForTextureSpec:mapName:, readRGBForTextureSpec:mapName:, and writeNormalIfNeeded.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initWithMaterialConfiguration:materialKey:entityName:

- (id) initWithMaterialConfiguration: (NSDictionary*) configuration
materialKey: (NSString*) materialKey
entityName: (NSString*) name 

Definition at line 301 of file OODefaultShaderSynthesizer.m.

301 :(NSDictionary*)configuration
302 materialKey:(NSString*)materialKey
303 entityName:(NSString*)name
304{
305 if ((self = [super init])) {
306 _configuration = [CanonicalizeMaterialSpecifier(configuration, materialKey) retain];
307 _materialKey = [materialKey copy];
308 _entityName = [_entityName copy];
309 }
310
311 return self;
312}

References _configuration, _entityName, and _materialKey.

◆ KeyFromTextureParameters

+ (NSString *) KeyFromTextureParameters (NSString *) name
(OOTextureFlags) options
(float) anisotropy
(float) lodBias 
implementation

Definition at line 537 of file OODefaultShaderSynthesizer.m.

538{
539#ifndef NDEBUG
540 options = OOApplyTextureOptionDefaults(options);
541#endif
542
543 // Extraction modes are ignored in synthesized shaders, since we use swizzling instead.
544 options &= ~kOOTextureExtractChannelMask;
545
546 return [NSString stringWithFormat:@"%@:%X:%g:%g", name, options, anisotropy, lodBias];
547}
OOTextureFlags OOApplyTextureOptionDefaults(OOTextureFlags options)
Definition OOTexture.m:819

References kOOTextureExtractChannelMask, and OOApplyTextureOptionDefaults().

Referenced by assignIDForTexture:, and KeyFromTextureSpec.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ KeyFromTextureSpec

+ (NSString *) KeyFromTextureSpec (NSDictionary *) spec
implementation

Definition at line 549 of file OODefaultShaderSynthesizer.m.

550{
551 NSString* texName = nil;
552 OOTextureFlags texOptions;
553 float anisotropy, lodBias;
554 if (!OOInterpretTextureSpecifier(spec, &texName, &texOptions, &anisotropy, &lodBias, YES)) {
555 // OOInterpretTextureSpecifier() will have logged something.
556 [NSException raise:NSGenericException format:@"Invalid texture specifier"];
557 }
558
559 return KeyFromTextureParameters(texName, texOptions, anisotropy, lodBias);
560}

References KeyFromTextureParameters, nil, and OOInterpretTextureSpecifier().

Here is the call graph for this function:

◆ materialKey

- (NSString *) materialKey

Definition at line 379 of file OODefaultShaderSynthesizer.m.

380{
381 return _materialKey;
382}

References _materialKey, and materialKey.

Referenced by assignIDForTexture:, CanonicalizeMaterialSpecifier(), materialKey, readOneChannelForTextureSpec:mapName:, readRGBForTextureSpec:mapName:, writeDiffuseColorTermIfNeeded, writeNormalIfNeeded, and writeTextureCoordRead.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ performStage:

- (void) performStage: (SEL) stage

Definition at line 684 of file OODefaultShaderSynthesizer.m.

684 :(SEL)stage
685{
686 // Ensure that we aren’t recursing.
687 if (NSHashGet(_stagesInProgress, stage) != NULL) {
688 OOLogERR(@"material.synthesis.error.recursion", @"Shader synthesis recursion for stage %@.", NSStringFromSelector(stage));
689 [NSException raise:NSInternalInconsistencyException format:@"stage recursion"];
690 }
691
692 NSHashInsertKnownAbsent(_stagesInProgress, stage);
693
694 [self performSelector:stage];
695
696 NSHashRemove(_stagesInProgress, stage);
697}

References _stagesInProgress, and OOLogERR.

◆ readOneChannelForTextureSpec:mapName:

- (NSString *) readOneChannelForTextureSpec: (NSDictionary*) textureSpec
mapName: (NSString*) mapName 

Definition at line 664 of file OODefaultShaderSynthesizer.m.

664 :(NSDictionary*)textureSpec mapName:(NSString*)mapName
665{
666 NSString *sample, *swizzle;
667 [self getSampleName:&sample andSwizzleOp:&swizzle forTextureSpec:textureSpec];
668
669 if (swizzle == nil) {
670 return [sample stringByAppendingString:@".r"];
671 }
672
673 NSUInteger channelCount = [swizzle length];
674
675 if (channelCount == 1) {
676 return [NSString stringWithFormat:@"%@.%@", sample, swizzle];
677 }
678
679 OOLogWARN(@"material.synthesis.warning.extractionMismatch", @"The %@ map for material \"%@\" of \"%@\" specifies %zu channels to extract, but only %@ may be used.", mapName, [self materialKey], [self entityName], channelCount, @"1");
680 return nil;
681}
#define OOLogWARN(class, format,...)
Definition OOLogging.h:126

References entityName, getSampleName:andSwizzleOp:forTextureSpec:, materialKey, nil, and OOLogWARN.

Referenced by writeSpecularLighting.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readRGBForTextureSpec:mapName:

- (NSString *) readRGBForTextureSpec: (NSDictionary*) textureSpec
mapName: (NSString*) mapName 

Definition at line 643 of file OODefaultShaderSynthesizer.m.

643 :(NSDictionary*)textureSpec mapName:(NSString*)mapName
644{
645 NSString *sample, *swizzle;
646 [self getSampleName:&sample andSwizzleOp:&swizzle forTextureSpec:textureSpec];
647
648 if (swizzle == nil) {
649 return [sample stringByAppendingString:@".rgb"];
650 }
651
652 NSUInteger channelCount = [swizzle length];
653
654 if (channelCount == 1) {
655 return [NSString stringWithFormat:@"%@.%@%@%@", sample, swizzle, swizzle, swizzle];
656 } else if (channelCount == 3) {
657 return [NSString stringWithFormat:@"%@.%@", sample, swizzle];
658 }
659
660 OOLogWARN(@"material.synthesis.warning.extractionMismatch", @"The %@ map for material \"%@\" of \"%@\" specifies %zu channels to extract, but only %@ may be used.", mapName, [self materialKey], [self entityName], channelCount, @"1 or 3");
661 return nil;
662}

References entityName, getSampleName:andSwizzleOp:forTextureSpec:, materialKey, nil, and OOLogWARN.

Referenced by writeDiffuseColorTermIfNeeded, writeLightMaps, and writeSpecularLighting.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run

- (BOOL) run

Definition at line 355 of file OODefaultShaderSynthesizer.m.

356{
357 [self createTemporaries];
358 _uniforms = [[NSMutableDictionary alloc] init];
359 [_vertexBody appendString:@"void main(void)\n{\n"];
360 [_fragmentPreTextures appendString:@"void main(void)\n{\n"];
361
362 @try {
364
365 [self composeVertexShader];
366 [self composeFragmentShader];
367 }
368 @catch (NSException* exception) {
369 // Error should have been reported already.
370 return NO;
371 }
372 @finally {
373 [self destroyTemporaries];
374 }
375
376 return YES;
377}
#define REQUIRE_STAGE(NAME)

References _uniforms, composeFragmentShader, composeVertexShader, createTemporaries, destroyTemporaries, REQUIRE_STAGE, run, and writeFinalColorComposite.

Referenced by OOSynthesizeMaterialShader(), and run.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setUpOneTexture:

- (void) setUpOneTexture: (NSDictionary*) textureSpec

Definition at line 618 of file OODefaultShaderSynthesizer.m.

618 :(NSDictionary*)textureSpec
619{
620 if (textureSpec == nil)
621 return;
622
624
625 NSUInteger texID = [self assignIDForTexture:textureSpec];
626 if ((NSUInteger)NSHashGet(_sampledTextures, (const void*)(texID + 1)) == 0) {
627 NSHashInsertKnownAbsent(_sampledTextures, (const void*)(texID + 1));
628 [_fragmentTextureLookups appendFormat:@"\tvec4 tex%zuSample = texture2D(uTexture%zu, texCoords); // %@\n", texID, texID, [textureSpec oo_stringForKey:kOOTextureSpecifierNameKey]];
629 }
630}

References _sampledTextures, assignIDForTexture:, nil, REQUIRE_STAGE, and writeTextureCoordRead.

Referenced by getSampleName:andSwizzleOp:forTextureSpec:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ textureIDForSpec:

- (NSUInteger) textureIDForSpec: (NSDictionary*) textureSpec

Definition at line 613 of file OODefaultShaderSynthesizer.m.

613 :(NSDictionary*)textureSpec
614{
615 return [_textureIDs oo_unsignedIntegerForKey:KeyFromTextureSpec(textureSpec)];
616}

Referenced by getSampleName:andSwizzleOp:forTextureSpec:.

Here is the caller graph for this function:

◆ textureSpecifications

- (NSArray *) textureSpecifications

Definition at line 337 of file OODefaultShaderSynthesizer.m.

338{
339#ifndef NDEBUG
340 return [NSArray arrayWithArray:_textures];
341#else
342 return _textures;
343#endif
344}

References _textures, and textureSpecifications.

Referenced by OOSynthesizeMaterialShader(), and textureSpecifications.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uniformSpecifications

- (NSDictionary *) uniformSpecifications

Definition at line 346 of file OODefaultShaderSynthesizer.m.

347{
348#ifndef NDEBUG
349 return [NSDictionary dictionaryWithDictionary:_uniforms];
350#else
351 return _uniforms;
352#endif
353}

References _uniforms, and uniformSpecifications.

Referenced by OOSynthesizeMaterialShader(), and uniformSpecifications.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ vertexShader

- (NSString *) vertexShader

Definition at line 327 of file OODefaultShaderSynthesizer.m.

328{
329 return _vertexShader;
330}

References _vertexShader, and vertexShader.

Referenced by composeVertexShader, OOSynthesizeMaterialShader(), and vertexShader.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeDiffuseColorTerm

- (void) writeDiffuseColorTerm

Definition at line 843 of file OODefaultShaderSynthesizer.m.

844{
846
847 if (!_usesDiffuseTerm) {
848 [_fragmentBody appendString:@"\tconst vec3 diffuseColor = vec3(0.0); // Diffuse colour is black.\n\t\n"];
849 }
850}

References _usesDiffuseTerm, REQUIRE_STAGE, writeDiffuseColorTerm, and writeDiffuseColorTermIfNeeded.

Referenced by writeDiffuseColorTerm, writeLightMaps, and writeSpecularLighting.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeDiffuseColorTermIfNeeded

- (void) writeDiffuseColorTermIfNeeded

Definition at line 806 of file OODefaultShaderSynthesizer.m.

807{
808 NSDictionary* diffuseMap = [_configuration oo_diffuseMapSpecifierWithDefaultName:[self materialKey]];
809 OOColor* diffuseColor = [_configuration oo_diffuseColor] ?: [OOColor whiteColor];
810
811 if ([diffuseColor isBlack])
812 return;
813 _usesDiffuseTerm = YES;
814
815 BOOL haveDiffuseColor = NO;
816 if (diffuseMap != nil) {
817 NSString* readInstr = [self readRGBForTextureSpec:diffuseMap mapName:@"diffuse"];
818 if (EXPECT_NOT(readInstr == nil)) {
819 [_fragmentBody appendString:@"\t// INVALID EXTRACTION KEY\n\t\n"];
820 } else {
821 [_fragmentBody appendFormat:@"\tvec3 diffuseColor = %@;\n", readInstr];
822 haveDiffuseColor = YES;
823 }
824 }
825
826 if (!haveDiffuseColor || ![diffuseColor isWhite]) {
827 float rgba[4];
828 [diffuseColor getRed:&rgba[0] green:&rgba[1] blue:&rgba[2] alpha:&rgba[3]];
829 NSString* format = nil;
830 if (haveDiffuseColor) {
831 format = @"\tdiffuseColor *= vec3(%@, %@, %@);\n";
832 } else {
833 format = @"\tconst vec3 diffuseColor = vec3(%@, %@, %@);\n";
834 haveDiffuseColor = YES;
835 }
836 [_fragmentBody appendFormat:format, FormatFloat(rgba[0]), FormatFloat(rgba[1]), FormatFloat(rgba[2])];
837 }
838
839 (void)haveDiffuseColor;
840 [_fragmentBody appendString:@"\t\n"];
841}
#define EXPECT_NOT(x)
void getRed:green:blue:alpha:(float *red,[green] float *green,[blue] float *blue,[alpha] float *alpha)
Definition OOColor.m:344
OOColor * whiteColor()
Definition OOColor.m:249

References _usesDiffuseTerm, EXPECT_NOT, OOColor::getRed:green:blue:alpha:, materialKey, nil, readRGBForTextureSpec:mapName:, OOColor::whiteColor, and writeDiffuseColorTermIfNeeded.

Referenced by writeDiffuseColorTerm, writeDiffuseColorTermIfNeeded, and writeDiffuseLighting.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeDiffuseLighting

- (void) writeDiffuseLighting

Definition at line 852 of file OODefaultShaderSynthesizer.m.

853{
855 if (!_usesDiffuseTerm)
856 return;
857
862
863 // FIXME: currently uncoloured diffuse and ambient lighting.
864 NSString* normalDotLight = _constZNormal ? @"lightVector.z" : @"dot(normal, lightVector)";
865
866 [_fragmentBody appendFormat:
867 @"\t// Diffuse (Lambertian) and ambient lighting\n"
868 "\tvec3 diffuseLight = (gl_LightSource[1].diffuse * max(0.0, %@) + gl_LightModel.ambient).rgb;\n\t\n",
869 normalDotLight];
870
871 _haveDiffuseLight = YES;
872}

References _constZNormal, _haveDiffuseLight, _usesDiffuseTerm, REQUIRE_STAGE, writeDiffuseColorTermIfNeeded, writeDiffuseLighting, writeLightVector, writeNormalIfNeeded, writeTotalColor, and writeVertexPosition.

Referenced by writeDiffuseLighting, writeFinalColorComposite, and writeLightMaps.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeEyeVector

- (void) writeEyeVector

Definition at line 887 of file OODefaultShaderSynthesizer.m.

888{
891
892 [self addVarying:@"vEyeVector" ofType:@"vec3"];
893
894 [_vertexBody appendString:@"\tvEyeVector = position.xyz * TBN;\n\t\n"];
895 [_fragmentPreTextures appendString:@"\tvec3 eyeVector = normalize(vEyeVector);\n\t\n"];
896}

References addVarying:ofType:, REQUIRE_STAGE, writeEyeVector, writeVertexPosition, and writeVertexTangentBasis.

Referenced by writeEyeVector, writeSpecularLighting, and writeTextureCoordRead.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeFinalColorComposite

- (void) writeFinalColorComposite

Definition at line 1175 of file OODefaultShaderSynthesizer.m.

1176{
1177 REQUIRE_STAGE(writeTotalColor); // Needed even if none of the following stages does anything.
1181
1182 if (_haveDiffuseLight) {
1183 [_fragmentBody appendString:@"\ttotalColor += diffuseColor * diffuseLight;\n"];
1184 }
1185
1186 [_fragmentBody appendString:@"\tgl_FragColor = vec4(totalColor, 1.0);\n\t\n"];
1187}

References _haveDiffuseLight, REQUIRE_STAGE, writeDiffuseLighting, writeFinalColorComposite, writeLightMaps, writeSpecularLighting, and writeTotalColor.

Referenced by run, and writeFinalColorComposite.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeLightMaps

- (void) writeLightMaps

Definition at line 1059 of file OODefaultShaderSynthesizer.m.

1060{
1061 NSArray* lightMaps = [_configuration oo_arrayForKey:kOOMaterialLightMapsName];
1062 NSUInteger idx, count = [lightMaps count];
1063 if (count == 0)
1064 return;
1065
1067
1068 // Check if we need the diffuse colour term.
1069 for (idx = 0; idx < count; idx++) {
1070 NSDictionary* lightMapSpec = [lightMaps oo_dictionaryAtIndex:idx];
1071 if ([lightMapSpec oo_boolForKey:kOOTextureSpecifierIlluminationModeKey]) {
1074 break;
1075 }
1076 }
1077
1078 [_fragmentBody appendString:@"\tvec3 lightMapColor;\n"];
1079
1080 for (idx = 0; idx < count; idx++) {
1081 NSDictionary* lightMapSpec = [lightMaps oo_dictionaryAtIndex:idx];
1082 NSDictionary* textureSpec = OOTextureSpecFromObject(lightMapSpec, nil);
1083 NSArray* color = [lightMapSpec oo_arrayForKey:kOOTextureSpecifierModulateColorKey];
1084 float rgba[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
1085 BOOL isIllumination = [lightMapSpec oo_boolForKey:kOOTextureSpecifierIlluminationModeKey];
1086
1087 if (EXPECT_NOT(color == nil && textureSpec == nil)) {
1088 [_fragmentBody appendString:@"\t// Light map with neither colour nor texture has no effect.\n\t\n"];
1089 continue;
1090 }
1091
1092 if (color != nil) {
1093 NSUInteger idx, count = [color count];
1094 if (count > 4)
1095 count = 4;
1096 for (idx = 0; idx < count; idx++) {
1097 rgba[idx] = [color oo_doubleAtIndex:idx];
1098 }
1099 rgba[0] *= rgba[3];
1100 rgba[1] *= rgba[3];
1101 rgba[2] *= rgba[3];
1102 }
1103
1104 if (EXPECT_NOT((rgba[0] == 0.0f && rgba[1] == 0.0f && rgba[2] == 0.0f) || (!_usesDiffuseTerm && isIllumination))) {
1105 [_fragmentBody appendString:@"\t// Light map tinted black has no effect.\n\t\n"];
1106 continue;
1107 }
1108
1109 if (textureSpec != nil) {
1110 NSString* readInstr = [self readRGBForTextureSpec:textureSpec mapName:@"light"];
1111 if (EXPECT_NOT(readInstr == nil)) {
1112 [_fragmentBody appendString:@"\t// INVALID EXTRACTION KEY\n\n"];
1113 continue;
1114 }
1115
1116 [_fragmentBody appendFormat:@"\tlightMapColor = %@;\n", readInstr];
1117
1118 if (rgba[0] != 1.0f || rgba[1] != 1.0f || rgba[2] != 1.0f) {
1119 [_fragmentBody appendFormat:@"\tlightMapColor *= vec3(%@, %@, %@);\n", FormatFloat(rgba[0]), FormatFloat(rgba[1]), FormatFloat(rgba[2])];
1120 }
1121 } else {
1122 [_fragmentBody appendFormat:@"\tlightMapColor = vec3(%@, %@, %@);\n", FormatFloat(rgba[0]), FormatFloat(rgba[1]), FormatFloat(rgba[2])];
1123 }
1124
1125 NSDictionary* binding = [textureSpec oo_dictionaryForKey:kOOTextureSpecifierBindingKey];
1126 if (binding != nil) {
1127 NSString* bindingName = [binding oo_stringForKey:@"binding"];
1128 NSDictionary* typeDict = [[ResourceManager shaderBindingTypesDictionary] oo_dictionaryForKey:@"player"]; // FIXME: select appropriate binding subset.
1129 NSString* bindingType = [typeDict oo_stringForKey:bindingName];
1130 NSString* glslType = nil;
1131 NSString* swizzle = @"";
1132
1133 if ([bindingType isEqualToString:@"float"]) {
1134 glslType = @"float";
1135 } else if ([bindingType isEqualToString:@"vector"]) {
1136 glslType = @"vec3";
1137 } else if ([bindingType isEqualToString:@"color"]) {
1138 glslType = @"vec4";
1139 swizzle = @".rgb";
1140 }
1141
1142 if (glslType != nil) {
1143 NSString* uniformName = [self defineBindingUniform:binding ofType:bindingType];
1144 [_fragmentBody appendFormat:@"\tlightMapColor *= %@%@;\n", uniformName, swizzle];
1145 } else {
1146 if (bindingType == nil) {
1147 OOLogERR(@"material.binding.error.unknown", @"Cannot bind light map to unknown attribute \"%@\".", bindingName);
1148 } else {
1149 OOLogERR(@"material.binding.error.badType", @"Cannot bind light map to attribute \"%@\" of type %@.", bindingName, bindingType);
1150 }
1151 [_fragmentBody appendString:@"\tlightMapColor = vec3(0.0); // Bad binding, see log.\n"];
1152 }
1153 }
1154
1155 if (!isIllumination) {
1156 [_fragmentBody appendString:@"\ttotalColor += lightMapColor;\n\t\n"];
1157 } else {
1158 [_fragmentBody appendString:@"\tdiffuseLight += lightMapColor;\n\t\n"];
1159 }
1160 }
1161}
unsigned count
NSDictionary * OOTextureSpecFromObject(id object, NSString *defaultName)
Definition OOTexture.m:602
NSString *const kOOTextureSpecifierIlluminationModeKey
Definition OOTexture.m:57
NSDictionary * shaderBindingTypesDictionary()

References _usesDiffuseTerm, count, defineBindingUniform:ofType:, EXPECT_NOT, kOOTextureSpecifierIlluminationModeKey, nil, OOLogERR, OOTextureSpecFromObject(), readRGBForTextureSpec:mapName:, REQUIRE_STAGE, ResourceManager::shaderBindingTypesDictionary, writeDiffuseColorTerm, writeDiffuseLighting, writeLightMaps, and writeTotalColor.

Referenced by writeFinalColorComposite, and writeLightMaps.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeLightVector

- (void) writeLightVector

Definition at line 874 of file OODefaultShaderSynthesizer.m.

875{
878
879 [self addVarying:@"vLightVector" ofType:@"vec3"];
880
881 [_vertexBody appendString:
882 @"\tvec3 lightVector = gl_LightSource[1].position.xyz;\n"
883 "\tvLightVector = lightVector * TBN;\n\t\n"];
884 [_fragmentBody appendFormat:@"\tvec3 lightVector = normalize(vLightVector);\n\t\n"];
885}

References addVarying:ofType:, REQUIRE_STAGE, writeLightVector, writeNormalIfNeeded, and writeVertexPosition.

Referenced by writeDiffuseLighting, writeLightVector, and writeSpecularLighting.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeNormal

- (void) writeNormal

Definition at line 936 of file OODefaultShaderSynthesizer.m.

937{
939
940 if (_constZNormal) {
941 [_fragmentBody appendString:@"\tconst vec3 normal = vec3(0.0, 0.0, 1.0);\n\t\n"];
942 }
943}

References _constZNormal, REQUIRE_STAGE, writeNormal, and writeNormalIfNeeded.

Referenced by writeNormal.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeNormalIfNeeded

- (void) writeNormalIfNeeded

Definition at line 910 of file OODefaultShaderSynthesizer.m.

911{
914
915 NSDictionary* normalMap = [_configuration oo_normalMapSpecifier];
916 if (normalMap == nil) {
917 // FIXME: this stuff should be handled in OOMaterialSpecifier.m when synthesizer takes over the world. -- Ahruman 2012-02-08
918 normalMap = [_configuration oo_normalAndParallaxMapSpecifier];
919 }
920 if (normalMap != nil) {
921 NSString *sample, *swizzle;
922 [self getSampleName:&sample andSwizzleOp:&swizzle forTextureSpec:normalMap];
923 if (swizzle == nil)
924 swizzle = @"rgb";
925 if ([swizzle length] == 3) {
926 [_fragmentBody appendFormat:@"\tvec3 normal = normalize(%@.%@ - 0.5);\n\t\n", sample, swizzle];
927 _usesNormalMap = YES;
928 return;
929 } else {
930 OOLogWARN(@"material.synthesis.warning.extractionMismatch", @"The %@ map for material \"%@\" of \"%@\" specifies %zu channels to extract, but only %@ may be used.", @"normal", [self materialKey], [self entityName], [swizzle length], @"3");
931 }
932 }
933 _constZNormal = YES;
934}

References _constZNormal, _usesNormalMap, entityName, getSampleName:andSwizzleOp:forTextureSpec:, materialKey, nil, OOLogWARN, REQUIRE_STAGE, writeNormalIfNeeded, writeVertexPosition, and writeVertexTangentBasis.

Referenced by writeDiffuseLighting, writeLightVector, writeNormal, writeNormalIfNeeded, and writeSpecularLighting.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeSpecularLighting

- (void) writeSpecularLighting

Definition at line 945 of file OODefaultShaderSynthesizer.m.

946{
947 float specularExponent = [_configuration oo_specularExponent];
948 if (specularExponent <= 0)
949 return;
950
951 NSDictionary* specularColorMap = [_configuration oo_specularColorMapSpecifier];
952 NSDictionary* specularExponentMap = [_configuration oo_specularExponentMapSpecifier];
953 float scaleFactor = 1.0f;
954
955 if (specularColorMap) {
956 scaleFactor = [specularColorMap oo_doubleForKey:kOOTextureSpecifierScaleFactorKey defaultValue:1.0f];
957 }
958
959 OOColor* specularColor = nil;
960 if (specularColorMap == nil) {
961 specularColor = [_configuration oo_specularColor];
962 } else {
963 specularColor = [_configuration oo_specularModulateColor];
964 }
965
966 if ([specularColor isBlack])
967 return;
968
969 BOOL modulateWithDiffuse = [specularColorMap oo_boolForKey:kOOTextureSpecifierSelfColorKey];
970
975 if (modulateWithDiffuse) {
977 }
978
979 [_fragmentBody appendString:@"\t// Specular (Blinn-Phong) lighting\n"];
980
981 BOOL haveSpecularColor = NO;
982 if (specularColorMap != nil) {
983 NSString* readInstr = [self readRGBForTextureSpec:specularColorMap mapName:@"specular colour"];
984 if (EXPECT_NOT(readInstr == nil)) {
985 [_fragmentBody appendString:@"\t// INVALID EXTRACTION KEY\n\t\n"];
986 return;
987 }
988
989 [_fragmentBody appendFormat:@"\tvec3 specularColor = %@;\n", readInstr];
990 haveSpecularColor = YES;
991 }
992
993 if (!haveSpecularColor || ![specularColor isWhite]) {
994 float rgba[4];
995 [specularColor getRed:&rgba[0] green:&rgba[1] blue:&rgba[2] alpha:&rgba[3]];
996
997 NSString* comment = (scaleFactor == 1.0f) ? @"Constant colour" : @"Constant colour and scale factor";
998
999 // Handle scale factor, colour, and colour alpha scaling as one multiply.
1000 scaleFactor *= rgba[3];
1001 rgba[0] *= scaleFactor;
1002 rgba[1] *= scaleFactor;
1003 rgba[2] *= scaleFactor;
1004
1005 // Avoid reapplying scaleFactor below.
1006 scaleFactor = 1.0;
1007
1008 NSString* format = nil;
1009 if (haveSpecularColor) {
1010 format = @"\tspecularColor *= vec3(%@, %@, %@); // %@\n";
1011 } else {
1012 format = @"\tvec3 specularColor = vec3(%@, %@, %@); // %@\n";
1013 haveSpecularColor = YES;
1014 }
1015 [_fragmentBody appendFormat:format, FormatFloat(rgba[0]), FormatFloat(rgba[1]), FormatFloat(rgba[2]), comment];
1016 }
1017
1018 // Handle scale_factor if no constant colour.
1019 if (haveSpecularColor && scaleFactor != 1.0f) {
1020 [_fragmentBody appendFormat:@"\tspecularColor *= %@; // Scale factor\n", FormatFloat(scaleFactor)];
1021 }
1022
1023 // Handle self_color.
1024 if (modulateWithDiffuse) {
1025 [_fragmentBody appendString:@"\tspecularColor *= diffuseColor; // Self-colouring\n"];
1026 }
1027
1028 // Specular exponent.
1029 BOOL haveSpecularExponent = NO;
1030 if (specularExponentMap != nil) {
1031 NSString* readInstr = [self readOneChannelForTextureSpec:specularExponentMap mapName:@"specular exponent"];
1032 if (EXPECT_NOT(readInstr == nil)) {
1033 [_fragmentBody appendString:@"\t// INVALID EXTRACTION KEY\n\t\n"];
1034 return;
1035 }
1036
1037 [_fragmentBody appendFormat:@"\tfloat specularExponent = %@ * %.1f;\n", readInstr, specularExponent];
1038 haveSpecularExponent = YES;
1039 }
1040 if (!haveSpecularExponent) {
1041 [_fragmentBody appendFormat:@"\tconst float specularExponent = %.1f;\n", specularExponent];
1042 }
1043
1044 if (_usesNormalMap) {
1045 [_fragmentBody appendFormat:@"\tvec3 reflection = reflect(lightVector, normal);\n"];
1046 } else {
1047 /* reflect(I, N) is defined as I - 2 * dot(N, I) * N
1048 If N is (0,0,1), this becomes (I.x,I.y,-I.z).
1049 */
1050 [_fragmentBody appendFormat:@"\tvec3 reflection = vec3(lightVector.x, lightVector.y, -lightVector.z); // Equivalent to reflect(lightVector, normal) since normal is known to be (0, 0, 1) in tangent space.\n"];
1051 }
1052
1053 [_fragmentBody appendFormat:
1054 @"\tfloat specIntensity = dot(reflection, eyeVector);\n"
1055 "\tspecIntensity = pow(max(0.0, specIntensity), specularExponent);\n"
1056 "\ttotalColor += specIntensity * specularColor * gl_LightSource[1].specular.rgb;\n\t\n"];
1057}

References _usesNormalMap, EXPECT_NOT, OOColor::getRed:green:blue:alpha:, nil, readOneChannelForTextureSpec:mapName:, readRGBForTextureSpec:mapName:, REQUIRE_STAGE, writeDiffuseColorTerm, writeEyeVector, writeLightVector, writeNormalIfNeeded, writeSpecularLighting, and writeTotalColor.

Referenced by writeFinalColorComposite, and writeSpecularLighting.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeTextureCoordRead

- (void) writeTextureCoordRead

Definition at line 757 of file OODefaultShaderSynthesizer.m.

758{
759 [self addVarying:@"vTexCoords" ofType:@"vec2"];
760 [_vertexBody appendString:@"\tvTexCoords = gl_MultiTexCoord0.st;\n\t\n"];
761
762 BOOL haveTexCoords = NO;
763 NSDictionary* parallaxMap = [_configuration oo_parallaxMapSpecifier];
764
765 if (parallaxMap != nil) {
766 float parallaxScale = [_configuration oo_parallaxScale];
767 if (parallaxScale != 0.0f) {
768 /*
769 We can’t call -getSampleName:... here because the standard
770 texture loading mechanism has to occur after determining
771 texture coordinates (duh).
772 */
773 NSString* swizzle = GetExtractMode(parallaxMap) ?: (NSString*)@"a";
774 NSUInteger channelCount = [swizzle length];
775 if (channelCount == 1) {
776 haveTexCoords = YES;
777
779
780 [_fragmentPreTextures appendString:@"\t// Parallax mapping\n"];
781
782 NSUInteger texID = [self assignIDForTexture:parallaxMap];
783 [_fragmentPreTextures appendFormat:@"\tfloat parallax = texture2D(uTexture%zu, vTexCoords).%@;\n", texID, swizzle];
784
785 if (parallaxScale != 1.0f) {
786 [_fragmentPreTextures appendFormat:@"\tparallax *= %@; // Parallax scale\n", FormatFloat(parallaxScale)];
787 }
788
789 float parallaxBias = [_configuration oo_parallaxBias];
790 if (parallaxBias != 0.0) {
791 [_fragmentPreTextures appendFormat:@"\tparallax += %@; // Parallax bias\n", FormatFloat(parallaxBias)];
792 }
793
794 [_fragmentPreTextures appendString:@"\tvec2 texCoords = vTexCoords - parallax * eyeVector.xy * vec2(1.0, -1.0);\n"];
795 } else {
796 OOLogWARN(@"material.synthesis.warning.extractionMismatch", @"The %@ map for material \"%@\" of \"%@\" specifies %zu channels to extract, but only %@ may be used.", @"parallax", [self materialKey], [self entityName], channelCount, @"1");
797 }
798 }
799 }
800
801 if (!haveTexCoords) {
802 [_fragmentPreTextures appendString:@"\tvec2 texCoords = vTexCoords;\n"];
803 }
804}

References addVarying:ofType:, assignIDForTexture:, entityName, GetExtractMode, materialKey, nil, OOLogWARN, REQUIRE_STAGE, writeEyeVector, and writeTextureCoordRead.

Referenced by setUpOneTexture:, and writeTextureCoordRead.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeTotalColor

- (void) writeTotalColor

Definition at line 1170 of file OODefaultShaderSynthesizer.m.

1171{
1172 [_fragmentPreTextures appendString:@"\tvec3 totalColor = vec3(0.0);\n\t\n"];
1173}

References writeTotalColor.

Referenced by writeDiffuseLighting, writeFinalColorComposite, writeLightMaps, writeSpecularLighting, and writeTotalColor.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeVertexPosition

- (void) writeVertexPosition

Definition at line 1163 of file OODefaultShaderSynthesizer.m.

1164{
1165 [_vertexBody appendString:
1166 @"\tvec4 position = gl_ModelViewMatrix * gl_Vertex;\n"
1167 "\tgl_Position = gl_ProjectionMatrix * position;\n\t\n"];
1168}

References writeVertexPosition.

Referenced by writeDiffuseLighting, writeEyeVector, writeLightVector, writeNormalIfNeeded, and writeVertexPosition.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeVertexTangentBasis

- (void) writeVertexTangentBasis

Definition at line 898 of file OODefaultShaderSynthesizer.m.

899{
900 [self addAttribute:@"tangent" ofType:@"vec3"];
901
902 [_vertexBody appendString:
903 @"\t// Build tangent space basis\n"
904 "\tvec3 n = gl_NormalMatrix * gl_Normal;\n"
905 "\tvec3 t = gl_NormalMatrix * tangent;\n"
906 "\tvec3 b = cross(n, t);\n"
907 "\tmat3 TBN = mat3(t, b, n);\n\t\n"];
908}

References addAttribute:ofType:, and writeVertexTangentBasis.

Referenced by writeEyeVector, writeNormalIfNeeded, and writeVertexTangentBasis.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _attributes

- (NSMutableString*) _attributes
private

◆ _completed_writeDiffuseColorTerm

- (NSUInteger) _completed_writeDiffuseColorTerm
private

Definition at line 92 of file OODefaultShaderSynthesizer.m.

◆ _completed_writeDiffuseColorTermIfNeeded

- (NSUInteger) _completed_writeDiffuseColorTermIfNeeded
private

Definition at line 96 of file OODefaultShaderSynthesizer.m.

◆ _completed_writeDiffuseLighting

- (NSUInteger) _completed_writeDiffuseLighting
private

Definition at line 95 of file OODefaultShaderSynthesizer.m.

◆ _completed_writeEyeVector

- (NSUInteger) _completed_writeEyeVector
private

Definition at line 101 of file OODefaultShaderSynthesizer.m.

◆ _completed_writeFinalColorComposite

- (NSUInteger) _completed_writeFinalColorComposite
private

Definition at line 91 of file OODefaultShaderSynthesizer.m.

◆ _completed_writeLightMaps

- (NSUInteger) _completed_writeLightMaps
private

Definition at line 94 of file OODefaultShaderSynthesizer.m.

◆ _completed_writeLightVector

- (NSUInteger) _completed_writeLightVector
private

Definition at line 100 of file OODefaultShaderSynthesizer.m.

◆ _completed_writeNormalIfNeeded

- (NSUInteger) _completed_writeNormalIfNeeded
private

Definition at line 98 of file OODefaultShaderSynthesizer.m.

◆ _completed_writeSpecularLighting

- (NSUInteger) _completed_writeSpecularLighting
private

Definition at line 93 of file OODefaultShaderSynthesizer.m.

◆ _completed_writeTextureCoordRead

- (NSUInteger) _completed_writeTextureCoordRead
private

Definition at line 103 of file OODefaultShaderSynthesizer.m.

◆ _completed_writeTotalColor

- (NSUInteger) _completed_writeTotalColor
private

Definition at line 102 of file OODefaultShaderSynthesizer.m.

◆ _completed_writeVertexPosition

- (NSUInteger) _completed_writeVertexPosition
private

Definition at line 97 of file OODefaultShaderSynthesizer.m.

◆ _completed_writeVertexTangentBasis

- (NSUInteger) _completed_writeVertexTangentBasis
private

Definition at line 104 of file OODefaultShaderSynthesizer.m.

◆ _configuration

- (NSDictionary*) _configuration
private

◆ _constZNormal

- (NSUInteger) _constZNormal
private

Definition at line 87 of file OODefaultShaderSynthesizer.m.

Referenced by writeDiffuseLighting, writeNormal, and writeNormalIfNeeded.

◆ _entityName

- (NSString*) _entityName
private

◆ _fragmentBody

- (NSMutableString*) _fragmentBody
private

◆ _fragmentHelpers

- (NSMutableString*) _fragmentHelpers
private

◆ _fragmentPreTextures

- (NSMutableString*) _fragmentPreTextures
private

◆ _fragmentShader

- (NSString*) _fragmentShader
private

Definition at line 61 of file OODefaultShaderSynthesizer.m.

Referenced by composeFragmentShader, dealloc, and fragmentShader.

◆ _fragmentTextureLookups

- (NSMutableString*) _fragmentTextureLookups
private

◆ _fragmentUniforms

- (NSMutableString*) _fragmentUniforms
private

◆ _haveDiffuseLight

- (NSUInteger) _haveDiffuseLight
private

Definition at line 88 of file OODefaultShaderSynthesizer.m.

Referenced by writeDiffuseLighting, and writeFinalColorComposite.

◆ _materialKey

- (NSString*) _materialKey
private

◆ _sampledTextures

- (NSHashTable*) _sampledTextures
private

Definition at line 81 of file OODefaultShaderSynthesizer.m.

Referenced by createTemporaries, destroyTemporaries, and setUpOneTexture:.

◆ _stagesInProgress

- (NSHashTable*) _stagesInProgress
private

Definition at line 107 of file OODefaultShaderSynthesizer.m.

Referenced by createTemporaries, destroyTemporaries, and performStage:.

◆ _textureIDs

- (NSMutableDictionary*) _textureIDs
private

Definition at line 79 of file OODefaultShaderSynthesizer.m.

Referenced by createTemporaries, and destroyTemporaries.

◆ _textures

- (NSMutableArray*) _textures
private

Definition at line 62 of file OODefaultShaderSynthesizer.m.

Referenced by createTemporaries, dealloc, and textureSpecifications.

◆ _texturesByName

- (NSMutableDictionary*) _texturesByName
private

Definition at line 77 of file OODefaultShaderSynthesizer.m.

Referenced by createTemporaries, and destroyTemporaries.

◆ _uniformBindingNames

- (NSMutableDictionary*) _uniformBindingNames
private

Definition at line 83 of file OODefaultShaderSynthesizer.m.

Referenced by createTemporaries, and destroyTemporaries.

◆ _uniforms

- (NSMutableDictionary*) _uniforms
private

Definition at line 63 of file OODefaultShaderSynthesizer.m.

Referenced by defineBindingUniform:ofType:, run, and uniformSpecifications.

◆ _usesDiffuseTerm

- (NSUInteger) _usesDiffuseTerm
private

◆ _usesNormalMap

- (NSUInteger) _usesNormalMap
private

Definition at line 85 of file OODefaultShaderSynthesizer.m.

Referenced by writeNormalIfNeeded, and writeSpecularLighting.

◆ _varyings

- (NSMutableString*) _varyings
private

◆ _vertexBody

- (NSMutableString*) _vertexBody
private

◆ _vertexHelpers

- (NSMutableString*) _vertexHelpers
private

◆ _vertexShader

- (NSString*) _vertexShader
private

Definition at line 60 of file OODefaultShaderSynthesizer.m.

Referenced by composeVertexShader, dealloc, and vertexShader.

◆ _vertexUniforms

- (NSMutableString*) _vertexUniforms
private

The documentation for this class was generated from the following file: