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