fMotionBlurC.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "fMotionBlurC.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLS.BaseClasses"
  9. #pragma link "GLS.Cadencer"
  10. #pragma link "GLS.Coordinates"
  11. #pragma link "GLS.GeomObjects"
  12. #pragma link "GLS.HUDObjects"
  13. #pragma link "GLS.Objects"
  14. #pragma link "GLS.Scene"
  15. #pragma link "GLS.SceneViewer"
  16. #pragma link "GLS.VectorFileObjects"
  17. #pragma resource "*.dfm"
  18. TFormMotionBlur *FormMotionBlur;
  19. //---------------------------------------------------------------------------
  20. __fastcall TFormMotionBlur::TFormMotionBlur(TComponent* Owner)
  21. : TForm(Owner)
  22. {
  23. }
  24. //---------------------------------------------------------------------------
  25. void __fastcall TFormMotionBlur::FormCreate(TObject *Sender)
  26. {
  27. Frames = 5;
  28. HUD->Material->FrontProperties->Diffuse->Alpha = 1.0 - (float)1.00/Frames;
  29. }
  30. //---------------------------------------------------------------------------
  31. void __fastcall TFormMotionBlur::GLSceneViewerPostRender(TObject *Sender)
  32. {
  33. // render is done, we transfer it to our hud plane so it can be used
  34. // in the next frame
  35. GLSceneViewer->Buffer->CopyToTexture(HUD->Material->Texture);
  36. }
  37. //---------------------------------------------------------------------------
  38. void __fastcall TFormMotionBlur::FormResize(TObject *Sender)
  39. {
  40. int w, h;
  41. // Here we resize our texture and plane to follow window dimension changes
  42. // Note that we have to stick to power of two texture dimensions if we don't
  43. // want performance to drop dramatically, this implies we can waste 3/4
  44. // of our texture memory... (f.i. a 513x513 window will require and use
  45. // a 1024x1024 texture)
  46. w = RoundUpToPowerOf2(GLSceneViewer->Width);
  47. h = RoundUpToPowerOf2(GLSceneViewer->Height);
  48. HUD->Material->Texture->DestroyHandles();
  49. /*
  50. HUD->Material->Texture->Image->Width = w;
  51. HUD->Material->Texture->Image->Height = h;
  52. */
  53. HUD->Position->X = w*0.5;
  54. HUD->Position->Y = GLSceneViewer->Height - h*0.5;
  55. HUD->Width = w;
  56. HUD->Height = h;
  57. }
  58. //---------------------------------------------------------------------------
  59. void __fastcall TFormMotionBlur::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  60. const double newTime)
  61. {
  62. // make things move
  63. Cube->TurnAngle = newTime*90;
  64. DummyCube->PitchAngle = newTime*60;
  65. Dodecahedron->RollAngle = newTime*15;
  66. }
  67. //---------------------------------------------------------------------------
  68. void __fastcall TFormMotionBlur::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
  69. {
  70. // turn on/off VSync, this has an obvious impact on framerate,
  71. // which in turns impacts the motion blur look
  72. if ((IsKeyDown('s') || IsKeyDown('S'))
  73. || (IsKeyDown('v') || IsKeyDown('V')))
  74. if (GLSceneViewer->VSync == vsmNoSync)
  75. GLSceneViewer->VSync = vsmSync;
  76. else
  77. GLSceneViewer->VSync = vsmNoSync;
  78. // change the number of motion blur frames, and adjust
  79. // the transparency of the plane accordingly
  80. if (Key == VK_UP) Frames++;
  81. if ((Key == VK_DOWN) && (Frames>0)) Frames--;
  82. if (Frames == 0)
  83. HUD->Visible = false;
  84. else
  85. {
  86. HUD->Visible = true;
  87. HUD->Material->FrontProperties->Diffuse->Alpha = 1 - (float)1/(1+Frames);
  88. }
  89. }
  90. // standard issue camera movement
  91. //---------------------------------------------------------------------------
  92. void __fastcall TFormMotionBlur::GLSceneViewerMouseDown(TObject *Sender, TMouseButton Button,
  93. TShiftState Shift, int X, int Y)
  94. {
  95. mx = X; my = Y;
  96. }
  97. //---------------------------------------------------------------------------
  98. void __fastcall TFormMotionBlur::GLSceneViewerMouseMove(TObject *Sender, TShiftState Shift,
  99. int X, int Y)
  100. {
  101. if (Shift.Contains(ssLeft))
  102. Camera->MoveAroundTarget(my-Y, mx-X);
  103. mx = X; my = Y;
  104. }
  105. //---------------------------------------------------------------------------
  106. void __fastcall TFormMotionBlur::Timer1Timer(TObject *Sender)
  107. {
  108. // const String cVSync[vsmSync][vsmNoSync] = {"VSync ON", "VSync OFF"};
  109. Panel1->Caption = Format("Motion Blur on %d Frames %f FPS",
  110. ARRAYOFCONST ((Frames, GLSceneViewer->FramesPerSecond())));
  111. GLSceneViewer->ResetPerformanceMonitor();
  112. }
  113. //---------------------------------------------------------------------------