tile.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #ifndef TILE_HEADER
  17. #define TILE_HEADER
  18. #include "irrlichttypes.h"
  19. #include "irr_v2d.h"
  20. #include "irr_v3d.h"
  21. #include <ITexture.h>
  22. #include <IrrlichtDevice.h>
  23. #include "threads.h"
  24. #include <string>
  25. #include <map>
  26. class IGameDef;
  27. /*
  28. tile.{h,cpp}: Texture handling stuff.
  29. */
  30. /*
  31. Find out the full path of an image by trying different filename
  32. extensions.
  33. If failed, return "".
  34. TODO: Should probably be moved out from here, because things needing
  35. this function do not need anything else from this header
  36. */
  37. std::string getImagePath(std::string path);
  38. /*
  39. Gets the path to a texture by first checking if the texture exists
  40. in texture_path and if not, using the data path.
  41. Checks all supported extensions by replacing the original extension.
  42. If not found, returns "".
  43. Utilizes a thread-safe cache.
  44. */
  45. std::string getTexturePath(const std::string &filename);
  46. void clearTextureNameCache();
  47. /*
  48. ITextureSource::generateTextureFromMesh parameters
  49. */
  50. namespace irr {namespace scene {class IMesh;}}
  51. struct TextureFromMeshParams
  52. {
  53. scene::IMesh *mesh;
  54. core::dimension2d<u32> dim;
  55. std::string rtt_texture_name;
  56. bool delete_texture_on_shutdown;
  57. v3f camera_position;
  58. v3f camera_lookat;
  59. core::CMatrix4<f32> camera_projection_matrix;
  60. video::SColorf ambient_light;
  61. v3f light_position;
  62. video::SColorf light_color;
  63. f32 light_radius;
  64. };
  65. /*
  66. TextureSource creates and caches textures.
  67. */
  68. class ISimpleTextureSource
  69. {
  70. public:
  71. ISimpleTextureSource(){}
  72. virtual ~ISimpleTextureSource(){}
  73. virtual video::ITexture* getTexture(
  74. const std::string &name, u32 *id = NULL) = 0;
  75. };
  76. class ITextureSource : public ISimpleTextureSource
  77. {
  78. public:
  79. ITextureSource(){}
  80. virtual ~ITextureSource(){}
  81. virtual u32 getTextureId(const std::string &name)=0;
  82. virtual std::string getTextureName(u32 id)=0;
  83. virtual video::ITexture* getTexture(u32 id)=0;
  84. virtual video::ITexture* getTexture(
  85. const std::string &name, u32 *id = NULL)=0;
  86. virtual IrrlichtDevice* getDevice()=0;
  87. virtual bool isKnownSourceImage(const std::string &name)=0;
  88. virtual video::ITexture* generateTextureFromMesh(
  89. const TextureFromMeshParams &params)=0;
  90. virtual video::ITexture* getNormalTexture(const std::string &name)=0;
  91. };
  92. class IWritableTextureSource : public ITextureSource
  93. {
  94. public:
  95. IWritableTextureSource(){}
  96. virtual ~IWritableTextureSource(){}
  97. virtual u32 getTextureId(const std::string &name)=0;
  98. virtual std::string getTextureName(u32 id)=0;
  99. virtual video::ITexture* getTexture(u32 id)=0;
  100. virtual video::ITexture* getTexture(
  101. const std::string &name, u32 *id = NULL)=0;
  102. virtual IrrlichtDevice* getDevice()=0;
  103. virtual bool isKnownSourceImage(const std::string &name)=0;
  104. virtual video::ITexture* generateTextureFromMesh(
  105. const TextureFromMeshParams &params)=0;
  106. virtual void processQueue()=0;
  107. virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
  108. virtual void rebuildImagesAndTextures()=0;
  109. virtual video::ITexture* getNormalTexture(const std::string &name)=0;
  110. };
  111. IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
  112. #ifdef __ANDROID__
  113. /**
  114. * @param size get next npot2 value
  115. * @return npot2 value
  116. */
  117. inline unsigned int npot2(unsigned int size)
  118. {
  119. if (size == 0) return 0;
  120. unsigned int npot = 1;
  121. while ((size >>= 1) > 0) {
  122. npot <<= 1;
  123. }
  124. return npot;
  125. }
  126. video::IImage * Align2Npot2(video::IImage * image, video::IVideoDriver* driver);
  127. #endif
  128. enum MaterialType{
  129. TILE_MATERIAL_BASIC,
  130. TILE_MATERIAL_ALPHA,
  131. TILE_MATERIAL_LIQUID_TRANSPARENT,
  132. TILE_MATERIAL_LIQUID_OPAQUE,
  133. TILE_MATERIAL_WAVING_LEAVES,
  134. TILE_MATERIAL_WAVING_PLANTS
  135. };
  136. // Material flags
  137. // Should backface culling be enabled?
  138. #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
  139. // Should a crack be drawn?
  140. #define MATERIAL_FLAG_CRACK 0x02
  141. // Should the crack be drawn on transparent pixels (unset) or not (set)?
  142. // Ignored if MATERIAL_FLAG_CRACK is not set.
  143. #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
  144. // Animation made up by splitting the texture to vertical frames, as
  145. // defined by extra parameters
  146. #define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
  147. #define MATERIAL_FLAG_HIGHLIGHTED 0x10
  148. /*
  149. This fully defines the looks of a tile.
  150. The SMaterial of a tile is constructed according to this.
  151. */
  152. struct FrameSpec
  153. {
  154. FrameSpec():
  155. texture_id(0),
  156. texture(NULL),
  157. normal_texture(NULL)
  158. {
  159. }
  160. u32 texture_id;
  161. video::ITexture *texture;
  162. video::ITexture *normal_texture;
  163. };
  164. struct TileSpec
  165. {
  166. TileSpec():
  167. texture_id(0),
  168. texture(NULL),
  169. normal_texture(NULL),
  170. alpha(255),
  171. material_type(TILE_MATERIAL_BASIC),
  172. material_flags(
  173. //0 // <- DEBUG, Use the one below
  174. MATERIAL_FLAG_BACKFACE_CULLING
  175. ),
  176. shader_id(0),
  177. animation_frame_count(1),
  178. animation_frame_length_ms(0),
  179. rotation(0)
  180. {
  181. }
  182. bool operator==(const TileSpec &other) const
  183. {
  184. return (
  185. texture_id == other.texture_id &&
  186. /* texture == other.texture && */
  187. alpha == other.alpha &&
  188. material_type == other.material_type &&
  189. material_flags == other.material_flags &&
  190. rotation == other.rotation
  191. );
  192. }
  193. bool operator!=(const TileSpec &other) const
  194. {
  195. return !(*this == other);
  196. }
  197. // Sets everything else except the texture in the material
  198. void applyMaterialOptions(video::SMaterial &material) const
  199. {
  200. switch(material_type){
  201. case TILE_MATERIAL_BASIC:
  202. material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
  203. break;
  204. case TILE_MATERIAL_ALPHA:
  205. material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
  206. break;
  207. case TILE_MATERIAL_LIQUID_TRANSPARENT:
  208. material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
  209. break;
  210. case TILE_MATERIAL_LIQUID_OPAQUE:
  211. material.MaterialType = video::EMT_SOLID;
  212. break;
  213. case TILE_MATERIAL_WAVING_LEAVES:
  214. material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
  215. break;
  216. case TILE_MATERIAL_WAVING_PLANTS:
  217. material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
  218. break;
  219. }
  220. material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
  221. }
  222. void applyMaterialOptionsWithShaders(video::SMaterial &material) const
  223. {
  224. material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
  225. }
  226. u32 texture_id;
  227. video::ITexture *texture;
  228. video::ITexture *normal_texture;
  229. // Vertex alpha (when MATERIAL_ALPHA_VERTEX is used)
  230. u8 alpha;
  231. // Material parameters
  232. u8 material_type;
  233. u8 material_flags;
  234. u32 shader_id;
  235. // Animation parameters
  236. u8 animation_frame_count;
  237. u16 animation_frame_length_ms;
  238. std::map<u32, FrameSpec> frames;
  239. u8 rotation;
  240. };
  241. #endif