shaders_mesh_instancing.c 6.1 KB

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