2020-09-27 11:29:05 +03:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
2022-07-20 02:28:37 +03:00
|
|
|
* raylib [shaders] example - Mesh instancing
|
2020-09-27 11:29:05 +03:00
|
|
|
*
|
2022-07-20 02:28:37 +03:00
|
|
|
* Example originally created with raylib 3.7, last time updated with raylib 4.2
|
2020-09-27 11:29:05 +03:00
|
|
|
*
|
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
|
|
|
*
|
2022-07-20 02:28:37 +03:00
|
|
|
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
|
|
|
* BSD-like license that allows static linking with closed source software
|
|
|
|
*
|
2024-01-02 22:58:12 +03:00
|
|
|
* Copyright (c) 2020-2024 @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"
|
|
|
|
|
2022-06-21 22:06:03 +03:00
|
|
|
#include <stdlib.h> // Required for: calloc(), free()
|
2020-11-01 15:39:48 +03:00
|
|
|
|
2021-03-25 15:41:32 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP)
|
|
|
|
#define GLSL_VERSION 330
|
2023-09-07 18:42:28 +03:00
|
|
|
#else // PLATFORM_ANDROID, PLATFORM_WEB
|
2021-03-25 15:41:32 +03:00
|
|
|
#define GLSL_VERSION 100
|
|
|
|
#endif
|
2021-03-25 13:12:59 +03:00
|
|
|
|
2022-07-04 18:47:47 +03:00
|
|
|
#define MAX_INSTANCES 10000
|
2021-04-23 10:09:34 +03:00
|
|
|
|
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;
|
2020-09-27 11:29:05 +03:00
|
|
|
|
2021-03-31 19:40:04 +03:00
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - mesh instancing");
|
2020-09-27 11:29:05 +03:00
|
|
|
|
|
|
|
// Define the camera to look into our 3d world
|
|
|
|
Camera camera = { 0 };
|
2023-02-14 22:00:51 +03:00
|
|
|
camera.position = (Vector3){ -125.0f, 125.0f, -125.0f }; // Camera position
|
|
|
|
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
|
|
|
|
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
|
|
|
|
camera.fovy = 45.0f; // Camera field-of-view Y
|
|
|
|
camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
2020-09-27 11:29:05 +03:00
|
|
|
|
2022-06-21 22:06:03 +03:00
|
|
|
// Define mesh to be instanced
|
2020-09-27 11:29:05 +03:00
|
|
|
Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
|
|
|
|
|
2022-06-21 22:06:03 +03:00
|
|
|
// Define transforms to be uploaded to GPU for instances
|
|
|
|
Matrix *transforms = (Matrix *)RL_CALLOC(MAX_INSTANCES, sizeof(Matrix)); // Pre-multiplied transformations passed to rlgl
|
2020-09-27 11:29:05 +03:00
|
|
|
|
2022-06-21 22:06:03 +03:00
|
|
|
// Translate and rotate cubes randomly
|
2021-04-23 10:09:34 +03:00
|
|
|
for (int i = 0; i < MAX_INSTANCES; i++)
|
2020-09-27 11:29:05 +03:00
|
|
|
{
|
2022-06-21 22:06:03 +03:00
|
|
|
Matrix translation = MatrixTranslate((float)GetRandomValue(-50, 50), (float)GetRandomValue(-50, 50), (float)GetRandomValue(-50, 50));
|
|
|
|
Vector3 axis = Vector3Normalize((Vector3){ (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360) });
|
2021-03-25 15:41:32 +03:00
|
|
|
float angle = (float)GetRandomValue(0, 10)*DEG2RAD;
|
2022-06-21 22:06:03 +03:00
|
|
|
Matrix rotation = MatrixRotate(axis, angle);
|
|
|
|
|
|
|
|
transforms[i] = MatrixMultiply(rotation, translation);
|
2020-09-27 11:29:05 +03:00
|
|
|
}
|
|
|
|
|
2022-06-21 22:06:03 +03:00
|
|
|
// Load lighting shader
|
|
|
|
Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/lighting_instancing.vs", GLSL_VERSION),
|
2021-03-12 20:00:36 +03:00
|
|
|
TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION));
|
2022-06-21 22:06:03 +03:00
|
|
|
// Get shader locations
|
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
|
|
|
|
2022-06-21 22:06:03 +03:00
|
|
|
// Set shader value: 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
|
|
|
|
2022-06-21 22:06:03 +03:00
|
|
|
// Create one light
|
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()
|
2022-06-21 22:06:03 +03:00
|
|
|
Material matInstances = LoadMaterialDefault();
|
|
|
|
matInstances.shader = shader;
|
|
|
|
matInstances.maps[MATERIAL_MAP_DIFFUSE].color = RED;
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2022-07-04 18:47:47 +03:00
|
|
|
// Load default material (using raylib intenral default shader) for non-instanced mesh drawing
|
|
|
|
// WARNING: Default shader enables vertex color attribute BUT GenMeshCube() does not generate vertex colors, so,
|
|
|
|
// when drawing the color attribute is disabled and a default color value is provided as input for thevertex attribute
|
2022-06-21 22:06:03 +03:00
|
|
|
Material matDefault = LoadMaterialDefault();
|
|
|
|
matDefault.maps[MATERIAL_MAP_DIFFUSE].color = BLUE;
|
2021-03-25 15:41:32 +03:00
|
|
|
|
2023-02-14 22:00:51 +03:00
|
|
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
2020-09-27 11:29:05 +03:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Main game loop
|
2023-02-14 22:00:51 +03:00
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
2020-09-27 11:29:05 +03:00
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
2023-02-14 19:47:21 +03:00
|
|
|
UpdateCamera(&camera, CAMERA_ORBITAL);
|
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
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
|
|
|
|
|
|
|
ClearBackground(RAYWHITE);
|
|
|
|
|
|
|
|
BeginMode3D(camera);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2022-06-21 22:06:03 +03:00
|
|
|
// Draw cube mesh with default material (BLUE)
|
|
|
|
DrawMesh(cube, matDefault, MatrixTranslate(-10.0f, 0.0f, 0.0f));
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2022-06-21 22:06:03 +03:00
|
|
|
// Draw meshes instanced using material containing instancing shader (RED + lighting),
|
|
|
|
// transforms[] for the instances should be provided, they are dynamically
|
|
|
|
// updated in GPU every frame, so we can animate the different mesh instances
|
|
|
|
DrawMeshInstanced(cube, matInstances, transforms, MAX_INSTANCES);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2022-06-21 22:06:03 +03:00
|
|
|
// Draw cube mesh with default material (BLUE)
|
|
|
|
DrawMesh(cube, matDefault, MatrixTranslate(10.0f, 0.0f, 0.0f));
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2022-06-21 22:06:03 +03:00
|
|
|
EndMode3D();
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-09-27 11:29:05 +03:00
|
|
|
DrawFPS(10, 10);
|
|
|
|
|
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2022-06-21 22:06:03 +03:00
|
|
|
RL_FREE(transforms); // Free transforms
|
2022-04-04 11:32:46 +03:00
|
|
|
|
2022-06-21 22:06:03 +03:00
|
|
|
CloseWindow(); // Close window and OpenGL context
|
2020-09-27 11:29:05 +03:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|