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"
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);
68 void clearTextureNameCache();
71 ITextureSource::generateTextureFromMesh parameters
73 namespace irr {namespace scene {class IMesh;}}
74 struct TextureFromMeshParams
76 scene::IMesh *mesh = nullptr;
77 core::dimension2d<u32> dim;
78 std::string rtt_texture_name;
79 bool delete_texture_on_shutdown;
82 core::CMatrix4<f32> camera_projection_matrix;
83 video::SColorf ambient_light;
85 video::SColorf light_color;
90 TextureSource creates and caches textures.
93 class ISimpleTextureSource
96 ISimpleTextureSource() = default;
98 virtual ~ISimpleTextureSource() = default;
100 virtual video::ITexture* getTexture(
101 const std::string &name, u32 *id = nullptr) = 0;
104 class ITextureSource : public ISimpleTextureSource
107 ITextureSource() = default;
109 virtual ~ITextureSource() = default;
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;
119 * Returns a palette from the given texture name.
120 * The pointer is valid until the texture source is
122 * Should be called from the main thread.
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 ¶ms)=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;
133 class IWritableTextureSource : public ITextureSource
136 IWritableTextureSource() = default;
138 virtual ~IWritableTextureSource() = default;
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 ¶ms)=0;
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;
157 IWritableTextureSource *createTextureSource();
160 video::IImage * Align2Npot2(video::IImage * image, irr::video::IVideoDriver* driver);
166 TILE_MATERIAL_LIQUID_TRANSPARENT,
167 TILE_MATERIAL_LIQUID_OPAQUE,
168 TILE_MATERIAL_WAVING_LEAVES,
169 TILE_MATERIAL_WAVING_PLANTS,
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
187 This fully defines the looks of a tile.
188 The SMaterial of a tile is constructed according to this.
192 FrameSpec() = default;
195 video::ITexture *texture = nullptr;
196 video::ITexture *normal_texture = nullptr;
197 video::ITexture *flags_texture = nullptr;
200 #define MAX_TILE_LAYERS 2
202 //! Defines a layer of a tile.
205 TileLayer() = default;
208 * Two layers are equal if they can be merged.
210 bool operator==(const TileLayer &other) const
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;
221 * Two tiles are not equal if they must have different vertices.
223 bool operator!=(const TileLayer &other) const
225 return !(*this == other);
228 // Sets everything else except the texture in the material
229 void applyMaterialOptions(video::SMaterial &material) const
231 switch (material_type) {
232 case TILE_MATERIAL_OPAQUE:
233 case TILE_MATERIAL_LIQUID_OPAQUE:
234 material.MaterialType = video::EMT_SOLID;
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;
242 case TILE_MATERIAL_ALPHA:
243 case TILE_MATERIAL_LIQUID_TRANSPARENT:
244 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
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;
253 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
254 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
258 void applyMaterialOptionsWithShaders(video::SMaterial &material) const
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;
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;
271 bool isTileable() const
273 return (material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)
274 && (material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL);
277 // Ordered for size, please do not reorder
279 video::ITexture *texture = nullptr;
280 video::ITexture *normal_texture = nullptr;
281 video::ITexture *flags_texture = nullptr;
287 u16 animation_frame_length_ms = 0;
288 u8 animation_frame_count = 1;
290 u8 material_type = TILE_MATERIAL_BASIC;
292 //0 // <- DEBUG, Use the one below
293 MATERIAL_FLAG_BACKFACE_CULLING |
294 MATERIAL_FLAG_TILEABLE_HORIZONTAL|
295 MATERIAL_FLAG_TILEABLE_VERTICAL;
297 //! If true, the tile has its own color.
298 bool has_color = false;
300 std::shared_ptr<std::vector<FrameSpec>> frames = nullptr;
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.
312 * Defines a face of a node. May have up to two layers.
316 TileSpec() = default;
319 * Returns true if this tile can be merged with the other tile.
321 bool isTileable(const TileSpec &other) const {
322 for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
323 if (layers[layer] != other.layers[layer])
325 if (!layers[layer].isTileable())
329 && rotation == other.rotation
330 && emissive_light == other.emissive_light;
333 //! If true, the tile rotation is ignored.
334 bool world_aligned = false;
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];
343 std::vector<std::string> getTextureDirs();