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 <map>
29 #include <string>
30 #include <vector>
31 
32 namespace irr
33 {
34  namespace video { class ITexture; class SMaterial; }
35  namespace scene { class ISceneNode; class IMeshBuffer; }
36 }
37 using namespace irr;
38 
39 class XMLNode;
40 class SFXBase;
41 class ParticleKind;
42 
46 class Material : public NoCopy
47 {
48 public:
49  enum ParticleConditions
50  {
51  EMIT_ON_DRIVE = 0,
52  EMIT_ON_SKID,
53 
54  EMIT_KINDS_COUNT
55  };
56 
57  enum CollisionReaction
58  {
59  NORMAL,
60  RESCUE,
61  PUSH_BACK,
62  PUSH_SOCCER_BALL
63  };
64 
65 private:
66 
68  video::ITexture *m_texture;
69 
71  std::string m_texname;
72 
73  std::string m_full_path;
74 
77  std::string m_sfx_name;
78 
83 
89 
98  bool m_surface;
99 
101  bool m_zipper;
102 
105 
109 
114 
117  bool m_ignore;
118 
121 
122  bool m_complain_if_not_found;
123 
124  bool m_deprecated;
125 
126  bool m_installed;
127 
130 
133 
136 
138  CollisionReaction m_collision_reaction;
139 
142 
147  std::map<void*, bool> m_mirrorred_mesh_buffers;
148 
149  ParticleKind* m_particles_effects[EMIT_KINDS_COUNT];
150 
152  unsigned int m_clamp_tex;
153 
155  std::vector<float> m_hue_settings;
156 
159 
162 
165 
198 
199  std::string m_mask;
200 
201  std::string m_colorization_mask;
202 
203  void init ();
204  void install (bool srgb = false, bool premul_alpha = false);
205  void initCustomSFX(const XMLNode *sfx);
206  void initParticlesEffect(const XMLNode *node);
207 
208  // SP usage
209  std::string m_shader_name;
210  std::string m_uv_two_tex;
211  // Full path for textures in sp shader
212  std::string m_sampler_path[6];
213  std::string m_container_id;
214  void loadContainerId();
215 
216 public:
217  Material(const XMLNode *node, bool deprecated);
218  Material(const std::string& fname,
219  bool is_full_path=false,
220  bool complain_if_not_found=true,
221  bool load_texture = true,
222  const std::string& shader_name = "solid");
223  ~Material ();
224 
225  void unloadTexture();
226 
227  void setSFXSpeed(SFXBase *sfx, float speed, bool should_be_paused) const;
228  void setMaterialProperties(video::SMaterial *m, scene::IMeshBuffer* mb);
229 
231  video::ITexture *getTexture(bool srgb = true, bool premul_alpha = false);
232  // ------------------------------------------------------------------------
233  bool isIgnore () const { return m_ignore; }
234  // ------------------------------------------------------------------------
236  bool isZipper () const { return m_zipper; }
237  // ------------------------------------------------------------------------
240  bool isDriveReset () const { return m_drive_reset; }
241  // ------------------------------------------------------------------------
244  bool isColorizable () const { return m_colorizable; }
245  // ------------------------------------------------------------------------
248  float getColorizationFactor () const { return m_colorization_factor; }
249  // ------------------------------------------------------------------------
250  bool hasRandomHue() const { return !m_hue_settings.empty(); }
251  // ------------------------------------------------------------------------
254  float getRandomHue()
255  {
256  if (m_hue_settings.empty())
257  return 0.0f;
258  const unsigned int hue = m_random_hue.get((int)m_hue_settings.size());
259  assert(hue < m_hue_settings.size());
260  return m_hue_settings[hue];
261  }
262  // ------------------------------------------------------------------------
265  CollisionReaction getCollisionReaction() const { return m_collision_reaction; }
266 
267  // ------------------------------------------------------------------------
268  std::string getCrashResetParticles() const { return m_collision_particles; }
269 
270  // ------------------------------------------------------------------------
271  bool highTireAdhesion () const { return m_high_tire_adhesion; }
272  // ------------------------------------------------------------------------
273  const std::string&
274  getTexFname () const { return m_texname; }
275  // ------------------------------------------------------------------------
276  const std::string&
277  getTexFullPath () const { return m_full_path; }
278 
279  // ------------------------------------------------------------------------
280  bool isTransparent () const
281  {
282  return m_shader_name == "additive" || m_shader_name == "alphablend" ||
283  m_shader_name == "displace";
284  }
285 
286  // ------------------------------------------------------------------------
288  float getMaxSpeedFraction() const { return m_max_speed_fraction; }
289  // ------------------------------------------------------------------------
293  int getSlowDownTicks() const { return m_slowdown_ticks; }
294  // ------------------------------------------------------------------------
299  bool isBelowSurface () const { return m_below_surface; }
300  // ------------------------------------------------------------------------
304  bool isSurface () const { return m_surface; }
305  // ------------------------------------------------------------------------
308  const std::string &getSFXName() const { return m_sfx_name; }
309  // ------------------------------------------------------------------------
313  const ParticleKind* getParticlesWhen(ParticleConditions cond) const
314  {
315  return m_particles_effects[cond];
316  } // getParticlesWhen
317  // ------------------------------------------------------------------------
320  bool hasFallingEffect() const {return m_falling_effect; }
321  // ------------------------------------------------------------------------
324  bool isJumpTexture() const { return m_is_jump_texture; }
325  // ------------------------------------------------------------------------
329  bool hasGravity() const { return m_has_gravity; }
330  // ------------------------------------------------------------------------
332  void getZipperParameter(float *zipper_max_speed_increase,
333  float *zipper_duration,
334  float *zipper_speed_gain,
335  float *zipper_fade_out_time,
336  float *zipper_engine_force) const
337  {
338  *zipper_max_speed_increase = m_zipper_max_speed_increase;
339  *zipper_duration = m_zipper_duration;
340  *zipper_speed_gain = m_zipper_speed_gain;
341  *zipper_fade_out_time = m_zipper_fade_out_time;
342  *zipper_engine_force = m_zipper_engine_force;
343  } // getZipperParameter
344  // ------------------------------------------------------------------------
348  float getZipperMinSpeed() const { return m_zipper_min_speed; }
349  // ------------------------------------------------------------------------
351  char getMirrorAxisInReverse() const { return m_mirror_axis_when_reverse; }
352  // ------------------------------------------------------------------------
353  const std::string& getAlphaMask() const { return m_mask; }
354  // ------------------------------------------------------------------------
355  const std::string& getColorizationMask() const
356  { return m_colorization_mask; }
357  // ------------------------------------------------------------------------
358  void setShaderName(const std::string& name) { m_shader_name = name; }
359  // ------------------------------------------------------------------------
360  const std::string& getShaderName() const { return m_shader_name; }
361  // ------------------------------------------------------------------------
362  /* This is used for finding correct material for spm*/
363  const std::string& getUVTwoTexture() const
364  { return m_uv_two_tex; }
365  // ------------------------------------------------------------------------
366  bool use2UV() const { return !m_uv_two_tex.empty(); }
367  // ------------------------------------------------------------------------
368  const std::string& getSamplerPath(unsigned layer) const
369  {
370  assert(layer < 6);
371  return m_sampler_path[layer];
372  }
373  // ------------------------------------------------------------------------
374  /* Return the container id used by texture compression to cache texture in
375  * cache folder with unique name, if no texture compression is used for
376  * this material, then it will always return empty. */
377  const std::string& getContainerId() const
378  {
379  static std::string empty;
380  if (!m_tex_compression)
381  return empty;
382  return m_container_id;
383  }
384 };
385 
386 
387 #endif
388 
389 /* EOF */
390 
bool m_surface
A material that is a surface only, i.e.
Definition: material.hpp:98
bool m_tex_compression
True if this material should use texture compression.
Definition: material.hpp:132
A random number generator.
Definition: random_generator.hpp:33
float m_zipper_duration
Time a zipper stays activated.
Definition: material.hpp:188
float m_zipper_min_speed
Minimum speed on this terrain.
Definition: material.hpp:169
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:175
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:113
bool isDriveReset() const
Returns if this material should trigger a rescue if a kart is driving on it.
Definition: material.hpp:240
RandomGenerator m_random_hue
Random generator for getting pre-defined hue.
Definition: material.hpp:158
bool isColorizable() const
Returns if this material can be colorized.
Definition: material.hpp:244
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:108
float m_sfx_max_pitch
The maximum pitch to be used (at maximum speed).
Definition: material.hpp:177
float m_colorization_factor
Minimum resulting saturation when colorized (from 0 to 1)
Definition: material.hpp:135
CollisionReaction m_collision_reaction
If a kart is rescued when crashing into this surface.
Definition: material.hpp:138
const std::string & getSFXName() const
Returns the name of a special sfx to play while a kart is on this terrain.
Definition: material.hpp:308
float getRandomHue()
Returns a random hue when colorized.
Definition: material.hpp:254
bool isBelowSurface() const
Returns true if this material is under some other mesh and therefore requires another raycast to find...
Definition: material.hpp:299
bool m_colorizable
True if this material can be colorized (like red/blue in team game).
Definition: material.hpp:129
float getMaxSpeedFraction() const
Returns the fraction of maximum speed on this material.
Definition: material.hpp:288
int m_slowdown_ticks
How much the top speed is reduced per second.
Definition: material.hpp:161
bool isJumpTexture() const
Returns if being in the air after this texture should start the jump animation.
Definition: material.hpp:324
int getSlowDownTicks() const
Returns how long it will take for a slowdown to take effect.
Definition: material.hpp:293
unsigned int m_clamp_tex
Texture clamp bitmask.
Definition: material.hpp:152
char getMirrorAxisInReverse() const
True if this texture should have the U coordinates mirrored.
Definition: material.hpp:351
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:92
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:329
std::string m_texname
Name of the texture.
Definition: material.hpp:71
bool isZipper() const
Returns true if this material is a zipper.
Definition: material.hpp:236
video::ITexture * m_texture
Pointer to the texture.
Definition: material.hpp:68
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:185
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:147
bool isSurface() const
Returns true if this material is a surface, i.e.
Definition: material.hpp:304
Definition: material.hpp:46
float m_zipper_fade_out_time
Time it takes for the zipper advantage to fade out.
Definition: material.hpp:195
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:313
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:192
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:101
float getZipperMinSpeed() const
Returns the minimum speed of a kart on this material.
Definition: material.hpp:348
float m_sfx_max_speed
The speed at which the maximum pitch is used.
Definition: material.hpp:173
float m_zipper_engine_force
Additional engine force.
Definition: material.hpp:197
bool m_high_tire_adhesion
True if the material shouldn't be "slippy" at an angle.
Definition: material.hpp:120
bool m_drive_reset
If a kart is rescued when driving on this surface.
Definition: material.hpp:104
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:155
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:77
std::string m_collision_particles
Particles to show on touch.
Definition: material.hpp:141
CollisionReaction getCollisionReaction() const
Returns if this material should trigger a rescue if a kart crashes against it.
Definition: material.hpp:265
bool m_below_surface
Set if being on this surface means being under some other mesh.
Definition: material.hpp:88
float getColorizationFactor() const
Returns the minimum resulting saturation when colorized.
Definition: material.hpp:248
float m_sfx_pitch_per_speed
(max_pitch-min_pitch) / (max_speed - min_speed).
Definition: material.hpp:181
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:332
float m_sfx_min_speed
The minimum speed at which a special sfx is started to be played.
Definition: material.hpp:171
bool m_ignore
If the property should be ignored in the physics.
Definition: material.hpp:117
float m_max_speed_fraction
Maximum speed at which no more slow down occurs.
Definition: material.hpp:164
bool hasFallingEffect() const
Returns true if a kart falling over this kind of material triggers the special falling camera.
Definition: material.hpp:320
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:82