1#ifndef RAYLIB_CPP_INCLUDE_MESHUNMANAGED_HPP_
2#define RAYLIB_CPP_INCLUDE_MESHUNMANAGED_HPP_
7#include "./BoundingBox.hpp"
10#include "./raylib-cpp-utils.hpp"
36 animVertices =
nullptr;
37 animNormals =
nullptr;
39 boneWeights =
nullptr;
40 boneMatrices =
nullptr;
63 return ::GenMeshPoly(sides, radius);
70 return ::GenMeshPlane(width, length, resX, resZ);
73 static MeshUnmanaged Plane(
float width,
float length,
int resX,
int resZ,
float textureScale) {
80 int vertexCount = resX*resZ;
82 Vector3 *vertices = (Vector3 *)RL_MALLOC(vertexCount*
sizeof(Vector3));
83 for (
int z = 0; z < resZ; z++)
86 float zPos = ((float)z/(resZ - 1) - 0.5f)*length;
87 for (
int x = 0; x < resX; x++)
90 float xPos = ((float)x/(resX - 1) - 0.5f)*width;
91 vertices[x + z*resX] = Vector3{ xPos, 0.0f, zPos };
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 };
100 Vector2 *texcoords = (Vector2 *)RL_MALLOC(vertexCount*
sizeof(Vector2));
101 for (
int v = 0; v < resZ; v++)
103 for (
int u = 0; u < resX; u++)
105 texcoords[u + v*resX] = Vector2{ (float)u/(resX - 1), (float)v/(resZ - 1) };
110 int numFaces = (resX - 1)*(resZ - 1);
111 int *triangles = (
int *)RL_MALLOC(numFaces*6*
sizeof(
int));
113 for (
int face = 0; face < numFaces; face++)
116 int i = face % (resX - 1) + (face/(resZ - 1)*resX);
118 triangles[t++] = i + resX;
119 triangles[t++] = i + 1;
122 triangles[t++] = i + resX;
123 triangles[t++] = i + resX + 1;
124 triangles[t++] = i + 1;
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));
135 for (
int i = 0; i < mesh.vertexCount; i++)
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;
143 for (
int i = 0; i < mesh.vertexCount; i++)
145 mesh.texcoords[2*i] = texcoords[i].x * textureScale;
146 mesh.texcoords[2*i + 1] = texcoords[i].y * textureScale;
150 for (
int i = 0; i < mesh.vertexCount; i++)
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;
158 for (
int i = 0; i < mesh.triangleCount*3; i++) mesh.indices[i] = triangles[i];
166 UploadMesh(&mesh,
false);
175 return ::GenMeshCube(width, height, length);
182 return ::GenMeshSphere(radius, rings, slices);
189 return ::GenMeshHemiSphere(radius, rings, slices);
196 return ::GenMeshCylinder(radius, height, slices);
203 return ::GenMeshCone(radius, height, slices);
210 return ::GenMeshTorus(radius, size, radSeg, sides);
217 return ::GenMeshKnot(radius, size, radSeg, sides);
224 return ::GenMeshHeightmap(heightmap, size);
231 return ::GenMeshCubicmap(cubicmap, cubeSize);
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)
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)
259 if (vboId !=
nullptr) {
268 void Upload(
bool dynamic =
false) { ::UploadMesh(
this, dynamic); }
273 void UpdateBuffer(
int index,
void* data,
int dataSize,
int offset = 0) {
274 ::UpdateMeshBuffer(*
this, index, data, dataSize, offset);
280 void Draw(const ::Material& material, const ::Matrix& transform)
const { ::DrawMesh(*
this, material, transform); }
285 void Draw(const ::Material& material, ::Matrix* transforms,
int instances)
const {
286 ::DrawMeshInstanced(*
this, material, transforms, instances);
295 if (!::ExportMesh(*
this, fileName.data())) {
306 if (!::ExportMeshAsCode(*
this, fileName.c_str())) {
324 if (vertices != NULL)
326 minVertex =
raylib::Vector3{ vertices[0], vertices[1], vertices[2] }.Transform(transform);
327 maxVertex =
raylib::Vector3{ vertices[0], vertices[1], vertices[2] }.Transform(transform);
329 for (
int i = 1; i < vertexCount; i++)
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));
348 ::GenMeshTangents(
this);
365 bool IsValid() { return ::IsModelValid(*
this); }
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;
396 for (
int i = 1; i < meshCount; i++)
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;
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;
Vertex data defining a mesh.
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.
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.
All raylib-cpp classes and functions appear in the raylib namespace.