fParticlesD.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. unit fParticlesD;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. System.SysUtils,
  6. System.Classes,
  7. Vcl.Forms,
  8. Vcl.StdCtrls,
  9. Vcl.ExtCtrls,
  10. Vcl.Controls,
  11. GLS.Scene,
  12. GLS.Objects,
  13. GLS.Particles,
  14. GLS.Behaviours,
  15. Stage.VectorGeometry,
  16. GLS.PersistentClasses,
  17. GLS.Cadencer,
  18. Stage.VectorTypes,
  19. GLS.SceneViewer,
  20. GLS.Coordinates,
  21. GLS.BaseClasses,
  22. Stage.Utils, GLS.SimpleNavigation;
  23. type
  24. TFormParticles = class(TForm)
  25. GLSceneViewer1: TGLSceneViewer;
  26. GLScene1: TGLScene;
  27. GLCamera1: TGLCamera;
  28. GLParticles1: TGLParticles;
  29. Sprite1: TGLSprite;
  30. GLCadencer1: TGLCadencer;
  31. Timer1: TTimer;
  32. GLSimpleNavigation1: TGLSimpleNavigation;
  33. procedure GLParticles1ActivateParticle(Sender: TObject;
  34. particle: TGLBaseSceneObject);
  35. procedure Sprite1Progress(Sender: TObject;
  36. const deltaTime, newTime: Double);
  37. procedure Timer1Timer(Sender: TObject);
  38. procedure FormCreate(Sender: TObject);
  39. procedure FormResize(Sender: TObject);
  40. public
  41. end;
  42. var
  43. FormParticles: TFormParticles;
  44. implementation
  45. {$R *.DFM}
  46. procedure TFormParticles.FormCreate(Sender: TObject);
  47. begin
  48. var Path: TFileName := GetCurrentAssetPath() + '\texture\';
  49. Sprite1.Material.Texture.Image.LoadFromFile(Path + 'flare1.bmp');
  50. // if we don't do this, our random won't look like random
  51. Randomize;
  52. end;
  53. procedure TFormParticles.GLParticles1ActivateParticle(Sender: TObject;
  54. particle: TGLBaseSceneObject);
  55. begin
  56. // this event is called when a particle is activated,
  57. // ie. just before it will be rendered we pick a random color
  58. TGLSprite(Particle).Material.FrontProperties.Emission.Color := PointMake(Random, Random, Random);
  59. // our halo starts transparent
  60. TGLSprite(Particle).Material.FrontProperties.Diffuse.Alpha := 0;
  61. // this is our "birth time"
  62. TGLSprite(Particle).TagFloat := GLCadencer1.CurrentTime;
  63. end;
  64. procedure TFormParticles.Sprite1Progress(Sender: TObject;
  65. const deltaTime, newTime: Double);
  66. var
  67. life: Double;
  68. begin
  69. with TGLSprite(Sender) do
  70. begin
  71. // calculate for how long we've been living
  72. life := (newTime - TagFloat);
  73. if life > 10 then
  74. // old particle to kill
  75. GLParticles1.KillParticle(TGLSprite(Sender))
  76. else if life < 1 then
  77. // baby particles become brighter in their 1st second of life...
  78. Material.FrontProperties.Diffuse.Alpha := life
  79. else // ...and slowly disappear in the darkness
  80. Material.FrontProperties.Diffuse.Alpha := (9 - life) / 9;
  81. end;
  82. end;
  83. procedure TFormParticles.Timer1Timer(Sender: TObject);
  84. begin
  85. // every timer, we create a particle at a random position
  86. with TGLSprite(GLParticles1.CreateParticle).Position do
  87. begin
  88. X := 3 * (Random - 0.5);
  89. Y := 3 * (Random - 0.5);
  90. Z := 3 * (Random - 0.5);
  91. end;
  92. // infos for the user
  93. Caption := 'Particles - ' + Format('%d particles, %.1f FPS',
  94. [GLParticles1.Count - 1, GLSceneViewer1.FramesPerSecond]);
  95. GLSceneViewer1.ResetPerformanceMonitor;
  96. end;
  97. procedure TFormParticles.FormResize(Sender: TObject);
  98. begin
  99. // change focal so the view will shrink and not just get clipped
  100. GLCamera1.FocalLength := 50 * Width / 280;
  101. end;
  102. end.