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>
35 tile.{h,cpp}: Texture handling stuff.
39 Find out the full path of an image by trying different filename
44 TODO: Should probably be moved out from here, because things needing
45 this function do not need anything else from this header
47 std::string getImagePath(std::string path);
50 Gets the path to a texture by first checking if the texture exists
51 in texture_path and if not, using the data path.
53 Checks all supported extensions by replacing the original extension.
55 If not found, returns "".
57 Utilizes a thread-safe cache.
59 std::string getTexturePath(const std::string &filename);
61 void clearTextureNameCache();
64 ITextureSource::generateTextureFromMesh parameters
66 namespace irr {namespace scene {class IMesh;}}
67 struct TextureFromMeshParams
70 core::dimension2d<u32> dim;
71 std::string rtt_texture_name;
72 bool delete_texture_on_shutdown;
75 core::CMatrix4<f32> camera_projection_matrix;
76 video::SColorf ambient_light;
78 video::SColorf light_color;
83 TextureSource creates and caches textures.
86 class ISimpleTextureSource
89 ISimpleTextureSource(){}
90 virtual ~ISimpleTextureSource(){}
91 virtual video::ITexture* getTexture(
92 const std::string &name, u32 *id = NULL) = 0;
95 class ITextureSource : public ISimpleTextureSource
99 virtual ~ITextureSource(){}
100 virtual u32 getTextureId(const std::string &name)=0;
101 virtual u32 getTextureIdDirect(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 IrrlichtDevice* getDevice()=0;
107 virtual bool isKnownSourceImage(const std::string &name)=0;
108 virtual video::ITexture* generateTextureFromMesh(
109 const TextureFromMeshParams ¶ms)=0;
110 virtual video::ITexture* getNormalTexture(const std::string &name)=0;
113 class IWritableTextureSource : public ITextureSource
116 IWritableTextureSource(){}
117 virtual ~IWritableTextureSource(){}
118 virtual u32 getTextureId(const std::string &name)=0;
119 virtual u32 getTextureIdDirect(const std::string &name)=0;
120 virtual std::string getTextureName(u32 id)=0;
121 virtual video::ITexture* getTexture(u32 id)=0;
122 virtual video::ITexture* getTexture(
123 const std::string &name, u32 *id = NULL)=0;
124 virtual IrrlichtDevice* getDevice()=0;
125 virtual bool isKnownSourceImage(const std::string &name)=0;
126 virtual video::ITexture* generateTextureFromMesh(
127 const TextureFromMeshParams ¶ms)=0;
129 virtual void processQueue()=0;
130 virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
131 virtual void rebuildImagesAndTextures()=0;
132 virtual video::ITexture* getNormalTexture(const std::string &name)=0;
135 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
139 * @param size get next npot2 value
140 * @return npot2 value
142 inline unsigned int npot2(unsigned int size)
144 if (size == 0) return 0;
145 unsigned int npot = 1;
147 while ((size >>= 1) > 0) {
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 // Animation made up by splitting the texture to vertical frames, as
174 // defined by extra parameters
175 #define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
178 This fully defines the looks of a tile.
179 The SMaterial of a tile is constructed according to this.
190 video::ITexture *texture;
191 video::ITexture *normal_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),
213 bool operator==(const TileSpec &other) const
216 texture_id == other.texture_id &&
217 /* texture == other.texture && */
218 alpha == other.alpha &&
219 material_type == other.material_type &&
220 material_flags == other.material_flags &&
221 rotation == other.rotation
225 bool operator!=(const TileSpec &other) const
227 return !(*this == other);
230 // Sets everything else except the texture in the material
231 void applyMaterialOptions(video::SMaterial &material) const
233 switch(material_type){
234 case TILE_MATERIAL_BASIC:
235 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
237 case TILE_MATERIAL_ALPHA:
238 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
240 case TILE_MATERIAL_LIQUID_TRANSPARENT:
241 material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
243 case TILE_MATERIAL_LIQUID_OPAQUE:
244 material.MaterialType = video::EMT_SOLID;
246 case TILE_MATERIAL_WAVING_LEAVES:
247 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
249 case TILE_MATERIAL_WAVING_PLANTS:
250 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
253 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
256 void applyMaterialOptionsWithShaders(video::SMaterial &material) const
258 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) ? true : false;
262 video::ITexture *texture;
263 video::ITexture *normal_texture;
265 // Vertex alpha (when MATERIAL_ALPHA_VERTEX is used)
267 // Material parameters
271 // Animation parameters
272 u8 animation_frame_count;
273 u16 animation_frame_length_ms;
274 std::map<u32, FrameSpec> frames;