fMotionBlurD.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. unit fMotionBlurD;
  2. interface
  3. uses
  4. Winapi.Windows,
  5. Winapi.OpenGL,
  6. System.Classes,
  7. System.SysUtils,
  8. Vcl.Forms,
  9. Vcl.ExtCtrls,
  10. Vcl.Controls,
  11. GLS.Scene,
  12. Stage.VectorTypes,
  13. GLS.Context,
  14. GLS.SceneViewer,
  15. GLS.Cadencer,
  16. GLS.Objects,
  17. GLS.Texture,
  18. GLS.HUDObjects,
  19. GLS.GeomObjects,
  20. Stage.Utils,
  21. GLS.Coordinates,
  22. GLS.BaseClasses, GLS.VectorFileObjects;
  23. type
  24. TFormMotionBlur = class(TForm)
  25. GLScene1: TGLScene;
  26. GLSceneViewer: TGLSceneViewer;
  27. Camera: TGLCamera;
  28. GLCadencer1: TGLCadencer;
  29. Light: TGLLightSource;
  30. Cube: TGLCube;
  31. HUD: TGLHUDSprite;
  32. Torus: TGLTorus;
  33. Timer1: TTimer;
  34. Dodecahedron: TGLDodecahedron;
  35. DummyCube: TGLDummyCube;
  36. Panel1: TPanel;
  37. procedure GLCadencer1Progress(Sender: TObject; const deltaTime,
  38. newTime: Double);
  39. procedure FormCreate(Sender: TObject);
  40. procedure GLSceneViewerPostRender(Sender: TObject);
  41. procedure FormResize(Sender: TObject);
  42. procedure Timer1Timer(Sender: TObject);
  43. procedure FormKeyDown(Sender: TObject; var Key: Word;
  44. Shift: TShiftState);
  45. procedure GLSceneViewerMouseDown(Sender: TObject;
  46. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  47. procedure GLSceneViewerMouseMove(Sender: TObject; Shift: TShiftState;
  48. X, Y: Integer);
  49. private
  50. public
  51. Frames : Integer;
  52. mx, my : Integer;
  53. end;
  54. var
  55. FormMotionBlur: TFormMotionBlur;
  56. implementation
  57. {$R *.dfm}
  58. procedure TFormMotionBlur.FormCreate(Sender: TObject);
  59. begin
  60. Frames:=5;
  61. HUD.Material.FrontProperties.Diffuse.Alpha:=1-1/Frames;
  62. end;
  63. procedure TFormMotionBlur.GLSceneViewerPostRender(Sender: TObject);
  64. begin
  65. // render is done, we transfer it to our hud plane so it can be used
  66. // in the next frame
  67. GLSceneViewer.Buffer.CopyToTexture(HUD.Material.Texture);
  68. end;
  69. procedure TFormMotionBlur.FormResize(Sender: TObject);
  70. var
  71. w, h : Integer;
  72. begin
  73. // Here we resize our texture and plane to follow window dimension changes
  74. // Note that we have to stick to power of two texture dimensions if we don't
  75. // want performance to drop dramatically, this implies we can waste 3/4
  76. // of our texture memory... (f.i. a 513x513 window will require and use
  77. // a 1024x1024 texture)
  78. w:=RoundUpToPowerOf2(GLSceneViewer.Width);
  79. h:=RoundUpToPowerOf2(GLSceneViewer.Height);
  80. HUD.Material.Texture.DestroyHandles;
  81. with ((HUD.Material.Texture.Image) as TGLBlankImage) do begin
  82. Width:=w;
  83. Height:=h;
  84. end;
  85. HUD.Position.X:=w*0.5;
  86. HUD.Position.Y:=GLSceneViewer.Height-h*0.5;
  87. HUD.Width:=w;
  88. HUD.Height:=h;
  89. end;
  90. procedure TFormMotionBlur.GLCadencer1Progress(Sender: TObject; const deltaTime,
  91. newTime: Double);
  92. begin
  93. // make things move
  94. Cube.TurnAngle:=newTime*90;
  95. DummyCube.PitchAngle:=newTime*60;
  96. Dodecahedron.RollAngle:=newTime*15;
  97. end;
  98. procedure TFormMotionBlur.FormKeyDown(Sender: TObject; var Key: Word;
  99. Shift: TShiftState);
  100. begin
  101. // turn on/off VSync, this has an obvious impact on framerate,
  102. // which in turns impacts the motion blur look
  103. if (Key=Ord('S')) or (Key=Ord('V')) then
  104. if GLSceneViewer.VSync=vsmNoSync then
  105. GLSceneViewer.VSync:=vsmSync
  106. else GLSceneViewer.VSync:=vsmNoSync;
  107. // change the number of motion blur frames, and adjust
  108. // the transparency of the plane accordingly
  109. if Key=VK_UP then Inc(Frames);
  110. if (Key=VK_DOWN) and (Frames>0) then Dec(Frames);
  111. if Frames=0 then
  112. HUD.Visible:=False
  113. else begin
  114. HUD.Visible:=True;
  115. HUD.Material.FrontProperties.Diffuse.Alpha:=1-1/(1+Frames);
  116. end;
  117. end;
  118. // standard issue camera movement
  119. procedure TFormMotionBlur.GLSceneViewerMouseDown(Sender: TObject;
  120. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  121. begin
  122. mx:=x; my:=y;
  123. end;
  124. procedure TFormMotionBlur.GLSceneViewerMouseMove(Sender: TObject;
  125. Shift: TShiftState; X, Y: Integer);
  126. begin
  127. if Shift=[ssLeft] then
  128. Camera.MoveAroundTarget(my-y, mx-x);
  129. mx:=x; my:=y;
  130. end;
  131. procedure TFormMotionBlur.Timer1Timer(Sender: TObject);
  132. const
  133. cVSync : array [vsmSync..vsmNoSync] of String = ('VSync ON', 'VSync OFF');
  134. begin
  135. Panel1.Caption:=Format('Motion Blur on %d frames | %s | %f FPS',
  136. [frames, cVSync[GLSceneViewer.VSync], GLSceneViewer.FramesPerSecond]);
  137. GLSceneViewer.ResetPerformanceMonitor;
  138. end;
  139. end.