raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
MeshUnmanaged.hpp
1#ifndef RAYLIB_CPP_INCLUDE_MESHUNMANAGED_HPP_
2#define RAYLIB_CPP_INCLUDE_MESHUNMANAGED_HPP_
3
4#include <string>
5#include <vector>
6
7#include "./BoundingBox.hpp"
8#include "./Matrix.hpp"
9#include "./Model.hpp"
10#include "./raylib-cpp-utils.hpp"
11
12namespace raylib {
13
21class MeshUnmanaged : public ::Mesh {
22public:
27 vertexCount = 0;
28 triangleCount = 0;
29 vertices = nullptr;
30 texcoords = nullptr;
31 texcoords2 = nullptr;
32 normals = nullptr;
33 tangents = nullptr;
34 colors = nullptr;
35 indices = nullptr;
36 animVertices = nullptr;
37 animNormals = nullptr;
38 boneIds = nullptr;
39 boneWeights = nullptr;
40 boneMatrices = nullptr;
41 boneCount = 0;
42 vaoId = 0;
43 vboId = nullptr;
44 }
45
46 MeshUnmanaged(const ::Mesh& mesh) { set(mesh); }
47
48 MeshUnmanaged(::Mesh&& mesh) { set(mesh); }
49
53 // static std::vector<Mesh> Load(std::string_view fileName) {
54 // int count = 0;
55 // ::Mesh* meshes = LoadMeshes(fileName.c_str(), &count);
56 // return std::vector<Mesh>(meshes, meshes + count);
57 // }
58
62 static MeshUnmanaged Poly(int sides, float radius) {
63 return ::GenMeshPoly(sides, radius);
64 }
65
69 static MeshUnmanaged Plane(float width, float length, int resX, int resZ) {
70 return ::GenMeshPlane(width, length, resX, resZ);
71 }
72
73 static MeshUnmanaged Plane(float width, float length, int resX, int resZ, float textureScale) {
74 ::Mesh mesh = {};
75
76 resX++;
77 resZ++;
78
79 // Vertices definition
80 int vertexCount = resX*resZ; // vertices get reused for the faces
81
82 Vector3 *vertices = (Vector3 *)RL_MALLOC(vertexCount*sizeof(Vector3));
83 for (int z = 0; z < resZ; z++)
84 {
85 // [-length/2, length/2]
86 float zPos = ((float)z/(resZ - 1) - 0.5f)*length;
87 for (int x = 0; x < resX; x++)
88 {
89 // [-width/2, width/2]
90 float xPos = ((float)x/(resX - 1) - 0.5f)*width;
91 vertices[x + z*resX] = Vector3{ xPos, 0.0f, zPos };
92 }
93 }
94
95 // Normals definition
96 Vector3 *normals = (Vector3 *)RL_MALLOC(vertexCount*sizeof(Vector3));
97 for (int n = 0; n < vertexCount; n++) normals[n] = Vector3{ 0.0f, 1.0f, 0.0f }; // Vector3.up;
98
99 // TexCoords definition
100 Vector2 *texcoords = (Vector2 *)RL_MALLOC(vertexCount*sizeof(Vector2));
101 for (int v = 0; v < resZ; v++)
102 {
103 for (int u = 0; u < resX; u++)
104 {
105 texcoords[u + v*resX] = Vector2{ (float)u/(resX - 1), (float)v/(resZ - 1) };
106 }
107 }
108
109 // Triangles definition (indices)
110 int numFaces = (resX - 1)*(resZ - 1);
111 int *triangles = (int *)RL_MALLOC(numFaces*6*sizeof(int));
112 int t = 0;
113 for (int face = 0; face < numFaces; face++)
114 {
115 // Retrieve lower left corner from face ind
116 int i = face % (resX - 1) + (face/(resZ - 1)*resX);
117
118 triangles[t++] = i + resX;
119 triangles[t++] = i + 1;
120 triangles[t++] = i;
121
122 triangles[t++] = i + resX;
123 triangles[t++] = i + resX + 1;
124 triangles[t++] = i + 1;
125 }
126
127 mesh.vertexCount = vertexCount;
128 mesh.triangleCount = numFaces*2;
129 mesh.vertices = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
130 mesh.texcoords = (float *)RL_MALLOC(mesh.vertexCount*2*sizeof(float));
131 mesh.normals = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
132 mesh.indices = (unsigned short *)RL_MALLOC(mesh.triangleCount*3*sizeof(unsigned short));
133
134 // Mesh vertices position array
135 for (int i = 0; i < mesh.vertexCount; i++)
136 {
137 mesh.vertices[3*i] = vertices[i].x;
138 mesh.vertices[3*i + 1] = vertices[i].y;
139 mesh.vertices[3*i + 2] = vertices[i].z;
140 }
141
142 // Mesh texcoords array
143 for (int i = 0; i < mesh.vertexCount; i++)
144 {
145 mesh.texcoords[2*i] = texcoords[i].x * textureScale;
146 mesh.texcoords[2*i + 1] = texcoords[i].y * textureScale;
147 }
148
149 // Mesh normals array
150 for (int i = 0; i < mesh.vertexCount; i++)
151 {
152 mesh.normals[3*i] = normals[i].x;
153 mesh.normals[3*i + 1] = normals[i].y;
154 mesh.normals[3*i + 2] = normals[i].z;
155 }
156
157 // Mesh indices array initialization
158 for (int i = 0; i < mesh.triangleCount*3; i++) mesh.indices[i] = triangles[i];
159
160 RL_FREE(vertices);
161 RL_FREE(normals);
162 RL_FREE(texcoords);
163 RL_FREE(triangles);
164
165 // Upload vertex data to GPU (static mesh)
166 UploadMesh(&mesh, false);
167
168 return mesh;
169 }
170
174 static MeshUnmanaged Cube(float width, float height, float length) {
175 return ::GenMeshCube(width, height, length);
176 }
177
181 static MeshUnmanaged Sphere(float radius, int rings, int slices) {
182 return ::GenMeshSphere(radius, rings, slices);
183 }
184
188 static MeshUnmanaged HemiSphere(float radius, int rings, int slices) {
189 return ::GenMeshHemiSphere(radius, rings, slices);
190 }
191
195 static MeshUnmanaged Cylinder(float radius, float height, int slices) {
196 return ::GenMeshCylinder(radius, height, slices);
197 }
198
202 static MeshUnmanaged Cone(float radius, float height, int slices) {
203 return ::GenMeshCone(radius, height, slices);
204 }
205
209 static MeshUnmanaged Torus(float radius, float size, int radSeg, int sides) {
210 return ::GenMeshTorus(radius, size, radSeg, sides);
211 }
212
216 static MeshUnmanaged Knot(float radius, float size, int radSeg, int sides) {
217 return ::GenMeshKnot(radius, size, radSeg, sides);
218 }
219
223 static MeshUnmanaged Heightmap(const ::Image& heightmap, ::Vector3 size) {
224 return ::GenMeshHeightmap(heightmap, size);
225 }
226
230 static MeshUnmanaged Cubicmap(const ::Image& cubicmap, ::Vector3 cubeSize) {
231 return ::GenMeshCubicmap(cubicmap, cubeSize);
232 }
233
234 GETTERSETTER(int, VertexCount, vertexCount)
235 GETTERSETTER(int, TriangleCount, triangleCount)
236 GETTERSETTER(float*, Vertices, vertices)
237 GETTERSETTER(float*, TexCoords, texcoords)
238 GETTERSETTER(float*, TexCoords2, texcoords2)
239 GETTERSETTER(float*, Normals, normals)
240 GETTERSETTER(float*, Tangents, tangents)
241 GETTERSETTER(unsigned char*, Colors, colors)
242 GETTERSETTER(unsigned short*, Indices, indices) // NOLINT
243 GETTERSETTER(float*, AnimVertices, animVertices)
244 GETTERSETTER(float*, AnimNormals, animNormals)
245 GETTERSETTER(unsigned char*, BoneIds, boneIds)
246 GETTERSETTER(float*, BoneWeights, boneWeights)
247 GETTERSETTER(unsigned int, VaoId, vaoId)
248 GETTERSETTER(unsigned int*, VboId, vboId)
249
250 MeshUnmanaged& operator=(const ::Mesh& mesh) {
251 set(mesh);
252 return *this;
253 }
254
258 void Unload() {
259 if (vboId != nullptr) {
260 ::UnloadMesh(*this);
261 vboId = nullptr;
262 }
263 }
264
268 void Upload(bool dynamic = false) { ::UploadMesh(this, dynamic); }
269
273 void UpdateBuffer(int index, void* data, int dataSize, int offset = 0) {
274 ::UpdateMeshBuffer(*this, index, data, dataSize, offset);
275 }
276
280 void Draw(const ::Material& material, const ::Matrix& transform) const { ::DrawMesh(*this, material, transform); }
281
285 void Draw(const ::Material& material, ::Matrix* transforms, int instances) const {
286 ::DrawMeshInstanced(*this, material, transforms, instances);
287 }
288
294 void Export(std::string_view fileName) {
295 if (!::ExportMesh(*this, fileName.data())) {
296 throw RaylibException("Failed to export the Mesh");
297 }
298 }
299
305 void ExportCode(const std::string& fileName) {
306 if (!::ExportMeshAsCode(*this, fileName.c_str())) {
307 throw RaylibException("Failed to export the Mesh");
308 }
309 }
310
314 [[nodiscard]] raylib::BoundingBox BoundingBox() const { return ::GetMeshBoundingBox(*this); }
315
320 // Get min and max vertex to construct bounds (AABB)
321 raylib::Vector3 minVertex = { 0 };
322 raylib::Vector3 maxVertex = { 0 };
323
324 if (vertices != NULL)
325 {
326 minVertex = raylib::Vector3{ vertices[0], vertices[1], vertices[2] }.Transform(transform);
327 maxVertex = raylib::Vector3{ vertices[0], vertices[1], vertices[2] }.Transform(transform);
328
329 for (int i = 1; i < vertexCount; i++)
330 {
331 minVertex = Vector3Min(minVertex, raylib::Vector3{ vertices[i*3], vertices[i*3 + 1], vertices[i*3 + 2] }.Transform(transform));
332 maxVertex = Vector3Max(maxVertex, raylib::Vector3{ vertices[i*3], vertices[i*3 + 1], vertices[i*3 + 2] }.Transform(transform));
333 }
334 }
335
336 // Create the bounding box
337 raylib::BoundingBox box = {};
338 box.min = minVertex;
339 box.max = maxVertex;
340
341 return box;
342 }
343
348 ::GenMeshTangents(this);
349 return *this;
350 }
351
355 [[nodiscard]] raylib::Model LoadModelFrom() const { return ::LoadModelFromMesh(*this); }
356
360 operator raylib::Model() { return ::LoadModelFromMesh(*this); }
361
365 bool IsValid() { return ::IsModelValid(*this); }
366
367protected:
368 void set(const ::Mesh& mesh) {
369 vertexCount = mesh.vertexCount;
370 triangleCount = mesh.triangleCount;
371 vertices = mesh.vertices;
372 texcoords = mesh.texcoords;
373 texcoords2 = mesh.texcoords2;
374 normals = mesh.normals;
375 tangents = mesh.tangents;
376 colors = mesh.colors;
377 indices = mesh.indices;
378 animVertices = mesh.animVertices;
379 animNormals = mesh.animNormals;
380 boneIds = mesh.boneIds;
381 boneWeights = mesh.boneWeights;
382 boneMatrices = mesh.boneMatrices;
383 vaoId = mesh.vaoId;
384 vboId = mesh.vboId;
385 }
386};
387
389 BoundingBox bounds = {};
390
391 if (meshCount > 0)
392 {
393 Vector3 temp = { 0 };
394 bounds = (*(raylib::MeshUnmanaged*)(&meshes[0])).GetTransformedBoundingBox(transform);
395
396 for (int i = 1; i < meshCount; i++)
397 {
398 BoundingBox tempBounds = (*(raylib::MeshUnmanaged*)(&meshes[i])).GetTransformedBoundingBox(transform);
399
400 temp.x = (bounds.min.x < tempBounds.min.x)? bounds.min.x : tempBounds.min.x;
401 temp.y = (bounds.min.y < tempBounds.min.y)? bounds.min.y : tempBounds.min.y;
402 temp.z = (bounds.min.z < tempBounds.min.z)? bounds.min.z : tempBounds.min.z;
403 bounds.min = temp;
404
405 temp.x = (bounds.max.x > tempBounds.max.x)? bounds.max.x : tempBounds.max.x;
406 temp.y = (bounds.max.y > tempBounds.max.y)? bounds.max.y : tempBounds.max.y;
407 temp.z = (bounds.max.z > tempBounds.max.z)? bounds.max.z : tempBounds.max.z;
408 bounds.max = temp;
409 }
410 }
411
412 return bounds;
413}
414} // namespace raylib
415
417
418#endif // RAYLIB_CPP_INCLUDE_MESHUNMANAGED_HPP_
Bounding box type.
Definition: BoundingBox.hpp:11
Vertex data defining a mesh.
Definition: Mesh.hpp:23
Vertex data defining a mesh, not managed by C++ RAII.
static MeshUnmanaged Poly(int sides, float radius)
Load meshes from model file.
static MeshUnmanaged Cylinder(float radius, float height, int slices)
Generate cylinder mesh.
static MeshUnmanaged Heightmap(const ::Image &heightmap, ::Vector3 size)
Generate heightmap mesh from image data.
raylib::Model LoadModelFrom() const
Load model from generated mesh.
static MeshUnmanaged Plane(float width, float length, int resX, int resZ)
Generate plane mesh (with subdivisions)
static MeshUnmanaged Cubicmap(const ::Image &cubicmap, ::Vector3 cubeSize)
Generate cubes-based map mesh from image data.
void Upload(bool dynamic=false)
Upload mesh vertex data to GPU (VRAM)
bool IsValid()
Returns whether or not the Mesh is valid.
Mesh & GenTangents()
Compute mesh tangents.
static MeshUnmanaged Knot(float radius, float size, int radSeg, int sides)
Generate trefoil knot mesh.
raylib::BoundingBox GetTransformedBoundingBox(::Matrix transform) const
Compute mesh bounding box limits with respect to the given transformation.
static MeshUnmanaged Cone(float radius, float height, int slices)
Generate cone/pyramid mesh.
static MeshUnmanaged HemiSphere(float radius, int rings, int slices)
Generate half-sphere mesh (no bottom cap)
raylib::BoundingBox BoundingBox() const
Compute mesh bounding box limits.
MeshUnmanaged()
Default texture constructor.
void Unload()
Unload mesh from memory (RAM and/or VRAM)
static MeshUnmanaged Sphere(float radius, int rings, int slices)
Generate sphere mesh (standard sphere)
void ExportCode(const std::string &fileName)
Export mesh as code file (.h) defining multiple arrays of vertex attributes.
void UpdateBuffer(int index, void *data, int dataSize, int offset=0)
Upload mesh vertex data to GPU (VRAM)
void Export(std::string_view fileName)
Export mesh data to file.
static MeshUnmanaged Torus(float radius, float size, int radSeg, int sides)
Generate torus mesh.
void Draw(const ::Material &material, const ::Matrix &transform) const
Draw a 3d mesh with material and transform.
static MeshUnmanaged Cube(float width, float height, float length)
Generate cuboid mesh.
void Draw(const ::Material &material, ::Matrix *transforms, int instances) const
Draw multiple mesh instances with material and different transforms.
Model type.
Definition: Model.hpp:21
BoundingBox GetTransformedBoundingBox() const
Compute model bounding box limits with respect to the Model's transformation (considers all meshes) T...
Exception used for most raylib-related exceptions.
Vector3 type.
Definition: Vector3.hpp:20
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8