models_mesh_generation.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*******************************************************************************************
  2. *
  3. * raylib example - procedural mesh generation
  4. *
  5. * This example has been created using raylib 1.8 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2017-2021 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #define NUM_MODELS 9 // Parametric 3d shapes to generate
  13. static Mesh GenMeshCustom(void); // Generate a simple triangle mesh from code
  14. //------------------------------------------------------------------------------------
  15. // Program main entry point
  16. //------------------------------------------------------------------------------------
  17. int main(void)
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. const int screenWidth = 800;
  22. const int screenHeight = 450;
  23. InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh generation");
  24. // We generate a checked image for texturing
  25. Image checked = GenImageChecked(2, 2, 1, 1, RED, GREEN);
  26. Texture2D texture = LoadTextureFromImage(checked);
  27. UnloadImage(checked);
  28. Model models[NUM_MODELS] = { 0 };
  29. models[0] = LoadModelFromMesh(GenMeshPlane(2, 2, 5, 5));
  30. models[1] = LoadModelFromMesh(GenMeshCube(2.0f, 1.0f, 2.0f));
  31. models[2] = LoadModelFromMesh(GenMeshSphere(2, 32, 32));
  32. models[3] = LoadModelFromMesh(GenMeshHemiSphere(2, 16, 16));
  33. models[4] = LoadModelFromMesh(GenMeshCylinder(1, 2, 16));
  34. models[5] = LoadModelFromMesh(GenMeshTorus(0.25f, 4.0f, 16, 32));
  35. models[6] = LoadModelFromMesh(GenMeshKnot(1.0f, 2.0f, 16, 128));
  36. models[7] = LoadModelFromMesh(GenMeshPoly(5, 2.0f));
  37. models[8] = LoadModelFromMesh(GenMeshCustom());
  38. // Generated meshes could be exported as .obj files
  39. //ExportMesh(models[0].meshes[0], "plane.obj");
  40. //ExportMesh(models[1].meshes[0], "cube.obj");
  41. //ExportMesh(models[2].meshes[0], "sphere.obj");
  42. //ExportMesh(models[3].meshes[0], "hemisphere.obj");
  43. //ExportMesh(models[4].meshes[0], "cylinder.obj");
  44. //ExportMesh(models[5].meshes[0], "torus.obj");
  45. //ExportMesh(models[6].meshes[0], "knot.obj");
  46. //ExportMesh(models[7].meshes[0], "poly.obj");
  47. //ExportMesh(models[8].meshes[0], "custom.obj");
  48. // Set checked texture as default diffuse component for all models material
  49. for (int i = 0; i < NUM_MODELS; i++) models[i].materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  50. // Define the camera to look into our 3d world
  51. Camera camera = { { 5.0f, 5.0f, 5.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  52. // Model drawing position
  53. Vector3 position = { 0.0f, 0.0f, 0.0f };
  54. int currentModel = 0;
  55. SetCameraMode(camera, CAMERA_ORBITAL); // Set a orbital camera mode
  56. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  57. //--------------------------------------------------------------------------------------
  58. // Main game loop
  59. while (!WindowShouldClose()) // Detect window close button or ESC key
  60. {
  61. // Update
  62. //----------------------------------------------------------------------------------
  63. UpdateCamera(&camera); // Update internal camera and our camera
  64. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
  65. {
  66. currentModel = (currentModel + 1)%NUM_MODELS; // Cycle between the textures
  67. }
  68. if (IsKeyPressed(KEY_RIGHT))
  69. {
  70. currentModel++;
  71. if (currentModel >= NUM_MODELS) currentModel = 0;
  72. }
  73. else if (IsKeyPressed(KEY_LEFT))
  74. {
  75. currentModel--;
  76. if (currentModel < 0) currentModel = NUM_MODELS - 1;
  77. }
  78. //----------------------------------------------------------------------------------
  79. // Draw
  80. //----------------------------------------------------------------------------------
  81. BeginDrawing();
  82. ClearBackground(RAYWHITE);
  83. BeginMode3D(camera);
  84. DrawModel(models[currentModel], position, 1.0f, WHITE);
  85. DrawGrid(10, 1.0);
  86. EndMode3D();
  87. DrawRectangle(30, 400, 310, 30, Fade(SKYBLUE, 0.5f));
  88. DrawRectangleLines(30, 400, 310, 30, Fade(DARKBLUE, 0.5f));
  89. DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL MODELS", 40, 410, 10, BLUE);
  90. switch(currentModel)
  91. {
  92. case 0: DrawText("PLANE", 680, 10, 20, DARKBLUE); break;
  93. case 1: DrawText("CUBE", 680, 10, 20, DARKBLUE); break;
  94. case 2: DrawText("SPHERE", 680, 10, 20, DARKBLUE); break;
  95. case 3: DrawText("HEMISPHERE", 640, 10, 20, DARKBLUE); break;
  96. case 4: DrawText("CYLINDER", 680, 10, 20, DARKBLUE); break;
  97. case 5: DrawText("TORUS", 680, 10, 20, DARKBLUE); break;
  98. case 6: DrawText("KNOT", 680, 10, 20, DARKBLUE); break;
  99. case 7: DrawText("POLY", 680, 10, 20, DARKBLUE); break;
  100. case 8: DrawText("Custom (triangle)", 580, 10, 20, DARKBLUE); break;
  101. default: break;
  102. }
  103. EndDrawing();
  104. //----------------------------------------------------------------------------------
  105. }
  106. // De-Initialization
  107. //--------------------------------------------------------------------------------------
  108. UnloadTexture(texture); // Unload texture
  109. // Unload models data (GPU VRAM)
  110. for (int i = 0; i < NUM_MODELS; i++) UnloadModel(models[i]);
  111. CloseWindow(); // Close window and OpenGL context
  112. //--------------------------------------------------------------------------------------
  113. return 0;
  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. }