axmol/cocos/2d/CCTexture2D.cpp

1415 lines
47 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2008 Apple Inc. All Rights Reserved.
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
/*
* Support for RGBA_4_4_4_4 and RGBA_5_5_5_1 was copied from:
* https://devforums.apple.com/message/37855#37855 by a1studmuffin
*/
2014-05-10 09:39:25 +08:00
#include "CCGL.h"
#include "2d/CCTexture2D.h"
2014-05-10 09:39:25 +08:00
#include "2d/platform/CCImage.h"
#include "2d/ccUtils.h"
#include "2d/platform/CCDevice.h"
2014-04-30 08:37:36 +08:00
#include "base/ccConfig.h"
#include "base/ccMacros.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "base/CCConfiguration.h"
#include "base/CCPlatformMacros.h"
2014-04-30 08:37:36 +08:00
#include "base/CCDirector.h"
#include "renderer/CCGLProgram.h"
#include "renderer/ccGLStateCache.h"
2014-05-10 09:39:25 +08:00
#include "renderer/CCGLProgramCache.h"
2014-04-09 22:30:49 +08:00
#include "deprecated/CCString.h"
2014-05-10 09:39:25 +08:00
#if CC_ENABLE_CACHE_TEXTURE_DATA
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "2d/CCTextureCache.h"
#endif
2012-04-18 18:43:45 +08:00
NS_CC_BEGIN
namespace {
typedef Texture2D::PixelFormatInfoMap::value_type PixelFormatInfoMapValue;
static const PixelFormatInfoMapValue TexturePixelFormatInfoTablesValue[] =
{
PixelFormatInfoMapValue(Texture2D::PixelFormat::BGRA8888, Texture2D::PixelFormatInfo(GL_RGBA, GL_BGRA, GL_UNSIGNED_BYTE, 32, false, true)),
PixelFormatInfoMapValue(Texture2D::PixelFormat::RGBA8888, Texture2D::PixelFormatInfo(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 32, false, true)),
PixelFormatInfoMapValue(Texture2D::PixelFormat::RGBA4444, Texture2D::PixelFormatInfo(GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 16, false, true)),
PixelFormatInfoMapValue(Texture2D::PixelFormat::RGB5A1, Texture2D::PixelFormatInfo(GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 16, false, true)),
PixelFormatInfoMapValue(Texture2D::PixelFormat::RGB565, Texture2D::PixelFormatInfo(GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 16, false, false)),
PixelFormatInfoMapValue(Texture2D::PixelFormat::RGB888, Texture2D::PixelFormatInfo(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, 24, false, false)),
PixelFormatInfoMapValue(Texture2D::PixelFormat::A8, Texture2D::PixelFormatInfo(GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, 8, false, false)),
PixelFormatInfoMapValue(Texture2D::PixelFormat::I8, Texture2D::PixelFormatInfo(GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, 8, false, false)),
PixelFormatInfoMapValue(Texture2D::PixelFormat::AI88, Texture2D::PixelFormatInfo(GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 16, false, true)),
#ifdef GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG
PixelFormatInfoMapValue(Texture2D::PixelFormat::PVRTC2, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, 0xFFFFFFFF, 0xFFFFFFFF, 2, true, false)),
PixelFormatInfoMapValue(Texture2D::PixelFormat::PVRTC2A, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, 0xFFFFFFFF, 0xFFFFFFFF, 2, true, true)),
PixelFormatInfoMapValue(Texture2D::PixelFormat::PVRTC4, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, 0xFFFFFFFF, 0xFFFFFFFF, 4, true, false)),
PixelFormatInfoMapValue(Texture2D::PixelFormat::PVRTC4A, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, 0xFFFFFFFF, 0xFFFFFFFF, 4, true, true)),
#endif
#ifdef GL_ETC1_RGB8_OES
PixelFormatInfoMapValue(Texture2D::PixelFormat::ETC, Texture2D::PixelFormatInfo(GL_ETC1_RGB8_OES, 0xFFFFFFFF, 0xFFFFFFFF, 24, true, false)),
#endif
2013-08-08 14:11:22 +08:00
#ifdef GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
2013-08-09 12:54:05 +08:00
PixelFormatInfoMapValue(Texture2D::PixelFormat::S3TC_DXT1, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 0xFFFFFFFF, 0xFFFFFFFF, 4, true, false)),
2013-08-08 14:11:22 +08:00
#endif
#ifdef GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
2013-08-09 12:54:05 +08:00
PixelFormatInfoMapValue(Texture2D::PixelFormat::S3TC_DXT3, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, 0xFFFFFFFF, 0xFFFFFFFF, 8, true, false)),
2013-08-08 14:11:22 +08:00
#endif
#ifdef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
2013-08-09 12:54:05 +08:00
PixelFormatInfoMapValue(Texture2D::PixelFormat::S3TC_DXT5, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 0xFFFFFFFF, 0xFFFFFFFF, 8, true, false)),
2013-08-08 14:11:22 +08:00
#endif
#ifdef GL_ATC_RGB_AMD
PixelFormatInfoMapValue(Texture2D::PixelFormat::ATC_RGB, Texture2D::PixelFormatInfo(GL_ATC_RGB_AMD,
0xFFFFFFFF, 0xFFFFFFFF, 4, true, false)),
#endif
#ifdef GL_ATC_RGBA_EXPLICIT_ALPHA_AMD
PixelFormatInfoMapValue(Texture2D::PixelFormat::ATC_EXPLICIT_ALPHA, Texture2D::PixelFormatInfo(GL_ATC_RGBA_EXPLICIT_ALPHA_AMD,
0xFFFFFFFF, 0xFFFFFFFF, 8, true, false)),
#endif
#ifdef GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD
PixelFormatInfoMapValue(Texture2D::PixelFormat::ATC_INTERPOLATED_ALPHA, Texture2D::PixelFormatInfo(GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD,
0xFFFFFFFF, 0xFFFFFFFF, 8, true, false)),
#endif
};
}
//CLASS IMPLEMENTATIONS:
//The PixpelFormat corresponding information
const Texture2D::PixelFormatInfoMap Texture2D::_pixelFormatInfoTables(TexturePixelFormatInfoTablesValue,
TexturePixelFormatInfoTablesValue + sizeof(TexturePixelFormatInfoTablesValue) / sizeof(TexturePixelFormatInfoTablesValue[0]));
// If the image has alpha, you can create RGBA8 (32-bit) or RGBA4 (16-bit) or RGB5A1 (16-bit)
// Default is: RGBA8888 (32-bit textures)
static Texture2D::PixelFormat g_defaultAlphaPixelFormat = Texture2D::PixelFormat::DEFAULT;
static bool _PVRHaveAlphaPremultiplied = false;
2013-07-17 17:12:04 +08:00
//////////////////////////////////////////////////////////////////////////
//conventer function
// IIIIIIII -> RRRRRRRRGGGGGGGGGBBBBBBBB
2013-12-05 17:19:01 +08:00
void Texture2D::convertI8ToRGB888(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
2013-12-05 17:19:01 +08:00
for (ssize_t i=0; i < dataLen; ++i)
2013-07-19 15:37:54 +08:00
{
*outData++ = data[i]; //R
*outData++ = data[i]; //G
*outData++ = data[i]; //B
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIIIAAAAAAAA -> RRRRRRRRGGGGGGGGBBBBBBBB
2013-12-05 17:19:01 +08:00
void Texture2D::convertAI88ToRGB888(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 1; i < l; i += 2)
2013-07-19 15:37:54 +08:00
{
*outData++ = data[i]; //R
*outData++ = data[i]; //G
*outData++ = data[i]; //B
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIII -> RRRRRRRRGGGGGGGGGBBBBBBBBAAAAAAAA
2013-12-05 17:19:01 +08:00
void Texture2D::convertI8ToRGBA8888(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0; i < dataLen; ++i)
2013-07-19 15:37:54 +08:00
{
*outData++ = data[i]; //R
*outData++ = data[i]; //G
*outData++ = data[i]; //B
*outData++ = 0xFF; //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIIIAAAAAAAA -> RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA
2013-12-05 17:19:01 +08:00
void Texture2D::convertAI88ToRGBA8888(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 1; i < l; i += 2)
2013-07-19 15:37:54 +08:00
{
*outData++ = data[i]; //R
*outData++ = data[i]; //G
*outData++ = data[i]; //B
*outData++ = data[i + 1]; //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIII -> RRRRRGGGGGGBBBBB
2013-12-05 17:19:01 +08:00
void Texture2D::convertI8ToRGB565(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
unsigned short* out16 = (unsigned short*)outData;
for (int i = 0; i < dataLen; ++i)
2013-07-19 15:37:54 +08:00
{
*out16++ = (data[i] & 0x00F8) << 8 //R
| (data[i] & 0x00FC) << 3 //G
| (data[i] & 0x00F8) >> 3; //B
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIIIAAAAAAAA -> RRRRRGGGGGGBBBBB
2013-12-05 17:19:01 +08:00
void Texture2D::convertAI88ToRGB565(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
unsigned short* out16 = (unsigned short*)outData;
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 1; i < l; i += 2)
2013-07-19 15:37:54 +08:00
{
*out16++ = (data[i] & 0x00F8) << 8 //R
| (data[i] & 0x00FC) << 3 //G
| (data[i] & 0x00F8) >> 3; //B
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIII -> RRRRGGGGBBBBAAAA
2013-12-05 17:19:01 +08:00
void Texture2D::convertI8ToRGBA4444(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
unsigned short* out16 = (unsigned short*)outData;
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0; i < dataLen; ++i)
2013-07-19 15:37:54 +08:00
{
*out16++ = (data[i] & 0x00F0) << 8 //R
| (data[i] & 0x00F0) << 4 //G
| (data[i] & 0x00F0) //B
| 0x000F; //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIIIAAAAAAAA -> RRRRGGGGBBBBAAAA
2013-12-05 17:19:01 +08:00
void Texture2D::convertAI88ToRGBA4444(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
unsigned short* out16 = (unsigned short*)outData;
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 1; i < l; i += 2)
2013-07-19 15:37:54 +08:00
{
*out16++ = (data[i] & 0x00F0) << 8 //R
| (data[i] & 0x00F0) << 4 //G
| (data[i] & 0x00F0) //B
| (data[i+1] & 0x00F0) >> 4; //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIII -> RRRRRGGGGGBBBBBA
2013-12-05 17:19:01 +08:00
void Texture2D::convertI8ToRGB5A1(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
unsigned short* out16 = (unsigned short*)outData;
for (int i = 0; i < dataLen; ++i)
2013-07-19 15:37:54 +08:00
{
*out16++ = (data[i] & 0x00F8) << 8 //R
| (data[i] & 0x00F8) << 3 //G
| (data[i] & 0x00F8) >> 2 //B
| 0x0001; //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIIIAAAAAAAA -> RRRRRGGGGGBBBBBA
2013-12-05 17:19:01 +08:00
void Texture2D::convertAI88ToRGB5A1(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
unsigned short* out16 = (unsigned short*)outData;
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 1; i < l; i += 2)
2013-07-19 15:37:54 +08:00
{
*out16++ = (data[i] & 0x00F8) << 8 //R
| (data[i] & 0x00F8) << 3 //G
| (data[i] & 0x00F8) >> 2 //B
| (data[i + 1] & 0x0080) >> 7; //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIII -> IIIIIIIIAAAAAAAA
2013-12-05 17:19:01 +08:00
void Texture2D::convertI8ToAI88(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
unsigned short* out16 = (unsigned short*)outData;
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0; i < dataLen; ++i)
2013-07-19 15:37:54 +08:00
{
*out16++ = 0xFF00 //A
| data[i]; //I
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIIIAAAAAAAA -> AAAAAAAA
2013-12-05 17:19:01 +08:00
void Texture2D::convertAI88ToA8(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
2013-12-05 17:19:01 +08:00
for (ssize_t i = 1; i < dataLen; i += 2)
2013-07-19 15:37:54 +08:00
{
*outData++ = data[i]; //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// IIIIIIIIAAAAAAAA -> IIIIIIII
2013-12-05 17:19:01 +08:00
void Texture2D::convertAI88ToI8(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 1; i < l; i += 2)
2013-07-19 15:37:54 +08:00
{
*outData++ = data[i]; //R
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBB -> RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA
2013-12-05 17:19:01 +08:00
void Texture2D::convertRGB888ToRGBA8888(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 2; i < l; i += 3)
2013-07-19 15:37:54 +08:00
{
*outData++ = data[i]; //R
*outData++ = data[i + 1]; //G
*outData++ = data[i + 2]; //B
*outData++ = 0xFF; //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> RRRRRRRRGGGGGGGGBBBBBBBB
2013-12-05 17:19:01 +08:00
void Texture2D::convertRGBA8888ToRGB888(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 3; i < l; i += 4)
2013-07-19 15:37:54 +08:00
{
*outData++ = data[i]; //R
*outData++ = data[i + 1]; //G
*outData++ = data[i + 2]; //B
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBB -> RRRRRGGGGGGBBBBB
2013-12-05 17:19:01 +08:00
void Texture2D::convertRGB888ToRGB565(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
unsigned short* out16 = (unsigned short*)outData;
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 2; i < l; i += 3)
2013-07-19 15:37:54 +08:00
{
*out16++ = (data[i] & 0x00F8) << 8 //R
| (data[i + 1] & 0x00FC) << 3 //G
| (data[i + 2] & 0x00F8) >> 3; //B
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> RRRRRGGGGGGBBBBB
2013-12-05 17:19:01 +08:00
void Texture2D::convertRGBA8888ToRGB565(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
unsigned short* out16 = (unsigned short*)outData;
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 3; i < l; i += 4)
2013-07-19 15:37:54 +08:00
{
*out16++ = (data[i] & 0x00F8) << 8 //R
| (data[i + 1] & 0x00FC) << 3 //G
| (data[i + 2] & 0x00F8) >> 3; //B
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBB -> IIIIIIII
2013-12-05 17:19:01 +08:00
void Texture2D::convertRGB888ToI8(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 2; i < l; i += 3)
2013-07-19 15:37:54 +08:00
{
*outData++ = (data[i] * 299 + data[i + 1] * 587 + data[i + 2] * 114 + 500) / 1000; //I = (R*299 + G*587 + B*114 + 500) / 1000
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> IIIIIIII
2013-12-05 17:19:01 +08:00
void Texture2D::convertRGBA8888ToI8(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 3; i < l; i += 4)
2013-07-19 15:37:54 +08:00
{
*outData++ = (data[i] * 299 + data[i + 1] * 587 + data[i + 2] * 114 + 500) / 1000; //I = (R*299 + G*587 + B*114 + 500) / 1000
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> AAAAAAAA
2013-12-05 17:19:01 +08:00
void Texture2D::convertRGBA8888ToA8(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen -3; i < l; i += 4)
2013-07-19 15:37:54 +08:00
{
*outData++ = data[i + 3]; //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBB -> IIIIIIIIAAAAAAAA
2013-12-05 17:19:01 +08:00
void Texture2D::convertRGB888ToAI88(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 2; i < l; i += 3)
2013-07-19 15:37:54 +08:00
{
*outData++ = (data[i] * 299 + data[i + 1] * 587 + data[i + 2] * 114 + 500) / 1000; //I = (R*299 + G*587 + B*114 + 500) / 1000
*outData++ = 0xFF;
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> IIIIIIIIAAAAAAAA
2013-12-05 17:19:01 +08:00
void Texture2D::convertRGBA8888ToAI88(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 3; i < l; i += 4)
2013-07-19 15:37:54 +08:00
{
*outData++ = (data[i] * 299 + data[i + 1] * 587 + data[i + 2] * 114 + 500) / 1000; //I = (R*299 + G*587 + B*114 + 500) / 1000
*outData++ = data[i + 3];
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBB -> RRRRGGGGBBBBAAAA
2013-12-05 17:19:01 +08:00
void Texture2D::convertRGB888ToRGBA4444(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
unsigned short* out16 = (unsigned short*)outData;
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 2; i < l; i += 3)
2013-07-19 15:37:54 +08:00
{
*out16++ = ((data[i] & 0x00F0) << 8 //R
| (data[i + 1] & 0x00F0) << 4 //G
| (data[i + 2] & 0xF0) //B
| 0x0F); //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> RRRRGGGGBBBBAAAA
2013-12-05 17:19:01 +08:00
void Texture2D::convertRGBA8888ToRGBA4444(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
unsigned short* out16 = (unsigned short*)outData;
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 3; i < l; i += 4)
2013-07-19 15:37:54 +08:00
{
*out16++ = (data[i] & 0x00F0) << 8 //R
| (data[i + 1] & 0x00F0) << 4 //G
| (data[i + 2] & 0xF0) //B
| (data[i + 3] & 0xF0) >> 4; //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBB -> RRRRRGGGGGBBBBBA
2013-12-05 17:19:01 +08:00
void Texture2D::convertRGB888ToRGB5A1(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
unsigned short* out16 = (unsigned short*)outData;
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 2; i < l; i += 3)
2013-07-19 15:37:54 +08:00
{
*out16++ = (data[i] & 0x00F8) << 8 //R
| (data[i + 1] & 0x00F8) << 3 //G
| (data[i + 2] & 0x00F8) >> 2 //B
| 0x01; //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// RRRRRRRRGGGGGGGGBBBBBBBB -> RRRRRGGGGGBBBBBA
2013-12-05 17:19:01 +08:00
void Texture2D::convertRGBA8888ToRGB5A1(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
2013-07-17 17:12:04 +08:00
{
unsigned short* out16 = (unsigned short*)outData;
2013-12-05 17:19:01 +08:00
for (ssize_t i = 0, l = dataLen - 2; i < l; i += 4)
2013-07-19 15:37:54 +08:00
{
*out16++ = (data[i] & 0x00F8) << 8 //R
| (data[i + 1] & 0x00F8) << 3 //G
| (data[i + 2] & 0x00F8) >> 2 //B
| (data[i + 3] & 0x0080) >> 7; //A
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
// conventer function end
//////////////////////////////////////////////////////////////////////////
Texture2D::Texture2D()
: _pixelFormat(Texture2D::PixelFormat::DEFAULT)
, _pixelsWide(0)
, _pixelsHigh(0)
, _name(0)
, _maxS(0.0)
, _maxT(0.0)
, _hasPremultipliedAlpha(false)
, _hasMipmaps(false)
, _shaderProgram(nullptr)
, _antialiasEnabled(true)
{
}
Texture2D::~Texture2D()
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
VolatileTextureMgr::removeTexture(this);
#endif
CCLOGINFO("deallocing Texture2D: %p - id=%u", this, _name);
CC_SAFE_RELEASE(_shaderProgram);
if(_name)
{
GL::deleteTexture(_name);
}
}
Texture2D::PixelFormat Texture2D::getPixelFormat() const
{
return _pixelFormat;
}
2013-12-05 17:19:01 +08:00
int Texture2D::getPixelsWide() const
{
return _pixelsWide;
}
2013-12-05 17:19:01 +08:00
int Texture2D::getPixelsHigh() const
{
return _pixelsHigh;
}
GLuint Texture2D::getName() const
{
return _name;
}
Size Texture2D::getContentSize() const
{
Size ret;
ret.width = _contentSize.width / CC_CONTENT_SCALE_FACTOR();
ret.height = _contentSize.height / CC_CONTENT_SCALE_FACTOR();
2012-04-08 22:37:58 +08:00
return ret;
}
const Size& Texture2D::getContentSizeInPixels()
2010-12-30 17:30:11 +08:00
{
return _contentSize;
2010-12-30 17:30:11 +08:00
}
GLfloat Texture2D::getMaxS() const
{
return _maxS;
}
void Texture2D::setMaxS(GLfloat maxS)
{
_maxS = maxS;
}
GLfloat Texture2D::getMaxT() const
{
return _maxT;
}
void Texture2D::setMaxT(GLfloat maxT)
{
_maxT = maxT;
}
2014-05-09 07:42:36 +08:00
GLProgram* Texture2D::getGLProgram() const
{
return _shaderProgram;
}
2014-05-09 07:42:36 +08:00
void Texture2D::setGLProgram(GLProgram* shaderProgram)
{
CC_SAFE_RETAIN(shaderProgram);
CC_SAFE_RELEASE(_shaderProgram);
_shaderProgram = shaderProgram;
}
void Texture2D::releaseData(void *data)
2010-12-30 17:30:11 +08:00
{
free(data);
}
void* Texture2D::keepData(void *data, unsigned int length)
2010-12-30 17:30:11 +08:00
{
2011-06-10 17:51:37 +08:00
CC_UNUSED_PARAM(length);
//The texture data mustn't be saved because it isn't a mutable texture.
return data;
2010-12-30 17:30:11 +08:00
}
bool Texture2D::hasPremultipliedAlpha() const
{
return _hasPremultipliedAlpha;
}
2013-12-05 17:19:01 +08:00
bool Texture2D::initWithData(const void *data, ssize_t dataLen, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh, const Size& contentSize)
{
CCASSERT(dataLen>0 && pixelsWide>0 && pixelsHigh>0, "Invalid size");
//if data has no mipmaps, we will consider it has only one mipmap
MipmapInfo mipmap;
mipmap.address = (unsigned char*)data;
2013-12-05 17:19:01 +08:00
mipmap.len = static_cast<int>(dataLen);
return initWithMipmaps(&mipmap, 1, pixelFormat, pixelsWide, pixelsHigh);
//update information
_contentSize = contentSize;
_maxS = contentSize.width / (float)(pixelsWide);
_maxT = contentSize.height / (float)(pixelsHigh);
}
2013-12-05 17:19:01 +08:00
bool Texture2D::initWithMipmaps(MipmapInfo* mipmaps, int mipmapsNum, PixelFormat pixelFormat, int pixelsWide, int pixelsHigh)
{
// cocos2d-x is currently calling this multiple times on the same Texture2D
// if the GL texture has already been created,it will be leaked in OpenGL
// For now, call deleteTexture if the texture already exists
if(_name)
{
GL::deleteTexture(_name);
_name = 0;
}
2013-07-19 15:37:54 +08:00
//the pixelFormat must be a certain value
CCASSERT(pixelFormat != PixelFormat::NONE && pixelFormat != PixelFormat::AUTO, "the \"pixelFormat\" param must be a certain value!");
CCASSERT(pixelsWide>0 && pixelsHigh>0, "Invalid size");
2013-07-19 15:37:54 +08:00
if (mipmapsNum <= 0)
{
CCLOG("cocos2d: WARNING: mipmap number is less than 1");
return false;
}
if(_pixelFormatInfoTables.find(pixelFormat) == _pixelFormatInfoTables.end())
{
CCLOG("cocos2d: WARNING: unsupported pixelformat: %lx", (unsigned long)pixelFormat );
return false;
}
const PixelFormatInfo& info = _pixelFormatInfoTables.at(pixelFormat);
2013-08-06 11:19:45 +08:00
if (info.compressed && !Configuration::getInstance()->supportsPVRTC()
&& !Configuration::getInstance()->supportsETC()
&& !Configuration::getInstance()->supportsS3TC()
&& !Configuration::getInstance()->supportsATITC())
{
CCLOG("cocos2d: WARNING: PVRTC/ETC images are not supported");
return false;
}
//Set the row align only when mipmapsNum == 1 and the data is uncompressed
if (mipmapsNum == 1 && !info.compressed)
{
unsigned int bytesPerRow = pixelsWide * info.bpp / 8;
if(bytesPerRow % 8 == 0)
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
}
else if(bytesPerRow % 4 == 0)
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
}
else if(bytesPerRow % 2 == 0)
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
}
else
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
}else
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
glGenTextures(1, &_name);
GL::bindTexture2D(_name);
if (mipmapsNum == 1)
{
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _antialiasEnabled ? GL_LINEAR : GL_NEAREST);
}else
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _antialiasEnabled ? GL_LINEAR_MIPMAP_NEAREST : GL_NEAREST_MIPMAP_NEAREST);
}
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _antialiasEnabled ? GL_LINEAR : GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
#if CC_ENABLE_CACHE_TEXTURE_DATA
if (_antialiasEnabled)
{
TexParams texParams = {(GLuint)(_hasMipmaps?GL_LINEAR_MIPMAP_NEAREST:GL_LINEAR),GL_LINEAR,GL_NONE,GL_NONE};
VolatileTextureMgr::setTexParameters(this, texParams);
}
else
{
TexParams texParams = {(GLuint)(_hasMipmaps?GL_NEAREST_MIPMAP_NEAREST:GL_NEAREST),GL_NEAREST,GL_NONE,GL_NONE};
VolatileTextureMgr::setTexParameters(this, texParams);
}
#endif
CHECK_GL_ERROR_DEBUG(); // clean possible GL error
// Specify OpenGL texture image
2013-12-05 17:19:01 +08:00
int width = pixelsWide;
int height = pixelsHigh;
for (int i = 0; i < mipmapsNum; ++i)
{
unsigned char *data = mipmaps[i].address;
GLsizei datalen = mipmaps[i].len;
if (info.compressed)
{
glCompressedTexImage2D(GL_TEXTURE_2D, i, info.internalFormat, (GLsizei)width, (GLsizei)height, 0, datalen, data);
}
else
{
glTexImage2D(GL_TEXTURE_2D, i, info.internalFormat, (GLsizei)width, (GLsizei)height, 0, info.format, info.type, data);
}
if (i > 0 && (width != height || ccNextPOT(width) != width ))
{
2013-12-06 16:32:06 +08:00
CCLOG("cocos2d: Texture2D. WARNING. Mipmap level %u is not squared. Texture won't render correctly. width=%d != height=%d", i, width, height);
}
GLenum err = glGetError();
if (err != GL_NO_ERROR)
{
CCLOG("cocos2d: Texture2D: Error uploading compressed texture level: %u . glError: 0x%04X", i, err);
return false;
}
width = MAX(width >> 1, 1);
height = MAX(height >> 1, 1);
}
_contentSize = Size((float)pixelsWide, (float)pixelsHigh);
_pixelsWide = pixelsWide;
_pixelsHigh = pixelsHigh;
_pixelFormat = pixelFormat;
_maxS = 1;
_maxT = 1;
_hasPremultipliedAlpha = false;
_hasMipmaps = mipmapsNum > 1;
2014-05-10 09:39:25 +08:00
// shader
setGLProgram(GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE));
return true;
}
bool Texture2D::updateWithData(const void *data,int offsetX,int offsetY,int width,int height)
{
if (_name)
{
GL::bindTexture2D(_name);
const PixelFormatInfo& info = _pixelFormatInfoTables.at(_pixelFormat);
glTexSubImage2D(GL_TEXTURE_2D,0,offsetX,offsetY,width,height,info.format, info.type,data);
return true;
}
return false;
}
std::string Texture2D::getDescription() const
{
return StringUtils::format("<Texture2D | Name = %u | Dimensions = %ld x %ld | Coordinates = (%.2f, %.2f)>", _name, (long)_pixelsWide, (long)_pixelsHigh, _maxS, _maxT);
}
// implementation Texture2D (Image)
bool Texture2D::initWithImage(Image *image)
{
return initWithImage(image, PixelFormat::NONE);
}
bool Texture2D::initWithImage(Image *image, PixelFormat format)
{
if (image == nullptr)
{
CCLOG("cocos2d: Texture2D. Can't create Texture. UIImage is nil");
return false;
}
2013-07-19 15:37:54 +08:00
int imageWidth = image->getWidth();
int imageHeight = image->getHeight();
2013-07-19 15:37:54 +08:00
Configuration *conf = Configuration::getInstance();
2013-07-19 15:37:54 +08:00
int maxTextureSize = conf->getMaxTextureSize();
if (imageWidth > maxTextureSize || imageHeight > maxTextureSize)
{
CCLOG("cocos2d: WARNING: Image (%u x %u) is bigger than the supported %u x %u", imageWidth, imageHeight, maxTextureSize, maxTextureSize);
2013-02-04 17:38:22 +08:00
return false;
}
2013-07-19 15:37:54 +08:00
unsigned char* tempData = image->getData();
Size imageSize = Size((float)imageWidth, (float)imageHeight);
PixelFormat pixelFormat = PixelFormat::NONE;
PixelFormat renderFormat = image->getRenderFormat();
size_t tempDataLen = image->getDataLen();
2013-07-19 15:37:54 +08:00
if (image->getNumberOfMipmaps() > 1)
2013-07-19 15:37:54 +08:00
{
if (format != PixelFormat::NONE)
{
CCLOG("cocos2d: WARNING: This image has more than 1 mipmaps and we will not convert the data format");
}
initWithMipmaps(image->getMipmaps(), image->getNumberOfMipmaps(), image->getRenderFormat(), imageWidth, imageHeight);
2013-08-06 11:19:45 +08:00
return true;
}
else if (image->isCompressed())
2013-07-19 15:37:54 +08:00
{
if (format != PixelFormat::NONE)
{
CCLOG("cocos2d: WARNING: This image is compressed and we cann't convert it for now");
}
initWithData(tempData, tempDataLen, image->getRenderFormat(), imageWidth, imageHeight, imageSize);
return true;
2013-07-19 15:37:54 +08:00
}
else
{
// compute pixel format
if (format != PixelFormat::NONE)
{
pixelFormat = format;
}else
{
pixelFormat = g_defaultAlphaPixelFormat;
}
2013-07-19 15:37:54 +08:00
unsigned char* outTempData = nullptr;
2013-12-05 17:19:01 +08:00
ssize_t outTempDataLen = 0;
2013-07-19 15:37:54 +08:00
pixelFormat = convertDataToFormat(tempData, tempDataLen, renderFormat, pixelFormat, &outTempData, &outTempDataLen);
2013-07-19 15:37:54 +08:00
initWithData(outTempData, outTempDataLen, pixelFormat, imageWidth, imageHeight, imageSize);
2013-07-19 15:37:54 +08:00
if (outTempData != nullptr && outTempData != tempData)
{
2013-07-19 15:37:54 +08:00
free(outTempData);
}
2013-07-19 15:37:54 +08:00
// set the premultiplied tag
if (!image->hasPremultipliedAlpha())
{
if (image->getFileType() == Image::Format::PVR)
{
_hasPremultipliedAlpha = _PVRHaveAlphaPremultiplied;
}else
{
CCLOG("wanning: We cann't find the data is premultiplied or not, we will assume it's false.");
_hasPremultipliedAlpha = false;
}
}else
{
_hasPremultipliedAlpha = image->isPremultipliedAlpha();
}
return true;
}
}
2013-12-05 17:19:01 +08:00
Texture2D::PixelFormat Texture2D::convertI8ToFormat(const unsigned char* data, ssize_t dataLen, PixelFormat format, unsigned char** outData, ssize_t* outDataLen)
2013-07-19 15:37:54 +08:00
{
switch (format)
{
case PixelFormat::RGBA8888:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen*4;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertI8ToRGBA8888(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::RGB888:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen*3;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertI8ToRGB888(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::RGB565:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen*2;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertI8ToRGB565(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::AI88:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen*2;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertI8ToAI88(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::RGBA4444:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen*2;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertI8ToRGBA4444(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::RGB5A1:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen*2;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertI8ToRGB5A1(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
default:
// unsupport convertion or don't need to convert
if (format != PixelFormat::AUTO && format != PixelFormat::I8)
2013-07-19 15:37:54 +08:00
{
CCLOG("Can not convert image format PixelFormat::I8 to format ID:%d, we will use it's origin format PixelFormat::I8", format);
2013-07-19 15:37:54 +08:00
}
2013-07-19 15:37:54 +08:00
*outData = (unsigned char*)data;
*outDataLen = dataLen;
return PixelFormat::I8;
2013-07-19 15:37:54 +08:00
}
return format;
}
2013-12-05 17:19:01 +08:00
Texture2D::PixelFormat Texture2D::convertAI88ToFormat(const unsigned char* data, ssize_t dataLen, PixelFormat format, unsigned char** outData, ssize_t* outDataLen)
2013-07-19 15:37:54 +08:00
{
switch (format)
{
case PixelFormat::RGBA8888:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen*2;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertAI88ToRGBA8888(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::RGB888:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen/2*3;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertAI88ToRGB888(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::RGB565:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertAI88ToRGB565(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::A8:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen/2;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertAI88ToA8(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::I8:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen/2;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertAI88ToI8(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::RGBA4444:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertAI88ToRGBA4444(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::RGB5A1:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertAI88ToRGB5A1(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
default:
// unsupport convertion or don't need to convert
if (format != PixelFormat::AUTO && format != PixelFormat::AI88)
2013-07-19 15:37:54 +08:00
{
CCLOG("Can not convert image format PixelFormat::AI88 to format ID:%d, we will use it's origin format PixelFormat::AI88", format);
2013-07-19 15:37:54 +08:00
}
*outData = (unsigned char*)data;
*outDataLen = dataLen;
return PixelFormat::AI88;
2013-07-19 15:37:54 +08:00
break;
}
return format;
}
2013-12-05 17:19:01 +08:00
Texture2D::PixelFormat Texture2D::convertRGB888ToFormat(const unsigned char* data, ssize_t dataLen, PixelFormat format, unsigned char** outData, ssize_t* outDataLen)
2013-07-19 15:37:54 +08:00
{
switch (format)
{
case PixelFormat::RGBA8888:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen/3*4;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertRGB888ToRGBA8888(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::RGB565:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen/3*2;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertRGB888ToRGB565(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::I8:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen/3;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertRGB888ToI8(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::AI88:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen/3*2;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertRGB888ToAI88(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::RGBA4444:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen/3*2;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertRGB888ToRGBA4444(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::RGB5A1:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertRGB888ToRGB5A1(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
default:
// unsupport convertion or don't need to convert
if (format != PixelFormat::AUTO && format != PixelFormat::RGB888)
2013-07-19 15:37:54 +08:00
{
CCLOG("Can not convert image format PixelFormat::RGB888 to format ID:%d, we will use it's origin format PixelFormat::RGB888", format);
2013-07-19 15:37:54 +08:00
}
*outData = (unsigned char*)data;
*outDataLen = dataLen;
return PixelFormat::RGB888;
2013-07-19 15:37:54 +08:00
}
return format;
}
2013-12-05 17:19:01 +08:00
Texture2D::PixelFormat Texture2D::convertRGBA8888ToFormat(const unsigned char* data, ssize_t dataLen, PixelFormat format, unsigned char** outData, ssize_t* outDataLen)
2013-07-19 15:37:54 +08:00
{
switch (format)
{
case PixelFormat::RGB888:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen/4*3;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertRGBA8888ToRGB888(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::RGB565:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen/2;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertRGBA8888ToRGB565(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::A8:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen/4;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertRGBA8888ToA8(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::I8:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen/4;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertRGBA8888ToI8(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::AI88:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen/2;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertRGBA8888ToAI88(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::RGBA4444:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen/2;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertRGBA8888ToRGBA4444(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
case PixelFormat::RGB5A1:
2013-07-19 15:37:54 +08:00
*outDataLen = dataLen/2;
*outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
convertRGBA8888ToRGB5A1(data, dataLen, *outData);
2013-07-19 15:37:54 +08:00
break;
default:
// unsupport convertion or don't need to convert
if (format != PixelFormat::AUTO && format != PixelFormat::RGBA8888)
2013-07-19 15:37:54 +08:00
{
CCLOG("Can not convert image format PixelFormat::RGBA8888 to format ID:%d, we will use it's origin format PixelFormat::RGBA8888", format);
2013-07-19 15:37:54 +08:00
}
*outData = (unsigned char*)data;
*outDataLen = dataLen;
return PixelFormat::RGBA8888;
2013-07-19 15:37:54 +08:00
}
return format;
}
2013-07-17 17:12:04 +08:00
/*
convert map:
1.PixelFormat::RGBA8888
2.PixelFormat::RGB888
3.PixelFormat::RGB565
4.PixelFormat::A8
5.PixelFormat::I8
6.PixelFormat::AI88
7.PixelFormat::RGBA4444
8.PixelFormat::RGB5A1
2013-07-17 17:12:04 +08:00
gray(5) -> 1235678
gray alpha(6) -> 12345678
rgb(2) -> 1235678
rgba(1) -> 12345678
*/
2013-12-05 17:19:01 +08:00
Texture2D::PixelFormat Texture2D::convertDataToFormat(const unsigned char* data, ssize_t dataLen, PixelFormat originFormat, PixelFormat format, unsigned char** outData, ssize_t* outDataLen)
2013-07-19 15:37:54 +08:00
{
switch (originFormat)
2013-07-19 15:37:54 +08:00
{
case PixelFormat::I8:
return convertI8ToFormat(data, dataLen, format, outData, outDataLen);
case PixelFormat::AI88:
return convertAI88ToFormat(data, dataLen, format, outData, outDataLen);
case PixelFormat::RGB888:
return convertRGB888ToFormat(data, dataLen, format, outData, outDataLen);
case PixelFormat::RGBA8888:
return convertRGBA8888ToFormat(data, dataLen, format, outData, outDataLen);
default:
CCLOG("unsupport convert for format %d to format %d", originFormat, format);
*outData = (unsigned char*)data;
*outDataLen = dataLen;
return originFormat;
}
2010-07-26 17:32:23 +08:00
}
// implementation Texture2D (Text)
2014-04-30 08:37:36 +08:00
bool Texture2D::initWithString(const char *text, const std::string& fontName, float fontSize, const Size& dimensions/* = Size(0, 0)*/, TextHAlignment hAlignment/* = TextHAlignment::CENTER */, TextVAlignment vAlignment/* = TextVAlignment::TOP */)
2013-04-26 09:22:26 +08:00
{
2013-07-10 04:21:43 +08:00
FontDefinition tempDef;
2013-07-10 04:21:43 +08:00
tempDef._shadow._shadowEnabled = false;
tempDef._stroke._strokeEnabled = false;
2014-04-30 08:37:36 +08:00
tempDef._fontName = fontName;
2013-07-10 04:21:43 +08:00
tempDef._fontSize = fontSize;
tempDef._dimensions = dimensions;
tempDef._alignment = hAlignment;
tempDef._vertAlignment = vAlignment;
tempDef._fontFillColor = Color3B::WHITE;
return initWithString(text, tempDef);
}
bool Texture2D::initWithString(const char *text, const FontDefinition& textDefinition)
{
if(!text || 0 == strlen(text))
{
return false;
}
#if CC_ENABLE_CACHE_TEXTURE_DATA
// cache the texture data
VolatileTextureMgr::addStringTexture(this, text, textDefinition);
#endif
2013-07-10 04:21:43 +08:00
bool ret = false;
Device::TextAlign align;
2013-04-26 09:22:26 +08:00
if (TextVAlignment::TOP == textDefinition._vertAlignment)
2013-07-10 04:21:43 +08:00
{
align = (TextHAlignment::CENTER == textDefinition._alignment) ? Device::TextAlign::TOP
: (TextHAlignment::LEFT == textDefinition._alignment) ? Device::TextAlign::TOP_LEFT : Device::TextAlign::TOP_RIGHT;
2013-07-10 04:21:43 +08:00
}
else if (TextVAlignment::CENTER == textDefinition._vertAlignment)
2013-07-10 04:21:43 +08:00
{
align = (TextHAlignment::CENTER == textDefinition._alignment) ? Device::TextAlign::CENTER
: (TextHAlignment::LEFT == textDefinition._alignment) ? Device::TextAlign::LEFT : Device::TextAlign::RIGHT;
2013-07-10 04:21:43 +08:00
}
else if (TextVAlignment::BOTTOM == textDefinition._vertAlignment)
2013-07-10 04:21:43 +08:00
{
align = (TextHAlignment::CENTER == textDefinition._alignment) ? Device::TextAlign::BOTTOM
: (TextHAlignment::LEFT == textDefinition._alignment) ? Device::TextAlign::BOTTOM_LEFT : Device::TextAlign::BOTTOM_RIGHT;
2013-07-10 04:21:43 +08:00
}
else
{
CCASSERT(false, "Not supported alignment format!");
2013-07-10 04:21:43 +08:00
return false;
}
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) && (CC_TARGET_PLATFORM != CC_PLATFORM_IOS)
2014-03-26 14:11:40 +08:00
CCASSERT(textDefinition._stroke._strokeEnabled == false, "Currently stroke only supported on iOS and Android!");
#endif
PixelFormat pixelFormat = g_defaultAlphaPixelFormat;
unsigned char* outTempData = nullptr;
ssize_t outTempDataLen = 0;
int imageWidth;
int imageHeight;
auto textDef = textDefinition;
auto contentScaleFactor = CC_CONTENT_SCALE_FACTOR();
textDef._fontSize *= contentScaleFactor;
textDef._dimensions.width *= contentScaleFactor;
textDef._dimensions.height *= contentScaleFactor;
textDef._stroke._strokeSize *= contentScaleFactor;
2014-03-26 14:11:40 +08:00
textDef._shadow._shadowEnabled = false;
Data outData = Device::getTextureDataForText(text, textDef, align, imageWidth, imageHeight, _hasPremultipliedAlpha);
if(outData.isNull())
{
return false;
}
Size imageSize = Size((float)imageWidth, (float)imageHeight);
pixelFormat = convertDataToFormat(outData.getBytes(), imageWidth*imageHeight*4, PixelFormat::RGBA8888, pixelFormat, &outTempData, &outTempDataLen);
ret = initWithData(outTempData, outTempDataLen, pixelFormat, imageWidth, imageHeight, imageSize);
if (outTempData != nullptr && outTempData != outData.getBytes())
{
free(outTempData);
}
return ret;
}
// implementation Texture2D (Drawing)
void Texture2D::drawAtPoint(const Vector2& point)
{
2013-07-10 04:21:43 +08:00
GLfloat coordinates[] = {
0.0f, _maxT,
_maxS,_maxT,
0.0f, 0.0f,
_maxS,0.0f };
GLfloat width = (GLfloat)_pixelsWide * _maxS,
height = (GLfloat)_pixelsHigh * _maxT;
GLfloat vertices[] = {
point.x, point.y,
width + point.x, point.y,
point.x, height + point.y,
width + point.x, height + point.y };
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORDS );
_shaderProgram->use();
_shaderProgram->setUniformsForBuiltins();
GL::bindTexture2D( _name );
2013-04-09 12:08:34 +08:00
#ifdef EMSCRIPTEN
setGLBufferData(vertices, 8 * sizeof(GLfloat), 0);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
2013-04-09 12:08:34 +08:00
setGLBufferData(coordinates, 8 * sizeof(GLfloat), 1);
2014-05-09 03:34:26 +08:00
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, 0);
2013-04-09 12:08:34 +08:00
#else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
2014-05-09 03:34:26 +08:00
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, coordinates);
2013-04-09 12:08:34 +08:00
#endif // EMSCRIPTEN
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
void Texture2D::drawInRect(const Rect& rect)
{
GLfloat coordinates[] = {
0.0f, _maxT,
_maxS,_maxT,
0.0f, 0.0f,
_maxS,0.0f };
GLfloat vertices[] = { rect.origin.x, rect.origin.y, /*0.0f,*/
rect.origin.x + rect.size.width, rect.origin.y, /*0.0f,*/
rect.origin.x, rect.origin.y + rect.size.height, /*0.0f,*/
rect.origin.x + rect.size.width, rect.origin.y + rect.size.height, /*0.0f*/ };
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORDS );
_shaderProgram->use();
_shaderProgram->setUniformsForBuiltins();
GL::bindTexture2D( _name );
2013-04-09 12:08:34 +08:00
#ifdef EMSCRIPTEN
setGLBufferData(vertices, 8 * sizeof(GLfloat), 0);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
2013-04-09 12:08:34 +08:00
setGLBufferData(coordinates, 8 * sizeof(GLfloat), 1);
2014-05-09 03:34:26 +08:00
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, 0);
2013-04-09 12:08:34 +08:00
#else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
2014-05-09 03:34:26 +08:00
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, coordinates);
2013-04-09 12:08:34 +08:00
#endif // EMSCRIPTEN
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
void Texture2D::PVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied)
{
_PVRHaveAlphaPremultiplied = haveAlphaPremultiplied;
}
//
// Use to apply MIN/MAG filter
//
// implementation Texture2D (GLFilter)
void Texture2D::generateMipmap()
{
2013-12-05 17:19:01 +08:00
CCASSERT(_pixelsWide == ccNextPOT(_pixelsWide) && _pixelsHigh == ccNextPOT(_pixelsHigh), "Mipmap texture only works in POT textures");
GL::bindTexture2D( _name );
glGenerateMipmap(GL_TEXTURE_2D);
_hasMipmaps = true;
#if CC_ENABLE_CACHE_TEXTURE_DATA
VolatileTextureMgr::setHasMipmaps(this, _hasMipmaps);
#endif
}
bool Texture2D::hasMipmaps() const
{
return _hasMipmaps;
}
void Texture2D::setTexParameters(const TexParams &texParams)
{
2013-12-05 17:19:01 +08:00
CCASSERT((_pixelsWide == ccNextPOT(_pixelsWide) || texParams.wrapS == GL_CLAMP_TO_EDGE) &&
(_pixelsHigh == ccNextPOT(_pixelsHigh) || texParams.wrapT == GL_CLAMP_TO_EDGE),
"GL_CLAMP_TO_EDGE should be used in NPOT dimensions");
GL::bindTexture2D( _name );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, texParams.minFilter );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, texParams.magFilter );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, texParams.wrapS );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, texParams.wrapT );
#if CC_ENABLE_CACHE_TEXTURE_DATA
VolatileTextureMgr::setTexParameters(this, texParams);
#endif
}
void Texture2D::setAliasTexParameters()
{
if (! _antialiasEnabled)
{
return;
}
_antialiasEnabled = false;
if (_name == 0)
{
return;
}
GL::bindTexture2D( _name );
if( ! _hasMipmaps )
{
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
}
else
{
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST );
}
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
#if CC_ENABLE_CACHE_TEXTURE_DATA
TexParams texParams = {(GLuint)(_hasMipmaps?GL_NEAREST_MIPMAP_NEAREST:GL_NEAREST),GL_NEAREST,GL_NONE,GL_NONE};
VolatileTextureMgr::setTexParameters(this, texParams);
#endif
}
void Texture2D::setAntiAliasTexParameters()
{
if ( _antialiasEnabled )
{
return;
}
_antialiasEnabled = true;
if (_name == 0)
{
return;
}
GL::bindTexture2D( _name );
if( ! _hasMipmaps )
{
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
}
else
{
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
}
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
#if CC_ENABLE_CACHE_TEXTURE_DATA
TexParams texParams = {(GLuint)(_hasMipmaps?GL_LINEAR_MIPMAP_NEAREST:GL_LINEAR),GL_LINEAR,GL_NONE,GL_NONE};
VolatileTextureMgr::setTexParameters(this, texParams);
#endif
}
const char* Texture2D::getStringForFormat() const
{
switch (_pixelFormat)
{
case Texture2D::PixelFormat::RGBA8888:
return "RGBA8888";
case Texture2D::PixelFormat::RGB888:
return "RGB888";
case Texture2D::PixelFormat::RGB565:
return "RGB565";
case Texture2D::PixelFormat::RGBA4444:
return "RGBA4444";
case Texture2D::PixelFormat::RGB5A1:
return "RGB5A1";
case Texture2D::PixelFormat::AI88:
return "AI88";
case Texture2D::PixelFormat::A8:
return "A8";
case Texture2D::PixelFormat::I8:
return "I8";
case Texture2D::PixelFormat::PVRTC4:
return "PVRTC4";
case Texture2D::PixelFormat::PVRTC2:
return "PVRTC2";
default:
CCASSERT(false , "unrecognized pixel format");
CCLOG("stringForFormat: %ld, cannot give useful result", (long)_pixelFormat);
break;
}
return nullptr;
}
//
// Texture options for images that contains alpha
//
// implementation Texture2D (PixelFormat)
void Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat format)
{
g_defaultAlphaPixelFormat = format;
}
Texture2D::PixelFormat Texture2D::getDefaultAlphaPixelFormat()
{
return g_defaultAlphaPixelFormat;
}
unsigned int Texture2D::getBitsPerPixelForFormat(Texture2D::PixelFormat format) const
{
if (format == PixelFormat::NONE)
{
return 0;
}
return _pixelFormatInfoTables.at(format).bpp;
}
unsigned int Texture2D::getBitsPerPixelForFormat() const
{
return this->getBitsPerPixelForFormat(_pixelFormat);
}
const Texture2D::PixelFormatInfoMap& Texture2D::getPixelFormatInfoMap()
{
return _pixelFormatInfoTables;
}
2012-04-18 18:43:45 +08:00
NS_CC_END