Texture.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. * TODO: Addd support for the following:
  11. * COMPRESSED_RGBA_ATITC = GL_ATC_RGBA_EXPLICIT_ALPHA_AMD,
  12. * COMPRESSED_RGBA_DXT1 = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
  13. */
  14. class Texture : public Ref
  15. {
  16. friend class Sampler;
  17. public:
  18. /**
  19. * Defines the set of supported texture formats.
  20. */
  21. enum Format
  22. {
  23. RGB = GL_RGB,
  24. RGBA = GL_RGBA,
  25. ALPHA = GL_ALPHA,
  26. DEPTH = GL_DEPTH_COMPONENT,
  27. #ifdef OPENGL_ES_PVR
  28. COMPRESSED_RGB_PVRTC_4BPP = GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG,
  29. COMPRESSED_RGBA_PVRTC_4BPP = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG,
  30. COMPRESSED_RGB_PVRTC_2BPP = GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG,
  31. COMPRESSED_RGBA_PVRTC_2BPP = GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG
  32. #endif
  33. };
  34. /**
  35. * Defines the set of supported texture filters.
  36. */
  37. enum Filter
  38. {
  39. NEAREST = GL_NEAREST,
  40. LINEAR = GL_LINEAR,
  41. NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST,
  42. LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST,
  43. NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR,
  44. LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR
  45. };
  46. /**
  47. * Defines the set of supported texture wrapping modes.
  48. */
  49. enum Wrap
  50. {
  51. REPEAT = GL_REPEAT,
  52. CLAMP = GL_CLAMP_TO_EDGE
  53. };
  54. /**
  55. * Defnies a texture sampler.
  56. *
  57. * A texture sampler is basically an instance of a texture that can be
  58. * used to sample a texture from a material. In addition to the texture
  59. * itself, a sampler stores per-instance texture state information, such
  60. * as wrap and filter modes.
  61. */
  62. class Sampler : public Ref
  63. {
  64. friend class Texture;
  65. public:
  66. /**
  67. * Creates a sampler for the specified texture.
  68. *
  69. * @param texture The texture.
  70. *
  71. * @return The new sampler.
  72. */
  73. static Sampler* create(Texture* texture);
  74. /**
  75. * Creates a sampler for the specified texture.
  76. *
  77. * @param path Path to the texture to create a sampler for.
  78. * @param generateMipmaps True to force a full mipmap chain to be generated for the texture, false otherwise.
  79. *
  80. * @return The new sampler.
  81. */
  82. static Sampler* create(const char* path, bool generateMipmaps = false);
  83. /**
  84. * Sets the wrap mode for this sampler.
  85. *
  86. * @param wrapS The horizontal wrap mode.
  87. * @param wrapT The vertical wrap mode.
  88. */
  89. void setWrapMode(Wrap wrapS, Wrap wrapT);
  90. /**
  91. * Sets the texture filter modes for this sampler.
  92. *
  93. * @param minificationFilter The texture minification filter.
  94. * @param magnificationFilter The texture magnification filter.
  95. */
  96. void setFilterMode(Filter minificationFilter, Filter magnificationFilter);
  97. /**
  98. * Returns the texture for this sampler.
  99. */
  100. Texture* getTexture() const;
  101. /**
  102. * Binds the texture of this sampler to the renderer and applies the sampler state.
  103. */
  104. void bind();
  105. private:
  106. Sampler(Texture* texture);
  107. ~Sampler();
  108. Texture* _texture;
  109. Wrap _wrapS;
  110. Wrap _wrapT;
  111. Filter _minFilter;
  112. Filter _magFilter;
  113. };
  114. /**
  115. * Creates a texture from the given image resource.
  116. *
  117. * @param path The image resource path.
  118. * @param generateMipmaps true to auto-generate a full mipmap chain, false otherwise.
  119. *
  120. * @return The new texture, or NULL if the texture could not be loaded/created.
  121. */
  122. static Texture* create(const char* path, bool generateMipmaps = false);
  123. /**
  124. * Creates a texture from the given image.
  125. */
  126. static Texture* create(Image* image, bool generateMipmaps = false);
  127. /**
  128. * Creates a texture from the given texture data.
  129. */
  130. static Texture* create(Format format, unsigned int width, unsigned int height, unsigned char* data, bool generateMipmaps = false);
  131. /**
  132. * Returns the texture width.
  133. */
  134. unsigned int getWidth() const;
  135. /**
  136. * Returns the texture height.
  137. */
  138. unsigned int getHeight() const;
  139. /**
  140. * Sets the wrap mode for this texture.
  141. *
  142. * @param wrapS Horizontal wrapping mode for the texture.
  143. * @param wrapT Vertical wrapping mode for the texture.
  144. */
  145. void setWrapMode(Wrap wrapS, Wrap wrapT);
  146. /**
  147. * Sets the minification and magnification filter modes for this texture.
  148. *
  149. * @param minificationFilter New texture minification filter.
  150. * @param magnificationFilter New texture magnification filter.
  151. */
  152. void setFilterMode(Filter minificationFilter, Filter magnificationFilter);
  153. /**
  154. * Generates a full mipmap chain for this texture if it isn't already mipmapped.
  155. */
  156. void generateMipmaps();
  157. /**
  158. * Determines if this texture currently contains a full mipmap chain.
  159. *
  160. * @return True if this texture is currently mipmapped, false otherwise.
  161. */
  162. bool isMipmapped() const;
  163. /**
  164. * Returns the texture handle.
  165. *
  166. * @return The texture handle.
  167. */
  168. TextureHandle getHandle() const;
  169. private:
  170. /**
  171. * Constructor.
  172. */
  173. Texture();
  174. /**
  175. * Copy constructor.
  176. */
  177. Texture(const Texture& copy);
  178. /**
  179. * Destructor.
  180. */
  181. virtual ~Texture();
  182. #ifdef OPENGL_ES_PVR
  183. static Texture* createCompressedPVR(const char* path);
  184. #endif
  185. std::string _path;
  186. TextureHandle _handle;
  187. unsigned int _width;
  188. unsigned int _height;
  189. bool _mipmapped;
  190. bool _cached;
  191. };
  192. }
  193. #endif