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.

126 lines
4.8 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 8 // Parametric 3d shapes to generate
  13. int main(void)
  14. {
  15. // Initialization
  16. //--------------------------------------------------------------------------------------
  17. const int screenWidth = 800;
  18. const 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] = { 0 };
  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. models[7] = LoadModelFromMesh(GenMeshPoly(5, 2.0f));
  33. // Set checked texture as default diffuse component for all models material
  34. for (int i = 0; i < NUM_MODELS; i++) models[i].materials[0].maps[MAP_DIFFUSE].texture = texture;
  35. // Define the camera to look into our 3d world
  36. Camera camera = { { 5.0f, 5.0f, 5.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  37. // Model drawing position
  38. Vector3 position = { 0.0f, 0.0f, 0.0f };
  39. int currentModel = 0;
  40. SetCameraMode(camera, CAMERA_ORBITAL); // Set a orbital camera mode
  41. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  42. //--------------------------------------------------------------------------------------
  43. // Main game loop
  44. while (!WindowShouldClose()) // Detect window close button or ESC key
  45. {
  46. // Update
  47. //----------------------------------------------------------------------------------
  48. UpdateCamera(&camera); // Update internal camera and our camera
  49. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  50. {
  51. currentModel = (currentModel + 1)%NUM_MODELS; // Cycle between the textures
  52. }
  53. if (IsKeyPressed(KEY_RIGHT))
  54. {
  55. currentModel++;
  56. if (currentModel >= NUM_MODELS) currentModel = 0;
  57. }
  58. else if (IsKeyPressed(KEY_LEFT))
  59. {
  60. currentModel--;
  61. if (currentModel < 0) currentModel = NUM_MODELS - 1;
  62. }
  63. //----------------------------------------------------------------------------------
  64. // Draw
  65. //----------------------------------------------------------------------------------
  66. BeginDrawing();
  67. ClearBackground(RAYWHITE);
  68. BeginMode3D(camera);
  69. DrawModel(models[currentModel], position, 1.0f, WHITE);
  70. DrawGrid(10, 1.0);
  71. EndMode3D();
  72. DrawRectangle(30, 400, 310, 30, Fade(SKYBLUE, 0.5f));
  73. DrawRectangleLines(30, 400, 310, 30, Fade(DARKBLUE, 0.5f));
  74. DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL MODELS", 40, 410, 10, BLUE);
  75. switch(currentModel)
  76. {
  77. case 0: DrawText("PLANE", 680, 10, 20, DARKBLUE); break;
  78. case 1: DrawText("CUBE", 680, 10, 20, DARKBLUE); break;
  79. case 2: DrawText("SPHERE", 680, 10, 20, DARKBLUE); break;
  80. case 3: DrawText("HEMISPHERE", 640, 10, 20, DARKBLUE); break;
  81. case 4: DrawText("CYLINDER", 680, 10, 20, DARKBLUE); break;
  82. case 5: DrawText("TORUS", 680, 10, 20, DARKBLUE); break;
  83. case 6: DrawText("KNOT", 680, 10, 20, DARKBLUE); break;
  84. case 7: DrawText("POLY", 680, 10, 20, DARKBLUE); break;
  85. default: break;
  86. }
  87. EndDrawing();
  88. //----------------------------------------------------------------------------------
  89. }
  90. // De-Initialization
  91. //--------------------------------------------------------------------------------------
  92. UnloadTexture(texture); // Unload texture
  93. // Unload models data (GPU VRAM)
  94. for (int i = 0; i < NUM_MODELS; i++) UnloadModel(models[i]);
  95. CloseWindow(); // Close window and OpenGL context
  96. //--------------------------------------------------------------------------------------
  97. return 0;
  98. }