SuperTuxKart
material.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2004-2015 Steve Baker <sjbaker1@airmail.net>
4 // Copyright (C) 2010-2015 Steve Baker, Joerg Henrichs
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 3
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 
20 #ifndef HEADER_MATERIAL_HPP
21 #define HEADER_MATERIAL_HPP
22 
23 #include "utils/no_copy.hpp"
24 #include "utils/random_generator.hpp"
25 
26 #include <array>
27 #include <assert.h>
28 #include <functional>
29 #include <map>
30 #include <string>
31 #include <vector>
32 
33 namespace irr
34 {
35  namespace video { class ITexture; class IImage; class SMaterial; }
36  namespace scene { class ISceneNode; class IMeshBuffer; }
37 }
38 using namespace irr;
39 
40 class XMLNode;
41 class SFXBase;
42 class ParticleKind;
43 
47 class Material : public NoCopy
48 {
49 public:
50  enum ParticleConditions
51  {
52  EMIT_ON_DRIVE = 0,
53  EMIT_ON_SKID,
54 
55  EMIT_KINDS_COUNT
56  };
57 
58  enum CollisionReaction
59  {
60  NORMAL,
61  RESCUE,
62  PUSH_BACK,
63  PUSH_SOCCER_BALL
64  };
65 
66 private:
67 
69  video::ITexture *m_texture;
70 
71  std::array<video::ITexture*, 4> m_vk_textures;
72 
74  std::string m_texname;
75 
76  std::string m_full_path;
77 
80  std::string m_sfx_name;
81 
86 
92 
101  bool m_surface;
102 
104  bool m_zipper;
105 
108 
112 
117 
120  bool m_ignore;
121 
124 
125  bool m_complain_if_not_found;
126 
127  bool m_deprecated;
128 
129  bool m_installed;
130 
133 
136 
139 
141  CollisionReaction m_collision_reaction;
142 
145 
150  std::map<void*, bool> m_mirrorred_mesh_buffers;
151 
152  ParticleKind* m_particles_effects[EMIT_KINDS_COUNT];
153 
155  unsigned int m_clamp_tex;
156 
158  std::vector<float> m_hue_settings;
159 
162 
165 
168 
201 
202  std::string m_mask;
203 
204  std::string m_colorization_mask;
205 
206  void init ();
207  void install (std::function<void(video::IImage*)> image_mani = nullptr,
208  video::SMaterial* m = NULL);
209  void initCustomSFX(const XMLNode *sfx);
210  void initParticlesEffect(const XMLNode *node);
211 
212  // SP usage
213  std::string m_shader_name;
214  std::string m_uv_two_tex;
215  // Full path for textures in sp shader
216  std::array<std::string, 6> m_sampler_path;
217  std::string m_container_id;
218  void loadContainerId();
219 
220 public:
221  Material(const XMLNode *node, bool deprecated);
222  Material(const std::string& fname,
223  bool is_full_path=false,
224  bool complain_if_not_found=true,
225  bool load_texture = true,
226  const std::string& shader_name = "solid");
227  ~Material ();
228 
229  void unloadTexture();
230 
231  void setSFXSpeed(SFXBase *sfx, float speed, bool should_be_paused) const;
232  void setMaterialProperties(video::SMaterial *m, scene::IMeshBuffer* mb);
233 
235  video::ITexture *getTexture(bool srgb = true, bool premul_alpha = false);
236  // ------------------------------------------------------------------------
237  bool isIgnore () const { return m_ignore; }
238  // ------------------------------------------------------------------------
240  bool isZipper () const { return m_zipper; }
241  // ------------------------------------------------------------------------
244  bool isDriveReset () const { return m_drive_reset; }
245  // ------------------------------------------------------------------------
248  bool isColorizable () const { return m_colorizable; }
249  // ------------------------------------------------------------------------
252  float getColorizationFactor () const { return m_colorization_factor; }
253  // ------------------------------------------------------------------------
254  bool hasRandomHue() const { return !m_hue_settings.empty(); }
255  // ------------------------------------------------------------------------
258  float getRandomHue()
259  {
260  if (m_hue_settings.empty())
261  return 0.0f;
262  const unsigned int hue = m_random_hue.get((int)m_hue_settings.size());
263  assert(hue < m_hue_settings.size());
264  return m_hue_settings[hue];
265  }
266  // ------------------------------------------------------------------------
269  CollisionReaction getCollisionReaction() const { return m_collision_reaction; }
270 
271  // ------------------------------------------------------------------------
272  std::string getCrashResetParticles() const { return m_collision_particles; }
273 
274  // ------------------------------------------------------------------------
275  bool highTireAdhesion () const { return m_high_tire_adhesion; }
276  // ------------------------------------------------------------------------
277  const std::string&
278  getTexFname () const { return m_texname; }
279  // ------------------------------------------------------------------------
280  const std::string&
281  getTexFullPath () const { return m_full_path; }
282 
283  // ------------------------------------------------------------------------
284  bool isTransparent () const
285  {
286  return m_shader_name == "additive" || m_shader_name == "alphablend" ||
287  m_shader_name == "displace";
288  }
289 
290  // ------------------------------------------------------------------------
291  bool useAlphaChannel () const
292  {
293  return isTransparent() || m_shader_name == "alphatest" ||
294  m_shader_name == "unlit" || m_shader_name == "grass";
295  }
296 
297  // ------------------------------------------------------------------------
299  float getMaxSpeedFraction() const { return m_max_speed_fraction; }
300  // ------------------------------------------------------------------------
304  int getSlowDownTicks() const { return m_slowdown_ticks; }
305  // ------------------------------------------------------------------------
310  bool isBelowSurface () const { return m_below_surface; }
311  // ------------------------------------------------------------------------
315  bool isSurface () const { return m_surface; }
316  // ------------------------------------------------------------------------
319  const std::string &getSFXName() const { return m_sfx_name; }
320  // ------------------------------------------------------------------------
324  const ParticleKind* getParticlesWhen(ParticleConditions cond) const
325  {
326  return m_particles_effects[cond];
327  } // getParticlesWhen
328  // ------------------------------------------------------------------------
331  bool hasFallingEffect() const {return m_falling_effect; }
332  // ------------------------------------------------------------------------
335  bool isJumpTexture() const { return m_is_jump_texture; }
336  // ------------------------------------------------------------------------
340  bool hasGravity() const { return m_has_gravity; }
341  // ------------------------------------------------------------------------
343  void getZipperParameter(float *zipper_max_speed_increase,
344  float *zipper_duration,
345  float *zipper_speed_gain,
346  float *zipper_fade_out_time,
347  float *zipper_engine_force) const
348  {
349  *zipper_max_speed_increase = m_zipper_max_speed_increase;
350  *zipper_duration = m_zipper_duration;
351  *zipper_speed_gain = m_zipper_speed_gain;
352  *zipper_fade_out_time = m_zipper_fade_out_time;
353  *zipper_engine_force = m_zipper_engine_force;
354  } // getZipperParameter
355  // ------------------------------------------------------------------------
359  float getZipperMinSpeed() const { return m_zipper_min_speed; }
360  // ------------------------------------------------------------------------
362  char getMirrorAxisInReverse() const { return m_mirror_axis_when_reverse; }
363  // ------------------------------------------------------------------------
364  const std::string& getAlphaMask() const { return m_mask; }
365  // ------------------------------------------------------------------------
366  const std::string& getColorizationMask() const
367  { return m_colorization_mask; }
368  // ------------------------------------------------------------------------
369  void setShaderName(const std::string& name) { m_shader_name = name; }
370  // ------------------------------------------------------------------------
371  const std::string& getShaderName() const { return m_shader_name; }
372  // ------------------------------------------------------------------------
373  /* This is used for finding correct material for spm*/
374  const std::string& getUVTwoTexture() const
375  { return m_uv_two_tex; }
376  // ------------------------------------------------------------------------
377  bool use2UV() const { return !m_uv_two_tex.empty(); }
378  // ------------------------------------------------------------------------
379  const std::string& getSamplerPath(unsigned layer) const
380  {
381  assert(layer < 6);
382  return m_sampler_path[layer];
383  }
384  // ------------------------------------------------------------------------
385  /* Return the container id used by texture compression to cache texture in
386  * cache folder with unique name, if no texture compression is used for
387  * this material, then it will always return empty. */
388  const std::string& getContainerId() const
389  {
390  static std::string empty;
391  if (!m_tex_compression)
392  return empty;
393  return m_container_id;
394  }
395  // ------------------------------------------------------------------------
396  std::function<void(irr::video::IImage*)> getMaskImageMani() const;
397 };
398 
399 
400 #endif
401 
402 /* EOF */
403 
Definition: material.hpp:48
float getZipperMinSpeed() const
Returns the minimum speed of a kart on this material.
Definition: material.hpp:359
void getZipperParameter(float *zipper_max_speed_increase, float *zipper_duration, float *zipper_speed_gain, float *zipper_fade_out_time, float *zipper_engine_force) const
Returns the zipper parametersfor the current material.
Definition: material.hpp:343
float m_sfx_pitch_per_speed
(max_pitch-min_pitch) / (max_speed - min_speed).
Definition: material.hpp:184
float getMaxSpeedFraction() const
Returns the fraction of maximum speed on this material.
Definition: material.hpp:299
CollisionReaction getCollisionReaction() const
Returns if this material should trigger a rescue if a kart crashes against it.
Definition: material.hpp:269
bool hasGravity() const
Returns true if this texture adjusts the gravity vector of the kart to be parallel to the normal of t...
Definition: material.hpp:340
float m_zipper_duration
Time a zipper stays activated.
Definition: material.hpp:191
char m_mirror_axis_when_reverse
Either ' ' (no mirroring), 'U' or 'V' if a texture needs to be mirrored when driving in reverse.
Definition: material.hpp:85
float m_zipper_fade_out_time
Time it takes for the zipper advantage to fade out.
Definition: material.hpp:198
bool m_is_jump_texture
True if this is a texture that will start the jump animation when leaving it and being in the air.
Definition: material.hpp:111
float m_zipper_max_speed_increase
Additional speed allowed on top of the kart-specific maximum kart speed if a zipper is used.
Definition: material.hpp:188
float m_zipper_engine_force
Additional engine force.
Definition: material.hpp:200
std::string m_texname
Name of the texture.
Definition: material.hpp:74
bool m_high_tire_adhesion
True if the material shouldn't be "slippy" at an angle.
Definition: material.hpp:123
float m_sfx_min_pitch
The minimum pitch to be used (at minimum speed).
Definition: material.hpp:178
bool m_ignore
If the property should be ignored in the physics.
Definition: material.hpp:120
bool isDriveReset() const
Returns if this material should trigger a rescue if a kart is driving on it.
Definition: material.hpp:244
float getColorizationFactor() const
Returns the minimum resulting saturation when colorized.
Definition: material.hpp:252
const ParticleKind * getParticlesWhen(ParticleConditions cond) const
Get the kind of particles that are to be used on this material, in the given conditions.
Definition: material.hpp:324
bool m_falling_effect
If a kart is falling over a material with this flag set, it will trigger the special camera fall effe...
Definition: material.hpp:95
float m_zipper_speed_gain
A one time additional speed gain - the kart will instantly add this amount of speed to its current sp...
Definition: material.hpp:195
CollisionReaction m_collision_reaction
If a kart is rescued when crashing into this surface.
Definition: material.hpp:141
float m_sfx_max_pitch
The maximum pitch to be used (at maximum speed).
Definition: material.hpp:180
std::map< void *, bool > m_mirrorred_mesh_buffers
Associated with m_mirror_axis_when_reverse, to avoid mirroring the same material twice (setAllMateria...
Definition: material.hpp:150
float m_sfx_max_speed
The speed at which the maximum pitch is used.
Definition: material.hpp:176
std::vector< float > m_hue_settings
List of hue pre-defined for colorization (from 0 to 1)
Definition: material.hpp:158
std::string m_sfx_name
Name of a special sfx to play when a kart is on this terrain, or "" if no special sfx exists.
Definition: material.hpp:80
bool isBelowSurface() const
Returns true if this material is under some other mesh and therefore requires another raycast to find...
Definition: material.hpp:310
float m_colorization_factor
Minimum resulting saturation when colorized (from 0 to 1)
Definition: material.hpp:138
bool m_tex_compression
True if this material should use texture compression.
Definition: material.hpp:135
float m_sfx_min_speed
The minimum speed at which a special sfx is started to be played.
Definition: material.hpp:174
float getRandomHue()
Returns a random hue when colorized.
Definition: material.hpp:258
float m_max_speed_fraction
Maximum speed at which no more slow down occurs.
Definition: material.hpp:167
char getMirrorAxisInReverse() const
True if this texture should have the U coordinates mirrored.
Definition: material.hpp:362
float m_zipper_min_speed
Minimum speed on this terrain.
Definition: material.hpp:172
bool m_zipper
If the material is a zipper, i.e.
Definition: material.hpp:104
unsigned int m_clamp_tex
Texture clamp bitmask.
Definition: material.hpp:155
const std::string & getSFXName() const
Returns the name of a special sfx to play while a kart is on this terrain.
Definition: material.hpp:319
bool hasFallingEffect() const
Returns true if a kart falling over this kind of material triggers the special falling camera.
Definition: material.hpp:331
int m_slowdown_ticks
How much the top speed is reduced per second.
Definition: material.hpp:164
RandomGenerator m_random_hue
Random generator for getting pre-defined hue.
Definition: material.hpp:161
bool isZipper() const
Returns true if this material is a zipper.
Definition: material.hpp:240
bool isColorizable() const
Returns if this material can be colorized.
Definition: material.hpp:248
bool m_drive_reset
If a kart is rescued when driving on this surface.
Definition: material.hpp:107
bool isJumpTexture() const
Returns if being in the air after this texture should start the jump animation.
Definition: material.hpp:335
bool m_colorizable
True if this material can be colorized (like red/blue in team game).
Definition: material.hpp:132
bool isSurface() const
Returns true if this material is a surface, i.e.
Definition: material.hpp:315
bool m_surface
A material that is a surface only, i.e.
Definition: material.hpp:101
bool m_has_gravity
True if driving on this texture should adjust the gravity of the kart to be along the normal of the t...
Definition: material.hpp:116
video::ITexture * m_texture
Pointer to the texture.
Definition: material.hpp:69
int getSlowDownTicks() const
Returns how long it will take for a slowdown to take effect.
Definition: material.hpp:304
bool m_below_surface
Set if being on this surface means being under some other mesh.
Definition: material.hpp:91
std::string m_collision_particles
Particles to show on touch.
Definition: material.hpp:144
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
type of particles
Definition: particle_kind.hpp:42
A random number generator.
Definition: random_generator.hpp:33
int get(int n)
Returns a pseudo random number between 0 and n-1 inclusive.
Definition: random_generator.hpp:49
The base class for sound effects.
Definition: sfx_base.hpp:43
utility class used to parse XML files
Definition: xml_node.hpp:48