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