Skip to content

Commit 9176e0a

Browse files
authored
Merge pull request #304 from furudbat/bugfix/299-double-free-mesh-model
Add MeshUnmanaged (#299)
2 parents e289c0e + e35ca6d commit 9176e0a

File tree

4 files changed

+286
-218
lines changed

4 files changed

+286
-218
lines changed

examples/models/models_first_person_maze.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ int main(void)
2525

2626
raylib::Image imMap("resources/cubicmap.png"); // Load cubicmap image (RAM)
2727
raylib::Texture cubicmap(imMap); // Convert image to texture to display (VRAM)
28-
Mesh mesh = raylib::Mesh::Cubicmap(imMap, Vector3{ 1.0f, 1.0f, 1.0f });
28+
raylib::MeshUnmanaged mesh = raylib::MeshUnmanaged::Cubicmap(imMap, Vector3{ 1.0f, 1.0f, 1.0f }); // Use MeshUnmanaged, Mesh will be handled by Model
2929
raylib::Model model(mesh);
3030

3131
// NOTE: By default each cube is mapped to one part of texture atlas

include/Mesh.hpp

Lines changed: 17 additions & 216 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,33 @@
88
#include "./raylib-cpp-utils.hpp"
99
#include "./BoundingBox.hpp"
1010
#include "./Model.hpp"
11+
#include "./MeshUnmanaged.hpp"
1112

1213
namespace raylib {
1314
/**
14-
* Vertex data definning a mesh
15+
* Vertex data defining a mesh
16+
*
17+
* The Mesh will be unloaded on object destruction.
18+
*
19+
* @see raylib::MeshUnmanaged
1520
*/
16-
class Mesh : public ::Mesh {
21+
class Mesh : public MeshUnmanaged {
1722
public:
18-
Mesh(const ::Mesh& mesh) {
19-
set(mesh);
20-
}
23+
using MeshUnmanaged::MeshUnmanaged;
2124

2225
/**
23-
* Load meshes from model file
26+
* Explicitly forbid the copy constructor.
2427
*/
25-
// static std::vector<Mesh> Load(const std::string& fileName) {
26-
// int count = 0;
27-
// ::Mesh* meshes = LoadMeshes(fileName.c_str(), &count);
28-
// return std::vector<Mesh>(meshes, meshes + count);
29-
// }
30-
3128
Mesh(const Mesh&) = delete;
3229

30+
/**
31+
* Explicitly forbid copy assignment.
32+
*/
33+
Mesh& operator=(const Mesh&) = delete;
34+
35+
/**
36+
* Move constructor.
37+
*/
3338
Mesh(Mesh&& other) {
3439
set(other);
3540

@@ -50,106 +55,6 @@ class Mesh : public ::Mesh {
5055
other.vboId = nullptr;
5156
}
5257

53-
/**
54-
* Generate polygonal mesh
55-
*/
56-
static ::Mesh Poly(int sides, float radius) {
57-
return ::GenMeshPoly(sides, radius);
58-
}
59-
60-
/**
61-
* Generate plane mesh (with subdivisions)
62-
*/
63-
static ::Mesh Plane(float width, float length, int resX, int resZ) {
64-
return ::GenMeshPlane(width, length, resX, resZ);
65-
}
66-
67-
/**
68-
* Generate cuboid mesh
69-
*/
70-
static ::Mesh Cube(float width, float height, float length) {
71-
return ::GenMeshCube(width, height, length);
72-
}
73-
74-
/**
75-
* Generate sphere mesh (standard sphere)
76-
*/
77-
static ::Mesh Sphere(float radius, int rings, int slices) {
78-
return ::GenMeshSphere(radius, rings, slices);
79-
}
80-
81-
/**
82-
* Generate half-sphere mesh (no bottom cap)
83-
*/
84-
static ::Mesh HemiSphere(float radius, int rings, int slices) {
85-
return ::GenMeshHemiSphere(radius, rings, slices);
86-
}
87-
88-
/**
89-
* Generate cylinder mesh
90-
*/
91-
static ::Mesh Cylinder(float radius, float height, int slices) {
92-
return ::GenMeshCylinder(radius, height, slices);
93-
}
94-
95-
/**
96-
* Generate cone/pyramid mesh
97-
*/
98-
static ::Mesh Cone(float radius, float height, int slices) {
99-
return ::GenMeshCone(radius, height, slices);
100-
}
101-
102-
/**
103-
* Generate torus mesh
104-
*/
105-
static ::Mesh Torus(float radius, float size, int radSeg, int sides) {
106-
return ::GenMeshTorus(radius, size, radSeg, sides);
107-
}
108-
109-
/**
110-
* Generate trefoil knot mesh
111-
*/
112-
static ::Mesh Knot(float radius, float size, int radSeg, int sides) {
113-
return ::GenMeshKnot(radius, size, radSeg, sides);
114-
}
115-
116-
/**
117-
* Generate heightmap mesh from image data
118-
*/
119-
static ::Mesh Heightmap(const ::Image& heightmap, ::Vector3 size) {
120-
return ::GenMeshHeightmap(heightmap, size);
121-
}
122-
123-
/**
124-
* Generate cubes-based map mesh from image data
125-
*/
126-
static ::Mesh Cubicmap(const ::Image& cubicmap, ::Vector3 cubeSize) {
127-
return ::GenMeshCubicmap(cubicmap, cubeSize);
128-
}
129-
130-
GETTERSETTER(int, VertexCount, vertexCount)
131-
GETTERSETTER(int, TriangleCount, triangleCount)
132-
GETTERSETTER(float*, Vertices, vertices)
133-
GETTERSETTER(float *, TexCoords, texcoords)
134-
GETTERSETTER(float *, TexCoords2, texcoords2)
135-
GETTERSETTER(float *, Normals, normals)
136-
GETTERSETTER(float *, Tangents, tangents)
137-
GETTERSETTER(unsigned char *, Colors, colors)
138-
GETTERSETTER(unsigned short *, Indices, indices) // NOLINT
139-
GETTERSETTER(float *, AnimVertices, animVertices)
140-
GETTERSETTER(float *, AnimNormals, animNormals)
141-
GETTERSETTER(unsigned char *, BoneIds, boneIds)
142-
GETTERSETTER(float *, BoneWeights, boneWeights)
143-
GETTERSETTER(unsigned int, VaoId, vaoId)
144-
GETTERSETTER(unsigned int *, VboId, vboId)
145-
146-
Mesh& operator=(const ::Mesh& mesh) {
147-
set(mesh);
148-
return *this;
149-
}
150-
151-
Mesh& operator=(const Mesh&) = delete;
152-
15358
Mesh& operator=(Mesh&& other) noexcept {
15459
if (this == &other) {
15560
return *this;
@@ -180,110 +85,6 @@ class Mesh : public ::Mesh {
18085
~Mesh() {
18186
Unload();
18287
}
183-
184-
/**
185-
* Upload mesh vertex data to GPU (VRAM)
186-
*/
187-
void Upload(bool dynamic = false) {
188-
::UploadMesh(this, dynamic);
189-
}
190-
191-
/**
192-
* Upload mesh vertex data to GPU (VRAM)
193-
*/
194-
void UpdateBuffer(int index, void *data, int dataSize, int offset = 0) {
195-
::UpdateMeshBuffer(*this, index, data, dataSize, offset);
196-
}
197-
198-
/**
199-
* Draw a 3d mesh with material and transform
200-
*/
201-
void Draw(const ::Material& material, const ::Matrix& transform) const {
202-
::DrawMesh(*this, material, transform);
203-
}
204-
205-
/**
206-
* Draw multiple mesh instances with material and different transforms
207-
*/
208-
void Draw(const ::Material& material, ::Matrix* transforms, int instances) const {
209-
::DrawMeshInstanced(*this, material, transforms, instances);
210-
}
211-
212-
/**
213-
* Export mesh data to file
214-
*
215-
* @throws raylib::RaylibException Throws if failed to export the Mesh.
216-
*/
217-
void Export(const std::string& fileName) {
218-
if (!::ExportMesh(*this, fileName.c_str())) {
219-
throw RaylibException("Failed to export the Mesh");
220-
}
221-
}
222-
223-
/**
224-
* Unload mesh from memory (RAM and/or VRAM)
225-
*/
226-
void Unload() {
227-
if (vboId != nullptr) {
228-
::UnloadMesh(*this);
229-
vboId = nullptr;
230-
}
231-
}
232-
233-
/**
234-
* Compute mesh bounding box limits
235-
*/
236-
raylib::BoundingBox BoundingBox() const {
237-
return ::GetMeshBoundingBox(*this);
238-
}
239-
240-
/**
241-
* Compute mesh bounding box limits
242-
*/
243-
operator raylib::BoundingBox() {
244-
return BoundingBox();
245-
}
246-
247-
/**
248-
* Compute mesh tangents
249-
*/
250-
Mesh& GenTangents() {
251-
::GenMeshTangents(this);
252-
return *this;
253-
}
254-
255-
/**
256-
* Load model from generated mesh
257-
*/
258-
raylib::Model LoadModelFrom() const {
259-
return ::LoadModelFromMesh(*this);
260-
}
261-
262-
/**
263-
* Load model from generated mesh
264-
*/
265-
operator raylib::Model() {
266-
return ::LoadModelFromMesh(*this);
267-
}
268-
269-
protected:
270-
void set(const ::Mesh& mesh) {
271-
vertexCount = mesh.vertexCount;
272-
triangleCount = mesh.triangleCount;
273-
vertices = mesh.vertices;
274-
texcoords = mesh.texcoords;
275-
texcoords2 = mesh.texcoords2;
276-
normals = mesh.normals;
277-
tangents = mesh.tangents;
278-
colors = mesh.colors;
279-
indices = mesh.indices;
280-
animVertices = mesh.animVertices;
281-
animNormals = mesh.animNormals;
282-
boneIds = mesh.boneIds;
283-
boneWeights = mesh.boneWeights;
284-
vaoId = mesh.vaoId;
285-
vboId = mesh.vboId;
286-
}
28788
};
28889
} // namespace raylib
28990

0 commit comments

Comments
 (0)