tile.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #if ENABLE_GLES
  26. #include <IVideoDriver.h>
  27. #endif
  28. class IGameDef;
  29. struct TileSpec;
  30. struct TileDef;
  31. typedef std::vector<video::SColor> Palette;
  32. /*
  33. tile.{h,cpp}: Texture handling stuff.
  34. */
  35. /*
  36. Find out the full path of an image by trying different filename
  37. extensions.
  38. If failed, return "".
  39. TODO: Should probably be moved out from here, because things needing
  40. this function do not need anything else from this header
  41. */
  42. std::string getImagePath(std::string path);
  43. /*
  44. Gets the path to a texture by first checking if the texture exists
  45. in texture_path and if not, using the data path.
  46. Checks all supported extensions by replacing the original extension.
  47. If not found, returns "".
  48. Utilizes a thread-safe cache.
  49. */
  50. std::string getTexturePath(const std::string &filename, bool *is_base_pack = nullptr);
  51. void clearTextureNameCache();
  52. /*
  53. TextureSource creates and caches textures.
  54. */
  55. class ISimpleTextureSource
  56. {
  57. public:
  58. ISimpleTextureSource() = default;
  59. virtual ~ISimpleTextureSource() = default;
  60. virtual video::ITexture* getTexture(
  61. const std::string &name, u32 *id = nullptr) = 0;
  62. };
  63. class ITextureSource : public ISimpleTextureSource
  64. {
  65. public:
  66. ITextureSource() = default;
  67. virtual ~ITextureSource() = default;
  68. virtual u32 getTextureId(const std::string &name)=0;
  69. virtual std::string getTextureName(u32 id)=0;
  70. virtual video::ITexture* getTexture(u32 id)=0;
  71. virtual video::ITexture* getTexture(
  72. const std::string &name, u32 *id = nullptr)=0;
  73. virtual video::ITexture* getTextureForMesh(
  74. const std::string &name, u32 *id = nullptr) = 0;
  75. /*!
  76. * Returns a palette from the given texture name.
  77. * The pointer is valid until the texture source is
  78. * destructed.
  79. * Should be called from the main thread.
  80. */
  81. virtual Palette* getPalette(const std::string &name) = 0;
  82. virtual bool isKnownSourceImage(const std::string &name)=0;
  83. virtual video::ITexture* getNormalTexture(const std::string &name)=0;
  84. virtual video::SColor getTextureAverageColor(const std::string &name)=0;
  85. virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
  86. };
  87. class IWritableTextureSource : public ITextureSource
  88. {
  89. public:
  90. IWritableTextureSource() = default;
  91. virtual ~IWritableTextureSource() = default;
  92. virtual u32 getTextureId(const std::string &name)=0;
  93. virtual std::string getTextureName(u32 id)=0;
  94. virtual video::ITexture* getTexture(u32 id)=0;
  95. virtual video::ITexture* getTexture(
  96. const std::string &name, u32 *id = nullptr)=0;
  97. virtual bool isKnownSourceImage(const std::string &name)=0;
  98. virtual void processQueue()=0;
  99. virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
  100. virtual void rebuildImagesAndTextures()=0;
  101. virtual video::ITexture* getNormalTexture(const std::string &name)=0;
  102. virtual video::SColor getTextureAverageColor(const std::string &name)=0;
  103. virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
  104. };
  105. IWritableTextureSource *createTextureSource();
  106. #if ENABLE_GLES
  107. video::IImage *Align2Npot2(video::IImage *image, video::IVideoDriver *driver);
  108. #endif
  109. enum MaterialType{
  110. TILE_MATERIAL_BASIC,
  111. TILE_MATERIAL_ALPHA,
  112. TILE_MATERIAL_LIQUID_TRANSPARENT,
  113. TILE_MATERIAL_LIQUID_OPAQUE,
  114. TILE_MATERIAL_WAVING_LEAVES,
  115. TILE_MATERIAL_WAVING_PLANTS,
  116. TILE_MATERIAL_OPAQUE,
  117. TILE_MATERIAL_WAVING_LIQUID_BASIC,
  118. TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT,
  119. TILE_MATERIAL_WAVING_LIQUID_OPAQUE,
  120. TILE_MATERIAL_PLAIN,
  121. TILE_MATERIAL_PLAIN_ALPHA
  122. };
  123. // Material flags
  124. // Should backface culling be enabled?
  125. #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
  126. // Should a crack be drawn?
  127. #define MATERIAL_FLAG_CRACK 0x02
  128. // Should the crack be drawn on transparent pixels (unset) or not (set)?
  129. // Ignored if MATERIAL_FLAG_CRACK is not set.
  130. #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
  131. #define MATERIAL_FLAG_ANIMATION 0x08
  132. //#define MATERIAL_FLAG_HIGHLIGHTED 0x10
  133. #define MATERIAL_FLAG_TILEABLE_HORIZONTAL 0x20
  134. #define MATERIAL_FLAG_TILEABLE_VERTICAL 0x40
  135. /*
  136. This fully defines the looks of a tile.
  137. The SMaterial of a tile is constructed according to this.
  138. */
  139. struct FrameSpec
  140. {
  141. FrameSpec() = default;
  142. u32 texture_id = 0;
  143. video::ITexture *texture = nullptr;
  144. video::ITexture *normal_texture = nullptr;
  145. video::ITexture *flags_texture = nullptr;
  146. };
  147. #define MAX_TILE_LAYERS 2
  148. //! Defines a layer of a tile.
  149. struct TileLayer
  150. {
  151. TileLayer() = default;
  152. /*!
  153. * Two layers are equal if they can be merged.
  154. */
  155. bool operator==(const TileLayer &other) const
  156. {
  157. return
  158. texture_id == other.texture_id &&
  159. material_type == other.material_type &&
  160. material_flags == other.material_flags &&
  161. has_color == other.has_color &&
  162. color == other.color &&
  163. scale == other.scale;
  164. }
  165. /*!
  166. * Two tiles are not equal if they must have different vertices.
  167. */
  168. bool operator!=(const TileLayer &other) const
  169. {
  170. return !(*this == other);
  171. }
  172. // Sets everything else except the texture in the material
  173. void applyMaterialOptions(video::SMaterial &material) const
  174. {
  175. switch (material_type) {
  176. case TILE_MATERIAL_OPAQUE:
  177. case TILE_MATERIAL_LIQUID_OPAQUE:
  178. case TILE_MATERIAL_WAVING_LIQUID_OPAQUE:
  179. material.MaterialType = video::EMT_SOLID;
  180. break;
  181. case TILE_MATERIAL_BASIC:
  182. case TILE_MATERIAL_WAVING_LEAVES:
  183. case TILE_MATERIAL_WAVING_PLANTS:
  184. case TILE_MATERIAL_WAVING_LIQUID_BASIC:
  185. material.MaterialTypeParam = 0.5;
  186. material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
  187. break;
  188. case TILE_MATERIAL_ALPHA:
  189. case TILE_MATERIAL_LIQUID_TRANSPARENT:
  190. case TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT:
  191. material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
  192. break;
  193. default:
  194. break;
  195. }
  196. material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0;
  197. if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
  198. material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
  199. }
  200. if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
  201. material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
  202. }
  203. }
  204. void applyMaterialOptionsWithShaders(video::SMaterial &material) const
  205. {
  206. material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0;
  207. if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
  208. material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
  209. material.TextureLayer[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
  210. }
  211. if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
  212. material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
  213. material.TextureLayer[1].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
  214. }
  215. }
  216. bool isTileable() const
  217. {
  218. return (material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)
  219. && (material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL);
  220. }
  221. bool isTransparent() const
  222. {
  223. switch (material_type) {
  224. case TILE_MATERIAL_ALPHA:
  225. case TILE_MATERIAL_LIQUID_TRANSPARENT:
  226. case TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT:
  227. return true;
  228. }
  229. return false;
  230. }
  231. // Ordered for size, please do not reorder
  232. video::ITexture *texture = nullptr;
  233. video::ITexture *normal_texture = nullptr;
  234. video::ITexture *flags_texture = nullptr;
  235. u32 shader_id = 0;
  236. u32 texture_id = 0;
  237. u16 animation_frame_length_ms = 0;
  238. u16 animation_frame_count = 1;
  239. u8 material_type = TILE_MATERIAL_BASIC;
  240. u8 material_flags =
  241. //0 // <- DEBUG, Use the one below
  242. MATERIAL_FLAG_BACKFACE_CULLING |
  243. MATERIAL_FLAG_TILEABLE_HORIZONTAL|
  244. MATERIAL_FLAG_TILEABLE_VERTICAL;
  245. //! If true, the tile has its own color.
  246. bool has_color = false;
  247. std::vector<FrameSpec> *frames = nullptr;
  248. /*!
  249. * The color of the tile, or if the tile does not own
  250. * a color then the color of the node owning this tile.
  251. */
  252. video::SColor color = video::SColor(0, 0, 0, 0);
  253. u8 scale = 1;
  254. };
  255. /*!
  256. * Defines a face of a node. May have up to two layers.
  257. */
  258. struct TileSpec
  259. {
  260. TileSpec() = default;
  261. /*!
  262. * Returns true if this tile can be merged with the other tile.
  263. */
  264. bool isTileable(const TileSpec &other) const {
  265. for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
  266. if (layers[layer] != other.layers[layer])
  267. return false;
  268. // Only non-transparent tiles can be merged into fast faces
  269. if (layers[layer].isTransparent() || !layers[layer].isTileable())
  270. return false;
  271. }
  272. return rotation == 0
  273. && rotation == other.rotation
  274. && emissive_light == other.emissive_light;
  275. }
  276. //! If true, the tile rotation is ignored.
  277. bool world_aligned = false;
  278. //! Tile rotation.
  279. u8 rotation = 0;
  280. //! This much light does the tile emit.
  281. u8 emissive_light = 0;
  282. //! The first is base texture, the second is overlay.
  283. TileLayer layers[MAX_TILE_LAYERS];
  284. };
  285. std::vector<std::string> getTextureDirs();