Unit1.cpp 3.4 KB

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