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.

119 lines
4.8 KiB

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