From 7d81e673ed7f399305ae12f69267190ade93064d Mon Sep 17 00:00:00 2001 From: Ray Date: Sat, 15 Dec 2018 23:31:56 +0100 Subject: [PATCH] ADDED: GenMeshPoly() To generate 2D polygonal shape --- examples/models/models_mesh_generation.c | 4 +- src/models.c | 62 ++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/examples/models/models_mesh_generation.c b/examples/models/models_mesh_generation.c index c02bd91a..d64889bd 100644 --- a/examples/models/models_mesh_generation.c +++ b/examples/models/models_mesh_generation.c @@ -11,7 +11,7 @@ #include "raylib.h" -#define NUM_MODELS 7 // We generate 7 parametric 3d shapes +#define NUM_MODELS 8 // We generate 8 parametric 3d shapes int main() { @@ -36,6 +36,7 @@ int main() models[4] = LoadModelFromMesh(GenMeshCylinder(1, 2, 16)); models[5] = LoadModelFromMesh(GenMeshTorus(0.25f, 4.0f, 16, 32)); models[6] = LoadModelFromMesh(GenMeshKnot(1.0f, 2.0f, 16, 128)); + models[7] = LoadModelFromMesh(GenMeshPoly(5, 2.0f)); // Set checked texture as default diffuse component for all models material for (int i = 0; i < NUM_MODELS; i++) models[i].material.maps[MAP_DIFFUSE].texture = texture; @@ -93,6 +94,7 @@ int main() case 4: DrawText("CYLINDER", 680, 10, 20, DARKBLUE); break; case 5: DrawText("TORUS", 680, 10, 20, DARKBLUE); break; case 6: DrawText("KNOT", 680, 10, 20, DARKBLUE); break; + case 7: DrawText("POLY", 680, 10, 20, DARKBLUE); break; default: break; } diff --git a/src/models.c b/src/models.c index 0955b7b0..e030166a 100644 --- a/src/models.c +++ b/src/models.c @@ -715,6 +715,68 @@ void ExportMesh(Mesh mesh, const char *fileName) } #if defined(SUPPORT_MESH_GENERATION) +// Generate polygonal mesh +Mesh GenMeshPoly(int sides, float radius) +{ + Mesh mesh = { 0 }; + int vertexCount = sides*3; + + // Vertices definition + Vector3 *vertices = (Vector3 *)malloc(vertexCount*sizeof(Vector3)); + for (int i = 0, v = 0; i < 360; i += 360/sides, v += 3) + { + vertices[v] = (Vector3){ 0.0f, 0.0f, 0.0f }; + vertices[v + 1] = (Vector3){ sinf(DEG2RAD*i)*radius, 0.0f, cosf(DEG2RAD*i)*radius }; + vertices[v + 2] = (Vector3){ sinf(DEG2RAD*(i + 360/sides))*radius, 0.0f, cosf(DEG2RAD*(i + 360/sides))*radius }; + } + + // Normals definition + Vector3 *normals = (Vector3 *)malloc(vertexCount*sizeof(Vector3)); + for (int n = 0; n < vertexCount; n++) normals[n] = (Vector3){ 0.0f, 1.0f, 0.0f }; // Vector3.up; + + // TexCoords definition + Vector2 *texcoords = (Vector2 *)malloc(vertexCount*sizeof(Vector2)); + for (int n = 0; n < vertexCount; n++) texcoords[n] = (Vector2){ 0.0f, 0.0f }; + + mesh.vertexCount = vertexCount; + mesh.triangleCount = sides; + mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float)); + mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + + // Mesh vertices position array + for (int i = 0; i < mesh.vertexCount; i++) + { + mesh.vertices[3*i] = vertices[i].x; + mesh.vertices[3*i + 1] = vertices[i].y; + mesh.vertices[3*i + 2] = vertices[i].z; + } + + // Mesh texcoords array + for (int i = 0; i < mesh.vertexCount; i++) + { + mesh.texcoords[2*i] = texcoords[i].x; + mesh.texcoords[2*i + 1] = texcoords[i].y; + } + + // Mesh normals array + for (int i = 0; i < mesh.vertexCount; i++) + { + mesh.normals[3*i] = normals[i].x; + mesh.normals[3*i + 1] = normals[i].y; + mesh.normals[3*i + 2] = normals[i].z; + } + + free(vertices); + free(normals); + free(texcoords); + + // Upload vertex data to GPU (static mesh) + rlLoadMesh(&mesh, false); + + return mesh; +} + // Generate plane mesh (with subdivisions) Mesh GenMeshPlane(float width, float length, int resX, int resZ) {