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