2010-08-02 10:58:00 +08:00
|
|
|
/****************************************************************************
|
2012-06-08 14:11:48 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2008 Apple Inc. All Rights Reserved.
|
2010-08-02 10:58:00 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
2010-07-15 18:15:00 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
#include "CCTexture2D.h"
|
2010-07-15 18:15:00 +08:00
|
|
|
#include "ccConfig.h"
|
|
|
|
#include "ccMacros.h"
|
2010-07-20 14:29:18 +08:00
|
|
|
#include "CCConfiguration.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "platform/CCImage.h"
|
2010-12-30 17:30:11 +08:00
|
|
|
#include "CCGL.h"
|
|
|
|
#include "support/ccUtils.h"
|
2011-01-10 17:54:44 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "textures/CCTexturePVR.h"
|
2013-05-27 14:42:22 +08:00
|
|
|
#include "textures/CCTextureETC.h"
|
2011-11-28 17:28:43 +08:00
|
|
|
#include "CCDirector.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "shaders/CCGLProgram.h"
|
|
|
|
#include "shaders/ccGLStateCache.h"
|
|
|
|
#include "shaders/CCShaderCache.h"
|
2010-08-31 16:53:25 +08:00
|
|
|
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2011-04-06 16:29:58 +08:00
|
|
|
#include "CCTextureCache.h"
|
2011-01-10 17:54:44 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2010-07-15 18:15:00 +08:00
|
|
|
|
|
|
|
//CLASS IMPLEMENTATIONS:
|
|
|
|
|
|
|
|
// 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)
|
2013-07-26 04:36:19 +08:00
|
|
|
static Texture2D::PixelFormat g_defaultAlphaPixelFormat = Texture2D::PixelFormat::DEFAULT;
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2013-07-29 14:07:57 +08:00
|
|
|
static bool _PVRHaveAlphaPremultiplied = false;
|
2011-12-20 14:59:15 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D::Texture2D()
|
2013-07-29 14:07:57 +08:00
|
|
|
: _pixelFormat(Texture2D::PixelFormat::DEFAULT)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _pixelsWide(0)
|
|
|
|
, _pixelsHigh(0)
|
|
|
|
, _name(0)
|
|
|
|
, _maxS(0.0)
|
|
|
|
, _maxT(0.0)
|
|
|
|
, _hasPremultipliedAlpha(false)
|
|
|
|
, _hasMipmaps(false)
|
|
|
|
, _shaderProgram(NULL)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D::~Texture2D()
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2011-01-10 17:54:44 +08:00
|
|
|
VolatileTexture::removeTexture(this);
|
|
|
|
#endif
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
CCLOGINFO("cocos2d: deallocing Texture2D %u.", _name);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_shaderProgram);
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_name)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::deleteTexture(_name);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-25 19:52:44 +08:00
|
|
|
Texture2D::PixelFormat Texture2D::getPixelFormat() const
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _pixelFormat;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int Texture2D::getPixelsWide() const
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _pixelsWide;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int Texture2D::getPixelsHigh() const
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _pixelsHigh;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
GLuint Texture2D::getName() const
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _name;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Size Texture2D::getContentSize() const
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Size ret;
|
2013-06-15 14:03:30 +08:00
|
|
|
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;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const Size& Texture2D::getContentSizeInPixels()
|
2010-12-30 17:30:11 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _contentSize;
|
2010-12-30 17:30:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-23 14:05:05 +08:00
|
|
|
GLfloat Texture2D::getMaxS() const
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _maxS;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Texture2D::setMaxS(GLfloat maxS)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_maxS = maxS;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-23 14:05:05 +08:00
|
|
|
GLfloat Texture2D::getMaxT() const
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _maxT;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Texture2D::setMaxT(GLfloat maxT)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_maxT = maxT;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-23 14:05:05 +08:00
|
|
|
GLProgram* Texture2D::getShaderProgram() const
|
2012-03-14 14:55:17 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _shaderProgram;
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Texture2D::setShaderProgram(GLProgram* pShaderProgram)
|
2012-03-14 14:55:17 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RETAIN(pShaderProgram);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_shaderProgram);
|
|
|
|
_shaderProgram = pShaderProgram;
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Texture2D::releaseData(void *data)
|
2010-12-30 17:30:11 +08:00
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
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);
|
2012-09-17 15:02:24 +08:00
|
|
|
//The texture data mustn't be saved because it isn't a mutable texture.
|
2012-04-19 14:35:52 +08:00
|
|
|
return data;
|
2010-12-30 17:30:11 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 08:44:41 +08:00
|
|
|
bool Texture2D::hasPremultipliedAlpha() const
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _hasPremultipliedAlpha;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-25 19:52:44 +08:00
|
|
|
bool Texture2D::initWithData(const void *data, Texture2D::PixelFormat pixelFormat, unsigned int pixelsWide, unsigned int pixelsHigh, const Size& contentSize)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-05-19 23:53:59 +08:00
|
|
|
unsigned int bitsPerPixel;
|
|
|
|
//Hack: bitsPerPixelForFormat returns wrong number for RGB_888 textures. See function.
|
2013-07-26 04:36:19 +08:00
|
|
|
if(pixelFormat == Texture2D::PixelFormat::RGB888)
|
2013-05-19 23:53:59 +08:00
|
|
|
{
|
|
|
|
bitsPerPixel = 24;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-19 14:57:54 +08:00
|
|
|
bitsPerPixel = getBitsPerPixelForFormat(pixelFormat);
|
2013-05-19 23:53:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int bytesPerRow = pixelsWide * bitsPerPixel / 8;
|
2013-05-12 23:08:33 +08:00
|
|
|
|
2013-05-19 23:53:59 +08:00
|
|
|
if(bytesPerRow % 8 == 0)
|
2013-05-19 20:12:39 +08:00
|
|
|
{
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
|
|
|
|
}
|
2013-05-19 23:53:59 +08:00
|
|
|
else if(bytesPerRow % 4 == 0)
|
2013-05-12 23:08:33 +08:00
|
|
|
{
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
}
|
2013-05-19 23:53:59 +08:00
|
|
|
else if(bytesPerRow % 2 == 0)
|
2013-05-19 20:12:39 +08:00
|
|
|
{
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
|
|
|
|
}
|
2012-06-13 16:20:58 +08:00
|
|
|
else
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
2013-05-12 23:08:33 +08:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
|
|
|
|
2013-05-19 20:17:48 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glGenTextures(1, &_name);
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2D(_name);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-13 16:20:58 +08:00
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
|
2012-06-08 14:11:48 +08:00
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Specify OpenGL texture image
|
|
|
|
|
|
|
|
switch(pixelFormat)
|
|
|
|
{
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::RGBA8888:
|
2012-04-19 14:35:52 +08:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::RGB888:
|
2012-04-19 14:35:52 +08:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::RGBA4444:
|
2012-04-19 14:35:52 +08:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::RGB5A1:
|
2012-04-19 14:35:52 +08:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, data);
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::RGB565:
|
2012-04-19 14:35:52 +08:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::AI88:
|
2012-04-19 14:35:52 +08:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, data);
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::A8:
|
2012-04-19 14:35:52 +08:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_ALPHA, GL_UNSIGNED_BYTE, data);
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::I8:
|
2012-04-19 14:35:52 +08:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
|
|
|
|
break;
|
|
|
|
default:
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(0, "NSInternalInconsistencyException");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_contentSize = contentSize;
|
|
|
|
_pixelsWide = pixelsWide;
|
|
|
|
_pixelsHigh = pixelsHigh;
|
|
|
|
_pixelFormat = pixelFormat;
|
|
|
|
_maxS = contentSize.width / (float)(pixelsWide);
|
|
|
|
_maxT = contentSize.height / (float)(pixelsHigh);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_hasPremultipliedAlpha = false;
|
|
|
|
_hasMipmaps = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-25 17:48:22 +08:00
|
|
|
setShaderProgram(ShaderCache::getInstance()->programForKey(GLProgram::SHADER_NAME_POSITION_TEXTURE));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-04 08:44:41 +08:00
|
|
|
const char* Texture2D::description(void) const
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return String::createWithFormat("<Texture2D | Name = %u | Dimensions = %u x %u | Coordinates = (%.2f, %.2f)>", _name, _pixelsWide, _pixelsHigh, _maxS, _maxT)->getCString();
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation Texture2D (Image)
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Texture2D::initWithImage(Image *uiImage)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
if (uiImage == NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
CCLOG("cocos2d: Texture2D. Can't create Texture. UIImage is nil");
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-24 15:02:18 +08:00
|
|
|
|
|
|
|
unsigned int imageWidth = uiImage->getWidth();
|
|
|
|
unsigned int imageHeight = uiImage->getHeight();
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Configuration *conf = Configuration::getInstance();
|
2012-08-08 18:39:33 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
unsigned maxTextureSize = conf->getMaxTextureSize();
|
2012-04-24 15:02:18 +08:00
|
|
|
if (imageWidth > maxTextureSize || imageHeight > maxTextureSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
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;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-08-08 18:39:33 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// always load premultiplied images
|
2012-04-24 15:02:18 +08:00
|
|
|
return initPremultipliedATextureWithImage(uiImage, imageWidth, imageHeight);
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
2012-08-08 18:39:33 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Texture2D::initPremultipliedATextureWithImage(Image *image, unsigned int width, unsigned int height)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-04-26 17:36:32 +08:00
|
|
|
unsigned char* tempData = image->getData();
|
2013-05-01 07:36:14 +08:00
|
|
|
unsigned int* inPixel32 = NULL;
|
2012-04-26 17:36:32 +08:00
|
|
|
unsigned char* inPixel8 = NULL;
|
|
|
|
unsigned short* outPixel16 = NULL;
|
|
|
|
bool hasAlpha = image->hasAlpha();
|
2013-07-12 14:30:26 +08:00
|
|
|
Size imageSize = Size((float)(image->getWidth()), (float)(image->getHeight()));
|
2013-07-25 19:52:44 +08:00
|
|
|
Texture2D::PixelFormat pixelFormat;
|
2012-04-26 17:36:32 +08:00
|
|
|
size_t bpp = image->getBitsPerComponent();
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2011-04-20 15:34:33 +08:00
|
|
|
// compute pixel format
|
2013-05-01 07:36:14 +08:00
|
|
|
if (hasAlpha)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-05-01 07:36:14 +08:00
|
|
|
pixelFormat = g_defaultAlphaPixelFormat;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (bpp >= 8)
|
|
|
|
{
|
2013-07-26 04:36:19 +08:00
|
|
|
pixelFormat = Texture2D::PixelFormat::RGB888;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-26 17:36:32 +08:00
|
|
|
else
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 04:36:19 +08:00
|
|
|
pixelFormat = Texture2D::PixelFormat::RGB565;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-26 17:36:32 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-26 17:36:32 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Repack the pixel data into the right format
|
2012-04-26 17:36:32 +08:00
|
|
|
unsigned int length = width * height;
|
2013-05-01 07:36:14 +08:00
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
if (pixelFormat == Texture2D::PixelFormat::RGB565)
|
2012-04-26 17:36:32 +08:00
|
|
|
{
|
2012-04-27 15:53:29 +08:00
|
|
|
if (hasAlpha)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-04-27 15:53:29 +08:00
|
|
|
// Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB"
|
|
|
|
|
|
|
|
tempData = new unsigned char[width * height * 2];
|
|
|
|
outPixel16 = (unsigned short*)tempData;
|
|
|
|
inPixel32 = (unsigned int*)image->getData();
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < length; ++i, ++inPixel32)
|
|
|
|
{
|
|
|
|
*outPixel16++ =
|
|
|
|
((((*inPixel32 >> 0) & 0xFF) >> 3) << 11) | // R
|
|
|
|
((((*inPixel32 >> 8) & 0xFF) >> 2) << 5) | // G
|
|
|
|
((((*inPixel32 >> 16) & 0xFF) >> 3) << 0); // B
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-27 15:53:29 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Convert "RRRRRRRRRGGGGGGGGBBBBBBBB" to "RRRRRGGGGGGBBBBB"
|
|
|
|
|
|
|
|
tempData = new unsigned char[width * height * 2];
|
|
|
|
outPixel16 = (unsigned short*)tempData;
|
|
|
|
inPixel8 = (unsigned char*)image->getData();
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < length; ++i)
|
|
|
|
{
|
|
|
|
*outPixel16++ =
|
|
|
|
(((*inPixel8++ & 0xFF) >> 3) << 11) | // R
|
|
|
|
(((*inPixel8++ & 0xFF) >> 2) << 5) | // G
|
|
|
|
(((*inPixel8++ & 0xFF) >> 3) << 0); // B
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-26 04:36:19 +08:00
|
|
|
else if (pixelFormat == Texture2D::PixelFormat::RGBA4444)
|
2012-04-26 17:36:32 +08:00
|
|
|
{
|
|
|
|
// Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRGGGGBBBBAAAA"
|
|
|
|
|
|
|
|
inPixel32 = (unsigned int*)image->getData();
|
|
|
|
tempData = new unsigned char[width * height * 2];
|
2012-04-19 14:35:52 +08:00
|
|
|
outPixel16 = (unsigned short*)tempData;
|
2012-04-26 17:36:32 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
for(unsigned int i = 0; i < length; ++i, ++inPixel32)
|
|
|
|
{
|
|
|
|
*outPixel16++ =
|
|
|
|
((((*inPixel32 >> 0) & 0xFF) >> 4) << 12) | // R
|
2012-04-26 17:36:32 +08:00
|
|
|
((((*inPixel32 >> 8) & 0xFF) >> 4) << 8) | // G
|
2012-04-19 14:35:52 +08:00
|
|
|
((((*inPixel32 >> 16) & 0xFF) >> 4) << 4) | // B
|
2012-04-26 17:36:32 +08:00
|
|
|
((((*inPixel32 >> 24) & 0xFF) >> 4) << 0); // A
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-26 04:36:19 +08:00
|
|
|
else if (pixelFormat == Texture2D::PixelFormat::RGB5A1)
|
2012-04-26 17:36:32 +08:00
|
|
|
{
|
|
|
|
// Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGBBBBBA"
|
|
|
|
inPixel32 = (unsigned int*)image->getData();
|
|
|
|
tempData = new unsigned char[width * height * 2];
|
2012-04-19 14:35:52 +08:00
|
|
|
outPixel16 = (unsigned short*)tempData;
|
2012-04-26 17:36:32 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
for(unsigned int i = 0; i < length; ++i, ++inPixel32)
|
|
|
|
{
|
|
|
|
*outPixel16++ =
|
|
|
|
((((*inPixel32 >> 0) & 0xFF) >> 3) << 11) | // R
|
2012-04-26 17:36:32 +08:00
|
|
|
((((*inPixel32 >> 8) & 0xFF) >> 3) << 6) | // G
|
2012-04-19 14:35:52 +08:00
|
|
|
((((*inPixel32 >> 16) & 0xFF) >> 3) << 1) | // B
|
2012-04-26 17:36:32 +08:00
|
|
|
((((*inPixel32 >> 24) & 0xFF) >> 7) << 0); // A
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-26 04:36:19 +08:00
|
|
|
else if (pixelFormat == Texture2D::PixelFormat::A8)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-04-27 15:53:29 +08:00
|
|
|
// Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "AAAAAAAA"
|
|
|
|
inPixel32 = (unsigned int*)image->getData();
|
|
|
|
tempData = new unsigned char[width * height];
|
|
|
|
unsigned char *outPixel8 = tempData;
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < length; ++i, ++inPixel32)
|
|
|
|
{
|
|
|
|
*outPixel8++ = (*inPixel32 >> 24) & 0xFF; // A
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
if (hasAlpha && pixelFormat == Texture2D::PixelFormat::RGB888)
|
2012-04-27 15:53:29 +08:00
|
|
|
{
|
|
|
|
// Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRRRRGGGGGGGGBBBBBBBB"
|
|
|
|
inPixel32 = (unsigned int*)image->getData();
|
|
|
|
tempData = new unsigned char[width * height * 3];
|
|
|
|
unsigned char *outPixel8 = tempData;
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < length; ++i, ++inPixel32)
|
|
|
|
{
|
|
|
|
*outPixel8++ = (*inPixel32 >> 0) & 0xFF; // R
|
|
|
|
*outPixel8++ = (*inPixel32 >> 8) & 0xFF; // G
|
|
|
|
*outPixel8++ = (*inPixel32 >> 16) & 0xFF; // B
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-26 17:36:32 +08:00
|
|
|
|
|
|
|
initWithData(tempData, pixelFormat, width, height, imageSize);
|
|
|
|
|
|
|
|
if (tempData != image->getData())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-04-26 17:36:32 +08:00
|
|
|
delete [] tempData;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-26 17:36:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_hasPremultipliedAlpha = image->isPremultipliedAlpha();
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2010-07-26 17:32:23 +08:00
|
|
|
}
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation Texture2D (Text)
|
2013-07-26 04:36:19 +08:00
|
|
|
bool Texture2D::initWithString(const char *text, const char *fontName, float fontSize, const Size& dimensions/* = Size(0, 0)*/, Label::HAlignment hAlignment/* = Label::HAlignment::CENTER */, Label::VAlignment vAlignment/* = Label::VAlignment::TOP */)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-07-10 04:21:43 +08:00
|
|
|
FontDefinition tempDef;
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-07-10 04:21:43 +08:00
|
|
|
tempDef._shadow._shadowEnabled = false;
|
|
|
|
tempDef._stroke._strokeEnabled = false;
|
|
|
|
|
2013-04-27 07:34:10 +08:00
|
|
|
|
2013-07-10 04:21:43 +08:00
|
|
|
tempDef._fontName = std::string(fontName);
|
|
|
|
tempDef._fontSize = fontSize;
|
|
|
|
tempDef._dimensions = dimensions;
|
|
|
|
tempDef._alignment = hAlignment;
|
|
|
|
tempDef._vertAlignment = vAlignment;
|
|
|
|
tempDef._fontFillColor = Color3B::WHITE;
|
|
|
|
|
|
|
|
return initWithString(text, tempDef);
|
2010-07-26 17:32:23 +08:00
|
|
|
}
|
2012-06-08 14:11:48 +08:00
|
|
|
|
2013-07-08 15:18:16 +08:00
|
|
|
bool Texture2D::initWithString(const char *text, const FontDefinition& textDefinition)
|
2013-04-26 09:22:26 +08:00
|
|
|
{
|
2013-07-16 16:47:35 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
// cache the texture data
|
|
|
|
VolatileTexture::addStringTexture(this, text, textDefinition);
|
|
|
|
#endif
|
2013-07-10 04:21:43 +08:00
|
|
|
|
|
|
|
bool bRet = false;
|
2013-07-25 23:09:18 +08:00
|
|
|
Image::TextAlign eAlign;
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
if (Label::VAlignment::TOP == textDefinition._vertAlignment)
|
2013-07-10 04:21:43 +08:00
|
|
|
{
|
2013-07-26 15:22:45 +08:00
|
|
|
eAlign = (Label::HAlignment::CENTER == textDefinition._alignment) ? Image::TextAlign::TOP
|
|
|
|
: (Label::HAlignment::LEFT == textDefinition._alignment) ? Image::TextAlign::TOP_LEFT : Image::TextAlign::TOP_RIGHT;
|
2013-07-10 04:21:43 +08:00
|
|
|
}
|
2013-07-26 04:36:19 +08:00
|
|
|
else if (Label::VAlignment::CENTER == textDefinition._vertAlignment)
|
2013-07-10 04:21:43 +08:00
|
|
|
{
|
2013-07-26 15:22:45 +08:00
|
|
|
eAlign = (Label::HAlignment::CENTER == textDefinition._alignment) ? Image::TextAlign::CENTER
|
|
|
|
: (Label::HAlignment::LEFT == textDefinition._alignment) ? Image::TextAlign::LEFT : Image::TextAlign::RIGHT;
|
2013-07-10 04:21:43 +08:00
|
|
|
}
|
2013-07-26 04:36:19 +08:00
|
|
|
else if (Label::VAlignment::BOTTOM == textDefinition._vertAlignment)
|
2013-07-10 04:21:43 +08:00
|
|
|
{
|
2013-07-26 15:22:45 +08:00
|
|
|
eAlign = (Label::HAlignment::CENTER == textDefinition._alignment) ? Image::TextAlign::BOTTOM
|
2013-07-26 18:07:40 +08:00
|
|
|
: (Label::HAlignment::LEFT == textDefinition._alignment) ? Image::TextAlign::BOTTOM_LEFT : Image::TextAlign::BOTTOM_RIGHT;
|
2013-07-10 04:21:43 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false, "Not supported alignment format!");
|
2013-07-10 04:21:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-04-27 07:34:10 +08:00
|
|
|
|
2013-07-16 16:47:35 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
|
|
|
|
// handle shadow parameters
|
|
|
|
bool shadowEnabled = false;
|
|
|
|
float shadowDX = 0.0f;
|
|
|
|
float shadowDY = 0.0f;
|
|
|
|
float shadowBlur = 0.0f;
|
|
|
|
float shadowOpacity = 0.0f;
|
2013-04-27 07:34:10 +08:00
|
|
|
|
2013-07-16 16:47:35 +08:00
|
|
|
if ( textDefinition._shadow._shadowEnabled )
|
|
|
|
{
|
|
|
|
shadowEnabled = true;
|
|
|
|
shadowDX = textDefinition._shadow._shadowOffset.width;
|
|
|
|
shadowDY = textDefinition._shadow._shadowOffset.height;
|
|
|
|
shadowBlur = textDefinition._shadow._shadowBlur;
|
|
|
|
shadowOpacity = textDefinition._shadow._shadowOpacity;
|
|
|
|
}
|
2013-04-27 07:34:10 +08:00
|
|
|
|
2013-07-16 16:47:35 +08:00
|
|
|
// handle stroke parameters
|
|
|
|
bool strokeEnabled = false;
|
|
|
|
float strokeColorR = 0.0f;
|
|
|
|
float strokeColorG = 0.0f;
|
|
|
|
float strokeColorB = 0.0f;
|
|
|
|
float strokeSize = 0.0f;
|
2013-04-27 07:34:10 +08:00
|
|
|
|
2013-07-16 16:47:35 +08:00
|
|
|
if ( textDefinition._stroke._strokeEnabled )
|
|
|
|
{
|
|
|
|
strokeEnabled = true;
|
|
|
|
strokeColorR = textDefinition._stroke._strokeColor.r / 255.0f;
|
|
|
|
strokeColorG = textDefinition._stroke._strokeColor.g / 255.0f;
|
|
|
|
strokeColorB = textDefinition._stroke._strokeColor.b / 255.0f;
|
|
|
|
strokeSize = textDefinition._stroke._strokeSize;
|
|
|
|
}
|
2011-01-19 14:23:26 +08:00
|
|
|
|
2013-07-16 16:47:35 +08:00
|
|
|
Image* pImage = new Image();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(NULL == pImage);
|
2013-05-02 08:11:53 +08:00
|
|
|
|
2013-07-16 16:47:35 +08:00
|
|
|
bRet = pImage->initWithStringShadowStroke(text,
|
|
|
|
(int)textDefinition._dimensions.width,
|
|
|
|
(int)textDefinition._dimensions.height,
|
|
|
|
eAlign,
|
|
|
|
textDefinition._fontName.c_str(),
|
|
|
|
textDefinition._fontSize,
|
|
|
|
textDefinition._fontFillColor.r / 255.0f,
|
|
|
|
textDefinition._fontFillColor.g / 255.0f,
|
|
|
|
textDefinition._fontFillColor.b / 255.0f,
|
|
|
|
shadowEnabled,
|
|
|
|
shadowDX,
|
|
|
|
shadowDY,
|
|
|
|
shadowOpacity,
|
|
|
|
shadowBlur,
|
|
|
|
strokeEnabled,
|
|
|
|
strokeColorR,
|
|
|
|
strokeColorG,
|
|
|
|
strokeColorB,
|
|
|
|
strokeSize);
|
2013-05-02 08:11:53 +08:00
|
|
|
|
|
|
|
|
2013-07-16 16:47:35 +08:00
|
|
|
CC_BREAK_IF(!bRet);
|
|
|
|
bRet = initWithImage(pImage);
|
2013-05-02 08:11:53 +08:00
|
|
|
|
2013-07-16 16:47:35 +08:00
|
|
|
} while (0);
|
2013-05-02 08:11:53 +08:00
|
|
|
|
2013-07-16 16:47:35 +08:00
|
|
|
CC_SAFE_RELEASE(pImage);
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-07-16 16:47:35 +08:00
|
|
|
return bRet;
|
|
|
|
|
|
|
|
#else
|
|
|
|
bool requestUnsupported = textDefinition._shadow._shadowEnabled || textDefinition._stroke._strokeEnabled;
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(requestUnsupported == false, "Currently shadow and stroke only supported on iOS and Android!");
|
2013-07-16 16:47:35 +08:00
|
|
|
|
|
|
|
Image* pImage = new Image();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(NULL == pImage);
|
|
|
|
bRet = pImage->initWithString(text, (int)textDefinition._dimensions.width, (int)textDefinition._dimensions.height, eAlign, textDefinition._fontName.c_str(), (int)textDefinition._fontSize);
|
|
|
|
CC_BREAK_IF(!bRet);
|
|
|
|
bRet = initWithImage(pImage);
|
|
|
|
} while (0);
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-07-16 16:47:35 +08:00
|
|
|
CC_SAFE_RELEASE(pImage);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
#endif
|
2010-08-11 18:09:10 +08:00
|
|
|
}
|
2010-07-15 18:15:00 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation Texture2D (Drawing)
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Texture2D::drawAtPoint(const Point& point)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-07-10 04:21:43 +08:00
|
|
|
GLfloat coordinates[] = {
|
2013-06-15 14:03:30 +08:00
|
|
|
0.0f, _maxT,
|
|
|
|
_maxS,_maxT,
|
2012-04-19 14:35:52 +08:00
|
|
|
0.0f, 0.0f,
|
2013-06-15 14:03:30 +08:00
|
|
|
_maxS,0.0f };
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
GLfloat width = (GLfloat)_pixelsWide * _maxS,
|
|
|
|
height = (GLfloat)_pixelsHigh * _maxT;
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
GLfloat vertices[] = {
|
|
|
|
point.x, point.y,
|
|
|
|
width + point.x, point.y,
|
|
|
|
point.x, height + point.y,
|
|
|
|
width + point.x, height + point.y };
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORDS );
|
2013-06-15 14:03:30 +08:00
|
|
|
_shaderProgram->use();
|
|
|
|
_shaderProgram->setUniformsForBuiltins();
|
2012-03-21 11:07:31 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2D( _name );
|
2012-03-21 11:07:31 +08:00
|
|
|
|
|
|
|
|
2013-04-09 12:08:34 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
setGLBufferData(vertices, 8 * sizeof(GLfloat), 0);
|
2013-07-25 17:48:22 +08:00
|
|
|
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);
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-09 12:08:34 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, coordinates);
|
2013-04-09 12:08:34 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Texture2D::drawInRect(const Rect& rect)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
GLfloat coordinates[] = {
|
2013-06-15 14:03:30 +08:00
|
|
|
0.0f, _maxT,
|
|
|
|
_maxS,_maxT,
|
2012-04-19 14:35:52 +08:00
|
|
|
0.0f, 0.0f,
|
2013-06-15 14:03:30 +08:00
|
|
|
_maxS,0.0f };
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
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*/ };
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORDS );
|
2013-06-15 14:03:30 +08:00
|
|
|
_shaderProgram->use();
|
|
|
|
_shaderProgram->setUniformsForBuiltins();
|
2012-03-21 11:07:31 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2D( _name );
|
2012-03-21 11:07:31 +08:00
|
|
|
|
2013-04-09 12:08:34 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
setGLBufferData(vertices, 8 * sizeof(GLfloat), 0);
|
2013-07-25 17:48:22 +08:00
|
|
|
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);
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-09 12:08:34 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, coordinates);
|
2013-04-09 12:08:34 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-04-19 14:35:52 +08:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Texture2D::initWithPVRFile(const char* file)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2011-07-19 15:14:59 +08:00
|
|
|
bool bRet = false;
|
2013-06-20 14:13:12 +08:00
|
|
|
// nothing to do with Object::init
|
2011-07-19 15:14:59 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TexturePVR *pvr = new TexturePVR;
|
2011-07-19 15:14:59 +08:00
|
|
|
bRet = pvr->initWithContentsOfFile(file);
|
|
|
|
|
|
|
|
if (bRet)
|
|
|
|
{
|
|
|
|
pvr->setRetainName(true); // don't dealloc texture on release
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_name = pvr->getName();
|
|
|
|
_maxS = 1.0f;
|
|
|
|
_maxT = 1.0f;
|
|
|
|
_pixelsWide = pvr->getWidth();
|
|
|
|
_pixelsHigh = pvr->getHeight();
|
2013-07-12 14:30:26 +08:00
|
|
|
_contentSize = Size((float)_pixelsWide, (float)_pixelsHigh);
|
2013-07-28 23:38:22 +08:00
|
|
|
// FIX ME, if premultiply should be false, or the test case in RenderTextureTest(RenderTextureTargetNode) works in wrong effect
|
|
|
|
//_hasPremultipliedAlpha = (pvr->isForcePremultipliedAlpha()) ? pvr->hasPremultipliedAlpha() : _PVRHaveAlphaPremultiplied;
|
2013-07-29 14:07:57 +08:00
|
|
|
_hasPremultipliedAlpha = _PVRHaveAlphaPremultiplied;
|
2013-06-15 14:03:30 +08:00
|
|
|
_pixelFormat = pvr->getFormat();
|
|
|
|
_hasMipmaps = pvr->getNumberOfMipmaps() > 1;
|
2012-06-08 14:11:48 +08:00
|
|
|
|
2011-07-20 15:35:20 +08:00
|
|
|
pvr->release();
|
2011-07-19 15:14:59 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Couldn't load PVR image %s", file);
|
|
|
|
}
|
2010-07-21 11:13:32 +08:00
|
|
|
|
2011-07-19 15:14:59 +08:00
|
|
|
return bRet;
|
|
|
|
}
|
2010-07-21 11:13:32 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Texture2D::initWithETCFile(const char* file)
|
2013-05-27 14:42:22 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
2013-06-20 14:13:12 +08:00
|
|
|
// nothing to do with Object::init
|
2013-05-27 14:42:22 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TextureETC *etc = new TextureETC;
|
2013-05-27 14:42:22 +08:00
|
|
|
bRet = etc->initWithFile(file);
|
|
|
|
|
|
|
|
if (bRet)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_name = etc->getName();
|
|
|
|
_maxS = 1.0f;
|
|
|
|
_maxT = 1.0f;
|
|
|
|
_pixelsWide = etc->getWidth();
|
|
|
|
_pixelsHigh = etc->getHeight();
|
2013-07-12 14:30:26 +08:00
|
|
|
_contentSize = Size((float)_pixelsWide, (float)_pixelsHigh);
|
2013-06-15 14:03:30 +08:00
|
|
|
_hasPremultipliedAlpha = true;
|
2013-05-27 14:42:22 +08:00
|
|
|
|
|
|
|
etc->release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Couldn't load ETC image %s", file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Texture2D::PVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied)
|
2011-07-19 15:14:59 +08:00
|
|
|
{
|
2013-07-29 14:07:57 +08:00
|
|
|
_PVRHaveAlphaPremultiplied = haveAlphaPremultiplied;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2011-07-19 15:14:59 +08:00
|
|
|
|
2010-07-15 18:15:00 +08:00
|
|
|
//
|
|
|
|
// Use to apply MIN/MAG filter
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation Texture2D (GLFilter)
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Texture2D::generateMipmap()
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( _pixelsWide == ccNextPOT(_pixelsWide) && _pixelsHigh == ccNextPOT(_pixelsHigh), "Mipmap texture only works in POT textures");
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2D( _name );
|
2012-04-19 14:35:52 +08:00
|
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
2013-06-15 14:03:30 +08:00
|
|
|
_hasMipmaps = true;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 08:44:41 +08:00
|
|
|
bool Texture2D::hasMipmaps() const
|
2012-06-15 15:10:40 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _hasMipmaps;
|
2012-06-15 15:10:40 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 08:59:22 +08:00
|
|
|
void Texture2D::setTexParameters(const ccTexParams &texParams)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( (_pixelsWide == ccNextPOT(_pixelsWide) || texParams.wrapS == GL_CLAMP_TO_EDGE) &&
|
2013-07-04 08:59:22 +08:00
|
|
|
(_pixelsHigh == ccNextPOT(_pixelsHigh) || texParams.wrapT == GL_CLAMP_TO_EDGE),
|
2012-06-08 14:11:48 +08:00
|
|
|
"GL_CLAMP_TO_EDGE should be used in NPOT dimensions");
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2D( _name );
|
2013-07-04 08:59:22 +08:00
|
|
|
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 );
|
2012-08-31 03:02:05 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
VolatileTexture::setTexParameters(this, texParams);
|
|
|
|
#endif
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Texture2D::setAliasTexParameters()
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2D( _name );
|
2012-06-13 16:20:58 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( ! _hasMipmaps )
|
2012-06-13 16:20:58 +08:00
|
|
|
{
|
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST );
|
|
|
|
}
|
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
|
2012-08-31 03:02:05 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2013-06-15 14:03:30 +08:00
|
|
|
ccTexParams texParams = {(GLuint)(_hasMipmaps?GL_NEAREST_MIPMAP_NEAREST:GL_NEAREST),GL_NEAREST,GL_NONE,GL_NONE};
|
2013-07-04 08:59:22 +08:00
|
|
|
VolatileTexture::setTexParameters(this, texParams);
|
2012-08-31 03:02:05 +08:00
|
|
|
#endif
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Texture2D::setAntiAliasTexParameters()
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2D( _name );
|
2012-06-13 16:20:58 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( ! _hasMipmaps )
|
2012-06-13 16:20:58 +08:00
|
|
|
{
|
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
|
|
|
|
}
|
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
2012-08-31 03:02:05 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2013-06-15 14:03:30 +08:00
|
|
|
ccTexParams texParams = {(GLuint)(_hasMipmaps?GL_LINEAR_MIPMAP_NEAREST:GL_LINEAR),GL_LINEAR,GL_NONE,GL_NONE};
|
2013-07-04 08:59:22 +08:00
|
|
|
VolatileTexture::setTexParameters(this, texParams);
|
2012-08-31 03:02:05 +08:00
|
|
|
#endif
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 14:57:54 +08:00
|
|
|
const char* Texture2D::getStringForFormat() const
|
2012-06-13 16:20:58 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
switch (_pixelFormat)
|
2012-06-13 16:20:58 +08:00
|
|
|
{
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::RGBA8888:
|
2012-06-13 16:20:58 +08:00
|
|
|
return "RGBA8888";
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::RGB888:
|
2012-06-13 16:20:58 +08:00
|
|
|
return "RGB888";
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::RGB565:
|
2012-06-13 16:20:58 +08:00
|
|
|
return "RGB565";
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::RGBA4444:
|
2012-06-13 16:20:58 +08:00
|
|
|
return "RGBA4444";
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::RGB5A1:
|
2012-06-13 16:20:58 +08:00
|
|
|
return "RGB5A1";
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::AI88:
|
2012-06-13 16:20:58 +08:00
|
|
|
return "AI88";
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::A8:
|
2012-06-13 16:20:58 +08:00
|
|
|
return "A8";
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::I8:
|
2012-06-13 16:20:58 +08:00
|
|
|
return "I8";
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::PRVTC4:
|
2012-06-13 16:20:58 +08:00
|
|
|
return "PVRTC4";
|
|
|
|
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::PRVTC2:
|
2012-06-13 16:20:58 +08:00
|
|
|
return "PVRTC2";
|
|
|
|
|
|
|
|
default:
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false , "unrecognized pixel format");
|
2013-06-15 14:03:30 +08:00
|
|
|
CCLOG("stringForFormat: %ld, cannot give useful result", (long)_pixelFormat);
|
2012-06-13 16:20:58 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-06-11 21:58:04 +08:00
|
|
|
|
2010-07-15 18:15:00 +08:00
|
|
|
//
|
|
|
|
// Texture options for images that contains alpha
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation Texture2D (PixelFormat)
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2013-07-25 19:52:44 +08:00
|
|
|
void Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat format)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
g_defaultAlphaPixelFormat = format;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-25 19:52:44 +08:00
|
|
|
Texture2D::PixelFormat Texture2D::getDefaultAlphaPixelFormat()
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return g_defaultAlphaPixelFormat;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
2010-08-02 10:58:00 +08:00
|
|
|
|
2013-07-25 19:52:44 +08:00
|
|
|
unsigned int Texture2D::getBitsPerPixelForFormat(Texture2D::PixelFormat format) const
|
2012-06-13 16:20:58 +08:00
|
|
|
{
|
|
|
|
unsigned int ret=0;
|
|
|
|
|
|
|
|
switch (format) {
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::RGBA8888:
|
2012-06-13 16:20:58 +08:00
|
|
|
ret = 32;
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::RGB888:
|
2012-06-13 16:20:58 +08:00
|
|
|
// It is 32 and not 24, since its internal representation uses 32 bits.
|
|
|
|
ret = 32;
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::RGB565:
|
2012-06-13 16:20:58 +08:00
|
|
|
ret = 16;
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::RGBA4444:
|
2012-06-13 16:20:58 +08:00
|
|
|
ret = 16;
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::RGB5A1:
|
2012-06-13 16:20:58 +08:00
|
|
|
ret = 16;
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::AI88:
|
2012-06-13 16:20:58 +08:00
|
|
|
ret = 16;
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::A8:
|
2012-06-13 16:20:58 +08:00
|
|
|
ret = 8;
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::I8:
|
2012-06-13 16:20:58 +08:00
|
|
|
ret = 8;
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::PRVTC4:
|
2012-06-13 16:20:58 +08:00
|
|
|
ret = 4;
|
|
|
|
break;
|
2013-07-26 04:36:19 +08:00
|
|
|
case Texture2D::PixelFormat::PRVTC2:
|
2012-06-13 16:20:58 +08:00
|
|
|
ret = 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -1;
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false , "unrecognized pixel format");
|
2012-06-13 16:20:58 +08:00
|
|
|
CCLOG("bitsPerPixelForFormat: %ld, cannot give useful result", (long)format);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-19 14:57:54 +08:00
|
|
|
unsigned int Texture2D::getBitsPerPixelForFormat() const
|
2012-06-13 16:20:58 +08:00
|
|
|
{
|
2013-07-19 14:57:54 +08:00
|
|
|
return this->getBitsPerPixelForFormat(_pixelFormat);
|
2012-06-13 16:20:58 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2011-07-05 10:47:25 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|