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() {
149  Unload();
150  }
151 
155  inline bool Export(const std::string& fileName) {
156  // TODO(RobLoach): Switch to an exception when failed.
157  return ExportMesh(*this, fileName.c_str());
158  }
159 
160  inline void Unload() {
161  if (vboId != NULL) {
162  ::UnloadMesh(*this);
163  vboId = NULL;
164  }
165  }
166 
171  return ::MeshBoundingBox(*this);
172  }
173 
177  operator raylib::BoundingBox() {
178  return BoundingBox();
179  }
180 
184  inline Mesh& Tangents() {
185  ::MeshTangents(this);
186  return *this;
187  }
188 
192  inline Mesh& Binormals() {
193  ::MeshBinormals(this);
194  return *this;
195  }
196 
200  inline Mesh& NormalsSmooth() {
201  ::MeshNormalsSmooth(this);
202  return *this;
203  }
204 
208  inline raylib::Model LoadModelFrom() const {
209  return ::LoadModelFromMesh(*this);
210  }
211 
215  operator raylib::Model() {
216  return ::LoadModelFromMesh(*this);
217  }
218 
219  protected:
220  inline void set(const ::Mesh& mesh) {
221  vertexCount = mesh.vertexCount;
222  triangleCount = mesh.triangleCount;
223  vertices = mesh.vertices;
224  texcoords = mesh.texcoords;
225  texcoords2 = mesh.texcoords2;
226  normals = mesh.normals;
227  tangents = mesh.tangents;
228  colors = mesh.colors;
229  indices = mesh.indices;
230  animVertices = mesh.animVertices;
231  animNormals = mesh.animNormals;
232  boneIds = mesh.boneIds;
233  boneWeights = mesh.boneWeights;
234  vaoId = mesh.vaoId;
235  vboId = mesh.vboId;
236  }
237 };
238 } // namespace raylib
239 
240 #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:155
raylib::Mesh::NormalsSmooth
Mesh & NormalsSmooth()
Smooth (average) vertex normals.
Definition: Mesh.hpp:200
raylib::Mesh::BoundingBox
raylib::BoundingBox BoundingBox() const
Compute mesh bounding box limits.
Definition: Mesh.hpp:170
raylib::Mesh::Tangents
Mesh & Tangents()
Compute mesh tangents.
Definition: Mesh.hpp:184
raylib::Mesh::~Mesh
~Mesh()
Definition: Mesh.hpp:148
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:220
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:192
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:160
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::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:208
raylib::Mesh::Plane
::Mesh Plane(float width, float length, int resX, int resZ)
Generate plane mesh (with subdivisions)
Definition: Mesh.hpp:67