particles.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <r3d/r3d.h>
  2. int main(void)
  3. {
  4. // Initialize window
  5. InitWindow(800, 450, "[r3d] - Particles example");
  6. SetTargetFPS(60);
  7. // Initialize R3D
  8. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  9. // Set environment
  10. R3D_ENVIRONMENT_SET(background.color, (Color){4, 4, 4});
  11. R3D_ENVIRONMENT_SET(ambient.color, BLACK);
  12. R3D_ENVIRONMENT_SET(bloom.mode, R3D_BLOOM_ADDITIVE);
  13. // Create particle mesh and material
  14. R3D_Mesh sphere = R3D_GenMeshSphere(0.1f, 16, 32);
  15. R3D_Material material = R3D_GetDefaultMaterial();
  16. material.emission.color = (Color){255, 0, 0, 255};
  17. material.emission.energy = 1.0f;
  18. // Create interpolation curve for particle scaling
  19. R3D_InterpolationCurve curve = R3D_LoadInterpolationCurve(3);
  20. R3D_AddKeyframe(&curve, 0.0f, 0.0f);
  21. R3D_AddKeyframe(&curve, 0.5f, 1.0f);
  22. R3D_AddKeyframe(&curve, 1.0f, 0.0f);
  23. // Create particle system
  24. R3D_ParticleSystem particles = R3D_LoadParticleSystem(2048);
  25. particles.initialVelocity = (Vector3){0, 10.0f, 0};
  26. particles.scaleOverLifetime = &curve;
  27. particles.spreadAngle = 45.0f;
  28. particles.emissionRate = 2048;
  29. particles.lifetime = 2.0f;
  30. R3D_CalculateParticleSystemBoundingBox(&particles);
  31. // Setup camera
  32. Camera3D camera = {
  33. .position = {-7, 7, -7},
  34. .target = {0, 1, 0},
  35. .up = {0, 1, 0},
  36. .fovy = 60.0f,
  37. .projection = CAMERA_PERSPECTIVE
  38. };
  39. // Main loop
  40. while (!WindowShouldClose())
  41. {
  42. UpdateCamera(&camera, CAMERA_ORBITAL);
  43. R3D_UpdateParticleSystem(&particles, GetFrameTime());
  44. BeginDrawing();
  45. ClearBackground(RAYWHITE);
  46. R3D_Begin(camera);
  47. R3D_DrawParticleSystem(&particles, &sphere, &material);
  48. R3D_End();
  49. // Draw bounding box
  50. BeginMode3D(camera);
  51. DrawBoundingBox(particles.aabb, GREEN);
  52. EndMode3D();
  53. DrawFPS(10, 10);
  54. EndDrawing();
  55. }
  56. // Cleanup
  57. R3D_UnloadInterpolationCurve(curve);
  58. R3D_UnloadParticleSystem(&particles);
  59. R3D_UnloadMesh(&sphere);
  60. R3D_UnloadMaterial(&material);
  61. R3D_Close();
  62. CloseWindow();
  63. return 0;
  64. }