particles.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  15. SetTargetFPS(60);
  16. R3D_SetBloomMode(R3D_BLOOM_ADDITIVE);
  17. R3D_SetBackgroundColor((Color) { 4, 4, 4 });
  18. R3D_SetAmbientColor(BLACK);
  19. sphere = R3D_GenMeshSphere(0.1f, 16, 32, true);
  20. material = R3D_GetDefaultMaterial();
  21. material.emission.color = (Color) { 255, 0, 0, 255 };
  22. material.emission.energy = 1.0f;
  23. curve = R3D_LoadInterpolationCurve(3);
  24. R3D_AddKeyframe(&curve, 0.0f, 0.0f);
  25. R3D_AddKeyframe(&curve, 0.5f, 1.0f);
  26. R3D_AddKeyframe(&curve, 1.0f, 0.0f);
  27. particles = R3D_LoadParticleSystem(2048);
  28. particles.initialVelocity = (Vector3){ 0, 10.0f, 0 };
  29. particles.scaleOverLifetime = &curve;
  30. particles.spreadAngle = 45.0f;
  31. particles.emissionRate = 2048;
  32. particles.lifetime = 2.0f;
  33. R3D_CalculateParticleSystemBoundingBox(&particles);
  34. camera = (Camera3D) {
  35. .position = (Vector3) { -7, 7, -7 },
  36. .target = (Vector3) { 0, 1, 0 },
  37. .up = (Vector3) { 0, 1, 0 },
  38. .fovy = 60.0f,
  39. .projection = CAMERA_PERSPECTIVE
  40. };
  41. return "[r3d] - Particles example";
  42. }
  43. void Update(float delta)
  44. {
  45. UpdateCamera(&camera, CAMERA_ORBITAL);
  46. R3D_UpdateParticleSystem(&particles, GetFrameTime());
  47. }
  48. void Draw(void)
  49. {
  50. R3D_Begin(camera);
  51. R3D_DrawParticleSystem(&particles, &sphere, &material);
  52. R3D_End();
  53. BeginMode3D(camera);
  54. DrawBoundingBox(particles.aabb, GREEN);
  55. EndMode3D();
  56. DrawFPS(10, 10);
  57. }
  58. void Close(void)
  59. {
  60. R3D_UnloadInterpolationCurve(curve);
  61. R3D_UnloadParticleSystem(&particles);
  62. R3D_UnloadMesh(&sphere);
  63. R3D_UnloadMaterial(&material);
  64. R3D_Close();
  65. }