CCTextureCube.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /****************************************************************************
  2. Copyright (c) 2015-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "renderer/CCTextureCube.h"
  22. #include "platform/CCImage.h"
  23. #include "platform/CCFileUtils.h"
  24. #include "renderer/ccGLStateCache.h"
  25. NS_CC_BEGIN
  26. unsigned char* getImageData(Image* img, Texture2D::PixelFormat& ePixFmt)
  27. {
  28. unsigned char* pTmpData = img->getData();
  29. unsigned int* inPixel32 = nullptr;
  30. unsigned char* inPixel8 = nullptr;
  31. unsigned short* outPixel16 = nullptr;
  32. bool bHasAlpha = img->hasAlpha();
  33. size_t uBPP = img->getBitPerPixel();
  34. int nWidth = img->getWidth();
  35. int nHeight = img->getHeight();
  36. // compute pixel format
  37. if (bHasAlpha)
  38. {
  39. ePixFmt = Texture2D::PixelFormat::DEFAULT;
  40. }
  41. else
  42. {
  43. if (uBPP >= 8)
  44. {
  45. ePixFmt = Texture2D::PixelFormat::RGB888;
  46. }
  47. else
  48. {
  49. ePixFmt = Texture2D::PixelFormat::RGB565;
  50. }
  51. }
  52. // Repack the pixel data into the right format
  53. unsigned int uLen = nWidth * nHeight;
  54. if (ePixFmt == Texture2D::PixelFormat::RGB565)
  55. {
  56. if (bHasAlpha)
  57. {
  58. // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB"
  59. inPixel32 = (unsigned int*)img->getData();
  60. pTmpData = new (std::nothrow) unsigned char[nWidth * nHeight * 2];
  61. outPixel16 = (unsigned short*)pTmpData;
  62. for (unsigned int i = 0; i < uLen; ++i, ++inPixel32)
  63. {
  64. *outPixel16++ =
  65. ((((*inPixel32 >> 0) & 0xFF) >> 3) << 11) | // R
  66. ((((*inPixel32 >> 8) & 0xFF) >> 2) << 5) | // G
  67. ((((*inPixel32 >> 16) & 0xFF) >> 3) << 0); // B
  68. }
  69. }
  70. else
  71. {
  72. // Convert "RRRRRRRRGGGGGGGGBBBBBBBB" to "RRRRRGGGGGGBBBBB"
  73. pTmpData = new (std::nothrow) unsigned char[nWidth * nHeight * 2];
  74. outPixel16 = (unsigned short*)pTmpData;
  75. inPixel8 = (unsigned char*)img->getData();
  76. for (unsigned int i = 0; i < uLen; ++i)
  77. {
  78. unsigned char R = *inPixel8++;
  79. unsigned char G = *inPixel8++;
  80. unsigned char B = *inPixel8++;
  81. *outPixel16++ =
  82. ((R >> 3) << 11) | // R
  83. ((G >> 2) << 5) | // G
  84. ((B >> 3) << 0); // B
  85. }
  86. }
  87. }
  88. if (bHasAlpha && ePixFmt == Texture2D::PixelFormat::RGB888)
  89. {
  90. // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRRRRGGGGGGGGBBBBBBBB"
  91. inPixel32 = (unsigned int*)img->getData();
  92. pTmpData = new (std::nothrow) unsigned char[nWidth * nHeight * 3];
  93. unsigned char* outPixel8 = pTmpData;
  94. for (unsigned int i = 0; i < uLen; ++i, ++inPixel32)
  95. {
  96. *outPixel8++ = (*inPixel32 >> 0) & 0xFF; // R
  97. *outPixel8++ = (*inPixel32 >> 8) & 0xFF; // G
  98. *outPixel8++ = (*inPixel32 >> 16) & 0xFF; // B
  99. }
  100. }
  101. return pTmpData;
  102. }
  103. Image* createImage(const std::string& path)
  104. {
  105. // Split up directory and filename
  106. // MUTEX:
  107. // Needed since addImageAsync calls this method from a different thread
  108. std::string fullpath = FileUtils::getInstance()->fullPathForFilename(path);
  109. if (fullpath.size() == 0)
  110. {
  111. return nullptr;
  112. }
  113. // all images are handled by UIImage except PVR extension that is handled by our own handler
  114. Image* image = nullptr;
  115. do
  116. {
  117. image = new (std::nothrow) Image();
  118. CC_BREAK_IF(nullptr == image);
  119. bool bRet = image->initWithImageFile(fullpath);
  120. CC_BREAK_IF(!bRet);
  121. }
  122. while (0);
  123. return image;
  124. }
  125. TextureCube::TextureCube()
  126. {
  127. _imgPath.resize(6);
  128. }
  129. TextureCube::~TextureCube()
  130. {
  131. }
  132. TextureCube* TextureCube::create(const std::string& positive_x, const std::string& negative_x,
  133. const std::string& positive_y, const std::string& negative_y,
  134. const std::string& positive_z, const std::string& negative_z)
  135. {
  136. auto ret = new (std::nothrow) TextureCube();
  137. if (ret && ret->init(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z))
  138. {
  139. ret->autorelease();
  140. return ret;
  141. }
  142. CC_SAFE_DELETE(ret);
  143. return nullptr;
  144. }
  145. bool TextureCube::init(const std::string& positive_x, const std::string& negative_x,
  146. const std::string& positive_y, const std::string& negative_y,
  147. const std::string& positive_z, const std::string& negative_z)
  148. {
  149. _imgPath[0] = positive_x;
  150. _imgPath[1] = negative_x;
  151. _imgPath[2] = positive_y;
  152. _imgPath[3] = negative_y;
  153. _imgPath[4] = positive_z;
  154. _imgPath[5] = negative_z;
  155. std::vector<Image*> images(6);
  156. images[0] = createImage(positive_x);
  157. images[1] = createImage(negative_x);
  158. images[2] = createImage(positive_y);
  159. images[3] = createImage(negative_y);
  160. images[4] = createImage(positive_z);
  161. images[5] = createImage(negative_z);
  162. GLuint handle;
  163. glGenTextures(1, &handle);
  164. GL::bindTextureN(0, handle, GL_TEXTURE_CUBE_MAP);
  165. for (int i = 0; i < 6; i++)
  166. {
  167. Image* img = images[i];
  168. Texture2D::PixelFormat ePixelFmt;
  169. unsigned char* pData = getImageData(img, ePixelFmt);
  170. if (ePixelFmt == Texture2D::PixelFormat::RGBA8888 || ePixelFmt == Texture2D::PixelFormat::DEFAULT)
  171. {
  172. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
  173. 0, // level
  174. GL_RGBA, // internal format
  175. img->getWidth(), // width
  176. img->getHeight(), // height
  177. 0, // border
  178. GL_RGBA, // format
  179. GL_UNSIGNED_BYTE, // type
  180. pData); // pixel data
  181. }
  182. else if (ePixelFmt == Texture2D::PixelFormat::RGB888)
  183. {
  184. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
  185. 0, // level
  186. GL_RGB, // internal format
  187. img->getWidth(), // width
  188. img->getHeight(), // height
  189. 0, // border
  190. GL_RGB, // format
  191. GL_UNSIGNED_BYTE, // type
  192. pData); // pixel data
  193. }
  194. if (pData != img->getData())
  195. delete[] pData;
  196. }
  197. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  198. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  199. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  200. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  201. _name = handle;
  202. GL::bindTextureN(0, 0, GL_TEXTURE_CUBE_MAP);
  203. for (auto img: images)
  204. {
  205. CC_SAFE_RELEASE(img);
  206. }
  207. return true;
  208. }
  209. void TextureCube::setTexParameters(const TexParams& texParams)
  210. {
  211. CCASSERT(_name != 0, __FUNCTION__);
  212. GL::bindTextureN(0, _name, GL_TEXTURE_CUBE_MAP);
  213. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, texParams.minFilter);
  214. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, texParams.magFilter);
  215. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, texParams.wrapS);
  216. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, texParams.wrapT);
  217. GL::bindTextureN(0, 0, GL_TEXTURE_CUBE_MAP);
  218. }
  219. bool TextureCube::reloadTexture()
  220. {
  221. return init(_imgPath[0], _imgPath[1], _imgPath[2], _imgPath[3], _imgPath[4], _imgPath[5]);
  222. }
  223. NS_CC_END