You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
4.4 KiB

  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 Ramon Santamaria (Ray San)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #define NUM_MODELS 7 // We generate 7 parametric 3d shapes
  13. int main()
  14. {
  15. // Initialization
  16. //--------------------------------------------------------------------------------------
  17. int screenWidth = 800;
  18. int screenHeight = 450;
  19. InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh generation");
  20. // We generate a checked image for texturing
  21. Image checked = GenImageChecked(2, 2, 1, 1, RED, GREEN);
  22. Texture2D texture = LoadTextureFromImage(checked);
  23. UnloadImage(checked);
  24. Model models[NUM_MODELS];
  25. models[0] = LoadModelFromMesh(GenMeshPlane(2, 2, 5, 5));
  26. models[1] = LoadModelFromMesh(GenMeshCube(2.0f, 1.0f, 2.0f));
  27. models[2] = LoadModelFromMesh(GenMeshSphere(2, 32, 32));
  28. models[3] = LoadModelFromMesh(GenMeshHemiSphere(2, 16, 16));
  29. models[4] = LoadModelFromMesh(GenMeshCylinder(1, 2, 16));
  30. models[5] = LoadModelFromMesh(GenMeshTorus(0.25f, 4.0f, 16, 32));
  31. models[6] = LoadModelFromMesh(GenMeshKnot(1.0f, 2.0f, 16, 128));
  32. // Set checked texture as default diffuse component for all models material
  33. for (int i = 0; i < NUM_MODELS; i++) models[i].material.maps[MAP_DIFFUSE].texture = texture;
  34. // Define the camera to look into our 3d world
  35. Camera camera = {{ 5.0f, 5.0f, 5.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  36. // Model drawing position
  37. Vector3 position = { 0.0f, 0.0f, 0.0f };
  38. int currentModel = 0;
  39. SetCameraMode(camera, CAMERA_ORBITAL); // Set a orbital camera mode
  40. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  41. //--------------------------------------------------------------------------------------
  42. // Main game loop
  43. while (!WindowShouldClose()) // Detect window close button or ESC key
  44. {
  45. // Update
  46. //----------------------------------------------------------------------------------
  47. UpdateCamera(&camera); // Update internal camera and our camera
  48. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  49. {
  50. currentModel = (currentModel + 1)%NUM_MODELS; // Cycle between the textures
  51. }
  52. //----------------------------------------------------------------------------------
  53. // Draw
  54. //----------------------------------------------------------------------------------
  55. BeginDrawing();
  56. ClearBackground(RAYWHITE);
  57. Begin3dMode(camera);
  58. DrawModel(models[currentModel], position, 1.0f, WHITE);
  59. DrawGrid(10, 1.0);
  60. End3dMode();
  61. DrawRectangle(30, 400, 310, 30, Fade(SKYBLUE, 0.5f));
  62. DrawRectangleLines(30, 400, 310, 30, Fade(DARKBLUE, 0.5f));
  63. DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL MODELS", 40, 410, 10, BLUE);
  64. switch(currentModel)
  65. {
  66. case 0: DrawText("PLANE", 680, 10, 20, DARKBLUE); break;
  67. case 1: DrawText("CUBE", 680, 10, 20, DARKBLUE); break;
  68. case 2: DrawText("SPHERE", 680, 10, 20, DARKBLUE); break;
  69. case 3: DrawText("HEMISPHERE", 640, 10, 20, DARKBLUE); break;
  70. case 4: DrawText("CYLINDER", 680, 10, 20, DARKBLUE); break;
  71. case 5: DrawText("TORUS", 680, 10, 20, DARKBLUE); break;
  72. case 6: DrawText("KNOT", 680, 10, 20, DARKBLUE); break;
  73. default: break;
  74. }
  75. EndDrawing();
  76. //----------------------------------------------------------------------------------
  77. }
  78. // De-Initialization
  79. //--------------------------------------------------------------------------------------
  80. // Unload models data (GPU VRAM)
  81. for (int i = 0; i < NUM_MODELS; i++) UnloadModel(models[i]);
  82. CloseWindow(); // Close window and OpenGL context
  83. //--------------------------------------------------------------------------------------
  84. return 0;
  85. }