Texture.h 7.5 KB

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