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 "./RaylibException.hpp"
8#include "./raylib-cpp-utils.hpp"
9#include "./raylib.hpp"
10#include "./raylib-cpp-utils.hpp"
11#include "./Vector3.hpp"
12#include "./BoundingBox.hpp"
13#include "./RaylibException.hpp"
14#include "./RadiansDegrees.hpp"
15
16namespace raylib {
17class Mesh;
21class Model : public ::Model {
22public:
23 Model() {
24 // Nothing.
25 meshes = nullptr;
26 materials = nullptr;
27 }
28
29 /*
30 * Copy a model from another model.
31 */
32 Model(const ::Model& model) : ::Model(model) { }
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) { Load(mesh); }
49
57 Model(const raylib::Mesh& mesh) = delete;
58
59 ~Model() { Unload(); }
60
61 Model(const Model&) = delete;
62
63 Model(Model&& other) noexcept {
64 set(other);
65
66 other.meshCount = 0;
67 other.materialCount = 0;
68 other.meshes = nullptr;
69 other.materials = nullptr;
70 other.meshMaterial = nullptr;
71 other.boneCount = 0;
72 other.bones = nullptr;
73 other.bindPose = nullptr;
74 }
75
76 GETTERSETTER(::Matrix, Transform, transform)
77 GETTERSETTER(int, MeshCount, meshCount)
78 GETTERSETTER(int, MaterialCount, materialCount)
79 GETTERSETTER(::Mesh*, Meshes, meshes)
80 GETTERSETTER(::Material*, Materials, materials)
81 GETTERSETTER(int*, MeshMaterial, meshMaterial)
82 GETTERSETTER(int, BoneCount, boneCount)
83 GETTERSETTER(::BoneInfo*, Bones, bones)
84 GETTERSETTER(::Transform*, BindPose, bindPose)
85
86 Model& operator=(const ::Model& model) {
87 set(model);
88 return *this;
89 }
90
91 Model& operator=(const Model&) = delete;
92
93 Model& operator=(Model&& other) noexcept {
94 if (this == &other) {
95 return *this;
96 }
97
98 Unload();
99 set(other);
100
101 other.meshCount = 0;
102 other.materialCount = 0;
103 other.meshes = nullptr;
104 other.materials = nullptr;
105 other.meshMaterial = nullptr;
106 other.boneCount = 0;
107 other.bones = nullptr;
108 other.bindPose = nullptr;
109
110 return *this;
111 }
112
116 void Unload() {
117 if (meshes != nullptr || materials != nullptr) {
118 ::UnloadModel(*this);
119 meshes = nullptr;
120 materials = nullptr;
121 }
122 }
123
127 Model& SetMeshMaterial(int meshId, int materialId) {
128 ::SetModelMeshMaterial(this, meshId, materialId);
129 return *this;
130 }
131
135 Model& UpdateAnimation(const ::ModelAnimation& anim, int frame) {
136 ::UpdateModelAnimation(*this, anim, frame);
137 return *this;
138 }
139
143 Model& UpdateAnimationBones(const ::ModelAnimation& anim, int frame) {
144 ::UpdateModelAnimationBones(*this, anim, frame);
145 return *this;
146 }
147
151 [[nodiscard]] bool IsModelAnimationValid(const ::ModelAnimation& anim) const { return ::IsModelAnimationValid(*this, anim); }
152
156 void Draw(::Vector3 position, float scale = 1.0f, ::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, float scale = 1.0f, ::Color tint = {255, 255, 255, 255}) const {
176 ::DrawModelWires(*this, position, scale, tint);
177 }
178
183 ::Vector3 position,
184 ::Vector3 rotationAxis,
185 Degree rotationAngle = 0.0f,
186 ::Vector3 scale = {1.0f, 1.0f, 1.0f},
187 ::Color tint = {255, 255, 255, 255}) const {
188 ::DrawModelWiresEx(*this, position, rotationAxis, rotationAngle, scale, tint);
189 }
190
194 void DrawPoints(::Vector3 position, float scale = 1.0f, ::Color tint = {255, 255, 255, 255}) {
195 ::DrawModelPoints(*this, position, scale, tint);
196 }
197
202 BoundingBox GetTransformedBoundingBox() const;
203
207 operator BoundingBox() const {
208 return GetBoundingBox();
209 }
210
214 [[nodiscard]] BoundingBox GetBoundingBox() const { return ::GetModelBoundingBox(*this); }
215
219 [[nodiscard]] bool IsValid() const { return ::IsModelValid(*this); }
220
226 void Load(const std::string_view fileName) {
227 set(::LoadModel(fileName.data()));
228 if (!IsValid()) {
229 throw RaylibException("Failed to load Model from " + std::string(fileName));
230 }
231 }
232
238 void Load(const ::Mesh& mesh) {
239 set(::LoadModelFromMesh(mesh));
240 if (!IsValid()) {
241 throw RaylibException("Failed to load Model from Mesh");
242 }
243 }
244protected:
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:11
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:21
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:156
void DrawPoints(::Vector3 position, float scale=1.0f, ::Color tint={255, 255, 255, 255})
Draw a model as points.
Definition: Model.hpp:194
void Load(const ::Mesh &mesh)
Loads a Model from the given Mesh.
Definition: Model.hpp:238
void Unload()
Unload model (including meshes) from memory (RAM and/or VRAM)
Definition: Model.hpp:116
bool IsModelAnimationValid(const ::ModelAnimation &anim) const
Check model animation skeleton match.
Definition: Model.hpp:151
Model & SetMeshMaterial(int meshId, int materialId)
Set material for a mesh.
Definition: Model.hpp:127
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:226
Model & UpdateAnimationBones(const ::ModelAnimation &anim, int frame)
Update model animation pose.
Definition: Model.hpp:143
bool IsValid() const
Determines whether or not the Model has data in it.
Definition: Model.hpp:219
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:182
Model & UpdateAnimation(const ::ModelAnimation &anim, int frame)
Update model animation pose.
Definition: Model.hpp:135
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:214
Exception used for most raylib-related exceptions.
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8