3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
22 #include "irrlichttypes.h"
27 #include <SMaterial.h>
28 #include "util/numeric.h"
32 #include <IVideoDriver.h>
39 typedef std::vector<video::SColor> Palette;
42 tile.{h,cpp}: Texture handling stuff.
46 Find out the full path of an image by trying different filename
51 TODO: Should probably be moved out from here, because things needing
52 this function do not need anything else from this header
54 std::string getImagePath(std::string path);
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.
60 Checks all supported extensions by replacing the original extension.
62 If not found, returns "".
64 Utilizes a thread-safe cache.
66 std::string getTexturePath(const std::string &filename, bool *is_base_pack = nullptr);
68 void clearTextureNameCache();
71 TextureSource creates and caches textures.
74 class ISimpleTextureSource
77 ISimpleTextureSource() = default;
79 virtual ~ISimpleTextureSource() = default;
81 virtual video::ITexture* getTexture(
82 const std::string &name, u32 *id = nullptr) = 0;
85 class ITextureSource : public ISimpleTextureSource
88 ITextureSource() = default;
90 virtual ~ITextureSource() = default;
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;
100 * Returns a palette from the given texture name.
101 * The pointer is valid until the texture source is
103 * Should be called from the main thread.
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;
112 class IWritableTextureSource : public ITextureSource
115 IWritableTextureSource() = default;
117 virtual ~IWritableTextureSource() = default;
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;
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;
134 IWritableTextureSource *createTextureSource();
137 video::IImage *Align2Npot2(video::IImage *image, video::IVideoDriver *driver);
143 TILE_MATERIAL_LIQUID_TRANSPARENT,
144 TILE_MATERIAL_LIQUID_OPAQUE,
145 TILE_MATERIAL_WAVING_LEAVES,
146 TILE_MATERIAL_WAVING_PLANTS,
147 TILE_MATERIAL_OPAQUE,
148 TILE_MATERIAL_WAVING_LIQUID_BASIC,
149 TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT,
150 TILE_MATERIAL_WAVING_LIQUID_OPAQUE,
152 TILE_MATERIAL_PLAIN_ALPHA
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
169 This fully defines the looks of a tile.
170 The SMaterial of a tile is constructed according to this.
174 FrameSpec() = default;
177 video::ITexture *texture = nullptr;
178 video::ITexture *normal_texture = nullptr;
179 video::ITexture *flags_texture = nullptr;
182 #define MAX_TILE_LAYERS 2
184 //! Defines a layer of a tile.
187 TileLayer() = default;
190 * Two layers are equal if they can be merged.
192 bool operator==(const TileLayer &other) const
195 texture_id == other.texture_id &&
196 material_type == other.material_type &&
197 material_flags == other.material_flags &&
198 has_color == other.has_color &&
199 color == other.color &&
200 scale == other.scale;
204 * Two tiles are not equal if they must have different vertices.
206 bool operator!=(const TileLayer &other) const
208 return !(*this == other);
211 // Sets everything else except the texture in the material
212 void applyMaterialOptions(video::SMaterial &material) const
214 switch (material_type) {
215 case TILE_MATERIAL_OPAQUE:
216 case TILE_MATERIAL_LIQUID_OPAQUE:
217 case TILE_MATERIAL_WAVING_LIQUID_OPAQUE:
218 material.MaterialType = video::EMT_SOLID;
220 case TILE_MATERIAL_BASIC:
221 case TILE_MATERIAL_WAVING_LEAVES:
222 case TILE_MATERIAL_WAVING_PLANTS:
223 case TILE_MATERIAL_WAVING_LIQUID_BASIC:
224 material.MaterialTypeParam = 0.5;
225 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
227 case TILE_MATERIAL_ALPHA:
228 case TILE_MATERIAL_LIQUID_TRANSPARENT:
229 case TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT:
230 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
235 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0;
236 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
237 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
239 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
240 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
244 void applyMaterialOptionsWithShaders(video::SMaterial &material) const
246 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0;
247 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
248 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
249 material.TextureLayer[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
251 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
252 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
253 material.TextureLayer[1].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
257 bool isTileable() const
259 return (material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)
260 && (material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL);
263 bool isTransparent() const
265 switch (material_type) {
266 case TILE_MATERIAL_ALPHA:
267 case TILE_MATERIAL_LIQUID_TRANSPARENT:
268 case TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT:
274 // Ordered for size, please do not reorder
276 video::ITexture *texture = nullptr;
277 video::ITexture *normal_texture = nullptr;
278 video::ITexture *flags_texture = nullptr;
284 u16 animation_frame_length_ms = 0;
285 u16 animation_frame_count = 1;
287 u8 material_type = TILE_MATERIAL_BASIC;
289 //0 // <- DEBUG, Use the one below
290 MATERIAL_FLAG_BACKFACE_CULLING |
291 MATERIAL_FLAG_TILEABLE_HORIZONTAL|
292 MATERIAL_FLAG_TILEABLE_VERTICAL;
294 //! If true, the tile has its own color.
295 bool has_color = false;
297 std::vector<FrameSpec> *frames = nullptr;
300 * The color of the tile, or if the tile does not own
301 * a color then the color of the node owning this tile.
303 video::SColor color = video::SColor(0, 0, 0, 0);
309 * Defines a face of a node. May have up to two layers.
313 TileSpec() = default;
316 * Returns true if this tile can be merged with the other tile.
318 bool isTileable(const TileSpec &other) const {
319 for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
320 if (layers[layer] != other.layers[layer])
322 // Only non-transparent tiles can be merged into fast faces
323 if (layers[layer].isTransparent() || !layers[layer].isTileable())
327 && rotation == other.rotation
328 && emissive_light == other.emissive_light;
331 //! If true, the tile rotation is ignored.
332 bool world_aligned = false;
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];
341 std::vector<std::string> getTextureDirs();