raylib-cpp
C++ object-oriented wrapper library for raylib.
Mesh.hpp
1 /*
2 * LICENSE: zlib/libpng
3 *
4 * raylib-cpp is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
5 * BSD-like license that allows static linking with closed source software:
6 *
7 * Copyright (c) 2020 Rob Loach (@RobLoach)
8 *
9 * This software is provided "as-is", without any express or implied warranty. In no event
10 * will the authors be held liable for any damages arising from the use of this software.
11 *
12 * Permission is granted to anyone to use this software for any purpose, including commercial
13 * applications, and to alter it and redistribute it freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not claim that you
16 * wrote the original software. If you use this software in a product, an acknowledgment
17 * in the product documentation would be appreciated but is not required.
18 *
19 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
20 * as being the original software.
21 *
22 * 3. This notice may not be removed or altered from any source distribution.
23 */
24 
25 #ifndef RAYLIB_CPP_INCLUDE_MESH_HPP_
26 #define RAYLIB_CPP_INCLUDE_MESH_HPP_
27 
28 #include <string>
29 #include <vector>
30 
31 #include "./raylib.hpp"
32 #include "./raylib-cpp-utils.hpp"
33 #include "./BoundingBox.hpp"
34 #include "./Model.hpp"
35 
36 namespace raylib {
37 class Mesh : public ::Mesh {
38  public:
39  Mesh(const ::Mesh& mesh) {
40  set(mesh);
41  }
42 
43  Mesh(int VertexCount = 0, int TriangleCount = 0) {
44  vertexCount = VertexCount;
45  triangleCount = TriangleCount;
46  }
47 
51  static std::vector<Mesh> Load(const std::string& fileName) {
52  int count = 0;
53  ::Mesh* meshes = LoadMeshes(fileName.c_str(), &count);
54  return std::vector<Mesh>(meshes, meshes + count);
55  }
56 
60  static ::Mesh Poly(int sides, float radius) {
61  return ::GenMeshPoly(sides, radius);
62  }
63 
67  static ::Mesh Plane(float width, float length, int resX, int resZ) {
68  return ::GenMeshPlane(width, length, resX, resZ);
69  }
70 
74  static ::Mesh Cube(float width, float height, float length) {
75  return ::GenMeshCube(width, height, length);
76  }
77 
81  static ::Mesh Sphere(float radius, int rings, int slices) {
82  return ::GenMeshSphere(radius, rings, slices);
83  }
84 
88  static ::Mesh HemiSphere(float radius, int rings, int slices) {
89  return ::GenMeshHemiSphere(radius, rings, slices);
90  }
91 
95  static ::Mesh Cylinder(float radius, float height, int slices) {
96  return ::GenMeshCylinder(radius, height, slices);
97  }
98 
102  static ::Mesh Torus(float radius, float size, int radSeg, int sides) {
103  return ::GenMeshTorus(radius, size, radSeg, sides);
104  }
105 
109  static ::Mesh Knot(float radius, float size, int radSeg, int sides) {
110  return ::GenMeshKnot(radius, size, radSeg, sides);
111  }
112 
116  static ::Mesh Heightmap(const ::Image& heightmap, ::Vector3 size) {
117  return ::GenMeshHeightmap(heightmap, size);
118  }
119 
123  static ::Mesh Cubicmap(const ::Image& cubicmap, ::Vector3 cubeSize) {
124  return ::GenMeshCubicmap(cubicmap, cubeSize);
125  }
126 
127  GETTERSETTER(int, VertexCount, vertexCount)
128  GETTERSETTER(int, TriangleCount, triangleCount)
129  GETTERSETTER(float*, Vertices, vertices)
130  GETTERSETTER(float *, TexCoords, texcoords)
131  GETTERSETTER(float *, TexCoords2, texcoords2)
132  GETTERSETTER(float *, Normals, normals)
133  GETTERSETTER(float *, Tangents, tangents)
134  GETTERSETTER(unsigned char *, Colors, colors)
135  GETTERSETTER(unsigned short *, Indices, indices) // NOLINT
136  GETTERSETTER(float *, AnimVertices, animVertices)
137  GETTERSETTER(float *, AnimNormals, animNormals)
138  GETTERSETTER(int *, BoneIds, boneIds)
139  GETTERSETTER(float *, BoneWeights, boneWeights)
140  GETTERSETTER(unsigned int, VaoId, vaoId)
141  GETTERSETTER(unsigned int *, VboId, vboId)
142 
143  Mesh& operator=(const ::Mesh& mesh) {
144  set(mesh);
145  return *this;
146  }
147 
148  Mesh& operator=(const Mesh& mesh) {
149  set(mesh);
150  return *this;
151  }
152 
153  ~Mesh() {
154  Unload();
155  }
156 
160  inline bool Export(const std::string& fileName) {
161  // TODO(RobLoach): Switch to an exception when failed.
162  return ExportMesh(*this, fileName.c_str());
163  }
164 
165  inline void Unload() {
166  if (vboId != NULL) {
167  ::UnloadMesh(*this);
168  vboId = NULL;
169  }
170  }
171 
173  return ::MeshBoundingBox(*this);
174  }
175 
176  operator raylib::BoundingBox() {
177  return BoundingBox();
178  }
179 
183  inline Mesh& Tangents() {
184  ::MeshTangents(this);
185  return *this;
186  }
187 
191  inline Mesh& Binormals() {
192  ::MeshBinormals(this);
193  return *this;
194  }
195 
199  inline Mesh& NormalsSmooth() {
200  ::MeshNormalsSmooth(this);
201  return *this;
202  }
203 
207  inline raylib::Model LoadModelFrom() const {
208  return ::LoadModelFromMesh(*this);
209  }
210 
214  operator raylib::Model() {
215  return ::LoadModelFromMesh(*this);
216  }
217 
218  protected:
219  inline void set(const ::Mesh& mesh) {
220  vertexCount = mesh.vertexCount;
221  triangleCount = mesh.triangleCount;
222  vertices = mesh.vertices;
223  texcoords = mesh.texcoords;
224  texcoords2 = mesh.texcoords2;
225  normals = mesh.normals;
226  tangents = mesh.tangents;
227  colors = mesh.colors;
228  indices = mesh.indices;
229  animVertices = mesh.animVertices;
230  animNormals = mesh.animNormals;
231  boneIds = mesh.boneIds;
232  boneWeights = mesh.boneWeights;
233  vaoId = mesh.vaoId;
234  vboId = mesh.vboId;
235  }
236 };
237 } // namespace raylib
238 
239 #endif // RAYLIB_CPP_INCLUDE_MESH_HPP_
raylib::Mesh::Cube
::Mesh Cube(float width, float height, float length)
Generate cuboid mesh.
Definition: Mesh.hpp:74
raylib
Provides all the classes associated with raylib-cpp.
Definition: AudioDevice.hpp:31
raylib::Mesh::Export
bool Export(const std::string &fileName)
Export mesh data to file.
Definition: Mesh.hpp:160
raylib::Mesh::NormalsSmooth
Mesh & NormalsSmooth()
Smooth (average) vertex normals.
Definition: Mesh.hpp:199
raylib::Mesh::BoundingBox
raylib::BoundingBox BoundingBox() const
Definition: Mesh.hpp:172
raylib::Mesh::Tangents
Mesh & Tangents()
Compute mesh tangents.
Definition: Mesh.hpp:183
raylib::Mesh::~Mesh
~Mesh()
Definition: Mesh.hpp:153
raylib::Mesh::Poly
::Mesh Poly(int sides, float radius)
Generate polygonal mesh.
Definition: Mesh.hpp:60
raylib::Mesh::Torus
::Mesh Torus(float radius, float size, int radSeg, int sides)
Generate torus mesh.
Definition: Mesh.hpp:102
raylib::Mesh::set
void set(const ::Mesh &mesh)
Definition: Mesh.hpp:219
raylib::Mesh::Mesh
Mesh(const ::Mesh &mesh)
Definition: Mesh.hpp:39
raylib::Vector3
Definition: Vector3.hpp:37
raylib::Mesh::Binormals
Mesh & Binormals()
Compute mesh binormals (aka bitangent)
Definition: Mesh.hpp:191
raylib::Mesh::HemiSphere
::Mesh HemiSphere(float radius, int rings, int slices)
Generate half-sphere mesh (no bottom cap)
Definition: Mesh.hpp:88
raylib::Mesh::Unload
void Unload()
Definition: Mesh.hpp:165
raylib::Mesh
Definition: Mesh.hpp:37
raylib::Mesh::Load
static std::vector< Mesh > Load(const std::string &fileName)
Load meshes from model file.
Definition: Mesh.hpp:51
raylib::Mesh::Heightmap
::Mesh Heightmap(const ::Image &heightmap, ::Vector3 size)
Generate heightmap mesh from image data.
Definition: Mesh.hpp:116
raylib::Mesh::Knot
::Mesh Knot(float radius, float size, int radSeg, int sides)
Generate trefoil knot mesh.
Definition: Mesh.hpp:109
raylib::Mesh::Cubicmap
::Mesh Cubicmap(const ::Image &cubicmap, ::Vector3 cubeSize)
Generate cubes-based map mesh from image data.
Definition: Mesh.hpp:123
raylib::Mesh::Sphere
::Mesh Sphere(float radius, int rings, int slices)
Generate sphere mesh (standard sphere)
Definition: Mesh.hpp:81
raylib::Mesh::operator=
Mesh & operator=(const Mesh &mesh)
Definition: Mesh.hpp:148
raylib::Mesh::Mesh
Mesh(int VertexCount=0, int TriangleCount=0)
Definition: Mesh.hpp:43
raylib::Model
Definition: Model.hpp:35
raylib::Mesh::Cylinder
::Mesh Cylinder(float radius, float height, int slices)
Generate cylinder mesh.
Definition: Mesh.hpp:95
raylib::BoundingBox
Bounding box type.
Definition: BoundingBox.hpp:35
raylib::Mesh::LoadModelFrom
raylib::Model LoadModelFrom() const
Load model from generated mesh.
Definition: Mesh.hpp:207
raylib::Mesh::Plane
::Mesh Plane(float width, float length, int resX, int resZ)
Generate plane mesh (with subdivisions)
Definition: Mesh.hpp:67