OpenGL ES教程系列_LessonY_使用2D纹理渲染文字

来源:互联网 发布:橙鑫数据科技靠谱吗 编辑:程序博客网 时间:2024/05/21 15:03

http://blog.csdn.net/casablaneca/article/details/23617069




在创建OpenGL场景时,我们常常需要在场景中渲染一些文字,如在游戏结束时,你需要显示一个“Game Over” 字样。 下面我在GLKit框架下写了一个方法,代码详细描述如下:

[objc] view plain copy
  1. //用文字作为image  
  2. -(UIImage *) imageWithText:(NSString *)text  
  3.                   fontName:(NSString *)fontName  
  4.                      color:(UIColor *)color  
  5.                      width:(float)width  
  6.                     height:(float)height  
  7.                 rightAlign:(BOOL) rightAlign  
  8. {  
  9.       
  10.     CGRect textRect = [text boundingRectWithSize:CGSizeMake(width, height)  
  11.                                          options:NSStringDrawingUsesLineFragmentOrigin  
  12.                                       attributes:@{NSFontAttributeName:[UIFont fontWithName:fontName size:60.0]}  
  13.                                          context:nil];  
  14.       
  15.     CGSize expectedLabelSize = textRect.size;  
  16.       
  17.     float offsetX = rightAlign ? (width - expectedLabelSize.width) : 0.0f;  
  18.     UIImage *image2 = [[UIImage alloc] init];  
  19.     UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), YES1);  
  20.     [image2 drawInRect:CGRectMake(offsetX,  
  21.                                   expectedLabelSize.height / 2.0,  
  22.                                   expectedLabelSize.width,  
  23.                                   expectedLabelSize.height)  
  24.              blendMode:kCGBlendModeNormal  
  25.                  alpha:1.0f];  
  26.     [image2 drawAtPoint:CGPointMake(offsetX, 0.0f)];  
  27.     [text drawAtPoint:CGPointMake(offsetX,0.0f)  
  28.        withAttributes:@{NSFontAttributeName:[UIFont fontWithName:fontName size:36.0],  
  29.                         NSForegroundColorAttributeName: color}];  
  30.       
  31.     UIImage *result = UIGraphicsGetImageFromCurrentImageContext();  
  32.     UIGraphicsEndImageContext();  
  33.       
  34.     return result;  
  35. }  

然后,按照下面的方式调用上述方法:

[objc] view plain copy
  1. UIImage *image = [self imageWithText:@"齐天大圣到此一游" fontName:@"Helvetica" color:[UIColor   greenColor] width:300.0 height:100.0 rightAlign:YES];  
  2.       
  3.     //设置纹理  
  4.     CGImageRef imageRef = [image CGImage];  
  5.       
  6.     //接受一个CGImgaeRef并创建一个新的包含CGImageRef的像素数据的OpenGL ES 纹理缓存  
  7.     GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithCGImage:imageRef options:nil error:NULL];  
  8.     self.effect.texture2d1.name = textureInfo.name;  
  9.     self.effect.texture2d1.target = textureInfo.target;  

由于用2D纹理绘制文字的原理比较简单,只需要看懂
[objc] view plain copy
  1. -(UIImage *) imageWithText:(NSString *)text  
  2.                   fontName:(NSString *)fontName  
  3.                      color:(UIColor *)color  
  4.                      width:(float)width  
  5.                     height:(float)height  
  6.                 rightAlign:(BOOL) rightAlign    
[objc] view plain copy
  1.   

方法就OK,整个十分方便。


最后是源码下载地址: http://download.csdn.net/detail/luozhonglan/7187875


阅读全文
0 0
原创粉丝点击