fParticlesC.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <stdlib.h>
  4. #pragma hdrstop
  5. #include "fParticlesC.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLS.SceneViewer"
  9. #pragma link "Stage.VectorGeometry"
  10. #pragma link "GLS.Behaviours"
  11. #pragma link "GLS.Cadencer"
  12. #pragma link "GLS.Particles"
  13. #pragma link "GLS.Objects"
  14. #pragma link "GLS.Scene"
  15. #pragma link "GLS.BaseClasses"
  16. #pragma link "GLS.Coordinates"
  17. #pragma link "GLS.SceneViewer"
  18. #pragma link "GLS.SceneViewer"
  19. #pragma link "GLS.SimpleNavigation"
  20. #pragma resource "*.dfm"
  21. TFormStars *FormStars;
  22. float random(void)
  23. {
  24. return (float)(rand() & 0xFFF) / (float)0xFFF;
  25. }
  26. //---------------------------------------------------------------------------
  27. __fastcall TFormStars::TFormStars(TComponent * Owner):TForm(Owner)
  28. {
  29. TFileName Path = GetCurrentAssetPath() + "\\texture\\";
  30. // work only with (path + "flare1.bmp") for sprites
  31. Sprite1->Material->Texture->Image->LoadFromFile(Path + "flare1.bmp");
  32. Randomize();
  33. }
  34. //---------------------------------------------------------------------------
  35. void __fastcall TFormStars::GLParticles1ActivateParticle(TObject * Sender,
  36. TGLBaseSceneObject *
  37. particle)
  38. {
  39. // this event is called when a star is activated,
  40. // ie. just before it will be rendered
  41. TGLFaceProperties *fp = ((TGLSprite *) particle)->Material->FrontProperties;
  42. // we pick a random color
  43. fp->Emission->Color = PointMake(random(), random(), random());
  44. // our halo starts transparent
  45. fp->Diffuse->Alpha = 0;
  46. // this is our "birth time"
  47. ((TGLSprite *) particle)->TagFloat = GLCadencer1->CurrentTime;
  48. }
  49. //---------------------------------------------------------------------------
  50. void __fastcall TFormStars::Sprite1Progress(TObject * Sender,
  51. const double deltaTime,
  52. const double newTime)
  53. {
  54. double life;
  55. // calculate for how long we've been living
  56. life = (newTime - ((TGLSprite *) Sender)->TagFloat);
  57. if(life > 10)
  58. // old particle to kill
  59. GLParticles1->KillParticle((TGLSprite *) Sender);
  60. else if(life < 1)
  61. // baby particles become brighter in their 1st second of life...
  62. ((TGLSprite *) Sender)->Material->FrontProperties->Diffuse->Alpha = life;
  63. else // ...and slowly disappear in the darkness
  64. ((TGLSprite *) Sender)->Material->FrontProperties->Diffuse->Alpha =
  65. (9.0 - life) / 9.0;
  66. }
  67. //---------------------------------------------------------------------------
  68. void __fastcall TFormStars::Timer1Timer(TObject * Sender)
  69. {
  70. // every timer, we create a particle at a random position
  71. TGLCoordinates *c = ((TGLSprite *) GLParticles1->CreateParticle())->Position;
  72. c->X = 3 * (random() - 0.5);
  73. c->Y = 3 * (random() - 0.5);
  74. c->Z = 3 * (random() - 0.5);
  75. // infos for the user
  76. Caption = "Stars - "+
  77. Format("%d stars, %.1f FPS", ARRAYOFCONST((GLParticles1->Count - 1,
  78. GLSceneViewer1->FramesPerSecond())));
  79. GLSceneViewer1->ResetPerformanceMonitor();
  80. }
  81. //---------------------------------------------------------------------------
  82. void __fastcall TFormStars::FormResize(TObject * Sender)
  83. {
  84. // change focal so the view will shrink and not just get clipped
  85. GLCamera1->FocalLength = 50 * Width / 280;
  86. }
  87. //---------------------------------------------------------------------------