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>
29 #include "util/numeric.h"
33 #include <IVideoDriver.h>
40 typedef std::vector<video::SColor> Palette;
43 tile.{h,cpp}: Texture handling stuff.
47 Find out the full path of an image by trying different filename
52 TODO: Should probably be moved out from here, because things needing
53 this function do not need anything else from this header
55 std::string getImagePath(std::string path);
58 Gets the path to a texture by first checking if the texture exists
59 in texture_path and if not, using the data path.
61 Checks all supported extensions by replacing the original extension.
63 If not found, returns "".
65 Utilizes a thread-safe cache.
67 std::string getTexturePath(const std::string &filename);
69 void clearTextureNameCache();
72 TextureSource creates and caches textures.
75 class ISimpleTextureSource
78 ISimpleTextureSource() = default;
80 virtual ~ISimpleTextureSource() = default;
82 virtual video::ITexture* getTexture(
83 const std::string &name, u32 *id = nullptr) = 0;
86 class ITextureSource : public ISimpleTextureSource
89 ITextureSource() = default;
91 virtual ~ITextureSource() = default;
93 virtual u32 getTextureId(const std::string &name)=0;
94 virtual std::string getTextureName(u32 id)=0;
95 virtual video::ITexture* getTexture(u32 id)=0;
96 virtual video::ITexture* getTexture(
97 const std::string &name, u32 *id = nullptr)=0;
98 virtual video::ITexture* getTextureForMesh(
99 const std::string &name, u32 *id = nullptr) = 0;
101 * Returns a palette from the given texture name.
102 * The pointer is valid until the texture source is
104 * Should be called from the main thread.
106 virtual Palette* getPalette(const std::string &name) = 0;
107 virtual bool isKnownSourceImage(const std::string &name)=0;
108 virtual video::ITexture* getNormalTexture(const std::string &name)=0;
109 virtual video::SColor getTextureAverageColor(const std::string &name)=0;
110 virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
113 class IWritableTextureSource : public ITextureSource
116 IWritableTextureSource() = default;
118 virtual ~IWritableTextureSource() = default;
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 = nullptr)=0;
125 virtual bool isKnownSourceImage(const std::string &name)=0;
127 virtual void processQueue()=0;
128 virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
129 virtual void rebuildImagesAndTextures()=0;
130 virtual video::ITexture* getNormalTexture(const std::string &name)=0;
131 virtual video::SColor getTextureAverageColor(const std::string &name)=0;
132 virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
135 IWritableTextureSource *createTextureSource();
138 video::IImage * Align2Npot2(video::IImage * image, irr::video::IVideoDriver* driver);
144 TILE_MATERIAL_LIQUID_TRANSPARENT,
145 TILE_MATERIAL_LIQUID_OPAQUE,
146 TILE_MATERIAL_WAVING_LEAVES,
147 TILE_MATERIAL_WAVING_PLANTS,
148 TILE_MATERIAL_OPAQUE,
149 TILE_MATERIAL_WAVING_LIQUID_BASIC,
150 TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT,
151 TILE_MATERIAL_WAVING_LIQUID_OPAQUE,
155 // Should backface culling be enabled?
156 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
157 // Should a crack be drawn?
158 #define MATERIAL_FLAG_CRACK 0x02
159 // Should the crack be drawn on transparent pixels (unset) or not (set)?
160 // Ignored if MATERIAL_FLAG_CRACK is not set.
161 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
162 #define MATERIAL_FLAG_ANIMATION 0x08
163 //#define MATERIAL_FLAG_HIGHLIGHTED 0x10
164 #define MATERIAL_FLAG_TILEABLE_HORIZONTAL 0x20
165 #define MATERIAL_FLAG_TILEABLE_VERTICAL 0x40
168 This fully defines the looks of a tile.
169 The SMaterial of a tile is constructed according to this.
173 FrameSpec() = default;
176 video::ITexture *texture = nullptr;
177 video::ITexture *normal_texture = nullptr;
178 video::ITexture *flags_texture = nullptr;
181 #define MAX_TILE_LAYERS 2
183 //! Defines a layer of a tile.
186 TileLayer() = default;
189 * Two layers are equal if they can be merged.
191 bool operator==(const TileLayer &other) const
194 texture_id == other.texture_id &&
195 material_type == other.material_type &&
196 material_flags == other.material_flags &&
197 color == other.color &&
198 scale == other.scale;
202 * Two tiles are not equal if they must have different vertices.
204 bool operator!=(const TileLayer &other) const
206 return !(*this == other);
209 // Sets everything else except the texture in the material
210 void applyMaterialOptions(video::SMaterial &material) const
212 switch (material_type) {
213 case TILE_MATERIAL_OPAQUE:
214 case TILE_MATERIAL_LIQUID_OPAQUE:
215 case TILE_MATERIAL_WAVING_LIQUID_OPAQUE:
216 material.MaterialType = video::EMT_SOLID;
218 case TILE_MATERIAL_BASIC:
219 case TILE_MATERIAL_WAVING_LEAVES:
220 case TILE_MATERIAL_WAVING_PLANTS:
221 case TILE_MATERIAL_WAVING_LIQUID_BASIC:
222 material.MaterialTypeParam = 0.5;
223 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
225 case TILE_MATERIAL_ALPHA:
226 case TILE_MATERIAL_LIQUID_TRANSPARENT:
227 case TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT:
228 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
233 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0;
234 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
235 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
237 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
238 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
242 void applyMaterialOptionsWithShaders(video::SMaterial &material) const
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 material.TextureLayer[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
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;
255 bool isTileable() const
257 return (material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)
258 && (material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL);
261 // Ordered for size, please do not reorder
263 video::ITexture *texture = nullptr;
264 video::ITexture *normal_texture = nullptr;
265 video::ITexture *flags_texture = nullptr;
271 u16 animation_frame_length_ms = 0;
272 u8 animation_frame_count = 1;
274 u8 material_type = TILE_MATERIAL_BASIC;
276 //0 // <- DEBUG, Use the one below
277 MATERIAL_FLAG_BACKFACE_CULLING |
278 MATERIAL_FLAG_TILEABLE_HORIZONTAL|
279 MATERIAL_FLAG_TILEABLE_VERTICAL;
281 //! If true, the tile has its own color.
282 bool has_color = false;
284 std::shared_ptr<std::vector<FrameSpec>> frames = nullptr;
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.
296 * Defines a face of a node. May have up to two layers.
300 TileSpec() = default;
303 * Returns true if this tile can be merged with the other tile.
305 bool isTileable(const TileSpec &other) const {
306 for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
307 if (layers[layer] != other.layers[layer])
309 if (!layers[layer].isTileable())
313 && rotation == other.rotation
314 && emissive_light == other.emissive_light;
317 //! If true, the tile rotation is ignored.
318 bool world_aligned = false;
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];
327 std::vector<std::string> getTextureDirs();