CCTextureCube.cpp 8.9 KB

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