shaders_rlgl_mesh_instanced.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - rlgl module usage for instanced meshes
  4. *
  5. * This example uses [rlgl] module funtionality (pseudo-OpenGL 1.1 style coding)
  6. *
  7. * This example has been created using raylib 3.5 (www.raylib.com)
  8. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. *
  10. * Example contributed by @seanpringle and reviewed by Ramon Santamaria (@raysan5)
  11. *
  12. * Copyright (c) 2020 @seanpringle
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include "raymath.h"
  17. #include "rlgl.h"
  18. #define RLIGHTS_IMPLEMENTATION
  19. #include "rlights.h"
  20. #include <stdlib.h>
  21. #define GLSL_VERSION 330
  22. //------------------------------------------------------------------------------------
  23. // Program main entry point
  24. //------------------------------------------------------------------------------------
  25. int main(void)
  26. {
  27. // Initialization
  28. //--------------------------------------------------------------------------------------
  29. const int screenWidth = 800;
  30. const int screenHeight = 450;
  31. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  32. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - rlgl mesh instanced");
  33. // Define the camera to look into our 3d world
  34. Camera camera = { 0 };
  35. camera.position = (Vector3){ 125.0f, 125.0f, 125.0f };
  36. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  37. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  38. camera.fovy = 45.0f;
  39. camera.type = CAMERA_PERSPECTIVE;
  40. const int count = 10000; // Number of instances to display
  41. Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
  42. Matrix *rotations = RL_MALLOC(count*sizeof(Matrix)); // Rotation state of instances
  43. Matrix *rotationsInc = RL_MALLOC(count*sizeof(Matrix)); // Per-frame rotation animation of instances
  44. Matrix *translations = RL_MALLOC(count*sizeof(Matrix)); // Locations of instances
  45. // Scatter random cubes around
  46. for (int i = 0; i < count; i++)
  47. {
  48. float x = GetRandomValue(-50, 50);
  49. float y = GetRandomValue(-50, 50);
  50. float z = GetRandomValue(-50, 50);
  51. translations[i] = MatrixTranslate(x, y, z);
  52. x = GetRandomValue(0, 360);
  53. y = GetRandomValue(0, 360);
  54. z = GetRandomValue(0, 360);
  55. Vector3 axis = Vector3Normalize((Vector3){x, y, z});
  56. float angle = (float)GetRandomValue(0, 10) * DEG2RAD;
  57. rotationsInc[i] = MatrixRotate(axis, angle);
  58. rotations[i] = MatrixIdentity();
  59. }
  60. Matrix *transforms = RL_MALLOC(count*sizeof(Matrix)); // Pre-multiplied transformations passed to rlgl
  61. Shader shader = LoadShader(FormatText("resources/shaders/glsl%i/base_lighting_instanced.vs", GLSL_VERSION),
  62. FormatText("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION));
  63. // Get some shader loactions
  64. shader.locs[LOC_MATRIX_MVP] = GetShaderLocation(shader, "mvp");
  65. shader.locs[LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
  66. shader.locs[LOC_MATRIX_MODEL] = GetShaderLocationAttrib(shader, "instance");
  67. // Ambient light level
  68. int ambientLoc = GetShaderLocation(shader, "ambient");
  69. SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, UNIFORM_VEC4);
  70. CreateLight(LIGHT_DIRECTIONAL, (Vector3){ 50, 50, 0 }, Vector3Zero(), WHITE, shader);
  71. Material material = LoadMaterialDefault();
  72. material.shader = shader;
  73. material.maps[MAP_DIFFUSE].color = RED;
  74. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  75. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  76. //--------------------------------------------------------------------------------------
  77. // Main game loop
  78. while (!WindowShouldClose()) // Detect window close button or ESC key
  79. {
  80. // Update
  81. //----------------------------------------------------------------------------------
  82. UpdateCamera(&camera);
  83. // Update the light shader with the camera view position
  84. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  85. SetShaderValue(shader, shader.locs[LOC_VECTOR_VIEW], cameraPos, UNIFORM_VEC3);
  86. // Apply per-instance rotations
  87. for (int i = 0; i < count; i++)
  88. {
  89. rotations[i] = MatrixMultiply(rotations[i], rotationsInc[i]);
  90. transforms[i] = MatrixMultiply(rotations[i], translations[i]);
  91. }
  92. //----------------------------------------------------------------------------------
  93. // Draw
  94. //----------------------------------------------------------------------------------
  95. BeginDrawing();
  96. ClearBackground(RAYWHITE);
  97. BeginMode3D(camera);
  98. rlDrawMeshInstanced(cube, material, transforms, count);
  99. EndMode3D();
  100. DrawText("A CUBE OF DANCING CUBES!", 490, 10, 20, MAROON);
  101. DrawFPS(10, 10);
  102. EndDrawing();
  103. //----------------------------------------------------------------------------------
  104. }
  105. // De-Initialization
  106. //--------------------------------------------------------------------------------------
  107. CloseWindow(); // Close window and OpenGL context
  108. //--------------------------------------------------------------------------------------
  109. return 0;
  110. }