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(::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  }
66  static Mesh Plane(float width, float length, int resX, int resZ) {
67  return ::GenMeshPlane(width, length, resX, resZ);
68  }
69 
73  static Mesh Cube(float width, float height, float length) {
74  return ::GenMeshCube(width, height, length);
75  }
79  static Mesh Sphere(float radius, int rings, int slices) {
80  return ::GenMeshSphere(radius, rings, slices);
81  }
82 
86  static Mesh HemiSphere(float radius, int rings, int slices) {
87  return ::GenMeshHemiSphere(radius, rings, slices);
88  }
89 
93  static Mesh Cylinder(float radius, float height, int slices) {
94  return ::GenMeshCylinder(radius, height, slices);
95  }
96 
100  static Mesh Torus(float radius, float size, int radSeg, int sides) {
101  return ::GenMeshTorus(radius, size, radSeg, sides);
102  }
103 
107  static Mesh Knot(float radius, float size, int radSeg, int sides) {
108  return ::GenMeshKnot(radius, size, radSeg, sides);
109  }
110 
114  static Mesh Heightmap(::Image heightmap, ::Vector3 size) {
115  return ::GenMeshHeightmap(heightmap, size);
116  }
117 
121  static Mesh Cubicmap(::Image cubicmap, ::Vector3 cubeSize) {
122  return ::GenMeshCubicmap(cubicmap, cubeSize);
123  }
124 
125  GETTERSETTER(int, VertexCount, vertexCount)
126  GETTERSETTER(int, TriangleCount, triangleCount)
127  GETTERSETTER(float*, Vertices, vertices)
128  GETTERSETTER(float *, TexCoords, texcoords)
129  GETTERSETTER(float *, TexCoords2, texcoords2)
130  GETTERSETTER(float *, Normals, normals)
131  GETTERSETTER(float *, Tangents, tangents)
132  GETTERSETTER(unsigned char *, Colors, colors)
133  GETTERSETTER(unsigned short *, Indices, indices)
134  GETTERSETTER(float *, AnimVertices, animVertices)
135  GETTERSETTER(float *, AnimNormals, animNormals)
136  GETTERSETTER(int *, BoneIds, boneIds)
137  GETTERSETTER(float *, BoneWeights, boneWeights)
138  GETTERSETTER(unsigned int, VaoId, vaoId)
139  GETTERSETTER(unsigned int *, VboId, vboId)
140 
141  Mesh& operator=(const ::Mesh& mesh) {
142  set(mesh);
143  return *this;
144  }
145 
146  Mesh& operator=(const Mesh& mesh) {
147  set(mesh);
148  return *this;
149  }
150 
151  ~Mesh() {
152  Unload();
153  }
154 
158  inline bool Export(const std::string& fileName) {
159  // TODO(RobLoach): Switch to an exception when failed.
160  return ExportMesh(*this, fileName.c_str());
161  }
162 
163  inline void Unload() {
164  ::UnloadMesh(*this);
165  }
166 
168  return ::MeshBoundingBox(*this);
169  }
170 
171  operator raylib::BoundingBox() {
172  return BoundingBox();
173  }
174 
178  inline Mesh& Tangents() {
179  ::MeshTangents(this);
180  return *this;
181  }
182 
186  inline Mesh& Binormals() {
187  ::MeshBinormals(this);
188  return *this;
189  }
190 
194  inline Mesh& NormalsSmooth() {
195  ::MeshNormalsSmooth(this);
196  return *this;
197  }
198 
202  inline raylib::Model LoadModelFrom() const {
203  return ::LoadModelFromMesh(*this);
204  }
205 
209  operator raylib::Model() {
210  return ::LoadModelFromMesh(*this);
211  }
212 
213  protected:
214  inline void set(::Mesh mesh) {
215  vertexCount = mesh.vertexCount;
216  triangleCount = mesh.triangleCount;
217  vertices = mesh.vertices;
218  texcoords = mesh.texcoords;
219  texcoords2 = mesh.texcoords2;
220  normals = mesh.normals;
221  tangents = mesh.tangents;
222  colors = mesh.colors;
223  indices = mesh.indices;
224  animVertices = mesh.animVertices;
225  animNormals = mesh.animNormals;
226  boneIds = mesh.boneIds;
227  boneWeights = mesh.boneWeights;
228  vaoId = mesh.vaoId;
229  vboId = mesh.vboId;
230  }
231 };
232 } // namespace raylib
233 
234 #endif // RAYLIB_CPP_INCLUDE_MESH_HPP_
raylib
Provides all the classes associated with raylib-cpp.
Definition: AudioDevice.hpp:31
raylib::Mesh::Cube
static Mesh Cube(float width, float height, float length)
Generate cuboid mesh.
Definition: Mesh.hpp:73
raylib::Mesh::set
void set(::Mesh mesh)
Definition: Mesh.hpp:214
raylib::Mesh::Load
static std::vector< Mesh > Load(const std::string fileName)
Load meshes from model file.
Definition: Mesh.hpp:51
raylib::Mesh::Export
bool Export(const std::string &fileName)
Export mesh data to file.
Definition: Mesh.hpp:158
raylib::Mesh::NormalsSmooth
Mesh & NormalsSmooth()
Smooth (average) vertex normals.
Definition: Mesh.hpp:194
raylib::Mesh::BoundingBox
raylib::BoundingBox BoundingBox() const
Definition: Mesh.hpp:167
raylib::Mesh::Tangents
Mesh & Tangents()
Compute mesh tangents.
Definition: Mesh.hpp:178
raylib::Mesh::~Mesh
~Mesh()
Definition: Mesh.hpp:151
raylib::Image
Definition: Image.hpp:34
raylib::Mesh::Plane
static Mesh Plane(float width, float length, int resX, int resZ)
Generate plane mesh (with subdivisions)
Definition: Mesh.hpp:66
raylib::Vector3
Definition: Vector3.hpp:37
raylib::Mesh::Binormals
Mesh & Binormals()
Compute mesh binormals (aka bitangent)
Definition: Mesh.hpp:186
raylib::Mesh::Unload
void Unload()
Definition: Mesh.hpp:163
raylib::Mesh
Definition: Mesh.hpp:37
raylib::Mesh::Torus
static Mesh Torus(float radius, float size, int radSeg, int sides)
Generate torus mesh.
Definition: Mesh.hpp:100
raylib::Mesh::Knot
static Mesh Knot(float radius, float size, int radSeg, int sides)
Generate trefoil knot mesh.
Definition: Mesh.hpp:107
raylib::Mesh::Heightmap
static Mesh Heightmap(::Image heightmap, ::Vector3 size)
Generate heightmap mesh from image data.
Definition: Mesh.hpp:114
raylib::Mesh::operator=
Mesh & operator=(const Mesh &mesh)
Definition: Mesh.hpp:146
raylib::Mesh::Mesh
Mesh(int VertexCount=0, int TriangleCount=0)
Definition: Mesh.hpp:43
raylib::Model
Definition: Model.hpp:35
raylib::Mesh::HemiSphere
static Mesh HemiSphere(float radius, int rings, int slices)
Generate half-sphere mesh (no bottom cap)
Definition: Mesh.hpp:86
raylib::BoundingBox
Bounding box type.
Definition: BoundingBox.hpp:35
raylib::Mesh::Mesh
Mesh(::Mesh mesh)
Definition: Mesh.hpp:39
raylib::Mesh::Sphere
static Mesh Sphere(float radius, int rings, int slices)
Generate sphere mesh (standard sphere)
Definition: Mesh.hpp:79
raylib::Mesh::LoadModelFrom
raylib::Model LoadModelFrom() const
Load model from generated mesh.
Definition: Mesh.hpp:202
raylib::Mesh::Cylinder
static Mesh Cylinder(float radius, float height, int slices)
Generate cylinder mesh.
Definition: Mesh.hpp:93
raylib::Mesh::Cubicmap
static Mesh Cubicmap(::Image cubicmap, ::Vector3 cubeSize)
Generate cubes-based map mesh from image data.
Definition: Mesh.hpp:121
raylib::Mesh::Poly
static Mesh Poly(int sides, float radius)
Generate polygonal mesh.
Definition: Mesh.hpp:60