raylib/examples/models/models_waving_cubes.c

113 lines
4.2 KiB
C
Raw Normal View History

2019-06-05 11:35:20 +03:00
/*******************************************************************************************
*
* raylib [models] example - Waving cubes
*
* This example has been created using raylib 2.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
2019-06-05 13:58:53 +03:00
* Example contributed by Codecat (@codecat) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2019 Codecat (@codecat) and Ramon Santamaria (@raysan5)
2019-06-05 11:35:20 +03:00
*
********************************************************************************************/
#include "raylib.h"
#include <math.h>
int main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - waving cubes");
// Initialize the camera
2019-06-05 13:58:53 +03:00
Camera3D camera = { 0 };
2019-06-05 11:35:20 +03:00
camera.position = (Vector3){ 30.0f, 20.0f, 30.0f };
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
camera.fovy = 70.0f;
camera.type = CAMERA_PERSPECTIVE;
// Specify the amount of blocks in each direction
const int numBlocks = 15;
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
2019-06-05 13:58:53 +03:00
//----------------------------------------------------------------------------------
2019-06-05 11:35:20 +03:00
double time = GetTime();
// Calculate time scale for cube position and size
2019-06-05 13:58:53 +03:00
float scale = (2.0f + (float)sin(time))*0.7f;
2019-06-05 11:35:20 +03:00
// Move camera around the scene
2019-06-05 13:58:53 +03:00
double cameraTime = time*0.3;
camera.position.x = (float)cos(cameraTime)*40.0f;
camera.position.z = (float)sin(cameraTime)*40.0f;
//----------------------------------------------------------------------------------
2019-06-05 11:35:20 +03:00
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
DrawGrid(10, 5.0f);
2019-06-05 13:58:53 +03:00
for (int x = 0; x < numBlocks; x++)
{
for (int y = 0; y < numBlocks; y++)
{
for (int z = 0; z < numBlocks; z++)
{
2019-06-05 11:35:20 +03:00
// Scale of the blocks depends on x/y/z positions
2019-06-05 13:58:53 +03:00
float blockScale = (x + y + z)/30.0f;
2019-06-05 11:35:20 +03:00
// Scatter makes the waving effect by adding blockScale over time
2019-06-05 13:58:53 +03:00
float scatter = sinf(blockScale*20.0f + (float)(time*4.0f));
2019-06-05 11:35:20 +03:00
// Calculate the cube position
Vector3 cubePos = {
2019-06-05 13:58:53 +03:00
(float)(x - numBlocks/2)*(scale*3.0f) + scatter,
(float)(y - numBlocks/2)*(scale*2.0f) + scatter,
(float)(z - numBlocks/2)*(scale*3.0f) + scatter
2019-06-05 11:35:20 +03:00
};
// Pick a color with a hue depending on cube position for the rainbow color effect
2019-06-05 13:58:53 +03:00
Color cubeColor = ColorFromHSV((Vector3){ (float)(((x + y + z)*18)%360), 0.75f, 0.9f });
2019-06-05 11:35:20 +03:00
// Calculate cube size
2019-06-05 13:58:53 +03:00
float cubeSize = (2.4f - scale)*blockScale;
2019-06-05 11:35:20 +03:00
// And finally, draw the cube!
DrawCube(cubePos, cubeSize, cubeSize, cubeSize, cubeColor);
}
}
}
2019-06-05 13:58:53 +03:00
2019-06-05 11:35:20 +03:00
EndMode3D();
2019-06-05 13:58:53 +03:00
DrawFPS(10, 10);
2019-06-05 11:35:20 +03:00
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}