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"
26 #include <IrrlichtDevice.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
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(){}
94 virtual ~ISimpleTextureSource(){}
95 virtual video::ITexture* getTexture(
96 const std::string &name, u32 *id = NULL) = 0;
99 class ITextureSource : public ISimpleTextureSource
103 virtual ~ITextureSource(){}
104 virtual u32 getTextureId(const std::string &name)=0;
105 virtual std::string getTextureName(u32 id)=0;
106 virtual video::ITexture* getTexture(u32 id)=0;
107 virtual video::ITexture* getTexture(
108 const std::string &name, u32 *id = NULL)=0;
109 virtual video::ITexture* getTextureForMesh(
110 const std::string &name, u32 *id = NULL) = 0;
112 * Returns a palette from the given texture name.
113 * The pointer is valid until the texture source is
115 * Should be called from the main thread.
117 virtual Palette* getPalette(const std::string &name) = 0;
118 virtual IrrlichtDevice* getDevice()=0;
119 virtual bool isKnownSourceImage(const std::string &name)=0;
120 virtual video::ITexture* generateTextureFromMesh(
121 const TextureFromMeshParams ¶ms)=0;
122 virtual video::ITexture* getNormalTexture(const std::string &name)=0;
123 virtual video::SColor getTextureAverageColor(const std::string &name)=0;
124 virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
127 class IWritableTextureSource : public ITextureSource
130 IWritableTextureSource(){}
131 virtual ~IWritableTextureSource(){}
132 virtual u32 getTextureId(const std::string &name)=0;
133 virtual std::string getTextureName(u32 id)=0;
134 virtual video::ITexture* getTexture(u32 id)=0;
135 virtual video::ITexture* getTexture(
136 const std::string &name, u32 *id = NULL)=0;
137 virtual IrrlichtDevice* getDevice()=0;
138 virtual bool isKnownSourceImage(const std::string &name)=0;
139 virtual video::ITexture* generateTextureFromMesh(
140 const TextureFromMeshParams ¶ms)=0;
142 virtual void processQueue()=0;
143 virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
144 virtual void rebuildImagesAndTextures()=0;
145 virtual video::ITexture* getNormalTexture(const std::string &name)=0;
146 virtual video::SColor getTextureAverageColor(const std::string &name)=0;
147 virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
150 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
153 video::IImage * Align2Npot2(video::IImage * image, video::IVideoDriver* driver);
159 TILE_MATERIAL_LIQUID_TRANSPARENT,
160 TILE_MATERIAL_LIQUID_OPAQUE,
161 TILE_MATERIAL_WAVING_LEAVES,
162 TILE_MATERIAL_WAVING_PLANTS
166 // Should backface culling be enabled?
167 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
168 // Should a crack be drawn?
169 #define MATERIAL_FLAG_CRACK 0x02
170 // Should the crack be drawn on transparent pixels (unset) or not (set)?
171 // Ignored if MATERIAL_FLAG_CRACK is not set.
172 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
173 #define MATERIAL_FLAG_ANIMATION 0x08
174 #define MATERIAL_FLAG_HIGHLIGHTED 0x10
175 #define MATERIAL_FLAG_TILEABLE_HORIZONTAL 0x20
176 #define MATERIAL_FLAG_TILEABLE_VERTICAL 0x40
179 This fully defines the looks of a tile.
180 The SMaterial of a tile is constructed according to this.
187 normal_texture(NULL),
192 video::ITexture *texture;
193 video::ITexture *normal_texture;
194 video::ITexture *flags_texture;
197 #define MAX_TILE_LAYERS 2
199 //! Defines a layer of a tile.
204 normal_texture(NULL),
208 animation_frame_length_ms(0),
209 animation_frame_count(1),
210 material_type(TILE_MATERIAL_BASIC),
212 //0 // <- DEBUG, Use the one below
213 MATERIAL_FLAG_BACKFACE_CULLING |
214 MATERIAL_FLAG_TILEABLE_HORIZONTAL|
215 MATERIAL_FLAG_TILEABLE_VERTICAL
223 * Two layers are equal if they can be merged.
225 bool operator==(const TileLayer &other) const
228 texture_id == other.texture_id &&
229 material_type == other.material_type &&
230 material_flags == other.material_flags &&
231 color == other.color;
235 * Two tiles are not equal if they must have different vertices.
237 bool operator!=(const TileLayer &other) const
239 return !(*this == other);
242 // Sets everything else except the texture in the material
243 void applyMaterialOptions(video::SMaterial &material) const
245 switch (material_type) {
246 case TILE_MATERIAL_BASIC:
247 case TILE_MATERIAL_WAVING_LEAVES:
248 case TILE_MATERIAL_WAVING_PLANTS:
249 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
251 case TILE_MATERIAL_ALPHA:
252 case TILE_MATERIAL_LIQUID_TRANSPARENT:
253 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
255 case TILE_MATERIAL_LIQUID_OPAQUE:
256 material.MaterialType = video::EMT_SOLID;
259 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
261 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
262 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
264 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
265 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
269 void applyMaterialOptionsWithShaders(video::SMaterial &material) const
271 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
273 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
274 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
275 material.TextureLayer[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
277 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
278 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
279 material.TextureLayer[1].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
283 bool isTileable() const
285 return (material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)
286 && (material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL);
289 // Ordered for size, please do not reorder
291 video::ITexture *texture;
292 video::ITexture *normal_texture;
293 video::ITexture *flags_texture;
299 u16 animation_frame_length_ms;
300 u8 animation_frame_count;
305 //! If true, the tile has its own color.
308 std::vector<FrameSpec> frames;
311 * The color of the tile, or if the tile does not own
312 * a color then the color of the node owning this tile.
318 * Defines a face of a node. May have up to two layers.
326 for (int layer = 0; layer < MAX_TILE_LAYERS; layer++)
327 layers[layer] = TileLayer();
331 * Returns true if this tile can be merged with the other tile.
333 bool isTileable(const TileSpec &other) const {
334 for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
335 if (layers[layer] != other.layers[layer])
337 if (!layers[layer].isTileable())
341 && rotation == other.rotation
342 && emissive_light == other.emissive_light;
346 //! This much light does the tile emit.
348 //! The first is base texture, the second is overlay.
349 TileLayer layers[MAX_TILE_LAYERS];