2015-01-29 09:28:14 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2015-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2015-03-26 13:05:42 +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.
|
|
|
|
****************************************************************************/
|
2015-01-29 09:28:14 +08:00
|
|
|
|
2015-08-24 10:49:10 +08:00
|
|
|
#include "renderer/CCTextureCube.h"
|
2015-01-29 09:28:14 +08:00
|
|
|
#include "platform/CCImage.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2019-02-19 10:15:24 +08:00
|
|
|
#include "renderer/backend/Texture.h"
|
|
|
|
#include "renderer/backend/Device.h"
|
|
|
|
#include "renderer/CCTextureUtils.h"
|
2015-01-29 09:28:14 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2019-06-03 09:39:51 +08:00
|
|
|
unsigned char* getImageData(Image* img, backend::PixelFormat& ePixFmt)
|
2015-01-29 09:28:14 +08:00
|
|
|
{
|
|
|
|
unsigned char* pTmpData = img->getData();
|
|
|
|
unsigned int* inPixel32 = nullptr;
|
|
|
|
unsigned char* inPixel8 = nullptr;
|
|
|
|
unsigned short* outPixel16 = nullptr;
|
|
|
|
bool bHasAlpha = img->hasAlpha();
|
|
|
|
size_t uBPP = img->getBitPerPixel();
|
|
|
|
|
|
|
|
int nWidth = img->getWidth();
|
|
|
|
int nHeight = img->getHeight();
|
|
|
|
|
|
|
|
// compute pixel format
|
|
|
|
if (bHasAlpha)
|
|
|
|
{
|
2019-06-03 09:39:51 +08:00
|
|
|
ePixFmt = backend::PixelFormat::DEFAULT;
|
2015-01-29 09:28:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (uBPP >= 8)
|
|
|
|
{
|
2019-06-03 09:39:51 +08:00
|
|
|
ePixFmt = backend::PixelFormat::RGB888;
|
2015-01-29 09:28:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-06-03 09:39:51 +08:00
|
|
|
ePixFmt = backend::PixelFormat::RGB565;
|
2015-01-29 09:28:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repack the pixel data into the right format
|
|
|
|
unsigned int uLen = nWidth * nHeight;
|
|
|
|
|
2019-06-03 09:39:51 +08:00
|
|
|
if (ePixFmt == backend::PixelFormat::RGB565)
|
2015-01-29 09:28:14 +08:00
|
|
|
{
|
|
|
|
if (bHasAlpha)
|
|
|
|
{
|
|
|
|
// Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB"
|
|
|
|
inPixel32 = (unsigned int*)img->getData();
|
2019-02-19 10:15:24 +08:00
|
|
|
pTmpData = (unsigned char *)malloc(nWidth * nHeight * 2);
|
2015-01-29 09:28:14 +08:00
|
|
|
outPixel16 = (unsigned short*)pTmpData;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < uLen; ++i, ++inPixel32)
|
|
|
|
{
|
|
|
|
*outPixel16++ =
|
|
|
|
((((*inPixel32 >> 0) & 0xFF) >> 3) << 11) | // R
|
|
|
|
((((*inPixel32 >> 8) & 0xFF) >> 2) << 5) | // G
|
|
|
|
((((*inPixel32 >> 16) & 0xFF) >> 3) << 0); // B
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Convert "RRRRRRRRGGGGGGGGBBBBBBBB" to "RRRRRGGGGGGBBBBB"
|
2019-02-19 10:15:24 +08:00
|
|
|
pTmpData = (unsigned char *)malloc(nWidth * nHeight * 2);
|
2015-01-29 09:28:14 +08:00
|
|
|
outPixel16 = (unsigned short*)pTmpData;
|
|
|
|
inPixel8 = (unsigned char*)img->getData();
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < uLen; ++i)
|
|
|
|
{
|
|
|
|
unsigned char R = *inPixel8++;
|
|
|
|
unsigned char G = *inPixel8++;
|
|
|
|
unsigned char B = *inPixel8++;
|
|
|
|
|
|
|
|
*outPixel16++ =
|
|
|
|
((R >> 3) << 11) | // R
|
|
|
|
((G >> 2) << 5) | // G
|
|
|
|
((B >> 3) << 0); // B
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 09:39:51 +08:00
|
|
|
if (bHasAlpha && ePixFmt == backend::PixelFormat::RGB888)
|
2015-01-29 09:28:14 +08:00
|
|
|
{
|
|
|
|
// Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRRRRGGGGGGGGBBBBBBBB"
|
|
|
|
inPixel32 = (unsigned int*)img->getData();
|
|
|
|
|
2019-02-19 10:15:24 +08:00
|
|
|
pTmpData = (unsigned char*)malloc(nWidth * nHeight * 3);
|
2015-01-29 09:28:14 +08:00
|
|
|
unsigned char* outPixel8 = pTmpData;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < uLen; ++i, ++inPixel32)
|
|
|
|
{
|
|
|
|
*outPixel8++ = (*inPixel32 >> 0) & 0xFF; // R
|
|
|
|
*outPixel8++ = (*inPixel32 >> 8) & 0xFF; // G
|
|
|
|
*outPixel8++ = (*inPixel32 >> 16) & 0xFF; // B
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pTmpData;
|
|
|
|
}
|
|
|
|
|
|
|
|
Image* createImage(const std::string& path)
|
|
|
|
{
|
|
|
|
// Split up directory and filename
|
|
|
|
// MUTEX:
|
|
|
|
// Needed since addImageAsync calls this method from a different thread
|
|
|
|
|
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(path);
|
2020-08-18 14:29:09 +08:00
|
|
|
if (fullpath.empty())
|
2015-01-29 09:28:14 +08:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// all images are handled by UIImage except PVR extension that is handled by our own handler
|
|
|
|
Image* image = nullptr;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
image = new (std::nothrow) Image();
|
|
|
|
CC_BREAK_IF(nullptr == image);
|
|
|
|
|
|
|
|
bool bRet = image->initWithImageFile(fullpath);
|
|
|
|
CC_BREAK_IF(!bRet);
|
|
|
|
}
|
|
|
|
while (0);
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextureCube::TextureCube()
|
|
|
|
{
|
2015-03-24 15:43:59 +08:00
|
|
|
_imgPath.resize(6);
|
2015-01-29 09:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureCube::~TextureCube()
|
|
|
|
{
|
2019-02-19 10:15:24 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_texture);
|
2015-01-29 09:28:14 +08:00
|
|
|
}
|
|
|
|
|
2015-01-30 17:23:30 +08:00
|
|
|
TextureCube* TextureCube::create(const std::string& positive_x, const std::string& negative_x,
|
|
|
|
const std::string& positive_y, const std::string& negative_y,
|
|
|
|
const std::string& positive_z, const std::string& negative_z)
|
2015-01-29 09:28:14 +08:00
|
|
|
{
|
|
|
|
auto ret = new (std::nothrow) TextureCube();
|
2015-01-30 17:23:30 +08:00
|
|
|
if (ret && ret->init(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z))
|
2015-01-29 09:28:14 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-30 17:23:30 +08:00
|
|
|
bool TextureCube::init(const std::string& positive_x, const std::string& negative_x,
|
|
|
|
const std::string& positive_y, const std::string& negative_y,
|
|
|
|
const std::string& positive_z, const std::string& negative_z)
|
2015-01-29 09:28:14 +08:00
|
|
|
{
|
2015-03-24 15:43:59 +08:00
|
|
|
_imgPath[0] = positive_x;
|
|
|
|
_imgPath[1] = negative_x;
|
|
|
|
_imgPath[2] = positive_y;
|
|
|
|
_imgPath[3] = negative_y;
|
|
|
|
_imgPath[4] = positive_z;
|
|
|
|
_imgPath[5] = negative_z;
|
|
|
|
|
2015-01-29 09:28:14 +08:00
|
|
|
std::vector<Image*> images(6);
|
|
|
|
|
2015-01-30 17:23:30 +08:00
|
|
|
images[0] = createImage(positive_x);
|
|
|
|
images[1] = createImage(negative_x);
|
|
|
|
images[2] = createImage(positive_y);
|
|
|
|
images[3] = createImage(negative_y);
|
|
|
|
images[4] = createImage(positive_z);
|
|
|
|
images[5] = createImage(negative_z);
|
2015-01-29 09:28:14 +08:00
|
|
|
|
2019-02-19 10:15:24 +08:00
|
|
|
int imageSize = images[0]->getHeight();
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
{
|
|
|
|
Image* img = images[i];
|
|
|
|
if(img->getWidth() != img->getHeight())
|
|
|
|
{
|
|
|
|
CCASSERT(false, "TextureCubemap: width should be equal to height!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(imageSize != img->getWidth())
|
|
|
|
{
|
|
|
|
CCASSERT(imageSize == img->getWidth(), "TextureCubmap: texture of each face should have same dimension");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-01-29 09:28:14 +08:00
|
|
|
|
2019-03-07 11:08:12 +08:00
|
|
|
backend::TextureDescriptor textureDescriptor;
|
|
|
|
textureDescriptor.width = textureDescriptor.height = imageSize;
|
|
|
|
textureDescriptor.textureType = backend::TextureType::TEXTURE_CUBE;
|
|
|
|
textureDescriptor.samplerDescriptor.minFilter = backend::SamplerFilter::LINEAR;
|
|
|
|
textureDescriptor.samplerDescriptor.magFilter = backend::SamplerFilter::LINEAR;
|
|
|
|
textureDescriptor.samplerDescriptor.sAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
|
|
|
|
textureDescriptor.samplerDescriptor.tAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
|
2019-06-12 15:01:15 +08:00
|
|
|
_texture = static_cast<backend::TextureCubemapBackend*>(backend::Device::getInstance()->newTexture(textureDescriptor));
|
2019-03-07 11:08:12 +08:00
|
|
|
CCASSERT(_texture != nullptr, "TextureCubemap: texture can not be nullptr");
|
|
|
|
|
2015-01-29 09:28:14 +08:00
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
{
|
|
|
|
Image* img = images[i];
|
|
|
|
|
2019-06-03 09:39:51 +08:00
|
|
|
backend::PixelFormat ePixelFmt;
|
2015-01-29 09:28:14 +08:00
|
|
|
unsigned char* pData = getImageData(img, ePixelFmt);
|
2019-02-19 10:15:24 +08:00
|
|
|
uint8_t *cData = nullptr;
|
|
|
|
uint8_t *useData = pData;
|
|
|
|
|
|
|
|
//convert pixel format to RGBA
|
2019-12-06 15:23:00 +08:00
|
|
|
if (ePixelFmt != backend::PixelFormat::RGBA8888 && ePixelFmt != backend::PixelFormat::DEFAULT)
|
2015-01-29 09:28:14 +08:00
|
|
|
{
|
2019-05-16 09:17:26 +08:00
|
|
|
size_t len = 0;
|
2019-06-03 09:39:51 +08:00
|
|
|
backend::PixelFormatUtils::convertDataToFormat(pData, img->getDataLen(), ePixelFmt, backend::PixelFormat::RGBA8888, &cData, &len);
|
2019-02-19 10:15:24 +08:00
|
|
|
if (cData != pData) //convert error
|
|
|
|
{
|
|
|
|
useData = cData;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(false, "error: CubeMap texture may be incorrect, failed to convert pixel format data to RGBA8888");
|
|
|
|
}
|
2015-01-29 09:28:14 +08:00
|
|
|
}
|
|
|
|
|
2019-02-19 10:15:24 +08:00
|
|
|
_texture->updateFaceData(static_cast<backend::TextureCubeFace>(i), useData);
|
|
|
|
|
|
|
|
if (cData != pData)
|
|
|
|
free(cData);
|
|
|
|
|
2015-01-29 09:28:14 +08:00
|
|
|
if (pData != img->getData())
|
2019-02-19 10:15:24 +08:00
|
|
|
free(pData);
|
2015-01-29 09:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto img: images)
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(img);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-19 10:15:24 +08:00
|
|
|
void TextureCube::setTexParameters(const Texture2D::TexParams& texParams)
|
2015-01-29 09:28:14 +08:00
|
|
|
{
|
2019-03-13 10:54:26 +08:00
|
|
|
_texture->updateSamplerDescriptor(texParams);
|
2015-01-29 09:28:14 +08:00
|
|
|
}
|
|
|
|
|
2015-03-24 15:43:59 +08:00
|
|
|
bool TextureCube::reloadTexture()
|
|
|
|
{
|
|
|
|
return init(_imgPath[0], _imgPath[1], _imgPath[2], _imgPath[3], _imgPath[4], _imgPath[5]);
|
|
|
|
}
|
|
|
|
|
2015-01-29 09:28:14 +08:00
|
|
|
NS_CC_END
|