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 | |
14 | #include "raylib.h" |
15 | |
16 | #include <math.h> |
17 | |
18 | int main() |
19 | { |
20 | // Initialization |
21 | //-------------------------------------------------------------------------------------- |
22 | const int screenWidth = 800; |
23 | const int screenHeight = 450; |
24 | |
25 | InitWindow(screenWidth, screenHeight, "raylib [models] example - waving cubes" ); |
26 | |
27 | // Initialize the camera |
28 | Camera3D camera = { 0 }; |
29 | camera.position = (Vector3){ 30.0f, 20.0f, 30.0f }; |
30 | camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; |
31 | camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; |
32 | camera.fovy = 70.0f; |
33 | camera.type = CAMERA_PERSPECTIVE; |
34 | |
35 | // Specify the amount of blocks in each direction |
36 | const int numBlocks = 15; |
37 | |
38 | SetTargetFPS(60); |
39 | //-------------------------------------------------------------------------------------- |
40 | |
41 | // Main game loop |
42 | while (!WindowShouldClose()) // Detect window close button or ESC key |
43 | { |
44 | // Update |
45 | //---------------------------------------------------------------------------------- |
46 | double time = GetTime(); |
47 | |
48 | // Calculate time scale for cube position and size |
49 | float scale = (2.0f + (float)sin(time))*0.7f; |
50 | |
51 | // Move camera around the scene |
52 | double cameraTime = time*0.3; |
53 | camera.position.x = (float)cos(cameraTime)*40.0f; |
54 | camera.position.z = (float)sin(cameraTime)*40.0f; |
55 | //---------------------------------------------------------------------------------- |
56 | |
57 | // Draw |
58 | //---------------------------------------------------------------------------------- |
59 | BeginDrawing(); |
60 | |
61 | ClearBackground(RAYWHITE); |
62 | |
63 | BeginMode3D(camera); |
64 | |
65 | DrawGrid(10, 5.0f); |
66 | |
67 | for (int x = 0; x < numBlocks; x++) |
68 | { |
69 | for (int y = 0; y < numBlocks; y++) |
70 | { |
71 | for (int z = 0; z < numBlocks; z++) |
72 | { |
73 | // Scale of the blocks depends on x/y/z positions |
74 | float blockScale = (x + y + z)/30.0f; |
75 | |
76 | // Scatter makes the waving effect by adding blockScale over time |
77 | float scatter = sinf(blockScale*20.0f + (float)(time*4.0f)); |
78 | |
79 | // Calculate the cube position |
80 | Vector3 cubePos = { |
81 | (float)(x - numBlocks/2)*(scale*3.0f) + scatter, |
82 | (float)(y - numBlocks/2)*(scale*2.0f) + scatter, |
83 | (float)(z - numBlocks/2)*(scale*3.0f) + scatter |
84 | }; |
85 | |
86 | // Pick a color with a hue depending on cube position for the rainbow color effect |
87 | Color cubeColor = ColorFromHSV((Vector3){ (float)(((x + y + z)*18)%360), 0.75f, 0.9f }); |
88 | |
89 | // Calculate cube size |
90 | float cubeSize = (2.4f - scale)*blockScale; |
91 | |
92 | // And finally, draw the cube! |
93 | DrawCube(cubePos, cubeSize, cubeSize, cubeSize, cubeColor); |
94 | } |
95 | } |
96 | } |
97 | |
98 | EndMode3D(); |
99 | |
100 | DrawFPS(10, 10); |
101 | |
102 | EndDrawing(); |
103 | //---------------------------------------------------------------------------------- |
104 | } |
105 | |
106 | // De-Initialization |
107 | //-------------------------------------------------------------------------------------- |
108 | CloseWindow(); // Close window and OpenGL context |
109 | //-------------------------------------------------------------------------------------- |
110 | |
111 | return 0; |
112 | } |
113 | |