particles.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "./common.h"
  2. /* === Resources === */
  3. static R3D_Mesh sphere = { 0 };
  4. static R3D_Material material = { 0 };
  5. static R3D_Skybox skybox = { 0 };
  6. static Camera3D camera = { 0 };
  7. static R3D_InterpolationCurve curve = { 0 };
  8. static R3D_ParticleSystem particles = { 0 };
  9. /* === Example === */
  10. const char* Init(void)
  11. {
  12. /* --- Initialize R3D with its internal resolution --- */
  13. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  14. SetTargetFPS(60);
  15. /* --- Setup the background color and ambient light --- */
  16. R3D_SetBackgroundColor((Color) { 4, 4, 4 });
  17. R3D_SetAmbientColor(BLACK);
  18. /* --- Activate bloom in additive mode --- */
  19. R3D_SetBloomMode(R3D_BLOOM_ADDITIVE);
  20. /* --- Gen a sphere as particle mesh --- */
  21. sphere = R3D_GenMeshSphere(0.1f, 16, 32);
  22. /* --- Setup material of particle mesh --- */
  23. material = R3D_GetDefaultMaterial();
  24. material.emission.color = (Color) { 255, 0, 0, 255 };
  25. material.emission.energy = 1.0f;
  26. /* --- Create scale over time interpolation curve for particle system --- */
  27. curve = R3D_LoadInterpolationCurve(3);
  28. R3D_AddKeyframe(&curve, 0.0f, 0.0f);
  29. R3D_AddKeyframe(&curve, 0.5f, 1.0f);
  30. R3D_AddKeyframe(&curve, 1.0f, 0.0f);
  31. /* --- Create a particle system --- */
  32. particles = R3D_LoadParticleSystem(2048);
  33. particles.initialVelocity = (Vector3){ 0, 10.0f, 0 };
  34. particles.scaleOverLifetime = &curve;
  35. particles.spreadAngle = 45.0f;
  36. particles.emissionRate = 2048;
  37. particles.lifetime = 2.0f;
  38. /* --- Calculates the bounding box of the particle system (can be used for frustum culling) --- */
  39. R3D_CalculateParticleSystemBoundingBox(&particles);
  40. /* --- Setup the camera --- */
  41. camera = (Camera3D) {
  42. .position = (Vector3) { -7, 7, -7 },
  43. .target = (Vector3) { 0, 1, 0 },
  44. .up = (Vector3) { 0, 1, 0 },
  45. .fovy = 60.0f,
  46. .projection = CAMERA_PERSPECTIVE
  47. };
  48. return "[r3d] - Particles example";
  49. }
  50. void Update(float delta)
  51. {
  52. UpdateCamera(&camera, CAMERA_ORBITAL);
  53. R3D_UpdateParticleSystem(&particles, GetFrameTime());
  54. }
  55. void Draw(void)
  56. {
  57. R3D_Begin(camera);
  58. R3D_DrawParticleSystem(&particles, &sphere, &material);
  59. R3D_End();
  60. BeginMode3D(camera);
  61. DrawBoundingBox(particles.aabb, GREEN);
  62. EndMode3D();
  63. DrawFPS(10, 10);
  64. }
  65. void Close(void)
  66. {
  67. R3D_UnloadInterpolationCurve(curve);
  68. R3D_UnloadParticleSystem(&particles);
  69. R3D_UnloadMesh(&sphere);
  70. R3D_UnloadMaterial(&material);
  71. R3D_Close();
  72. }