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