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 "./raylib.hpp"
8#include "./raylib-cpp-utils.hpp"
9#include "./BoundingBox.hpp"
10#include "./Model.hpp"
11
12namespace raylib {
13
21class MeshUnmanaged : public ::Mesh {
22 public:
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 vaoId = 0;
41 vboId = nullptr;
42 }
43
44 MeshUnmanaged(const ::Mesh& mesh) {
45 set(mesh);
46 }
47
48 MeshUnmanaged(::Mesh&& mesh) {
49 set(mesh);
50 }
51
55 // static std::vector<Mesh> Load(std::string_view fileName) {
56 // int count = 0;
57 // ::Mesh* meshes = LoadMeshes(fileName.c_str(), &count);
58 // return std::vector<Mesh>(meshes, meshes + count);
59 // }
60
64 static MeshUnmanaged Poly(int sides, float radius) {
65 return ::GenMeshPoly(sides, radius);
66 }
67
71 static MeshUnmanaged Plane(float width, float length, int resX, int resZ) {
72 return ::GenMeshPlane(width, length, resX, resZ);
73 }
74
75 static MeshUnmanaged Plane(float width, float length, int resX, int resZ, float textureScale) {
76 ::Mesh mesh = {};
77
78 resX++;
79 resZ++;
80
81 // Vertices definition
82 int vertexCount = resX*resZ; // vertices get reused for the faces
83
84 Vector3 *vertices = (Vector3 *)RL_MALLOC(vertexCount*sizeof(Vector3));
85 for (int z = 0; z < resZ; z++)
86 {
87 // [-length/2, length/2]
88 float zPos = ((float)z/(resZ - 1) - 0.5f)*length;
89 for (int x = 0; x < resX; x++)
90 {
91 // [-width/2, width/2]
92 float xPos = ((float)x/(resX - 1) - 0.5f)*width;
93 vertices[x + z*resX] = (Vector3){ xPos, 0.0f, zPos };
94 }
95 }
96
97 // Normals definition
98 Vector3 *normals = (Vector3 *)RL_MALLOC(vertexCount*sizeof(Vector3));
99 for (int n = 0; n < vertexCount; n++) normals[n] = (Vector3){ 0.0f, 1.0f, 0.0f }; // Vector3.up;
100
101 // TexCoords definition
102 Vector2 *texcoords = (Vector2 *)RL_MALLOC(vertexCount*sizeof(Vector2));
103 for (int v = 0; v < resZ; v++)
104 {
105 for (int u = 0; u < resX; u++)
106 {
107 texcoords[u + v*resX] = (Vector2){ (float)u/(resX - 1), (float)v/(resZ - 1) };
108 }
109 }
110
111 // Triangles definition (indices)
112 int numFaces = (resX - 1)*(resZ - 1);
113 int *triangles = (int *)RL_MALLOC(numFaces*6*sizeof(int));
114 int t = 0;
115 for (int face = 0; face < numFaces; face++)
116 {
117 // Retrieve lower left corner from face ind
118 int i = face % (resX - 1) + (face/(resZ - 1)*resX);
119
120 triangles[t++] = i + resX;
121 triangles[t++] = i + 1;
122 triangles[t++] = i;
123
124 triangles[t++] = i + resX;
125 triangles[t++] = i + resX + 1;
126 triangles[t++] = i + 1;
127 }
128
129 mesh.vertexCount = vertexCount;
130 mesh.triangleCount = numFaces*2;
131 mesh.vertices = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
132 mesh.texcoords = (float *)RL_MALLOC(mesh.vertexCount*2*sizeof(float));
133 mesh.normals = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
134 mesh.indices = (unsigned short *)RL_MALLOC(mesh.triangleCount*3*sizeof(unsigned short));
135
136 // Mesh vertices position array
137 for (int i = 0; i < mesh.vertexCount; i++)
138 {
139 mesh.vertices[3*i] = vertices[i].x;
140 mesh.vertices[3*i + 1] = vertices[i].y;
141 mesh.vertices[3*i + 2] = vertices[i].z;
142 }
143
144 // Mesh texcoords array
145 for (int i = 0; i < mesh.vertexCount; i++)
146 {
147 mesh.texcoords[2*i] = texcoords[i].x * textureScale;
148 mesh.texcoords[2*i + 1] = texcoords[i].y * textureScale;
149 }
150
151 // Mesh normals array
152 for (int i = 0; i < mesh.vertexCount; i++)
153 {
154 mesh.normals[3*i] = normals[i].x;
155 mesh.normals[3*i + 1] = normals[i].y;
156 mesh.normals[3*i + 2] = normals[i].z;
157 }
158
159 // Mesh indices array initialization
160 for (int i = 0; i < mesh.triangleCount*3; i++) mesh.indices[i] = triangles[i];
161
162 RL_FREE(vertices);
163 RL_FREE(normals);
164 RL_FREE(texcoords);
165 RL_FREE(triangles);
166
167 // Upload vertex data to GPU (static mesh)
168 UploadMesh(&mesh, false);
169
170 return mesh;
171 }
172
176 static MeshUnmanaged Cube(float width, float height, float length) {
177 return ::GenMeshCube(width, height, length);
178 }
179
183 static MeshUnmanaged Sphere(float radius, int rings, int slices) {
184 return ::GenMeshSphere(radius, rings, slices);
185 }
186
190 static MeshUnmanaged HemiSphere(float radius, int rings, int slices) {
191 return ::GenMeshHemiSphere(radius, rings, slices);
192 }
193
197 static MeshUnmanaged Cylinder(float radius, float height, int slices) {
198 return ::GenMeshCylinder(radius, height, slices);
199 }
200
204 static MeshUnmanaged Cone(float radius, float height, int slices) {
205 return ::GenMeshCone(radius, height, slices);
206 }
207
211 static MeshUnmanaged Torus(float radius, float size, int radSeg, int sides) {
212 return ::GenMeshTorus(radius, size, radSeg, sides);
213 }
214
218 static MeshUnmanaged Knot(float radius, float size, int radSeg, int sides) {
219 return ::GenMeshKnot(radius, size, radSeg, sides);
220 }
221
225 static MeshUnmanaged Heightmap(const ::Image& heightmap, ::Vector3 size) {
226 return ::GenMeshHeightmap(heightmap, size);
227 }
228
232 static MeshUnmanaged Cubicmap(const ::Image& cubicmap, ::Vector3 cubeSize) {
233 return ::GenMeshCubicmap(cubicmap, cubeSize);
234 }
235
236 GETTERSETTER(int, VertexCount, vertexCount)
237 GETTERSETTER(int, TriangleCount, triangleCount)
238 GETTERSETTER(float*, Vertices, vertices)
239 GETTERSETTER(float *, TexCoords, texcoords)
240 GETTERSETTER(float *, TexCoords2, texcoords2)
241 GETTERSETTER(float *, Normals, normals)
242 GETTERSETTER(float *, Tangents, tangents)
243 GETTERSETTER(unsigned char *, Colors, colors)
244 GETTERSETTER(unsigned short *, Indices, indices) // NOLINT
245 GETTERSETTER(float *, AnimVertices, animVertices)
246 GETTERSETTER(float *, AnimNormals, animNormals)
247 GETTERSETTER(unsigned char *, BoneIds, boneIds)
248 GETTERSETTER(float *, BoneWeights, boneWeights)
249 GETTERSETTER(unsigned int, VaoId, vaoId)
250 GETTERSETTER(unsigned int *, VboId, vboId)
251
252 MeshUnmanaged& operator=(const ::Mesh& mesh) {
253 set(mesh);
254 return *this;
255 }
256
260 void Unload() {
261 if (vboId != nullptr) {
262 ::UnloadMesh(*this);
263 vboId = nullptr;
264 }
265 }
266
270 void Upload(bool dynamic = false) {
271 ::UploadMesh(this, dynamic);
272 }
273
277 void UpdateBuffer(int index, void *data, int dataSize, int offset = 0) {
278 ::UpdateMeshBuffer(*this, index, data, dataSize, offset);
279 }
280
284 void Draw(const ::Material& material, const ::Matrix& transform) const {
285 ::DrawMesh(*this, material, transform);
286 }
287
291 void Draw(const ::Material& material, ::Matrix* transforms, int instances) const {
292 ::DrawMeshInstanced(*this, material, transforms, instances);
293 }
294
300 void Export(std::string_view fileName) {
301 if (!::ExportMesh(*this, fileName.data())) {
302 throw RaylibException("Failed to export the Mesh");
303 }
304 }
305
310 return ::GetMeshBoundingBox(*this);
311 }
312
317 return BoundingBox();
318 }
319
324 // Get min and max vertex to construct bounds (AABB)
325 raylib::Vector3 minVertex = { 0 };
326 raylib::Vector3 maxVertex = { 0 };
327
328 if (vertices != NULL)
329 {
330 minVertex = raylib::Vector3{ vertices[0], vertices[1], vertices[2] }.Transform(transform);
331 maxVertex = raylib::Vector3{ vertices[0], vertices[1], vertices[2] }.Transform(transform);
332
333 for (int i = 1; i < vertexCount; i++)
334 {
335 minVertex = Vector3Min(minVertex, raylib::Vector3{ vertices[i*3], vertices[i*3 + 1], vertices[i*3 + 2] }.Transform(transform));
336 maxVertex = Vector3Max(maxVertex, raylib::Vector3{ vertices[i*3], vertices[i*3 + 1], vertices[i*3 + 2] }.Transform(transform));
337 }
338 }
339
340 // Create the bounding box
341 raylib::BoundingBox box = {};
342 box.min = minVertex;
343 box.max = maxVertex;
344
345 return box;
346 }
347
352 ::GenMeshTangents(this);
353 return *this;
354 }
355
360 return ::LoadModelFromMesh(*this);
361 }
362
366 operator raylib::Model() {
367 return ::LoadModelFromMesh(*this);
368 }
369
370 protected:
371 void set(const ::Mesh& mesh) {
372 vertexCount = mesh.vertexCount;
373 triangleCount = mesh.triangleCount;
374 vertices = mesh.vertices;
375 texcoords = mesh.texcoords;
376 texcoords2 = mesh.texcoords2;
377 normals = mesh.normals;
378 tangents = mesh.tangents;
379 colors = mesh.colors;
380 indices = mesh.indices;
381 animVertices = mesh.animVertices;
382 animNormals = mesh.animNormals;
383 boneIds = mesh.boneIds;
384 boneWeights = mesh.boneWeights;
385 vaoId = mesh.vaoId;
386 vboId = mesh.vboId;
387 }
388};
389
391 BoundingBox bounds = {};
392
393 if (meshCount > 0)
394 {
395 Vector3 temp = { 0 };
396 bounds = (*(raylib::MeshUnmanaged*)(&meshes[0])).GetTransformedBoundingBox(transform);
397
398 for (int i = 1; i < meshCount; i++)
399 {
400 BoundingBox tempBounds = (*(raylib::MeshUnmanaged*)(&meshes[i])).GetTransformedBoundingBox(transform);
401
402 temp.x = (bounds.min.x < tempBounds.min.x)? bounds.min.x : tempBounds.min.x;
403 temp.y = (bounds.min.y < tempBounds.min.y)? bounds.min.y : tempBounds.min.y;
404 temp.z = (bounds.min.z < tempBounds.min.z)? bounds.min.z : tempBounds.min.z;
405 bounds.min = temp;
406
407 temp.x = (bounds.max.x > tempBounds.max.x)? bounds.max.x : tempBounds.max.x;
408 temp.y = (bounds.max.y > tempBounds.max.y)? bounds.max.y : tempBounds.max.y;
409 temp.z = (bounds.max.z > tempBounds.max.z)? bounds.max.z : tempBounds.max.z;
410 bounds.max = temp;
411 }
412 }
413
414 return bounds;
415}
416} // namespace raylib
417
419
420#endif // RAYLIB_CPP_INCLUDE_MESHUNMANAGED_HPP_
Bounding box type.
Definition: BoundingBox.hpp:12
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)
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 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:19
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:19