Line data Source code
1 : /*
2 : Minetest
3 : Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 :
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.
9 :
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.
14 :
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.
18 : */
19 :
20 : #ifndef TILE_HEADER
21 : #define TILE_HEADER
22 :
23 : #include "irrlichttypes.h"
24 : #include "irr_v2d.h"
25 : #include "irr_v3d.h"
26 : #include <ITexture.h>
27 : #include <IrrlichtDevice.h>
28 : #include "threads.h"
29 : #include <string>
30 : #include <vector>
31 : #include "util/numeric.h"
32 :
33 : class IGameDef;
34 :
35 : /*
36 : tile.{h,cpp}: Texture handling stuff.
37 : */
38 :
39 : /*
40 : Find out the full path of an image by trying different filename
41 : extensions.
42 :
43 : If failed, return "".
44 :
45 : TODO: Should probably be moved out from here, because things needing
46 : this function do not need anything else from this header
47 : */
48 : std::string getImagePath(std::string path);
49 :
50 : /*
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.
53 :
54 : Checks all supported extensions by replacing the original extension.
55 :
56 : If not found, returns "".
57 :
58 : Utilizes a thread-safe cache.
59 : */
60 : std::string getTexturePath(const std::string &filename);
61 :
62 : void clearTextureNameCache();
63 :
64 : /*
65 : ITextureSource::generateTextureFromMesh parameters
66 : */
67 : namespace irr {namespace scene {class IMesh;}}
68 2 : struct TextureFromMeshParams
69 : {
70 : scene::IMesh *mesh;
71 : core::dimension2d<u32> dim;
72 : std::string rtt_texture_name;
73 : bool delete_texture_on_shutdown;
74 : v3f camera_position;
75 : v3f camera_lookat;
76 : core::CMatrix4<f32> camera_projection_matrix;
77 : video::SColorf ambient_light;
78 : v3f light_position;
79 : video::SColorf light_color;
80 : f32 light_radius;
81 : };
82 :
83 : /*
84 : TextureSource creates and caches textures.
85 : */
86 :
87 : class ISimpleTextureSource
88 : {
89 : public:
90 2 : ISimpleTextureSource(){}
91 2 : virtual ~ISimpleTextureSource(){}
92 : virtual video::ITexture* getTexture(
93 : const std::string &name, u32 *id = NULL) = 0;
94 : };
95 :
96 : class ITextureSource : public ISimpleTextureSource
97 : {
98 : public:
99 1 : ITextureSource(){}
100 1 : 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;
114 : };
115 :
116 : class IWritableTextureSource : public ITextureSource
117 : {
118 : public:
119 1 : IWritableTextureSource(){}
120 1 : 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;
130 :
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;
136 : };
137 :
138 : IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
139 :
140 : #ifdef __ANDROID__
141 : video::IImage * Align2Npot2(video::IImage * image, video::IVideoDriver* driver);
142 : #endif
143 :
144 : enum MaterialType{
145 : TILE_MATERIAL_BASIC,
146 : TILE_MATERIAL_ALPHA,
147 : TILE_MATERIAL_LIQUID_TRANSPARENT,
148 : TILE_MATERIAL_LIQUID_OPAQUE,
149 : TILE_MATERIAL_WAVING_LEAVES,
150 : TILE_MATERIAL_WAVING_PLANTS
151 : };
152 :
153 : // Material flags
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
165 :
166 : /*
167 : This fully defines the looks of a tile.
168 : The SMaterial of a tile is constructed according to this.
169 : */
170 : struct FrameSpec
171 : {
172 863 : FrameSpec():
173 : texture_id(0),
174 : texture(NULL),
175 863 : normal_texture(NULL)
176 : {
177 863 : }
178 : u32 texture_id;
179 : video::ITexture *texture;
180 : video::ITexture *normal_texture;
181 : };
182 :
183 64556027 : struct TileSpec
184 : {
185 28836573 : TileSpec():
186 : texture_id(0),
187 : texture(NULL),
188 : normal_texture(NULL),
189 : alpha(255),
190 : material_type(TILE_MATERIAL_BASIC),
191 : material_flags(
192 : //0 // <- DEBUG, Use the one below
193 : MATERIAL_FLAG_BACKFACE_CULLING
194 : ),
195 : shader_id(0),
196 : animation_frame_count(1),
197 : animation_frame_length_ms(0),
198 28836573 : rotation(0)
199 : {
200 28836573 : }
201 :
202 14728756 : bool operator==(const TileSpec &other) const
203 : {
204 : return (
205 16356249 : texture_id == other.texture_id &&
206 : /* texture == other.texture && */
207 3254986 : alpha == other.alpha &&
208 3254986 : material_type == other.material_type &&
209 17981589 : material_flags == other.material_flags &&
210 1625340 : rotation == other.rotation
211 14728756 : );
212 : }
213 :
214 14638135 : bool operator!=(const TileSpec &other) const
215 : {
216 14638135 : return !(*this == other);
217 : }
218 :
219 : // Sets everything else except the texture in the material
220 157 : void applyMaterialOptions(video::SMaterial &material) const
221 : {
222 157 : switch (material_type) {
223 : case TILE_MATERIAL_BASIC:
224 157 : material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
225 157 : break;
226 : case TILE_MATERIAL_ALPHA:
227 0 : material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
228 0 : break;
229 : case TILE_MATERIAL_LIQUID_TRANSPARENT:
230 0 : material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
231 0 : break;
232 : case TILE_MATERIAL_LIQUID_OPAQUE:
233 0 : material.MaterialType = video::EMT_SOLID;
234 0 : break;
235 : case TILE_MATERIAL_WAVING_LEAVES:
236 0 : material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
237 0 : break;
238 : case TILE_MATERIAL_WAVING_PLANTS:
239 0 : material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
240 0 : break;
241 : }
242 157 : material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
243 157 : ? true : false;
244 157 : }
245 :
246 31066 : void applyMaterialOptionsWithShaders(video::SMaterial &material) const
247 : {
248 31066 : material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING)
249 31066 : ? true : false;
250 31066 : }
251 :
252 : u32 texture_id;
253 : video::ITexture *texture;
254 : video::ITexture *normal_texture;
255 :
256 : // Vertex alpha (when MATERIAL_ALPHA_VERTEX is used)
257 : u8 alpha;
258 : // Material parameters
259 : u8 material_type;
260 : u8 material_flags;
261 : u32 shader_id;
262 : // Animation parameters
263 : u8 animation_frame_count;
264 : u16 animation_frame_length_ms;
265 : std::vector<FrameSpec> frames;
266 :
267 : u8 rotation;
268 : };
269 :
270 : #endif
|