raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
Material.hpp
1#ifndef RAYLIB_CPP_INCLUDE_MATERIAL_HPP_
2#define RAYLIB_CPP_INCLUDE_MATERIAL_HPP_
3
4#include <span>
5#include <string>
6#include <string_view>
7#include <vector>
8
9#include "./raylib.hpp"
10#include "./raylib-cpp-utils.hpp"
11
12namespace raylib {
16class Material : public ::Material {
17 public:
18 Material(const ::Material& material) {
19 set(material);
20 }
21
26 set(LoadMaterialDefault());
27 }
28
29 Material(const Material&) = delete;
30
31 Material(Material&& other) {
32 set(other);
33
34 other.maps = nullptr;
35 other.shader = {};
36 other.params[0] = 0.0f;
37 other.params[1] = 0.0f;
38 other.params[2] = 0.0f;
39 other.params[3] = 0.0f;
40 }
41
42 ~Material() {
43 Unload();
44 }
45
49 static std::vector<Material> Load(const std::string_view fileName) {
50 int count = 0;
51 // TODO(RobLoach): Material::Load() possibly leaks the materials array.
52 ::Material* materials = ::LoadMaterials(fileName.data(), &count);
53 return std::vector<Material>(materials, materials + count);
54 }
55
56 GETTERSETTER(::Shader, Shader, shader)
57 GETTERSETTER(::MaterialMap*, Maps, maps)
58 // TODO(RobLoach): Resolve the Material params being a float[4].
59 // GETTERSETTER(float[4], Params, params)
60
61 Material& operator=(const ::Material& material) {
62 set(material);
63 return *this;
64 }
65
66 Material& operator=(const Material&) = delete;
67
68 Material& operator=(Material&& other) noexcept {
69 if (this == &other) {
70 return *this;
71 }
72
73 Unload();
74 set(other);
75
76 other.maps = nullptr;
77 other.shader = {};
78
79 return *this;
80 }
81
85 void Unload() {
86 if (maps != nullptr) {
87 ::UnloadMaterial(*this);
88 maps = nullptr;
89 }
90 }
91
95 Material& SetTexture(int mapType, const ::Texture2D& texture) {
96 ::SetMaterialTexture(this, mapType, texture);
97 return *this;
98 }
99
103 void DrawMesh(const ::Mesh& mesh, ::Matrix transform) const {
104 ::DrawMesh(mesh, *this, transform);
105 }
106
110 void DrawMesh(const ::Mesh& mesh, ::Matrix* transforms, int instances) const {
111 ::DrawMeshInstanced(mesh, *this, transforms, instances);
112 }
113
117 inline void DrawMesh(const ::Mesh& mesh, std::span<::Matrix> transforms) const {
118 ::DrawMeshInstanced(mesh, *this, transforms.data(), static_cast<int>(transforms.size()));
119 }
120
124 bool IsReady() const {
125 return ::IsMaterialReady(*this);
126 }
127
128 protected:
129 void set(const ::Material& material) {
130 shader = material.shader;
131 maps = material.maps;
132 params[0] = material.params[0];
133 params[1] = material.params[1];
134 params[2] = material.params[2];
135 params[3] = material.params[3];
136 }
137};
138} // namespace raylib
139
141
142#endif // RAYLIB_CPP_INCLUDE_MATERIAL_HPP_
Material type (generic)
Definition: Material.hpp:16
bool IsReady() const
Check if material is ready.
Definition: Material.hpp:124
void DrawMesh(const ::Mesh &mesh, std::span<::Matrix > transforms) const
Draw multiple mesh instances with material and different transforms.
Definition: Material.hpp:117
Material & SetTexture(int mapType, const ::Texture2D &texture)
Set texture for a material map type (MAP_DIFFUSE, MAP_SPECULAR...)
Definition: Material.hpp:95
void Unload()
Unload material from memory.
Definition: Material.hpp:85
Material()
Load default material (Supports: DIFFUSE, SPECULAR, NORMAL maps)
Definition: Material.hpp:25
void DrawMesh(const ::Mesh &mesh, ::Matrix *transforms, int instances) const
Draw multiple mesh instances with material and different transforms.
Definition: Material.hpp:110
void DrawMesh(const ::Mesh &mesh, ::Matrix transform) const
Draw a 3d mesh with material and transform.
Definition: Material.hpp:103
static std::vector< Material > Load(const std::string_view fileName)
Load materials from model file.
Definition: Material.hpp:49
Shader type (generic)
Definition: Shader.hpp:16