Oolite
Loading...
Searching...
No Matches
OOMaterialConvenienceCreators.m File Reference
Include dependency graph for OOMaterialConvenienceCreators.m:

Go to the source code of this file.

Classes

struct  OOMaterialSynthContext

Macros

#define USE_NEW_SHADER_SYNTHESIZER   0

Functions

static void SetUniform (NSMutableDictionary *uniforms, NSString *key, NSString *type, id value)
static void SetUniformFloat (OOMaterialSynthContext *context, NSString *key, float value)
static void AddTexture (OOMaterialSynthContext *context, NSString *uniformName, NSString *nonShaderKey, NSString *macroName, NSDictionary *specifier)
static void AddColorIfAppropriate (OOMaterialSynthContext *context, SEL selector, NSString *key, NSString *macroName)
static void AddMacroColorIfAppropriate (OOMaterialSynthContext *context, SEL selector, NSString *macroName)
static void SynthDiffuse (OOMaterialSynthContext *context, NSString *name)
static void SynthEmissionAndIllumination (OOMaterialSynthContext *context)
static void SynthNormalMap (OOMaterialSynthContext *context)
static void SynthSpecular (OOMaterialSynthContext *context)

Macro Definition Documentation

◆ USE_NEW_SHADER_SYNTHESIZER

#define USE_NEW_SHADER_SYNTHESIZER   0

Definition at line 29 of file OOMaterialConvenienceCreators.m.

Function Documentation

◆ AddColorIfAppropriate()

void AddColorIfAppropriate ( OOMaterialSynthContext * context,
SEL selector,
NSString * key,
NSString * macroName )
static

Definition at line 377 of file OOMaterialConvenienceCreators.m.

378{
379 OOColor* color = [context->inConfig performSelector:selector];
380
381 if (color != nil) {
382 [context->outConfig setObject:[color normalizedArray] forKey:key];
383 if (macroName != nil)
384 [context->macros setObject:@"1" forKey:macroName];
385 }
386}
return nil
NSArray * normalizedArray()
Definition OOColor.m:465

References nil, and OOColor::normalizedArray.

Referenced by SynthDiffuse(), and SynthEmissionAndIllumination().

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

◆ AddMacroColorIfAppropriate()

void AddMacroColorIfAppropriate ( OOMaterialSynthContext * context,
SEL selector,
NSString * macroName )
static

Definition at line 388 of file OOMaterialConvenienceCreators.m.

389{
390 OOColor* color = [context->inConfig performSelector:selector];
391
392 if (color != nil) {
393 NSString* macroText = [NSString stringWithFormat:@"vec4(%g, %g, %g, %g)",
394 [color redComponent],
395 [color greenComponent],
396 [color blueComponent],
397 [color alphaComponent]];
398 [context->macros setObject:macroText forKey:macroName];
399 }
400}
float alphaComponent()
Definition OOColor.m:442
float blueComponent()
Definition OOColor.m:339
float redComponent()
Definition OOColor.m:329
float greenComponent()
Definition OOColor.m:334

References OOColor::alphaComponent, OOColor::blueComponent, OOColor::greenComponent, nil, and OOColor::redComponent.

Referenced by SynthEmissionAndIllumination().

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

◆ AddTexture()

void AddTexture ( OOMaterialSynthContext * context,
NSString * uniformName,
NSString * nonShaderKey,
NSString * macroName,
NSDictionary * specifier )
static

Definition at line 362 of file OOMaterialConvenienceCreators.m.

363{
364 NSCParameterAssert(context->texturesUsed < context->maxTextures);
365
366 context->texturesUsed++;
367 SetUniform(context->uniforms, uniformName, @"texture", [NSNumber numberWithUnsignedInteger:[context->textures count]]);
368 [context->textures addObject:specifier];
369 if (nonShaderKey != nil) {
370 [context->outConfig setObject:specifier forKey:kOOMaterialDiffuseMapName];
371 }
372 if (macroName != nil) {
373 [context->macros setObject:@"1" forKey:macroName];
374 }
375}
static void SetUniform(NSMutableDictionary *uniforms, NSString *key, NSString *type, id value)
unsigned count

References count, OOMaterialSynthContext::maxTextures, nil, SetUniform(), OOMaterialSynthContext::textures, OOMaterialSynthContext::texturesUsed, and OOMaterialSynthContext::uniforms.

Referenced by SynthDiffuse(), SynthEmissionAndIllumination(), SynthNormalMap(), and SynthSpecular().

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

◆ SetUniform()

void SetUniform ( NSMutableDictionary * uniforms,
NSString * key,
NSString * type,
id value )
static

Definition at line 352 of file OOMaterialConvenienceCreators.m.

353{
354 [uniforms setObject:[NSDictionary dictionaryWithObjectsAndKeys:type, @"type", value, @"value", nil] forKey:key];
355}

Referenced by AddTexture(), and SetUniformFloat().

Here is the caller graph for this function:

◆ SetUniformFloat()

void SetUniformFloat ( OOMaterialSynthContext * context,
NSString * key,
float value )
static

Definition at line 357 of file OOMaterialConvenienceCreators.m.

358{
359 SetUniform(context->uniforms, key, @"float", [NSNumber numberWithFloat:value]);
360}

References SetUniform(), and OOMaterialSynthContext::uniforms.

Referenced by SynthNormalMap(), and SynthSpecular().

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

◆ SynthDiffuse()

void SynthDiffuse ( OOMaterialSynthContext * context,
NSString * name )
static

Definition at line 402 of file OOMaterialConvenienceCreators.m.

403{
404 // Set up diffuse map if appropriate.
405 NSDictionary* diffuseMapSpec = [context->inConfig oo_diffuseMapSpecifierWithDefaultName:name];
406 if (diffuseMapSpec != nil && context->texturesUsed < context->maxTextures) {
407 AddTexture(context, @"uDiffuseMap", kOOMaterialDiffuseMapName, @"OOSTD_DIFFUSE_MAP", diffuseMapSpec);
408
409 if ([diffuseMapSpec oo_boolForKey:@"cube_map"]) {
410 [context->macros setObject:@"1" forKey:@"OOSTD_DIFFUSE_MAP_IS_CUBE_MAP"];
411 }
412 } else {
413 // No diffuse map must be specified explicitly.
414 [context->outConfig setObject:@"" forKey:kOOMaterialDiffuseMapName];
415 }
416
417 // Set up diffuse colour if any.
418 AddColorIfAppropriate(context, @selector(oo_diffuseColor), kOOMaterialDiffuseColorName, nil);
419}
static void AddColorIfAppropriate(OOMaterialSynthContext *context, SEL selector, NSString *key, NSString *macroName)
static void AddTexture(OOMaterialSynthContext *context, NSString *uniformName, NSString *nonShaderKey, NSString *macroName, NSDictionary *specifier)
NSString *const kOOMaterialDiffuseColorName
NSString *const kOOMaterialDiffuseMapName

References AddColorIfAppropriate(), AddTexture(), kOOMaterialDiffuseColorName, kOOMaterialDiffuseMapName, OOMaterialSynthContext::maxTextures, nil, and OOMaterialSynthContext::texturesUsed.

Referenced by OOMaterial(OOConvenienceCreators)::synthesizeMaterialDictionaryWithName:configuration:macros:.

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

◆ SynthEmissionAndIllumination()

void SynthEmissionAndIllumination ( OOMaterialSynthContext * context)
static

Definition at line 421 of file OOMaterialConvenienceCreators.m.

422{
423 // Read the various emission and illumination textures, and decide what to do with them.
424 NSDictionary* emissionMapSpec = [context->inConfig oo_emissionMapSpecifier];
425 NSDictionary* illuminationMapSpec = [context->inConfig oo_illuminationMapSpecifier];
426 NSDictionary* emissionAndIlluminationSpec = [context->inConfig oo_emissionAndIlluminationMapSpecifier];
427 BOOL isCombinedSpec = NO;
428 BOOL haveIlluminationMap = NO;
429
430 if (emissionMapSpec == nil && emissionAndIlluminationSpec != nil) {
431 emissionMapSpec = emissionAndIlluminationSpec;
432 if (illuminationMapSpec == nil)
433 isCombinedSpec = YES; // Else use only emission part of emission_and_illumination_map, combined with full illumination_map.
434 }
435
436 if (emissionMapSpec != nil && context->texturesUsed < context->maxTextures) {
437 /* FIXME: at this point, if there is an illumination map, we should
438 consider merging it into the emission map using
439 OOCombinedEmissionMapGenerator if the total number of texture
440 specifiers is greater than context->maxTextures. This will
441 require adding a new type of texture specifier - not a big deal.
442 -- Ahruman 2010-05-21
443 */
444 AddTexture(context, @"uEmissionMap", nil, isCombinedSpec ? @"OOSTD_EMISSION_AND_ILLUMINATION_MAP" : @"OOSTD_EMISSION_MAP", emissionMapSpec);
445 /* Note that this sets emission_color, not emission_modulate_color.
446 This is because the emission colour value is sent through the
447 standard OpenGL emission colour attribute by OOBasicMaterial.
448 */
449 AddColorIfAppropriate(context, @selector(oo_emissionModulateColor), kOOMaterialEmissionColorName, @"OOSTD_EMISSION");
450
451 haveIlluminationMap = isCombinedSpec;
452 } else {
453 // No emission map, use overall emission colour if specified.
454 AddColorIfAppropriate(context, @selector(oo_emissionColor), kOOMaterialEmissionColorName, @"OOSTD_EMISSION");
455 }
456
457 if (illuminationMapSpec != nil && context->texturesUsed < context->maxTextures) {
458 AddTexture(context, @"uIlluminationMap", nil, @"OOSTD_ILLUMINATION_MAP", illuminationMapSpec);
459 haveIlluminationMap = YES;
460 }
461
462 if (haveIlluminationMap) {
463 AddMacroColorIfAppropriate(context, @selector(oo_illuminationModulateColor), @"OOSTD_ILLUMINATION_COLOR");
464 }
465}
static void AddMacroColorIfAppropriate(OOMaterialSynthContext *context, SEL selector, NSString *macroName)
NSString *const kOOMaterialEmissionColorName

References AddColorIfAppropriate(), AddMacroColorIfAppropriate(), AddTexture(), kOOMaterialEmissionColorName, OOMaterialSynthContext::maxTextures, nil, and OOMaterialSynthContext::texturesUsed.

Referenced by OOMaterial(OOConvenienceCreators)::synthesizeMaterialDictionaryWithName:configuration:macros:.

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

◆ SynthNormalMap()

void SynthNormalMap ( OOMaterialSynthContext * context)
static

Definition at line 467 of file OOMaterialConvenienceCreators.m.

468{
469 if (context->texturesUsed < context->maxTextures) {
470 BOOL hasParallax = YES;
471 NSDictionary* normalMapSpec = [context->inConfig oo_normalAndParallaxMapSpecifier];
472 if (normalMapSpec == nil) {
473 hasParallax = NO;
474 normalMapSpec = [context->inConfig oo_normalMapSpecifier];
475 }
476
477 if (normalMapSpec != nil) {
478 AddTexture(context, @"uNormalMap", nil, @"OOSTD_NORMAL_MAP", normalMapSpec);
479
480 if (hasParallax) {
481 [context->macros setObject:@"1" forKey:@"OOSTD_NORMAL_AND_PARALLAX_MAP"];
482 SetUniformFloat(context, @"uParallaxScale", [context->inConfig oo_parallaxScale]);
483 SetUniformFloat(context, @"uParallaxBias", [context->inConfig oo_parallaxBias]);
484 }
485 }
486 }
487}
static void SetUniformFloat(OOMaterialSynthContext *context, NSString *key, float value)

References AddTexture(), OOMaterialSynthContext::inConfig, OOMaterialSynthContext::maxTextures, nil, SetUniformFloat(), and OOMaterialSynthContext::texturesUsed.

Referenced by OOMaterial(OOConvenienceCreators)::synthesizeMaterialDictionaryWithName:configuration:macros:.

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

◆ SynthSpecular()

void SynthSpecular ( OOMaterialSynthContext * context)
static

Definition at line 489 of file OOMaterialConvenienceCreators.m.

490{
491 GLint shininess = [context->inConfig oo_specularExponent];
492 if (shininess <= 0)
493 return;
494
495 GLfloat gloss = [context->inConfig oo_gloss];
496 if (gloss < 0.0f || gloss > 1.0f)
497 return;
498
499 BOOL gammaCorrect = [context->inConfig oo_gammaCorrect];
500
501 NSDictionary* specularMapSpec = nil;
502 OOColor* specularColor = nil;
503
504 if (context->texturesUsed < context->maxTextures) {
505 specularMapSpec = [context->inConfig oo_combinedSpecularMapSpecifier];
506 }
507
508 if (specularMapSpec != nil)
509 specularColor = [context->inConfig oo_specularModulateColor];
510 else
511 specularColor = [context->inConfig oo_specularColor];
512 if ([specularColor isBlack])
513 return;
514
515 SetUniformFloat(context, @"uGloss", gloss);
516
517 [context->outConfig setObject:[NSNumber numberWithUnsignedInt:shininess] forKey:kOOMaterialSpecularExponentLegacyName];
518
519 if (specularMapSpec != nil) {
520 AddTexture(context, @"uSpecularMap", kOOMaterialDiffuseMapName, @"OOSTD_SPECULAR_MAP", specularMapSpec);
521 }
522
523 if (specularColor != nil) {
524 /* As with emission colour, specular_modulate_color is transformed to
525 specular_color here because the shader reads it from the standard
526 material specular colour property set by OOBasicMaterial.
527 */
528 [context->outConfig setObject:[specularColor normalizedArray] forKey:kOOMaterialSpecularColorName];
529 }
530 [context->macros setObject:@"1" forKey:@"OOSTD_SPECULAR"];
531
532 // setting a bool as a float uniform, to be used in the shader as a bool again
533 // this is how hackish I can get... maybe a better way exists, but this is quick
534 // and can be used also for the shader materials in a not too different way
535 // - Nikos 20181001
536 SetUniformFloat(context, @"uGammaCorrect", (float)gammaCorrect);
537}

References AddTexture(), kOOMaterialDiffuseMapName, OOMaterialSynthContext::maxTextures, nil, OOColor::normalizedArray, SetUniformFloat(), and OOMaterialSynthContext::texturesUsed.

Referenced by OOMaterial(OOConvenienceCreators)::synthesizeMaterialDictionaryWithName:configuration:macros:.

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