instanced.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "./common.h"
  2. #define INSTANCE_COUNT 1000
  3. /* === Resources === */
  4. static Camera3D camera = { 0 };
  5. static Mesh mesh = { 0 };
  6. static Material material = { 0 };
  7. static Matrix transforms[INSTANCE_COUNT] = {0};
  8. static Color colors[INSTANCE_COUNT] = {0};
  9. /* === Examples === */
  10. const char* Init(void)
  11. {
  12. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  13. SetTargetFPS(60);
  14. mesh = GenMeshCube(1, 1, 1);
  15. material = LoadMaterialDefault();
  16. R3D_SetMaterialOcclusion(&material, NULL, 1.0f);
  17. R3D_SetMaterialRoughness(&material, NULL, 0.5f);
  18. R3D_SetMaterialMetalness(&material, NULL, 0.5f);
  19. //GenMeshTangents(&mesh);
  20. for (int i = 0; i < INSTANCE_COUNT; i++) {
  21. Matrix translate = MatrixTranslate(
  22. (float)GetRandomValue(-50000, 50000) / 1000,
  23. (float)GetRandomValue(-50000, 50000) / 1000,
  24. (float)GetRandomValue(-50000, 50000) / 1000
  25. );
  26. Matrix rotate = MatrixRotateXYZ((Vector3) {
  27. (float)GetRandomValue(-314000, 314000) / 100000,
  28. (float)GetRandomValue(-314000, 314000) / 100000,
  29. (float)GetRandomValue(-314000, 314000) / 100000
  30. });
  31. Matrix scale = MatrixScale(
  32. (float)GetRandomValue(100, 2000) / 1000,
  33. (float)GetRandomValue(100, 2000) / 1000,
  34. (float)GetRandomValue(100, 2000) / 1000
  35. );
  36. transforms[i] = MatrixMultiply(MatrixMultiply(scale, rotate), translate);
  37. colors[i] = ColorFromHSV((float)GetRandomValue(0, 360000) / 1000, 1.0f, 1.0f);
  38. }
  39. camera = (Camera3D){
  40. .position = (Vector3) { 0, 2, 2 },
  41. .target = (Vector3) { 0, 0, 0 },
  42. .up = (Vector3) { 0, 1, 0 },
  43. .fovy = 60,
  44. };
  45. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  46. {
  47. R3D_SetLightDirection(light, (Vector3) { 0, -1, 0 });
  48. R3D_SetLightActive(light, true);
  49. }
  50. DisableCursor();
  51. return "[r3d] - instanced example";
  52. }
  53. void Update(float delta)
  54. {
  55. UpdateCamera(&camera, CAMERA_FREE);
  56. }
  57. void Draw(void)
  58. {
  59. R3D_Begin(camera);
  60. R3D_DrawMeshInstancedEx(mesh, material, transforms, colors, INSTANCE_COUNT);
  61. R3D_End();
  62. DrawFPS(10, 10);
  63. }
  64. void Close(void)
  65. {
  66. UnloadMaterial(material);
  67. UnloadMesh(mesh);
  68. R3D_Close();
  69. }