instanced.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "r3d/r3d_instance.h"
  2. #include <r3d/r3d.h>
  3. #include <raymath.h>
  4. #define INSTANCE_COUNT 1000
  5. int main(void)
  6. {
  7. // Initialize window
  8. InitWindow(800, 450, "[r3d] - Instanced rendering example");
  9. SetTargetFPS(60);
  10. // Initialize R3D
  11. R3D_Init(GetScreenWidth(), GetScreenHeight());
  12. // Set ambient light
  13. R3D_ENVIRONMENT_SET(ambient.color, DARKGRAY);
  14. // Create cube mesh and default material
  15. R3D_Mesh mesh = R3D_GenMeshCube(1, 1, 1);
  16. R3D_Material material = R3D_GetDefaultMaterial();
  17. // Generate random transforms and colors for instances
  18. R3D_InstanceBuffer instances = R3D_LoadInstanceBuffer(INSTANCE_COUNT, R3D_INSTANCE_POSITION | R3D_INSTANCE_ROTATION | R3D_INSTANCE_SCALE | R3D_INSTANCE_COLOR);
  19. Vector3* positions = R3D_MapInstances(instances, R3D_INSTANCE_POSITION);
  20. Quaternion* rotations = R3D_MapInstances(instances, R3D_INSTANCE_ROTATION);
  21. Vector3* scales = R3D_MapInstances(instances, R3D_INSTANCE_SCALE);
  22. Color* colors = R3D_MapInstances(instances, R3D_INSTANCE_COLOR);
  23. for (int i = 0; i < INSTANCE_COUNT; i++)
  24. {
  25. positions[i] = (Vector3) {
  26. (float)GetRandomValue(-50000, 50000) / 1000,
  27. (float)GetRandomValue(-50000, 50000) / 1000,
  28. (float)GetRandomValue(-50000, 50000) / 1000
  29. };
  30. rotations[i] = QuaternionFromEuler(
  31. (float)GetRandomValue(-314000, 314000) / 100000,
  32. (float)GetRandomValue(-314000, 314000) / 100000,
  33. (float)GetRandomValue(-314000, 314000) / 100000
  34. );
  35. scales[i] = (Vector3) {
  36. (float)GetRandomValue(100, 2000) / 1000,
  37. (float)GetRandomValue(100, 2000) / 1000,
  38. (float)GetRandomValue(100, 2000) / 1000
  39. };
  40. colors[i] = ColorFromHSV(
  41. (float)GetRandomValue(0, 360000) / 1000, 1.0f, 1.0f
  42. );
  43. }
  44. R3D_UnmapInstances(instances, R3D_INSTANCE_POSITION | R3D_INSTANCE_ROTATION | R3D_INSTANCE_SCALE | R3D_INSTANCE_COLOR);
  45. // Setup directional light
  46. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  47. R3D_SetLightDirection(light, (Vector3){0, -1, 0});
  48. R3D_SetLightActive(light, true);
  49. // Setup camera
  50. Camera3D camera = {
  51. .position = {0, 2, 2},
  52. .target = {0, 0, 0},
  53. .up = {0, 1, 0},
  54. .fovy = 60
  55. };
  56. // Capture mouse
  57. DisableCursor();
  58. // Main loop
  59. while (!WindowShouldClose())
  60. {
  61. UpdateCamera(&camera, CAMERA_FREE);
  62. BeginDrawing();
  63. ClearBackground(RAYWHITE);
  64. R3D_Begin(camera);
  65. R3D_DrawMeshInstanced(mesh, material, instances, INSTANCE_COUNT);
  66. R3D_End();
  67. DrawFPS(10, 10);
  68. EndDrawing();
  69. }
  70. // Cleanup
  71. R3D_UnloadInstanceBuffer(instances);
  72. R3D_UnloadMaterial(material);
  73. R3D_UnloadMesh(mesh);
  74. R3D_Close();
  75. CloseWindow();
  76. return 0;
  77. }