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