1#ifndef RAYLIB_CPP_INCLUDE_MESHUNMANAGED_HPP_
2#define RAYLIB_CPP_INCLUDE_MESHUNMANAGED_HPP_
8#include "./raylib-cpp-utils.hpp"
9#include "./BoundingBox.hpp"
36 animVertices =
nullptr;
37 animNormals =
nullptr;
39 boneWeights =
nullptr;
65 return ::GenMeshPoly(sides, radius);
72 return ::GenMeshPlane(width, length, resX, resZ);
75 static MeshUnmanaged Plane(
float width,
float length,
int resX,
int resZ,
float textureScale) {
82 int vertexCount = resX*resZ;
84 Vector3 *vertices = (Vector3 *)RL_MALLOC(vertexCount*
sizeof(Vector3));
85 for (
int z = 0; z < resZ; z++)
88 float zPos = ((float)z/(resZ - 1) - 0.5f)*length;
89 for (
int x = 0; x < resX; x++)
92 float xPos = ((float)x/(resX - 1) - 0.5f)*width;
93 vertices[x + z*resX] = (Vector3){ xPos, 0.0f, zPos };
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 };
102 Vector2 *texcoords = (Vector2 *)RL_MALLOC(vertexCount*
sizeof(Vector2));
103 for (
int v = 0; v < resZ; v++)
105 for (
int u = 0; u < resX; u++)
107 texcoords[u + v*resX] = (Vector2){ (float)u/(resX - 1), (float)v/(resZ - 1) };
112 int numFaces = (resX - 1)*(resZ - 1);
113 int *triangles = (
int *)RL_MALLOC(numFaces*6*
sizeof(
int));
115 for (
int face = 0; face < numFaces; face++)
118 int i = face % (resX - 1) + (face/(resZ - 1)*resX);
120 triangles[t++] = i + resX;
121 triangles[t++] = i + 1;
124 triangles[t++] = i + resX;
125 triangles[t++] = i + resX + 1;
126 triangles[t++] = i + 1;
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));
137 for (
int i = 0; i < mesh.vertexCount; i++)
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;
145 for (
int i = 0; i < mesh.vertexCount; i++)
147 mesh.texcoords[2*i] = texcoords[i].x * textureScale;
148 mesh.texcoords[2*i + 1] = texcoords[i].y * textureScale;
152 for (
int i = 0; i < mesh.vertexCount; i++)
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;
160 for (
int i = 0; i < mesh.triangleCount*3; i++) mesh.indices[i] = triangles[i];
168 UploadMesh(&mesh,
false);
177 return ::GenMeshCube(width, height, length);
184 return ::GenMeshSphere(radius, rings, slices);
191 return ::GenMeshHemiSphere(radius, rings, slices);
198 return ::GenMeshCylinder(radius, height, slices);
205 return ::GenMeshCone(radius, height, slices);
212 return ::GenMeshTorus(radius, size, radSeg, sides);
219 return ::GenMeshKnot(radius, size, radSeg, sides);
226 return ::GenMeshHeightmap(heightmap, size);
233 return ::GenMeshCubicmap(cubicmap, cubeSize);
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)
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)
261 if (vboId !=
nullptr) {
271 ::UploadMesh(
this, dynamic);
277 void UpdateBuffer(
int index,
void *data,
int dataSize,
int offset = 0) {
278 ::UpdateMeshBuffer(*
this, index, data, dataSize, offset);
284 void Draw(const ::Material& material, const ::Matrix& transform)
const {
285 ::DrawMesh(*
this, material, transform);
291 void Draw(const ::Material& material, ::Matrix* transforms,
int instances)
const {
292 ::DrawMeshInstanced(*
this, material, transforms, instances);
301 if (!::ExportMesh(*
this, fileName.data())) {
310 return ::GetMeshBoundingBox(*
this);
328 if (vertices != NULL)
330 minVertex =
raylib::Vector3{ vertices[0], vertices[1], vertices[2] }.Transform(transform);
331 maxVertex =
raylib::Vector3{ vertices[0], vertices[1], vertices[2] }.Transform(transform);
333 for (
int i = 1; i < vertexCount; i++)
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));
352 ::GenMeshTangents(
this);
360 return ::LoadModelFromMesh(*
this);
367 return ::LoadModelFromMesh(*
this);
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;
398 for (
int i = 1; i < meshCount; i++)
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;
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;
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)
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.
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.