解析Cocos2D中座標系問題

來源:互聯網
上載者:User

Cocos2D是以OpenglES為底層圖形庫, 所以它採用的是OpenglES座標系統,即x軸向右,y軸向上。  而蘋果的Quarze2D則使用的是不同的座標系統,x軸向右,y軸向下。在初學OpenglES紋理的時候,用Quarze2D讀取的圖片在貼在OpenglES座標系裡圖片是上下顛倒的,原因就是座標系的不同造成的。

最近在學習Cocos2D的時候,用它的API進行渲染到紋理的時候也出現了上下顛倒的情況。

具體是這樣發生的。首先我進行渲染到紋理的功能,虛擬碼如下:

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

然後我用第五步產生的精靈加到情境中,發現圖是上下倒立的。因為我在第三步中我是採用的是openglES的座標系,如果我用Quarze2D座標系的方式去繪製,結果就是正確的。

還有一種解決辦法,就是

 
  1. renderSprite.flipY = YES; 

發生這種問題的具體原因不太清楚,只有看看cocos2d具體實現了。

小結:解析Cocos2D座標系問題的內容介紹完了,希望通過本文的學習,能對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.