Texture.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #pragma once
  23. #include "Types.h"
  24. #include "Color4.h"
  25. #include "Resource.h"
  26. #include "Str.h"
  27. #include "Image.h"
  28. namespace crown
  29. {
  30. /**
  31. * Enumerates the type of a texture
  32. */
  33. enum TextureType
  34. {
  35. TT_1D = 0,
  36. TT_2D = 1,
  37. TT_3D = 2,
  38. TT_CUBEMAP = 3,
  39. TT_COUNT
  40. };
  41. enum TextureMode
  42. {
  43. TM_MODULATE = 0, // Multiplies texel color by the geometry color after lighting
  44. TM_REPLACE = 1, // Replaces the fragment color with the texel color
  45. TM_DECAL = 2, // WTF?
  46. TM_BLEND = 3, // Blends the texel color with a constant blending color
  47. TM_ADD = 4, // Adds the texel color to the fragment color
  48. TM_COUNT
  49. };
  50. /**
  51. * Enumerates the hardware filter to use when applying a texture
  52. */
  53. enum TextureFilter
  54. {
  55. TF_NEAREST = 0,
  56. TF_LINEAR = 1,
  57. TF_BILINEAR = 2,
  58. TF_TRILINEAR = 3,
  59. TF_ANISOTROPIC = 4,
  60. TF_COUNT
  61. };
  62. /**
  63. * Enumerates the wrapping mode to use when applying a texture
  64. */
  65. enum TextureWrap
  66. {
  67. TW_REPEAT = 0,
  68. TW_CLAMP = 1,
  69. TW_CLAMP_TO_EDGE = 2,
  70. TW_CLAMP_TO_BORDER = 3,
  71. TW_COUNT
  72. };
  73. class Image;
  74. /**
  75. * Abstracts a texture
  76. */
  77. class Texture : public Resource
  78. {
  79. public:
  80. //! Constructor
  81. Texture() :
  82. mType(TT_2D),
  83. mMode(TM_MODULATE),
  84. mFilter(TF_LINEAR),
  85. mWrap(TW_REPEAT),
  86. mWidth(0),
  87. mHeight(0),
  88. mGenerateMipMaps(false),
  89. mBlendColor(1.0f, 1.0f, 1.0f, 1.0f)
  90. {
  91. }
  92. //! Destructor
  93. virtual ~Texture() { }
  94. //! Returns the texture type
  95. inline TextureType GetType() const
  96. {
  97. return mType;
  98. }
  99. inline void SetType(TextureType type) { mType = type; }
  100. //! Returns the mode used by this texture
  101. inline TextureMode GetMode() const
  102. {
  103. return mMode;
  104. }
  105. //! Sets the mode used by this texture
  106. inline void SetMode(TextureMode mode)
  107. {
  108. mMode = mode;
  109. }
  110. //! Returns the filter type used by this texture
  111. inline TextureFilter GetFilter() const
  112. {
  113. return mFilter;
  114. }
  115. //! Sets the filter type used by this texture
  116. inline void SetFilter(TextureFilter filter)
  117. {
  118. mFilter = filter;
  119. }
  120. //! Returns the wrap mode used by this texture
  121. inline TextureWrap GetWrap() const
  122. {
  123. return mWrap;
  124. }
  125. //! Sets the wrap mode used by this texture
  126. inline void SetWrap(TextureWrap wrap)
  127. {
  128. mWrap = wrap;
  129. }
  130. //! Returns whether MipMaps' hardware auto generation is enabled
  131. inline bool GetGenerateMipMaps() const
  132. {
  133. return mGenerateMipMaps;
  134. }
  135. //! Sets whether MipMaps should be auto generated by the hardware (if supported)
  136. inline void SetGenerateMipMaps(bool mipmaps)
  137. {
  138. mGenerateMipMaps = mipmaps;
  139. }
  140. //! Returns the texture's blend color
  141. inline const Color4& GetBlendColor() const
  142. {
  143. return mBlendColor;
  144. }
  145. //! Sets the texture's blend color
  146. inline void SetBlendColor(const Color4& color)
  147. {
  148. mBlendColor = color;
  149. }
  150. //! Returns the texture's width
  151. inline uint32_t GetWidth() const
  152. {
  153. return mWidth;
  154. }
  155. //! Returns the texture's height
  156. inline uint32_t GetHeight() const
  157. {
  158. return mHeight;
  159. }
  160. virtual void LoadFromImage(const Image* image) = 0;
  161. virtual void LoadFromFile(const char* relativePath) = 0;
  162. virtual void LoadFromFile(const char* relativePath, Color4 colorKey) = 0;
  163. virtual void LoadFromFile(const char* relativePath, const char* alphaGreyscale) = 0;
  164. virtual Image* GetImage() const = 0;
  165. virtual PixelFormat GetTextureFormat() const = 0;
  166. protected:
  167. TextureType mType;
  168. TextureMode mMode;
  169. TextureFilter mFilter;
  170. TextureWrap mWrap;
  171. uint32_t mWidth;
  172. uint32_t mHeight;
  173. bool mGenerateMipMaps;
  174. //! It's the blending color used when mMode == TM_BLEND
  175. Color4 mBlendColor;
  176. };
  177. } // namespace crown