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 
11 namespace raylib {
15 class Model : public ::Model {
16  public:
17  Model() {
18  // Nothing.
19  }
20 
21  /*
22  * Copy a model from another model.
23  */
24  Model(const ::Model& model) {
25  set(model);
26  }
27 
28  /*
29  * Load a model from a file.
30  *
31  * @throws raylib::RaylibException Throws if failed to load the Modal.
32  */
33  Model(const std::string& fileName) {
34  Load(fileName);
35  }
36 
37  /*
38  * Load a model from a mesh.
39  *
40  * @throws raylib::RaylibException Throws if failed to load the Modal.
41  */
42  Model(const ::Mesh& mesh) {
43  Load(mesh);
44  }
45 
46  ~Model() {
47  Unload();
48  }
49 
50  Model(const Model&) = delete;
51 
52  Model(Model&& other) {
53  set(other);
54 
55  other.meshCount = 0;
56  other.materialCount = 0;
57  other.meshes = nullptr;
58  other.materials = nullptr;
59  other.meshMaterial = nullptr;
60  other.boneCount = 0;
61  other.bones = nullptr;
62  other.bindPose = nullptr;
63  }
64 
65  GETTERSETTER(::Matrix, Transform, transform)
66  GETTERSETTER(int, MeshCount, meshCount)
67  GETTERSETTER(int, MaterialCount, materialCount)
68  GETTERSETTER(::Mesh*, Meshes, meshes)
69  GETTERSETTER(::Material*, Materials, materials)
70  GETTERSETTER(int*, MeshMaterial, meshMaterial)
71  GETTERSETTER(int, BoneCount, boneCount)
72  GETTERSETTER(::BoneInfo*, Bones, bones)
73  GETTERSETTER(::Transform*, BindPose, bindPose)
74 
75  Model& operator=(const ::Model& model) {
76  set(model);
77  return *this;
78  }
79 
80  Model& operator=(const Model&) = delete;
81 
82  Model& operator=(Model&& other) noexcept {
83  if (this == &other) {
84  return *this;
85  }
86 
87  Unload();
88  set(other);
89 
90  other.meshCount = 0;
91  other.materialCount = 0;
92  other.meshes = nullptr;
93  other.materials = nullptr;
94  other.meshMaterial = nullptr;
95  other.boneCount = 0;
96  other.bones = nullptr;
97  other.bindPose = nullptr;
98 
99  return *this;
100  }
101 
105  inline void Unload() {
106  if (meshes != nullptr || materials != nullptr) {
107  ::UnloadModel(*this);
108  meshes = nullptr;
109  materials = nullptr;
110  }
111  }
112 
117  ::UnloadModelKeepMeshes(*this);
118  return *this;
119  }
120 
124  inline Model& SetMeshMaterial(int meshId, int materialId) {
125  ::SetModelMeshMaterial(this, meshId, materialId);
126  return *this;
127  }
128 
132  inline Model& UpdateAnimation(const ::ModelAnimation& anim, int frame) {
133  ::UpdateModelAnimation(*this, anim, frame);
134  return *this;
135  }
136 
140  inline bool IsModelAnimationValid(const ::ModelAnimation& anim) const {
141  return ::IsModelAnimationValid(*this, anim);
142  }
143 
147  inline void Draw(::Vector3 position,
148  float scale = 1.0f,
149  ::Color tint = {255, 255, 255, 255}) const {
150  ::DrawModel(*this, position, scale, tint);
151  }
152 
156  inline void Draw(
157  ::Vector3 position,
158  ::Vector3 rotationAxis,
159  float rotationAngle = 0.0f,
160  ::Vector3 scale = {1.0f, 1.0f, 1.0f},
161  ::Color tint = {255, 255, 255, 255}) const {
162  ::DrawModelEx(*this, position, rotationAxis, rotationAngle, scale, tint);
163  }
164 
168  inline void DrawWires(::Vector3 position,
169  float scale = 1.0f,
170  ::Color tint = {255, 255, 255, 255}) const {
171  ::DrawModelWires(*this, position, scale, tint);
172  }
173 
177  inline void DrawWires(
178  ::Vector3 position,
179  ::Vector3 rotationAxis,
180  float rotationAngle = 0.0f,
181  ::Vector3 scale = {1.0f, 1.0f, 1.0f},
182  ::Color tint = {255, 255, 255, 255}) const {
183  ::DrawModelWiresEx(*this, position, rotationAxis, rotationAngle, scale, tint);
184  }
185 
189  inline BoundingBox GetBoundingBox() const {
190  return ::GetModelBoundingBox(*this);
191  }
192 
196  operator BoundingBox() const {
197  return ::GetModelBoundingBox(*this);
198  }
199 
203  bool IsReady() const {
204  return meshCount > 0 || materialCount > 0 || boneCount > 0;
205  }
206 
212  void Load(const std::string& fileName) {
213  set(::LoadModel(fileName.c_str()));
214  if (!IsReady()) {
215  throw RaylibException("Failed to load Model from " + fileName);
216  }
217  }
218 
224  void Load(const ::Mesh& mesh) {
225  set(::LoadModelFromMesh(mesh));
226  if (!IsReady()) {
227  throw RaylibException("Failed to load Model from Mesh");
228  }
229  }
230 
231  private:
232  void set(const ::Model& model) {
233  transform = model.transform;
234 
235  meshCount = model.meshCount;
236  materialCount = model.materialCount;
237  meshes = model.meshes;
238  materials = model.materials;
239  meshMaterial = model.meshMaterial;
240 
241  boneCount = model.boneCount;
242  bones = model.bones;
243  bindPose = model.bindPose;
244  }
245 };
246 
247 } // namespace raylib
248 
249 using RModel = raylib::Model;
250 
251 #endif // RAYLIB_CPP_INCLUDE_MODEL_HPP_
Bounding box type.
Definition: BoundingBox.hpp:11
Color type, RGBA (32bit)
Definition: Color.hpp:14
Material type (generic)
Definition: Material.hpp:14
Matrix type (OpenGL style 4x4 - right handed, column major)
Definition: Matrix.hpp:16
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:203
void Load(const std::string &fileName)
Loads a Model from the given file.
Definition: Model.hpp:212
Model & UnloadKeepMeshes()
Unload model (but not meshes) from memory (RAM and/or VRAM)
Definition: Model.hpp:116
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:147
void Draw(::Vector3 position, ::Vector3 rotationAxis, float 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:156
void Load(const ::Mesh &mesh)
Loads a Model from the given Mesh.
Definition: Model.hpp:224
void Unload()
Unload model (including meshes) from memory (RAM and/or VRAM)
Definition: Model.hpp:105
bool IsModelAnimationValid(const ::ModelAnimation &anim) const
Check model animation skeleton match.
Definition: Model.hpp:140
void DrawWires(::Vector3 position, ::Vector3 rotationAxis, float 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:177
Model & UpdateAnimation(const ::ModelAnimation &anim, int frame)
Update model animation pose.
Definition: Model.hpp:132
Model & SetMeshMaterial(int meshId, int materialId)
Set material for a mesh.
Definition: Model.hpp:124
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:168
BoundingBox GetBoundingBox() const
Compute model bounding box limits (considers all meshes)
Definition: Model.hpp:189
Exception used for most raylib-related exceptions.
Vector3 type.
Definition: Vector3.hpp:16
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8