Texture.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "Base.h"
  2. #include "Texture.h"
  3. #include "FileSystem.h"
  4. namespace gameplay
  5. {
  6. static std::vector<Texture*> __textureCache;
  7. Texture::Texture() : _handle(0), _mipmapped(false), _cached(false)
  8. {
  9. }
  10. Texture::Texture(const Texture& copy)
  11. {
  12. }
  13. Texture::~Texture()
  14. {
  15. if (_handle)
  16. {
  17. glDeleteTextures(1, &_handle);
  18. _handle = 0;
  19. }
  20. // Remove ourself from the texture cache.
  21. if (_cached)
  22. {
  23. std::vector<Texture*>::iterator itr = std::find(__textureCache.begin(), __textureCache.end(), this);
  24. if (itr != __textureCache.end())
  25. {
  26. __textureCache.erase(itr);
  27. }
  28. }
  29. }
  30. Texture* Texture::create(const char* path, bool generateMipmaps)
  31. {
  32. // Search texture cache first.
  33. for (unsigned int i = 0, count = __textureCache.size(); i < count; ++i)
  34. {
  35. Texture* t = __textureCache[i];
  36. if (t->_path == path)
  37. {
  38. // If 'generateMipmaps' is true, call Texture::generateMipamps() to force the
  39. // texture to generate its mipmap chain if it hasn't already done so.
  40. if (generateMipmaps)
  41. {
  42. t->generateMipmaps();
  43. }
  44. // Found a match.
  45. t->addRef();
  46. return t;
  47. }
  48. }
  49. Texture* texture = NULL;
  50. // Filter loading based on file extension.
  51. const char* ext = strrchr(path, '.');
  52. if (ext)
  53. {
  54. switch (strlen(ext))
  55. {
  56. case 4:
  57. if (tolower(ext[1]) == 'p' && tolower(ext[2]) == 'n' && tolower(ext[3]) == 'g')
  58. {
  59. texture = loadPNG(path, generateMipmaps);
  60. }
  61. break;
  62. }
  63. }
  64. if (texture)
  65. {
  66. texture->_path = path;
  67. texture->_cached = true;
  68. // Add to texture cache.
  69. __textureCache.push_back(texture);
  70. return texture;
  71. }
  72. LOG_ERROR_VARG("Failed to load texture: %s", path);
  73. return NULL;
  74. }
  75. Texture* Texture::loadPNG(const char* path, bool generateMipmaps)
  76. {
  77. // Open the file.
  78. FILE* fp = FileSystem::openFile(path, "rb");
  79. if (fp == NULL)
  80. {
  81. return NULL;
  82. }
  83. // Verify PNG signature.
  84. unsigned char sig[8];
  85. if (fread(sig, 1, 8, fp) != 8 || png_sig_cmp(sig, 0, 8) != 0)
  86. {
  87. LOG_ERROR_VARG("Texture is not a valid PNG: %s", path);
  88. fclose(fp);
  89. return NULL;
  90. }
  91. // Initialize png read struct (last three parameters use stderr+longjump if NULL).
  92. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  93. if (png == NULL)
  94. {
  95. fclose(fp);
  96. return NULL;
  97. }
  98. // Initialize info struct.
  99. png_infop info = png_create_info_struct(png);
  100. if (info == NULL)
  101. {
  102. fclose(fp);
  103. png_destroy_read_struct(&png, NULL, NULL);
  104. return NULL;
  105. }
  106. // Set up error handling (required without using custom error handlers above).
  107. if (setjmp(png_jmpbuf(png)))
  108. {
  109. fclose(fp);
  110. png_destroy_read_struct(&png, &info, NULL);
  111. return NULL;
  112. }
  113. // Initialize file io.
  114. png_init_io(png, fp);
  115. // Indicate that we already read the first 8 bytes (signature).
  116. png_set_sig_bytes(png, 8);
  117. // Read the entire image into memory.
  118. png_read_png(png, info, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, NULL);
  119. unsigned int width = png_get_image_width(png, info);
  120. unsigned int height = png_get_image_height(png, info);
  121. png_byte colorType = png_get_color_type(png, info);
  122. Format format;
  123. switch (colorType)
  124. {
  125. case PNG_COLOR_TYPE_RGBA:
  126. format = RGBA8888;
  127. break;
  128. case PNG_COLOR_TYPE_RGB:
  129. format = RGB888;
  130. break;
  131. default:
  132. LOG_ERROR_VARG("Unsupported PNG color type (%d) for texture: %s", (int)colorType, path);
  133. fclose(fp);
  134. png_destroy_read_struct(&png, &info, NULL);
  135. return NULL;
  136. }
  137. unsigned int stride = png_get_rowbytes(png, info);
  138. // Allocate image data.
  139. unsigned char* data = new unsigned char[stride * height];
  140. // Read rows into image data.
  141. png_bytepp rows = png_get_rows(png, info);
  142. for (unsigned int i = 0; i < height; ++i)
  143. {
  144. memcpy(data+(stride * (height-1-i)), rows[i], stride);
  145. }
  146. // Clean up.
  147. png_destroy_read_struct(&png, &info, NULL);
  148. fclose(fp);
  149. // Create texture.
  150. Texture* texture = create(format, width, height, data, generateMipmaps);
  151. // Free temporary data.
  152. SAFE_DELETE_ARRAY(data);
  153. return texture;
  154. }
  155. Texture* Texture::create(Format format, unsigned int width, unsigned int height, unsigned char* data, bool generateMipmaps)
  156. {
  157. // Load our texture.
  158. GLuint textureId;
  159. GL_ASSERT( glGenTextures(1, &textureId) );
  160. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, textureId) );
  161. if (format == DEPTH)
  162. {
  163. // <type> must be UNSIGNED_SHORT or UNSIGNED_INT for a format of DEPTH_COMPONENT.
  164. GL_ASSERT( glTexImage2D(GL_TEXTURE_2D, 0, (GLenum)format, width, height, 0, (GLenum)format, GL_UNSIGNED_INT, data) );
  165. }
  166. else
  167. {
  168. GL_ASSERT( glTexImage2D(GL_TEXTURE_2D, 0, (GLenum)format, width, height, 0, (GLenum)format, GL_UNSIGNED_BYTE, data) );
  169. }
  170. // Set initial minification filter based on whether or not mipmaping was enabled
  171. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, generateMipmaps ? GL_NEAREST_MIPMAP_LINEAR : GL_LINEAR) );
  172. Texture* texture = new Texture();
  173. texture->_handle = textureId;
  174. texture->_width = width;
  175. texture->_height = height;
  176. if (generateMipmaps)
  177. {
  178. texture->generateMipmaps();
  179. }
  180. return texture;
  181. }
  182. unsigned int Texture::getWidth() const
  183. {
  184. return _width;
  185. }
  186. unsigned int Texture::getHeight() const
  187. {
  188. return _height;
  189. }
  190. TextureHandle Texture::getHandle() const
  191. {
  192. return _handle;
  193. }
  194. void Texture::setWrapMode(Wrap wrapS, Wrap wrapT)
  195. {
  196. GLint currentTextureId;
  197. GL_ASSERT( glGetIntegerv(GL_TEXTURE_BINDING_2D, &currentTextureId) );
  198. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, _handle) );
  199. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLenum)wrapS) );
  200. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLenum)wrapT) );
  201. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, (GLuint)currentTextureId) );
  202. }
  203. void Texture::setFilterMode(Filter minificationFilter, Filter magnificationFilter)
  204. {
  205. GLint currentTextureId;
  206. GL_ASSERT( glGetIntegerv(GL_TEXTURE_BINDING_2D, &currentTextureId) );
  207. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, _handle) );
  208. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLenum)minificationFilter) );
  209. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLenum)magnificationFilter) );
  210. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, (GLuint)currentTextureId) );
  211. }
  212. void Texture::generateMipmaps()
  213. {
  214. if (!_mipmapped)
  215. {
  216. GLint currentTextureId;
  217. GL_ASSERT( glGetIntegerv(GL_TEXTURE_BINDING_2D, &currentTextureId) );
  218. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, _handle) );
  219. GL_ASSERT( glGenerateMipmap(GL_TEXTURE_2D) );
  220. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, (GLuint)currentTextureId) );
  221. _mipmapped = true;
  222. }
  223. }
  224. bool Texture::isMipmapped() const
  225. {
  226. return _mipmapped;
  227. }
  228. Texture::Sampler::Sampler(Texture* texture)
  229. : _texture(texture), _wrapS(Texture::REPEAT), _wrapT(Texture::REPEAT), _magFilter(Texture::LINEAR)
  230. {
  231. _minFilter = texture->isMipmapped() ? Texture::NEAREST_MIPMAP_LINEAR : Texture::LINEAR;
  232. }
  233. Texture::Sampler::~Sampler()
  234. {
  235. SAFE_RELEASE(_texture);
  236. }
  237. Texture::Sampler* Texture::Sampler::create(Texture* texture)
  238. {
  239. assert(texture != NULL);
  240. texture->addRef();
  241. return new Sampler(texture);
  242. }
  243. Texture::Sampler* Texture::Sampler::create(const char* path, bool generateMipmaps)
  244. {
  245. Texture* texture = Texture::create(path, generateMipmaps);
  246. return texture ? new Sampler(texture) : NULL;
  247. }
  248. void Texture::Sampler::setWrapMode(Wrap wrapS, Wrap wrapT)
  249. {
  250. _wrapS = wrapS;
  251. _wrapT = wrapT;
  252. }
  253. void Texture::Sampler::setFilterMode(Filter minificationFilter, Filter magnificationFilter)
  254. {
  255. _minFilter = minificationFilter;
  256. _magFilter = magnificationFilter;
  257. }
  258. Texture* Texture::Sampler::getTexture() const
  259. {
  260. return _texture;
  261. }
  262. void Texture::Sampler::bind()
  263. {
  264. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, _texture->_handle) );
  265. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLenum)_wrapS) );
  266. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLenum)_wrapT) );
  267. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLenum)_minFilter) );
  268. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLenum)_magFilter) );
  269. }
  270. }