2020-09-27 11:29:05 +03:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
2021-03-31 19:40:04 +03:00
|
|
|
* raylib [shaders] example - mesh instancing
|
2020-09-27 11:29:05 +03:00
|
|
|
*
|
2021-03-31 19:40:04 +03:00
|
|
|
* This example has been created using raylib 3.7 (www.raylib.com)
|
2020-09-27 11:29:05 +03:00
|
|
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
|
|
|
*
|
2021-03-31 19:40:04 +03:00
|
|
|
* Example contributed by @seanpringle and reviewed by Max (@moliad) and Ramon Santamaria (@raysan5)
|
2020-11-01 15:39:48 +03:00
|
|
|
*
|
2021-03-31 19:40:04 +03:00
|
|
|
* Copyright (c) 2020-2021 @seanpringle, Max (@moliad) and Ramon Santamaria (@raysan5)
|
2020-09-27 11:29:05 +03:00
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
#include "raymath.h"
|
|
|
|
|
|
|
|
#define RLIGHTS_IMPLEMENTATION
|
|
|
|
#include "rlights.h"
|
|
|
|
|
2020-11-01 15:39:48 +03:00
|
|
|
#include <stdlib.h>
|
2021-03-25 15:41:32 +03:00
|
|
|
#include <math.h>
|
2020-11-01 15:39:48 +03:00
|
|
|
|
2021-03-25 15:41:32 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP)
|
|
|
|
#define GLSL_VERSION 330
|
|
|
|
#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
|
|
|
|
#define GLSL_VERSION 100
|
|
|
|
#endif
|
2021-03-25 13:12:59 +03:00
|
|
|
|
2021-04-23 10:09:34 +03:00
|
|
|
#define MAX_INSTANCES 10000
|
|
|
|
|
2020-09-27 11:29:05 +03:00
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
// Program main entry point
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2020-11-01 15:39:48 +03:00
|
|
|
const int screenWidth = 800;
|
|
|
|
const int screenHeight = 450;
|
2021-03-25 13:12:59 +03:00
|
|
|
const int fps = 60;
|
2020-09-27 11:29:05 +03:00
|
|
|
|
|
|
|
SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
|
2021-03-31 19:40:04 +03:00
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - mesh instancing");
|
2020-09-27 11:29:05 +03:00
|
|
|
|
2021-03-25 15:41:32 +03:00
|
|
|
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
|
|
|
|
|
2020-09-27 11:29:05 +03:00
|
|
|
// Define the camera to look into our 3d world
|
|
|
|
Camera camera = { 0 };
|
2021-03-25 13:12:59 +03:00
|
|
|
camera.position = (Vector3){ -125.0f, 125.0f, -125.0f };
|
2020-09-27 11:29:05 +03:00
|
|
|
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
|
|
|
|
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
|
|
|
|
camera.fovy = 45.0f;
|
2021-03-19 22:14:14 +03:00
|
|
|
camera.projection = CAMERA_PERSPECTIVE;
|
2020-09-27 11:29:05 +03:00
|
|
|
|
|
|
|
Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
|
|
|
|
|
2021-04-23 10:09:34 +03:00
|
|
|
Matrix *rotations = RL_MALLOC(MAX_INSTANCES*sizeof(Matrix)); // Rotation state of instances
|
|
|
|
Matrix *rotationsInc = RL_MALLOC(MAX_INSTANCES*sizeof(Matrix)); // Per-frame rotation animation of instances
|
|
|
|
Matrix *translations = RL_MALLOC(MAX_INSTANCES*sizeof(Matrix)); // Locations of instances
|
2020-09-27 11:29:05 +03:00
|
|
|
|
|
|
|
// Scatter random cubes around
|
2021-04-23 10:09:34 +03:00
|
|
|
for (int i = 0; i < MAX_INSTANCES; i++)
|
2020-09-27 11:29:05 +03:00
|
|
|
{
|
2021-10-25 11:21:16 +03:00
|
|
|
x = (float)GetRandomValue(-50, 50);
|
|
|
|
y = (float)GetRandomValue(-50, 50);
|
|
|
|
z = (float)GetRandomValue(-50, 50);
|
2021-04-22 19:55:24 +03:00
|
|
|
translations[i] = MatrixTranslate(x, y, z);
|
2020-09-27 11:29:05 +03:00
|
|
|
|
2021-10-25 11:21:16 +03:00
|
|
|
x = (float)GetRandomValue(0, 360);
|
|
|
|
y = (float)GetRandomValue(0, 360);
|
|
|
|
z = (float)GetRandomValue(0, 360);
|
2021-03-25 15:41:32 +03:00
|
|
|
Vector3 axis = Vector3Normalize((Vector3){ x, y, z });
|
|
|
|
float angle = (float)GetRandomValue(0, 10)*DEG2RAD;
|
2020-09-27 11:29:05 +03:00
|
|
|
|
|
|
|
rotationsInc[i] = MatrixRotate(axis, angle);
|
|
|
|
rotations[i] = MatrixIdentity();
|
|
|
|
}
|
|
|
|
|
2021-04-23 10:09:34 +03:00
|
|
|
Matrix *transforms = RL_MALLOC(MAX_INSTANCES*sizeof(Matrix)); // Pre-multiplied transformations passed to rlgl
|
2020-09-27 11:29:05 +03:00
|
|
|
|
2021-04-22 19:55:24 +03:00
|
|
|
Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/base_lighting_instanced.vs", GLSL_VERSION),
|
2021-03-12 20:00:36 +03:00
|
|
|
TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION));
|
2020-09-27 11:29:05 +03:00
|
|
|
|
|
|
|
// Get some shader loactions
|
2021-03-14 13:05:51 +03:00
|
|
|
shader.locs[SHADER_LOC_MATRIX_MVP] = GetShaderLocation(shader, "mvp");
|
|
|
|
shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
|
2021-03-31 20:59:23 +03:00
|
|
|
shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocationAttrib(shader, "instanceTransform");
|
2020-09-27 11:29:05 +03:00
|
|
|
|
2020-11-01 15:39:48 +03:00
|
|
|
// Ambient light level
|
2020-09-27 11:29:05 +03:00
|
|
|
int ambientLoc = GetShaderLocation(shader, "ambient");
|
2021-03-14 13:05:51 +03:00
|
|
|
SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, SHADER_UNIFORM_VEC4);
|
2020-09-27 11:29:05 +03:00
|
|
|
|
2021-03-25 15:41:32 +03:00
|
|
|
CreateLight(LIGHT_DIRECTIONAL, (Vector3){ 50.0f, 50.0f, 0.0f }, Vector3Zero(), WHITE, shader);
|
2020-09-27 11:29:05 +03:00
|
|
|
|
2021-03-31 19:40:04 +03:00
|
|
|
// NOTE: We are assigning the intancing shader to material.shader
|
|
|
|
// to be used on mesh drawing with DrawMeshInstanced()
|
2020-09-27 11:29:05 +03:00
|
|
|
Material material = LoadMaterialDefault();
|
|
|
|
material.shader = shader;
|
2021-03-14 13:05:51 +03:00
|
|
|
material.maps[MATERIAL_MAP_DIFFUSE].color = RED;
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-03-25 15:41:32 +03:00
|
|
|
SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
|
|
|
|
|
|
|
|
int textPositionY = 300;
|
2021-03-26 02:12:29 +03:00
|
|
|
int framesCounter = 0; // Simple frames counter to manage animation
|
2020-09-27 11:29:05 +03:00
|
|
|
|
2021-03-26 02:12:29 +03:00
|
|
|
SetTargetFPS(fps); // Set our game to run at 60 frames-per-second
|
2020-09-27 11:29:05 +03:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Main game loop
|
2021-03-26 02:12:29 +03:00
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
2020-09-27 11:29:05 +03:00
|
|
|
{
|
2021-03-25 13:12:59 +03:00
|
|
|
|
2020-09-27 11:29:05 +03:00
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
2021-03-26 02:12:29 +03:00
|
|
|
textPositionY = 300;
|
2021-03-25 15:41:32 +03:00
|
|
|
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);
|
2021-03-25 13:12:59 +03:00
|
|
|
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;
|
2021-03-25 15:41:32 +03:00
|
|
|
if (IsKeyDown(KEY_W)) { groups = 7; amp = 25; speed = 18; variance = 0.70f; }
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-10-25 11:21:16 +03:00
|
|
|
if (IsKeyDown(KEY_EQUAL)) speed = (speed <= (fps*0.25f))? (int)(fps*0.25f) : (int)(speed*0.95f);
|
|
|
|
if (IsKeyDown(KEY_KP_ADD)) speed = (speed <= (fps*0.25f))? (int)(fps*0.25f) : (int)(speed*0.95f);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-10-25 11:21:16 +03:00
|
|
|
if (IsKeyDown(KEY_MINUS)) speed = (int)fmaxf(speed*1.02f, speed + 1);
|
|
|
|
if (IsKeyDown(KEY_KP_SUBTRACT)) speed = (int)fmaxf(speed*1.02f, speed + 1);
|
2020-09-27 11:29:05 +03:00
|
|
|
|
|
|
|
// Update the light shader with the camera view position
|
|
|
|
float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
|
2021-03-14 13:05:51 +03:00
|
|
|
SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
|
2020-09-27 11:29:05 +03:00
|
|
|
|
2021-03-25 13:12:59 +03:00
|
|
|
// Apply per-instance transformations
|
2021-04-23 10:09:34 +03:00
|
|
|
for (int i = 0; i < MAX_INSTANCES; i++)
|
2021-03-25 15:41:32 +03:00
|
|
|
{
|
2020-09-27 11:29:05 +03:00
|
|
|
rotations[i] = MatrixMultiply(rotations[i], rotationsInc[i]);
|
|
|
|
transforms[i] = MatrixMultiply(rotations[i], translations[i]);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-03-25 15:41:32 +03:00
|
|
|
// Get the animation cycle's framesCounter for this instance
|
|
|
|
loop = (float)((framesCounter + (int)(((float)(i%groups)/groups)*speed))%speed)/speed;
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-03-25 15:41:32 +03:00
|
|
|
// Calculate the y according to loop cycle
|
|
|
|
y = (sinf(loop*PI*2))*amp*((1 - variance) + (variance*(float)(i%(groups*10))/(groups*10)));
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-03-25 15:41:32 +03:00
|
|
|
// Clamp to floor
|
|
|
|
y = (y < 0)? 0.0f : y;
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-03-25 15:41:32 +03:00
|
|
|
transforms[i] = MatrixMultiply(transforms[i], MatrixTranslate(0.0f, y, 0.0f));
|
2020-09-27 11:29:05 +03:00
|
|
|
}
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-03-26 02:12:29 +03:00
|
|
|
UpdateCamera(&camera);
|
2020-09-27 11:29:05 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
|
|
|
|
|
|
|
ClearBackground(RAYWHITE);
|
|
|
|
|
|
|
|
BeginMode3D(camera);
|
2021-03-31 20:59:23 +03:00
|
|
|
//DrawMesh(cube, material, MatrixIdentity());
|
2021-04-23 10:09:34 +03:00
|
|
|
DrawMeshInstanced(cube, material, transforms, MAX_INSTANCES);
|
2020-09-27 11:29:05 +03:00
|
|
|
EndMode3D();
|
2021-03-25 15:41:32 +03:00
|
|
|
|
2020-11-01 15:39:48 +03:00
|
|
|
DrawText("A CUBE OF DANCING CUBES!", 490, 10, 20, MAROON);
|
2021-03-25 15:41:32 +03:00
|
|
|
DrawText("PRESS KEYS:", 10, textPositionY, 20, BLACK);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
|
|
|
DrawText("1 - 9", 10, textPositionY += 25, 10, BLACK);
|
2021-03-25 15:41:32 +03:00
|
|
|
DrawText(": Number of groups", 50, textPositionY , 10, BLACK);
|
|
|
|
DrawText(TextFormat(": %d", groups), 160, textPositionY , 10, BLACK);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
|
|
|
DrawText("UP", 10, textPositionY += 15, 10, BLACK);
|
2021-03-25 15:41:32 +03:00
|
|
|
DrawText(": increase amplitude", 50, textPositionY, 10, BLACK);
|
|
|
|
DrawText(TextFormat(": %.2f", amp), 160, textPositionY , 10, BLACK);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
|
|
|
DrawText("DOWN", 10, textPositionY += 15, 10, BLACK);
|
2021-03-25 15:41:32 +03:00
|
|
|
DrawText(": decrease amplitude", 50, textPositionY, 10, BLACK);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
|
|
|
DrawText("LEFT", 10, textPositionY += 15, 10, BLACK);
|
2021-03-25 15:41:32 +03:00
|
|
|
DrawText(": decrease variance", 50, textPositionY, 10, BLACK);
|
|
|
|
DrawText(TextFormat(": %.2f", variance), 160, textPositionY , 10, BLACK);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
|
|
|
DrawText("RIGHT", 10, textPositionY += 15, 10, BLACK);
|
2021-03-25 15:41:32 +03:00
|
|
|
DrawText(": increase variance", 50, textPositionY, 10, BLACK);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
|
|
|
DrawText("+/=", 10, textPositionY += 15, 10, BLACK);
|
2021-03-25 15:41:32 +03:00
|
|
|
DrawText(": increase speed", 50, textPositionY, 10, BLACK);
|
|
|
|
DrawText(TextFormat(": %d = %f loops/sec", speed, ((float)fps / speed)), 160, textPositionY , 10, BLACK);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
|
|
|
DrawText("-", 10, textPositionY += 15, 10, BLACK);
|
2021-03-25 15:41:32 +03:00
|
|
|
DrawText(": decrease speed", 50, textPositionY, 10, BLACK);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
|
|
|
DrawText("W", 10, textPositionY += 15, 10, BLACK);
|
2021-03-25 15:41:32 +03:00
|
|
|
DrawText(": Wild setup!", 50, textPositionY, 10, BLACK);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-09-27 11:29:05 +03:00
|
|
|
DrawFPS(10, 10);
|
|
|
|
|
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2022-04-04 11:32:46 +03:00
|
|
|
// Free allocated matrices
|
|
|
|
RL_FREE(rotations);
|
|
|
|
RL_FREE(rotationsInc);
|
|
|
|
RL_FREE(translations);
|
|
|
|
RL_FREE(transforms);
|
|
|
|
|
2020-09-27 11:29:05 +03:00
|
|
|
CloseWindow(); // Close window and OpenGL context
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|