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-cpp-utils.hpp"
10#include "./raylib.hpp"
11
12namespace raylib {
16class Material : public ::Material {
17public:
18 Material(const ::Material& material) : ::Material(material) { }
19
23 Material() : ::Material(LoadMaterialDefault()) { }
24
25 Material(const Material&) = delete;
26
27 Material(Material&& other) noexcept {
28 set(other);
29
30 other.maps = nullptr;
31 other.shader = {};
32 other.params[0] = 0.0f;
33 other.params[1] = 0.0f;
34 other.params[2] = 0.0f;
35 other.params[3] = 0.0f;
36 }
37
38 ~Material() { Unload(); }
39
43 static std::vector<Material> Load(const std::string_view fileName) {
44 int count = 0;
45 // TODO(RobLoach): Material::Load() possibly leaks the materials array.
46 ::Material* materials = ::LoadMaterials(fileName.data(), &count);
47 return std::vector<Material>(materials, materials + count);
48 }
49
50 GETTERSETTER(::Shader, Shader, shader)
51 GETTERSETTER(::MaterialMap*, Maps, maps)
52 // TODO(RobLoach): Resolve the Material params being a float[4].
53 // GETTERSETTER(float[4], Params, params)
54
55 Material& operator=(const ::Material& material) {
56 set(material);
57 return *this;
58 }
59
60 Material& operator=(const Material&) = delete;
61
62 Material& operator=(Material&& other) noexcept {
63 if (this == &other) {
64 return *this;
65 }
66
67 Unload();
68 set(other);
69
70 other.maps = nullptr;
71 other.shader = {};
72
73 return *this;
74 }
75
79 void Unload() {
80 if (maps != nullptr) {
81 ::UnloadMaterial(*this);
82 maps = nullptr;
83 }
84 }
85
89 Material& SetTexture(int mapType, const ::Texture2D& texture) {
90 ::SetMaterialTexture(this, mapType, texture);
91 return *this;
92 }
93
97 void DrawMesh(const ::Mesh& mesh, ::Matrix transform) const { ::DrawMesh(mesh, *this, transform); }
98
102 void DrawMesh(const ::Mesh& mesh, ::Matrix* transforms, int instances) const {
103 ::DrawMeshInstanced(mesh, *this, transforms, instances);
104 }
105
109 inline void DrawMesh(const ::Mesh& mesh, std::span<::Matrix> transforms) const {
110 ::DrawMeshInstanced(mesh, *this, transforms.data(), static_cast<int>(transforms.size()));
111 }
112
116 [[nodiscard]] bool IsValid() const { return ::IsMaterialValid(*this); }
117protected:
118 void set(const ::Material& material) {
119 shader = material.shader;
120 maps = material.maps;
121 params[0] = material.params[0];
122 params[1] = material.params[1];
123 params[2] = material.params[2];
124 params[3] = material.params[3];
125 }
126};
127} // namespace raylib
128
130
131#endif // RAYLIB_CPP_INCLUDE_MATERIAL_HPP_
Material type (generic)
Definition: Material.hpp:16
void DrawMesh(const ::Mesh &mesh, std::span<::Matrix > transforms) const
Draw multiple mesh instances with material and different transforms.
Definition: Material.hpp:109
bool IsValid() const
Check if material is ready.
Definition: Material.hpp:116
Material & SetTexture(int mapType, const ::Texture2D &texture)
Set texture for a material map type (MAP_DIFFUSE, MAP_SPECULAR...)
Definition: Material.hpp:89
void Unload()
Unload material from memory.
Definition: Material.hpp:79
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:102
void DrawMesh(const ::Mesh &mesh, ::Matrix transform) const
Draw a 3d mesh with material and transform.
Definition: Material.hpp:97
static std::vector< Material > Load(const std::string_view fileName)
Load materials from model file.
Definition: Material.hpp:43
Shader type (generic)
Definition: Shader.hpp:17
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8