raylib-cpp
C++ object-oriented wrapper library for raylib.
Mesh.hpp
1 #ifndef RAYLIB_CPP_INCLUDE_MESH_HPP_
2 #define RAYLIB_CPP_INCLUDE_MESH_HPP_
3 
4 #include <string>
5 #include <vector>
6 
7 #include "./raylib.hpp"
8 #include "./raylib-cpp-utils.hpp"
9 #include "./BoundingBox.hpp"
10 #include "./Model.hpp"
11 
12 namespace raylib {
16 class Mesh : public ::Mesh {
17  public:
18  Mesh(const ::Mesh& mesh) {
19  set(mesh);
20  }
21 
25  // static std::vector<Mesh> Load(const std::string& fileName) {
26  // int count = 0;
27  // ::Mesh* meshes = LoadMeshes(fileName.c_str(), &count);
28  // return std::vector<Mesh>(meshes, meshes + count);
29  // }
30 
31  Mesh(const Mesh&) = delete;
32 
33  Mesh(Mesh&& other) {
34  set(other);
35 
36  other.vertexCount = 0;
37  other.triangleCount = 0;
38  other.vertices = nullptr;
39  other.texcoords = nullptr;
40  other.texcoords2 = nullptr;
41  other.normals = nullptr;
42  other.tangents = nullptr;
43  other.colors = nullptr;
44  other.indices = nullptr;
45  other.animVertices = nullptr;
46  other.animNormals = nullptr;
47  other.boneIds = nullptr;
48  other.boneWeights = nullptr;
49  other.vaoId = 0;
50  other.vboId = nullptr;
51  }
52 
56  static ::Mesh Poly(int sides, float radius) {
57  return ::GenMeshPoly(sides, radius);
58  }
59 
63  static ::Mesh Plane(float width, float length, int resX, int resZ) {
64  return ::GenMeshPlane(width, length, resX, resZ);
65  }
66 
70  static ::Mesh Cube(float width, float height, float length) {
71  return ::GenMeshCube(width, height, length);
72  }
73 
77  static ::Mesh Sphere(float radius, int rings, int slices) {
78  return ::GenMeshSphere(radius, rings, slices);
79  }
80 
84  static ::Mesh HemiSphere(float radius, int rings, int slices) {
85  return ::GenMeshHemiSphere(radius, rings, slices);
86  }
87 
91  static ::Mesh Cylinder(float radius, float height, int slices) {
92  return ::GenMeshCylinder(radius, height, slices);
93  }
94 
98  static ::Mesh Cone(float radius, float height, int slices) {
99  return ::GenMeshCone(radius, height, slices);
100  }
101 
105  static ::Mesh Torus(float radius, float size, int radSeg, int sides) {
106  return ::GenMeshTorus(radius, size, radSeg, sides);
107  }
108 
112  static ::Mesh Knot(float radius, float size, int radSeg, int sides) {
113  return ::GenMeshKnot(radius, size, radSeg, sides);
114  }
115 
119  static ::Mesh Heightmap(const ::Image& heightmap, ::Vector3 size) {
120  return ::GenMeshHeightmap(heightmap, size);
121  }
122 
126  static ::Mesh Cubicmap(const ::Image& cubicmap, ::Vector3 cubeSize) {
127  return ::GenMeshCubicmap(cubicmap, cubeSize);
128  }
129 
130  GETTERSETTER(int, VertexCount, vertexCount)
131  GETTERSETTER(int, TriangleCount, triangleCount)
132  GETTERSETTER(float*, Vertices, vertices)
133  GETTERSETTER(float *, TexCoords, texcoords)
134  GETTERSETTER(float *, TexCoords2, texcoords2)
135  GETTERSETTER(float *, Normals, normals)
136  GETTERSETTER(float *, Tangents, tangents)
137  GETTERSETTER(unsigned char *, Colors, colors)
138  GETTERSETTER(unsigned short *, Indices, indices) // NOLINT
139  GETTERSETTER(float *, AnimVertices, animVertices)
140  GETTERSETTER(float *, AnimNormals, animNormals)
141  GETTERSETTER(unsigned char *, BoneIds, boneIds)
142  GETTERSETTER(float *, BoneWeights, boneWeights)
143  GETTERSETTER(unsigned int, VaoId, vaoId)
144  GETTERSETTER(unsigned int *, VboId, vboId)
145 
146  Mesh& operator=(const ::Mesh& mesh) {
147  set(mesh);
148  return *this;
149  }
150 
151  Mesh& operator=(const Mesh&) = delete;
152 
153  Mesh& operator=(Mesh&& other) noexcept {
154  if (this == &other) {
155  return *this;
156  }
157 
158  Unload();
159  set(other);
160 
161  other.vertexCount = 0;
162  other.triangleCount = 0;
163  other.vertices = nullptr;
164  other.texcoords = nullptr;
165  other.texcoords2 = nullptr;
166  other.normals = nullptr;
167  other.tangents = nullptr;
168  other.colors = nullptr;
169  other.indices = nullptr;
170  other.animVertices = nullptr;
171  other.animNormals = nullptr;
172  other.boneIds = nullptr;
173  other.boneWeights = nullptr;
174  other.vaoId = 0;
175  other.vboId = nullptr;
176 
177  return *this;
178  }
179 
180  ~Mesh() {
181  Unload();
182  }
183 
187  inline void Upload(bool dynamic = false) {
188  ::UploadMesh(this, dynamic);
189  }
190 
194  inline void UpdateBuffer(int index, void *data, int dataSize, int offset = 0) {
195  ::UpdateMeshBuffer(*this, index, data, dataSize, offset);
196  }
197 
201  inline void Draw(const ::Material& material, const ::Matrix& transform) const {
202  ::DrawMesh(*this, material, transform);
203  }
204 
208  inline void Draw(const ::Material& material, ::Matrix* transforms, int instances) const {
209  ::DrawMeshInstanced(*this, material, transforms, instances);
210  }
211 
217  inline void Export(const std::string& fileName) {
218  if (!::ExportMesh(*this, fileName.c_str())) {
219  throw new RaylibException("Failed to export the Mesh");
220  }
221  }
222 
226  inline void Unload() {
227  if (vboId != nullptr) {
228  ::UnloadMesh(*this);
229  vboId = nullptr;
230  }
231  }
232 
237  return ::GetMeshBoundingBox(*this);
238  }
239 
243  operator raylib::BoundingBox() {
244  return BoundingBox();
245  }
246 
250  inline Mesh& GenTangents() {
251  ::GenMeshTangents(this);
252  return *this;
253  }
254 
258  inline raylib::Model LoadModelFrom() const {
259  return ::LoadModelFromMesh(*this);
260  }
261 
265  operator raylib::Model() {
266  return ::LoadModelFromMesh(*this);
267  }
268 
269  private:
270  void set(const ::Mesh& mesh) {
271  vertexCount = mesh.vertexCount;
272  triangleCount = mesh.triangleCount;
273  vertices = mesh.vertices;
274  texcoords = mesh.texcoords;
275  texcoords2 = mesh.texcoords2;
276  normals = mesh.normals;
277  tangents = mesh.tangents;
278  colors = mesh.colors;
279  indices = mesh.indices;
280  animVertices = mesh.animVertices;
281  animNormals = mesh.animNormals;
282  boneIds = mesh.boneIds;
283  boneWeights = mesh.boneWeights;
284  vaoId = mesh.vaoId;
285  vboId = mesh.vboId;
286  }
287 };
288 } // namespace raylib
289 
290 using RMesh = raylib::Mesh;
291 
292 #endif // RAYLIB_CPP_INCLUDE_MESH_HPP_
Bounding box type.
Definition: BoundingBox.hpp:11
Matrix type (OpenGL style 4x4 - right handed, column major)
Definition: Matrix.hpp:16
Vertex data definning a mesh.
Definition: Mesh.hpp:16
raylib::BoundingBox BoundingBox() const
Compute mesh bounding box limits.
Definition: Mesh.hpp:236
Mesh(const Mesh &)=delete
Load meshes from model file.
void Draw(const ::Material &material, ::Matrix *transforms, int instances) const
Draw multiple mesh instances with material and different transforms.
Definition: Mesh.hpp:208
raylib::Model LoadModelFrom() const
Load model from generated mesh.
Definition: Mesh.hpp:258
::Mesh Sphere(float radius, int rings, int slices)
Generate sphere mesh (standard sphere)
Definition: Mesh.hpp:77
::Mesh Knot(float radius, float size, int radSeg, int sides)
Generate trefoil knot mesh.
Definition: Mesh.hpp:112
void Unload()
Unload mesh from memory (RAM and/or VRAM)
Definition: Mesh.hpp:226
Mesh & GenTangents()
Compute mesh tangents.
Definition: Mesh.hpp:250
void UpdateBuffer(int index, void *data, int dataSize, int offset=0)
Upload mesh vertex data to GPU (VRAM)
Definition: Mesh.hpp:194
::Mesh Cube(float width, float height, float length)
Generate cuboid mesh.
Definition: Mesh.hpp:70
::Mesh Cone(float radius, float height, int slices)
Generate cone/pyramid mesh.
Definition: Mesh.hpp:98
::Mesh Plane(float width, float length, int resX, int resZ)
Generate plane mesh (with subdivisions)
Definition: Mesh.hpp:63
::Mesh Poly(int sides, float radius)
Generate polygonal mesh.
Definition: Mesh.hpp:56
::Mesh HemiSphere(float radius, int rings, int slices)
Generate half-sphere mesh (no bottom cap)
Definition: Mesh.hpp:84
::Mesh Torus(float radius, float size, int radSeg, int sides)
Generate torus mesh.
Definition: Mesh.hpp:105
void Upload(bool dynamic=false)
Upload mesh vertex data to GPU (VRAM)
Definition: Mesh.hpp:187
::Mesh Heightmap(const ::Image &heightmap, ::Vector3 size)
Generate heightmap mesh from image data.
Definition: Mesh.hpp:119
void Draw(const ::Material &material, const ::Matrix &transform) const
Draw a 3d mesh with material and transform.
Definition: Mesh.hpp:201
::Mesh Cylinder(float radius, float height, int slices)
Generate cylinder mesh.
Definition: Mesh.hpp:91
::Mesh Cubicmap(const ::Image &cubicmap, ::Vector3 cubeSize)
Generate cubes-based map mesh from image data.
Definition: Mesh.hpp:126
void Export(const std::string &fileName)
Export mesh data to file.
Definition: Mesh.hpp:217
Model type.
Definition: Model.hpp:15
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