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 
22  Mesh(int vertexCount, int triangleCount) : ::Mesh{
23  vertexCount,
24  triangleCount,
25  nullptr,
26  nullptr,
27  nullptr,
28  nullptr,
29  nullptr,
30  nullptr,
31  nullptr,
32  nullptr,
33  nullptr,
34  nullptr,
35  nullptr,
36  0,
37  nullptr
38  } {}
39 
43  // static std::vector<Mesh> Load(const std::string& fileName) {
44  // int count = 0;
45  // ::Mesh* meshes = LoadMeshes(fileName.c_str(), &count);
46  // return std::vector<Mesh>(meshes, meshes + count);
47  // }
48 
49  Mesh(const Mesh&) = delete;
50 
51  Mesh(Mesh&& other) {
52  set(other);
53 
54  other.vertexCount = 0;
55  other.triangleCount = 0;
56  other.vertices = nullptr;
57  other.texcoords = nullptr;
58  other.texcoords2 = nullptr;
59  other.normals = nullptr;
60  other.tangents = nullptr;
61  other.colors = nullptr;
62  other.indices = nullptr;
63  other.animVertices = nullptr;
64  other.animNormals = nullptr;
65  other.boneIds = nullptr;
66  other.boneWeights = nullptr;
67  other.vaoId = 0;
68  other.vboId = nullptr;
69  }
70 
74  static ::Mesh Poly(int sides, float radius) {
75  return ::GenMeshPoly(sides, radius);
76  }
77 
81  static ::Mesh Plane(float width, float length, int resX, int resZ) {
82  return ::GenMeshPlane(width, length, resX, resZ);
83  }
84 
88  static ::Mesh Cube(float width, float height, float length) {
89  return ::GenMeshCube(width, height, length);
90  }
91 
95  static ::Mesh Sphere(float radius, int rings, int slices) {
96  return ::GenMeshSphere(radius, rings, slices);
97  }
98 
102  static ::Mesh HemiSphere(float radius, int rings, int slices) {
103  return ::GenMeshHemiSphere(radius, rings, slices);
104  }
105 
109  static ::Mesh Cylinder(float radius, float height, int slices) {
110  return ::GenMeshCylinder(radius, height, slices);
111  }
112 
116  static ::Mesh Torus(float radius, float size, int radSeg, int sides) {
117  return ::GenMeshTorus(radius, size, radSeg, sides);
118  }
119 
123  static ::Mesh Knot(float radius, float size, int radSeg, int sides) {
124  return ::GenMeshKnot(radius, size, radSeg, sides);
125  }
126 
130  static ::Mesh Heightmap(const ::Image& heightmap, ::Vector3 size) {
131  return ::GenMeshHeightmap(heightmap, size);
132  }
133 
137  static ::Mesh Cubicmap(const ::Image& cubicmap, ::Vector3 cubeSize) {
138  return ::GenMeshCubicmap(cubicmap, cubeSize);
139  }
140 
141  GETTERSETTER(int, VertexCount, vertexCount)
142  GETTERSETTER(int, TriangleCount, triangleCount)
143  GETTERSETTER(float*, Vertices, vertices)
144  GETTERSETTER(float *, TexCoords, texcoords)
145  GETTERSETTER(float *, TexCoords2, texcoords2)
146  GETTERSETTER(float *, Normals, normals)
147  GETTERSETTER(float *, Tangents, tangents)
148  GETTERSETTER(unsigned char *, Colors, colors)
149  GETTERSETTER(unsigned short *, Indices, indices) // NOLINT
150  GETTERSETTER(float *, AnimVertices, animVertices)
151  GETTERSETTER(float *, AnimNormals, animNormals)
152  GETTERSETTER(unsigned char *, BoneIds, boneIds)
153  GETTERSETTER(float *, BoneWeights, boneWeights)
154  GETTERSETTER(unsigned int, VaoId, vaoId)
155  GETTERSETTER(unsigned int *, VboId, vboId)
156 
157  Mesh& operator=(const ::Mesh& mesh) {
158  set(mesh);
159  return *this;
160  }
161 
162  Mesh& operator=(const Mesh&) = delete;
163 
164  Mesh& operator=(Mesh&& other) {
165  if (this != &other) {
166  return *this;
167  }
168 
169  Unload();
170  set(other);
171 
172  other.vertexCount = 0;
173  other.triangleCount = 0;
174  other.vertices = nullptr;
175  other.texcoords = nullptr;
176  other.texcoords2 = nullptr;
177  other.normals = nullptr;
178  other.tangents = nullptr;
179  other.colors = nullptr;
180  other.indices = nullptr;
181  other.animVertices = nullptr;
182  other.animNormals = nullptr;
183  other.boneIds = nullptr;
184  other.boneWeights = nullptr;
185  other.vaoId = 0;
186  other.vboId = nullptr;
187 
188  return *this;
189  }
190 
191  ~Mesh() {
192  Unload();
193  }
194 
198  inline void Upload(bool dynamic = false) {
199  ::UploadMesh(this, dynamic);
200  }
201 
205  inline void UpdateBuffer(int index, void *data, int dataSize, int offset = 0) {
206  ::UpdateMeshBuffer(*this, index, data, dataSize, offset);
207  }
208 
212  inline void Draw(const ::Material& material, const ::Matrix& transform) {
213  ::DrawMesh(*this, material, transform);
214  }
215 
219  inline void Draw(const ::Material& material, ::Matrix* transforms, int instances) {
220  ::DrawMeshInstanced(*this, material, transforms, instances);
221  }
222 
226  inline bool Export(const std::string& fileName) {
227  // TODO(RobLoach): Switch to an exception when failed.
228  return ExportMesh(*this, fileName.c_str());
229  }
230 
234  inline void Unload() {
235  if (vboId != nullptr) {
236  ::UnloadMesh(*this);
237  vboId = nullptr;
238  }
239  }
240 
245  return ::GetMeshBoundingBox(*this);
246  }
247 
251  operator raylib::BoundingBox() {
252  return BoundingBox();
253  }
254 
258  inline Mesh& GenTangents() {
259  ::GenMeshTangents(this);
260  return *this;
261  }
262 
266  inline Mesh& GenBinormals() {
267  ::GenMeshBinormals(this);
268  return *this;
269  }
270 
274  inline raylib::Model LoadModelFrom() const {
275  return ::LoadModelFromMesh(*this);
276  }
277 
281  operator raylib::Model() {
282  return ::LoadModelFromMesh(*this);
283  }
284 
285  private:
286  inline void set(const ::Mesh& mesh) {
287  vertexCount = mesh.vertexCount;
288  triangleCount = mesh.triangleCount;
289  vertices = mesh.vertices;
290  texcoords = mesh.texcoords;
291  texcoords2 = mesh.texcoords2;
292  normals = mesh.normals;
293  tangents = mesh.tangents;
294  colors = mesh.colors;
295  indices = mesh.indices;
296  animVertices = mesh.animVertices;
297  animNormals = mesh.animNormals;
298  boneIds = mesh.boneIds;
299  boneWeights = mesh.boneWeights;
300  vaoId = mesh.vaoId;
301  vboId = mesh.vboId;
302  }
303 };
304 } // namespace raylib
305 
306 #endif // RAYLIB_CPP_INCLUDE_MESH_HPP_
raylib::Mesh::Cube
::Mesh Cube(float width, float height, float length)
Generate cuboid mesh.
Definition: Mesh.hpp:88
raylib
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
raylib::Matrix
Matrix type (OpenGL style 4x4 - right handed, column major)
Definition: Matrix.hpp:16
raylib::Mesh::Export
bool Export(const std::string &fileName)
Export mesh data to file.
Definition: Mesh.hpp:226
raylib::Mesh::BoundingBox
raylib::BoundingBox BoundingBox() const
Compute mesh bounding box limits.
Definition: Mesh.hpp:244
raylib::Mesh::Poly
::Mesh Poly(int sides, float radius)
Generate polygonal mesh.
Definition: Mesh.hpp:74
raylib::Mesh::Torus
::Mesh Torus(float radius, float size, int radSeg, int sides)
Generate torus mesh.
Definition: Mesh.hpp:116
raylib::Vector3
Vector3 type.
Definition: Vector3.hpp:16
raylib::Mesh::HemiSphere
::Mesh HemiSphere(float radius, int rings, int slices)
Generate half-sphere mesh (no bottom cap)
Definition: Mesh.hpp:102
raylib::Mesh::GenTangents
Mesh & GenTangents()
Compute mesh tangents.
Definition: Mesh.hpp:258
raylib::Mesh::Upload
void Upload(bool dynamic=false)
Upload mesh vertex data to GPU (VRAM)
Definition: Mesh.hpp:198
raylib::Mesh::Unload
void Unload()
Unload mesh from memory (RAM and/or VRAM)
Definition: Mesh.hpp:234
raylib::Mesh
Vertex data definning a mesh.
Definition: Mesh.hpp:16
raylib::Mesh::Heightmap
::Mesh Heightmap(const ::Image &heightmap, ::Vector3 size)
Generate heightmap mesh from image data.
Definition: Mesh.hpp:130
raylib::Mesh::Knot
::Mesh Knot(float radius, float size, int radSeg, int sides)
Generate trefoil knot mesh.
Definition: Mesh.hpp:123
raylib::Mesh::Cubicmap
::Mesh Cubicmap(const ::Image &cubicmap, ::Vector3 cubeSize)
Generate cubes-based map mesh from image data.
Definition: Mesh.hpp:137
raylib::Mesh::Sphere
::Mesh Sphere(float radius, int rings, int slices)
Generate sphere mesh (standard sphere)
Definition: Mesh.hpp:95
raylib::Mesh::GenBinormals
Mesh & GenBinormals()
Compute mesh binormals (aka bitangent)
Definition: Mesh.hpp:266
raylib::Model
Model type.
Definition: Model.hpp:15
raylib::Mesh::Draw
void Draw(const ::Material &material, ::Matrix *transforms, int instances)
Draw multiple mesh instances with material and different transforms.
Definition: Mesh.hpp:219
raylib::Mesh::Cylinder
::Mesh Cylinder(float radius, float height, int slices)
Generate cylinder mesh.
Definition: Mesh.hpp:109
raylib::BoundingBox
Bounding box type.
Definition: BoundingBox.hpp:11
raylib::Mesh::LoadModelFrom
raylib::Model LoadModelFrom() const
Load model from generated mesh.
Definition: Mesh.hpp:274
raylib::Mesh::Draw
void Draw(const ::Material &material, const ::Matrix &transform)
Draw a 3d mesh with material and transform.
Definition: Mesh.hpp:212
raylib::Mesh::UpdateBuffer
void UpdateBuffer(int index, void *data, int dataSize, int offset=0)
Upload mesh vertex data to GPU (VRAM)
Definition: Mesh.hpp:205
raylib::Mesh::Plane
::Mesh Plane(float width, float length, int resX, int resZ)
Generate plane mesh (with subdivisions)
Definition: Mesh.hpp:81