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 Model& Draw(::Vector3 position,
148  float scale = 1.0f,
149  ::Color tint = {255, 255, 255, 255}) {
150  ::DrawModel(*this, position, scale, tint);
151  return *this;
152  }
153 
157  inline Model& Draw(
158  ::Vector3 position,
159  ::Vector3 rotationAxis,
160  float rotationAngle = 0.0f,
161  ::Vector3 scale = {1.0f, 1.0f, 1.0f},
162  ::Color tint = {255, 255, 255, 255}) {
163  ::DrawModelEx(*this, position, rotationAxis, rotationAngle, scale, tint);
164  return *this;
165  }
166 
170  inline Model& DrawWires(::Vector3 position,
171  float scale = 1.0f,
172  ::Color tint = {255, 255, 255, 255}) {
173  ::DrawModelWires(*this, position, scale, tint);
174  return *this;
175  }
176 
180  inline Model& DrawWires(
181  ::Vector3 position,
182  ::Vector3 rotationAxis,
183  float rotationAngle = 0.0f,
184  ::Vector3 scale = {1.0f, 1.0f, 1.0f},
185  ::Color tint = {255, 255, 255, 255}) {
186  ::DrawModelWiresEx(*this, position, rotationAxis, rotationAngle, scale, tint);
187  return *this;
188  }
189 
193  inline BoundingBox GetBoundingBox() const {
194  return ::GetModelBoundingBox(*this);
195  }
196 
200  operator BoundingBox() const {
201  return ::GetModelBoundingBox(*this);
202  }
203 
207  bool IsReady() const {
208  return meshCount > 0 || materialCount > 0 || boneCount > 0;
209  }
210 
216  void Load(const std::string& fileName) {
217  set(::LoadModel(fileName.c_str()));
218  if (!IsReady()) {
219  throw RaylibException("Failed to load Model from " + fileName);
220  }
221  }
222 
228  void Load(const ::Mesh& mesh) {
229  set(::LoadModelFromMesh(mesh));
230  if (!IsReady()) {
231  throw RaylibException("Failed to load Model from Mesh");
232  }
233  }
234 
235  private:
236  void set(const ::Model& model) {
237  transform = model.transform;
238 
239  meshCount = model.meshCount;
240  materialCount = model.materialCount;
241  meshes = model.meshes;
242  materials = model.materials;
243  meshMaterial = model.meshMaterial;
244 
245  boneCount = model.boneCount;
246  bones = model.bones;
247  bindPose = model.bindPose;
248  }
249 };
250 
251 } // namespace raylib
252 
253 using RModel = raylib::Model;
254 
255 #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:207
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:170
void Load(const std::string &fileName)
Loads a Model from the given file.
Definition: Model.hpp:216
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:157
Model & UnloadKeepMeshes()
Unload model (but not meshes) from memory (RAM and/or VRAM)
Definition: Model.hpp:116
void Load(const ::Mesh &mesh)
Loads a Model from the given Mesh.
Definition: Model.hpp:228
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
Model & UpdateAnimation(const ::ModelAnimation &anim, int frame)
Update model animation pose.
Definition: Model.hpp:132
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:180
Model & Draw(::Vector3 position, float scale=1.0f, ::Color tint={255, 255, 255, 255})
Draw a model (with texture if set)
Definition: Model.hpp:147
Model & SetMeshMaterial(int meshId, int materialId)
Set material for a mesh.
Definition: Model.hpp:124
BoundingBox GetBoundingBox() const
Compute model bounding box limits (considers all meshes)
Definition: Model.hpp:193
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