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"
37 tile.{h,cpp}: Texture handling stuff.
41 Find out the full path of an image by trying different filename
46 TODO: Should probably be moved out from here, because things needing
47 this function do not need anything else from this header
49 std::string getImagePath(std::string path);
52 Gets the path to a texture by first checking if the texture exists
53 in texture_path and if not, using the data path.
55 Checks all supported extensions by replacing the original extension.
57 If not found, returns "".
59 Utilizes a thread-safe cache.
61 std::string getTexturePath(const std::string &filename);
63 void clearTextureNameCache();
66 ITextureSource::generateTextureFromMesh parameters
68 namespace irr {namespace scene {class IMesh;}}
69 struct TextureFromMeshParams
72 core::dimension2d<u32> dim;
73 std::string rtt_texture_name;
74 bool delete_texture_on_shutdown;
77 core::CMatrix4<f32> camera_projection_matrix;
78 video::SColorf ambient_light;
80 video::SColorf light_color;
85 TextureSource creates and caches textures.
88 class ISimpleTextureSource
91 ISimpleTextureSource(){}
92 virtual ~ISimpleTextureSource(){}
93 virtual video::ITexture* getTexture(
94 const std::string &name, u32 *id = NULL) = 0;
97 class ITextureSource : public ISimpleTextureSource
101 virtual ~ITextureSource(){}
102 virtual u32 getTextureId(const std::string &name)=0;
103 virtual std::string getTextureName(u32 id)=0;
104 virtual video::ITexture* getTexture(u32 id)=0;
105 virtual video::ITexture* getTexture(
106 const std::string &name, u32 *id = NULL)=0;
107 virtual video::ITexture* getTextureForMesh(
108 const std::string &name, u32 *id = NULL) = 0;
109 virtual IrrlichtDevice* getDevice()=0;
110 virtual bool isKnownSourceImage(const std::string &name)=0;
111 /*! Generates an image from a full string like
112 * "stone.png^mineral_coal.png^[crack:1:0".
113 * Shall be called from the main thread.
114 * The returned Image should be dropped.
116 virtual video::IImage* generateImage(const std::string &name)=0;
117 virtual video::ITexture* generateTextureFromMesh(
118 const TextureFromMeshParams ¶ms)=0;
119 virtual video::ITexture* getNormalTexture(const std::string &name)=0;
120 virtual video::SColor getTextureAverageColor(const std::string &name)=0;
121 virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
124 class IWritableTextureSource : public ITextureSource
127 IWritableTextureSource(){}
128 virtual ~IWritableTextureSource(){}
129 virtual u32 getTextureId(const std::string &name)=0;
130 virtual std::string getTextureName(u32 id)=0;
131 virtual video::ITexture* getTexture(u32 id)=0;
132 virtual video::ITexture* getTexture(
133 const std::string &name, u32 *id = NULL)=0;
134 virtual IrrlichtDevice* getDevice()=0;
135 virtual bool isKnownSourceImage(const std::string &name)=0;
136 virtual video::ITexture* generateTextureFromMesh(
137 const TextureFromMeshParams ¶ms)=0;
139 virtual void processQueue()=0;
140 virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
141 virtual void rebuildImagesAndTextures()=0;
142 virtual video::ITexture* getNormalTexture(const std::string &name)=0;
143 virtual video::SColor getTextureAverageColor(const std::string &name)=0;
144 virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
147 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
150 video::IImage * Align2Npot2(video::IImage * image, video::IVideoDriver* driver);
156 TILE_MATERIAL_LIQUID_TRANSPARENT,
157 TILE_MATERIAL_LIQUID_OPAQUE,
158 TILE_MATERIAL_WAVING_LEAVES,
159 TILE_MATERIAL_WAVING_PLANTS
163 // Should backface culling be enabled?
164 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
165 // Should a crack be drawn?
166 #define MATERIAL_FLAG_CRACK 0x02
167 // Should the crack be drawn on transparent pixels (unset) or not (set)?
168 // Ignored if MATERIAL_FLAG_CRACK is not set.
169 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
170 #define MATERIAL_FLAG_ANIMATION 0x08
171 #define MATERIAL_FLAG_HIGHLIGHTED 0x10
172 #define MATERIAL_FLAG_TILEABLE_HORIZONTAL 0x20
173 #define MATERIAL_FLAG_TILEABLE_VERTICAL 0x40
176 This fully defines the looks of a tile.
177 The SMaterial of a tile is constructed according to this.
184 normal_texture(NULL),
189 video::ITexture *texture;
190 video::ITexture *normal_texture;
191 video::ITexture *flags_texture;
199 normal_texture(NULL),
201 material_type(TILE_MATERIAL_BASIC),
203 //0 // <- DEBUG, Use the one below
204 MATERIAL_FLAG_BACKFACE_CULLING
207 animation_frame_count(1),
208 animation_frame_length_ms(0),
217 * Two tiles are equal if they can be appended to
218 * the same mesh buffer.
220 bool operator==(const TileSpec &other) const
223 texture_id == other.texture_id &&
224 material_type == other.material_type &&
225 material_flags == other.material_flags &&
226 rotation == other.rotation
231 * Two tiles are not equal if they must be in different mesh buffers.
233 bool operator!=(const TileSpec &other) const
235 return !(*this == other);
238 // Sets everything else except the texture in the material
239 void applyMaterialOptions(video::SMaterial &material) const
241 switch (material_type) {
242 case TILE_MATERIAL_BASIC:
243 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
245 case TILE_MATERIAL_ALPHA:
246 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
248 case TILE_MATERIAL_LIQUID_TRANSPARENT:
249 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
251 case TILE_MATERIAL_LIQUID_OPAQUE:
252 material.MaterialType = video::EMT_SOLID;
254 case TILE_MATERIAL_WAVING_LEAVES:
255 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
257 case TILE_MATERIAL_WAVING_PLANTS:
258 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
261 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
263 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
264 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
266 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
267 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
271 void applyMaterialOptionsWithShaders(video::SMaterial &material) const
273 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
275 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
276 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
277 material.TextureLayer[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
279 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
280 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
281 material.TextureLayer[1].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
286 video::ITexture *texture;
287 video::ITexture *normal_texture;
288 video::ITexture *flags_texture;
290 // Material parameters
294 // Animation parameters
295 u8 animation_frame_count;
296 u16 animation_frame_length_ms;
297 std::vector<FrameSpec> frames;
300 //! If true, the tile has its own color.
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.
307 //! This much light does the tile emit.