Simulant  21.12-194
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
material_property_overrider.h
1 #pragma once
2 
3 #include <unordered_map>
4 #include <list>
5 #include <string>
6 
7 #include "core_material.h"
8 
9 namespace smlt {
10 
11 /* All materials and passes inherit the properties and
12  * values of the core material. Overriders allow two things:
13  *
14  * 1. Overriding the value of the core material
15  * 2. Adding additional property values (e.g. shader uniforms)
16  */
17 
18 
20 public:
21  MaterialPropertyOverrider() = default;
23  parent_(parent) {}
24 
25  void set_property_value(const char* name, const bool& value);
26  void set_property_value(const char* name, const float& value);
27  void set_property_value(const char* name, const int32_t& value);
28  void set_property_value(const char* name, const Colour& value);
29  void set_property_value(const char* name, const Vec4& value);
30  void set_property_value(const char* name, const Vec3& value);
31  void set_property_value(const char* name, const Vec2& value);
32  void set_property_value(const char* name, const Mat3& value);
33  void set_property_value(const char* name, const Mat4& value);
34  void set_property_value(const char* name, const TexturePtr& value);
35 
36  bool property_value(const MaterialPropertyNameHash hsh, const bool*& out) const;
37  bool property_value(const MaterialPropertyNameHash hsh, const float*& out) const;
38  bool property_value(const MaterialPropertyNameHash hsh, const int32_t *&out) const;
39 
40  bool property_value(const MaterialPropertyNameHash hsh, const Colour*& out) const;
41  bool property_value(const MaterialPropertyNameHash hsh, const Vec2*& out) const;
42  bool property_value(const MaterialPropertyNameHash hsh, const Vec3*& out) const;
43 
44  bool property_value(const MaterialPropertyNameHash hsh, const Vec4*& out) const;
45 
46  bool property_value(const MaterialPropertyNameHash hsh, const Mat3*& out) const;
47  bool property_value(const MaterialPropertyNameHash hsh, const Mat4*& out) const;
48  bool property_value(const MaterialPropertyNameHash hsh, const TexturePtr*& out) const;
49 
50  /* Helpers for std::string */
51  template<typename T>
52  void set_property_value(const std::string& str, const T& v) {
53  set_property_value(str.c_str(), v);
54  }
55 
56  template<typename T>
57  bool property_value(const std::string& str, const T*& out) const {
58  return property_value(str.c_str(), out);
59  }
60 
61  template<typename T>
62  bool property_value(const char* name, const T*& out) const {
63  auto hsh = material_property_hash(name);
64  return property_value(hsh, out);
65  }
66 
67  bool clear_override(const char* name) {
68  return clear_override(material_property_hash(name));
69  }
70 
71  bool property_type(const char* property_name, MaterialPropertyType* type) const;
72 
73  template<typename T>
74  struct PropertyValue {
75  MaterialPropertyNameHash hsh;
76  T value;
77 
78  PropertyValue() = default;
79  PropertyValue(MaterialPropertyNameHash hsh, T value):
80  hsh(hsh), value(value) {}
81  };
82 protected:
83  virtual void on_override(
84  MaterialPropertyNameHash hsh,
85  const char* name,
86  MaterialPropertyType type
87  ) {
88  _S_UNUSED(hsh);
89  _S_UNUSED(name);
90  _S_UNUSED(type);
91  }
92 
93  virtual void on_clear_override(MaterialPropertyNameHash hsh) { _S_UNUSED(hsh); }
94 
95  /* If we have a parent, then we can't override unless the property has
96  * been defined on the parent - or it's a core property */
97  bool check_existance(const MaterialPropertyNameHash hsh) const;
98  bool check_existance(const char* property_name) const;
99  bool clear_override(const unsigned hsh);
100 
101  const MaterialPropertyOverrider* parent_ = nullptr;
102 
103  std::unordered_map<MaterialPropertyNameHash, MaterialPropertyType> all_overrides_;
104 
105  std::vector<PropertyValue<int32_t>> int_properties_;
106  std::vector<PropertyValue<float>> float_properties_;
107  std::vector<PropertyValue<bool>> bool_properties_;
108  std::vector<PropertyValue<Vec2>> vec2_properties_;
109  std::vector<PropertyValue<Vec3>> vec3_properties_;
110  std::vector<PropertyValue<Vec4>> vec4_properties_;
111  std::vector<PropertyValue<Mat3>> mat3_properties_;
112  std::vector<PropertyValue<Mat4>> mat4_properties_;
113  std::vector<PropertyValue<TexturePtr>> texture_properties_;
114 };
115 
116 }
smlt::Mat4
Definition: mat4.h:25
smlt::Vec3
Definition: vec3.h:23
smlt::Mat3
Definition: mat3.h:13
smlt
Definition: animation.cpp:25
smlt::MaterialPropertyOverrider
Definition: material_property_overrider.h:19
smlt::Colour
Definition: colour.h:29
smlt::Vec4
Definition: vec4.h:11
smlt::Vec2
Definition: vec2.h:13
smlt::MaterialPropertyOverrider::PropertyValue
Definition: material_property_overrider.h:74