一、聲明
筆者以cocos2d架構cocos2d-x-3.3rc0版本的源碼做分析。本文為筆者原創,允許轉載和分享,隻要注明文章出處即可。
二、簡介
Texture2D類簡介
Texture2D類允許開發者用圖像、文本資訊和簡單的資料來建立OpenGL2D紋理。被建立的紋理擁有兩個次元。根據開發者建立Texture2D對象方式的不同,實際圖像的尺寸可能比生成的紋理的尺寸要小,而且紋理的内容是倒置的。
像素格式
在計算機圖形學中,人們用每個像素在記憶體中的總位數以及分别存儲紅、藍、綠和alpha(阿爾法通道)分量的位數來定義一個像素格式。像素格式描述了像素資料在記憶體中的存儲格式,也定義了像素在記憶體中的編碼方式。
在Texture2D類中,用一個枚舉類型 PixelFormat 來表示不同的像素格式。下面是像素格式枚舉類型的定義。
enum class PixelFormat
{
//! auto detect the type
AUTO,
//! 32-bit texture: BGRA8888
BGRA8888,
//! 32-bit texture: RGBA8888
RGBA8888,
//! 24-bit texture: RGBA888
RGB888,
//! 16-bit texture without Alpha channel
RGB565,
//! 8-bit textures used as masks
A8,
//! 8-bit intensity texture
I8,
//! 16-bit textures used as masks
AI88,
//! 16-bit textures: RGBA4444
RGBA4444,
//! 16-bit textures: RGB5A1
RGB5A1,
//! 4-bit PVRTC-compressed texture: PVRTC4
PVRTC4,
//! 4-bit PVRTC-compressed texture: PVRTC4 (has alpha channel)
PVRTC4A,
//! 2-bit PVRTC-compressed texture: PVRTC2
PVRTC2,
//! 2-bit PVRTC-compressed texture: PVRTC2 (has alpha channel)
PVRTC2A,
//! ETC-compressed texture: ETC
ETC,
//! S3TC-compressed texture: S3TC_Dxt1
S3TC_DXT1,
//! S3TC-compressed texture: S3TC_Dxt3
S3TC_DXT3,
//! S3TC-compressed texture: S3TC_Dxt5
S3TC_DXT5,
//! ATITC-compressed texture: ATC_RGB
ATC_RGB,
//! ATITC-compressed texture: ATC_EXPLICIT_ALPHA
ATC_EXPLICIT_ALPHA,
//! ATITC-compresed texture: ATC_INTERPOLATED_ALPHA
ATC_INTERPOLATED_ALPHA,
//! Default texture format: AUTO
DEFAULT = AUTO,
NONE = -1
};
阿爾法通道 alpha
alpha是指一張圖檔的透明度和半透明度。阿爾法通道的值由像素值的某幾位表示,比如說,一個16位像素可能使用5位儲存紅色、5為儲存藍色、5位儲存綠色,以及最後一位儲存阿爾法通量。這時候alpha的值隻有1和0,意味着紋理要麼透明,要麼不透明。
阿爾法通道除了能夠表示圖形的透明度外,還關系到不同像素顔色融合的效果。
三、源碼詳解
筆者比較支援從源代碼中尋找尋找架構接口的使用方法。筆者會在本文中通過家少cocos2d源代碼來介紹如何将一個圖形資源檔案加載成為Texture2D對象。
Texture2D紋理能夠用于生成精靈,接下來看一下Sprite類中運用Texture2D的例子。
bool Sprite::initWithFile(const std::string &filename, const Rect& rect)
{
CCASSERT(filename.size()>0, "Invalid filename");
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);
if (texture)
{
return initWithTexture(texture, rect);
}
// don't release here.
// when load texture failed, it's better to get a "transparent" sprite then a crashed program
// this->release();
return false;
}
上面這個函數中,Sprite類能夠利用圖像檔案名和一個裁剪矩陣生成Texture2D紋理,進而生成精靈。生成紋理的函數是 Director::getInstance()->getTextureCache()->addImage(filename). 下面看一下這些函數的定義。
TextureCache* Director::getTextureCache() const
{
return _textureCache;
}
//texture cache belongs to this director
TextureCache *_textureCache;
可見,在Director類中有一個TextureCache類型的 _textureCache 成員變量。TextureCache類中的addImage函數能夠将一個圖形檔案加載成為Texture2D紋理。下面看addImage函數的定義。
/** Returns a Texture2D object given an filename.
* If the filename was not previously loaded, it will create a new Texture2D
* object and it will return it. It will use the filename as a key.
* Otherwise it will return a reference of a previously loaded image.
* Supported image extensions: .png, .bmp, .tiff, .jpeg, .pvr
*/
Texture2D* addImage(const std::string &filepath);
從addImage函數的聲明中可以知道,addImage函數能夠将給定檔案名的圖形檔案加載成紋理。從中也可以看出TextureCache類的設計其實就是為了減少因生成紋理而多次多資源檔案加載造成CPU和記憶體的開銷。對于已經加載過的圖形資源檔案,TextureCache類不會再次加載,而是之間傳回對資源檔案的引用。
值得注意的一點是,開發者不用自行建立TextureCache對象,Director對形象持有一個TextureCache對象。
四、總結
筆者在本文中簡單介紹了像素和阿爾法通量等一些計算機圖形學的簡單概念,以及跟蹤源碼尋找在cocos2d開發中如何将一個圖形資源檔案加載成紋理。下面将生成紋理的接口在總結一下。
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);
轉載于:https://www.cnblogs.com/chenshi/p/4095827.html