tile.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irrlichttypes.h"
  18. #include "irr_v3d.h"
  19. #include <ITexture.h>
  20. #include <string>
  21. #include <vector>
  22. #include <SMaterial.h>
  23. #include "util/numeric.h"
  24. #include "config.h"
  25. class IGameDef;
  26. struct TileSpec;
  27. struct TileDef;
  28. namespace irr::video { class IVideoDriver; }
  29. typedef std::vector<video::SColor> Palette;
  30. /*
  31. tile.{h,cpp}: Texture handling stuff.
  32. */
  33. /*
  34. Find out the full path of an image by trying different filename
  35. extensions.
  36. If failed, return "".
  37. TODO: Should probably be moved out from here, because things needing
  38. this function do not need anything else from this header
  39. */
  40. std::string getImagePath(std::string path);
  41. /*
  42. Gets the path to a texture by first checking if the texture exists
  43. in texture_path and if not, using the data path.
  44. Checks all supported extensions by replacing the original extension.
  45. If not found, returns "".
  46. Utilizes a thread-safe cache.
  47. */
  48. std::string getTexturePath(const std::string &filename, bool *is_base_pack = nullptr);
  49. void clearTextureNameCache();
  50. /*
  51. TextureSource creates and caches textures.
  52. */
  53. class ISimpleTextureSource
  54. {
  55. public:
  56. ISimpleTextureSource() = default;
  57. virtual ~ISimpleTextureSource() = default;
  58. virtual video::ITexture* getTexture(
  59. const std::string &name, u32 *id = nullptr) = 0;
  60. };
  61. class ITextureSource : public ISimpleTextureSource
  62. {
  63. public:
  64. ITextureSource() = default;
  65. virtual ~ITextureSource() = default;
  66. virtual u32 getTextureId(const std::string &name)=0;
  67. virtual std::string getTextureName(u32 id)=0;
  68. virtual video::ITexture* getTexture(u32 id)=0;
  69. virtual video::ITexture* getTexture(
  70. const std::string &name, u32 *id = nullptr)=0;
  71. virtual video::ITexture* getTextureForMesh(
  72. const std::string &name, u32 *id = nullptr) = 0;
  73. /*!
  74. * Returns a palette from the given texture name.
  75. * The pointer is valid until the texture source is
  76. * destructed.
  77. * Should be called from the main thread.
  78. */
  79. virtual Palette* getPalette(const std::string &name) = 0;
  80. virtual bool isKnownSourceImage(const std::string &name)=0;
  81. virtual video::ITexture* getNormalTexture(const std::string &name)=0;
  82. virtual video::SColor getTextureAverageColor(const std::string &name)=0;
  83. virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
  84. };
  85. class IWritableTextureSource : public ITextureSource
  86. {
  87. public:
  88. IWritableTextureSource() = default;
  89. virtual ~IWritableTextureSource() = default;
  90. virtual u32 getTextureId(const std::string &name)=0;
  91. virtual std::string getTextureName(u32 id)=0;
  92. virtual video::ITexture* getTexture(u32 id)=0;
  93. virtual video::ITexture* getTexture(
  94. const std::string &name, u32 *id = nullptr)=0;
  95. virtual bool isKnownSourceImage(const std::string &name)=0;
  96. virtual void processQueue()=0;
  97. virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
  98. virtual void rebuildImagesAndTextures()=0;
  99. virtual video::ITexture* getNormalTexture(const std::string &name)=0;
  100. virtual video::SColor getTextureAverageColor(const std::string &name)=0;
  101. virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
  102. };
  103. IWritableTextureSource *createTextureSource();
  104. video::IImage *Align2Npot2(video::IImage *image, video::IVideoDriver *driver);
  105. enum MaterialType{
  106. TILE_MATERIAL_BASIC,
  107. TILE_MATERIAL_ALPHA,
  108. TILE_MATERIAL_LIQUID_TRANSPARENT,
  109. TILE_MATERIAL_LIQUID_OPAQUE,
  110. TILE_MATERIAL_WAVING_LEAVES,
  111. TILE_MATERIAL_WAVING_PLANTS,
  112. TILE_MATERIAL_OPAQUE,
  113. TILE_MATERIAL_WAVING_LIQUID_BASIC,
  114. TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT,
  115. TILE_MATERIAL_WAVING_LIQUID_OPAQUE,
  116. TILE_MATERIAL_PLAIN,
  117. TILE_MATERIAL_PLAIN_ALPHA
  118. };
  119. // Material flags
  120. // Should backface culling be enabled?
  121. #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
  122. // Should a crack be drawn?
  123. #define MATERIAL_FLAG_CRACK 0x02
  124. // Should the crack be drawn on transparent pixels (unset) or not (set)?
  125. // Ignored if MATERIAL_FLAG_CRACK is not set.
  126. #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
  127. #define MATERIAL_FLAG_ANIMATION 0x08
  128. //#define MATERIAL_FLAG_HIGHLIGHTED 0x10
  129. #define MATERIAL_FLAG_TILEABLE_HORIZONTAL 0x20
  130. #define MATERIAL_FLAG_TILEABLE_VERTICAL 0x40
  131. /*
  132. This fully defines the looks of a tile.
  133. The SMaterial of a tile is constructed according to this.
  134. */
  135. struct FrameSpec
  136. {
  137. FrameSpec() = default;
  138. u32 texture_id = 0;
  139. video::ITexture *texture = nullptr;
  140. video::ITexture *normal_texture = nullptr;
  141. video::ITexture *flags_texture = nullptr;
  142. };
  143. #define MAX_TILE_LAYERS 2
  144. //! Defines a layer of a tile.
  145. struct TileLayer
  146. {
  147. TileLayer() = default;
  148. /*!
  149. * Two layers are equal if they can be merged.
  150. */
  151. bool operator==(const TileLayer &other) const
  152. {
  153. return
  154. texture_id == other.texture_id &&
  155. material_type == other.material_type &&
  156. material_flags == other.material_flags &&
  157. has_color == other.has_color &&
  158. color == other.color &&
  159. scale == other.scale;
  160. }
  161. /*!
  162. * Two tiles are not equal if they must have different vertices.
  163. */
  164. bool operator!=(const TileLayer &other) const
  165. {
  166. return !(*this == other);
  167. }
  168. // Sets everything else except the texture in the material
  169. void applyMaterialOptions(video::SMaterial &material) const
  170. {
  171. switch (material_type) {
  172. case TILE_MATERIAL_OPAQUE:
  173. case TILE_MATERIAL_LIQUID_OPAQUE:
  174. case TILE_MATERIAL_WAVING_LIQUID_OPAQUE:
  175. material.MaterialType = video::EMT_SOLID;
  176. break;
  177. case TILE_MATERIAL_BASIC:
  178. case TILE_MATERIAL_WAVING_LEAVES:
  179. case TILE_MATERIAL_WAVING_PLANTS:
  180. case TILE_MATERIAL_WAVING_LIQUID_BASIC:
  181. material.MaterialTypeParam = 0.5;
  182. material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
  183. break;
  184. case TILE_MATERIAL_ALPHA:
  185. case TILE_MATERIAL_LIQUID_TRANSPARENT:
  186. case TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT:
  187. material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
  188. break;
  189. default:
  190. break;
  191. }
  192. material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0;
  193. if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
  194. material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
  195. }
  196. if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
  197. material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
  198. }
  199. }
  200. void applyMaterialOptionsWithShaders(video::SMaterial &material) const
  201. {
  202. material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0;
  203. if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
  204. material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
  205. material.TextureLayer[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
  206. }
  207. if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
  208. material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
  209. material.TextureLayer[1].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
  210. }
  211. }
  212. bool isTransparent() const
  213. {
  214. switch (material_type) {
  215. case TILE_MATERIAL_ALPHA:
  216. case TILE_MATERIAL_LIQUID_TRANSPARENT:
  217. case TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT:
  218. return true;
  219. }
  220. return false;
  221. }
  222. // Ordered for size, please do not reorder
  223. video::ITexture *texture = nullptr;
  224. video::ITexture *normal_texture = nullptr;
  225. video::ITexture *flags_texture = nullptr;
  226. u32 shader_id = 0;
  227. u32 texture_id = 0;
  228. u16 animation_frame_length_ms = 0;
  229. u16 animation_frame_count = 1;
  230. u8 material_type = TILE_MATERIAL_BASIC;
  231. u8 material_flags =
  232. //0 // <- DEBUG, Use the one below
  233. MATERIAL_FLAG_BACKFACE_CULLING |
  234. MATERIAL_FLAG_TILEABLE_HORIZONTAL|
  235. MATERIAL_FLAG_TILEABLE_VERTICAL;
  236. //! If true, the tile has its own color.
  237. bool has_color = false;
  238. std::vector<FrameSpec> *frames = nullptr;
  239. /*!
  240. * The color of the tile, or if the tile does not own
  241. * a color then the color of the node owning this tile.
  242. */
  243. video::SColor color = video::SColor(0, 0, 0, 0);
  244. u8 scale = 1;
  245. };
  246. enum class TileRotation: u8 {
  247. None,
  248. R90,
  249. R180,
  250. R270,
  251. };
  252. /*!
  253. * Defines a face of a node. May have up to two layers.
  254. */
  255. struct TileSpec
  256. {
  257. TileSpec() = default;
  258. //! If true, the tile rotation is ignored.
  259. bool world_aligned = false;
  260. //! Tile rotation.
  261. TileRotation rotation = TileRotation::None;
  262. //! This much light does the tile emit.
  263. u8 emissive_light = 0;
  264. //! The first is base texture, the second is overlay.
  265. TileLayer layers[MAX_TILE_LAYERS];
  266. };
  267. std::vector<std::string> getTextureDirs();