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) {
23  vertexCount = VertexCount;
24  triangleCount = TriangleCount;
25  }
26 
30  // static std::vector<Mesh> Load(const std::string& fileName) {
31  // int count = 0;
32  // ::Mesh* meshes = LoadMeshes(fileName.c_str(), &count);
33  // return std::vector<Mesh>(meshes, meshes + count);
34  // }
35 
39  static ::Mesh Poly(int sides, float radius) {
40  return ::GenMeshPoly(sides, radius);
41  }
42 
46  static ::Mesh Plane(float width, float length, int resX, int resZ) {
47  return ::GenMeshPlane(width, length, resX, resZ);
48  }
49 
53  static ::Mesh Cube(float width, float height, float length) {
54  return ::GenMeshCube(width, height, length);
55  }
56 
60  static ::Mesh Sphere(float radius, int rings, int slices) {
61  return ::GenMeshSphere(radius, rings, slices);
62  }
63 
67  static ::Mesh HemiSphere(float radius, int rings, int slices) {
68  return ::GenMeshHemiSphere(radius, rings, slices);
69  }
70 
74  static ::Mesh Cylinder(float radius, float height, int slices) {
75  return ::GenMeshCylinder(radius, height, slices);
76  }
77 
81  static ::Mesh Torus(float radius, float size, int radSeg, int sides) {
82  return ::GenMeshTorus(radius, size, radSeg, sides);
83  }
84 
88  static ::Mesh Knot(float radius, float size, int radSeg, int sides) {
89  return ::GenMeshKnot(radius, size, radSeg, sides);
90  }
91 
95  static ::Mesh Heightmap(const ::Image& heightmap, ::Vector3 size) {
96  return ::GenMeshHeightmap(heightmap, size);
97  }
98 
102  static ::Mesh Cubicmap(const ::Image& cubicmap, ::Vector3 cubeSize) {
103  return ::GenMeshCubicmap(cubicmap, cubeSize);
104  }
105 
106  GETTERSETTER(int, VertexCount, vertexCount)
107  GETTERSETTER(int, TriangleCount, triangleCount)
108  GETTERSETTER(float*, Vertices, vertices)
109  GETTERSETTER(float *, TexCoords, texcoords)
110  GETTERSETTER(float *, TexCoords2, texcoords2)
111  GETTERSETTER(float *, Normals, normals)
112  GETTERSETTER(float *, Tangents, tangents)
113  GETTERSETTER(unsigned char *, Colors, colors)
114  GETTERSETTER(unsigned short *, Indices, indices) // NOLINT
115  GETTERSETTER(float *, AnimVertices, animVertices)
116  GETTERSETTER(float *, AnimNormals, animNormals)
117  GETTERSETTER(int *, BoneIds, boneIds)
118  GETTERSETTER(float *, BoneWeights, boneWeights)
119  GETTERSETTER(unsigned int, VaoId, vaoId)
120  GETTERSETTER(unsigned int *, VboId, vboId)
121 
122  Mesh& operator=(const ::Mesh& mesh) {
123  set(mesh);
124  return *this;
125  }
126 
127  ~Mesh() {
128  Unload();
129  }
130 
134  inline void Upload(bool dynamic = false) {
135  ::UploadMesh(this, dynamic);
136  }
137 
141  inline void UpdateBuffer(int index, void *data, int dataSize, int offset = 0) {
142  ::UpdateMeshBuffer(*this, index, data, dataSize, offset);
143  }
144 
145  inline void Draw(const ::Material& material, const ::Matrix& transform) {
146  ::DrawMesh(*this, material, transform);
147  }
148 
149  inline void DrawInstanced(const ::Material& material, ::Matrix* transforms, int instances) {
150  ::DrawMeshInstanced(*this, material, transforms, instances);
151  }
152 
156  inline bool Export(const std::string& fileName) {
157  // TODO(RobLoach): Switch to an exception when failed.
158  return ExportMesh(*this, fileName.c_str());
159  }
160 
164  inline void Unload() {
165  if (vboId != NULL) {
166  ::UnloadMesh(*this);
167  vboId = NULL;
168  }
169  }
170 
175  return ::MeshBoundingBox(*this);
176  }
177 
181  operator raylib::BoundingBox() {
182  return BoundingBox();
183  }
184 
188  inline Mesh& Tangents() {
189  ::MeshTangents(this);
190  return *this;
191  }
192 
196  inline Mesh& Binormals() {
197  ::MeshBinormals(this);
198  return *this;
199  }
200 
204  inline raylib::Model LoadModelFrom() const {
205  return ::LoadModelFromMesh(*this);
206  }
207 
211  operator raylib::Model() {
212  return ::LoadModelFromMesh(*this);
213  }
214 
215  private:
216  inline void set(const ::Mesh& mesh) {
217  vertexCount = mesh.vertexCount;
218  triangleCount = mesh.triangleCount;
219  vertices = mesh.vertices;
220  texcoords = mesh.texcoords;
221  texcoords2 = mesh.texcoords2;
222  normals = mesh.normals;
223  tangents = mesh.tangents;
224  colors = mesh.colors;
225  indices = mesh.indices;
226  animVertices = mesh.animVertices;
227  animNormals = mesh.animNormals;
228  boneIds = mesh.boneIds;
229  boneWeights = mesh.boneWeights;
230  vaoId = mesh.vaoId;
231  vboId = mesh.vboId;
232  }
233 };
234 } // namespace raylib
235 
236 #endif // RAYLIB_CPP_INCLUDE_MESH_HPP_
raylib::Mesh::Cube
::Mesh Cube(float width, float height, float length)
Generate cuboid mesh.
Definition: Mesh.hpp:53
raylib
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:7
raylib::Mesh::Export
bool Export(const std::string &fileName)
Export mesh data to file.
Definition: Mesh.hpp:156
raylib::Mesh::BoundingBox
raylib::BoundingBox BoundingBox() const
Compute mesh bounding box limits.
Definition: Mesh.hpp:174
raylib::Mesh::Tangents
Mesh & Tangents()
Compute mesh tangents.
Definition: Mesh.hpp:188
raylib::Mesh::Poly
::Mesh Poly(int sides, float radius)
Load meshes from model file.
Definition: Mesh.hpp:39
raylib::Mesh::Torus
::Mesh Torus(float radius, float size, int radSeg, int sides)
Generate torus mesh.
Definition: Mesh.hpp:81
raylib::Vector3
Vector3 type.
Definition: Vector3.hpp:16
raylib::Mesh::Binormals
Mesh & Binormals()
Compute mesh binormals (aka bitangent)
Definition: Mesh.hpp:196
raylib::Mesh::HemiSphere
::Mesh HemiSphere(float radius, int rings, int slices)
Generate half-sphere mesh (no bottom cap)
Definition: Mesh.hpp:67
raylib::Mesh::Upload
void Upload(bool dynamic=false)
Upload mesh vertex data to GPU (VRAM)
Definition: Mesh.hpp:134
raylib::Mesh::Unload
void Unload()
Unload mesh from memory (RAM and/or VRAM)
Definition: Mesh.hpp:164
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:95
raylib::Mesh::Knot
::Mesh Knot(float radius, float size, int radSeg, int sides)
Generate trefoil knot mesh.
Definition: Mesh.hpp:88
raylib::Mesh::Cubicmap
::Mesh Cubicmap(const ::Image &cubicmap, ::Vector3 cubeSize)
Generate cubes-based map mesh from image data.
Definition: Mesh.hpp:102
raylib::Mesh::Sphere
::Mesh Sphere(float radius, int rings, int slices)
Generate sphere mesh (standard sphere)
Definition: Mesh.hpp:60
raylib::Model
Model type.
Definition: Model.hpp:14
raylib::Mesh::Cylinder
::Mesh Cylinder(float radius, float height, int slices)
Generate cylinder mesh.
Definition: Mesh.hpp:74
raylib::BoundingBox
Bounding box type.
Definition: BoundingBox.hpp:11
raylib::Mesh::LoadModelFrom
raylib::Model LoadModelFrom() const
Load model from generated mesh.
Definition: Mesh.hpp:204
raylib::Mesh::UpdateBuffer
void UpdateBuffer(int index, void *data, int dataSize, int offset=0)
Upload mesh vertex data to GPU (VRAM)
Definition: Mesh.hpp:141
raylib::Mesh::Plane
::Mesh Plane(float width, float length, int resX, int resZ)
Generate plane mesh (with subdivisions)
Definition: Mesh.hpp:46