shaders_mesh_instancing.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Mesh instancing
  4. *
  5. * Example complexity rating: [★★★★] 4/4
  6. *
  7. * Example originally created with raylib 3.7, last time updated with raylib 4.2
  8. *
  9. * Example contributed by seanpringle (@seanpringle) and reviewed by Max (@moliad) and Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2020-2025 seanpringle (@seanpringle), Max (@moliad) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "raymath.h"
  19. #define RLIGHTS_IMPLEMENTATION
  20. #include "rlights.h"
  21. #include <stdlib.h> // Required for: calloc(), free()
  22. #if defined(PLATFORM_DESKTOP)
  23. #define GLSL_VERSION 330
  24. #else // PLATFORM_ANDROID, PLATFORM_WEB
  25. #define GLSL_VERSION 100
  26. #endif
  27. #define MAX_INSTANCES 10000
  28. //------------------------------------------------------------------------------------
  29. // Program main entry point
  30. //------------------------------------------------------------------------------------
  31. int main(void)
  32. {
  33. // Initialization
  34. //--------------------------------------------------------------------------------------
  35. const int screenWidth = 800;
  36. const int screenHeight = 450;
  37. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - mesh instancing");
  38. // Define the camera to look into our 3d world
  39. Camera camera = { 0 };
  40. camera.position = (Vector3){ -125.0f, 125.0f, -125.0f }; // Camera position
  41. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  42. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  43. camera.fovy = 45.0f; // Camera field-of-view Y
  44. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  45. // Define mesh to be instanced
  46. Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
  47. // Define transforms to be uploaded to GPU for instances
  48. Matrix *transforms = (Matrix *)RL_CALLOC(MAX_INSTANCES, sizeof(Matrix)); // Pre-multiplied transformations passed to rlgl
  49. // Translate and rotate cubes randomly
  50. for (int i = 0; i < MAX_INSTANCES; i++)
  51. {
  52. Matrix translation = MatrixTranslate((float)GetRandomValue(-50, 50), (float)GetRandomValue(-50, 50), (float)GetRandomValue(-50, 50));
  53. Vector3 axis = Vector3Normalize((Vector3){ (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360) });
  54. float angle = (float)GetRandomValue(0, 180)*DEG2RAD;
  55. Matrix rotation = MatrixRotate(axis, angle);
  56. transforms[i] = MatrixMultiply(rotation, translation);
  57. }
  58. // Load lighting shader
  59. Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/lighting_instancing.vs", GLSL_VERSION),
  60. TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION));
  61. // Get shader locations
  62. shader.locs[SHADER_LOC_MATRIX_MVP] = GetShaderLocation(shader, "mvp");
  63. shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
  64. // Set shader value: ambient light level
  65. int ambientLoc = GetShaderLocation(shader, "ambient");
  66. SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, SHADER_UNIFORM_VEC4);
  67. // Create one light
  68. CreateLight(LIGHT_DIRECTIONAL, (Vector3){ 50.0f, 50.0f, 0.0f }, Vector3Zero(), WHITE, shader);
  69. // NOTE: We are assigning the intancing shader to material.shader
  70. // to be used on mesh drawing with DrawMeshInstanced()
  71. Material matInstances = LoadMaterialDefault();
  72. matInstances.shader = shader;
  73. matInstances.maps[MATERIAL_MAP_DIFFUSE].color = RED;
  74. // Load default material (using raylib intenral default shader) for non-instanced mesh drawing
  75. // WARNING: Default shader enables vertex color attribute BUT GenMeshCube() does not generate vertex colors, so,
  76. // when drawing the color attribute is disabled and a default color value is provided as input for thevertex attribute
  77. Material matDefault = LoadMaterialDefault();
  78. matDefault.maps[MATERIAL_MAP_DIFFUSE].color = BLUE;
  79. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  80. //--------------------------------------------------------------------------------------
  81. // Main game loop
  82. while (!WindowShouldClose()) // Detect window close button or ESC key
  83. {
  84. // Update
  85. //----------------------------------------------------------------------------------
  86. UpdateCamera(&camera, CAMERA_ORBITAL);
  87. // Update the light shader with the camera view position
  88. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  89. SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
  90. //----------------------------------------------------------------------------------
  91. // Draw
  92. //----------------------------------------------------------------------------------
  93. BeginDrawing();
  94. ClearBackground(RAYWHITE);
  95. BeginMode3D(camera);
  96. // Draw cube mesh with default material (BLUE)
  97. DrawMesh(cube, matDefault, MatrixTranslate(-10.0f, 0.0f, 0.0f));
  98. // Draw meshes instanced using material containing instancing shader (RED + lighting),
  99. // transforms[] for the instances should be provided, they are dynamically
  100. // updated in GPU every frame, so we can animate the different mesh instances
  101. DrawMeshInstanced(cube, matInstances, transforms, MAX_INSTANCES);
  102. // Draw cube mesh with default material (BLUE)
  103. DrawMesh(cube, matDefault, MatrixTranslate(10.0f, 0.0f, 0.0f));
  104. EndMode3D();
  105. DrawFPS(10, 10);
  106. EndDrawing();
  107. //----------------------------------------------------------------------------------
  108. }
  109. // De-Initialization
  110. //--------------------------------------------------------------------------------------
  111. RL_FREE(transforms); // Free transforms
  112. CloseWindow(); // Close window and OpenGL context
  113. //--------------------------------------------------------------------------------------
  114. return 0;
  115. }