Texture.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #ifndef TEXTURE_H_
  2. #define TEXTURE_H_
  3. #include "Ref.h"
  4. #include "Stream.h"
  5. namespace gameplay
  6. {
  7. class Image;
  8. /**
  9. * Represents a texture.
  10. */
  11. class Texture : public Ref
  12. {
  13. friend class Sampler;
  14. public:
  15. /**
  16. * Defines the set of supported texture formats.
  17. */
  18. enum Format
  19. {
  20. UNKNOWN = 0,
  21. RGB = GL_RGB,
  22. RGBA = GL_RGBA,
  23. ALPHA = GL_ALPHA
  24. };
  25. /**
  26. * Defines the set of supported texture filters.
  27. */
  28. enum Filter
  29. {
  30. NEAREST = GL_NEAREST,
  31. LINEAR = GL_LINEAR,
  32. NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST,
  33. LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST,
  34. NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR,
  35. LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR
  36. };
  37. /**
  38. * Defines the set of supported texture wrapping modes.
  39. */
  40. enum Wrap
  41. {
  42. REPEAT = GL_REPEAT,
  43. CLAMP = GL_CLAMP_TO_EDGE
  44. };
  45. /**
  46. * Defines a texture sampler.
  47. *
  48. * A texture sampler is basically an instance of a texture that can be
  49. * used to sample a texture from a material. In addition to the texture
  50. * itself, a sampler stores per-instance texture state information, such
  51. * as wrap and filter modes.
  52. */
  53. class Sampler : public Ref
  54. {
  55. friend class Texture;
  56. public:
  57. /**
  58. * Destructor.
  59. */
  60. virtual ~Sampler();
  61. /**
  62. * Creates a sampler for the specified texture.
  63. *
  64. * @param texture The texture.
  65. *
  66. * @return The new sampler.
  67. * @script{create}
  68. */
  69. static Sampler* create(Texture* texture);
  70. /**
  71. * Creates a sampler for the specified texture.
  72. *
  73. * @param path Path to the texture to create a sampler for.
  74. * @param generateMipmaps True to force a full mipmap chain to be generated for the texture, false otherwise.
  75. *
  76. * @return The new sampler.
  77. * @script{create}
  78. */
  79. static Sampler* create(const char* path, bool generateMipmaps = false);
  80. /**
  81. * Sets the wrap mode for this sampler.
  82. *
  83. * @param wrapS The horizontal wrap mode.
  84. * @param wrapT The vertical wrap mode.
  85. */
  86. void setWrapMode(Wrap wrapS, Wrap wrapT);
  87. /**
  88. * Sets the texture filter modes for this sampler.
  89. *
  90. * @param minificationFilter The texture minification filter.
  91. * @param magnificationFilter The texture magnification filter.
  92. */
  93. void setFilterMode(Filter minificationFilter, Filter magnificationFilter);
  94. /**
  95. * Gets the texture for this sampler.
  96. *
  97. * @return The texture for this sampler.
  98. */
  99. Texture* getTexture() const;
  100. /**
  101. * Binds the texture of this sampler to the renderer and applies the sampler state.
  102. */
  103. void bind();
  104. private:
  105. /**
  106. * Constructor.
  107. */
  108. Sampler(Texture* texture);
  109. /**
  110. * Hidden copy assignment operator.
  111. */
  112. Sampler& operator=(const Sampler&);
  113. Texture* _texture;
  114. Wrap _wrapS;
  115. Wrap _wrapT;
  116. Filter _minFilter;
  117. Filter _magFilter;
  118. };
  119. /**
  120. * Creates a texture from the given image resource.
  121. *
  122. * Note that for textures that include mipmap data in the source data (such as most compressed textures),
  123. * the generateMipmaps flags should NOT be set to true.
  124. *
  125. * @param path The image resource path.
  126. * @param generateMipmaps true to auto-generate a full mipmap chain, false otherwise.
  127. *
  128. * @return The new texture, or NULL if the texture could not be loaded/created.
  129. * @script{create}
  130. */
  131. static Texture* create(const char* path, bool generateMipmaps = false);
  132. /**
  133. * Creates a texture from the given image.
  134. *
  135. * @param image The image containing the texture data.
  136. * @param generateMipmaps True to generate a full mipmap chain, false otherwise.
  137. *
  138. * @return The new texture, or NULL if the image is not of a supported texture format.
  139. * @script{create}
  140. */
  141. static Texture* create(Image* image, bool generateMipmaps = false);
  142. /**
  143. * Creates a texture from the given texture data.
  144. *
  145. * The data in the texture is expected to be tightly packed (no padding at the end of rows).
  146. *
  147. * @param format Format of the texture data.
  148. * @param width Width of the texture data.
  149. * @param height Height of the texture data.
  150. * @param data Raw texture data (expected to be tightly packed).
  151. * @param generateMipmaps True to generate a full mipmap chain, false otherwise.
  152. *
  153. * @return The new texture.
  154. * @script{create}
  155. */
  156. static Texture* create(Format format, unsigned int width, unsigned int height, const unsigned char* data, bool generateMipmaps = false);
  157. /**
  158. * Creates a texture object to wrap the specified pre-created native texture handle.
  159. *
  160. * The specified TextureHandle must represent a valid texture that has been created
  161. * on the underlying renderer and it should not be referenced by any other Texture
  162. * object. When the returned Texture object is destroyed, the passed in TextureHandle
  163. * will also be destroyed.
  164. *
  165. * @param handle Native texture handle.
  166. * @param width The width of the texture represented by 'handle'.
  167. * @param height The height of the texture represented by 'handle'.
  168. * @param format Optionally, the format of the texture represented by 'handle'.
  169. * If the format cannot be represented by any of the Texture::Format values,
  170. * use a value of UNKNOWN.
  171. *
  172. * @return The new texture.
  173. * @script{create}
  174. */
  175. static Texture* create(TextureHandle handle, int width, int height, Format format = UNKNOWN);
  176. /**
  177. * Returns the path that the texture was originally loaded from (if applicable).
  178. *
  179. * @return The texture path, or an empty string if the texture was not loaded from file.
  180. */
  181. const char* getPath() const;
  182. /**
  183. * Gets the format of the texture.
  184. *
  185. * @return The texture format.
  186. */
  187. Format getFormat() const;
  188. /**
  189. * Gets the texture width.
  190. *
  191. * @return The texture width.
  192. */
  193. unsigned int getWidth() const;
  194. /**
  195. * Gets the texture height.
  196. *
  197. * @return The texture height.
  198. */
  199. unsigned int getHeight() const;
  200. /**
  201. * Generates a full mipmap chain for this texture if it isn't already mipmapped.
  202. */
  203. void generateMipmaps();
  204. /**
  205. * Determines if this texture currently contains a full mipmap chain.
  206. *
  207. * @return True if this texture is currently mipmapped, false otherwise.
  208. */
  209. bool isMipmapped() const;
  210. /**
  211. * Determines if this texture is a compressed texture.
  212. */
  213. bool isCompressed() const;
  214. /**
  215. * Returns the texture handle.
  216. *
  217. * @return The texture handle.
  218. */
  219. TextureHandle getHandle() const;
  220. private:
  221. /**
  222. * Constructor.
  223. */
  224. Texture();
  225. /**
  226. * Copy constructor.
  227. */
  228. Texture(const Texture& copy);
  229. /**
  230. * Destructor.
  231. */
  232. virtual ~Texture();
  233. /**
  234. * Hidden copy assignment operator.
  235. */
  236. Texture& operator=(const Texture&);
  237. static Texture* createCompressedPVRTC(const char* path);
  238. static Texture* createCompressedDDS(const char* path);
  239. static GLubyte* readCompressedPVRTC(const char* path, Stream* stream, GLsizei* width, GLsizei* height, GLenum* format, unsigned int* mipMapCount);
  240. static GLubyte* readCompressedPVRTCLegacy(const char* path, Stream* stream, GLsizei* width, GLsizei* height, GLenum* format, unsigned int* mipMapCount);
  241. static int getMaskByteIndex(unsigned int mask);
  242. std::string _path;
  243. TextureHandle _handle;
  244. Format _format;
  245. unsigned int _width;
  246. unsigned int _height;
  247. bool _mipmapped;
  248. bool _cached;
  249. bool _compressed;
  250. Wrap _wrapS;
  251. Wrap _wrapT;
  252. Filter _minFilter;
  253. Filter _magFilter;
  254. };
  255. }
  256. #endif