]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/tile.h
Clean scaling pre-filter for formspec/HUD.
[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_v2d.h"
25 #include "irr_v3d.h"
26 #include <ITexture.h>
27 #include <IrrlichtDevice.h>
28 #include "threads.h"
29 #include <string>
30 #include <vector>
31 #include "util/numeric.h"
32
33 class IGameDef;
34
35 /*
36         tile.{h,cpp}: Texture handling stuff.
37 */
38
39 /*
40         Find out the full path of an image by trying different filename
41         extensions.
42
43         If failed, return "".
44
45         TODO: Should probably be moved out from here, because things needing
46               this function do not need anything else from this header
47 */
48 std::string getImagePath(std::string path);
49
50 /*
51         Gets the path to a texture by first checking if the texture exists
52         in texture_path and if not, using the data path.
53
54         Checks all supported extensions by replacing the original extension.
55
56         If not found, returns "".
57
58         Utilizes a thread-safe cache.
59 */
60 std::string getTexturePath(const std::string &filename);
61
62 void clearTextureNameCache();
63
64 /*
65         ITextureSource::generateTextureFromMesh parameters
66 */
67 namespace irr {namespace scene {class IMesh;}}
68 struct TextureFromMeshParams
69 {
70         scene::IMesh *mesh;
71         core::dimension2d<u32> dim;
72         std::string rtt_texture_name;
73         bool delete_texture_on_shutdown;
74         v3f camera_position;
75         v3f camera_lookat;
76         core::CMatrix4<f32> camera_projection_matrix;
77         video::SColorf ambient_light;
78         v3f light_position;
79         video::SColorf light_color;
80         f32 light_radius;
81 };
82
83 /*
84         TextureSource creates and caches textures.
85 */
86
87 class ISimpleTextureSource
88 {
89 public:
90         ISimpleTextureSource(){}
91         virtual ~ISimpleTextureSource(){}
92         virtual video::ITexture* getTexture(
93                         const std::string &name, u32 *id = NULL) = 0;
94 };
95
96 class ITextureSource : public ISimpleTextureSource
97 {
98 public:
99         ITextureSource(){}
100         virtual ~ITextureSource(){}
101         virtual u32 getTextureId(const std::string &name)=0;
102         virtual std::string getTextureName(u32 id)=0;
103         virtual video::ITexture* getTexture(u32 id)=0;
104         virtual video::ITexture* getTexture(
105                         const std::string &name, u32 *id = NULL)=0;
106         virtual video::ITexture* getTextureForMesh(
107                         const std::string &name, u32 *id = NULL) = 0;
108         virtual IrrlichtDevice* getDevice()=0;
109         virtual bool isKnownSourceImage(const std::string &name)=0;
110         virtual video::ITexture* generateTextureFromMesh(
111                         const TextureFromMeshParams &params)=0;
112         virtual video::ITexture* getNormalTexture(const std::string &name)=0;
113 };
114
115 class IWritableTextureSource : public ITextureSource
116 {
117 public:
118         IWritableTextureSource(){}
119         virtual ~IWritableTextureSource(){}
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 = NULL)=0;
125         virtual IrrlichtDevice* getDevice()=0;
126         virtual bool isKnownSourceImage(const std::string &name)=0;
127         virtual video::ITexture* generateTextureFromMesh(
128                         const TextureFromMeshParams &params)=0;
129
130         virtual void processQueue()=0;
131         virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
132         virtual void rebuildImagesAndTextures()=0;
133         virtual video::ITexture* getNormalTexture(const std::string &name)=0;
134 };
135
136 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
137
138 #ifdef __ANDROID__
139 video::IImage * Align2Npot2(video::IImage * image, 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 };
150
151 // Material flags
152 // Should backface culling be enabled?
153 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
154 // Should a crack be drawn?
155 #define MATERIAL_FLAG_CRACK 0x02
156 // Should the crack be drawn on transparent pixels (unset) or not (set)?
157 // Ignored if MATERIAL_FLAG_CRACK is not set.
158 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
159 // Animation made up by splitting the texture to vertical frames, as
160 // defined by extra parameters
161 #define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
162 #define MATERIAL_FLAG_HIGHLIGHTED 0x10
163
164 /*
165         This fully defines the looks of a tile.
166         The SMaterial of a tile is constructed according to this.
167 */
168 struct FrameSpec
169 {
170         FrameSpec():
171                 texture_id(0),
172                 texture(NULL),
173                 normal_texture(NULL)
174         {
175         }
176         u32 texture_id;
177         video::ITexture *texture;
178         video::ITexture *normal_texture;
179 };
180
181 struct TileSpec
182 {
183         TileSpec():
184                 texture_id(0),
185                 texture(NULL),
186                 normal_texture(NULL),
187                 alpha(255),
188                 material_type(TILE_MATERIAL_BASIC),
189                 material_flags(
190                         //0 // <- DEBUG, Use the one below
191                         MATERIAL_FLAG_BACKFACE_CULLING
192                 ),
193                 shader_id(0),
194                 animation_frame_count(1),
195                 animation_frame_length_ms(0),
196                 rotation(0)
197         {
198         }
199
200         bool operator==(const TileSpec &other) const
201         {
202                 return (
203                         texture_id == other.texture_id &&
204                         /* texture == other.texture && */
205                         alpha == other.alpha &&
206                         material_type == other.material_type &&
207                         material_flags == other.material_flags &&
208                         rotation == other.rotation
209                 );
210         }
211
212         bool operator!=(const TileSpec &other) const
213         {
214                 return !(*this == other);
215         }
216         
217         // Sets everything else except the texture in the material
218         void applyMaterialOptions(video::SMaterial &material) const
219         {
220                 switch (material_type) {
221                 case TILE_MATERIAL_BASIC:
222                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
223                         break;
224                 case TILE_MATERIAL_ALPHA:
225                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
226                         break;
227                 case TILE_MATERIAL_LIQUID_TRANSPARENT:
228                         material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
229                         break;
230                 case TILE_MATERIAL_LIQUID_OPAQUE:
231                         material.MaterialType = video::EMT_SOLID;
232                         break;
233                 case TILE_MATERIAL_WAVING_LEAVES:
234                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
235                         break;
236                 case TILE_MATERIAL_WAVING_PLANTS:
237                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
238                         break;
239                 }
240                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
241                         ? true : false;
242         }
243
244         void applyMaterialOptionsWithShaders(video::SMaterial &material) const
245         {
246                 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
247                         ? true : false;
248         }
249         
250         u32 texture_id;
251         video::ITexture *texture;
252         video::ITexture *normal_texture;
253         
254         // Vertex alpha (when MATERIAL_ALPHA_VERTEX is used)
255         u8 alpha;
256         // Material parameters
257         u8 material_type;
258         u8 material_flags;
259         u32 shader_id;
260         // Animation parameters
261         u8 animation_frame_count;
262         u16 animation_frame_length_ms;
263         std::vector<FrameSpec> frames;
264
265         u8 rotation;
266 };
267
268 #endif