raylib-cpp
C++ object-oriented wrapper library for raylib.
Material.hpp
1 #ifndef RAYLIB_CPP_INCLUDE_MATERIAL_HPP_
2 #define RAYLIB_CPP_INCLUDE_MATERIAL_HPP_
3 
4 #include <string>
5 #include <vector>
6 
7 #include "./raylib.hpp"
8 #include "./raylib-cpp-utils.hpp"
9 
10 namespace raylib {
14 class Material : public ::Material {
15  public:
16  Material(const ::Material& material) {
17  set(material);
18  }
19 
24  set(LoadMaterialDefault());
25  }
26 
27  Material(const Material&) = delete;
28 
29  Material(Material&& other) {
30  set(other);
31 
32  other.maps = nullptr;
33  other.shader = {};
34  other.params[0] = 0.0f;
35  other.params[1] = 0.0f;
36  other.params[2] = 0.0f;
37  other.params[3] = 0.0f;
38  }
39 
40  ~Material() {
41  Unload();
42  }
43 
47  static std::vector<Material> Load(const std::string& fileName) {
48  int count = 0;
49  // TODO(RobLoach): Material::Load() possibly leaks the materials array.
50  ::Material* materials = ::LoadMaterials(fileName.c_str(), &count);
51  return std::vector<Material>(materials, materials + count);
52  }
53 
54  GETTERSETTER(::Shader, Shader, shader)
55  GETTERSETTER(::MaterialMap*, Maps, maps)
56  // TODO(RobLoach): Resolve the Material params being a float[4].
57  // GETTERSETTER(float[4], Params, params)
58 
59  Material& operator=(const ::Material& material) {
60  set(material);
61  return *this;
62  }
63 
64  Material& operator=(const Material&) = delete;
65 
66  Material& operator=(Material&& other) noexcept {
67  if (this == &other) {
68  return *this;
69  }
70 
71  Unload();
72  set(other);
73 
74  other.maps = nullptr;
75  other.shader = {};
76 
77  return *this;
78  }
79 
83  inline void Unload() {
84  if (maps != nullptr) {
85  ::UnloadMaterial(*this);
86  maps = nullptr;
87  }
88  }
89 
93  inline Material& SetTexture(int mapType, const ::Texture2D& texture) {
94  ::SetMaterialTexture(this, mapType, texture);
95  return *this;
96  }
97 
101  inline void DrawMesh(const ::Mesh& mesh, ::Matrix transform) const {
102  ::DrawMesh(mesh, *this, transform);
103  }
104 
108  inline void DrawMesh(const ::Mesh& mesh, ::Matrix* transforms, int instances) const {
109  ::DrawMeshInstanced(mesh, *this, transforms, instances);
110  }
111 
112  private:
113  void set(const ::Material& material) {
114  shader = material.shader;
115  maps = material.maps;
116  params[0] = material.params[0];
117  params[1] = material.params[1];
118  params[2] = material.params[2];
119  params[3] = material.params[3];
120  }
121 };
122 } // namespace raylib
123 
125 
126 #endif // RAYLIB_CPP_INCLUDE_MATERIAL_HPP_
Material type (generic)
Definition: Material.hpp:14
Material & SetTexture(int mapType, const ::Texture2D &texture)
Set texture for a material map type (MAP_DIFFUSE, MAP_SPECULAR...)
Definition: Material.hpp:93
void Unload()
Unload material from memory.
Definition: Material.hpp:83
Material()
Load default material (Supports: DIFFUSE, SPECULAR, NORMAL maps)
Definition: Material.hpp:23
void DrawMesh(const ::Mesh &mesh, ::Matrix *transforms, int instances) const
Draw multiple mesh instances with material and different transforms.
Definition: Material.hpp:108
void DrawMesh(const ::Mesh &mesh, ::Matrix transform) const
Draw a 3d mesh with material and transform.
Definition: Material.hpp:101
static std::vector< Material > Load(const std::string &fileName)
Load materials from model file.
Definition: Material.hpp:47
Matrix type (OpenGL style 4x4 - right handed, column major)
Definition: Matrix.hpp:16
Shader type (generic)
Definition: Shader.hpp:14
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8