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  Model(const std::string& fileName) {
32  Load(fileName);
33  }
34 
35  /*
36  * Load a model from a mesh.
37  */
38  Model(const ::Mesh& mesh) {
39  Load(mesh);
40  }
41 
42  ~Model() {
43  Unload();
44  }
45 
46  Model(const Model&) = delete;
47 
48  Model(Model&& other) {
49  set(other);
50 
51  other.materials = nullptr;
52  other.materialCount = 0;
53  other.meshes = nullptr;
54  other.meshCount = 0;
55  other.bones = nullptr;
56  other.boneCount = 0;
57  other.bindPose = nullptr;
58  }
59 
60  GETTERSETTER(::Matrix, Transform, transform)
61  GETTERSETTER(int, MeshCount, meshCount)
62  GETTERSETTER(int, MaterialCount, materialCount)
63  GETTERSETTER(::Mesh*, Meshes, meshes)
64  GETTERSETTER(::Material*, Materials, materials)
65  GETTERSETTER(int*, MeshMaterial, meshMaterial)
66  GETTERSETTER(int, BoneCount, boneCount)
67  GETTERSETTER(::BoneInfo*, Bones, bones)
68  GETTERSETTER(::Transform*, BindPose, bindPose)
69 
70  Model& operator=(const ::Model& model) {
71  set(model);
72  return *this;
73  }
74 
75  Model& operator=(const Model&) = delete;
76 
77  Model& operator=(Model&& other) noexcept {
78  if (this == &other) {
79  return *this;
80  }
81 
82  Unload();
83  set(other);
84 
85  other.bones = nullptr;
86  other.boneCount = 0;
87  other.materials = nullptr;
88  other.materialCount = 0;
89  other.meshes = nullptr;
90  other.meshCount = 0;
91  other.bindPose = nullptr;
92 
93  return *this;
94  }
95 
99  inline void Unload() {
100  if (meshes != nullptr || materials != nullptr) {
101  ::UnloadModel(*this);
102  meshes = nullptr;
103  materials = nullptr;
104  }
105  }
106 
111  ::UnloadModelKeepMeshes(*this);
112  return *this;
113  }
114 
118  inline Model& SetMeshMaterial(int meshId, int materialId) {
119  ::SetModelMeshMaterial(this, meshId, materialId);
120  return *this;
121  }
122 
126  inline RayCollision GetCollision(const ::Ray& ray) const {
127  return ::GetRayCollisionModel(ray, *this);
128  }
129 
133  inline Model& UpdateAnimation(const ::ModelAnimation& anim, int frame) {
134  ::UpdateModelAnimation(*this, anim, frame);
135  return *this;
136  }
137 
141  inline bool IsModelAnimationValid(const ::ModelAnimation& anim) const {
142  return ::IsModelAnimationValid(*this, anim);
143  }
144 
148  inline Model& Draw(::Vector3 position,
149  float scale = 1.0f,
150  ::Color tint = {255, 255, 255, 255}) {
151  ::DrawModel(*this, position, scale, tint);
152  return *this;
153  }
154 
158  inline Model& Draw(
159  ::Vector3 position,
160  ::Vector3 rotationAxis,
161  float rotationAngle = 0.0f,
162  ::Vector3 scale = {1.0f, 1.0f, 1.0f},
163  ::Color tint = {255, 255, 255, 255}) {
164  ::DrawModelEx(*this, position, rotationAxis, rotationAngle, scale, tint);
165  return *this;
166  }
167 
171  inline Model& DrawWires(::Vector3 position,
172  float scale = 1.0f,
173  ::Color tint = {255, 255, 255, 255}) {
174  ::DrawModelWires(*this, position, scale, tint);
175  return *this;
176  }
177 
181  inline Model& DrawWires(
182  ::Vector3 position,
183  ::Vector3 rotationAxis,
184  float rotationAngle = 0.0f,
185  ::Vector3 scale = {1.0f, 1.0f, 1.0f},
186  ::Color tint = {255, 255, 255, 255}) {
187  ::DrawModelWiresEx(*this, position, rotationAxis, rotationAngle, scale, tint);
188  return *this;
189  }
190 
194  inline BoundingBox GetBoundingBox() const {
195  return ::GetModelBoundingBox(*this);
196  }
197 
201  operator BoundingBox() const {
202  return ::GetModelBoundingBox(*this);
203  }
204 
208  bool IsReady() const {
209  return meshCount > 0 || materialCount > 0 || boneCount > 0;
210  }
211 
217  bool Load(const std::string& fileName) {
218  set(::LoadModel(fileName.c_str()));
219  if (!IsReady()) {
220  throw RaylibException("Failed to load Model from " + fileName);
221  }
222  return IsReady();
223  }
224 
230  bool Load(const ::Mesh& mesh) {
231  set(::LoadModelFromMesh(mesh));
232  if (!IsReady()) {
233  throw RaylibException("Failed to load Model from Mesh");
234  }
235  return IsReady();
236  }
237 
238  private:
239  void set(const ::Model& model) {
240  transform = model.transform;
241 
242  meshCount = model.meshCount;
243  materialCount = model.materialCount;
244  meshes = model.meshes;
245  materials = model.materials;
246  meshMaterial = model.meshMaterial;
247 
248  boneCount = model.boneCount;
249  bones = model.bones;
250  bindPose = model.bindPose;
251  }
252 };
253 
254 } // namespace raylib
255 using RModel = raylib::Model;
256 
257 #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:208
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:171
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:158
Model & UnloadKeepMeshes()
Unload model (but not meshes) from memory (RAM and/or VRAM)
Definition: Model.hpp:110
void Unload()
Unload model (including meshes) from memory (RAM and/or VRAM)
Definition: Model.hpp:99
bool IsModelAnimationValid(const ::ModelAnimation &anim) const
Check model animation skeleton match.
Definition: Model.hpp:141
Model & UpdateAnimation(const ::ModelAnimation &anim, int frame)
Update model animation pose.
Definition: Model.hpp:133
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:181
Model & Draw(::Vector3 position, float scale=1.0f, ::Color tint={255, 255, 255, 255})
Draw a model (with texture if set)
Definition: Model.hpp:148
bool Load(const ::Mesh &mesh)
Loads a Model from the given Mesh.
Definition: Model.hpp:230
bool Load(const std::string &fileName)
Loads a Model from the given file.
Definition: Model.hpp:217
Model & SetMeshMaterial(int meshId, int materialId)
Set material for a mesh.
Definition: Model.hpp:118
RayCollision GetCollision(const ::Ray &ray) const
Get collision info between ray and model.
Definition: Model.hpp:126
BoundingBox GetBoundingBox() const
Compute model bounding box limits (considers all meshes)
Definition: Model.hpp:194
Raycast hit information.
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