Unit1.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #include <math.h>
  5. #pragma hdrstop
  6. #include "Unit1.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma link "GLS.Objects"
  10. #pragma link "GLS.Scene"
  11. #pragma link "GLS.Texture"
  12. #pragma link "GLS.SceneViewer"
  13. #pragma link "GLS.BaseClasses"
  14. #pragma link "GLS.Coordinates"
  15. #pragma link "GLS.Material"
  16. #pragma link "GLS.KeyBoard"
  17. #pragma resource "*.dfm"
  18. TForm1 *Form1;
  19. //---------------------------------------------------------------------------
  20. __fastcall TForm1::TForm1(TComponent* Owner)
  21. : TForm(Owner)
  22. {
  23. }
  24. //---------------------------------------------------------------------------
  25. void __fastcall TForm1::BtnLoadClick(TObject *Sender)
  26. {
  27. // SetGLSceneMediaDir();
  28. OpenPictureDialog1->InitialDir = ExtractFilePath(ParamStr(0));
  29. OpenPictureDialog1->FileName = "sejourstmathieu2048.jpg";
  30. if (OpenPictureDialog1->Execute())
  31. GLMaterialLibrary1->Materials->Items[0]->Material->
  32. Texture->Image->LoadFromFile(OpenPictureDialog1->FileName);
  33. }
  34. //---------------------------------------------------------------------------
  35. void __fastcall TForm1::TrackBar1Change(TObject *Sender)
  36. {
  37. GLCamera1->FocalLength = TrackBar1->Position;
  38. }
  39. //---------------------------------------------------------------------------
  40. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender,
  41. const double deltaTime, const double newTime)
  42. {
  43. const int step_size = 20;
  44. float delta;
  45. float dx, dy;
  46. delta = step_size * 40/GLCamera1->FocalLength * deltaTime;
  47. dx = 0;
  48. dy = 0;
  49. if (IsKeyDown(VK_LEFT) ) dx = dx+delta;
  50. if (IsKeyDown(VK_UP) ) dy = dy+delta;
  51. if (IsKeyDown(VK_RIGHT)) dx = dx-delta;
  52. if (IsKeyDown(VK_DOWN) ) dy = dy-delta;
  53. PanCameraAround(dx, dy);
  54. }
  55. //---------------------------------------------------------------------------
  56. void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
  57. TShiftState Shift)
  58. {
  59. Key = 0; // all keys handled by Form1
  60. }
  61. //---------------------------------------------------------------------------
  62. void TForm1::PanCameraAround(float dx, float dy)
  63. {
  64. pitch = pitch+dy;
  65. yaw = yaw-dx;
  66. if (pitch>90) pitch = 90;
  67. if (pitch<-90) pitch = -90;
  68. if (yaw>360) yaw = yaw-360;
  69. if (yaw<0) yaw = yaw+360;
  70. GLCamera1->Up->SetVector(0, 1, 0);
  71. GLCamera1->Direction->SetVector( sin(DegToRad(yaw)),
  72. sin(DegToRad(pitch)),
  73. -cos(DegToRad(yaw)));
  74. LabelPitch->Caption = Format("Pitch: %3f", ARRAYOFCONST((pitch)));
  75. LabelYaw->Caption = Format("Yaw: %3f", ARRAYOFCONST((yaw)));
  76. }
  77. //---------------------------------------------------------------------------
  78. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject *Sender,
  79. TMouseButton Button, TShiftState Shift, int X, int Y)
  80. {
  81. mx = X;
  82. my = Y;
  83. }
  84. //---------------------------------------------------------------------------
  85. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender,
  86. TShiftState Shift, int X, int Y)
  87. {
  88. float dx, dy, f;
  89. if (Shift.Contains(ssLeft))
  90. {
  91. f = 0.2*40/GLCamera1->FocalLength;
  92. dx = (X-mx)*f;
  93. dy = (Y-my)*f;
  94. PanCameraAround(dx, dy);
  95. }
  96. mx = X;
  97. my = Y;
  98. }
  99. //---------------------------------------------------------------------------
  100. //---------------------------------------------------------------------------
  101. void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
  102. TPoint &MousePos, bool &Handled)
  103. {
  104. TrackBar1->Position = TrackBar1->Position+(int)(2*WheelDelta/120);
  105. }
  106. //---------------------------------------------------------------------------