(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.17使用glColorMask照明

(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook

著作声明:本文由iam126 翻译,欢迎转载分享。

请尊重作者劳动,转载时保留该声 明和作者博客链接,谢谢!

相关程序代码下载:http://download.csdn.net/detail/iam126/4068610

或搜索“Cocos2d_for_iPhone_1_Game_Development_Cookbook代码”于CSDN;

新手翻译,不准确请见谅,参考代码与原书。

 

1.17使用glColorMask照明

 

灯光,是大部分3D游戏的根本。一些2D游戏并不是特别的看重灯光效果,但是恰当的技术,可以让我们再2D游戏中,通过灯光,烘托出一些重要的角色,或为我们的2D场景增加悬念。

 

 
 

在这个案例中,我们将会看到一个拿着灯缓步行走的僧侣。僧侣的灯光使得在黑暗的场景中出现一丝光明。当僧侣走过的时候,身边的蝙蝠也变得可以看清了。

 

如何去做…

 

执行下列代码:

 

@interface Ch1_ColorMaskLighting : Recipe

{

   SimpleAnimObject *burnSprite;

   SimpleAnimObject *lightSprite;

   SimpleAnimObject *monkSprite;

   CCRenderTexture *darknessLayer;

   NSMutableArray *bats;

   CCAnimation *batFlyUp;

   CCAnimation *batGlideDown;

}

@end

@implementation Ch1_ColorMaskLighting

-(CCLayer*) runRecipe

{

   //Add our PLISTs to the SpriteFrameCache singleton

   //为一个精灵帧缓存单例添加PLIST文件

   CCSpriteFrameCache * cache = [CCSpriteFrameCachesharedSpriteFrameCache];

   [cache addSpriteFramesWithFile:@"simple_bat.plist"];

   [cache addSpriteFramesWithFile:@"monk_lantern.plist"];

   //Add cave background

   //洞穴背景

   CCSprite *caveBg = [CCSprite spriteWithFile:@"cave.png"];

   [caveBg setPosition:ccp(240,160)];

   [self addChild: caveBg z:0 tag:TAG_CAVE_BG];

   //Set up the burn sprite that will "knock out" parts of thedarkness layer depending on the alpha value of the pixels in the image.

   //创建一个精灵,用来去掉部分黑暗层的图片透明度

   burnSprite = [SimpleAnimObject spriteWithFile:@"fire.png"];

   burnSprite.position = ccp(50,50);

   burnSprite.scale = 10.0f;

   [burnSprite setBlendFunc: (ccBlendFunc) { GL_ZERO,GL_ONE_MINUS_SRC_ALPHA }];

   [burnSprite retain];

   burnSprite.velocity = ccp(1,0);

   //Add a 'light' sprite which additively blends onto the scene. Thisrepresents the cone of light created by the monk's candle.

   //加入一个光源,与场景融为一体。这代表着僧侣的蜡烛发出的光芒

   lightSprite = [SimpleAnimObject spriteWithFile:@"fire.png"];

   lightSprite.position = ccp(50,50);

   lightSprite.scale = 10.0f;

   [lightSprite setColor:ccc3(100,100,50)];

   [lightSprite setBlendFunc: (ccBlendFunc) { GL_ONE, GL_ONE }];

   lightSprite.velocity = ccp(1,0);

   [self addChild:lightSprite z:4 tag:TAG_LIGHT_SPRITE];

   //Add the monk

   //添加一个僧侣

   monkSprite = [[SimpleAnimObject alloc] init];

   monkSprite.position = ccp(50,50);

   monkSprite.velocity = ccp(1,0);

   [self addChild:monkSprite z:1 tag:TAG_MONK];

   //Animate the monk to simulate walking.

   //僧侣的行走动作

   CCAnimation *animation = [[CCAnimation alloc]initWithName:@"monk_lantern_walk" delay:0.1f];

   for(int i=1; i<=5; i+=1)

   {

       [animation addFrame:[cache spriteFrameByName:[NSStringstringWithFormat:@"monk_lantern_0%i.png",i]]];

   }

   for(int i=4; i>=2; i-=1)

   {

       [animation addFrame:[cache spriteFrameByName:[NSStringstringWithFormat:@"monk_lantern_0%i.png",i]]];

   }

   [monkSprite runAction:[CCRepeatForever actionWithAction: [CCAnimateactionWithAnimation:animation]]];

   //Add the 'darkness' layer. This simulates darkness in the cave.

   //添加黑暗层,计算洞穴中的黑暗。

   darknessLayer = [CCRenderTexture renderTextureWithWidth:480 height:320];

   darknessLayer.position = ccp(240,160);

   [self addChild:darknessLayer z:0 tag:TAG_DARKNESS_LAYER];

   //Schedule physics updates

   //循环方法。

   [self schedule:@selector(step:)];

   return self;

}

-(void)step:(ccTime)delta

{

   CGSize s = [[CCDirector sharedDirector] winSize];

   //Clear the darkness layer for redrawing. Here we clear it to BLACK with90% opacity.

   //为了重绘,我们需要时刻清除黑暗层,并且为之创建90%的透明度

   [darknessLayer clear:0.0f g:0.0f b:0.0f a:0.9f];

   //Begin the darkness layer drawing routine. This transforms to theproper location, among other things.

   //开始绘制黑暗层。

   [darknessLayer begin];

   //Limit drawing to the alpha channel.

   //绘制透明通道

   glColorMask(0.0f, 0.0f, 0.0f, 1.0f);

   //Draw the burn sprite only on the alpha channel.

   //在通道上绘制burnSprite

   [burnSprite visit];

   //Reset glColorMask to allow drawing of colors.

   //重置glColorMask去允许绘制颜色

   glColorMask(1.0f, 1.0f, 1.0f, 1.0f);

   //Finish transformation.

   //完成这些变化

   [darknessLayer end];

   //Make the monk walk back and forth.

   //设置僧侣的图片方向

   if(monkSprite.position.x > 480)

   {

       monkSprite.flipX = YES;

       burnSprite.velocity = ccp(-1,0);

       lightSprite.velocity = ccp(-1,0);

       monkSprite.velocity = ccp(-1,0);

   }

   else if(monkSprite.position.x < 0)

   {

       monkSprite.flipX = NO;

       burnSprite.velocity = ccp(1,0);

       lightSprite.velocity = ccp(1,0);

       monkSprite.velocity = ccp(1,0);

   }

   //Update our SimpleAnimObjects

   //看其实就是继续移动。移动的方法封装进了update:。

   [burnSprite update:delta];

   [lightSprite update:delta];

   [monkSprite update:delta];

}

@end

 

如何工作…

 

cocos2d封装了部分的OpenGL绘图逻辑来制作,使得复杂的渲染操作看起来很容易。通过使用CCRenderTexture类,我们得到了想要的效果。首先,我们先用如下代码清理了场景:

[darknessLayer clear:0.0f g:0.0f b:0.0fa:0.9f];

然后我们使用glColorMask方法仅仅绘制了透明通道。在我们渲染的基础上,OpenGL修改了图像的透明度(仅仅是透明度,而不是颜色)。所以,我们渲染了fire.png这个材质去模拟光源,一个平面的扩展到圆形的光源。

 

最后,我们添加了另一个fire.png材质,通过这个去模拟亮度以及光源的颜色。

 

burnSprite和lightSprite在灯的位置被渲染的范围,darknessLayer在场景的可视区域内渲染。

 

更多的事…

 

使用一个相似技术,可以创建所有形状尺寸颜色的光源。这些包括动态的光源比如火把、有形的光源比如车灯,快速的光源比如爆炸所产生的照明效果。

 

最重要的是,这个效果可以让我们去“捉弄”玩家,比如在游戏世界的阴影中潜伏一些东西。

 

(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook

著作声明:本文由iam126 翻译,欢迎转载分享。

请尊重作者劳动,转载时保留该声 明和作者博客链接,谢谢!

相关程序代码下载:http://download.csdn.net/detail/iam126/4068610

或搜索“Cocos2d_for_iPhone_1_Game_Development_Cookbook代码”于CSDN;

新手翻译,不准确请见谅,参考代码与原书。

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值