2
\$\begingroup\$

How can I apply a texture over a material?

I've set up an object with the following material:

GLKBaseEffect *effect;
effect.colorMaterialEnabled = false;
effect.material.ambientColor = GLKVector4Make(251.0/255.0, 95.0/255.0, 96.0/255.0, 1.0);
effect.material.diffuseColor = GLKVector4Make(251.0/255.0, 95.0/255.0, 96.0/255.0, 1.0);

and it is correctly rendered with a pink color.

However, when I apply a texture with:

GLKTextureInfo * info = [GLKTextureLoader textureWithCGImage:...];
effect.texture2d0.name = info.name;
effect.texture2d0.enabled = true;

the material seems to disappear and I just see the texture; the object is transparent where the texture is not visible, instead of being pink.


Here's an example:

The texture is created with the following code:

- (UIImage*)generateTexturefromText:(NSString*)text {
    NSError *error;

    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    myLabel.text = text;
    myLabel.font = [UIFont fontWithName:"Helvetica" size:50];
    myLabel.textAlignment = NSTextAlignmentLeft;
    myLabel.textColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1];
    myLabel.backgroundColor = [UIColor clearColor];
    myLabel.numberOfLines = -1;
    myLabel.lineBreakMode = NSLineBreakByCharWrapping;

    UIGraphicsBeginImageContextWithOptions(myLabel.bounds.size, NO, 1.0);
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, 0);
    CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, 1.0);
    [myLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    if (error) {
        NSLog(" Error loading texture from image: %",error);
        return nil;
    }

    return layerImage;
}

Why is no color visible where the texture is transparent?

texture over material

\$\endgroup\$
3
\$\begingroup\$

Resolved!

effect.texture2d0.envMode = GLKTextureEnvModeDecal;

It is related to the Texture Environment Parameters, i.e. how OpenGL combines the diffuse color of the material with the texture value.

As described here: khronos.org/opengles/sdk/1.1/docs/man/glTexEnv.xml. If I'm right that GLKit instruction should correspond to:

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 
\$\endgroup\$
  • \$\begingroup\$ You should really flesh this out more. Tell us why it works, what it does, etc. \$\endgroup\$ – MichaelHouse Mar 14 '14 at 14:12
  • \$\begingroup\$ Done, hope it helps :) \$\endgroup\$ – DAN Mar 14 '14 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.