Texture.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "Base.h"
  2. #include "Image.h"
  3. #include "Texture.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. Image* image = Image::create(path);
  60. texture = create(image, generateMipmaps);
  61. SAFE_RELEASE(image);
  62. }
  63. break;
  64. }
  65. }
  66. if (texture)
  67. {
  68. texture->_path = path;
  69. texture->_cached = true;
  70. // Add to texture cache.
  71. __textureCache.push_back(texture);
  72. return texture;
  73. }
  74. LOG_ERROR_VARG("Failed to load texture: %s", path);
  75. return NULL;
  76. }
  77. Texture* Texture::create(Image* image, bool generateMipmaps)
  78. {
  79. switch (image->getFormat())
  80. {
  81. case Image::RGB:
  82. return create(Texture::RGB, image->getWidth(), image->getHeight(), image->getData(), generateMipmaps);
  83. case Image::RGBA:
  84. return create(Texture::RGBA, image->getWidth(), image->getHeight(), image->getData(), generateMipmaps);
  85. }
  86. return NULL;
  87. }
  88. Texture* Texture::create(Format format, unsigned int width, unsigned int height, unsigned char* data, bool generateMipmaps)
  89. {
  90. // Load our texture.
  91. GLuint textureId;
  92. GL_ASSERT( glGenTextures(1, &textureId) );
  93. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, textureId) );
  94. if (format == DEPTH)
  95. {
  96. // <type> must be UNSIGNED_SHORT or UNSIGNED_INT for a format of DEPTH_COMPONENT.
  97. GL_ASSERT( glTexImage2D(GL_TEXTURE_2D, 0, (GLenum)format, width, height, 0, (GLenum)format, GL_UNSIGNED_INT, data) );
  98. }
  99. else
  100. {
  101. GL_ASSERT( glTexImage2D(GL_TEXTURE_2D, 0, (GLenum)format, width, height, 0, (GLenum)format, GL_UNSIGNED_BYTE, data) );
  102. }
  103. // Set initial minification filter based on whether or not mipmaping was enabled
  104. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, generateMipmaps ? GL_NEAREST_MIPMAP_LINEAR : GL_LINEAR) );
  105. Texture* texture = new Texture();
  106. texture->_handle = textureId;
  107. texture->_width = width;
  108. texture->_height = height;
  109. if (generateMipmaps)
  110. {
  111. texture->generateMipmaps();
  112. }
  113. return texture;
  114. }
  115. unsigned int Texture::getWidth() const
  116. {
  117. return _width;
  118. }
  119. unsigned int Texture::getHeight() const
  120. {
  121. return _height;
  122. }
  123. TextureHandle Texture::getHandle() const
  124. {
  125. return _handle;
  126. }
  127. void Texture::setWrapMode(Wrap wrapS, Wrap wrapT)
  128. {
  129. GLint currentTextureId;
  130. GL_ASSERT( glGetIntegerv(GL_TEXTURE_BINDING_2D, &currentTextureId) );
  131. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, _handle) );
  132. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLenum)wrapS) );
  133. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLenum)wrapT) );
  134. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, (GLuint)currentTextureId) );
  135. }
  136. void Texture::setFilterMode(Filter minificationFilter, Filter magnificationFilter)
  137. {
  138. GLint currentTextureId;
  139. GL_ASSERT( glGetIntegerv(GL_TEXTURE_BINDING_2D, &currentTextureId) );
  140. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, _handle) );
  141. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLenum)minificationFilter) );
  142. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLenum)magnificationFilter) );
  143. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, (GLuint)currentTextureId) );
  144. }
  145. void Texture::generateMipmaps()
  146. {
  147. if (!_mipmapped)
  148. {
  149. GLint currentTextureId;
  150. GL_ASSERT( glGetIntegerv(GL_TEXTURE_BINDING_2D, &currentTextureId) );
  151. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, _handle) );
  152. GL_ASSERT( glGenerateMipmap(GL_TEXTURE_2D) );
  153. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, (GLuint)currentTextureId) );
  154. _mipmapped = true;
  155. }
  156. }
  157. bool Texture::isMipmapped() const
  158. {
  159. return _mipmapped;
  160. }
  161. Texture::Sampler::Sampler(Texture* texture)
  162. : _texture(texture), _wrapS(Texture::REPEAT), _wrapT(Texture::REPEAT), _magFilter(Texture::LINEAR)
  163. {
  164. _minFilter = texture->isMipmapped() ? Texture::NEAREST_MIPMAP_LINEAR : Texture::LINEAR;
  165. }
  166. Texture::Sampler::~Sampler()
  167. {
  168. SAFE_RELEASE(_texture);
  169. }
  170. Texture::Sampler* Texture::Sampler::create(Texture* texture)
  171. {
  172. assert(texture != NULL);
  173. texture->addRef();
  174. return new Sampler(texture);
  175. }
  176. Texture::Sampler* Texture::Sampler::create(const char* path, bool generateMipmaps)
  177. {
  178. Texture* texture = Texture::create(path, generateMipmaps);
  179. return texture ? new Sampler(texture) : NULL;
  180. }
  181. void Texture::Sampler::setWrapMode(Wrap wrapS, Wrap wrapT)
  182. {
  183. _wrapS = wrapS;
  184. _wrapT = wrapT;
  185. }
  186. void Texture::Sampler::setFilterMode(Filter minificationFilter, Filter magnificationFilter)
  187. {
  188. _minFilter = minificationFilter;
  189. _magFilter = magnificationFilter;
  190. }
  191. Texture* Texture::Sampler::getTexture() const
  192. {
  193. return _texture;
  194. }
  195. void Texture::Sampler::bind()
  196. {
  197. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, _texture->_handle) );
  198. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLenum)_wrapS) );
  199. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLenum)_wrapT) );
  200. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLenum)_minFilter) );
  201. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLenum)_magFilter) );
  202. }
  203. }