123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- #include "renderer/CCTextureCube.h"
- #include "platform/CCImage.h"
- #include "platform/CCFileUtils.h"
- #include "renderer/ccGLStateCache.h"
- NS_CC_BEGIN
- unsigned char* getImageData(Image* img, Texture2D::PixelFormat& ePixFmt)
- {
- 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();
-
- if (bHasAlpha)
- {
- ePixFmt = Texture2D::PixelFormat::DEFAULT;
- }
- else
- {
- if (uBPP >= 8)
- {
- ePixFmt = Texture2D::PixelFormat::RGB888;
- }
- else
- {
- ePixFmt = Texture2D::PixelFormat::RGB565;
- }
- }
-
- unsigned int uLen = nWidth * nHeight;
- if (ePixFmt == Texture2D::PixelFormat::RGB565)
- {
- if (bHasAlpha)
- {
-
- inPixel32 = (unsigned int*)img->getData();
- pTmpData = new (std::nothrow) unsigned char[nWidth * nHeight * 2];
- outPixel16 = (unsigned short*)pTmpData;
- for (unsigned int i = 0; i < uLen; ++i, ++inPixel32)
- {
- *outPixel16++ =
- ((((*inPixel32 >> 0) & 0xFF) >> 3) << 11) |
- ((((*inPixel32 >> 8) & 0xFF) >> 2) << 5) |
- ((((*inPixel32 >> 16) & 0xFF) >> 3) << 0);
- }
- }
- else
- {
-
- pTmpData = new (std::nothrow) unsigned char[nWidth * nHeight * 2];
- 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) |
- ((G >> 2) << 5) |
- ((B >> 3) << 0);
- }
- }
- }
- if (bHasAlpha && ePixFmt == Texture2D::PixelFormat::RGB888)
- {
-
- inPixel32 = (unsigned int*)img->getData();
- pTmpData = new (std::nothrow) unsigned char[nWidth * nHeight * 3];
- unsigned char* outPixel8 = pTmpData;
- for (unsigned int i = 0; i < uLen; ++i, ++inPixel32)
- {
- *outPixel8++ = (*inPixel32 >> 0) & 0xFF;
- *outPixel8++ = (*inPixel32 >> 8) & 0xFF;
- *outPixel8++ = (*inPixel32 >> 16) & 0xFF;
- }
- }
- return pTmpData;
- }
- Image* createImage(const std::string& path)
- {
-
-
-
- std::string fullpath = FileUtils::getInstance()->fullPathForFilename(path);
- if (fullpath.size() == 0)
- {
- return nullptr;
- }
-
- 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()
- {
- _imgPath.resize(6);
- }
- TextureCube::~TextureCube()
- {
- }
- 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)
- {
- auto ret = new (std::nothrow) TextureCube();
- if (ret && ret->init(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z))
- {
- ret->autorelease();
- return ret;
- }
- CC_SAFE_DELETE(ret);
- return nullptr;
- }
- 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)
- {
- _imgPath[0] = positive_x;
- _imgPath[1] = negative_x;
- _imgPath[2] = positive_y;
- _imgPath[3] = negative_y;
- _imgPath[4] = positive_z;
- _imgPath[5] = negative_z;
- std::vector<Image*> images(6);
- 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);
- GLuint handle;
- glGenTextures(1, &handle);
- GL::bindTextureN(0, handle, GL_TEXTURE_CUBE_MAP);
- for (int i = 0; i < 6; i++)
- {
- Image* img = images[i];
- Texture2D::PixelFormat ePixelFmt;
- unsigned char* pData = getImageData(img, ePixelFmt);
- if (ePixelFmt == Texture2D::PixelFormat::RGBA8888 || ePixelFmt == Texture2D::PixelFormat::DEFAULT)
- {
- glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
- 0,
- GL_RGBA,
- img->getWidth(),
- img->getHeight(),
- 0,
- GL_RGBA,
- GL_UNSIGNED_BYTE,
- pData);
- }
- else if (ePixelFmt == Texture2D::PixelFormat::RGB888)
- {
- glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
- 0,
- GL_RGB,
- img->getWidth(),
- img->getHeight(),
- 0,
- GL_RGB,
- GL_UNSIGNED_BYTE,
- pData);
- }
- if (pData != img->getData())
- delete[] pData;
- }
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- _name = handle;
- GL::bindTextureN(0, 0, GL_TEXTURE_CUBE_MAP);
- for (auto img: images)
- {
- CC_SAFE_RELEASE(img);
- }
- return true;
- }
- void TextureCube::setTexParameters(const TexParams& texParams)
- {
- CCASSERT(_name != 0, __FUNCTION__);
- GL::bindTextureN(0, _name, GL_TEXTURE_CUBE_MAP);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, texParams.minFilter);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, texParams.magFilter);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, texParams.wrapS);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, texParams.wrapT);
- GL::bindTextureN(0, 0, GL_TEXTURE_CUBE_MAP);
- }
- bool TextureCube::reloadTexture()
- {
- return init(_imgPath[0], _imgPath[1], _imgPath[2], _imgPath[3], _imgPath[4], _imgPath[5]);
- }
- NS_CC_END
|