shaders_rlgl_mesh_instanced.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.0 (www.raylib.com)
  8. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. *
  10. * Copyright (c) 2018 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #define GRAPHICS_API_OPENGL_33
  14. #define GLSL_VERSION 330
  15. #include <stdlib.h>
  16. #include "raylib.h"
  17. #include "raymath.h"
  18. #include "rlgl.h"
  19. #define RLIGHTS_IMPLEMENTATION
  20. #include "rlights.h"
  21. //------------------------------------------------------------------------------------
  22. // Program main entry point
  23. //------------------------------------------------------------------------------------
  24. int main(void)
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. const int screenWidth = 1024;
  29. const int screenHeight = 768;
  30. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  31. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - rlgl module usage for instanced meshes");
  32. // Define the camera to look into our 3d world
  33. Camera camera = { 0 };
  34. camera.position = (Vector3){ 125.0f, 125.0f, 125.0f };
  35. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  36. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  37. camera.fovy = 45.0f;
  38. camera.type = CAMERA_PERSPECTIVE;
  39. SetCameraMode(camera, CAMERA_FREE);
  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. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  75. //--------------------------------------------------------------------------------------
  76. // Main game loop
  77. while (!WindowShouldClose()) // Detect window close button or ESC key
  78. {
  79. // Update
  80. //----------------------------------------------------------------------------------
  81. UpdateCamera(&camera);
  82. // Update the light shader with the camera view position
  83. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  84. SetShaderValue(shader, shader.locs[LOC_VECTOR_VIEW], cameraPos, UNIFORM_VEC3);
  85. // Apply per-instance rotations
  86. for (int i = 0; i < count; i++)
  87. {
  88. rotations[i] = MatrixMultiply(rotations[i], rotationsInc[i]);
  89. transforms[i] = MatrixMultiply(rotations[i], translations[i]);
  90. }
  91. //----------------------------------------------------------------------------------
  92. // Draw
  93. //----------------------------------------------------------------------------------
  94. BeginDrawing();
  95. ClearBackground(RAYWHITE);
  96. BeginMode3D(camera);
  97. rlDrawMeshInstanced(cube, material, transforms, count);
  98. EndMode3D();
  99. DrawText("A CUBE OF DANCING CUBES!", 400, 10, 20, MAROON);
  100. DrawFPS(10, 10);
  101. EndDrawing();
  102. //----------------------------------------------------------------------------------
  103. }
  104. // De-Initialization
  105. //--------------------------------------------------------------------------------------
  106. CloseWindow(); // Close window and OpenGL context
  107. //--------------------------------------------------------------------------------------
  108. return 0;
  109. }