]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/tile.h
Remove threads.h and replace its definitions with their C++11 equivalents (#5957)
[dragonfireclient.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_v3d.h"
25 #include <ITexture.h>
26 #include <IrrlichtDevice.h>
27 #include <string>
28 #include <vector>
29 #include "util/numeric.h"
30
31 class IGameDef;
32 struct TileSpec;
33 struct TileDef;
34
35 typedef std::vector<video::SColor> Palette;
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         /*!
111          * Returns a palette from the given texture name.
112          * The pointer is valid until the texture source is
113          * destructed.
114          * Should be called from the main thread.
115          */
116         virtual Palette* getPalette(const std::string &name) = 0;
117         virtual IrrlichtDevice* getDevice()=0;
118         virtual bool isKnownSourceImage(const std::string &name)=0;
119         virtual video::ITexture* generateTextureFromMesh(
120                         const TextureFromMeshParams &params)=0;
121         virtual video::ITexture* getNormalTexture(const std::string &name)=0;
122         virtual video::SColor getTextureAverageColor(const std::string &name)=0;
123         virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
124 };
125
126 class IWritableTextureSource : public ITextureSource
127 {
128 public:
129         IWritableTextureSource(){}
130         virtual ~IWritableTextureSource(){}
131         virtual u32 getTextureId(const std::string &name)=0;
132         virtual std::string getTextureName(u32 id)=0;
133         virtual video::ITexture* getTexture(u32 id)=0;
134         virtual video::ITexture* getTexture(
135                         const std::string &name, u32 *id = NULL)=0;
136         virtual IrrlichtDevice* getDevice()=0;
137         virtual bool isKnownSourceImage(const std::string &name)=0;
138         virtual video::ITexture* generateTextureFromMesh(
139                         const TextureFromMeshParams &params)=0;
140
141         virtual void processQueue()=0;
142         virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
143         virtual void rebuildImagesAndTextures()=0;
144         virtual video::ITexture* getNormalTexture(const std::string &name)=0;
145         virtual video::SColor getTextureAverageColor(const std::string &name)=0;
146         virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
147 };
148
149 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
150
151 #ifdef __ANDROID__
152 video::IImage * Align2Npot2(video::IImage * image, video::IVideoDriver* driver);
153 #endif
154
155 enum MaterialType{
156         TILE_MATERIAL_BASIC,
157         TILE_MATERIAL_ALPHA,
158         TILE_MATERIAL_LIQUID_TRANSPARENT,
159         TILE_MATERIAL_LIQUID_OPAQUE,
160         TILE_MATERIAL_WAVING_LEAVES,
161         TILE_MATERIAL_WAVING_PLANTS
162 };
163
164 // Material flags
165 // Should backface culling be enabled?
166 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
167 // Should a crack be drawn?
168 #define MATERIAL_FLAG_CRACK 0x02
169 // Should the crack be drawn on transparent pixels (unset) or not (set)?
170 // Ignored if MATERIAL_FLAG_CRACK is not set.
171 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
172 #define MATERIAL_FLAG_ANIMATION 0x08
173 #define MATERIAL_FLAG_HIGHLIGHTED 0x10
174 #define MATERIAL_FLAG_TILEABLE_HORIZONTAL 0x20
175 #define MATERIAL_FLAG_TILEABLE_VERTICAL 0x40
176
177 /*
178         This fully defines the looks of a tile.
179         The SMaterial of a tile is constructed according to this.
180 */
181 struct FrameSpec
182 {
183         FrameSpec():
184                 texture_id(0),
185                 texture(NULL),
186                 normal_texture(NULL),
187                 flags_texture(NULL)
188         {
189         }
190         u32 texture_id;
191         video::ITexture *texture;
192         video::ITexture *normal_texture;
193         video::ITexture *flags_texture;
194 };
195
196 #define MAX_TILE_LAYERS 2
197
198 //! Defines a layer of a tile.
199 struct TileLayer
200 {
201         TileLayer():
202                 texture(NULL),
203                 normal_texture(NULL),
204                 flags_texture(NULL),
205                 shader_id(0),
206                 texture_id(0),
207                 animation_frame_length_ms(0),
208                 animation_frame_count(1),
209                 material_type(TILE_MATERIAL_BASIC),
210                 material_flags(
211                         //0 // <- DEBUG, Use the one below
212                         MATERIAL_FLAG_BACKFACE_CULLING |
213                         MATERIAL_FLAG_TILEABLE_HORIZONTAL|
214                         MATERIAL_FLAG_TILEABLE_VERTICAL
215                 ),
216                 has_color(false),
217                 color()
218         {
219         }
220
221         /*!
222          * Two layers are equal if they can be merged.
223          */
224         bool operator==(const TileLayer &other) const
225         {
226                 return
227                         texture_id == other.texture_id &&
228                         material_type == other.material_type &&
229                         material_flags == other.material_flags &&
230                         color == other.color;
231         }
232
233         /*!
234          * Two tiles are not equal if they must have different vertices.
235          */
236         bool operator!=(const TileLayer &other) const
237         {
238                 return !(*this == other);
239         }
240
241         // Sets everything else except the texture in the material
242         void applyMaterialOptions(video::SMaterial &material) const
243         {
244                 switch (material_type) {
245                 case TILE_MATERIAL_BASIC:
246                 case TILE_MATERIAL_WAVING_LEAVES:
247                 case TILE_MATERIAL_WAVING_PLANTS:
248                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
249                         break;
250                 case TILE_MATERIAL_ALPHA:
251                 case TILE_MATERIAL_LIQUID_TRANSPARENT:
252                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
253                         break;
254                 case TILE_MATERIAL_LIQUID_OPAQUE:
255                         material.MaterialType = video::EMT_SOLID;
256                         break;
257                 }
258                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
259                         ? true : false;
260                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
261                         material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
262                 }
263                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
264                         material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
265                 }
266         }
267
268         void applyMaterialOptionsWithShaders(video::SMaterial &material) const
269         {
270                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
271                         ? true : false;
272                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
273                         material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
274                         material.TextureLayer[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
275                 }
276                 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
277                         material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
278                         material.TextureLayer[1].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
279                 }
280         }
281
282         bool isTileable() const
283         {
284                 return (material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)
285                         && (material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL);
286         }
287
288         // Ordered for size, please do not reorder
289
290         video::ITexture *texture;
291         video::ITexture *normal_texture;
292         video::ITexture *flags_texture;
293
294         u32 shader_id;
295
296         u32 texture_id;
297
298         u16 animation_frame_length_ms;
299         u8 animation_frame_count;
300
301         u8 material_type;
302         u8 material_flags;
303
304         //! If true, the tile has its own color.
305         bool has_color;
306
307         std::vector<FrameSpec> frames;
308
309         /*!
310          * The color of the tile, or if the tile does not own
311          * a color then the color of the node owning this tile.
312          */
313         video::SColor color;
314 };
315
316 /*!
317  * Defines a face of a node. May have up to two layers.
318  */
319 struct TileSpec
320 {
321         TileSpec():
322                 rotation(0),
323                 emissive_light(0)
324         {
325                 for (int layer = 0; layer < MAX_TILE_LAYERS; layer++)
326                         layers[layer] = TileLayer();
327         }
328         
329         /*!
330          * Returns true if this tile can be merged with the other tile.
331          */
332         bool isTileable(const TileSpec &other) const {
333                 for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
334                         if (layers[layer] != other.layers[layer])
335                                 return false;
336                         if (!layers[layer].isTileable())
337                                 return false;
338                 }
339                 return rotation == 0
340                         && rotation == other.rotation
341                         && emissive_light == other.emissive_light;
342         }
343
344         u8 rotation;
345         //! This much light does the tile emit.
346         u8 emissive_light;
347         //! The first is base texture, the second is overlay.
348         TileLayer layers[MAX_TILE_LAYERS];
349 };
350 #endif