Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

121 строка
4.9 KiB

5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
3 лет назад
5 лет назад
3 лет назад
5 лет назад
3 лет назад
5 лет назад
3 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
9 месяцев назад
5 лет назад
5 лет назад
5 лет назад
3 лет назад
5 лет назад
3 лет назад
5 лет назад
5 лет назад
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Waving cubes
  4. *
  5. * Example complexity rating: [] 3/4
  6. *
  7. * Example originally created with raylib 2.5, last time updated with raylib 3.7
  8. *
  9. * Example contributed by Codecat (@codecat) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2019-2025 Codecat (@codecat) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include <math.h> // Required for: sinf()
  19. //------------------------------------------------------------------------------------
  20. // Program main entry point
  21. //------------------------------------------------------------------------------------
  22. int main()
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. InitWindow(screenWidth, screenHeight, "raylib [models] example - waving cubes");
  29. // Initialize the camera
  30. Camera3D camera = { 0 };
  31. camera.position = (Vector3){ 30.0f, 20.0f, 30.0f }; // Camera position
  32. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  33. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  34. camera.fovy = 70.0f; // Camera field-of-view Y
  35. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  36. // Specify the amount of blocks in each direction
  37. const int numBlocks = 15;
  38. SetTargetFPS(60);
  39. //--------------------------------------------------------------------------------------
  40. // Main game loop
  41. while (!WindowShouldClose()) // Detect window close button or ESC key
  42. {
  43. // Update
  44. //----------------------------------------------------------------------------------
  45. double time = GetTime();
  46. // Calculate time scale for cube position and size
  47. float scale = (2.0f + (float)sin(time))*0.7f;
  48. // Move camera around the scene
  49. double cameraTime = time*0.3;
  50. camera.position.x = (float)cos(cameraTime)*40.0f;
  51. camera.position.z = (float)sin(cameraTime)*40.0f;
  52. //----------------------------------------------------------------------------------
  53. // Draw
  54. //----------------------------------------------------------------------------------
  55. BeginDrawing();
  56. ClearBackground(RAYWHITE);
  57. BeginMode3D(camera);
  58. DrawGrid(10, 5.0f);
  59. for (int x = 0; x < numBlocks; x++)
  60. {
  61. for (int y = 0; y < numBlocks; y++)
  62. {
  63. for (int z = 0; z < numBlocks; z++)
  64. {
  65. // Scale of the blocks depends on x/y/z positions
  66. float blockScale = (x + y + z)/30.0f;
  67. // Scatter makes the waving effect by adding blockScale over time
  68. float scatter = sinf(blockScale*20.0f + (float)(time*4.0f));
  69. // Calculate the cube position
  70. Vector3 cubePos = {
  71. (float)(x - numBlocks/2)*(scale*3.0f) + scatter,
  72. (float)(y - numBlocks/2)*(scale*2.0f) + scatter,
  73. (float)(z - numBlocks/2)*(scale*3.0f) + scatter
  74. };
  75. // Pick a color with a hue depending on cube position for the rainbow color effect
  76. // NOTE: This function is quite costly to be done per cube and frame,
  77. // pre-catching the results into a separate array could improve performance
  78. Color cubeColor = ColorFromHSV((float)(((x + y + z)*18)%360), 0.75f, 0.9f);
  79. // Calculate cube size
  80. float cubeSize = (2.4f - scale)*blockScale;
  81. // And finally, draw the cube!
  82. DrawCube(cubePos, cubeSize, cubeSize, cubeSize, cubeColor);
  83. }
  84. }
  85. }
  86. EndMode3D();
  87. DrawFPS(10, 10);
  88. EndDrawing();
  89. //----------------------------------------------------------------------------------
  90. }
  91. // De-Initialization
  92. //--------------------------------------------------------------------------------------
  93. CloseWindow(); // Close window and OpenGL context
  94. //--------------------------------------------------------------------------------------
  95. return 0;
  96. }