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 
72  std::string m_texname;
73 
74  std::string m_full_path;
75 
78  std::string m_sfx_name;
79 
84 
90 
99  bool m_surface;
100 
102  bool m_zipper;
103 
106 
110 
115 
118  bool m_ignore;
119 
122 
123  bool m_complain_if_not_found;
124 
125  bool m_deprecated;
126 
127  bool m_installed;
128 
131 
134 
137 
139  CollisionReaction m_collision_reaction;
140 
143 
148  std::map<void*, bool> m_mirrorred_mesh_buffers;
149 
150  ParticleKind* m_particles_effects[EMIT_KINDS_COUNT];
151 
153  unsigned int m_clamp_tex;
154 
156  std::vector<float> m_hue_settings;
157 
160 
163 
166 
199 
200  std::string m_mask;
201 
202  std::string m_colorization_mask;
203 
204  void init ();
205  void install (std::function<void(video::IImage*)> image_mani = nullptr);
206  void initCustomSFX(const XMLNode *sfx);
207  void initParticlesEffect(const XMLNode *node);
208 
209  // SP usage
210  std::string m_shader_name;
211  std::string m_uv_two_tex;
212  // Full path for textures in sp shader
213  std::string m_sampler_path[6];
214  std::string m_container_id;
215  void loadContainerId();
216 
217 public:
218  Material(const XMLNode *node, bool deprecated);
219  Material(const std::string& fname,
220  bool is_full_path=false,
221  bool complain_if_not_found=true,
222  bool load_texture = true,
223  const std::string& shader_name = "solid");
224  ~Material ();
225 
226  void unloadTexture();
227 
228  void setSFXSpeed(SFXBase *sfx, float speed, bool should_be_paused) const;
229  void setMaterialProperties(video::SMaterial *m, scene::IMeshBuffer* mb);
230 
232  video::ITexture *getTexture(bool srgb = true, bool premul_alpha = false);
233  // ------------------------------------------------------------------------
234  bool isIgnore () const { return m_ignore; }
235  // ------------------------------------------------------------------------
237  bool isZipper () const { return m_zipper; }
238  // ------------------------------------------------------------------------
241  bool isDriveReset () const { return m_drive_reset; }
242  // ------------------------------------------------------------------------
245  bool isColorizable () const { return m_colorizable; }
246  // ------------------------------------------------------------------------
249  float getColorizationFactor () const { return m_colorization_factor; }
250  // ------------------------------------------------------------------------
251  bool hasRandomHue() const { return !m_hue_settings.empty(); }
252  // ------------------------------------------------------------------------
255  float getRandomHue()
256  {
257  if (m_hue_settings.empty())
258  return 0.0f;
259  const unsigned int hue = m_random_hue.get((int)m_hue_settings.size());
260  assert(hue < m_hue_settings.size());
261  return m_hue_settings[hue];
262  }
263  // ------------------------------------------------------------------------
266  CollisionReaction getCollisionReaction() const { return m_collision_reaction; }
267 
268  // ------------------------------------------------------------------------
269  std::string getCrashResetParticles() const { return m_collision_particles; }
270 
271  // ------------------------------------------------------------------------
272  bool highTireAdhesion () const { return m_high_tire_adhesion; }
273  // ------------------------------------------------------------------------
274  const std::string&
275  getTexFname () const { return m_texname; }
276  // ------------------------------------------------------------------------
277  const std::string&
278  getTexFullPath () const { return m_full_path; }
279 
280  // ------------------------------------------------------------------------
281  bool isTransparent () const
282  {
283  return m_shader_name == "additive" || m_shader_name == "alphablend" ||
284  m_shader_name == "displace";
285  }
286 
287  // ------------------------------------------------------------------------
289  float getMaxSpeedFraction() const { return m_max_speed_fraction; }
290  // ------------------------------------------------------------------------
294  int getSlowDownTicks() const { return m_slowdown_ticks; }
295  // ------------------------------------------------------------------------
300  bool isBelowSurface () const { return m_below_surface; }
301  // ------------------------------------------------------------------------
305  bool isSurface () const { return m_surface; }
306  // ------------------------------------------------------------------------
309  const std::string &getSFXName() const { return m_sfx_name; }
310  // ------------------------------------------------------------------------
314  const ParticleKind* getParticlesWhen(ParticleConditions cond) const
315  {
316  return m_particles_effects[cond];
317  } // getParticlesWhen
318  // ------------------------------------------------------------------------
321  bool hasFallingEffect() const {return m_falling_effect; }
322  // ------------------------------------------------------------------------
325  bool isJumpTexture() const { return m_is_jump_texture; }
326  // ------------------------------------------------------------------------
330  bool hasGravity() const { return m_has_gravity; }
331  // ------------------------------------------------------------------------
333  void getZipperParameter(float *zipper_max_speed_increase,
334  float *zipper_duration,
335  float *zipper_speed_gain,
336  float *zipper_fade_out_time,
337  float *zipper_engine_force) const
338  {
339  *zipper_max_speed_increase = m_zipper_max_speed_increase;
340  *zipper_duration = m_zipper_duration;
341  *zipper_speed_gain = m_zipper_speed_gain;
342  *zipper_fade_out_time = m_zipper_fade_out_time;
343  *zipper_engine_force = m_zipper_engine_force;
344  } // getZipperParameter
345  // ------------------------------------------------------------------------
349  float getZipperMinSpeed() const { return m_zipper_min_speed; }
350  // ------------------------------------------------------------------------
352  char getMirrorAxisInReverse() const { return m_mirror_axis_when_reverse; }
353  // ------------------------------------------------------------------------
354  const std::string& getAlphaMask() const { return m_mask; }
355  // ------------------------------------------------------------------------
356  const std::string& getColorizationMask() const
357  { return m_colorization_mask; }
358  // ------------------------------------------------------------------------
359  void setShaderName(const std::string& name) { m_shader_name = name; }
360  // ------------------------------------------------------------------------
361  const std::string& getShaderName() const { return m_shader_name; }
362  // ------------------------------------------------------------------------
363  /* This is used for finding correct material for spm*/
364  const std::string& getUVTwoTexture() const
365  { return m_uv_two_tex; }
366  // ------------------------------------------------------------------------
367  bool use2UV() const { return !m_uv_two_tex.empty(); }
368  // ------------------------------------------------------------------------
369  const std::string& getSamplerPath(unsigned layer) const
370  {
371  assert(layer < 6);
372  return m_sampler_path[layer];
373  }
374  // ------------------------------------------------------------------------
375  /* Return the container id used by texture compression to cache texture in
376  * cache folder with unique name, if no texture compression is used for
377  * this material, then it will always return empty. */
378  const std::string& getContainerId() const
379  {
380  static std::string empty;
381  if (!m_tex_compression)
382  return empty;
383  return m_container_id;
384  }
385 };
386 
387 
388 #endif
389 
390 /* EOF */
391 
bool m_surface
A material that is a surface only, i.e.
Definition: material.hpp:99
bool m_tex_compression
True if this material should use texture compression.
Definition: material.hpp:133
A random number generator.
Definition: random_generator.hpp:33
float m_zipper_duration
Time a zipper stays activated.
Definition: material.hpp:189
float m_zipper_min_speed
Minimum speed on this terrain.
Definition: material.hpp:170
type of particles
Definition: particle_kind.hpp:41
float m_sfx_min_pitch
The minimum pitch to be used (at minimum speed).
Definition: material.hpp:176
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:114
bool isDriveReset() const
Returns if this material should trigger a rescue if a kart is driving on it.
Definition: material.hpp:241
RandomGenerator m_random_hue
Random generator for getting pre-defined hue.
Definition: material.hpp:159
bool isColorizable() const
Returns if this material can be colorized.
Definition: material.hpp:245
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:109
Definition: three_d_animation.hpp:32
float m_sfx_max_pitch
The maximum pitch to be used (at maximum speed).
Definition: material.hpp:178
float m_colorization_factor
Minimum resulting saturation when colorized (from 0 to 1)
Definition: material.hpp:136
CollisionReaction m_collision_reaction
If a kart is rescued when crashing into this surface.
Definition: material.hpp:139
const std::string & getSFXName() const
Returns the name of a special sfx to play while a kart is on this terrain.
Definition: material.hpp:309
float getRandomHue()
Returns a random hue when colorized.
Definition: material.hpp:255
bool isBelowSurface() const
Returns true if this material is under some other mesh and therefore requires another raycast to find...
Definition: material.hpp:300
bool m_colorizable
True if this material can be colorized (like red/blue in team game).
Definition: material.hpp:130
float getMaxSpeedFraction() const
Returns the fraction of maximum speed on this material.
Definition: material.hpp:289
int m_slowdown_ticks
How much the top speed is reduced per second.
Definition: material.hpp:162
bool isJumpTexture() const
Returns if being in the air after this texture should start the jump animation.
Definition: material.hpp:325
int getSlowDownTicks() const
Returns how long it will take for a slowdown to take effect.
Definition: material.hpp:294
unsigned int m_clamp_tex
Texture clamp bitmask.
Definition: material.hpp:153
char getMirrorAxisInReverse() const
True if this texture should have the U coordinates mirrored.
Definition: material.hpp:352
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:93
int get(int n)
Returns a pseudo random number between 0 and n-1 inclusive.
Definition: random_generator.hpp:45
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:25
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:330
std::string m_texname
Name of the texture.
Definition: material.hpp:72
bool isZipper() const
Returns true if this material is a zipper.
Definition: material.hpp:237
video::ITexture * m_texture
Pointer to the texture.
Definition: material.hpp:69
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:186
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:148
bool isSurface() const
Returns true if this material is a surface, i.e.
Definition: material.hpp:305
Definition: material.hpp:47
float m_zipper_fade_out_time
Time it takes for the zipper advantage to fade out.
Definition: material.hpp:196
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:314
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:193
utility class used to parse XML files
Definition: xml_node.hpp:47
bool m_zipper
If the material is a zipper, i.e.
Definition: material.hpp:102
float getZipperMinSpeed() const
Returns the minimum speed of a kart on this material.
Definition: material.hpp:349
float m_sfx_max_speed
The speed at which the maximum pitch is used.
Definition: material.hpp:174
float m_zipper_engine_force
Additional engine force.
Definition: material.hpp:198
bool m_high_tire_adhesion
True if the material shouldn&#39;t be "slippy" at an angle.
Definition: material.hpp:121
bool m_drive_reset
If a kart is rescued when driving on this surface.
Definition: material.hpp:105
The base class for sound effects.
Definition: sfx_base.hpp:42
std::vector< float > m_hue_settings
List of hue pre-defined for colorization (from 0 to 1)
Definition: material.hpp:156
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:78
std::string m_collision_particles
Particles to show on touch.
Definition: material.hpp:142
CollisionReaction getCollisionReaction() const
Returns if this material should trigger a rescue if a kart crashes against it.
Definition: material.hpp:266
bool m_below_surface
Set if being on this surface means being under some other mesh.
Definition: material.hpp:89
float getColorizationFactor() const
Returns the minimum resulting saturation when colorized.
Definition: material.hpp:249
float m_sfx_pitch_per_speed
(max_pitch-min_pitch) / (max_speed - min_speed).
Definition: material.hpp:182
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:333
float m_sfx_min_speed
The minimum speed at which a special sfx is started to be played.
Definition: material.hpp:172
bool m_ignore
If the property should be ignored in the physics.
Definition: material.hpp:118
float m_max_speed_fraction
Maximum speed at which no more slow down occurs.
Definition: material.hpp:165
bool hasFallingEffect() const
Returns true if a kart falling over this kind of material triggers the special falling camera...
Definition: material.hpp:321
char m_mirror_axis_when_reverse
Either &#39; &#39; (no mirroring), &#39;U&#39; or &#39;V&#39; if a texture needs to be mirrored when driving in reverse...
Definition: material.hpp:83