]> git.lizzy.rs Git - minetest.git/blob - src/client/tile.h
Fix GUITable selection issues with trees
[minetest.git] / src / client / tile.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef TILE_HEADER
21 #define TILE_HEADER
22
23 #include "irrlichttypes.h"
24 #include "irr_v2d.h"
25 #include "irr_v3d.h"
26 #include <ITexture.h>
27 #include <IrrlichtDevice.h>
28 #include "threads.h"
29 #include <string>
30 #include <vector>
31 #include "util/numeric.h"
32
33 class IGameDef;
34 struct TileSpec;
35 struct TileDef;
36
37 /*
38         tile.{h,cpp}: Texture handling stuff.
39 */
40
41 /*
42         Find out the full path of an image by trying different filename
43         extensions.
44
45         If failed, return "".
46
47         TODO: Should probably be moved out from here, because things needing
48               this function do not need anything else from this header
49 */
50 std::string getImagePath(std::string path);
51
52 /*
53         Gets the path to a texture by first checking if the texture exists
54         in texture_path and if not, using the data path.
55
56         Checks all supported extensions by replacing the original extension.
57
58         If not found, returns "".
59
60         Utilizes a thread-safe cache.
61 */
62 std::string getTexturePath(const std::string &filename);
63
64 void clearTextureNameCache();
65
66 /*
67         ITextureSource::generateTextureFromMesh parameters
68 */
69 namespace irr {namespace scene {class IMesh;}}
70 struct TextureFromMeshParams
71 {
72         scene::IMesh *mesh;
73         core::dimension2d<u32> dim;
74         std::string rtt_texture_name;
75         bool delete_texture_on_shutdown;
76         v3f camera_position;
77         v3f camera_lookat;
78         core::CMatrix4<f32> camera_projection_matrix;
79         video::SColorf ambient_light;
80         v3f light_position;
81         video::SColorf light_color;
82         f32 light_radius;
83 };
84
85 /*
86         TextureSource creates and caches textures.
87 */
88
89 class ISimpleTextureSource
90 {
91 public:
92         ISimpleTextureSource(){}
93         virtual ~ISimpleTextureSource(){}
94         virtual video::ITexture* getTexture(
95                         const std::string &name, u32 *id = NULL) = 0;
96 };
97
98 class ITextureSource : public ISimpleTextureSource
99 {
100 public:
101         ITextureSource(){}
102         virtual ~ITextureSource(){}
103         virtual u32 getTextureId(const std::string &name)=0;
104         virtual std::string getTextureName(u32 id)=0;
105         virtual video::ITexture* getTexture(u32 id)=0;
106         virtual video::ITexture* getTexture(
107                         const std::string &name, u32 *id = NULL)=0;
108         virtual video::ITexture* getTextureForMesh(
109                         const std::string &name, u32 *id = NULL) = 0;
110         virtual IrrlichtDevice* getDevice()=0;
111         virtual bool isKnownSourceImage(const std::string &name)=0;
112         virtual video::ITexture* generateTextureFromMesh(
113                         const TextureFromMeshParams &params)=0;
114         virtual video::ITexture* getNormalTexture(const std::string &name)=0;
115         virtual video::SColor getTextureAverageColor(const std::string &name)=0;
116         virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
117 };
118
119 class IWritableTextureSource : public ITextureSource
120 {
121 public:
122         IWritableTextureSource(){}
123         virtual ~IWritableTextureSource(){}
124         virtual u32 getTextureId(const std::string &name)=0;
125         virtual std::string getTextureName(u32 id)=0;
126         virtual video::ITexture* getTexture(u32 id)=0;
127         virtual video::ITexture* getTexture(
128                         const std::string &name, u32 *id = NULL)=0;
129         virtual IrrlichtDevice* getDevice()=0;
130         virtual bool isKnownSourceImage(const std::string &name)=0;
131         virtual video::ITexture* generateTextureFromMesh(
132                         const TextureFromMeshParams &params)=0;
133
134         virtual void processQueue()=0;
135         virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
136         virtual void rebuildImagesAndTextures()=0;
137         virtual video::ITexture* getNormalTexture(const std::string &name)=0;
138         virtual video::SColor getTextureAverageColor(const std::string &name)=0;
139         virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
140 };
141
142 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
143
144 #ifdef __ANDROID__
145 video::IImage * Align2Npot2(video::IImage * image, video::IVideoDriver* driver);
146 #endif
147
148 enum MaterialType{
149         TILE_MATERIAL_BASIC,
150         TILE_MATERIAL_ALPHA,
151         TILE_MATERIAL_LIQUID_TRANSPARENT,
152         TILE_MATERIAL_LIQUID_OPAQUE,
153         TILE_MATERIAL_WAVING_LEAVES,
154         TILE_MATERIAL_WAVING_PLANTS
155 };
156
157 // Material flags
158 // Should backface culling be enabled?
159 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
160 // Should a crack be drawn?
161 #define MATERIAL_FLAG_CRACK 0x02
162 // Should the crack be drawn on transparent pixels (unset) or not (set)?
163 // Ignored if MATERIAL_FLAG_CRACK is not set.
164 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
165 // Animation made up by splitting the texture to vertical frames, as
166 // defined by extra parameters
167 #define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
168 #define MATERIAL_FLAG_HIGHLIGHTED 0x10
169 #define MATERIAL_FLAG_TILEABLE_HORIZONTAL 0x20
170 #define MATERIAL_FLAG_TILEABLE_VERTICAL 0x40
171
172 /*
173         This fully defines the looks of a tile.
174         The SMaterial of a tile is constructed according to this.
175 */
176 struct FrameSpec
177 {
178         FrameSpec():
179                 texture_id(0),
180                 texture(NULL),
181                 normal_texture(NULL),
182                 flags_texture(NULL)
183         {
184         }
185         u32 texture_id;
186         video::ITexture *texture;
187         video::ITexture *normal_texture;
188         video::ITexture *flags_texture;
189 };
190
191 struct TileSpec
192 {
193         TileSpec():
194                 texture_id(0),
195                 texture(NULL),
196                 normal_texture(NULL),
197                 flags_texture(NULL),
198                 alpha(255),
199                 material_type(TILE_MATERIAL_BASIC),
200                 material_flags(
201                         //0 // <- DEBUG, Use the one below
202                         MATERIAL_FLAG_BACKFACE_CULLING
203                 ),
204                 shader_id(0),
205                 animation_frame_count(1),
206                 animation_frame_length_ms(0),
207                 rotation(0)
208         {
209         }
210
211         bool operator==(const TileSpec &other) const
212         {
213                 return (
214                         texture_id == other.texture_id &&
215                         /* texture == other.texture && */
216                         alpha == other.alpha &&
217                         material_type == other.material_type &&
218                         material_flags == other.material_flags &&
219                         rotation == other.rotation
220                 );
221         }
222
223         bool operator!=(const TileSpec &other) const
224         {
225                 return !(*this == other);
226         }
227         
228         // Sets everything else except the texture in the material
229         void applyMaterialOptions(video::SMaterial &material) const
230         {
231                 switch (material_type) {
232                 case TILE_MATERIAL_BASIC:
233                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
234                         break;
235                 case TILE_MATERIAL_ALPHA:
236                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
237                         break;
238                 case TILE_MATERIAL_LIQUID_TRANSPARENT:
239                         material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
240                         break;
241                 case TILE_MATERIAL_LIQUID_OPAQUE:
242                         material.MaterialType = video::EMT_SOLID;
243                         break;
244                 case TILE_MATERIAL_WAVING_LEAVES:
245                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
246                         break;
247                 case TILE_MATERIAL_WAVING_PLANTS:
248                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
249                         break;
250                 }
251                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
252                         ? true : false;
253                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
254                         material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
255                 }
256                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
257                         material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
258                 }
259         }
260
261         void applyMaterialOptionsWithShaders(video::SMaterial &material) const
262         {
263                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
264                         ? true : false;
265                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
266                         material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
267                         material.TextureLayer[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
268                 }
269                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
270                         material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
271                         material.TextureLayer[1].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
272                 }
273         }
274         
275         u32 texture_id;
276         video::ITexture *texture;
277         video::ITexture *normal_texture;
278         video::ITexture *flags_texture;
279         
280         // Vertex alpha (when MATERIAL_ALPHA_VERTEX is used)
281         u8 alpha;
282         // Material parameters
283         u8 material_type;
284         u8 material_flags;
285         u32 shader_id;
286         // Animation parameters
287         u8 animation_frame_count;
288         u16 animation_frame_length_ms;
289         std::vector<FrameSpec> frames;
290
291         u8 rotation;
292 };
293 #endif