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.
23 #include "irrlichttypes.h"
28 #include <SMaterial.h>
30 #include "util/numeric.h"
36 typedef std::vector<video::SColor> Palette;
39 tile.{h,cpp}: Texture handling stuff.
43 Find out the full path of an image by trying different filename
48 TODO: Should probably be moved out from here, because things needing
49 this function do not need anything else from this header
51 std::string getImagePath(std::string path);
54 Gets the path to a texture by first checking if the texture exists
55 in texture_path and if not, using the data path.
57 Checks all supported extensions by replacing the original extension.
59 If not found, returns "".
61 Utilizes a thread-safe cache.
63 std::string getTexturePath(const std::string &filename);
65 void clearTextureNameCache();
68 ITextureSource::generateTextureFromMesh parameters
70 namespace irr {namespace scene {class IMesh;}}
71 struct TextureFromMeshParams
73 scene::IMesh *mesh = nullptr;
74 core::dimension2d<u32> dim;
75 std::string rtt_texture_name;
76 bool delete_texture_on_shutdown;
79 core::CMatrix4<f32> camera_projection_matrix;
80 video::SColorf ambient_light;
82 video::SColorf light_color;
87 TextureSource creates and caches textures.
90 class ISimpleTextureSource
93 ISimpleTextureSource() = default;
95 virtual ~ISimpleTextureSource() = default;
97 virtual video::ITexture* getTexture(
98 const std::string &name, u32 *id = nullptr) = 0;
101 class ITextureSource : public ISimpleTextureSource
104 ITextureSource() = default;
106 virtual ~ITextureSource() = default;
108 virtual u32 getTextureId(const std::string &name)=0;
109 virtual std::string getTextureName(u32 id)=0;
110 virtual video::ITexture* getTexture(u32 id)=0;
111 virtual video::ITexture* getTexture(
112 const std::string &name, u32 *id = nullptr)=0;
113 virtual video::ITexture* getTextureForMesh(
114 const std::string &name, u32 *id = nullptr) = 0;
116 * Returns a palette from the given texture name.
117 * The pointer is valid until the texture source is
119 * Should be called from the main thread.
121 virtual Palette* getPalette(const std::string &name) = 0;
122 virtual bool isKnownSourceImage(const std::string &name)=0;
123 virtual video::ITexture* generateTextureFromMesh(
124 const TextureFromMeshParams ¶ms)=0;
125 virtual video::ITexture* getNormalTexture(const std::string &name)=0;
126 virtual video::SColor getTextureAverageColor(const std::string &name)=0;
127 virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
130 class IWritableTextureSource : public ITextureSource
133 IWritableTextureSource() = default;
135 virtual ~IWritableTextureSource() = default;
137 virtual u32 getTextureId(const std::string &name)=0;
138 virtual std::string getTextureName(u32 id)=0;
139 virtual video::ITexture* getTexture(u32 id)=0;
140 virtual video::ITexture* getTexture(
141 const std::string &name, u32 *id = nullptr)=0;
142 virtual bool isKnownSourceImage(const std::string &name)=0;
143 virtual video::ITexture* generateTextureFromMesh(
144 const TextureFromMeshParams ¶ms)=0;
146 virtual void processQueue()=0;
147 virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
148 virtual void rebuildImagesAndTextures()=0;
149 virtual video::ITexture* getNormalTexture(const std::string &name)=0;
150 virtual video::SColor getTextureAverageColor(const std::string &name)=0;
151 virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
154 IWritableTextureSource *createTextureSource();
157 video::IImage * Align2Npot2(video::IImage * image, video::IVideoDriver* driver);
163 TILE_MATERIAL_LIQUID_TRANSPARENT,
164 TILE_MATERIAL_LIQUID_OPAQUE,
165 TILE_MATERIAL_WAVING_LEAVES,
166 TILE_MATERIAL_WAVING_PLANTS,
171 // Should backface culling be enabled?
172 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
173 // Should a crack be drawn?
174 #define MATERIAL_FLAG_CRACK 0x02
175 // Should the crack be drawn on transparent pixels (unset) or not (set)?
176 // Ignored if MATERIAL_FLAG_CRACK is not set.
177 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
178 #define MATERIAL_FLAG_ANIMATION 0x08
179 //#define MATERIAL_FLAG_HIGHLIGHTED 0x10
180 #define MATERIAL_FLAG_TILEABLE_HORIZONTAL 0x20
181 #define MATERIAL_FLAG_TILEABLE_VERTICAL 0x40
184 This fully defines the looks of a tile.
185 The SMaterial of a tile is constructed according to this.
189 FrameSpec() = default;
192 video::ITexture *texture = nullptr;
193 video::ITexture *normal_texture = nullptr;
194 video::ITexture *flags_texture = nullptr;
197 #define MAX_TILE_LAYERS 2
199 //! Defines a layer of a tile.
202 TileLayer() = default;
205 * Two layers are equal if they can be merged.
207 bool operator==(const TileLayer &other) const
210 texture_id == other.texture_id &&
211 material_type == other.material_type &&
212 material_flags == other.material_flags &&
213 color == other.color;
217 * Two tiles are not equal if they must have different vertices.
219 bool operator!=(const TileLayer &other) const
221 return !(*this == other);
224 // Sets everything else except the texture in the material
225 void applyMaterialOptions(video::SMaterial &material) const
227 switch (material_type) {
228 case TILE_MATERIAL_OPAQUE:
229 case TILE_MATERIAL_LIQUID_OPAQUE:
230 material.MaterialType = video::EMT_SOLID;
232 case TILE_MATERIAL_BASIC:
233 case TILE_MATERIAL_WAVING_LEAVES:
234 case TILE_MATERIAL_WAVING_PLANTS:
235 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
237 case TILE_MATERIAL_ALPHA:
238 case TILE_MATERIAL_LIQUID_TRANSPARENT:
239 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
242 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0;
243 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
244 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
246 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
247 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
251 void applyMaterialOptionsWithShaders(video::SMaterial &material) const
253 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0;
254 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
255 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
256 material.TextureLayer[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
258 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
259 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
260 material.TextureLayer[1].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
264 bool isTileable() const
266 return (material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)
267 && (material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL);
270 // Ordered for size, please do not reorder
272 video::ITexture *texture = nullptr;
273 video::ITexture *normal_texture = nullptr;
274 video::ITexture *flags_texture = nullptr;
280 u16 animation_frame_length_ms = 0;
281 u8 animation_frame_count = 1;
283 u8 material_type = TILE_MATERIAL_BASIC;
285 //0 // <- DEBUG, Use the one below
286 MATERIAL_FLAG_BACKFACE_CULLING |
287 MATERIAL_FLAG_TILEABLE_HORIZONTAL|
288 MATERIAL_FLAG_TILEABLE_VERTICAL;
290 //! If true, the tile has its own color.
291 bool has_color = false;
293 std::shared_ptr<std::vector<FrameSpec>> frames = nullptr;
296 * The color of the tile, or if the tile does not own
297 * a color then the color of the node owning this tile.
303 * Defines a face of a node. May have up to two layers.
308 for (int layer = 0; layer < MAX_TILE_LAYERS; layer++)
309 layers[layer] = TileLayer();
313 * Returns true if this tile can be merged with the other tile.
315 bool isTileable(const TileSpec &other) const {
316 for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
317 if (layers[layer] != other.layers[layer])
319 if (!layers[layer].isTileable())
323 && rotation == other.rotation
324 && emissive_light == other.emissive_light;
328 //! This much light does the tile emit.
329 u8 emissive_light = 0;
330 //! The first is base texture, the second is overlay.
331 TileLayer layers[MAX_TILE_LAYERS];