-
Notifications
You must be signed in to change notification settings - Fork 86
/
Material.hpp
122 lines (99 loc) · 3.05 KB
/
Material.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifndef RAYLIB_CPP_INCLUDE_MATERIAL_HPP_
#define RAYLIB_CPP_INCLUDE_MATERIAL_HPP_
#include <string>
#include <vector>
#include "./raylib-cpp-utils.hpp"
#include "./raylib.hpp"
namespace raylib {
/**
* Material type (generic)
*/
class Material : public ::Material {
public:
Material(const ::Material& material) { set(material); }
/**
* Load default material (Supports: DIFFUSE, SPECULAR, NORMAL maps)
*/
Material() { set(LoadMaterialDefault()); }
Material(const Material&) = delete;
Material(Material&& other) {
set(other);
other.maps = nullptr;
other.shader = {};
other.params[0] = 0.0f;
other.params[1] = 0.0f;
other.params[2] = 0.0f;
other.params[3] = 0.0f;
}
~Material() { Unload(); }
/**
* Load materials from model file
*/
static std::vector<Material> Load(const std::string& fileName) {
int count = 0;
// TODO(RobLoach): Material::Load() possibly leaks the materials array.
::Material* materials = ::LoadMaterials(fileName.c_str(), &count);
return std::vector<Material>(materials, materials + count);
}
GETTERSETTER(::Shader, Shader, shader)
GETTERSETTER(::MaterialMap*, Maps, maps)
// TODO(RobLoach): Resolve the Material params being a float[4].
// GETTERSETTER(float[4], Params, params)
Material& operator=(const ::Material& material) {
set(material);
return *this;
}
Material& operator=(const Material&) = delete;
Material& operator=(Material&& other) noexcept {
if (this == &other) {
return *this;
}
Unload();
set(other);
other.maps = nullptr;
other.shader = {};
return *this;
}
/**
* Unload material from memory
*/
void Unload() {
if (maps != nullptr) {
::UnloadMaterial(*this);
maps = nullptr;
}
}
/**
* Set texture for a material map type (MAP_DIFFUSE, MAP_SPECULAR...)
*/
Material& SetTexture(int mapType, const ::Texture2D& texture) {
::SetMaterialTexture(this, mapType, texture);
return *this;
}
/**
* Draw a 3d mesh with material and transform
*/
void DrawMesh(const ::Mesh& mesh, ::Matrix transform) const { ::DrawMesh(mesh, *this, transform); }
/**
* Draw multiple mesh instances with material and different transforms
*/
void DrawMesh(const ::Mesh& mesh, ::Matrix* transforms, int instances) const {
::DrawMeshInstanced(mesh, *this, transforms, instances);
}
/**
* Check if material is ready
*/
bool IsReady() const { return ::IsMaterialReady(*this); }
protected:
void set(const ::Material& material) {
shader = material.shader;
maps = material.maps;
params[0] = material.params[0];
params[1] = material.params[1];
params[2] = material.params[2];
params[3] = material.params[3];
}
};
} // namespace raylib
using RMaterial = raylib::Material;
#endif // RAYLIB_CPP_INCLUDE_MATERIAL_HPP_