forked from RobLoach/raylib-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeshUnmanaged.hpp
More file actions
254 lines (218 loc) · 7.17 KB
/
MeshUnmanaged.hpp
File metadata and controls
254 lines (218 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#ifndef RAYLIB_CPP_INCLUDE_MESHUNMANAGED_HPP_
#define RAYLIB_CPP_INCLUDE_MESHUNMANAGED_HPP_
#include <string>
#include <vector>
#include "./BoundingBox.hpp"
#include "./Matrix.hpp"
#include "./Model.hpp"
#include "./raylib-cpp-utils.hpp"
namespace raylib {
/**
* Vertex data defining a mesh, not managed by C++ RAII.
*
* Make sure to Unload() this if needed, otherwise use raylib::Mesh.
*
* @see raylib::Mesh
*/
class MeshUnmanaged : public ::Mesh {
public:
/**
* Default texture constructor.
*/
MeshUnmanaged() {
vertexCount = 0;
triangleCount = 0;
vertices = nullptr;
texcoords = nullptr;
texcoords2 = nullptr;
normals = nullptr;
tangents = nullptr;
colors = nullptr;
indices = nullptr;
animVertices = nullptr;
animNormals = nullptr;
boneIndices = nullptr;
boneWeights = nullptr;
boneCount = 0;
vaoId = 0;
vboId = nullptr;
}
MeshUnmanaged(const ::Mesh& mesh) { set(mesh); }
MeshUnmanaged(::Mesh&& mesh) { set(mesh); }
/**
* Load meshes from model file
*/
// static std::vector<Mesh> Load(const std::string& fileName) {
// int count = 0;
// ::Mesh* meshes = LoadMeshes(fileName.c_str(), &count);
// return std::vector<Mesh>(meshes, meshes + count);
// }
/**
* Generate polygonal mesh
*/
static ::Mesh Poly(int sides, float radius) { return ::GenMeshPoly(sides, radius); }
/**
* Generate plane mesh (with subdivisions)
*/
static ::Mesh Plane(float width, float length, int resX, int resZ) {
return ::GenMeshPlane(width, length, resX, resZ);
}
/**
* Generate cuboid mesh
*/
static ::Mesh Cube(float width, float height, float length) { return ::GenMeshCube(width, height, length); }
/**
* Generate sphere mesh (standard sphere)
*/
static ::Mesh Sphere(float radius, int rings, int slices) { return ::GenMeshSphere(radius, rings, slices); }
/**
* Generate half-sphere mesh (no bottom cap)
*/
static ::Mesh HemiSphere(float radius, int rings, int slices) { return ::GenMeshHemiSphere(radius, rings, slices); }
/**
* Generate cylinder mesh
*/
static ::Mesh Cylinder(float radius, float height, int slices) { return ::GenMeshCylinder(radius, height, slices); }
/**
* Generate cone/pyramid mesh
*/
static ::Mesh Cone(float radius, float height, int slices) { return ::GenMeshCone(radius, height, slices); }
/**
* Generate torus mesh
*/
static ::Mesh Torus(float radius, float size, int radSeg, int sides) {
return ::GenMeshTorus(radius, size, radSeg, sides);
}
/**
* Generate trefoil knot mesh
*/
static ::Mesh Knot(float radius, float size, int radSeg, int sides) {
return ::GenMeshKnot(radius, size, radSeg, sides);
}
/**
* Generate heightmap mesh from image data
*/
static ::Mesh Heightmap(const ::Image& heightmap, ::Vector3 size) { return ::GenMeshHeightmap(heightmap, size); }
/**
* Generate cubes-based map mesh from image data
*/
static ::Mesh Cubicmap(const ::Image& cubicmap, ::Vector3 cubeSize) {
return ::GenMeshCubicmap(cubicmap, cubeSize);
}
GETTERSETTER(int, VertexCount, vertexCount)
GETTERSETTER(int, TriangleCount, triangleCount)
GETTERSETTER(float*, Vertices, vertices)
GETTERSETTER(float*, TexCoords, texcoords)
GETTERSETTER(float*, TexCoords2, texcoords2)
GETTERSETTER(float*, Normals, normals)
GETTERSETTER(float*, Tangents, tangents)
GETTERSETTER(unsigned char*, Colors, colors)
GETTERSETTER(unsigned short*, Indices, indices) // NOLINT
GETTERSETTER(float*, AnimVertices, animVertices)
GETTERSETTER(float*, AnimNormals, animNormals)
GETTERSETTER(unsigned char*, BoneIndices, boneIndices)
GETTERSETTER(float*, BoneWeights, boneWeights)
GETTERSETTER(unsigned int, VaoId, vaoId)
GETTERSETTER(unsigned int*, VboId, vboId)
MeshUnmanaged& operator=(const ::Mesh& mesh) {
set(mesh);
return *this;
}
/**
* Unload mesh from memory (RAM and/or VRAM)
*/
void Unload() {
if (vboId != nullptr) {
::UnloadMesh(*this);
vboId = nullptr;
}
}
/**
* Upload mesh vertex data to GPU (VRAM)
*/
void Upload(bool dynamic = false) { ::UploadMesh(this, dynamic); }
/**
* Upload mesh vertex data to GPU (VRAM)
*/
void UpdateBuffer(int index, void* data, int dataSize, int offset = 0) {
::UpdateMeshBuffer(*this, index, data, dataSize, offset);
}
/**
* Draw a 3d mesh with material and transform
*/
void Draw(const ::Material& material, const ::Matrix& transform) const { ::DrawMesh(*this, material, transform); }
/**
* Draw multiple mesh instances with material and different transforms
*/
void Draw(const ::Material& material, ::Matrix* transforms, int instances) const {
::DrawMeshInstanced(*this, material, transforms, instances);
}
/**
* Export mesh data to file
*
* @throws raylib::RaylibException Throws if failed to export the Mesh.
*/
void Export(const std::string& fileName) {
if (!::ExportMesh(*this, fileName.c_str())) {
throw RaylibException("Failed to export the Mesh");
}
}
/**
* Export mesh as code file (.h) defining multiple arrays of vertex attributes
*
* @throws raylib::RaylibException Throws if failed to export the Mesh.
*/
void ExportCode(const std::string& fileName) {
if (!::ExportMeshAsCode(*this, fileName.c_str())) {
throw RaylibException("Failed to export the Mesh");
}
}
/**
* Compute mesh bounding box limits
*/
[[nodiscard]] raylib::BoundingBox BoundingBox() const { return ::GetMeshBoundingBox(*this); }
/**
* Compute mesh bounding box limits
*/
operator raylib::BoundingBox() const { return BoundingBox(); }
/**
* Compute mesh tangents
*/
Mesh& GenTangents() {
::GenMeshTangents(this);
return *this;
}
/**
* Load model from generated mesh
*/
[[nodiscard]] raylib::Model LoadModelFrom() const { return ::LoadModelFromMesh(*this); }
/**
* Load model from generated mesh
*/
operator raylib::Model() { return ::LoadModelFromMesh(*this); }
/**
* Returns whether or not the Mesh is valid.
*/
bool IsValid() { return ::IsModelValid(*this); }
protected:
void set(const ::Mesh& mesh) {
vertexCount = mesh.vertexCount;
triangleCount = mesh.triangleCount;
vertices = mesh.vertices;
texcoords = mesh.texcoords;
texcoords2 = mesh.texcoords2;
normals = mesh.normals;
tangents = mesh.tangents;
colors = mesh.colors;
indices = mesh.indices;
animVertices = mesh.animVertices;
animNormals = mesh.animNormals;
boneIndices = mesh.boneIndices;
boneWeights = mesh.boneWeights;
vaoId = mesh.vaoId;
vboId = mesh.vboId;
}
};
} // namespace raylib
using RMeshUnmanaged = raylib::MeshUnmanaged;
#endif // RAYLIB_CPP_INCLUDE_MESHUNMANAGED_HPP_