fTexAnim.pas 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. unit fTexAnim;
  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.ExtCtrls,
  12. Vcl.StdCtrls,
  13. GLS.Cadencer,
  14. GLS.Scene,
  15. GLS.Objects,
  16. GLS.Texture,
  17. GLS.SceneViewer,
  18. GLS.Material,
  19. GLS.Coordinates,
  20. GLS.BaseClasses;
  21. type
  22. TFormTexAnim = class(TForm)
  23. GLScene1: TGLScene;
  24. GLSceneViewer1: TGLSceneViewer;
  25. GLMaterialLibrary1: TGLMaterialLibrary;
  26. GLCamera1: TGLCamera;
  27. Cube1: TGLCube;
  28. GLLightSource1: TGLLightSource;
  29. GLCadencer1: TGLCadencer;
  30. Timer1: TTimer;
  31. Panel1: TPanel;
  32. Button1: TButton;
  33. CBAnimate: TCheckBox;
  34. LabelFPS: TLabel;
  35. procedure Timer1Timer(Sender: TObject);
  36. procedure GLCadencer1Progress(Sender: TObject;
  37. const deltaTime, newTime: Double);
  38. procedure Button1Click(Sender: TObject);
  39. procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  40. public
  41. timeToNextFrame: Double;
  42. end;
  43. var
  44. FormTexAnim: TFormTexAnim;
  45. implementation
  46. {$R *.DFM}
  47. procedure TFormTexAnim.Button1Click(Sender: TObject);
  48. var
  49. i: Integer;
  50. bmp: TBitmap;
  51. begin
  52. // We generate a handful of bitmaps from scratch
  53. // you could also load them from a set of files, extract from an AVI etc.
  54. for i := 0 to 9 do
  55. begin
  56. bmp := TBitmap.Create;
  57. bmp.PixelFormat := pf24bit;
  58. bmp.Width := 60;
  59. bmp.Height := 60;
  60. bmp.Canvas.Font.Name := 'Arial';
  61. bmp.Canvas.Font.Height := 56;
  62. bmp.Canvas.TextOut(15, 5, IntToStr(i));
  63. GLMaterialLibrary1.AddTextureMaterial('IMG' + IntToStr(i), bmp);
  64. bmp.Free;
  65. end;
  66. // Initialize our loop
  67. Cube1.Material.MaterialLibrary := GLMaterialLibrary1;
  68. Cube1.Material.LibMaterialName := 'IMG0';
  69. GLMaterialLibrary1.Tag := 0;
  70. // GUI update
  71. CBAnimate.Enabled := True;
  72. Button1.Enabled := False;
  73. end;
  74. procedure TFormTexAnim.GLCadencer1Progress(Sender: TObject;
  75. const deltaTime, newTime: Double);
  76. begin
  77. // cube turns slowly
  78. Cube1.Turn(deltaTime * 3);
  79. // cycle textures
  80. if CBAnimate.Checked then
  81. begin
  82. // coutdown to next frame
  83. timeToNextFrame := timeToNextFrame - deltaTime;
  84. // if it's time for the next frame
  85. if timeToNextFrame < 0 then
  86. begin
  87. // first, update frame counter (the Tag property in our sample)
  88. // (such a loop is a little overkill, yeah)
  89. while timeToNextFrame < 0 do
  90. begin
  91. timeToNextFrame := timeToNextFrame + 0.2;
  92. GLMaterialLibrary1.Tag := (GLMaterialLibrary1.Tag + 1) mod 10;
  93. end;
  94. // then, we update the material reference
  95. Cube1.Material.LibMaterialName := 'IMG' +
  96. IntToStr(GLMaterialLibrary1.Tag);
  97. end;
  98. end;
  99. end;
  100. procedure TFormTexAnim.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  101. begin
  102. // stop animation
  103. CBAnimate.Checked := False;
  104. end;
  105. procedure TFormTexAnim.Timer1Timer(Sender: TObject);
  106. begin
  107. // standard FPS
  108. LabelFPS.Caption := Format('%.1f FPS', [GLSceneViewer1.FramesPerSecond]);
  109. GLSceneViewer1.ResetPerformanceMonitor;
  110. end;
  111. end.