raylib-cpp
C++ object-oriented wrapper library for raylib.
Model.hpp
1#ifndef RAYLIB_CPP_INCLUDE_MODEL_HPP_
2#define RAYLIB_CPP_INCLUDE_MODEL_HPP_
3
4#include <string>
5
6#include "./raylib.hpp"
7#include "./raylib-cpp-utils.hpp"
8#include "./Mesh.hpp"
9#include "./RaylibException.hpp"
10
11namespace raylib {
15class Model : public ::Model {
16 public:
17 Model(const ::Model& model) {
18 set(model);
19 }
20
21 Model(const std::string& fileName) {
22 if (!Load(fileName)) {
23 throw RaylibException("Failed to load Model from filename");
24 }
25 }
26
27 Model(const ::Mesh& mesh) {
28 if (!Load(mesh)) {
29 throw RaylibException("Failed to load Model from Mesh");
30 }
31 }
32
34 Unload();
35 }
36
37 Model(const Model&) = delete;
38
39 Model(Model&& other) {
40 set(other);
41
42 other.bones = nullptr;
43 other.boneCount = 0;
44 other.materials = nullptr;
45 other.materialCount = 0;
46 other.meshes = nullptr;
47 other.meshCount = 0;
48 other.bindPose = nullptr;
49 }
50
51 GETTERSETTER(::Matrix, Transform, transform)
52 GETTERSETTER(int, MeshCount, meshCount)
53 GETTERSETTER(int, MaterialCount, materialCount)
54 GETTERSETTER(::Mesh *, Meshes, meshes)
55 GETTERSETTER(::Material *, Materials, materials)
56 GETTERSETTER(int *, MeshMaterial, meshMaterial)
57 GETTERSETTER(int, BoneCount, boneCount)
58 GETTERSETTER(::BoneInfo *, Bones, bones)
59 GETTERSETTER(::Transform *, BindPoe, bindPose)
60
61 Model& operator=(const ::Model& model) {
62 set(model);
63 return *this;
64 }
65
66 Model& operator=(const Model&) = delete;
67
68 Model& operator=(Model&& other) noexcept {
69 if (this == &other) {
70 return *this;
71 }
72
73 Unload();
74 set(other);
75
76 other.bones = nullptr;
77 other.boneCount = 0;
78 other.materials = nullptr;
79 other.materialCount = 0;
80 other.meshes = nullptr;
81 other.meshCount = 0;
82 other.bindPose = nullptr;
83
84 return *this;
85 }
86
90 inline void Unload() {
91 if (meshes != nullptr || materials != nullptr) {
92 ::UnloadModel(*this);
93 meshes = nullptr;
94 materials = nullptr;
95 }
96 }
97
102 ::UnloadModelKeepMeshes(*this);
103 return *this;
104 }
105
109 inline Model& SetMeshMaterial(int meshId, int materialId) {
110 ::SetModelMeshMaterial(this, meshId, materialId);
111 return *this;
112 }
113
117 inline RayCollision GetCollision(const ::Ray& ray) const {
118 return ::GetRayCollisionModel(ray, *this);
119 }
120
124 inline Model& UpdateAnimation(const ::ModelAnimation& anim, int frame) {
125 ::UpdateModelAnimation(*this, anim, frame);
126 return *this;
127 }
128
132 inline bool IsModelAnimationValid(const ::ModelAnimation& anim) const {
133 return ::IsModelAnimationValid(*this, anim);
134 }
135
139 inline Model& Draw(::Vector3 position,
140 float scale = 1.0f,
141 ::Color tint = {255, 255, 255, 255}) {
142 ::DrawModel(*this, position, scale, tint);
143 return *this;
144 }
145
149 inline Model& Draw(
150 ::Vector3 position,
151 ::Vector3 rotationAxis,
152 float rotationAngle = 0.0f,
153 ::Vector3 scale = {1.0f, 1.0f, 1.0f},
154 ::Color tint = {255, 255, 255, 255}) {
155 ::DrawModelEx(*this, position, rotationAxis, rotationAngle, scale, tint);
156 return *this;
157 }
158
162 inline Model& DrawWires(::Vector3 position,
163 float scale = 1.0f,
164 ::Color tint = {255, 255, 255, 255}) {
165 ::DrawModelWires(*this, position, scale, tint);
166 return *this;
167 }
168
173 ::Vector3 position,
174 ::Vector3 rotationAxis,
175 float rotationAngle = 0.0f,
176 ::Vector3 scale = {1.0f, 1.0f, 1.0f},
177 ::Color tint = {255, 255, 255, 255}) {
178 ::DrawModelWiresEx(*this, position, rotationAxis, rotationAngle, scale, tint);
179 return *this;
180 }
181
186 return ::GetModelBoundingBox(*this);
187 }
188
192 operator BoundingBox() const {
193 return ::GetModelBoundingBox(*this);
194 }
195
199 bool IsReady() const {
200 return meshCount > 0 || materialCount > 0 || boneCount > 0;
201 }
202
208 bool Load(const std::string& fileName) {
209 set(::LoadModel(fileName.c_str()));
210 return IsReady();
211 }
212
218 bool Load(const ::Mesh& mesh) {
219 set(::LoadModelFromMesh(mesh));
220 return IsReady();
221 }
222
223 private:
224 inline void set(const ::Model& model) {
225 transform = model.transform;
226
227 meshCount = model.meshCount;
228 materialCount = model.materialCount;
229 meshes = model.meshes;
230 materials = model.materials;
231 meshMaterial = model.meshMaterial;
232
233 boneCount = model.boneCount;
234 bones = model.bones;
235 bindPose = model.bindPose;
236 }
237};
238
239} // namespace raylib
240
241#endif // RAYLIB_CPP_INCLUDE_MODEL_HPP_
Bounding box type.
Definition: BoundingBox.hpp:11
Material type (generic)
Definition: Material.hpp:14
Vertex data definning a mesh.
Definition: Mesh.hpp:16
Model type.
Definition: Model.hpp:15
bool IsReady() const
Determines whether or not the Model has data in it.
Definition: Model.hpp:199
Model & operator=(const Model &)=delete
void Unload()
Unload model (including meshes) from memory (RAM and/or VRAM)
Definition: Model.hpp:90
bool IsModelAnimationValid(const ::ModelAnimation &anim) const
Check model animation skeleton match.
Definition: Model.hpp:132
Model & SetMeshMaterial(int meshId, int materialId)
Set material for a mesh.
Definition: Model.hpp:109
Model & Draw(::Vector3 position, ::Vector3 rotationAxis, float rotationAngle=0.0f, ::Vector3 scale={1.0f, 1.0f, 1.0f}, ::Color tint={255, 255, 255, 255})
Draw a model with extended parameters.
Definition: Model.hpp:149
Model & DrawWires(::Vector3 position, ::Vector3 rotationAxis, float rotationAngle=0.0f, ::Vector3 scale={1.0f, 1.0f, 1.0f}, ::Color tint={255, 255, 255, 255})
Draw a model wires (with texture if set) with extended parameters.
Definition: Model.hpp:172
Model(const ::Model &model)
Definition: Model.hpp:17
Model(const ::Mesh &mesh)
Definition: Model.hpp:27
Model(Model &&other)
Definition: Model.hpp:39
Model(const std::string &fileName)
Definition: Model.hpp:21
Model(const Model &)=delete
bool Load(const ::Mesh &mesh)
Loads a Model from the given Mesh.
Definition: Model.hpp:218
Model & UpdateAnimation(const ::ModelAnimation &anim, int frame)
Update model animation pose.
Definition: Model.hpp:124
bool Load(const std::string &fileName)
Loads a Model from the given file.
Definition: Model.hpp:208
Model & Draw(::Vector3 position, float scale=1.0f, ::Color tint={255, 255, 255, 255})
Draw a model (with texture if set)
Definition: Model.hpp:139
RayCollision GetCollision(const ::Ray &ray) const
Get collision info between ray and model.
Definition: Model.hpp:117
Model & UnloadKeepMeshes()
Unload model (but not meshes) from memory (RAM and/or VRAM)
Definition: Model.hpp:101
Model & DrawWires(::Vector3 position, float scale=1.0f, ::Color tint={255, 255, 255, 255})
Draw a model wires (with texture if set)
Definition: Model.hpp:162
Model & operator=(Model &&other) noexcept
Definition: Model.hpp:68
BoundingBox GetBoundingBox() const
Compute model bounding box limits (considers all meshes)
Definition: Model.hpp:185
Raycast hit information.
Exception used for most raylib-related exceptions.
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8