Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

190 řádky
7.1 KiB

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