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 virtual video::ITexture* generateTextureFromMesh(
112 const TextureFromMeshParams ¶ms)=0;
113 virtual video::ITexture* getNormalTexture(const std::string &name)=0;
114 virtual video::SColor getTextureAverageColor(const std::string &name)=0;
115 virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
118 class IWritableTextureSource : public ITextureSource
121 IWritableTextureSource(){}
122 virtual ~IWritableTextureSource(){}
123 virtual u32 getTextureId(const std::string &name)=0;
124 virtual std::string getTextureName(u32 id)=0;
125 virtual video::ITexture* getTexture(u32 id)=0;
126 virtual video::ITexture* getTexture(
127 const std::string &name, u32 *id = NULL)=0;
128 virtual IrrlichtDevice* getDevice()=0;
129 virtual bool isKnownSourceImage(const std::string &name)=0;
130 virtual video::ITexture* generateTextureFromMesh(
131 const TextureFromMeshParams ¶ms)=0;
133 virtual void processQueue()=0;
134 virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
135 virtual void rebuildImagesAndTextures()=0;
136 virtual video::ITexture* getNormalTexture(const std::string &name)=0;
137 virtual video::SColor getTextureAverageColor(const std::string &name)=0;
138 virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
141 IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
144 video::IImage * Align2Npot2(video::IImage * image, video::IVideoDriver* driver);
150 TILE_MATERIAL_LIQUID_TRANSPARENT,
151 TILE_MATERIAL_LIQUID_OPAQUE,
152 TILE_MATERIAL_WAVING_LEAVES,
153 TILE_MATERIAL_WAVING_PLANTS
157 // Should backface culling be enabled?
158 #define MATERIAL_FLAG_BACKFACE_CULLING 0x01
159 // Should a crack be drawn?
160 #define MATERIAL_FLAG_CRACK 0x02
161 // Should the crack be drawn on transparent pixels (unset) or not (set)?
162 // Ignored if MATERIAL_FLAG_CRACK is not set.
163 #define MATERIAL_FLAG_CRACK_OVERLAY 0x04
164 // Animation made up by splitting the texture to vertical frames, as
165 // defined by extra parameters
166 #define MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES 0x08
167 #define MATERIAL_FLAG_HIGHLIGHTED 0x10
168 #define MATERIAL_FLAG_TILEABLE_HORIZONTAL 0x20
169 #define MATERIAL_FLAG_TILEABLE_VERTICAL 0x40
172 This fully defines the looks of a tile.
173 The SMaterial of a tile is constructed according to this.
180 normal_texture(NULL),
185 video::ITexture *texture;
186 video::ITexture *normal_texture;
187 video::ITexture *flags_texture;
195 normal_texture(NULL),
198 material_type(TILE_MATERIAL_BASIC),
200 //0 // <- DEBUG, Use the one below
201 MATERIAL_FLAG_BACKFACE_CULLING
204 animation_frame_count(1),
205 animation_frame_length_ms(0),
210 bool operator==(const TileSpec &other) const
213 texture_id == other.texture_id &&
214 /* texture == other.texture && */
215 alpha == other.alpha &&
216 material_type == other.material_type &&
217 material_flags == other.material_flags &&
218 rotation == other.rotation
222 bool operator!=(const TileSpec &other) const
224 return !(*this == other);
227 // Sets everything else except the texture in the material
228 void applyMaterialOptions(video::SMaterial &material) const
230 switch (material_type) {
231 case TILE_MATERIAL_BASIC:
232 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
234 case TILE_MATERIAL_ALPHA:
235 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
237 case TILE_MATERIAL_LIQUID_TRANSPARENT:
238 material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
240 case TILE_MATERIAL_LIQUID_OPAQUE:
241 material.MaterialType = video::EMT_SOLID;
243 case TILE_MATERIAL_WAVING_LEAVES:
244 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
246 case TILE_MATERIAL_WAVING_PLANTS:
247 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
250 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
252 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
253 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
255 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
256 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
260 void applyMaterialOptionsWithShaders(video::SMaterial &material) const
262 material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
264 if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) {
265 material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
266 material.TextureLayer[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
268 if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) {
269 material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
270 material.TextureLayer[1].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
275 video::ITexture *texture;
276 video::ITexture *normal_texture;
277 video::ITexture *flags_texture;
279 // Vertex alpha (when MATERIAL_ALPHA_VERTEX is used)
281 // Material parameters
285 // Animation parameters
286 u8 animation_frame_count;
287 u16 animation_frame_length_ms;
288 std::vector<FrameSpec> frames;