Skip to content

Commit b663724

Browse files
authored
Generate a mesh in client code. (#1735)
1 parent bb33033 commit b663724

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

examples/models/models_mesh_generation.c

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,58 @@
1111

1212
#include "raylib.h"
1313

14-
#define NUM_MODELS 8 // Parametric 3d shapes to generate
14+
#define NUM_MODELS 9 // Parametric 3d shapes to generate
15+
16+
void AllocateMeshData(Mesh* mesh, int triangleCount)
17+
{
18+
mesh->vertexCount = triangleCount * 3;
19+
mesh->triangleCount = triangleCount;
20+
21+
mesh->vertices = (float*)MemAlloc(mesh->vertexCount * 3 * sizeof(float));
22+
mesh->texcoords = (float*)MemAlloc(mesh->vertexCount * 2 * sizeof(float));
23+
mesh->normals = (float*)MemAlloc(mesh->vertexCount * 3 * sizeof(float));
24+
}
25+
26+
// generate a simple triangle mesh from code
27+
Mesh MakeMesh()
28+
{
29+
Mesh mesh = { 0 };
30+
AllocateMeshData(&mesh, 1);
31+
32+
// vertex at the origin
33+
mesh.vertices[0] = 0;
34+
mesh.vertices[1] = 0;
35+
mesh.vertices[2] = 0;
36+
mesh.normals[0] = 0;
37+
mesh.normals[1] = 1;
38+
mesh.normals[2] = 0;
39+
mesh.texcoords[0] = 0;
40+
mesh.texcoords[1] = 0;
41+
42+
// vertex at 1,0,2
43+
mesh.vertices[3] = 1;
44+
mesh.vertices[4] = 0;
45+
mesh.vertices[5] = 2;
46+
mesh.normals[3] = 0;
47+
mesh.normals[4] = 1;
48+
mesh.normals[5] = 0;
49+
mesh.texcoords[2] = 0.5f;
50+
mesh.texcoords[3] = 1.0f;
51+
52+
// vertex at 2,0,0
53+
mesh.vertices[6] = 2;
54+
mesh.vertices[7] = 0;
55+
mesh.vertices[8] = 0;
56+
mesh.normals[6] = 0;
57+
mesh.normals[7] = 1;
58+
mesh.normals[8] = 0;
59+
mesh.texcoords[4] = 1;
60+
mesh.texcoords[5] =0;
61+
62+
UploadMesh(&mesh, false);
63+
64+
return mesh;
65+
}
1566

1667
int main(void)
1768
{
@@ -37,6 +88,7 @@ int main(void)
3788
models[5] = LoadModelFromMesh(GenMeshTorus(0.25f, 4.0f, 16, 32));
3889
models[6] = LoadModelFromMesh(GenMeshKnot(1.0f, 2.0f, 16, 128));
3990
models[7] = LoadModelFromMesh(GenMeshPoly(5, 2.0f));
91+
models[8] = LoadModelFromMesh(MakeMesh());
4092

4193
// Set checked texture as default diffuse component for all models material
4294
for (int i = 0; i < NUM_MODELS; i++) models[i].materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
@@ -86,9 +138,8 @@ int main(void)
86138

87139
BeginMode3D(camera);
88140

89-
DrawModel(models[currentModel], position, 1.0f, WHITE);
90-
91-
DrawGrid(10, 1.0);
141+
DrawModel(models[currentModel], position, 1.0f, WHITE);
142+
DrawGrid(10, 1.0);
92143

93144
EndMode3D();
94145

@@ -106,6 +157,7 @@ int main(void)
106157
case 5: DrawText("TORUS", 680, 10, 20, DARKBLUE); break;
107158
case 6: DrawText("KNOT", 680, 10, 20, DARKBLUE); break;
108159
case 7: DrawText("POLY", 680, 10, 20, DARKBLUE); break;
160+
case 8: DrawText("Parametric(custom)", 580, 10, 20, DARKBLUE); break;
109161
default: break;
110162
}
111163

0 commit comments

Comments
 (0)