Texture.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "Shared.h"
  27. #include "Str.h"
  28. #include "Image.h"
  29. namespace crown
  30. {
  31. /**
  32. * Enumerates the type of a texture
  33. */
  34. enum TextureType
  35. {
  36. TT_1D = 0,
  37. TT_2D = 1,
  38. TT_3D = 2,
  39. TT_CUBEMAP = 3,
  40. TT_COUNT
  41. };
  42. enum TextureMode
  43. {
  44. TM_MODULATE = 0, // Multiplies texel color by the geometry color after lighting
  45. TM_REPLACE = 1, // Replaces the fragment color with the texel color
  46. TM_DECAL = 2, // WTF?
  47. TM_BLEND = 3, // Blends the texel color with a constant blending color
  48. TM_ADD = 4, // Adds the texel color to the fragment color
  49. TM_COUNT
  50. };
  51. /**
  52. * Enumerates the hardware filter to use when applying a texture
  53. */
  54. enum TextureFilter
  55. {
  56. TF_NEAREST = 0,
  57. TF_LINEAR = 1,
  58. TF_BILINEAR = 2,
  59. TF_TRILINEAR = 3,
  60. TF_ANISOTROPIC = 4,
  61. TF_COUNT
  62. };
  63. /**
  64. * Enumerates the wrapping mode to use when applying a texture
  65. */
  66. enum TextureWrap
  67. {
  68. TW_REPEAT = 0,
  69. TW_CLAMP = 1,
  70. TW_CLAMP_TO_EDGE = 2,
  71. TW_CLAMP_TO_BORDER = 3,
  72. TW_COUNT
  73. };
  74. class Image;
  75. /**
  76. * Abstracts a texture
  77. */
  78. class Texture : public Resource
  79. {
  80. public:
  81. //! Constructor
  82. Texture() :
  83. mType(TT_2D),
  84. mMode(TM_MODULATE),
  85. mFilter(TF_LINEAR),
  86. mWrap(TW_REPEAT),
  87. mWidth(0),
  88. mHeight(0),
  89. mGenerateMipMaps(false),
  90. mBlendColor(1.0f, 1.0f, 1.0f, 1.0f)
  91. {
  92. }
  93. //! Destructor
  94. virtual ~Texture() { }
  95. //! Returns the texture type
  96. inline TextureType GetType() const
  97. {
  98. return mType;
  99. }
  100. inline void SetType(TextureType type) { mType = type; }
  101. //! Returns the mode used by this texture
  102. inline TextureMode GetMode() const
  103. {
  104. return mMode;
  105. }
  106. //! Sets the mode used by this texture
  107. inline void SetMode(TextureMode mode)
  108. {
  109. mMode = mode;
  110. }
  111. //! Returns the filter type used by this texture
  112. inline TextureFilter GetFilter() const
  113. {
  114. return mFilter;
  115. }
  116. //! Sets the filter type used by this texture
  117. inline void SetFilter(TextureFilter filter)
  118. {
  119. mFilter = filter;
  120. }
  121. //! Returns the wrap mode used by this texture
  122. inline TextureWrap GetWrap() const
  123. {
  124. return mWrap;
  125. }
  126. //! Sets the wrap mode used by this texture
  127. inline void SetWrap(TextureWrap wrap)
  128. {
  129. mWrap = wrap;
  130. }
  131. //! Returns whether MipMaps' hardware auto generation is enabled
  132. inline bool GetGenerateMipMaps() const
  133. {
  134. return mGenerateMipMaps;
  135. }
  136. //! Sets whether MipMaps should be auto generated by the hardware (if supported)
  137. inline void SetGenerateMipMaps(bool mipmaps)
  138. {
  139. mGenerateMipMaps = mipmaps;
  140. }
  141. //! Returns the texture's blend color
  142. inline const Color4& GetBlendColor() const
  143. {
  144. return mBlendColor;
  145. }
  146. //! Sets the texture's blend color
  147. inline void SetBlendColor(const Color4& color)
  148. {
  149. mBlendColor = color;
  150. }
  151. //! Returns the texture's width
  152. inline uint GetWidth() const
  153. {
  154. return mWidth;
  155. }
  156. //! Returns the texture's height
  157. inline uint GetHeight() const
  158. {
  159. return mHeight;
  160. }
  161. virtual void LoadFromImage(const Image* image) = 0;
  162. virtual void LoadFromFile(const char* relativePath) = 0;
  163. virtual void LoadFromFile(const char* relativePath, Color4 colorKey) = 0;
  164. virtual void LoadFromFile(const char* relativePath, const char* alphaGreyscale) = 0;
  165. virtual Image* GetImage() const = 0;
  166. virtual PixelFormat GetTextureFormat() const = 0;
  167. protected:
  168. TextureType mType;
  169. TextureMode mMode;
  170. TextureFilter mFilter;
  171. TextureWrap mWrap;
  172. uint mWidth;
  173. uint mHeight;
  174. bool mGenerateMipMaps;
  175. //! It's the blending color used when mMode == TM_BLEND
  176. Color4 mBlendColor;
  177. };
  178. } // namespace crown