raylib/examples/shaders/shaders_mesh_instancing.c

148 lines
6.5 KiB
C
Raw Normal View History

/*******************************************************************************************
*
2022-07-20 02:28:37 +03:00
* raylib [shaders] example - Mesh instancing
*
2022-07-20 02:28:37 +03:00
* Example originally created with raylib 3.7, last time updated with raylib 4.2
*
* 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)
*
********************************************************************************************/
#include "raylib.h"
#include "raymath.h"
#define RLIGHTS_IMPLEMENTATION
#include "rlights.h"
#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
#else // PLATFORM_ANDROID, PLATFORM_WEB
2021-03-25 15:41:32 +03:00
#define GLSL_VERSION 100
#endif
#define MAX_INSTANCES 10000
2021-04-23 10:09:34 +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;
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - mesh instancing");
// 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
// Define mesh to be instanced
Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
// Define transforms to be uploaded to GPU for instances
Matrix *transforms = (Matrix *)RL_CALLOC(MAX_INSTANCES, sizeof(Matrix)); // Pre-multiplied transformations passed to rlgl
// Translate and rotate cubes randomly
2021-04-23 10:09:34 +03:00
for (int i = 0; i < MAX_INSTANCES; i++)
{
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;
Matrix rotation = MatrixRotate(axis, angle);
transforms[i] = MatrixMultiply(rotation, translation);
}
// Load lighting shader
Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/lighting_instancing.vs", GLSL_VERSION),
TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION));
// Get shader locations
shader.locs[SHADER_LOC_MATRIX_MVP] = GetShaderLocation(shader, "mvp");
shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocationAttrib(shader, "instanceTransform");
// Set shader value: ambient light level
int ambientLoc = GetShaderLocation(shader, "ambient");
SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, SHADER_UNIFORM_VEC4);
// Create one light
2021-03-25 15:41:32 +03:00
CreateLight(LIGHT_DIRECTIONAL, (Vector3){ 50.0f, 50.0f, 0.0f }, Vector3Zero(), WHITE, shader);
// NOTE: We are assigning the intancing shader to material.shader
// to be used on mesh drawing with DrawMeshInstanced()
Material matInstances = LoadMaterialDefault();
matInstances.shader = shader;
matInstances.maps[MATERIAL_MAP_DIFFUSE].color = RED;
2021-04-22 19:55:24 +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
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
//--------------------------------------------------------------------------------------
// Main game loop
2023-02-14 22:00:51 +03:00
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&camera, CAMERA_ORBITAL);
// Update the light shader with the camera view position
float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
2021-04-22 19:55:24 +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
// 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
// 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
EndMode3D();
2021-04-22 19:55:24 +03:00
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
RL_FREE(transforms); // Free transforms
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}