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