Minetest  5.4.0
tile.h
Go to the documentation of this file.
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 #pragma once
21 
22 #include "irrlichttypes.h"
23 #include "irr_v3d.h"
24 #include <ITexture.h>
25 #include <string>
26 #include <vector>
27 #include <SMaterial.h>
28 #include "util/numeric.h"
29 #include "config.h"
30 
31 #if ENABLE_GLES
32 #include <IVideoDriver.h>
33 #endif
34 
35 class IGameDef;
36 struct TileSpec;
37 struct TileDef;
38 
39 typedef std::vector<video::SColor> Palette;
40 
41 /*
42  tile.{h,cpp}: Texture handling stuff.
43 */
44 
45 /*
46  Find out the full path of an image by trying different filename
47  extensions.
48 
49  If failed, return "".
50 
51  TODO: Should probably be moved out from here, because things needing
52  this function do not need anything else from this header
53 */
54 std::string getImagePath(std::string path);
55 
56 /*
57  Gets the path to a texture by first checking if the texture exists
58  in texture_path and if not, using the data path.
59 
60  Checks all supported extensions by replacing the original extension.
61 
62  If not found, returns "".
63 
64  Utilizes a thread-safe cache.
65 */
66 std::string getTexturePath(const std::string &filename, bool *is_base_pack = nullptr);
67 
69 
70 /*
71  TextureSource creates and caches textures.
72 */
73 
75 {
76 public:
77  ISimpleTextureSource() = default;
78 
79  virtual ~ISimpleTextureSource() = default;
80 
81  virtual video::ITexture* getTexture(
82  const std::string &name, u32 *id = nullptr) = 0;
83 };
84 
86 {
87 public:
88  ITextureSource() = default;
89 
90  virtual ~ITextureSource() = default;
91 
92  virtual u32 getTextureId(const std::string &name)=0;
93  virtual std::string getTextureName(u32 id)=0;
94  virtual video::ITexture* getTexture(u32 id)=0;
95  virtual video::ITexture* getTexture(
96  const std::string &name, u32 *id = nullptr)=0;
97  virtual video::ITexture* getTextureForMesh(
98  const std::string &name, u32 *id = nullptr) = 0;
105  virtual Palette* getPalette(const std::string &name) = 0;
106  virtual bool isKnownSourceImage(const std::string &name)=0;
107  virtual video::ITexture* getNormalTexture(const std::string &name)=0;
108  virtual video::SColor getTextureAverageColor(const std::string &name)=0;
109  virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
110 };
111 
113 {
114 public:
116 
117  virtual ~IWritableTextureSource() = default;
118 
119  virtual u32 getTextureId(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 = nullptr)=0;
124  virtual bool isKnownSourceImage(const std::string &name)=0;
125 
126  virtual void processQueue()=0;
127  virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
128  virtual void rebuildImagesAndTextures()=0;
129  virtual video::ITexture* getNormalTexture(const std::string &name)=0;
130  virtual video::SColor getTextureAverageColor(const std::string &name)=0;
131  virtual video::ITexture *getShaderFlagsTexture(bool normalmap_present)=0;
132 };
133 
135 
136 #if ENABLE_GLES
137 bool hasNPotSupport();
138 video::IImage * Align2Npot2(video::IImage * image, irr::video::IVideoDriver* driver);
139 #endif
140 
154 };
155 
156 // Material flags
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 #define MATERIAL_FLAG_ANIMATION 0x08
165 //#define MATERIAL_FLAG_HIGHLIGHTED 0x10
166 #define MATERIAL_FLAG_TILEABLE_HORIZONTAL 0x20
167 #define MATERIAL_FLAG_TILEABLE_VERTICAL 0x40
168 
169 /*
170  This fully defines the looks of a tile.
171  The SMaterial of a tile is constructed according to this.
172 */
173 struct FrameSpec
174 {
175  FrameSpec() = default;
176 
177  u32 texture_id = 0;
178  video::ITexture *texture = nullptr;
179  video::ITexture *normal_texture = nullptr;
180  video::ITexture *flags_texture = nullptr;
181 };
182 
183 #define MAX_TILE_LAYERS 2
184 
186 struct TileLayer
187 {
188  TileLayer() = default;
189 
193  bool operator==(const TileLayer &other) const
194  {
195  return
196  texture_id == other.texture_id &&
197  material_type == other.material_type &&
198  material_flags == other.material_flags &&
199  color == other.color &&
200  scale == other.scale;
201  }
202 
206  bool operator!=(const TileLayer &other) const
207  {
208  return !(*this == other);
209  }
210 
211  // Sets everything else except the texture in the material
212  void applyMaterialOptions(video::SMaterial &material) const
213  {
214  switch (material_type) {
218  material.MaterialType = video::EMT_SOLID;
219  break;
220  case TILE_MATERIAL_BASIC:
224  material.MaterialTypeParam = 0.5;
225  material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
226  break;
227  case TILE_MATERIAL_ALPHA:
230  material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
231  break;
232  default:
233  break;
234  }
235  material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0;
237  material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
238  }
240  material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
241  }
242  }
243 
244  void applyMaterialOptionsWithShaders(video::SMaterial &material) const
245  {
246  material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0;
248  material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
249  material.TextureLayer[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
250  }
252  material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
253  material.TextureLayer[1].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
254  }
255  }
256 
257  bool isTileable() const
258  {
261  }
262 
263  // Ordered for size, please do not reorder
264 
265  video::ITexture *texture = nullptr;
266  video::ITexture *normal_texture = nullptr;
267  video::ITexture *flags_texture = nullptr;
268 
269  u32 shader_id = 0;
270 
271  u32 texture_id = 0;
272 
275 
278  //0 // <- DEBUG, Use the one below
282 
284  bool has_color = false;
285 
286  std::vector<FrameSpec> *frames = nullptr;
287 
292  video::SColor color;
293 
294  u8 scale;
295 };
296 
300 struct TileSpec
301 {
302  TileSpec() = default;
303 
307  bool isTileable(const TileSpec &other) const {
308  for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
309  if (layers[layer] != other.layers[layer])
310  return false;
311  if (!layers[layer].isTileable())
312  return false;
313  }
314  return rotation == 0
315  && rotation == other.rotation
316  && emissive_light == other.emissive_light;
317  }
318 
320  bool world_aligned = false;
322  u8 rotation = 0;
327 };
328 
329 std::vector<std::string> getTextureDirs();
Definition: gamedef.h:49
Definition: tile.h:75
virtual ~ISimpleTextureSource()=default
virtual video::ITexture * getTexture(const std::string &name, u32 *id=nullptr)=0
ISimpleTextureSource()=default
Definition: tile.h:86
ITextureSource()=default
virtual std::string getTextureName(u32 id)=0
virtual u32 getTextureId(const std::string &name)=0
virtual video::ITexture * getTexture(u32 id)=0
virtual video::ITexture * getNormalTexture(const std::string &name)=0
virtual Palette * getPalette(const std::string &name)=0
virtual video::ITexture * getTextureForMesh(const std::string &name, u32 *id=nullptr)=0
virtual video::ITexture * getTexture(const std::string &name, u32 *id=nullptr)=0
virtual ~ITextureSource()=default
virtual video::SColor getTextureAverageColor(const std::string &name)=0
virtual video::ITexture * getShaderFlagsTexture(bool normalmap_present)=0
virtual bool isKnownSourceImage(const std::string &name)=0
Definition: tile.h:113
virtual video::ITexture * getShaderFlagsTexture(bool normalmap_present)=0
virtual video::SColor getTextureAverageColor(const std::string &name)=0
virtual video::ITexture * getNormalTexture(const std::string &name)=0
virtual void insertSourceImage(const std::string &name, video::IImage *img)=0
IWritableTextureSource()=default
virtual video::ITexture * getTexture(const std::string &name, u32 *id=nullptr)=0
virtual std::string getTextureName(u32 id)=0
virtual u32 getTextureId(const std::string &name)=0
virtual void processQueue()=0
virtual bool isKnownSourceImage(const std::string &name)=0
virtual ~IWritableTextureSource()=default
virtual video::ITexture * getTexture(u32 id)=0
virtual void rebuildImagesAndTextures()=0
Definition: tile.h:174
video::ITexture * texture
Definition: tile.h:178
u32 texture_id
Definition: tile.h:177
video::ITexture * normal_texture
Definition: tile.h:179
video::ITexture * flags_texture
Definition: tile.h:180
FrameSpec()=default
Definition: nodedef.h:247
Defines a layer of a tile.
Definition: tile.h:187
video::SColor color
Definition: tile.h:292
bool operator==(const TileLayer &other) const
Definition: tile.h:193
std::vector< FrameSpec > * frames
Definition: tile.h:286
bool has_color
If true, the tile has its own color.
Definition: tile.h:284
void applyMaterialOptionsWithShaders(video::SMaterial &material) const
Definition: tile.h:244
u8 scale
Definition: tile.h:294
void applyMaterialOptions(video::SMaterial &material) const
Definition: tile.h:212
bool isTileable() const
Definition: tile.h:257
TileLayer()=default
u16 animation_frame_count
Definition: tile.h:274
u8 material_flags
Definition: tile.h:277
video::ITexture * normal_texture
Definition: tile.h:266
video::ITexture * texture
Definition: tile.h:265
u32 shader_id
Definition: tile.h:269
u16 animation_frame_length_ms
Definition: tile.h:273
u8 material_type
Definition: tile.h:276
video::ITexture * flags_texture
Definition: tile.h:267
u32 texture_id
Definition: tile.h:271
bool operator!=(const TileLayer &other) const
Definition: tile.h:206
Definition: tile.h:301
TileSpec()=default
bool world_aligned
If true, the tile rotation is ignored.
Definition: tile.h:320
u8 rotation
Tile rotation.
Definition: tile.h:322
bool isTileable(const TileSpec &other) const
Definition: tile.h:307
u8 emissive_light
This much light does the tile emit.
Definition: tile.h:324
TileLayer layers[MAX_TILE_LAYERS]
The first is base texture, the second is overlay.
Definition: tile.h:326
IWritableTextureSource * createTextureSource()
Definition: tile.cpp:434
#define MATERIAL_FLAG_BACKFACE_CULLING
Definition: tile.h:158
std::vector< video::SColor > Palette
Definition: tile.h:37
std::string getImagePath(std::string path)
Definition: tile.cpp:90
#define MATERIAL_FLAG_TILEABLE_VERTICAL
Definition: tile.h:167
std::vector< std::string > getTextureDirs()
Definition: tile.cpp:2286
void clearTextureNameCache()
Definition: tile.cpp:173
std::string getTexturePath(const std::string &filename, bool *is_base_pack=nullptr)
Definition: tile.cpp:125
MaterialType
Definition: tile.h:141
@ TILE_MATERIAL_PLAIN_ALPHA
Definition: tile.h:153
@ TILE_MATERIAL_OPAQUE
Definition: tile.h:148
@ TILE_MATERIAL_LIQUID_TRANSPARENT
Definition: tile.h:144
@ TILE_MATERIAL_WAVING_PLANTS
Definition: tile.h:147
@ TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT
Definition: tile.h:150
@ TILE_MATERIAL_BASIC
Definition: tile.h:142
@ TILE_MATERIAL_LIQUID_OPAQUE
Definition: tile.h:145
@ TILE_MATERIAL_WAVING_LIQUID_OPAQUE
Definition: tile.h:151
@ TILE_MATERIAL_WAVING_LIQUID_BASIC
Definition: tile.h:149
@ TILE_MATERIAL_WAVING_LEAVES
Definition: tile.h:146
@ TILE_MATERIAL_ALPHA
Definition: tile.h:143
@ TILE_MATERIAL_PLAIN
Definition: tile.h:152
#define MATERIAL_FLAG_TILEABLE_HORIZONTAL
Definition: tile.h:166
#define MAX_TILE_LAYERS
Definition: tile.h:183