raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
Model.hpp
1#ifndef RAYLIB_CPP_INCLUDE_MODEL_HPP_
2#define RAYLIB_CPP_INCLUDE_MODEL_HPP_
3
4#include <string>
5#include <string_view>
6
7#include "./raylib.hpp"
8#include "./raylib-cpp-utils.hpp"
9#include "./Vector3.hpp"
10#include "./BoundingBox.hpp"
11#include "./RaylibException.hpp"
12#include "./RadiansDegrees.hpp"
13
14namespace raylib {
15class Mesh;
19class Model : public ::Model {
20 public:
21 Model() {
22 // Nothing.
23 meshes = nullptr;
24 materials = nullptr;
25 }
26
27 /*
28 * Copy a model from another model.
29 */
30 Model(const ::Model& model) {
31 set(model);
32 }
33
34 /*
35 * Load a model from a file.
36 *
37 * @throws raylib::RaylibException Throws if failed to load the Modal.
38 */
39 Model(const std::string_view fileName) {
40 Load(fileName);
41 }
42
43 /*
44 * Load a model from a mesh.
45 *
46 * @throws raylib::RaylibException Throws if failed to load the Modal.
47 */
48 Model(const ::Mesh& mesh) {
49 Load(mesh);
50 }
51
59 Model(const raylib::Mesh& mesh) = delete;
60
61 ~Model() {
62 Unload();
63 }
64
65 Model(const Model&) = delete;
66
67 Model(Model&& other) {
68 set(other);
69
70 other.meshCount = 0;
71 other.materialCount = 0;
72 other.meshes = nullptr;
73 other.materials = nullptr;
74 other.meshMaterial = nullptr;
75 other.boneCount = 0;
76 other.bones = nullptr;
77 other.bindPose = nullptr;
78 }
79
80 GETTERSETTER(::Matrix, Transform, transform)
81 GETTERSETTER(int, MeshCount, meshCount)
82 GETTERSETTER(int, MaterialCount, materialCount)
83 GETTERSETTER(::Mesh*, Meshes, meshes)
84 GETTERSETTER(::Material*, Materials, materials)
85 GETTERSETTER(int*, MeshMaterial, meshMaterial)
86 GETTERSETTER(int, BoneCount, boneCount)
87 GETTERSETTER(::BoneInfo*, Bones, bones)
88 GETTERSETTER(::Transform*, BindPose, bindPose)
89
90 Model& operator=(const ::Model& model) {
91 set(model);
92 return *this;
93 }
94
95 Model& operator=(const Model&) = delete;
96
97 Model& operator=(Model&& other) noexcept {
98 if (this == &other) {
99 return *this;
100 }
101
102 Unload();
103 set(other);
104
105 other.meshCount = 0;
106 other.materialCount = 0;
107 other.meshes = nullptr;
108 other.materials = nullptr;
109 other.meshMaterial = nullptr;
110 other.boneCount = 0;
111 other.bones = nullptr;
112 other.bindPose = nullptr;
113
114 return *this;
115 }
116
120 void Unload() {
121 if (meshes != nullptr || materials != nullptr) {
122 ::UnloadModel(*this);
123 meshes = nullptr;
124 materials = nullptr;
125 }
126 }
127
131 Model& SetMeshMaterial(int meshId, int materialId) {
132 ::SetModelMeshMaterial(this, meshId, materialId);
133 return *this;
134 }
135
139 Model& UpdateAnimation(const ::ModelAnimation& anim, int frame) {
140 ::UpdateModelAnimation(*this, anim, frame);
141 return *this;
142 }
143
147 bool IsModelAnimationValid(const ::ModelAnimation& anim) const {
148 return ::IsModelAnimationValid(*this, anim);
149 }
150
154 void Draw(::Vector3 position,
155 float scale = 1.0f,
156 ::Color tint = {255, 255, 255, 255}) const {
157 ::DrawModel(*this, position, scale, tint);
158 }
159
163 void Draw(
164 ::Vector3 position,
165 ::Vector3 rotationAxis,
166 Degree rotationAngle = 0.0f,
167 ::Vector3 scale = {1.0f, 1.0f, 1.0f},
168 ::Color tint = {255, 255, 255, 255}) const {
169 ::DrawModelEx(*this, position, rotationAxis, rotationAngle, scale, tint);
170 }
171
175 void DrawWires(::Vector3 position,
176 float scale = 1.0f,
177 ::Color tint = {255, 255, 255, 255}) const {
178 ::DrawModelWires(*this, position, scale, tint);
179 }
180
185 ::Vector3 position,
186 ::Vector3 rotationAxis,
187 Degree rotationAngle = 0.0f,
188 ::Vector3 scale = {1.0f, 1.0f, 1.0f},
189 ::Color tint = {255, 255, 255, 255}) const {
190 ::DrawModelWiresEx(*this, position, rotationAxis, rotationAngle, scale, tint);
191 }
192
197 return ::GetModelBoundingBox(*this);
198 }
199
205
209 operator BoundingBox() const {
210 return GetBoundingBox();
211 }
212
216 bool IsReady() const {
217 return ::IsModelReady(*this);
218 }
219
225 void Load(const std::string_view fileName) {
226 set(::LoadModel(fileName.data()));
227 if (!IsReady()) {
228 throw RaylibException("Failed to load Model from " + std::string(fileName));
229 }
230 }
231
237 void Load(const ::Mesh& mesh) {
238 set(::LoadModelFromMesh(mesh));
239 if (!IsReady()) {
240 throw RaylibException("Failed to load Model from Mesh");
241 }
242 }
243
244 protected:
245 void set(const ::Model& model) {
246 transform = model.transform;
247
248 meshCount = model.meshCount;
249 materialCount = model.materialCount;
250 meshes = model.meshes;
251 materials = model.materials;
252 meshMaterial = model.meshMaterial;
253
254 boneCount = model.boneCount;
255 bones = model.bones;
256 bindPose = model.bindPose;
257 }
258};
259
260} // namespace raylib
261
262using RModel = raylib::Model;
263
264#endif // RAYLIB_CPP_INCLUDE_MODEL_HPP_
Bounding box type.
Definition: BoundingBox.hpp:12
Degree type (allows automatic worry free conversion between radians and degrees)
Material type (generic)
Definition: Material.hpp:16
Matrix type (OpenGL style 4x4 - right handed, column major)
Definition: Matrix.hpp:18
Vertex data defining a mesh.
Definition: Mesh.hpp:23
Model type.
Definition: Model.hpp:19
bool IsReady() const
Determines whether or not the Model has data in it.
Definition: Model.hpp:216
void Draw(::Vector3 position, ::Vector3 rotationAxis, Degree rotationAngle=0.0f, ::Vector3 scale={1.0f, 1.0f, 1.0f}, ::Color tint={255, 255, 255, 255}) const
Draw a model with extended parameters.
Definition: Model.hpp:163
void Draw(::Vector3 position, float scale=1.0f, ::Color tint={255, 255, 255, 255}) const
Draw a model (with texture if set)
Definition: Model.hpp:154
void Load(const ::Mesh &mesh)
Loads a Model from the given Mesh.
Definition: Model.hpp:237
void Unload()
Unload model (including meshes) from memory (RAM and/or VRAM)
Definition: Model.hpp:120
bool IsModelAnimationValid(const ::ModelAnimation &anim) const
Check model animation skeleton match.
Definition: Model.hpp:147
Model & SetMeshMaterial(int meshId, int materialId)
Set material for a mesh.
Definition: Model.hpp:131
BoundingBox GetTransformedBoundingBox() const
Compute model bounding box limits with respect to the Model's transformation (considers all meshes) T...
void Load(const std::string_view fileName)
Loads a Model from the given file.
Definition: Model.hpp:225
void DrawWires(::Vector3 position, ::Vector3 rotationAxis, Degree rotationAngle=0.0f, ::Vector3 scale={1.0f, 1.0f, 1.0f}, ::Color tint={255, 255, 255, 255}) const
Draw a model wires (with texture if set) with extended parameters.
Definition: Model.hpp:184
Model & UpdateAnimation(const ::ModelAnimation &anim, int frame)
Update model animation pose.
Definition: Model.hpp:139
void DrawWires(::Vector3 position, float scale=1.0f, ::Color tint={255, 255, 255, 255}) const
Draw a model wires (with texture if set)
Definition: Model.hpp:175
Model(const raylib::Mesh &mesh)=delete
The Model constructor with a Mesh() is removed.
BoundingBox GetBoundingBox() const
Compute model bounding box limits (considers all meshes)
Definition: Model.hpp:196
Exception used for most raylib-related exceptions.