particles.c 2.5 KB

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