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.2 KiB

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