Merge remote-tracking branch 'upstream/master'

This commit is contained in:
codifies 2021-03-25 13:03:45 +00:00
commit 64f43ff03c

View File

@ -22,8 +22,13 @@
#include "rlights.h" #include "rlights.h"
#include <stdlib.h> #include <stdlib.h>
#include <math.h>
#define GLSL_VERSION 330 #if defined(PLATFORM_DESKTOP)
#define GLSL_VERSION 330
#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
#define GLSL_VERSION 100
#endif
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Program main entry point // Program main entry point
@ -34,13 +39,21 @@ int main(void)
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
const int screenWidth = 800; const int screenWidth = 800;
const int screenHeight = 450; const int screenHeight = 450;
const int fps = 60;
SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - rlgl mesh instanced"); InitWindow(screenWidth, screenHeight, "raylib [shaders] example - rlgl mesh instanced");
int speed = 30; // Speed of jump animation
int groups = 2; // Count of separate groups jumping around
float amp = 10; // Maximum amplitude of jump
float variance = 0.8f; // Global variance in jump height
float loop = 0.0f; // Individual cube's computed loop timer
float x = 0.0f, y = 0.0f, z = 0.0f; // Used for various 3D coordinate & vector ops
// Define the camera to look into our 3d world // Define the camera to look into our 3d world
Camera camera = { 0 }; Camera camera = { 0 };
camera.position = (Vector3){ 125.0f, 125.0f, 125.0f }; camera.position = (Vector3){ -125.0f, 125.0f, -125.0f };
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
camera.fovy = 45.0f; camera.fovy = 45.0f;
@ -56,16 +69,16 @@ int main(void)
// Scatter random cubes around // Scatter random cubes around
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
float x = (float)GetRandomValue(-50, 50); x = GetRandomValue(-50, 50);
float y = (float)GetRandomValue(-50, 50); y = GetRandomValue(-50, 50);
float z = (float)GetRandomValue(-50, 50); z = GetRandomValue(-50, 50);
translations[i] = MatrixTranslate(x, y, z); translations[i] = MatrixTranslate(x, y, z);
x = (float)GetRandomValue(0, 360); x = GetRandomValue(0, 360);
y = (float)GetRandomValue(0, 360); y = GetRandomValue(0, 360);
z = (float)GetRandomValue(0, 360); z = GetRandomValue(0, 360);
Vector3 axis = Vector3Normalize((Vector3){x, y, z}); Vector3 axis = Vector3Normalize((Vector3){ x, y, z });
float angle = (float)GetRandomValue(0, 10) * DEG2RAD; float angle = (float)GetRandomValue(0, 10)*DEG2RAD;
rotationsInc[i] = MatrixRotate(axis, angle); rotationsInc[i] = MatrixRotate(axis, angle);
rotations[i] = MatrixIdentity(); rotations[i] = MatrixIdentity();
@ -85,33 +98,71 @@ int main(void)
int ambientLoc = GetShaderLocation(shader, "ambient"); int ambientLoc = GetShaderLocation(shader, "ambient");
SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, SHADER_UNIFORM_VEC4); SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, SHADER_UNIFORM_VEC4);
CreateLight(LIGHT_DIRECTIONAL, (Vector3){ 50, 50, 0 }, Vector3Zero(), WHITE, shader); CreateLight(LIGHT_DIRECTIONAL, (Vector3){ 50.0f, 50.0f, 0.0f }, Vector3Zero(), WHITE, shader);
Material material = LoadMaterialDefault(); Material material = LoadMaterialDefault();
material.shader = shader; material.shader = shader;
material.maps[MATERIAL_MAP_DIFFUSE].color = RED; material.maps[MATERIAL_MAP_DIFFUSE].color = RED;
SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
SetTargetFPS(60); // Set our game to run at 60 frames-per-second int textPositionY = 300;
int framesCounter = 0; // Simple frames counter to manage animation
SetTargetFPS(fps); // Set our game to run at 60 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Main game loop // Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key while (!WindowShouldClose()) // Detect window close button or ESC key
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
UpdateCamera(&camera); UpdateCamera(&camera);
framesCounter++;
if (IsKeyDown(KEY_UP)) amp += 0.5f;
if (IsKeyDown(KEY_DOWN)) amp = (amp <= 1)? 1.0f : (amp - 1.0f);
if (IsKeyDown(KEY_LEFT)) variance = (variance <= 0.0f)? 0.0f : (variance - 0.01f);
if (IsKeyDown(KEY_RIGHT)) variance = (variance >= 1.0f)? 1.0f : (variance + 0.01f);
if (IsKeyDown(KEY_ONE)) groups = 1;
if (IsKeyDown(KEY_TWO)) groups = 2;
if (IsKeyDown(KEY_THREE)) groups = 3;
if (IsKeyDown(KEY_FOUR)) groups = 4;
if (IsKeyDown(KEY_FIVE)) groups = 5;
if (IsKeyDown(KEY_SIX)) groups = 6;
if (IsKeyDown(KEY_SEVEN)) groups = 7;
if (IsKeyDown(KEY_EIGHT)) groups = 8;
if (IsKeyDown(KEY_NINE)) groups = 9;
if (IsKeyDown(KEY_W)) { groups = 7; amp = 25; speed = 18; variance = 0.70f; }
if (IsKeyDown(KEY_EQUAL)) speed = (speed <= (fps*0.25f))? (fps*0.25f) : (speed*0.95f);
if (IsKeyDown(KEY_KP_ADD)) speed = (speed <= (fps*0.25f))? (fps*0.25f) : (speed*0.95f);
if (IsKeyDown(KEY_MINUS)) speed = fmaxf(speed*1.02f, speed + 1);
if (IsKeyDown(KEY_KP_SUBTRACT)) speed = fmaxf(speed*1.02f, speed + 1);
// Update the light shader with the camera view position // Update the light shader with the camera view position
float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z }; float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3); SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
// Apply per-instance rotations // Apply per-instance transformations
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
rotations[i] = MatrixMultiply(rotations[i], rotationsInc[i]); rotations[i] = MatrixMultiply(rotations[i], rotationsInc[i]);
transforms[i] = MatrixMultiply(rotations[i], translations[i]); transforms[i] = MatrixMultiply(rotations[i], translations[i]);
// Get the animation cycle's framesCounter for this instance
loop = (float)((framesCounter + (int)(((float)(i%groups)/groups)*speed))%speed)/speed;
// Calculate the y according to loop cycle
y = (sinf(loop*PI*2))*amp*((1 - variance) + (variance*(float)(i%(groups*10))/(groups*10)));
// Clamp to floor
y = (y < 0)? 0.0f : y;
transforms[i] = MatrixMultiply(transforms[i], MatrixTranslate(0.0f, y, 0.0f));
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -122,10 +173,39 @@ int main(void)
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
BeginMode3D(camera); BeginMode3D(camera);
rlDrawMeshInstanced(cube, material, transforms, count); DrawMeshInstanced(cube, material, transforms, count);
EndMode3D(); EndMode3D();
DrawText("A CUBE OF DANCING CUBES!", 490, 10, 20, MAROON); DrawText("A CUBE OF DANCING CUBES!", 490, 10, 20, MAROON);
DrawText("PRESS KEYS:", 10, textPositionY, 20, BLACK);
DrawText("1 - 9", 10, textPositionY += 25, 10, BLACK);
DrawText(": Number of groups", 50, textPositionY , 10, BLACK);
DrawText(TextFormat(": %d", groups), 160, textPositionY , 10, BLACK);
DrawText("UP", 10, textPositionY += 15, 10, BLACK);
DrawText(": increase amplitude", 50, textPositionY, 10, BLACK);
DrawText(TextFormat(": %.2f", amp), 160, textPositionY , 10, BLACK);
DrawText("DOWN", 10, textPositionY += 15, 10, BLACK);
DrawText(": decrease amplitude", 50, textPositionY, 10, BLACK);
DrawText("LEFT", 10, textPositionY += 15, 10, BLACK);
DrawText(": decrease variance", 50, textPositionY, 10, BLACK);
DrawText(TextFormat(": %.2f", variance), 160, textPositionY , 10, BLACK);
DrawText("RIGHT", 10, textPositionY += 15, 10, BLACK);
DrawText(": increase variance", 50, textPositionY, 10, BLACK);
DrawText("+/=", 10, textPositionY += 15, 10, BLACK);
DrawText(": increase speed", 50, textPositionY, 10, BLACK);
DrawText(TextFormat(": %d = %f loops/sec", speed, ((float)fps / speed)), 160, textPositionY , 10, BLACK);
DrawText("-", 10, textPositionY += 15, 10, BLACK);
DrawText(": decrease speed", 50, textPositionY, 10, BLACK);
DrawText("W", 10, textPositionY += 15, 10, BLACK);
DrawText(": Wild setup!", 50, textPositionY, 10, BLACK);
DrawFPS(10, 10); DrawFPS(10, 10);