解析Cocos2D中坐标系问题

移动开发 iOS 游戏开发
初学OpenglES纹理的时候,用Quarze2D读取的图片在贴在OpenglES坐标系里图片是上下颠倒的,原因就是坐标系的不同造成的。

Cocos2D是以OpenglES为底层图形库, 所以它采用的是OpenglES坐标系统,即x轴向右,y轴向上。  而苹果的Quarze2D则使用的是不同的坐标系统,x轴向右,y轴向下。在初学OpenglES纹理的时候,用Quarze2D读取的图片在贴在OpenglES坐标系里图片是上下颠倒的,原因就是坐标系的不同造成的。

最近在学习Cocos2D的时候,用它的API进行渲染到纹理的时候也出现了上下颠倒的情况。

具体是这样发生的。首先我进行渲染到纹理的功能,伪代码如下:

// 1: Create new CCRenderTexture     
   CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:textureSize height:textureSize];    
       
   // 2: Call CCRenderTexture:begin     
   [rt beginWithClear:bgColor.r g:bgColor.g b:bgColor.b a:bgColor.a];    
   // 3: Draw all you want here with OpenglES     
   // 4: Call CCRenderTexture:end     
   [rt end];    
       
   // 5: Create a new Sprite from the texture     
   CCSprite *renderSprite [CCSprite spriteWithTexture:rt.sprite.texture];    
 // 1: Create new CCRenderTexture  
    CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:textureSize height:textureSize];  
      
    // 2: Call CCRenderTexture:begin  
    [rt beginWithClear:bgColor.r g:bgColor.g b:bgColor.b a:bgColor.a];  
    // 3: Draw all you want here with OpenglES  
    // 4: Call CCRenderTexture:end  
    [rt end];  
      
    // 5: Create a new Sprite from the texture  
    CCSprite *renderSprite [CCSprite spriteWithTexture:rt.sprite.texture];  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

然后我用第五步生成的精灵加到场景中,发现图是上下倒立的。因为我在第三步中我是采用的是openglES的坐标系,如果我用Quarze2D坐标系的方式去绘制,结果就是正确的。

还有一种解决办法,就是

renderSprite.flipY = YES
  • 1.

发生这种问题的具体原因不太清楚,只有看看cocos2d具体实现了。

小结:解析Cocos2D坐标系问题的内容介绍完了,希望通过本文的学习,能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-27 14:48:21

iPhone Cocos2D 坐标

2011-08-08 15:40:47

Cocos2d

2013-05-22 14:38:44

iOS开发Cocos2d-x坐标系统

2011-07-27 13:44:08

2012-06-01 10:27:44

Cocos2d触摸分发原理

2011-07-08 14:33:02

Cocos2d iphone

2011-08-11 17:52:01

Cocos2d游戏对象

2011-08-11 14:22:47

iPhone游戏Cocos2D

2013-05-22 15:49:46

2011-08-09 16:08:58

IOS游戏Cocos2d

2011-08-11 14:32:04

iPhone游戏Cocos2dActions

2011-08-22 10:49:42

Cocos2d 开发CCLayerTouch事件

2011-08-08 11:40:42

Cocos2d CCLayer Touch

2011-07-27 10:13:23

Cocos2D iPhone

2011-08-11 18:00:18

Cocos2d动作Action

2011-08-09 16:25:16

Cocos2d视图坐标

2011-07-29 18:02:06

2011-08-17 15:04:48

Cocos2DUIAccelerom加速计

2011-08-02 15:37:48

Cocos2D UIAccelero

2011-07-20 14:04:46

Cocos2d iPhone 游戏
点赞
收藏

51CTO技术栈公众号