Adding the glass effect

We will look at some of the more complex effects.

Getting ready

To add a glass effect, first of all, we need to add the glass image and then a normal map of the glass image to the project.

How to do it…

First, we will add the glass sprite to the scene, as follows:

//crystal sprite

CCSprite* crystal = [CCSprite spriteWithImageNamed:@"granite_DIF.png"];

crystal.position = CGPointMake(winSize.width/2, winSize.height/2);

[self addChild:crystal];

crystal.normalMapSpriteFrame = [CCSpriteFrame frameWithImageNamed:@"granite_NRM.jpg"];

Now that the glass sprite is added in, we can create the effect. We will execute the following code for this:

        //** Glass effect
        CCEffectGlass* glassEffect = [CCEffectGlass 
          effectWithShininess:1.0f

          refraction:1.0f
          refractionEnvironment:backgroundImage 
          reflectionEnvironment:backgroundImage];

        crystal.effect = glassEffect;

When creating the effect, we will pass in the shininess and reflection factors. We will also pass in the image that will be affected by the glass effect. In this case, we will give the background image to be affected by the reflection and refraction.

Finally, we will add the effect to the crystal's effect property.

How it works…

Run the application to see the background image getting affected by the crystal.

How it works…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.237.164