fTrailsD.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. unit fTrailsD;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. System.SysUtils,
  6. System.Classes,
  7. Vcl.Graphics,
  8. Vcl.Controls,
  9. Vcl.Forms,
  10. Vcl.Dialogs,
  11. Vcl.StdCtrls,
  12. Vcl.ComCtrls,
  13. Vcl.ExtCtrls,
  14. GLS.Cadencer,
  15. GLS.Scene,
  16. GLS.Objects,
  17. GLS.GeomObjects,
  18. GLS.SceneViewer,
  19. GLS.Trail,
  20. GLS.Texture,
  21. Stage.VectorGeometry,
  22. GLS.Mesh,
  23. Stage.VectorTypes,
  24. GLS.Coordinates,
  25. GLS.BaseClasses;
  26. type
  27. TFormTrails = class(TForm)
  28. GLScene1: TGLScene;
  29. GLSceneViewer1: TGLSceneViewer;
  30. GLCamera1: TGLCamera;
  31. GLCadencer1: TGLCadencer;
  32. Timer1: TTimer;
  33. GLSphere1: TGLSphere;
  34. Room: TGLSphere;
  35. GLLightSource1: TGLLightSource;
  36. Panel1: TPanel;
  37. TrackBarSpeed: TTrackBar;
  38. Label1: TLabel;
  39. procedure GLCadencer1Progress(Sender: TObject; const deltaTime,
  40. newTime: Double);
  41. procedure GLSceneViewer1MouseDown(Sender: TObject;
  42. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  43. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  44. X, Y: Integer);
  45. procedure Timer1Timer(Sender: TObject);
  46. procedure FormCreate(Sender: TObject);
  47. private
  48. public
  49. mx,my : integer;
  50. Trail: TGLTrail;
  51. Direction : TVector3f;
  52. LastTimeDirectionChange : Double;
  53. end;
  54. var
  55. FormTrails: TFormTrails;
  56. implementation
  57. {$R *.dfm}
  58. procedure TFormTrails.FormCreate(Sender: TObject);
  59. begin
  60. Randomize;
  61. Direction := VectorScale(AffineVectormake(random-0.5,random-0.5,random-0.5),0.5);
  62. with TGLTrail(GLScene1.Objects.AddNewChild(TGLTrail)) do
  63. begin
  64. TrailObject := GLSphere1; // Trail will follow this sphere
  65. TimeLimit := 0.2; // 0.2 seconds trail
  66. VertLimit := 200; // max 200 vertices for trail
  67. MinDistance := 0.1; // minimal distance before adding a segment to the trail
  68. MarkStyle := msFaceCamera; // Trail will be facing the camera (ideal for bullets trails or similar)
  69. MarkWidth := 0.5;
  70. Material.FrontProperties.Diffuse.AsWinColor := clWhite;
  71. end;
  72. end;
  73. procedure TFormTrails.GLCadencer1Progress(Sender: TObject; const deltaTime,
  74. newTime: Double);
  75. begin
  76. GLSphere1.Position.AsAffineVector := VectorAdd(GLSphere1.Position.AsAffineVector,VectorScale(Direction,TrackBarSpeed.Position));
  77. // Keep sphere in place
  78. if VectorLength(GLSphere1.Position.AsAffineVector)> 6 then
  79. begin
  80. // move to previous position
  81. GLSphere1.Position.AsAffineVector := VectorSubtract(GLSphere1.Position.AsAffineVector,VectorScale(Direction,TrackBarSpeed.Position));
  82. // set opposite direction
  83. Direction := VectorNegate(Direction);
  84. // Add some randomness
  85. Direction := VectorScale(AffineVectormake(random-0.5,random-0.5,random-0.5),deltatime*10);
  86. end;
  87. GLSceneViewer1.Invalidate;
  88. end;
  89. procedure TFormTrails.GLSceneViewer1MouseDown(Sender: TObject;
  90. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  91. begin
  92. mx := x;
  93. my := y;
  94. end;
  95. procedure TFormTrails.GLSceneViewer1MouseMove(Sender: TObject;
  96. Shift: TShiftState; X, Y: Integer);
  97. begin
  98. if ssLeft in Shift then
  99. begin
  100. GLCamera1.MoveAroundTarget(my-y,mx-x);
  101. end;
  102. mx := x;
  103. my := y;
  104. end;
  105. procedure TFormTrails.Timer1Timer(Sender: TObject);
  106. begin
  107. caption := Inttostr(Round(GLSceneViewer1.FramesPerSecond));
  108. GLSceneViewer1.ResetPerformanceMonitor;
  109. end;
  110. end.