models_mesh_generation.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - mesh generation
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 1.8, last time updated with raylib 4.0
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2017-2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #define NUM_MODELS 9 // Parametric 3d shapes to generate
  17. //------------------------------------------------------------------------------------
  18. // Module Functions Declaration
  19. //------------------------------------------------------------------------------------
  20. static Mesh GenMeshCustom(void); // Generate a simple triangle mesh from code
  21. //------------------------------------------------------------------------------------
  22. // Program main entry point
  23. //------------------------------------------------------------------------------------
  24. int main(void)
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. const int screenWidth = 800;
  29. const int screenHeight = 450;
  30. InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh generation");
  31. // We generate a checked image for texturing
  32. Image checked = GenImageChecked(2, 2, 1, 1, RED, GREEN);
  33. Texture2D texture = LoadTextureFromImage(checked);
  34. UnloadImage(checked);
  35. Model models[NUM_MODELS] = { 0 };
  36. models[0] = LoadModelFromMesh(GenMeshPlane(2, 2, 4, 3));
  37. models[1] = LoadModelFromMesh(GenMeshCube(2.0f, 1.0f, 2.0f));
  38. models[2] = LoadModelFromMesh(GenMeshSphere(2, 32, 32));
  39. models[3] = LoadModelFromMesh(GenMeshHemiSphere(2, 16, 16));
  40. models[4] = LoadModelFromMesh(GenMeshCylinder(1, 2, 16));
  41. models[5] = LoadModelFromMesh(GenMeshTorus(0.25f, 4.0f, 16, 32));
  42. models[6] = LoadModelFromMesh(GenMeshKnot(1.0f, 2.0f, 16, 128));
  43. models[7] = LoadModelFromMesh(GenMeshPoly(5, 2.0f));
  44. models[8] = LoadModelFromMesh(GenMeshCustom());
  45. // NOTE: Generated meshes could be exported using ExportMesh()
  46. // Set checked texture as default diffuse component for all models material
  47. for (int i = 0; i < NUM_MODELS; i++) models[i].materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  48. // Define the camera to look into our 3d world
  49. Camera camera = { { 5.0f, 5.0f, 5.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  50. // Model drawing position
  51. Vector3 position = { 0.0f, 0.0f, 0.0f };
  52. int currentModel = 0;
  53. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  54. //--------------------------------------------------------------------------------------
  55. // Main game loop
  56. while (!WindowShouldClose()) // Detect window close button or ESC key
  57. {
  58. // Update
  59. //----------------------------------------------------------------------------------
  60. UpdateCamera(&camera, CAMERA_ORBITAL);
  61. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
  62. {
  63. currentModel = (currentModel + 1)%NUM_MODELS; // Cycle between the textures
  64. }
  65. if (IsKeyPressed(KEY_RIGHT))
  66. {
  67. currentModel++;
  68. if (currentModel >= NUM_MODELS) currentModel = 0;
  69. }
  70. else if (IsKeyPressed(KEY_LEFT))
  71. {
  72. currentModel--;
  73. if (currentModel < 0) currentModel = NUM_MODELS - 1;
  74. }
  75. //----------------------------------------------------------------------------------
  76. // Draw
  77. //----------------------------------------------------------------------------------
  78. BeginDrawing();
  79. ClearBackground(RAYWHITE);
  80. BeginMode3D(camera);
  81. DrawModel(models[currentModel], position, 1.0f, WHITE);
  82. DrawGrid(10, 1.0);
  83. EndMode3D();
  84. DrawRectangle(30, 400, 310, 30, Fade(SKYBLUE, 0.5f));
  85. DrawRectangleLines(30, 400, 310, 30, Fade(DARKBLUE, 0.5f));
  86. DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL MODELS", 40, 410, 10, BLUE);
  87. switch (currentModel)
  88. {
  89. case 0: DrawText("PLANE", 680, 10, 20, DARKBLUE); break;
  90. case 1: DrawText("CUBE", 680, 10, 20, DARKBLUE); break;
  91. case 2: DrawText("SPHERE", 680, 10, 20, DARKBLUE); break;
  92. case 3: DrawText("HEMISPHERE", 640, 10, 20, DARKBLUE); break;
  93. case 4: DrawText("CYLINDER", 680, 10, 20, DARKBLUE); break;
  94. case 5: DrawText("TORUS", 680, 10, 20, DARKBLUE); break;
  95. case 6: DrawText("KNOT", 680, 10, 20, DARKBLUE); break;
  96. case 7: DrawText("POLY", 680, 10, 20, DARKBLUE); break;
  97. case 8: DrawText("Custom (triangle)", 580, 10, 20, DARKBLUE); break;
  98. default: break;
  99. }
  100. EndDrawing();
  101. //----------------------------------------------------------------------------------
  102. }
  103. // De-Initialization
  104. //--------------------------------------------------------------------------------------
  105. UnloadTexture(texture); // Unload texture
  106. // Unload models data (GPU VRAM)
  107. for (int i = 0; i < NUM_MODELS; i++) UnloadModel(models[i]);
  108. CloseWindow(); // Close window and OpenGL context
  109. //--------------------------------------------------------------------------------------
  110. return 0;
  111. }
  112. //------------------------------------------------------------------------------------
  113. // Module Functions Definition
  114. //------------------------------------------------------------------------------------
  115. // Generate a simple triangle mesh from code
  116. static Mesh GenMeshCustom(void)
  117. {
  118. Mesh mesh = { 0 };
  119. mesh.triangleCount = 1;
  120. mesh.vertexCount = mesh.triangleCount*3;
  121. mesh.vertices = (float *)MemAlloc(mesh.vertexCount*3*sizeof(float)); // 3 vertices, 3 coordinates each (x, y, z)
  122. mesh.texcoords = (float *)MemAlloc(mesh.vertexCount*2*sizeof(float)); // 3 vertices, 2 coordinates each (x, y)
  123. mesh.normals = (float *)MemAlloc(mesh.vertexCount*3*sizeof(float)); // 3 vertices, 3 coordinates each (x, y, z)
  124. // Vertex at (0, 0, 0)
  125. mesh.vertices[0] = 0;
  126. mesh.vertices[1] = 0;
  127. mesh.vertices[2] = 0;
  128. mesh.normals[0] = 0;
  129. mesh.normals[1] = 1;
  130. mesh.normals[2] = 0;
  131. mesh.texcoords[0] = 0;
  132. mesh.texcoords[1] = 0;
  133. // Vertex at (1, 0, 2)
  134. mesh.vertices[3] = 1;
  135. mesh.vertices[4] = 0;
  136. mesh.vertices[5] = 2;
  137. mesh.normals[3] = 0;
  138. mesh.normals[4] = 1;
  139. mesh.normals[5] = 0;
  140. mesh.texcoords[2] = 0.5f;
  141. mesh.texcoords[3] = 1.0f;
  142. // Vertex at (2, 0, 0)
  143. mesh.vertices[6] = 2;
  144. mesh.vertices[7] = 0;
  145. mesh.vertices[8] = 0;
  146. mesh.normals[6] = 0;
  147. mesh.normals[7] = 1;
  148. mesh.normals[8] = 0;
  149. mesh.texcoords[4] = 1;
  150. mesh.texcoords[5] =0;
  151. // Upload mesh data from CPU (RAM) to GPU (VRAM) memory
  152. UploadMesh(&mesh, false);
  153. return mesh;
  154. }