fdPanoViewer.pas 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. unit fdPanoViewer;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. Winapi.Windows,
  6. System.SysUtils,
  7. System.Classes,
  8. System.Math,
  9. Vcl.Graphics,
  10. Vcl.Controls,
  11. Vcl.Forms,
  12. Vcl.Dialogs,
  13. Vcl.ComCtrls,
  14. Vcl.StdCtrls,
  15. Vcl.ExtCtrls,
  16. Vcl.ExtDlgs,
  17. Vcl.Imaging.Jpeg,
  18. GLS.Coordinates,
  19. GLS.BaseClasses,
  20. Stage.VectorGeometry,
  21. Stage.Keyboard,
  22. GLS.XCollection,
  23. GLS.Scene,
  24. GLS.Objects,
  25. GLS.Texture,
  26. GLS.Cadencer,
  27. GLS.SceneViewer,
  28. GLS.Material,
  29. Stage.Utils;
  30. type
  31. TFormPamorama = class(TForm)
  32. GLSceneViewer1: TGLSceneViewer;
  33. GLScene1: TGLScene;
  34. Panel1: TPanel;
  35. Camera: TGLCamera;
  36. BtnLoad: TButton;
  37. TrackBarFocal: TTrackBar;
  38. LabelYaw: TLabel;
  39. LabelPitch: TLabel;
  40. OpenPictureDialog1: TOpenPictureDialog;
  41. Label1: TLabel;
  42. SpherePano: TGLSphere;
  43. GLMaterialLibrary1: TGLMaterialLibrary;
  44. Label2: TLabel;
  45. GLCadencer1: TGLCadencer;
  46. procedure GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  47. Shift: TShiftState; X, Y: Integer);
  48. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  49. X, Y: Integer);
  50. procedure BtnLoadClick(Sender: TObject);
  51. procedure TrackBarFocalChange(Sender: TObject);
  52. procedure FormMouseWheel(Sender: TObject; Shift: TShiftState;
  53. WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  54. procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  55. procedure GLCadencer1Progress(Sender: TObject;
  56. const deltaTime, newTime: Double);
  57. procedure FormCreate(Sender: TObject);
  58. private
  59. Path: TFileName;
  60. mx, my: Integer;
  61. pitch, yaw: single; // in degrees
  62. procedure PanCameraAround(dx, dy: single);
  63. public
  64. end;
  65. var
  66. FormPamorama: TFormPamorama;
  67. implementation //============================================================
  68. {$R *.DFM}
  69. //---------------------------------------------------------------------------
  70. procedure TFormPamorama.FormCreate(Sender: TObject);
  71. begin
  72. Path := GetCurrentAssetPath(); // or Path := ExtractFilePath(ParamStr(0));
  73. SetCurrentDir(Path + '\panorana'); // GetDir(0, Path);
  74. OpenPictureDialog1.InitialDir := Path + '\panorama';
  75. OpenPictureDialog1.FileName := 'sejourstmathieu2048.jpg';
  76. end;
  77. //---------------------------------------------------------------------------
  78. procedure TFormPamorama.GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  79. Shift: TShiftState; X, Y: Integer);
  80. begin
  81. mx := X;
  82. my := Y;
  83. end;
  84. //---------------------------------------------------------------------------
  85. procedure TFormPamorama.GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  86. X, Y: Integer);
  87. var
  88. dx, dy, f: single;
  89. begin
  90. if Shift = [ssLeft] then
  91. begin
  92. f := 0.2 * 40 / Camera.FocalLength;
  93. dx := (X - mx) * f;
  94. dy := (Y - my) * f;
  95. PanCameraAround(dx, dy);
  96. end;
  97. mx := X;
  98. my := Y;
  99. end;
  100. //---------------------------------------------------------------------------
  101. procedure TFormPamorama.BtnLoadClick(Sender: TObject);
  102. begin
  103. with OpenPictureDialog1 do
  104. if Execute then
  105. GLMaterialLibrary1.Materials[0].Material.Texture.Image.LoadFromFile
  106. (FileName);
  107. end;
  108. //---------------------------------------------------------------------------
  109. procedure TFormPamorama.TrackBarFocalChange(Sender: TObject);
  110. begin
  111. Camera.FocalLength := TrackBarFocal.Position;
  112. end;
  113. //---------------------------------------------------------------------------
  114. procedure TFormPamorama.FormMouseWheel(Sender: TObject; Shift: TShiftState;
  115. WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  116. begin
  117. TrackBarFocal.Position := TrackBarFocal.Position + Round(2 * WheelDelta / 120);
  118. end;
  119. //---------------------------------------------------------------------------
  120. procedure TFormPamorama.GLCadencer1Progress(Sender: TObject;
  121. const deltaTime, newTime: Double);
  122. const
  123. step_size = 20;
  124. var
  125. delta: single;
  126. dx, dy: single;
  127. begin
  128. delta := step_size * 40 / Camera.FocalLength * deltaTime;
  129. dx := 0;
  130. dy := 0;
  131. if IsKeyDown(VK_LEFT) then
  132. dx := dx + delta;
  133. if IsKeyDown(VK_UP) then
  134. dy := dy + delta;
  135. if IsKeyDown(VK_RIGHT) then
  136. dx := dx - delta;
  137. if IsKeyDown(VK_DOWN) then
  138. dy := dy - delta;
  139. PanCameraAround(dx, dy);
  140. end;
  141. //---------------------------------------------------------------------------
  142. procedure TFormPamorama.FormKeyDown(Sender: TObject; var Key: Word;
  143. Shift: TShiftState);
  144. begin
  145. Key := 0; // all keys handled by Form1
  146. end;
  147. //---------------------------------------------------------------------------
  148. procedure TFormPamorama.PanCameraAround(dx, dy: single);
  149. begin
  150. pitch := pitch + dy;
  151. yaw := yaw - dx;
  152. if pitch > 90 then
  153. pitch := 90;
  154. if pitch < -90 then
  155. pitch := -90;
  156. if yaw > 360 then
  157. yaw := yaw - 360;
  158. if yaw < 0 then
  159. yaw := yaw + 360;
  160. Camera.Up.SetVector(0, 1, 0);
  161. Camera.Direction.SetVector(sin(DegToRad(yaw)), sin(DegToRad(pitch)),
  162. -cos(DegToRad(yaw)));
  163. LabelPitch.caption := format('Pitch: %3f', [pitch]);
  164. LabelYaw.caption := format('Yaw: %3f', [yaw]);
  165. end;
  166. end.