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"
27 #include <IrrlichtDevice.h>
31 #include "util/numeric.h"
36 tile.{h,cpp}: Texture handling stuff.
40 Find out the full path of an image by trying different filename
45 TODO: Should probably be moved out from here, because things needing
46 this function do not need anything else from this header
48 std::string getImagePath(std::string path);
51 Gets the path to a texture by first checking if the texture exists
52 in texture_path and if not, using the data path.
54 Checks all supported extensions by replacing the original extension.
56 If not found, returns "".
58 Utilizes a thread-safe cache.
60 std::string getTexturePath(const std::string &filename);
62 void clearTextureNameCache();
65 ITextureSource::generateTextureFromMesh parameters
67 namespace irr {namespace scene {class IMesh;}}
68 struct TextureFromMeshParams
71 core::dimension2d<u32> dim;
72 std::string rtt_texture_name;
73 bool delete_texture_on_shutdown;
76 core::CMatrix4<f32> camera_projection_matrix;
77 video::SColorf ambient_light;
79 video::SColorf light_color;
84 TextureSource creates and caches textures.
87 class ISimpleTextureSource
90 ISimpleTextureSource(){}
91 virtual ~ISimpleTextureSource(){}
92 virtual video::ITexture* getTexture(
93 const std::string &name, u32 *id = NULL) = 0;
96 class ITextureSource : public ISimpleTextureSource
100 virtual ~ITextureSource(){}
101 virtual u32 getTextureId(const std::string &name)=0;
102 virtual std::string getTextureName(u32 id)=0;
103 virtual video::ITexture* getTexture(u32 id)=0;
104 virtual video::ITexture* getTexture(
105 const std::string &name, u32 *id = NULL)=0;
106 virtual video::ITexture* getTextureForMesh(
107 const std::string &name, u32 *id = NULL) = 0;
108 virtual IrrlichtDevice* getDevice()=0;
109 virtual bool isKnownSourceImage(const std::string &name)=0;
110 virtual video::ITexture* generateTextureFromMesh(
111 const TextureFromMeshParams ¶ms)=0;
112 virtual video::ITexture* getNormalTexture(const std::string &name)=0;
113 virtual video::SColor getTextureAverageColor(const std::string &name)=0;
116 class IWritableTextureSource : public ITextureSource
119 IWritableTextureSource(){}
120 virtual ~IWritableTextureSource(){}
121 virtual u32 getTextureId(const std::string &name)=0;
122 virtual std::string getTextureName(u32 id)=0;
123 virtual video::ITexture* getTexture(u32 id)=0;
124 virtual video::ITexture* getTexture(
125 const std::string &name, u32 *id = NULL)=0;
126 virtual IrrlichtDevice* getDevice()=0;
127 virtual bool isKnownSourceImage(const std::string &name)=0;
128 virtual video::ITexture* generateTextureFromMesh(
129 const TextureFromMeshParams ¶ms)=0;
131 virtual void processQueue()=0;
132 virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
133 virtual void rebuildImagesAndTextures()=0;
134 virtual video::ITexture* getNormalTexture(const std::string &name)=0;
135 virtual video::SColor getTextureAverageColor(const std::string &name)=0;
138 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
141 video::IImage * Align2Npot2(video::IImage * image, video::IVideoDriver* driver);
147 TILE_MATERIAL_LIQUID_TRANSPARENT,
148 TILE_MATERIAL_LIQUID_OPAQUE,
149 TILE_MATERIAL_WAVING_LEAVES,
150 TILE_MATERIAL_WAVING_PLANTS
154 // Should backface culling be enabled?
155 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
156 // Should a crack be drawn?
157 #define MATERIAL_FLAG_CRACK 0x02
158 // Should the crack be drawn on transparent pixels (unset) or not (set)?
159 // Ignored if MATERIAL_FLAG_CRACK is not set.
160 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
161 // Animation made up by splitting the texture to vertical frames, as
162 // defined by extra parameters
163 #define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
164 #define MATERIAL_FLAG_HIGHLIGHTED 0x10
167 This fully defines the looks of a tile.
168 The SMaterial of a tile is constructed according to this.
179 video::ITexture *texture;
180 video::ITexture *normal_texture;
188 normal_texture(NULL),
190 material_type(TILE_MATERIAL_BASIC),
192 //0 // <- DEBUG, Use the one below
193 MATERIAL_FLAG_BACKFACE_CULLING
196 animation_frame_count(1),
197 animation_frame_length_ms(0),
202 bool operator==(const TileSpec &other) const
205 texture_id == other.texture_id &&
206 /* texture == other.texture && */
207 alpha == other.alpha &&
208 material_type == other.material_type &&
209 material_flags == other.material_flags &&
210 rotation == other.rotation
214 bool operator!=(const TileSpec &other) const
216 return !(*this == other);
219 // Sets everything else except the texture in the material
220 void applyMaterialOptions(video::SMaterial &material) const
222 switch (material_type) {
223 case TILE_MATERIAL_BASIC:
224 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
226 case TILE_MATERIAL_ALPHA:
227 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
229 case TILE_MATERIAL_LIQUID_TRANSPARENT:
230 material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
232 case TILE_MATERIAL_LIQUID_OPAQUE:
233 material.MaterialType = video::EMT_SOLID;
235 case TILE_MATERIAL_WAVING_LEAVES:
236 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
238 case TILE_MATERIAL_WAVING_PLANTS:
239 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
242 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
246 void applyMaterialOptionsWithShaders(video::SMaterial &material) const
248 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
253 video::ITexture *texture;
254 video::ITexture *normal_texture;
256 // Vertex alpha (when MATERIAL_ALPHA_VERTEX is used)
258 // Material parameters
262 // Animation parameters
263 u8 animation_frame_count;
264 u16 animation_frame_length_ms;
265 std::vector<FrameSpec> frames;